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_query (GstPad * pad, GstQuery * query);
95 static const GstEventMask *gst_basesrc_get_event_mask (GstPad * pad);
98 static gboolean gst_basesrc_unlock (GstBaseSrc * basesrc);
99 static gboolean gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size);
100 static gboolean gst_basesrc_start (GstBaseSrc * basesrc);
101 static gboolean gst_basesrc_stop (GstBaseSrc * basesrc);
103 static GstElementStateReturn gst_basesrc_change_state (GstElement * element);
105 static void gst_basesrc_set_dataflow_funcs (GstBaseSrc * this);
106 static void gst_basesrc_loop (GstPad * pad);
107 static gboolean gst_basesrc_check_get_range (GstPad * pad);
108 static GstFlowReturn gst_basesrc_get_range (GstPad * pad, guint64 offset,
109 guint length, GstBuffer ** buf);
112 gst_basesrc_base_init (gpointer g_class)
114 GST_DEBUG_CATEGORY_INIT (gst_basesrc_debug, "basesrc", 0, "basesrc element");
118 gst_basesrc_class_init (GstBaseSrcClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_basesrc_set_property);
129 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_basesrc_get_property);
131 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
132 g_param_spec_ulong ("blocksize", "Block size",
133 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
136 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
137 g_param_spec_boolean ("has-loop", "Has loop function",
138 "True if the element should expose a loop function", TRUE,
139 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
141 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
142 g_param_spec_boolean ("has-getrange", "Has getrange function",
143 "True if the element should expose a getrange function", TRUE,
144 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
146 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_basesrc_change_state);
150 gst_basesrc_init (GstBaseSrc * basesrc, gpointer g_class)
153 GstPadTemplate *pad_template;
156 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
157 g_return_if_fail (pad_template != NULL);
159 pad = gst_pad_new_from_template (pad_template, "src");
161 gst_pad_set_activate_function (pad, gst_basesrc_activate);
162 gst_pad_set_event_function (pad, gst_basesrc_event_handler);
163 gst_pad_set_query_function (pad, gst_basesrc_query);
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_query (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, &format, NULL, NULL);
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_query_default (pad, query);
251 static const GstEventMask *
252 gst_basesrc_get_event_mask (GstPad * pad)
254 static const GstEventMask masks[] = {
255 {GST_EVENT_SEEK, GST_SEEK_METHOD_CUR | GST_SEEK_METHOD_SET |
256 GST_SEEK_METHOD_END | GST_SEEK_FLAG_FLUSH |
257 GST_SEEK_FLAG_SEGMENT_LOOP},
258 {GST_EVENT_FLUSH, 0},
267 gst_basesrc_do_seek (GstBaseSrc * src, GstEvent * event)
272 format = GST_EVENT_SEEK_FORMAT (event);
274 /* get seek format */
275 if (format == GST_FORMAT_DEFAULT)
276 format = GST_FORMAT_BYTES;
277 /* we can only seek bytes */
278 if (format != GST_FORMAT_BYTES)
281 /* get seek positions */
282 offset = GST_EVENT_SEEK_OFFSET (event);
283 src->segment_start = offset;
284 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
285 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
287 /* send flush start */
288 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
290 /* unblock streaming thread */
291 gst_basesrc_unlock (src);
293 /* grab streaming lock */
294 GST_STREAM_LOCK (src->srcpad);
296 switch (GST_EVENT_SEEK_METHOD (event)) {
297 case GST_SEEK_METHOD_SET:
300 src->offset = MIN (offset, src->size);
301 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
304 case GST_SEEK_METHOD_CUR:
305 offset += src->offset;
306 src->offset = CLAMP (offset, 0, src->size);
307 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
310 case GST_SEEK_METHOD_END:
313 offset = src->size + offset;
314 src->offset = MAX (0, offset);
315 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
322 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
324 /* now send discont */
328 event = gst_event_new_discontinuous (1.0,
330 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
332 gst_pad_push_event (src->srcpad, event);
335 /* and restart the task */
336 if (GST_RPAD_TASK (src->srcpad)) {
337 gst_task_start (GST_RPAD_TASK (src->srcpad));
339 GST_STREAM_UNLOCK (src->srcpad);
341 gst_event_unref (event);
348 GST_DEBUG_OBJECT (src, "seek error");
349 GST_STREAM_UNLOCK (src->srcpad);
350 gst_event_unref (event);
356 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
359 GstBaseSrcClass *bclass;
362 src = GST_BASESRC (GST_PAD_PARENT (pad));
363 bclass = GST_BASESRC_GET_CLASS (src);
366 result = bclass->event (src, event);
368 switch (GST_EVENT_TYPE (event)) {
370 return gst_basesrc_do_seek (src, event);
375 format = GST_EVENT_SIZE_FORMAT (event);
376 if (format == GST_FORMAT_DEFAULT)
377 format = GST_FORMAT_BYTES;
378 /* we can only accept bytes */
379 if (format != GST_FORMAT_BYTES)
382 src->blocksize = GST_EVENT_SIZE_VALUE (event);
383 g_object_notify (G_OBJECT (src), "blocksize");
386 case GST_EVENT_FLUSH:
387 /* cancel any blocking getrange */
388 if (!GST_EVENT_FLUSH_DONE (event))
389 gst_basesrc_unlock (src);
394 gst_event_unref (event);
400 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
405 src = GST_BASESRC (object);
409 src->blocksize = g_value_get_ulong (value);
412 src->has_loop = g_value_get_boolean (value);
413 gst_basesrc_set_dataflow_funcs (src);
415 case PROP_HAS_GETRANGE:
416 src->has_getrange = g_value_get_boolean (value);
417 gst_basesrc_set_dataflow_funcs (src);
420 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
426 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
431 src = GST_BASESRC (object);
435 g_value_set_ulong (value, src->blocksize);
438 g_value_set_boolean (value, src->has_loop);
440 case PROP_HAS_GETRANGE:
441 g_value_set_boolean (value, src->has_getrange);
444 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
450 gst_basesrc_get_range_unlocked (GstPad * pad, guint64 offset, guint length,
455 GstBaseSrcClass *bclass;
457 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
458 bclass = GST_BASESRC_GET_CLASS (src);
460 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
467 if (src->size != -1) {
468 if (offset + length > src->size) {
469 if (bclass->get_size)
470 bclass->get_size (src, &src->size);
472 if (offset + length > src->size) {
473 length = src->size - offset;
478 goto unexpected_length;
480 ret = bclass->create (src, offset, length, buf);
487 GST_DEBUG_OBJECT (src, "getrange but not started");
488 return GST_FLOW_WRONG_STATE;
492 GST_DEBUG_OBJECT (src, "no create function");
493 return GST_FLOW_ERROR;
497 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
498 return GST_FLOW_UNEXPECTED;
503 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
508 GST_STREAM_LOCK (pad);
510 fret = gst_basesrc_get_range_unlocked (pad, offset, length, ret);
512 GST_STREAM_UNLOCK (pad);
518 gst_basesrc_check_get_range (GstPad * pad)
522 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
524 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
525 gst_basesrc_start (src);
526 gst_basesrc_stop (src);
529 return src->seekable;
533 gst_basesrc_loop (GstPad * pad)
536 GstBuffer *buf = NULL;
539 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
541 GST_STREAM_LOCK (pad);
543 ret = gst_basesrc_get_range_unlocked (pad, src->offset, src->blocksize, &buf);
544 if (ret != GST_FLOW_OK)
547 src->offset += GST_BUFFER_SIZE (buf);
549 ret = gst_pad_push (pad, buf);
550 if (ret != GST_FLOW_OK)
553 GST_STREAM_UNLOCK (pad);
558 GST_DEBUG_OBJECT (src, "going to EOS");
559 gst_task_pause (GST_RPAD_TASK (pad));
560 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
561 GST_STREAM_UNLOCK (pad);
566 GST_DEBUG_OBJECT (src, "pausing task");
567 gst_task_pause (GST_RPAD_TASK (pad));
568 GST_STREAM_UNLOCK (pad);
574 gst_basesrc_unlock (GstBaseSrc * basesrc)
576 GstBaseSrcClass *bclass;
577 gboolean result = FALSE;
579 GST_DEBUG ("unlock");
580 /* unblock whatever the subclass is doing */
581 bclass = GST_BASESRC_GET_CLASS (basesrc);
583 result = bclass->unlock (basesrc);
585 GST_DEBUG ("unschedule clock");
586 /* and unblock the clock as well, if any */
588 if (basesrc->clock_id) {
589 gst_clock_id_unschedule (basesrc->clock_id);
591 GST_UNLOCK (basesrc);
593 GST_DEBUG ("unlock done");
599 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
601 GstBaseSrcClass *bclass;
602 gboolean result = FALSE;
604 bclass = GST_BASESRC_GET_CLASS (basesrc);
605 if (bclass->get_size)
606 result = bclass->get_size (basesrc, size);
609 basesrc->size = *size;
615 gst_basesrc_is_seekable (GstBaseSrc * basesrc)
617 GstBaseSrcClass *bclass;
619 bclass = GST_BASESRC_GET_CLASS (basesrc);
621 /* check if we can seek */
622 if (bclass->is_seekable)
623 basesrc->seekable = bclass->is_seekable (basesrc);
625 basesrc->seekable = FALSE;
627 return basesrc->seekable;
631 gst_basesrc_start (GstBaseSrc * basesrc)
633 GstBaseSrcClass *bclass;
636 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
639 bclass = GST_BASESRC_GET_CLASS (basesrc);
641 result = bclass->start (basesrc);
646 goto could_not_start;
648 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
650 /* start in the beginning */
652 basesrc->segment_start = 0;
654 /* figure out the size */
655 if (bclass->get_size) {
656 result = bclass->get_size (basesrc, &basesrc->size);
664 GST_DEBUG ("size %d %lld", result, basesrc->size);
666 /* we always run to the end */
667 basesrc->segment_end = -1;
669 /* check if we can seek, updates ->seekable */
670 gst_basesrc_is_seekable (basesrc);
674 if (basesrc->seekable) {
677 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
678 gst_pad_set_caps (basesrc->srcpad, caps);
687 GST_DEBUG_OBJECT (basesrc, "could not start");
693 gst_basesrc_stop (GstBaseSrc * basesrc)
695 GstBaseSrcClass *bclass;
696 gboolean result = TRUE;
698 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
701 bclass = GST_BASESRC_GET_CLASS (basesrc);
703 result = bclass->stop (basesrc);
706 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
712 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
717 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
719 /* prepare subclass first */
721 case GST_ACTIVATE_PUSH:
722 case GST_ACTIVATE_PULL:
723 result = gst_basesrc_start (basesrc);
729 /* if that failed we can stop here */
735 case GST_ACTIVATE_PUSH:
736 /* if we have a scheduler we can start the task */
737 if (GST_ELEMENT_SCHEDULER (basesrc)) {
738 GST_STREAM_LOCK (pad);
739 GST_RPAD_TASK (pad) =
740 gst_scheduler_create_task (GST_ELEMENT_SCHEDULER (basesrc),
741 (GstTaskFunction) gst_basesrc_loop, pad);
743 gst_task_start (GST_RPAD_TASK (pad));
744 GST_STREAM_UNLOCK (pad);
748 case GST_ACTIVATE_PULL:
751 case GST_ACTIVATE_NONE:
752 /* step 1, unblock clock sync (if any) */
753 gst_basesrc_unlock (basesrc);
755 /* step 2, make sure streaming finishes */
756 GST_STREAM_LOCK (pad);
757 /* step 3, stop the task */
758 if (GST_RPAD_TASK (pad)) {
759 gst_task_stop (GST_RPAD_TASK (pad));
760 gst_object_unref (GST_OBJECT (GST_RPAD_TASK (pad)));
761 GST_RPAD_TASK (pad) = NULL;
763 GST_STREAM_UNLOCK (pad);
773 GST_DEBUG_OBJECT (basesrc, "failed to start");
778 static GstElementStateReturn
779 gst_basesrc_change_state (GstElement * element)
782 GstElementStateReturn result = GST_STATE_FAILURE;
783 GstElementState transition;
785 basesrc = GST_BASESRC (element);
787 transition = GST_STATE_TRANSITION (element);
789 switch (transition) {
790 case GST_STATE_NULL_TO_READY:
792 case GST_STATE_READY_TO_PAUSED:
794 case GST_STATE_PAUSED_TO_PLAYING:
800 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
802 switch (transition) {
803 case GST_STATE_PLAYING_TO_PAUSED:
805 case GST_STATE_PAUSED_TO_READY:
806 if (!gst_basesrc_stop (basesrc))
807 result = GST_STATE_FAILURE;
809 case GST_STATE_READY_TO_NULL: