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);
92 static gboolean gst_basesrc_query2 (GstPad * pad, GstQuery * query);
94 static const GstEventMask *gst_basesrc_get_event_mask (GstPad * pad);
96 static gboolean gst_basesrc_unlock (GstBaseSrc * basesrc);
97 static gboolean gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size);
98 static gboolean gst_basesrc_start (GstBaseSrc * basesrc);
99 static gboolean gst_basesrc_stop (GstBaseSrc * basesrc);
101 static GstElementStateReturn gst_basesrc_change_state (GstElement * element);
103 static void gst_basesrc_set_dataflow_funcs (GstBaseSrc * this);
104 static void gst_basesrc_loop (GstPad * pad);
105 static gboolean gst_basesrc_check_get_range (GstPad * pad);
106 static GstFlowReturn gst_basesrc_get_range (GstPad * pad, guint64 offset,
107 guint length, GstBuffer ** buf);
110 gst_basesrc_base_init (gpointer g_class)
112 GST_DEBUG_CATEGORY_INIT (gst_basesrc_debug, "basesrc", 0, "basesrc element");
116 gst_basesrc_class_init (GstBaseSrcClass * klass)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
121 gobject_class = (GObjectClass *) klass;
122 gstelement_class = (GstElementClass *) klass;
124 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
126 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_basesrc_set_property);
127 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_basesrc_get_property);
129 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
130 g_param_spec_ulong ("blocksize", "Block size",
131 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
134 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
135 g_param_spec_boolean ("has-loop", "Has loop function",
136 "True if the element should expose a loop function", TRUE,
137 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
139 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
140 g_param_spec_boolean ("has-getrange", "Has getrange function",
141 "True if the element should expose a getrange function", TRUE,
142 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
144 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_basesrc_change_state);
148 gst_basesrc_init (GstBaseSrc * basesrc, gpointer g_class)
151 GstPadTemplate *pad_template;
154 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
155 g_return_if_fail (pad_template != NULL);
157 pad = gst_pad_new_from_template (pad_template, "src");
159 gst_pad_set_activate_function (pad, gst_basesrc_activate);
160 gst_pad_set_event_function (pad, gst_basesrc_event_handler);
161 gst_pad_set_event_mask_function (pad, gst_basesrc_get_event_mask);
163 gst_pad_set_query2_function (pad, gst_basesrc_query2);
165 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);
196 gst_basesrc_query2 (GstPad * pad, GstQuery * query)
203 src = GST_BASESRC (GST_PAD_PARENT (pad));
205 switch (GST_QUERY_TYPE (query)) {
206 case GST_QUERY_POSITION:
210 gst_query_parse_position_query (query, &format);
212 case GST_FORMAT_DEFAULT:
213 case GST_FORMAT_BYTES:
214 b = gst_basesrc_get_size (src, &ui64);
215 /* better to make get_size take an int64 */
216 i64 = b ? (gint64) ui64 : -1;
217 gst_query_set_position (query, GST_FORMAT_BYTES, src->offset, i64);
219 case GST_FORMAT_PERCENT:
220 b = gst_basesrc_get_size (src, &ui64);
221 i64 = GST_FORMAT_PERCENT_MAX;
222 i64 *= b ? (src->offset / (gdouble) ui64) : 1.0;
223 gst_query_set_position (query, GST_FORMAT_PERCENT,
224 i64, GST_FORMAT_PERCENT_MAX);
231 case GST_QUERY_SEEKING:
232 gst_query_set_seeking (query, GST_FORMAT_BYTES,
233 src->seekable, src->segment_start, src->segment_end);
236 case GST_QUERY_FORMATS:
237 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
238 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
241 case GST_QUERY_LATENCY:
242 case GST_QUERY_JITTER:
244 case GST_QUERY_CONVERT:
246 return gst_pad_query2_default (pad, query);
250 static const GstEventMask *
251 gst_basesrc_get_event_mask (GstPad * pad)
253 static const GstEventMask masks[] = {
254 {GST_EVENT_SEEK, GST_SEEK_METHOD_CUR | GST_SEEK_METHOD_SET |
255 GST_SEEK_METHOD_END | GST_SEEK_FLAG_FLUSH |
256 GST_SEEK_FLAG_SEGMENT_LOOP},
257 {GST_EVENT_FLUSH, 0},
265 gst_basesrc_do_seek (GstBaseSrc * src, GstEvent * event)
270 format = GST_EVENT_SEEK_FORMAT (event);
272 /* get seek format */
273 if (format == GST_FORMAT_DEFAULT)
274 format = GST_FORMAT_BYTES;
275 /* we can only seek bytes */
276 if (format != GST_FORMAT_BYTES)
279 /* get seek positions */
280 offset = GST_EVENT_SEEK_OFFSET (event);
281 src->segment_start = offset;
282 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
283 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
285 /* send flush start */
286 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
288 /* unblock streaming thread */
289 gst_basesrc_unlock (src);
291 /* grab streaming lock */
292 GST_STREAM_LOCK (src->srcpad);
294 switch (GST_EVENT_SEEK_METHOD (event)) {
295 case GST_SEEK_METHOD_SET:
298 src->offset = MIN (offset, src->size);
299 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
302 case GST_SEEK_METHOD_CUR:
303 offset += src->offset;
304 src->offset = CLAMP (offset, 0, src->size);
305 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
308 case GST_SEEK_METHOD_END:
311 offset = src->size + offset;
312 src->offset = MAX (0, offset);
313 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
320 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
322 /* now send discont */
326 event = gst_event_new_discontinuous (1.0,
328 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
330 gst_pad_push_event (src->srcpad, event);
333 /* and restart the task */
334 if (GST_RPAD_TASK (src->srcpad)) {
335 gst_task_start (GST_RPAD_TASK (src->srcpad));
337 GST_STREAM_UNLOCK (src->srcpad);
339 gst_event_unref (event);
346 GST_DEBUG_OBJECT (src, "seek error");
347 GST_STREAM_UNLOCK (src->srcpad);
348 gst_event_unref (event);
354 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
357 GstBaseSrcClass *bclass;
360 src = GST_BASESRC (GST_PAD_PARENT (pad));
361 bclass = GST_BASESRC_GET_CLASS (src);
364 result = bclass->event (src, event);
366 switch (GST_EVENT_TYPE (event)) {
368 return gst_basesrc_do_seek (src, event);
373 format = GST_EVENT_SIZE_FORMAT (event);
374 if (format == GST_FORMAT_DEFAULT)
375 format = GST_FORMAT_BYTES;
376 /* we can only accept bytes */
377 if (format != GST_FORMAT_BYTES)
380 src->blocksize = GST_EVENT_SIZE_VALUE (event);
381 g_object_notify (G_OBJECT (src), "blocksize");
384 case GST_EVENT_FLUSH:
385 /* cancel any blocking getrange */
386 if (!GST_EVENT_FLUSH_DONE (event))
387 gst_basesrc_unlock (src);
392 gst_event_unref (event);
398 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
403 src = GST_BASESRC (object);
407 src->blocksize = g_value_get_ulong (value);
410 src->has_loop = g_value_get_boolean (value);
411 gst_basesrc_set_dataflow_funcs (src);
413 case PROP_HAS_GETRANGE:
414 src->has_getrange = g_value_get_boolean (value);
415 gst_basesrc_set_dataflow_funcs (src);
418 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
424 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
429 src = GST_BASESRC (object);
433 g_value_set_ulong (value, src->blocksize);
436 g_value_set_boolean (value, src->has_loop);
438 case PROP_HAS_GETRANGE:
439 g_value_set_boolean (value, src->has_getrange);
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 gst_basesrc_get_range_unlocked (GstPad * pad, guint64 offset, guint length,
453 GstBaseSrcClass *bclass;
455 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
456 bclass = GST_BASESRC_GET_CLASS (src);
458 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
465 if (src->size != -1) {
466 if (offset + length > src->size) {
467 if (bclass->get_size)
468 bclass->get_size (src, &src->size);
470 if (offset + length > src->size) {
471 length = src->size - offset;
476 goto unexpected_length;
478 ret = bclass->create (src, offset, length, buf);
485 GST_DEBUG_OBJECT (src, "getrange but not started");
486 return GST_FLOW_WRONG_STATE;
490 GST_DEBUG_OBJECT (src, "no create function");
491 return GST_FLOW_ERROR;
495 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
496 return GST_FLOW_UNEXPECTED;
501 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
506 GST_STREAM_LOCK (pad);
508 fret = gst_basesrc_get_range_unlocked (pad, offset, length, ret);
510 GST_STREAM_UNLOCK (pad);
516 gst_basesrc_check_get_range (GstPad * pad)
520 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
522 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
523 gst_basesrc_start (src);
524 gst_basesrc_stop (src);
527 return src->seekable;
531 gst_basesrc_loop (GstPad * pad)
534 GstBuffer *buf = NULL;
537 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
539 GST_STREAM_LOCK (pad);
541 ret = gst_basesrc_get_range_unlocked (pad, src->offset, src->blocksize, &buf);
542 if (ret != GST_FLOW_OK)
545 src->offset += GST_BUFFER_SIZE (buf);
547 ret = gst_pad_push (pad, buf);
548 if (ret != GST_FLOW_OK)
551 GST_STREAM_UNLOCK (pad);
556 GST_DEBUG_OBJECT (src, "going to EOS");
557 gst_task_pause (GST_RPAD_TASK (pad));
558 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
559 GST_STREAM_UNLOCK (pad);
564 GST_DEBUG_OBJECT (src, "pausing task");
565 gst_task_pause (GST_RPAD_TASK (pad));
566 GST_STREAM_UNLOCK (pad);
572 gst_basesrc_unlock (GstBaseSrc * basesrc)
574 GstBaseSrcClass *bclass;
575 gboolean result = FALSE;
577 /* unblock whatever the subclass is doing */
578 bclass = GST_BASESRC_GET_CLASS (basesrc);
580 result = bclass->unlock (basesrc);
582 /* and unblock the clock as well, if any */
584 if (basesrc->clock_id) {
585 gst_clock_id_unschedule (basesrc->clock_id);
587 GST_UNLOCK (basesrc);
593 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
595 GstBaseSrcClass *bclass;
596 gboolean result = FALSE;
598 bclass = GST_BASESRC_GET_CLASS (basesrc);
599 if (bclass->get_size)
600 result = bclass->get_size (basesrc, size);
603 basesrc->size = *size;
609 gst_basesrc_start (GstBaseSrc * basesrc)
611 GstBaseSrcClass *bclass;
614 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
617 bclass = GST_BASESRC_GET_CLASS (basesrc);
619 result = bclass->start (basesrc);
624 goto could_not_start;
626 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
628 /* start in the beginning */
630 basesrc->segment_start = 0;
632 /* figure out the size */
633 if (bclass->get_size) {
634 result = bclass->get_size (basesrc, &basesrc->size);
642 GST_DEBUG ("size %d %lld", result, basesrc->size);
644 /* we always run to the end */
645 basesrc->segment_end = -1;
647 /* check if we can seek */
648 if (bclass->is_seekable)
649 basesrc->seekable = bclass->is_seekable (basesrc);
651 basesrc->seekable = FALSE;
655 if (basesrc->seekable) {
658 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
659 gst_pad_set_caps (basesrc->srcpad, caps);
668 GST_DEBUG_OBJECT (basesrc, "could not start");
674 gst_basesrc_stop (GstBaseSrc * basesrc)
676 GstBaseSrcClass *bclass;
677 gboolean result = TRUE;
679 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
682 bclass = GST_BASESRC_GET_CLASS (basesrc);
684 result = bclass->stop (basesrc);
687 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
693 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
698 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
700 /* prepare subclass first */
702 case GST_ACTIVATE_PUSH:
703 case GST_ACTIVATE_PULL:
704 result = gst_basesrc_start (basesrc);
710 /* if that failed we can stop here */
716 case GST_ACTIVATE_PUSH:
717 /* if we have a scheduler we can start the task */
718 if (GST_ELEMENT_SCHEDULER (basesrc)) {
719 GST_STREAM_LOCK (pad);
720 GST_RPAD_TASK (pad) =
721 gst_scheduler_create_task (GST_ELEMENT_SCHEDULER (basesrc),
722 (GstTaskFunction) gst_basesrc_loop, pad);
724 gst_task_start (GST_RPAD_TASK (pad));
725 GST_STREAM_UNLOCK (pad);
729 case GST_ACTIVATE_PULL:
732 case GST_ACTIVATE_NONE:
733 /* step 1, unblock clock sync (if any) */
734 gst_basesrc_unlock (basesrc);
736 /* step 2, make sure streaming finishes */
737 GST_STREAM_LOCK (pad);
738 /* step 3, stop the task */
739 if (GST_RPAD_TASK (pad)) {
740 gst_task_stop (GST_RPAD_TASK (pad));
741 gst_object_unref (GST_OBJECT (GST_RPAD_TASK (pad)));
742 GST_RPAD_TASK (pad) = NULL;
744 GST_STREAM_UNLOCK (pad);
754 GST_DEBUG_OBJECT (basesrc, "failed to start");
759 static GstElementStateReturn
760 gst_basesrc_change_state (GstElement * element)
763 GstElementStateReturn result = GST_STATE_FAILURE;
764 GstElementState transition;
766 basesrc = GST_BASESRC (element);
768 transition = GST_STATE_TRANSITION (element);
770 switch (transition) {
771 case GST_STATE_NULL_TO_READY:
773 case GST_STATE_READY_TO_PAUSED:
775 case GST_STATE_PAUSED_TO_PLAYING:
781 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
783 switch (transition) {
784 case GST_STATE_PLAYING_TO_PAUSED:
786 case GST_STATE_PAUSED_TO_READY:
787 result = gst_basesrc_stop (basesrc);
789 case GST_STATE_READY_TO_NULL: