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.
25 * @short_description: Base class for getrange based source elements
26 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
28 * This is a generice base class for source elements. The following
29 * types of sources are supported:
31 * <listitem><para>random access sources like files</para></listitem>
32 * <listitem><para>seekable sources</para></listitem>
33 * <listitem><para>live sources</para></listitem>
38 * The source can be configured to operate in any #GstFormat with the
39 * gst_base_src_set_format() method. The currently set format determines
40 * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT
41 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
44 * #GstBaseSrc always supports push mode scheduling. If the following
45 * conditions are met, it also supports pull mode scheduling:
47 * <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
49 * <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
54 * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any
55 * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE.
58 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
59 * automatically seekable in push mode as well. The following conditions must
60 * be met to make the element seekable in push mode when the format is not
64 * #GstBaseSrc::is_seekable returns %TRUE.
67 * #GstBaseSrc::query can convert all supported seek formats to the
68 * internal format as set with gst_base_src_set_format().
71 * #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
76 * When the element does not meet the requirements to operate in pull mode,
77 * the offset and length in the #GstBaseSrc::create method should be ignored.
78 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
79 * element can operate in pull mode but only with specific offsets and
80 * lengths, it is allowed to generate an error when the wrong values are passed
81 * to the #GstBaseSrc::create function.
84 * #GstBaseSrc has support for live sources. Live sources are sources that
85 * produce data at a fixed rate, such as audio or video capture devices. A
86 * typical live source also provides a clock to publish the rate at which
88 * Use gst_base_src_set_live() to activate the live source mode.
91 * A live source does not produce data in the PAUSED state. This means that the
92 * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
93 * To signal the pipeline that the element will not produce data, the return
94 * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
97 * A typical live source will timestamp the buffers it creates with the
98 * current stream time of the pipeline. This is one reason why a live source
99 * can only produce data in the PLAYING state, when the clock is actually
100 * distributed and running.
103 * Live sources that synchronize and block on the clock (and audio source, for
104 * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
105 * function was interrupted by a state change to PAUSED.
108 * The #GstBaseSrc::get_times method can be used to implement pseudo-live
109 * sources. The base source will wait for the specified stream time returned in
110 * #GstBaseSrc::get_times before pushing out the buffer.
111 * It only makes sense to implement the ::get_times function if the source is
115 * There is only support in #GstBaseSrc for exactly one source pad, which
116 * should be named "src". A source implementation (subclass of #GstBaseSrc)
117 * should install a pad template in its base_init function, like so:
122 * my_element_base_init (gpointer g_class)
124 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
125 * // srctemplate should be a #GstStaticPadTemplate with direction
126 * // #GST_PAD_SRC and name "src"
127 * gst_element_class_add_pad_template (gstelement_class,
128 * gst_static_pad_template_get (&srctemplate));
129 * // see #GstElementDetails
130 * gst_element_class_set_details (gstelement_class, &details);
134 * <title>Controlled shutdown of live sources in applications</title>
136 * Applications that record from a live source may want to stop recording
137 * in a controlled way, so that the recording is stopped, but the data
138 * already in the pipeline is processed to the end (remember that many live
139 * sources would go on recording forever otherwise). For that to happen the
140 * application needs to make the source stop recording and send an EOS
141 * event down the pipeline. The application would then wait for an
142 * EOS message posted on the pipeline's bus to know when all data has
143 * been processed and the pipeline can safely be stopped.
146 * Since GStreamer 0.10.3 an application may simply set the source
147 * element to NULL or READY state to make it send an EOS event downstream.
148 * The application should lock the state of the source afterwards, so that
149 * shutting down the pipeline from PLAYING doesn't temporarily start up the
150 * source element for a second time:
154 * gst_element_set_state (audio_source, #GST_STATE_NULL);
155 * gst_element_set_locked_state (audio_source, %TRUE);
158 * Now the application should wait for an EOS message
159 * to be posted on the pipeline's bus. Once it has received
160 * an EOS message, it may safely shut down the entire pipeline:
163 * // everything done - shut down pipeline
164 * gst_element_set_state (pipeline, #GST_STATE_NULL);
165 * gst_element_set_locked_state (audio_source, %FALSE);
170 * Note that setting the source element to NULL or READY when the
171 * pipeline is in the PAUSED state may cause a deadlock since the streaming
172 * thread might be blocked in PREROLL.
175 * Last reviewed on 2006-09-27 (0.10.11)
187 #include "gstbasesrc.h"
188 #include "gsttypefindhelper.h"
189 #include <gst/gstmarshal.h>
190 #include <gst/gst-i18n-lib.h>
192 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
193 #define GST_CAT_DEFAULT gst_base_src_debug
195 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
196 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
197 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
198 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
199 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
200 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
201 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
203 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
204 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
206 /* BaseSrc signals and args */
213 #define DEFAULT_BLOCKSIZE 4096
214 #define DEFAULT_NUM_BUFFERS -1
215 #define DEFAULT_TYPEFIND FALSE
225 #define GST_BASE_SRC_GET_PRIVATE(obj) \
226 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
228 struct _GstBaseSrcPrivate
230 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
231 * to avoid the sending of two EOS in some cases) */
234 /* two segments to be sent in the streaming thread with STREAM_LOCK */
235 GstEvent *close_segment;
236 GstEvent *start_segment;
239 static GstElementClass *parent_class = NULL;
241 static void gst_base_src_base_init (gpointer g_class);
242 static void gst_base_src_class_init (GstBaseSrcClass * klass);
243 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
244 static void gst_base_src_finalize (GObject * object);
248 gst_base_src_get_type (void)
250 static GType base_src_type = 0;
252 if (G_UNLIKELY (base_src_type == 0)) {
253 static const GTypeInfo base_src_info = {
254 sizeof (GstBaseSrcClass),
255 (GBaseInitFunc) gst_base_src_base_init,
257 (GClassInitFunc) gst_base_src_class_init,
262 (GInstanceInitFunc) gst_base_src_init,
265 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
266 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
268 return base_src_type;
270 static GstCaps *gst_base_src_getcaps (GstPad * pad);
271 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
272 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
274 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
275 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
276 static void gst_base_src_set_property (GObject * object, guint prop_id,
277 const GValue * value, GParamSpec * pspec);
278 static void gst_base_src_get_property (GObject * object, guint prop_id,
279 GValue * value, GParamSpec * pspec);
280 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
281 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
282 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
283 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
285 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
287 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
288 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
289 GstSegment * segment);
290 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
292 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
293 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
294 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
296 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
297 GstStateChange transition);
299 static void gst_base_src_loop (GstPad * pad);
300 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
301 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
302 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
303 guint length, GstBuffer ** buf);
304 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
305 guint length, GstBuffer ** buf);
308 gst_base_src_base_init (gpointer g_class)
310 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
314 gst_base_src_class_init (GstBaseSrcClass * klass)
316 GObjectClass *gobject_class;
317 GstElementClass *gstelement_class;
319 gobject_class = G_OBJECT_CLASS (klass);
320 gstelement_class = GST_ELEMENT_CLASS (klass);
322 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
324 parent_class = g_type_class_peek_parent (klass);
326 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
327 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
328 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
330 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
331 g_param_spec_ulong ("blocksize", "Block size",
332 "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
333 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
334 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
335 g_param_spec_int ("num-buffers", "num-buffers",
336 "Number of buffers to output before sending EOS", -1, G_MAXINT,
337 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
338 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
339 g_param_spec_boolean ("typefind", "Typefind",
340 "Run typefind before negotiating", DEFAULT_TYPEFIND,
343 gstelement_class->change_state =
344 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
345 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
346 gstelement_class->get_query_types =
347 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
349 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
350 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
351 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
352 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
353 klass->check_get_range =
354 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
358 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
361 GstPadTemplate *pad_template;
363 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
365 basesrc->is_live = FALSE;
366 basesrc->live_lock = g_mutex_new ();
367 basesrc->live_cond = g_cond_new ();
368 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
369 basesrc->num_buffers_left = -1;
371 basesrc->can_activate_push = TRUE;
372 basesrc->pad_mode = GST_ACTIVATE_NONE;
375 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
376 g_return_if_fail (pad_template != NULL);
378 GST_DEBUG_OBJECT (basesrc, "creating src pad");
379 pad = gst_pad_new_from_template (pad_template, "src");
381 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
382 gst_pad_set_activatepush_function (pad,
383 GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
384 gst_pad_set_activatepull_function (pad,
385 GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
386 gst_pad_set_event_function (pad,
387 GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
388 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
389 gst_pad_set_checkgetrange_function (pad,
390 GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
391 gst_pad_set_getrange_function (pad,
392 GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
393 gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
394 gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
395 gst_pad_set_fixatecaps_function (pad,
396 GST_DEBUG_FUNCPTR (gst_base_src_fixate));
398 /* hold pointer to pad */
399 basesrc->srcpad = pad;
400 GST_DEBUG_OBJECT (basesrc, "adding src pad");
401 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
403 basesrc->blocksize = DEFAULT_BLOCKSIZE;
404 basesrc->clock_id = NULL;
405 /* we operate in BYTES by default */
406 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
407 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
409 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
411 GST_DEBUG_OBJECT (basesrc, "init done");
415 gst_base_src_finalize (GObject * object)
420 basesrc = GST_BASE_SRC (object);
422 g_mutex_free (basesrc->live_lock);
423 g_cond_free (basesrc->live_cond);
425 event_p = &basesrc->data.ABI.pending_seek;
426 gst_event_replace ((GstEvent **) event_p, NULL);
428 G_OBJECT_CLASS (parent_class)->finalize (object);
432 * gst_base_src_wait_playing:
435 * If the #GstBaseSrcClass::create method performs its own synchronisation against
436 * the clock it must unblock when going from PLAYING to the PAUSED state and call
437 * this method before continuing to produce the remaining data.
439 * This function will block until a state change to PLAYING happens (in which
440 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
441 * to a state change to READY or a FLUSH event (in which case this function
442 * returns #GST_FLOW_WRONG_STATE).
446 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
447 * continue. Any other return value should be returned from the create vmethod.
450 gst_base_src_wait_playing (GstBaseSrc * src)
452 /* block until the state changes, or we get a flush, or something */
455 while (G_UNLIKELY (!src->live_running)) {
456 GST_DEBUG ("live source signal waiting");
457 GST_LIVE_SIGNAL (src);
458 GST_DEBUG ("live source waiting for running state");
460 GST_DEBUG ("live source unlocked");
462 /* FIXME, use another variable to signal stopping so that we don't
463 * have to grab another lock. */
464 GST_OBJECT_LOCK (src->srcpad);
465 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
467 GST_OBJECT_UNLOCK (src->srcpad);
469 GST_LIVE_UNLOCK (src);
476 GST_DEBUG_OBJECT (src, "pad is flushing");
477 GST_OBJECT_UNLOCK (src->srcpad);
478 GST_LIVE_UNLOCK (src);
479 return GST_FLOW_WRONG_STATE;
484 * gst_base_src_set_live:
485 * @src: base source instance
486 * @live: new live-mode
488 * If the element listens to a live source, @live should
491 * A live source will not produce data in the PAUSED state and
492 * will therefore not be able to participate in the PREROLL phase
493 * of a pipeline. To signal this fact to the application and the
494 * pipeline, the state change return value of the live source will
495 * be GST_STATE_CHANGE_NO_PREROLL.
498 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
502 GST_LIVE_UNLOCK (src);
506 * gst_base_src_is_live:
507 * @src: base source instance
509 * Check if an element is in live mode.
511 * Returns: %TRUE if element is in live mode.
514 gst_base_src_is_live (GstBaseSrc * src)
519 result = src->is_live;
520 GST_LIVE_UNLOCK (src);
526 * gst_base_src_set_format:
527 * @src: base source instance
528 * @format: the format to use
530 * Sets the default format of the source. This will be the format used
531 * for sending NEW_SEGMENT events and for performing seeks.
533 * If a format of GST_FORMAT_BYTES is set, the element will be able to
534 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
539 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
541 gst_segment_init (&src->segment, format);
545 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
547 GstBaseSrcClass *bclass;
551 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
552 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
554 if (bclass->set_caps)
555 res = bclass->set_caps (bsrc, caps);
561 gst_base_src_getcaps (GstPad * pad)
563 GstBaseSrcClass *bclass;
565 GstCaps *caps = NULL;
567 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
568 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
569 if (bclass->get_caps)
570 caps = bclass->get_caps (bsrc);
573 GstPadTemplate *pad_template;
576 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
577 if (pad_template != NULL) {
578 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
585 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
587 GstBaseSrcClass *bclass;
590 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
591 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
594 bclass->fixate (bsrc, caps);
596 gst_object_unref (bsrc);
600 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
604 switch (GST_QUERY_TYPE (query)) {
605 case GST_QUERY_POSITION:
609 gst_query_parse_position (query, &format, NULL);
611 case GST_FORMAT_PERCENT:
617 position = src->segment.last_stop;
618 duration = src->segment.duration;
620 if (position != -1 && duration != -1) {
621 if (position < duration)
622 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
625 percent = GST_FORMAT_PERCENT_MAX;
629 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
637 position = src->segment.last_stop;
639 if (position != -1) {
640 /* convert to requested format */
642 gst_pad_query_convert (src->srcpad, src->segment.format,
643 position, &format, &position);
647 gst_query_set_position (query, format, position);
653 case GST_QUERY_DURATION:
657 gst_query_parse_duration (query, &format, NULL);
659 case GST_FORMAT_PERCENT:
660 gst_query_set_duration (query, GST_FORMAT_PERCENT,
661 GST_FORMAT_PERCENT_MAX);
668 duration = src->segment.duration;
670 if (duration != -1) {
671 /* convert to requested format */
673 gst_pad_query_convert (src->srcpad, src->segment.format,
674 duration, &format, &duration);
678 gst_query_set_duration (query, format, duration);
685 case GST_QUERY_SEEKING:
687 gst_query_set_seeking (query, src->segment.format,
688 src->seekable, 0, src->segment.duration);
692 case GST_QUERY_SEGMENT:
696 /* no end segment configured, current duration then */
697 if ((stop = src->segment.stop) == -1)
698 stop = src->segment.duration;
699 start = src->segment.start;
701 /* adjust to stream time */
702 if (src->segment.time != -1) {
703 start -= src->segment.time;
705 stop -= src->segment.time;
707 gst_query_set_segment (query, src->segment.rate, src->segment.format,
713 case GST_QUERY_FORMATS:
715 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
716 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
720 case GST_QUERY_CONVERT:
722 GstFormat src_fmt, dest_fmt;
723 gint64 src_val, dest_val;
725 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
727 /* we can only convert between equal formats... */
728 if (src_fmt == dest_fmt) {
734 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
737 case GST_QUERY_LATENCY:
738 /* we can only report the fact that we are live or not, we know nothing
739 * about latency. Subclasses should override and implement something
742 gst_query_set_latency (query, src->is_live, 0, -1);
743 GST_LIVE_UNLOCK (src);
746 case GST_QUERY_JITTER:
756 gst_base_src_query (GstPad * pad, GstQuery * query)
759 GstBaseSrcClass *bclass;
760 gboolean result = FALSE;
762 src = GST_BASE_SRC (gst_pad_get_parent (pad));
764 bclass = GST_BASE_SRC_GET_CLASS (src);
767 result = bclass->query (src, query);
769 result = gst_pad_query_default (pad, query);
771 gst_object_unref (src);
777 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
781 /* update our offset if the start/stop position was updated */
782 if (segment->format == GST_FORMAT_BYTES) {
783 segment->last_stop = segment->start;
784 segment->time = segment->start;
785 } else if (segment->start == 0) {
786 /* seek to start, we can implement a default for this. */
787 segment->last_stop = 0;
797 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
799 GstBaseSrcClass *bclass;
800 gboolean result = FALSE;
802 bclass = GST_BASE_SRC_GET_CLASS (src);
805 result = bclass->do_seek (src, segment);
810 /* this code implements the seeking. It is a good example
811 * handling all cases.
813 * A seek updates the currently configured segment.start
814 * and segment.stop values based on the SEEK_TYPE. If the
815 * segment.start value is updated, a seek to this new position
816 * should be performed.
818 * The seek can only be executed when we are not currently
819 * streaming any data, to make sure that this is the case, we
820 * acquire the STREAM_LOCK which is taken when we are in the
821 * _loop() function or when a getrange() is called. Normally
822 * we will not receive a seek if we are operating in pull mode
825 * When we are in the loop() function, we might be in the middle
826 * of pushing a buffer, which might block in a sink. To make sure
827 * that the push gets unblocked we push out a FLUSH_START event.
828 * Our loop function will get a WRONG_STATE return value from
829 * the push and will pause, effectively releasing the STREAM_LOCK.
831 * For a non-flushing seek, we pause the task, which might eventually
832 * release the STREAM_LOCK. We say eventually because when the sink
833 * blocks on the sample we might wait a very long time until the sink
834 * unblocks the sample. In any case we acquire the STREAM_LOCK and
835 * can continue the seek. A non-flushing seek is normally done in a
836 * running pipeline to perform seamless playback.
837 * In the case of a non-flushing seek we need to make sure that the
838 * data we output after the seek is continuous with the previous data,
839 * this is because a non-flushing seek does not reset the stream-time
840 * to 0. We do this by closing the currently running segment, ie. sending
841 * a new_segment event with the stop position set to the last processed
844 * After updating the segment.start/stop values, we prepare for
845 * streaming again. We push out a FLUSH_STOP to make the peer pad
846 * accept data again and we start our task again.
848 * A segment seek posts a message on the bus saying that the playback
849 * of the segment started. We store the segment flag internally because
850 * when we reach the segment.stop we have to post a segment.done
851 * instead of EOS when doing a segment seek.
853 /* FIXME, we have the unlock gboolean here because most current implementations
854 * (fdsrc, -base/gst/tcp/, ...) not only unlock when there is something to
858 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
864 GstSeekType cur_type, stop_type;
868 GstSegment seeksegment;
870 GST_DEBUG_OBJECT (src, "doing seek");
873 gst_event_parse_seek (event, &rate, &format, &flags,
874 &cur_type, &cur, &stop_type, &stop);
876 /* we have to have a format as the segment format. Try to convert
878 if (src->segment.format != format) {
881 fmt = src->segment.format;
883 if (cur_type != GST_SEEK_TYPE_NONE)
884 res = gst_pad_query_convert (src->srcpad, format, cur, &fmt, &cur);
885 if (res && stop_type != GST_SEEK_TYPE_NONE)
886 res = gst_pad_query_convert (src->srcpad, format, stop, &fmt, &stop);
896 flush = flags & GST_SEEK_FLAG_FLUSH;
898 /* send flush start */
900 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
902 gst_pad_pause_task (src->srcpad);
904 /* unblock streaming thread */
906 gst_base_src_unlock (src);
908 /* grab streaming lock, this should eventually be possible, either
909 * because the task is paused or our streaming thread stopped
910 * because our peer is flushing. */
911 GST_PAD_STREAM_LOCK (src->srcpad);
913 /* make copy into temp structure, we can only update the main one
914 * when the subclass actually could do the seek. */
915 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
917 /* now configure the seek segment */
919 gst_segment_set_seek (&seeksegment, rate, format, flags,
920 cur_type, cur, stop_type, stop, &update);
923 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
924 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
925 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
927 /* do the seek, segment.last_stop contains new position. */
928 res = gst_base_src_do_seek (src, &seeksegment);
930 /* and prepare to continue streaming */
932 /* send flush stop, peer will accept data and events again. We
933 * are not yet providing data as we still have the STREAM_LOCK. */
934 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
935 } else if (res && src->data.ABI.running) {
936 /* we are running the current segment and doing a non-flushing seek,
937 * close the segment first based on the last_stop. */
938 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
939 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
941 /* queue the segment for sending in the stream thread */
942 if (src->priv->close_segment)
943 gst_event_unref (src->priv->close_segment);
944 src->priv->close_segment =
945 gst_event_new_new_segment_full (TRUE,
946 src->segment.rate, src->segment.applied_rate, src->segment.format,
947 src->segment.start, src->segment.last_stop, src->segment.time);
950 /* if successfull seek, we update our real segment and push
951 * out the new segment. */
953 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
955 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
956 gst_element_post_message (GST_ELEMENT (src),
957 gst_message_new_segment_start (GST_OBJECT (src),
958 src->segment.format, src->segment.last_stop));
961 /* for deriving a stop position for the playback segment form the seek
962 * segment, we must take the duration when the stop is not set */
963 if ((stop = src->segment.stop) == -1)
964 stop = src->segment.duration;
966 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
967 " to %" G_GINT64_FORMAT, src->segment.start, stop);
969 /* now replace the old segment so that we send it in the stream thread the
970 * next time it is scheduled. */
971 if (src->priv->start_segment)
972 gst_event_unref (src->priv->start_segment);
973 src->priv->start_segment =
974 gst_event_new_new_segment_full (FALSE,
975 src->segment.rate, src->segment.applied_rate, src->segment.format,
976 src->segment.last_stop, stop, src->segment.time);
979 src->priv->discont = TRUE;
980 src->data.ABI.running = TRUE;
981 /* and restart the task in case it got paused explicitely or by
982 * the FLUSH_START event we pushed out. */
983 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
986 /* and release the lock again so we can continue streaming */
987 GST_PAD_STREAM_UNLOCK (src->srcpad);
994 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
999 static const GstQueryType *
1000 gst_base_src_get_query_types (GstElement * element)
1002 static const GstQueryType query_types[] = {
1018 /* all events send to this element directly
1021 gst_base_src_send_event (GstElement * element, GstEvent * event)
1024 gboolean result = FALSE;
1026 src = GST_BASE_SRC (element);
1028 switch (GST_EVENT_TYPE (event)) {
1029 case GST_EVENT_FLUSH_START:
1030 case GST_EVENT_FLUSH_STOP:
1031 /* sending random flushes downstream can break stuff,
1032 * especially sync since all segment info will get flushed */
1035 /* FIXME, queue EOS and make sure the task or pull function
1036 * perform the EOS actions. */
1038 case GST_EVENT_NEWSEGMENT:
1039 /* sending random NEWSEGMENT downstream can break sync. */
1042 case GST_EVENT_BUFFERSIZE:
1046 case GST_EVENT_SEEK:
1050 GST_OBJECT_LOCK (src->srcpad);
1051 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1053 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1054 GST_OBJECT_UNLOCK (src->srcpad);
1057 /* when we are running in push mode, we can execute the
1058 * seek right now, we need to unlock. */
1059 result = gst_base_src_perform_seek (src, event, TRUE);
1063 /* else we store the event and execute the seek when we
1065 GST_OBJECT_LOCK (src);
1066 event_p = &src->data.ABI.pending_seek;
1067 gst_event_replace ((GstEvent **) event_p, event);
1068 GST_OBJECT_UNLOCK (src);
1069 /* assume the seek will work */
1074 case GST_EVENT_NAVIGATION:
1080 gst_event_unref (event);
1087 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1088 GST_OBJECT_UNLOCK (src->srcpad);
1095 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1099 switch (GST_EVENT_TYPE (event)) {
1100 case GST_EVENT_SEEK:
1101 /* is normally called when in push mode */
1105 result = gst_base_src_perform_seek (src, event, TRUE);
1107 case GST_EVENT_FLUSH_START:
1108 /* cancel any blocking getrange, is normally called
1109 * when in pull mode. */
1110 result = gst_base_src_unlock (src);
1112 case GST_EVENT_FLUSH_STOP:
1122 GST_DEBUG_OBJECT (src, "is not seekable");
1128 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1131 GstBaseSrcClass *bclass;
1132 gboolean result = FALSE;
1134 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1135 bclass = GST_BASE_SRC_GET_CLASS (src);
1137 if (bclass->event) {
1138 if (!(result = bclass->event (src, event)))
1139 goto subclass_failed;
1143 gst_event_unref (event);
1144 gst_object_unref (src);
1151 GST_DEBUG_OBJECT (src, "subclass refused event");
1157 gst_base_src_set_property (GObject * object, guint prop_id,
1158 const GValue * value, GParamSpec * pspec)
1162 src = GST_BASE_SRC (object);
1165 case PROP_BLOCKSIZE:
1166 src->blocksize = g_value_get_ulong (value);
1168 case PROP_NUM_BUFFERS:
1169 src->num_buffers = g_value_get_int (value);
1172 src->data.ABI.typefind = g_value_get_boolean (value);
1175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1181 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1186 src = GST_BASE_SRC (object);
1189 case PROP_BLOCKSIZE:
1190 g_value_set_ulong (value, src->blocksize);
1192 case PROP_NUM_BUFFERS:
1193 g_value_set_int (value, src->num_buffers);
1196 g_value_set_boolean (value, src->data.ABI.typefind);
1199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1204 /* with STREAM_LOCK and LOCK */
1205 static GstClockReturn
1206 gst_base_src_wait (GstBaseSrc * basesrc, GstClockTime time)
1212 /* get clock, if no clock, we don't sync */
1213 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1214 return GST_CLOCK_OK;
1216 id = gst_clock_new_single_shot_id (clock, time);
1218 basesrc->clock_id = id;
1219 /* release the object lock while waiting */
1220 GST_OBJECT_UNLOCK (basesrc);
1222 ret = gst_clock_id_wait (id, NULL);
1224 GST_OBJECT_LOCK (basesrc);
1225 gst_clock_id_unref (id);
1226 basesrc->clock_id = NULL;
1231 /* perform synchronisation on a buffer.
1234 static GstClockReturn
1235 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1237 GstClockReturn result;
1238 GstClockTime start, end;
1239 GstBaseSrcClass *bclass;
1240 GstClockTime base_time;
1242 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1245 if (bclass->get_times)
1246 bclass->get_times (basesrc, buffer, &start, &end);
1248 /* if we don't have a timestamp, we don't sync */
1249 if (!GST_CLOCK_TIME_IS_VALID (start))
1252 /* now do clocking */
1253 GST_OBJECT_LOCK (basesrc);
1254 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1256 GST_LOG_OBJECT (basesrc,
1257 "waiting for clock, base time %" GST_TIME_FORMAT
1258 ", stream_start %" GST_TIME_FORMAT,
1259 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1261 result = gst_base_src_wait (basesrc, start + base_time);
1262 GST_OBJECT_UNLOCK (basesrc);
1264 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1271 GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1272 return GST_CLOCK_OK;
1277 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1279 guint64 size, maxsize;
1280 GstBaseSrcClass *bclass;
1282 bclass = GST_BASE_SRC_GET_CLASS (src);
1284 /* only operate if we are working with bytes */
1285 if (src->segment.format != GST_FORMAT_BYTES)
1288 /* get total file size */
1289 size = (guint64) src->segment.duration;
1291 /* the max amount of bytes to read is the total size or
1292 * up to the segment.stop if present. */
1293 if (src->segment.stop != -1)
1294 maxsize = MIN (size, src->segment.stop);
1298 GST_DEBUG_OBJECT (src,
1299 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1300 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1301 *length, size, src->segment.stop, maxsize);
1303 /* check size if we have one */
1304 if (maxsize != -1) {
1305 /* if we run past the end, check if the file became bigger and
1307 if (G_UNLIKELY (offset + *length >= maxsize)) {
1308 /* see if length of the file changed */
1309 if (bclass->get_size)
1310 if (!bclass->get_size (src, &size))
1313 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1315 /* make sure we don't exceed the configured segment stop
1317 if (src->segment.stop != -1)
1318 maxsize = MIN (size, src->segment.stop);
1322 /* if we are at or past the end, EOS */
1323 if (G_UNLIKELY (offset >= maxsize))
1324 goto unexpected_length;
1326 /* else we can clip to the end */
1327 if (G_UNLIKELY (offset + *length >= maxsize))
1328 *length = maxsize - offset;
1333 /* keep track of current position. segment is in bytes, we checked
1335 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1346 static GstFlowReturn
1347 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1351 GstBaseSrcClass *bclass;
1352 GstClockReturn status;
1354 bclass = GST_BASE_SRC_GET_CLASS (src);
1356 ret = gst_base_src_wait_playing (src);
1357 if (ret != GST_FLOW_OK)
1360 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1363 if (G_UNLIKELY (!bclass->create))
1366 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1367 goto unexpected_length;
1369 /* normally we don't count buffers */
1370 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1371 if (src->num_buffers_left == 0)
1372 goto reached_num_buffers;
1374 src->num_buffers_left--;
1377 GST_DEBUG_OBJECT (src,
1378 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1379 G_GINT64_FORMAT, offset, length, src->segment.time);
1381 ret = bclass->create (src, offset, length, buf);
1382 if (G_UNLIKELY (ret != GST_FLOW_OK))
1385 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1386 if (offset == 0 && src->segment.time == 0
1387 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1388 GST_BUFFER_TIMESTAMP (*buf) = 0;
1390 /* now sync before pushing the buffer */
1391 status = gst_base_src_do_sync (src, *buf);
1393 case GST_CLOCK_EARLY:
1394 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1397 GST_DEBUG_OBJECT (src, "buffer ok");
1400 /* this case is triggered when we were waiting for the clock and
1401 * it got unlocked because we did a state change. We return
1402 * WRONG_STATE in this case to stop the dataflow also get rid of the
1403 * produced buffer. */
1404 GST_DEBUG_OBJECT (src, "clock returned %d, not returning", status);
1405 gst_buffer_unref (*buf);
1407 ret = GST_FLOW_WRONG_STATE;
1416 GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
1421 GST_DEBUG_OBJECT (src, "getrange but not started");
1422 return GST_FLOW_WRONG_STATE;
1426 GST_DEBUG_OBJECT (src, "no create function");
1427 return GST_FLOW_ERROR;
1431 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1432 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1433 return GST_FLOW_UNEXPECTED;
1435 reached_num_buffers:
1437 GST_DEBUG_OBJECT (src, "sent all buffers");
1438 return GST_FLOW_UNEXPECTED;
1442 static GstFlowReturn
1443 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1449 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1451 res = gst_base_src_get_range (src, offset, length, buf);
1453 gst_object_unref (src);
1459 gst_base_src_default_check_get_range (GstBaseSrc * src)
1463 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1464 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1465 if (G_LIKELY (gst_base_src_start (src)))
1466 gst_base_src_stop (src);
1469 /* we can operate in getrange mode if the native format is bytes
1470 * and we are seekable, this condition is set in the random_access
1471 * flag and is set in the _start() method. */
1472 res = src->random_access;
1478 gst_base_src_check_get_range (GstBaseSrc * src)
1480 GstBaseSrcClass *bclass;
1483 bclass = GST_BASE_SRC_GET_CLASS (src);
1485 if (bclass->check_get_range == NULL)
1488 res = bclass->check_get_range (src);
1489 GST_LOG_OBJECT (src, "%s() returned %d",
1490 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1497 GST_WARNING_OBJECT (src, "no check_get_range function set");
1503 gst_base_src_pad_check_get_range (GstPad * pad)
1508 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1510 res = gst_base_src_check_get_range (src);
1512 gst_object_unref (src);
1518 gst_base_src_loop (GstPad * pad)
1521 GstBuffer *buf = NULL;
1528 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1530 src->priv->last_sent_eos = FALSE;
1532 /* push events to close/start our segment before we do the real work. */
1533 if (src->priv->close_segment) {
1534 gst_pad_push_event (pad, src->priv->close_segment);
1535 src->priv->close_segment = NULL;
1537 if (src->priv->start_segment) {
1538 gst_pad_push_event (pad, src->priv->start_segment);
1539 src->priv->start_segment = NULL;
1542 /* if we operate in bytes, we can calculate an offset */
1543 if (src->segment.format == GST_FORMAT_BYTES)
1544 position = src->segment.last_stop;
1548 ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1549 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1550 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %d", ret);
1553 if (G_UNLIKELY (buf == NULL))
1556 /* figure out the new position */
1557 switch (src->segment.format) {
1558 case GST_FORMAT_BYTES:
1559 position += GST_BUFFER_SIZE (buf);
1561 case GST_FORMAT_TIME:
1563 GstClockTime start, duration;
1565 start = GST_BUFFER_TIMESTAMP (buf);
1566 duration = GST_BUFFER_DURATION (buf);
1568 if (GST_CLOCK_TIME_IS_VALID (start))
1571 position = src->segment.last_stop;
1573 if (GST_CLOCK_TIME_IS_VALID (duration))
1574 position += duration;
1577 case GST_FORMAT_DEFAULT:
1578 position = GST_BUFFER_OFFSET_END (buf);
1584 if (position != -1) {
1585 if (src->segment.stop != -1) {
1586 if (position >= src->segment.stop) {
1588 position = src->segment.stop;
1591 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1594 if (G_UNLIKELY (src->priv->discont)) {
1595 buf = gst_buffer_make_metadata_writable (buf);
1596 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1597 src->priv->discont = FALSE;
1600 ret = gst_pad_push (pad, buf);
1601 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1602 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %d", ret);
1607 GST_INFO_OBJECT (src, "pausing after EOS");
1608 ret = GST_FLOW_UNEXPECTED;
1613 gst_object_unref (src);
1619 const gchar *reason = gst_flow_get_name (ret);
1621 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1622 src->data.ABI.running = FALSE;
1623 gst_pad_pause_task (pad);
1624 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1625 if (ret == GST_FLOW_UNEXPECTED) {
1626 /* perform EOS logic */
1627 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1628 gst_element_post_message (GST_ELEMENT_CAST (src),
1629 gst_message_new_segment_done (GST_OBJECT_CAST (src),
1630 src->segment.format, src->segment.last_stop));
1632 gst_pad_push_event (pad, gst_event_new_eos ());
1633 src->priv->last_sent_eos = TRUE;
1636 /* for fatal errors we post an error message, post the error
1637 * first so the app knows about the error first. */
1638 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1639 (_("Internal data flow error.")),
1640 ("streaming task paused, reason %s (%d)", reason, ret));
1641 gst_pad_push_event (pad, gst_event_new_eos ());
1642 src->priv->last_sent_eos = TRUE;
1649 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1650 (_("Internal data flow error.")), ("element returned NULL buffer"));
1651 /* we finished the segment on error */
1652 src->data.ABI.running = FALSE;
1653 gst_pad_pause_task (pad);
1654 gst_pad_push_event (pad, gst_event_new_eos ());
1655 src->priv->last_sent_eos = TRUE;
1660 /* this will always be called between start() and stop(). So you can rely on
1661 * resources allocated by start() and freed from stop(). This needs to be added
1662 * to the docs at some point. */
1664 gst_base_src_unlock (GstBaseSrc * basesrc)
1666 GstBaseSrcClass *bclass;
1667 gboolean result = TRUE;
1669 GST_DEBUG ("unlock");
1670 /* unblock whatever the subclass is doing */
1671 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1673 result = bclass->unlock (basesrc);
1675 GST_DEBUG ("unschedule clock");
1676 /* and unblock the clock as well, if any */
1677 GST_OBJECT_LOCK (basesrc);
1678 if (basesrc->clock_id) {
1679 gst_clock_id_unschedule (basesrc->clock_id);
1681 GST_OBJECT_UNLOCK (basesrc);
1683 GST_DEBUG ("unlock done");
1688 /* default negotiation code.
1690 * Take intersection between src and sink pads, take first
1694 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1697 GstCaps *caps = NULL;
1698 GstCaps *peercaps = NULL;
1699 gboolean result = FALSE;
1701 /* first see what is possible on our source pad */
1702 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
1703 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1704 /* nothing or anything is allowed, we're done */
1705 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1706 goto no_nego_needed;
1708 /* get the peer caps */
1709 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
1710 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1714 /* get intersection */
1715 icaps = gst_caps_intersect (thiscaps, peercaps);
1716 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
1717 gst_caps_unref (thiscaps);
1718 gst_caps_unref (peercaps);
1720 /* take first (and best, since they are sorted) possibility */
1721 caps = gst_caps_copy_nth (icaps, 0);
1722 gst_caps_unref (icaps);
1725 /* no peer, work with our own caps then */
1729 caps = gst_caps_make_writable (caps);
1730 gst_caps_truncate (caps);
1733 if (!gst_caps_is_empty (caps)) {
1734 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
1735 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1737 if (gst_caps_is_any (caps)) {
1738 /* hmm, still anything, so element can do anything and
1739 * nego is not needed */
1741 } else if (gst_caps_is_fixed (caps)) {
1742 /* yay, fixed caps, use those then */
1743 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1747 gst_caps_unref (caps);
1753 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1755 gst_caps_unref (thiscaps);
1761 gst_base_src_negotiate (GstBaseSrc * basesrc)
1763 GstBaseSrcClass *bclass;
1764 gboolean result = TRUE;
1766 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1768 if (bclass->negotiate)
1769 result = bclass->negotiate (basesrc);
1775 gst_base_src_start (GstBaseSrc * basesrc)
1777 GstBaseSrcClass *bclass;
1781 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1784 GST_DEBUG_OBJECT (basesrc, "starting source");
1786 basesrc->num_buffers_left = basesrc->num_buffers;
1788 gst_segment_init (&basesrc->segment, basesrc->segment.format);
1789 basesrc->data.ABI.running = FALSE;
1791 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1793 result = bclass->start (basesrc);
1798 goto could_not_start;
1800 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
1802 /* figure out the size */
1803 if (basesrc->segment.format == GST_FORMAT_BYTES) {
1804 if (bclass->get_size) {
1805 if (!(result = bclass->get_size (basesrc, &size)))
1811 /* only update the size when operating in bytes, subclass is supposed
1812 * to set duration in the start method for other formats */
1813 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
1818 GST_DEBUG_OBJECT (basesrc,
1819 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
1820 G_GINT64_FORMAT, basesrc->segment.format, result, size,
1821 basesrc->segment.duration);
1823 /* check if we can seek */
1824 if (bclass->is_seekable)
1825 basesrc->seekable = bclass->is_seekable (basesrc);
1827 basesrc->seekable = FALSE;
1829 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
1831 /* update for random access flag */
1832 basesrc->random_access = basesrc->seekable &&
1833 basesrc->segment.format == GST_FORMAT_BYTES;
1835 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
1837 /* run typefind if we are random_access and the typefinding is enabled. */
1838 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
1841 caps = gst_type_find_helper (basesrc->srcpad, size);
1842 gst_pad_set_caps (basesrc->srcpad, caps);
1843 gst_caps_unref (caps);
1845 /* use class or default negotiate function */
1846 if (!gst_base_src_negotiate (basesrc))
1847 goto could_not_negotiate;
1855 GST_DEBUG_OBJECT (basesrc, "could not start");
1856 /* subclass is supposed to post a message. We don't have to call _stop. */
1859 could_not_negotiate:
1861 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
1862 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
1863 ("Could not negotiate format"), ("Check your filtered caps, if any"));
1864 /* we must call stop */
1865 gst_base_src_stop (basesrc);
1871 gst_base_src_stop (GstBaseSrc * basesrc)
1873 GstBaseSrcClass *bclass;
1874 gboolean result = TRUE;
1876 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1879 GST_DEBUG_OBJECT (basesrc, "stopping source");
1881 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1883 result = bclass->stop (basesrc);
1886 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
1892 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
1896 GST_LIVE_LOCK (basesrc);
1897 basesrc->live_running = TRUE;
1898 GST_LIVE_SIGNAL (basesrc);
1899 GST_LIVE_UNLOCK (basesrc);
1901 /* step 1, unblock clock sync (if any) */
1902 result = gst_base_src_unlock (basesrc);
1904 /* step 2, make sure streaming finishes */
1905 result &= gst_pad_stop_task (pad);
1911 gst_base_src_activate_push (GstPad * pad, gboolean active)
1913 GstBaseSrc *basesrc;
1916 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1918 /* prepare subclass first */
1920 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1922 if (G_UNLIKELY (!basesrc->can_activate_push))
1923 goto no_push_activation;
1925 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1928 basesrc->priv->last_sent_eos = FALSE;
1930 /* do initial seek, which will start the task */
1931 GST_OBJECT_LOCK (basesrc);
1932 event = basesrc->data.ABI.pending_seek;
1933 basesrc->data.ABI.pending_seek = NULL;
1934 GST_OBJECT_UNLOCK (basesrc);
1936 /* no need to unlock anything, the task is certainly
1937 * not running here. The perform seek code will start the task when
1939 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
1943 gst_event_unref (event);
1945 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1946 /* call the unlock function and stop the task */
1947 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
1948 goto deactivate_failed;
1950 /* now we can stop the source */
1951 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
1959 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
1964 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
1969 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
1970 gst_base_src_stop (basesrc);
1972 gst_event_unref (event);
1977 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
1982 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
1988 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1990 GstBaseSrc *basesrc;
1992 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1994 /* prepare subclass first */
1996 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1997 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2000 /* if not random_access, we cannot operate in pull mode for now */
2001 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2004 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2005 /* call the unlock function. We have no task to stop. */
2006 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
2007 goto deactivate_failed;
2009 /* don't send EOS when going from PAUSED => READY when in pull mode */
2010 basesrc->priv->last_sent_eos = TRUE;
2012 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2020 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2025 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2026 gst_base_src_stop (basesrc);
2031 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
2036 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2041 static GstStateChangeReturn
2042 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2044 GstBaseSrc *basesrc;
2045 GstStateChangeReturn result;
2046 gboolean no_preroll = FALSE;
2048 basesrc = GST_BASE_SRC (element);
2050 switch (transition) {
2051 case GST_STATE_CHANGE_NULL_TO_READY:
2053 case GST_STATE_CHANGE_READY_TO_PAUSED:
2054 GST_LIVE_LOCK (element);
2055 if (basesrc->is_live) {
2057 basesrc->live_running = FALSE;
2059 basesrc->priv->last_sent_eos = FALSE;
2060 basesrc->priv->discont = TRUE;
2061 GST_LIVE_UNLOCK (element);
2063 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2064 GST_LIVE_LOCK (element);
2065 if (basesrc->is_live) {
2066 basesrc->live_running = TRUE;
2067 GST_LIVE_SIGNAL (element);
2069 GST_LIVE_UNLOCK (element);
2076 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2077 transition)) == GST_STATE_CHANGE_FAILURE)
2080 switch (transition) {
2081 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2082 GST_LIVE_LOCK (element);
2083 if (basesrc->is_live) {
2085 basesrc->live_running = FALSE;
2087 GST_LIVE_UNLOCK (element);
2089 case GST_STATE_CHANGE_PAUSED_TO_READY:
2093 /* FIXME, deprecate this behaviour, it is very dangerous.
2094 * the prefered way of sending EOS downstream is by sending
2095 * the EOS event to the element */
2096 if (!basesrc->priv->last_sent_eos) {
2097 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2098 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2099 basesrc->priv->last_sent_eos = TRUE;
2101 event_p = &basesrc->data.ABI.pending_seek;
2102 gst_event_replace (event_p, NULL);
2103 event_p = &basesrc->priv->close_segment;
2104 gst_event_replace (event_p, NULL);
2105 event_p = &basesrc->priv->start_segment;
2106 gst_event_replace (event_p, NULL);
2109 case GST_STATE_CHANGE_READY_TO_NULL:
2115 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2116 result = GST_STATE_CHANGE_NO_PREROLL;
2123 GST_DEBUG_OBJECT (basesrc, "parent failed state change");