2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
31 #include "gstbasesrc.h"
32 #include "gsttypefindhelper.h"
33 #include <gst/gstmarshal.h>
35 #define DEFAULT_BLOCKSIZE 4096
37 GST_DEBUG_CATEGORY_STATIC (gst_basesrc_debug);
38 #define GST_CAT_DEFAULT gst_basesrc_debug
40 /* BaseSrc signals and args */
55 static GstElementClass *parent_class = NULL;
57 static void gst_basesrc_base_init (gpointer g_class);
58 static void gst_basesrc_class_init (GstBaseSrcClass * klass);
59 static void gst_basesrc_init (GstBaseSrc * src, gpointer g_class);
62 gst_basesrc_get_type (void)
64 static GType basesrc_type = 0;
67 static const GTypeInfo basesrc_info = {
68 sizeof (GstBaseSrcClass),
69 (GBaseInitFunc) gst_basesrc_base_init,
71 (GClassInitFunc) gst_basesrc_class_init,
76 (GInstanceInitFunc) gst_basesrc_init,
79 basesrc_type = g_type_register_static (GST_TYPE_ELEMENT,
80 "GstBaseSrc", &basesrc_info, G_TYPE_FLAG_ABSTRACT);
85 static gboolean gst_basesrc_activate (GstPad * pad, GstActivateMode mode);
86 static void gst_basesrc_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_basesrc_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
90 static gboolean gst_basesrc_event_handler (GstPad * pad, GstEvent * event);
91 static const GstEventMask *gst_basesrc_get_event_mask (GstPad * pad);
92 static const GstQueryType *gst_basesrc_get_query_types (GstPad * pad);
93 static gboolean gst_basesrc_query (GstPad * pad, GstQueryType type,
94 GstFormat * format, gint64 * value);
95 static const GstFormat *gst_basesrc_get_formats (GstPad * pad);
97 static gboolean gst_basesrc_unlock (GstBaseSrc * basesrc);
98 static gboolean gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size);
99 static gboolean gst_basesrc_start (GstBaseSrc * basesrc);
100 static gboolean gst_basesrc_stop (GstBaseSrc * basesrc);
102 static GstElementStateReturn gst_basesrc_change_state (GstElement * element);
104 static void gst_basesrc_set_dataflow_funcs (GstBaseSrc * this);
105 static void gst_basesrc_loop (GstPad * pad);
106 static gboolean gst_basesrc_check_get_range (GstPad * pad);
107 static GstFlowReturn gst_basesrc_get_range (GstPad * pad, guint64 offset,
108 guint length, GstBuffer ** buf);
111 gst_basesrc_base_init (gpointer g_class)
113 GST_DEBUG_CATEGORY_INIT (gst_basesrc_debug, "basesrc", 0, "basesrc element");
117 gst_basesrc_class_init (GstBaseSrcClass * klass)
119 GObjectClass *gobject_class;
120 GstElementClass *gstelement_class;
122 gobject_class = (GObjectClass *) klass;
123 gstelement_class = (GstElementClass *) klass;
125 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
127 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_basesrc_set_property);
128 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_basesrc_get_property);
130 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
131 g_param_spec_ulong ("blocksize", "Block size",
132 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
135 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
136 g_param_spec_boolean ("has-loop", "Has loop function",
137 "True if the element should expose a loop function", TRUE,
138 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
140 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
141 g_param_spec_boolean ("has-getrange", "Has getrange function",
142 "True if the element should expose a getrange function", TRUE,
143 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
145 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_basesrc_change_state);
149 gst_basesrc_init (GstBaseSrc * basesrc, gpointer g_class)
152 GstPadTemplate *pad_template;
155 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
156 g_return_if_fail (pad_template != NULL);
158 pad = gst_pad_new_from_template (pad_template, "src");
160 gst_pad_set_activate_function (pad, gst_basesrc_activate);
161 gst_pad_set_event_function (pad, gst_basesrc_event_handler);
162 gst_pad_set_event_mask_function (pad, gst_basesrc_get_event_mask);
163 gst_pad_set_query_function (pad, gst_basesrc_query);
164 gst_pad_set_query_type_function (pad, gst_basesrc_get_query_types);
165 gst_pad_set_formats_function (pad, gst_basesrc_get_formats);
166 gst_pad_set_checkgetrange_function (pad, gst_basesrc_check_get_range);
167 /* hold ref to pad */
168 basesrc->srcpad = pad;
169 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
171 basesrc->segment_start = -1;
172 basesrc->segment_end = -1;
173 basesrc->blocksize = DEFAULT_BLOCKSIZE;
174 basesrc->clock_id = NULL;
176 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
180 gst_basesrc_set_dataflow_funcs (GstBaseSrc * this)
182 GST_DEBUG ("updating dataflow functions");
185 gst_pad_set_loop_function (this->srcpad, gst_basesrc_loop);
187 gst_pad_set_loop_function (this->srcpad, NULL);
189 if (this->has_getrange)
190 gst_pad_set_getrange_function (this->srcpad, gst_basesrc_get_range);
192 gst_pad_set_getrange_function (this->srcpad, NULL);
195 static const GstFormat *
196 gst_basesrc_get_formats (GstPad * pad)
198 static const GstFormat formats[] = {
207 static const GstQueryType *
208 gst_basesrc_get_query_types (GstPad * pad)
210 static const GstQueryType types[] = {
214 GST_QUERY_SEGMENT_END,
222 gst_basesrc_query (GstPad * pad, GstQueryType type,
223 GstFormat * format, gint64 * value)
225 GstBaseSrc *src = GST_BASESRC (GST_PAD_PARENT (pad));
227 if (*format == GST_FORMAT_DEFAULT)
228 *format = GST_FORMAT_BYTES;
231 case GST_QUERY_TOTAL:
233 case GST_FORMAT_BYTES:
237 ret = gst_basesrc_get_size (src, (guint64 *) value);
238 GST_DEBUG ("getting length %d %lld", ret, *value);
241 case GST_FORMAT_PERCENT:
242 *value = GST_FORMAT_PERCENT_MAX;
248 case GST_QUERY_POSITION:
250 case GST_FORMAT_BYTES:
251 *value = src->offset;
253 case GST_FORMAT_PERCENT:
255 if (!gst_basesrc_get_size (src, (guint64 *) value))
257 *value = src->offset * GST_FORMAT_PERCENT_MAX / *value;
263 case GST_QUERY_START:
264 *value = src->segment_start;
266 case GST_QUERY_SEGMENT_END:
267 *value = src->segment_end;
275 static const GstEventMask *
276 gst_basesrc_get_event_mask (GstPad * pad)
278 static const GstEventMask masks[] = {
279 {GST_EVENT_SEEK, GST_SEEK_METHOD_CUR | GST_SEEK_METHOD_SET |
280 GST_SEEK_METHOD_END | GST_SEEK_FLAG_FLUSH |
281 GST_SEEK_FLAG_SEGMENT_LOOP},
282 {GST_EVENT_FLUSH, 0},
290 gst_basesrc_do_seek (GstBaseSrc * src, GstEvent * event)
295 format = GST_EVENT_SEEK_FORMAT (event);
297 /* get seek format */
298 if (format == GST_FORMAT_DEFAULT)
299 format = GST_FORMAT_BYTES;
300 /* we can only seek bytes */
301 if (format != GST_FORMAT_BYTES)
304 /* get seek positions */
305 offset = GST_EVENT_SEEK_OFFSET (event);
306 src->segment_start = offset;
307 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
308 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
310 /* send flush start */
311 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
313 /* unblock streaming thread */
314 gst_basesrc_unlock (src);
316 /* grab streaming lock */
317 GST_STREAM_LOCK (src->srcpad);
319 switch (GST_EVENT_SEEK_METHOD (event)) {
320 case GST_SEEK_METHOD_SET:
323 src->offset = MIN (offset, src->size);
324 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
327 case GST_SEEK_METHOD_CUR:
328 offset += src->offset;
329 src->offset = CLAMP (offset, 0, src->size);
330 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
333 case GST_SEEK_METHOD_END:
336 offset = src->size + offset;
337 src->offset = MAX (0, offset);
338 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
345 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
347 /* now send discont */
351 event = gst_event_new_discontinuous (1.0,
353 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
355 gst_pad_push_event (src->srcpad, event);
358 /* and restart the task */
359 if (GST_RPAD_TASK (src->srcpad)) {
360 gst_task_start (GST_RPAD_TASK (src->srcpad));
362 GST_STREAM_UNLOCK (src->srcpad);
364 gst_event_unref (event);
371 GST_DEBUG_OBJECT (src, "seek error");
372 GST_STREAM_UNLOCK (src->srcpad);
373 gst_event_unref (event);
379 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
382 GstBaseSrcClass *bclass;
385 src = GST_BASESRC (GST_PAD_PARENT (pad));
386 bclass = GST_BASESRC_GET_CLASS (src);
389 result = bclass->event (src, event);
391 switch (GST_EVENT_TYPE (event)) {
393 return gst_basesrc_do_seek (src, event);
398 format = GST_EVENT_SIZE_FORMAT (event);
399 if (format == GST_FORMAT_DEFAULT)
400 format = GST_FORMAT_BYTES;
401 /* we can only accept bytes */
402 if (format != GST_FORMAT_BYTES)
405 src->blocksize = GST_EVENT_SIZE_VALUE (event);
406 g_object_notify (G_OBJECT (src), "blocksize");
409 case GST_EVENT_FLUSH:
410 /* cancel any blocking getrange */
411 if (!GST_EVENT_FLUSH_DONE (event))
412 gst_basesrc_unlock (src);
417 gst_event_unref (event);
423 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
428 src = GST_BASESRC (object);
432 src->blocksize = g_value_get_ulong (value);
435 src->has_loop = g_value_get_boolean (value);
436 gst_basesrc_set_dataflow_funcs (src);
438 case PROP_HAS_GETRANGE:
439 src->has_getrange = g_value_get_boolean (value);
440 gst_basesrc_set_dataflow_funcs (src);
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
454 src = GST_BASESRC (object);
458 g_value_set_ulong (value, src->blocksize);
461 g_value_set_boolean (value, src->has_loop);
463 case PROP_HAS_GETRANGE:
464 g_value_set_boolean (value, src->has_getrange);
467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
473 gst_basesrc_get_range_unlocked (GstPad * pad, guint64 offset, guint length,
478 GstBaseSrcClass *bclass;
480 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
481 bclass = GST_BASESRC_GET_CLASS (src);
483 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
490 if (src->size != -1) {
491 if (offset + length > src->size) {
492 if (bclass->get_size)
493 bclass->get_size (src, &src->size);
495 if (offset + length > src->size) {
496 length = src->size - offset;
501 goto unexpected_length;
503 ret = bclass->create (src, offset, length, buf);
510 GST_DEBUG_OBJECT (src, "getrange but not started");
511 return GST_FLOW_WRONG_STATE;
515 GST_DEBUG_OBJECT (src, "no create function");
516 return GST_FLOW_ERROR;
520 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
521 return GST_FLOW_UNEXPECTED;
526 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
531 GST_STREAM_LOCK (pad);
533 fret = gst_basesrc_get_range_unlocked (pad, offset, length, ret);
535 GST_STREAM_UNLOCK (pad);
541 gst_basesrc_check_get_range (GstPad * pad)
545 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
547 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
548 gst_basesrc_start (src);
549 gst_basesrc_stop (src);
552 return src->seekable;
556 gst_basesrc_loop (GstPad * pad)
559 GstBuffer *buf = NULL;
562 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
564 GST_STREAM_LOCK (pad);
566 ret = gst_basesrc_get_range_unlocked (pad, src->offset, src->blocksize, &buf);
567 if (ret != GST_FLOW_OK)
570 src->offset += GST_BUFFER_SIZE (buf);
572 ret = gst_pad_push (pad, buf);
573 if (ret != GST_FLOW_OK)
576 GST_STREAM_UNLOCK (pad);
581 GST_DEBUG_OBJECT (src, "going to EOS");
582 gst_task_pause (GST_RPAD_TASK (pad));
583 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
584 GST_STREAM_UNLOCK (pad);
589 GST_DEBUG_OBJECT (src, "pausing task");
590 gst_task_pause (GST_RPAD_TASK (pad));
591 GST_STREAM_UNLOCK (pad);
597 gst_basesrc_unlock (GstBaseSrc * basesrc)
599 GstBaseSrcClass *bclass;
600 gboolean result = FALSE;
602 /* unblock whatever the subclass is doing */
603 bclass = GST_BASESRC_GET_CLASS (basesrc);
605 result = bclass->unlock (basesrc);
607 /* and unblock the clock as well, if any */
609 if (basesrc->clock_id) {
610 gst_clock_id_unschedule (basesrc->clock_id);
612 GST_UNLOCK (basesrc);
618 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
620 GstBaseSrcClass *bclass;
621 gboolean result = FALSE;
623 bclass = GST_BASESRC_GET_CLASS (basesrc);
624 if (bclass->get_size)
625 result = bclass->get_size (basesrc, size);
628 basesrc->size = *size;
634 gst_basesrc_start (GstBaseSrc * basesrc)
636 GstBaseSrcClass *bclass;
639 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
642 bclass = GST_BASESRC_GET_CLASS (basesrc);
644 result = bclass->start (basesrc);
649 goto could_not_start;
651 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
653 /* start in the beginning */
655 basesrc->segment_start = 0;
657 /* figure out the size */
658 if (bclass->get_size) {
659 result = bclass->get_size (basesrc, &basesrc->size);
667 GST_DEBUG ("size %d %lld", result, basesrc->size);
669 /* we always run to the end */
670 basesrc->segment_end = -1;
672 /* check if we can seek */
673 if (bclass->is_seekable)
674 basesrc->seekable = bclass->is_seekable (basesrc);
676 basesrc->seekable = FALSE;
680 if (basesrc->seekable) {
683 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
684 gst_pad_set_caps (basesrc->srcpad, caps);
693 GST_DEBUG_OBJECT (basesrc, "could not start");
699 gst_basesrc_stop (GstBaseSrc * basesrc)
701 GstBaseSrcClass *bclass;
702 gboolean result = TRUE;
704 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
707 bclass = GST_BASESRC_GET_CLASS (basesrc);
709 result = bclass->stop (basesrc);
712 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
718 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
723 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
725 /* prepare subclass first */
727 case GST_ACTIVATE_PUSH:
728 case GST_ACTIVATE_PULL:
729 result = gst_basesrc_start (basesrc);
735 /* if that failed we can stop here */
741 case GST_ACTIVATE_PUSH:
742 /* if we have a scheduler we can start the task */
743 if (GST_ELEMENT_SCHEDULER (basesrc)) {
744 GST_STREAM_LOCK (pad);
745 GST_RPAD_TASK (pad) =
746 gst_scheduler_create_task (GST_ELEMENT_SCHEDULER (basesrc),
747 (GstTaskFunction) gst_basesrc_loop, pad);
749 gst_task_start (GST_RPAD_TASK (pad));
750 GST_STREAM_UNLOCK (pad);
754 case GST_ACTIVATE_PULL:
757 case GST_ACTIVATE_NONE:
758 /* step 1, unblock clock sync (if any) */
759 gst_basesrc_unlock (basesrc);
761 /* step 2, make sure streaming finishes */
762 GST_STREAM_LOCK (pad);
763 /* step 3, stop the task */
764 if (GST_RPAD_TASK (pad)) {
765 gst_task_stop (GST_RPAD_TASK (pad));
766 gst_object_unref (GST_OBJECT (GST_RPAD_TASK (pad)));
767 GST_RPAD_TASK (pad) = NULL;
769 GST_STREAM_UNLOCK (pad);
779 GST_DEBUG_OBJECT (basesrc, "failed to start");
784 static GstElementStateReturn
785 gst_basesrc_change_state (GstElement * element)
788 GstElementStateReturn result = GST_STATE_FAILURE;
789 GstElementState transition;
791 basesrc = GST_BASESRC (element);
793 transition = GST_STATE_TRANSITION (element);
795 switch (transition) {
796 case GST_STATE_NULL_TO_READY:
798 case GST_STATE_READY_TO_PAUSED:
800 case GST_STATE_PAUSED_TO_PLAYING:
806 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
808 switch (transition) {
809 case GST_STATE_PLAYING_TO_PAUSED:
811 case GST_STATE_PAUSED_TO_READY:
812 result = gst_basesrc_stop (basesrc);
814 case GST_STATE_READY_TO_NULL: