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;
175 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
179 gst_basesrc_set_dataflow_funcs (GstBaseSrc * this)
181 GST_DEBUG ("updating dataflow functions");
184 gst_pad_set_loop_function (this->srcpad, gst_basesrc_loop);
186 gst_pad_set_loop_function (this->srcpad, NULL);
188 if (this->has_getrange)
189 gst_pad_set_getrange_function (this->srcpad, gst_basesrc_get_range);
191 gst_pad_set_getrange_function (this->srcpad, NULL);
194 static const GstFormat *
195 gst_basesrc_get_formats (GstPad * pad)
197 static const GstFormat formats[] = {
206 static const GstQueryType *
207 gst_basesrc_get_query_types (GstPad * pad)
209 static const GstQueryType types[] = {
213 GST_QUERY_SEGMENT_END,
221 gst_basesrc_query (GstPad * pad, GstQueryType type,
222 GstFormat * format, gint64 * value)
224 GstBaseSrc *src = GST_BASESRC (GST_PAD_PARENT (pad));
226 if (*format == GST_FORMAT_DEFAULT)
227 *format = GST_FORMAT_BYTES;
230 case GST_QUERY_TOTAL:
232 case GST_FORMAT_BYTES:
236 ret = gst_basesrc_get_size (src, (guint64 *) value);
237 GST_DEBUG ("getting length %d %lld", ret, *value);
240 case GST_FORMAT_PERCENT:
241 *value = GST_FORMAT_PERCENT_MAX;
247 case GST_QUERY_POSITION:
249 case GST_FORMAT_BYTES:
250 *value = src->offset;
252 case GST_FORMAT_PERCENT:
254 if (!gst_basesrc_get_size (src, (guint64 *) value))
256 *value = src->offset * GST_FORMAT_PERCENT_MAX / *value;
262 case GST_QUERY_START:
263 *value = src->segment_start;
265 case GST_QUERY_SEGMENT_END:
266 *value = src->segment_end;
274 static const GstEventMask *
275 gst_basesrc_get_event_mask (GstPad * pad)
277 static const GstEventMask masks[] = {
278 {GST_EVENT_SEEK, GST_SEEK_METHOD_CUR | GST_SEEK_METHOD_SET |
279 GST_SEEK_METHOD_END | GST_SEEK_FLAG_FLUSH |
280 GST_SEEK_FLAG_SEGMENT_LOOP},
281 {GST_EVENT_FLUSH, 0},
289 gst_basesrc_do_seek (GstBaseSrc * src, GstEvent * event)
294 format = GST_EVENT_SEEK_FORMAT (event);
296 /* get seek format */
297 if (format == GST_FORMAT_DEFAULT)
298 format = GST_FORMAT_BYTES;
299 /* we can only seek bytes */
300 if (format != GST_FORMAT_BYTES)
303 /* get seek positions */
304 offset = GST_EVENT_SEEK_OFFSET (event);
305 src->segment_start = offset;
306 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
307 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
309 /* send flush start */
310 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
312 /* unblock streaming thread */
313 gst_basesrc_unlock (src);
315 /* grab streaming lock */
316 GST_STREAM_LOCK (src->srcpad);
318 switch (GST_EVENT_SEEK_METHOD (event)) {
319 case GST_SEEK_METHOD_SET:
322 src->offset = MIN (offset, src->size);
323 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
326 case GST_SEEK_METHOD_CUR:
327 offset += src->offset;
328 src->offset = CLAMP (offset, 0, src->size);
329 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
332 case GST_SEEK_METHOD_END:
335 offset = src->size + offset;
336 src->offset = MAX (0, offset);
337 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
344 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
346 /* now send discont */
350 event = gst_event_new_discontinuous (1.0,
352 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
354 gst_pad_push_event (src->srcpad, event);
357 /* and restart the task */
358 if (GST_RPAD_TASK (src->srcpad)) {
359 gst_task_start (GST_RPAD_TASK (src->srcpad));
361 GST_STREAM_UNLOCK (src->srcpad);
363 gst_event_unref (event);
370 GST_DEBUG_OBJECT (src, "seek error");
371 GST_STREAM_UNLOCK (src->srcpad);
372 gst_event_unref (event);
378 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
381 GstBaseSrcClass *bclass;
384 src = GST_BASESRC (GST_PAD_PARENT (pad));
385 bclass = GST_BASESRC_GET_CLASS (src);
388 result = bclass->event (src, event);
390 switch (GST_EVENT_TYPE (event)) {
392 return gst_basesrc_do_seek (src, event);
397 format = GST_EVENT_SIZE_FORMAT (event);
398 if (format == GST_FORMAT_DEFAULT)
399 format = GST_FORMAT_BYTES;
400 /* we can only accept bytes */
401 if (format != GST_FORMAT_BYTES)
404 src->blocksize = GST_EVENT_SIZE_VALUE (event);
405 g_object_notify (G_OBJECT (src), "blocksize");
408 case GST_EVENT_FLUSH:
409 /* cancel any blocking getrange */
410 if (!GST_EVENT_FLUSH_DONE (event))
411 gst_basesrc_unlock (src);
416 gst_event_unref (event);
422 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
427 src = GST_BASESRC (object);
431 src->blocksize = g_value_get_ulong (value);
434 src->has_loop = g_value_get_boolean (value);
435 gst_basesrc_set_dataflow_funcs (src);
437 case PROP_HAS_GETRANGE:
438 src->has_getrange = g_value_get_boolean (value);
439 gst_basesrc_set_dataflow_funcs (src);
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
453 src = GST_BASESRC (object);
457 g_value_set_ulong (value, src->blocksize);
460 g_value_set_boolean (value, src->has_loop);
462 case PROP_HAS_GETRANGE:
463 g_value_set_boolean (value, src->has_getrange);
466 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
472 gst_basesrc_get_range_unlocked (GstPad * pad, guint64 offset, guint length,
477 GstBaseSrcClass *bclass;
479 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
480 bclass = GST_BASESRC_GET_CLASS (src);
482 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
489 if (src->size != -1) {
490 if (offset + length > src->size) {
491 if (bclass->get_size)
492 bclass->get_size (src, &src->size);
494 if (offset + length > src->size) {
495 length = src->size - offset;
500 goto unexpected_length;
502 ret = bclass->create (src, offset, length, buf);
509 GST_DEBUG_OBJECT (src, "getrange but not started");
510 return GST_FLOW_WRONG_STATE;
514 GST_DEBUG_OBJECT (src, "no create function");
515 return GST_FLOW_ERROR;
519 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
520 return GST_FLOW_UNEXPECTED;
525 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
530 GST_STREAM_LOCK (pad);
532 fret = gst_basesrc_get_range_unlocked (pad, offset, length, ret);
534 GST_STREAM_UNLOCK (pad);
540 gst_basesrc_check_get_range (GstPad * pad)
544 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
546 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
547 gst_basesrc_start (src);
548 gst_basesrc_stop (src);
551 return src->seekable;
555 gst_basesrc_loop (GstPad * pad)
558 GstBuffer *buf = NULL;
561 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
563 GST_STREAM_LOCK (pad);
565 ret = gst_basesrc_get_range_unlocked (pad, src->offset, src->blocksize, &buf);
566 if (ret != GST_FLOW_OK)
569 src->offset += GST_BUFFER_SIZE (buf);
571 ret = gst_pad_push (pad, buf);
572 if (ret != GST_FLOW_OK)
575 GST_STREAM_UNLOCK (pad);
580 GST_DEBUG_OBJECT (src, "going to EOS");
581 gst_task_pause (GST_RPAD_TASK (pad));
582 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
583 GST_STREAM_UNLOCK (pad);
588 GST_DEBUG_OBJECT (src, "pausing task");
589 gst_task_pause (GST_RPAD_TASK (pad));
590 GST_STREAM_UNLOCK (pad);
596 gst_basesrc_unlock (GstBaseSrc * basesrc)
598 GstBaseSrcClass *bclass;
599 gboolean result = FALSE;
601 bclass = GST_BASESRC_GET_CLASS (basesrc);
603 result = bclass->unlock (basesrc);
609 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
611 GstBaseSrcClass *bclass;
612 gboolean result = FALSE;
614 bclass = GST_BASESRC_GET_CLASS (basesrc);
615 if (bclass->get_size)
616 result = bclass->get_size (basesrc, size);
619 basesrc->size = *size;
625 gst_basesrc_start (GstBaseSrc * basesrc)
627 GstBaseSrcClass *bclass;
630 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
633 bclass = GST_BASESRC_GET_CLASS (basesrc);
635 result = bclass->start (basesrc);
640 goto could_not_start;
642 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
644 /* start in the beginning */
646 basesrc->segment_start = 0;
648 /* figure out the size */
649 if (bclass->get_size) {
650 result = bclass->get_size (basesrc, &basesrc->size);
658 GST_DEBUG ("size %d %lld", result, basesrc->size);
660 /* we always run to the end */
661 basesrc->segment_end = -1;
663 /* check if we can seek */
664 if (bclass->is_seekable)
665 basesrc->seekable = bclass->is_seekable (basesrc);
667 basesrc->seekable = FALSE;
671 if (basesrc->seekable) {
674 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
675 gst_pad_set_caps (basesrc->srcpad, caps);
684 GST_DEBUG_OBJECT (basesrc, "could not start");
690 gst_basesrc_stop (GstBaseSrc * basesrc)
692 GstBaseSrcClass *bclass;
693 gboolean result = TRUE;
695 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
698 bclass = GST_BASESRC_GET_CLASS (basesrc);
700 result = bclass->stop (basesrc);
703 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
709 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
714 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
716 /* prepare subclass first */
718 case GST_ACTIVATE_PUSH:
719 case GST_ACTIVATE_PULL:
720 result = gst_basesrc_start (basesrc);
726 /* if that failed we can stop here */
732 case GST_ACTIVATE_PUSH:
733 /* if we have a scheduler we can start the task */
734 if (GST_ELEMENT_SCHEDULER (basesrc)) {
735 GST_STREAM_LOCK (pad);
736 GST_RPAD_TASK (pad) =
737 gst_scheduler_create_task (GST_ELEMENT_SCHEDULER (basesrc),
738 (GstTaskFunction) gst_basesrc_loop, pad);
740 gst_task_start (GST_RPAD_TASK (pad));
741 GST_STREAM_UNLOCK (pad);
745 case GST_ACTIVATE_PULL:
748 case GST_ACTIVATE_NONE:
749 /* step 1, unblock clock sync (if any) */
751 /* step 2, make sure streaming finishes */
752 GST_STREAM_LOCK (pad);
753 /* step 3, stop the task */
754 if (GST_RPAD_TASK (pad)) {
755 gst_task_stop (GST_RPAD_TASK (pad));
756 gst_object_unref (GST_OBJECT (GST_RPAD_TASK (pad)));
757 GST_RPAD_TASK (pad) = NULL;
759 GST_STREAM_UNLOCK (pad);
769 GST_DEBUG_OBJECT (basesrc, "failed to start");
774 static GstElementStateReturn
775 gst_basesrc_change_state (GstElement * element)
778 GstElementStateReturn result = GST_STATE_FAILURE;
779 GstElementState transition;
781 basesrc = GST_BASESRC (element);
783 transition = GST_STATE_TRANSITION (element);
785 switch (transition) {
786 case GST_STATE_NULL_TO_READY:
788 case GST_STATE_READY_TO_PAUSED:
790 case GST_STATE_PAUSED_TO_PLAYING:
796 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
798 switch (transition) {
799 case GST_STATE_PLAYING_TO_PAUSED:
801 case GST_STATE_PAUSED_TO_READY:
802 result = gst_basesrc_stop (basesrc);
804 case GST_STATE_READY_TO_NULL: