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 basesrc->is_live = FALSE;
168 basesrc->live_lock = g_mutex_new ();
169 basesrc->live_cond = g_cond_new ();
171 /* hold ref to pad */
172 basesrc->srcpad = pad;
173 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
175 basesrc->segment_start = -1;
176 basesrc->segment_end = -1;
177 basesrc->blocksize = DEFAULT_BLOCKSIZE;
178 basesrc->clock_id = NULL;
180 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
184 gst_basesrc_set_live (GstBaseSrc * src, gboolean live)
188 GST_LIVE_UNLOCK (src);
192 gst_basesrc_is_live (GstBaseSrc * src)
197 result = src->is_live;
198 GST_LIVE_UNLOCK (src);
204 gst_basesrc_set_dataflow_funcs (GstBaseSrc * this)
206 GST_DEBUG ("updating dataflow functions");
209 gst_pad_set_loop_function (this->srcpad, gst_basesrc_loop);
211 gst_pad_set_loop_function (this->srcpad, NULL);
213 if (this->has_getrange)
214 gst_pad_set_getrange_function (this->srcpad, gst_basesrc_get_range);
216 gst_pad_set_getrange_function (this->srcpad, NULL);
220 gst_basesrc_query (GstPad * pad, GstQuery * query)
227 src = GST_BASESRC (GST_PAD_PARENT (pad));
229 switch (GST_QUERY_TYPE (query)) {
230 case GST_QUERY_POSITION:
234 gst_query_parse_position (query, &format, NULL, NULL);
236 case GST_FORMAT_DEFAULT:
237 case GST_FORMAT_BYTES:
238 b = gst_basesrc_get_size (src, &ui64);
239 /* better to make get_size take an int64 */
240 i64 = b ? (gint64) ui64 : -1;
241 gst_query_set_position (query, GST_FORMAT_BYTES, src->offset, i64);
243 case GST_FORMAT_PERCENT:
244 b = gst_basesrc_get_size (src, &ui64);
245 i64 = GST_FORMAT_PERCENT_MAX;
246 i64 *= b ? (src->offset / (gdouble) ui64) : 1.0;
247 gst_query_set_position (query, GST_FORMAT_PERCENT,
248 i64, GST_FORMAT_PERCENT_MAX);
255 case GST_QUERY_SEEKING:
256 gst_query_set_seeking (query, GST_FORMAT_BYTES,
257 src->seekable, src->segment_start, src->segment_end);
260 case GST_QUERY_FORMATS:
261 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
262 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
265 case GST_QUERY_LATENCY:
266 case GST_QUERY_JITTER:
268 case GST_QUERY_CONVERT:
270 return gst_pad_query_default (pad, query);
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},
291 gst_basesrc_do_seek (GstBaseSrc * src, GstEvent * event)
296 format = GST_EVENT_SEEK_FORMAT (event);
298 /* get seek format */
299 if (format == GST_FORMAT_DEFAULT)
300 format = GST_FORMAT_BYTES;
301 /* we can only seek bytes */
302 if (format != GST_FORMAT_BYTES)
305 /* get seek positions */
306 offset = GST_EVENT_SEEK_OFFSET (event);
307 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
309 switch (GST_EVENT_SEEK_METHOD (event)) {
310 case GST_SEEK_METHOD_SET:
313 src->offset = MIN (offset, src->size);
314 src->segment_start = src->offset;
315 src->segment_end = MIN (GST_EVENT_SEEK_ENDOFFSET (event), src->size);
316 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
319 case GST_SEEK_METHOD_CUR:
320 offset += src->offset;
321 src->offset = CLAMP (offset, 0, src->size);
322 src->segment_start = src->offset;
323 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
324 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
327 case GST_SEEK_METHOD_END:
330 offset = src->size + offset;
331 src->offset = MAX (0, offset);
332 src->segment_start = src->offset;
333 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
334 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
341 /* send flush start */
342 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
344 /* unblock streaming thread */
345 gst_basesrc_unlock (src);
347 /* grab streaming lock */
348 GST_STREAM_LOCK (src->srcpad);
351 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
353 /* now send discont */
357 event = gst_event_new_discontinuous (1.0,
359 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
361 gst_pad_push_event (src->srcpad, event);
364 /* and restart the task */
365 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_basesrc_loop,
367 GST_STREAM_UNLOCK (src->srcpad);
369 gst_event_unref (event);
376 GST_DEBUG_OBJECT (src, "seek error");
377 gst_event_unref (event);
383 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
386 GstBaseSrcClass *bclass;
389 src = GST_BASESRC (GST_PAD_PARENT (pad));
390 bclass = GST_BASESRC_GET_CLASS (src);
393 result = bclass->event (src, event);
395 switch (GST_EVENT_TYPE (event)) {
397 return gst_basesrc_do_seek (src, event);
402 format = GST_EVENT_SIZE_FORMAT (event);
403 if (format == GST_FORMAT_DEFAULT)
404 format = GST_FORMAT_BYTES;
405 /* we can only accept bytes */
406 if (format != GST_FORMAT_BYTES)
409 src->blocksize = GST_EVENT_SIZE_VALUE (event);
410 g_object_notify (G_OBJECT (src), "blocksize");
413 case GST_EVENT_FLUSH:
414 /* cancel any blocking getrange */
415 if (!GST_EVENT_FLUSH_DONE (event))
416 gst_basesrc_unlock (src);
421 gst_event_unref (event);
427 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
432 src = GST_BASESRC (object);
436 src->blocksize = g_value_get_ulong (value);
439 src->has_loop = g_value_get_boolean (value);
440 gst_basesrc_set_dataflow_funcs (src);
442 case PROP_HAS_GETRANGE:
443 src->has_getrange = g_value_get_boolean (value);
444 gst_basesrc_set_dataflow_funcs (src);
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
458 src = GST_BASESRC (object);
462 g_value_set_ulong (value, src->blocksize);
465 g_value_set_boolean (value, src->has_loop);
467 case PROP_HAS_GETRANGE:
468 g_value_set_boolean (value, src->has_getrange);
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
477 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
482 GstBaseSrcClass *bclass;
484 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
485 bclass = GST_BASESRC_GET_CLASS (src);
489 while (!src->live_running) {
490 GST_DEBUG ("live source waiting for running state");
492 GST_DEBUG ("live source unlocked");
495 GST_LIVE_UNLOCK (src);
497 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
504 if (src->size != -1) {
505 if (offset > src->size)
506 goto unexpected_length;
508 if (offset + length > src->size) {
509 if (bclass->get_size)
510 bclass->get_size (src, &src->size);
512 if (offset + length > src->size) {
513 length = src->size - offset;
518 goto unexpected_length;
520 ret = bclass->create (src, offset, length, buf);
527 GST_DEBUG_OBJECT (src, "getrange but not started");
528 return GST_FLOW_WRONG_STATE;
532 GST_DEBUG_OBJECT (src, "no create function");
533 return GST_FLOW_ERROR;
537 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
538 return GST_FLOW_UNEXPECTED;
543 gst_basesrc_check_get_range (GstPad * pad)
547 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
549 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
550 gst_basesrc_start (src);
551 gst_basesrc_stop (src);
554 return src->seekable;
558 gst_basesrc_loop (GstPad * pad)
561 GstBuffer *buf = NULL;
564 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
566 ret = gst_basesrc_get_range (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)
580 GST_DEBUG_OBJECT (src, "going to EOS");
581 gst_pad_pause_task (pad);
582 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
587 GST_DEBUG_OBJECT (src, "pausing task");
588 gst_pad_pause_task (pad);
594 gst_basesrc_unlock (GstBaseSrc * basesrc)
596 GstBaseSrcClass *bclass;
597 gboolean result = FALSE;
599 GST_DEBUG ("unlock");
600 /* unblock whatever the subclass is doing */
601 bclass = GST_BASESRC_GET_CLASS (basesrc);
603 result = bclass->unlock (basesrc);
605 GST_DEBUG ("unschedule clock");
606 /* and unblock the clock as well, if any */
608 if (basesrc->clock_id) {
609 gst_clock_id_unschedule (basesrc->clock_id);
611 GST_UNLOCK (basesrc);
613 GST_DEBUG ("unlock done");
619 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
621 GstBaseSrcClass *bclass;
622 gboolean result = FALSE;
624 bclass = GST_BASESRC_GET_CLASS (basesrc);
625 if (bclass->get_size)
626 result = bclass->get_size (basesrc, size);
629 basesrc->size = *size;
635 gst_basesrc_is_seekable (GstBaseSrc * basesrc)
637 GstBaseSrcClass *bclass;
639 bclass = GST_BASESRC_GET_CLASS (basesrc);
641 /* check if we can seek */
642 if (bclass->is_seekable)
643 basesrc->seekable = bclass->is_seekable (basesrc);
645 basesrc->seekable = FALSE;
647 return basesrc->seekable;
651 gst_basesrc_start (GstBaseSrc * basesrc)
653 GstBaseSrcClass *bclass;
656 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
659 bclass = GST_BASESRC_GET_CLASS (basesrc);
661 result = bclass->start (basesrc);
666 goto could_not_start;
668 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
670 /* start in the beginning */
672 basesrc->segment_start = 0;
674 /* figure out the size */
675 if (bclass->get_size) {
676 result = bclass->get_size (basesrc, &basesrc->size);
684 GST_DEBUG ("size %d %lld", result, basesrc->size);
686 /* we always run to the end */
687 basesrc->segment_end = -1;
689 /* check if we can seek, updates ->seekable */
690 gst_basesrc_is_seekable (basesrc);
694 if (basesrc->seekable) {
697 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
698 gst_pad_set_caps (basesrc->srcpad, caps);
707 GST_DEBUG_OBJECT (basesrc, "could not start");
713 gst_basesrc_stop (GstBaseSrc * basesrc)
715 GstBaseSrcClass *bclass;
716 gboolean result = TRUE;
718 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
721 bclass = GST_BASESRC_GET_CLASS (basesrc);
723 result = bclass->stop (basesrc);
726 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
732 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
737 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
739 /* prepare subclass first */
741 case GST_ACTIVATE_PUSH:
742 case GST_ACTIVATE_PULL:
743 result = gst_basesrc_start (basesrc);
749 /* if that failed we can stop here */
755 case GST_ACTIVATE_PUSH:
757 gst_pad_start_task (pad, (GstTaskFunction) gst_basesrc_loop, pad);
759 case GST_ACTIVATE_PULL:
760 result = basesrc->seekable;
762 gst_basesrc_stop (basesrc);
764 case GST_ACTIVATE_NONE:
765 GST_LIVE_LOCK (basesrc);
766 basesrc->live_running = TRUE;
767 GST_LIVE_SIGNAL (basesrc);
768 GST_LIVE_UNLOCK (basesrc);
770 /* step 1, unblock clock sync (if any) */
771 gst_basesrc_unlock (basesrc);
773 /* step 2, make sure streaming finishes */
774 result = gst_pad_stop_task (pad);
782 GST_DEBUG_OBJECT (basesrc, "failed to start");
787 static GstElementStateReturn
788 gst_basesrc_change_state (GstElement * element)
791 GstElementStateReturn result = GST_STATE_SUCCESS;
792 GstElementStateReturn presult;
793 GstElementState transition;
795 basesrc = GST_BASESRC (element);
797 transition = GST_STATE_TRANSITION (element);
799 switch (transition) {
800 case GST_STATE_NULL_TO_READY:
802 case GST_STATE_READY_TO_PAUSED:
803 GST_LIVE_LOCK (element);
804 if (basesrc->is_live) {
805 result = GST_STATE_NO_PREROLL;
806 basesrc->live_running = FALSE;
808 GST_LIVE_UNLOCK (element);
810 case GST_STATE_PAUSED_TO_PLAYING:
811 GST_LIVE_LOCK (element);
812 basesrc->live_running = TRUE;
813 GST_LIVE_SIGNAL (element);
814 GST_LIVE_UNLOCK (element);
820 if ((presult = GST_ELEMENT_CLASS (parent_class)->change_state (element)) !=
824 switch (transition) {
825 case GST_STATE_PLAYING_TO_PAUSED:
826 GST_LIVE_LOCK (element);
827 if (basesrc->is_live) {
828 result = GST_STATE_NO_PREROLL;
829 basesrc->live_running = FALSE;
831 GST_LIVE_UNLOCK (element);
833 case GST_STATE_PAUSED_TO_READY:
834 if (!gst_basesrc_stop (basesrc))
835 result = GST_STATE_FAILURE;
837 case GST_STATE_READY_TO_NULL: