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_unlock_stop (GstBaseSrc * basesrc);
294 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
295 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
297 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
298 GstStateChange transition);
300 static void gst_base_src_loop (GstPad * pad);
301 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
302 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
303 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
304 guint length, GstBuffer ** buf);
305 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
306 guint length, GstBuffer ** buf);
309 gst_base_src_base_init (gpointer g_class)
311 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
315 gst_base_src_class_init (GstBaseSrcClass * klass)
317 GObjectClass *gobject_class;
318 GstElementClass *gstelement_class;
320 gobject_class = G_OBJECT_CLASS (klass);
321 gstelement_class = GST_ELEMENT_CLASS (klass);
323 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
325 parent_class = g_type_class_peek_parent (klass);
327 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
328 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
329 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
331 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
332 g_param_spec_ulong ("blocksize", "Block size",
333 "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
334 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
335 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
336 g_param_spec_int ("num-buffers", "num-buffers",
337 "Number of buffers to output before sending EOS", -1, G_MAXINT,
338 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
339 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
340 g_param_spec_boolean ("typefind", "Typefind",
341 "Run typefind before negotiating", DEFAULT_TYPEFIND,
344 gstelement_class->change_state =
345 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
346 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
347 gstelement_class->get_query_types =
348 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
350 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
351 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
352 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
353 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
354 klass->check_get_range =
355 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
359 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
362 GstPadTemplate *pad_template;
364 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
366 basesrc->is_live = FALSE;
367 basesrc->live_lock = g_mutex_new ();
368 basesrc->live_cond = g_cond_new ();
369 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
370 basesrc->num_buffers_left = -1;
372 basesrc->can_activate_push = TRUE;
373 basesrc->pad_mode = GST_ACTIVATE_NONE;
376 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
377 g_return_if_fail (pad_template != NULL);
379 GST_DEBUG_OBJECT (basesrc, "creating src pad");
380 pad = gst_pad_new_from_template (pad_template, "src");
382 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
383 gst_pad_set_activatepush_function (pad,
384 GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
385 gst_pad_set_activatepull_function (pad,
386 GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
387 gst_pad_set_event_function (pad,
388 GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
389 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
390 gst_pad_set_checkgetrange_function (pad,
391 GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
392 gst_pad_set_getrange_function (pad,
393 GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
394 gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
395 gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
396 gst_pad_set_fixatecaps_function (pad,
397 GST_DEBUG_FUNCPTR (gst_base_src_fixate));
399 /* hold pointer to pad */
400 basesrc->srcpad = pad;
401 GST_DEBUG_OBJECT (basesrc, "adding src pad");
402 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
404 basesrc->blocksize = DEFAULT_BLOCKSIZE;
405 basesrc->clock_id = NULL;
406 /* we operate in BYTES by default */
407 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
408 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
410 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
412 GST_DEBUG_OBJECT (basesrc, "init done");
416 gst_base_src_finalize (GObject * object)
421 basesrc = GST_BASE_SRC (object);
423 g_mutex_free (basesrc->live_lock);
424 g_cond_free (basesrc->live_cond);
426 event_p = &basesrc->data.ABI.pending_seek;
427 gst_event_replace ((GstEvent **) event_p, NULL);
429 G_OBJECT_CLASS (parent_class)->finalize (object);
433 * gst_base_src_wait_playing:
436 * If the #GstBaseSrcClass::create method performs its own synchronisation against
437 * the clock it must unblock when going from PLAYING to the PAUSED state and call
438 * this method before continuing to produce the remaining data.
440 * This function will block until a state change to PLAYING happens (in which
441 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
442 * to a state change to READY or a FLUSH event (in which case this function
443 * returns #GST_FLOW_WRONG_STATE).
447 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
448 * continue. Any other return value should be returned from the create vmethod.
451 gst_base_src_wait_playing (GstBaseSrc * src)
453 /* block until the state changes, or we get a flush, or something */
456 while (G_UNLIKELY (!src->live_running)) {
457 GST_DEBUG ("live source signal waiting");
458 GST_LIVE_SIGNAL (src);
459 GST_DEBUG ("live source waiting for running state");
461 GST_DEBUG ("live source unlocked");
463 /* FIXME, use another variable to signal stopping so that we don't
464 * have to grab another lock. */
465 GST_OBJECT_LOCK (src->srcpad);
466 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
468 GST_OBJECT_UNLOCK (src->srcpad);
470 GST_LIVE_UNLOCK (src);
477 GST_DEBUG_OBJECT (src, "pad is flushing");
478 GST_OBJECT_UNLOCK (src->srcpad);
479 GST_LIVE_UNLOCK (src);
480 return GST_FLOW_WRONG_STATE;
485 * gst_base_src_set_live:
486 * @src: base source instance
487 * @live: new live-mode
489 * If the element listens to a live source, @live should
492 * A live source will not produce data in the PAUSED state and
493 * will therefore not be able to participate in the PREROLL phase
494 * of a pipeline. To signal this fact to the application and the
495 * pipeline, the state change return value of the live source will
496 * be GST_STATE_CHANGE_NO_PREROLL.
499 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
503 GST_LIVE_UNLOCK (src);
507 * gst_base_src_is_live:
508 * @src: base source instance
510 * Check if an element is in live mode.
512 * Returns: %TRUE if element is in live mode.
515 gst_base_src_is_live (GstBaseSrc * src)
520 result = src->is_live;
521 GST_LIVE_UNLOCK (src);
527 * gst_base_src_set_format:
528 * @src: base source instance
529 * @format: the format to use
531 * Sets the default format of the source. This will be the format used
532 * for sending NEW_SEGMENT events and for performing seeks.
534 * If a format of GST_FORMAT_BYTES is set, the element will be able to
535 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
540 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
542 gst_segment_init (&src->segment, format);
546 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
548 GstBaseSrcClass *bclass;
552 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
553 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
555 if (bclass->set_caps)
556 res = bclass->set_caps (bsrc, caps);
562 gst_base_src_getcaps (GstPad * pad)
564 GstBaseSrcClass *bclass;
566 GstCaps *caps = NULL;
568 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
569 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
570 if (bclass->get_caps)
571 caps = bclass->get_caps (bsrc);
574 GstPadTemplate *pad_template;
577 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
578 if (pad_template != NULL) {
579 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
586 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
588 GstBaseSrcClass *bclass;
591 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
592 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
595 bclass->fixate (bsrc, caps);
597 gst_object_unref (bsrc);
601 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
605 switch (GST_QUERY_TYPE (query)) {
606 case GST_QUERY_POSITION:
610 gst_query_parse_position (query, &format, NULL);
612 case GST_FORMAT_PERCENT:
618 position = src->segment.last_stop;
619 duration = src->segment.duration;
621 if (position != -1 && duration != -1) {
622 if (position < duration)
623 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
626 percent = GST_FORMAT_PERCENT_MAX;
630 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
638 position = src->segment.last_stop;
640 if (position != -1) {
641 /* convert to requested format */
643 gst_pad_query_convert (src->srcpad, src->segment.format,
644 position, &format, &position);
648 gst_query_set_position (query, format, position);
654 case GST_QUERY_DURATION:
658 gst_query_parse_duration (query, &format, NULL);
660 case GST_FORMAT_PERCENT:
661 gst_query_set_duration (query, GST_FORMAT_PERCENT,
662 GST_FORMAT_PERCENT_MAX);
669 duration = src->segment.duration;
671 if (duration != -1) {
672 /* convert to requested format */
674 gst_pad_query_convert (src->srcpad, src->segment.format,
675 duration, &format, &duration);
679 gst_query_set_duration (query, format, duration);
686 case GST_QUERY_SEEKING:
688 gst_query_set_seeking (query, src->segment.format,
689 src->seekable, 0, src->segment.duration);
693 case GST_QUERY_SEGMENT:
697 /* no end segment configured, current duration then */
698 if ((stop = src->segment.stop) == -1)
699 stop = src->segment.duration;
700 start = src->segment.start;
702 /* adjust to stream time */
703 if (src->segment.time != -1) {
704 start -= src->segment.time;
706 stop -= src->segment.time;
708 gst_query_set_segment (query, src->segment.rate, src->segment.format,
714 case GST_QUERY_FORMATS:
716 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
717 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
721 case GST_QUERY_CONVERT:
723 GstFormat src_fmt, dest_fmt;
724 gint64 src_val, dest_val;
726 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
728 /* we can only convert between equal formats... */
729 if (src_fmt == dest_fmt) {
735 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
738 case GST_QUERY_LATENCY:
739 /* we can only report the fact that we are live or not, we know nothing
740 * about latency. Subclasses should override and implement something
743 gst_query_set_latency (query, src->is_live, 0, -1);
744 GST_LIVE_UNLOCK (src);
747 case GST_QUERY_JITTER:
757 gst_base_src_query (GstPad * pad, GstQuery * query)
760 GstBaseSrcClass *bclass;
761 gboolean result = FALSE;
763 src = GST_BASE_SRC (gst_pad_get_parent (pad));
765 bclass = GST_BASE_SRC_GET_CLASS (src);
768 result = bclass->query (src, query);
770 result = gst_pad_query_default (pad, query);
772 gst_object_unref (src);
778 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
782 /* update our offset if the start/stop position was updated */
783 if (segment->format == GST_FORMAT_BYTES) {
784 segment->last_stop = segment->start;
785 segment->time = segment->start;
786 } else if (segment->start == 0) {
787 /* seek to start, we can implement a default for this. */
788 segment->last_stop = 0;
798 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
800 GstBaseSrcClass *bclass;
801 gboolean result = FALSE;
803 bclass = GST_BASE_SRC_GET_CLASS (src);
806 result = bclass->do_seek (src, segment);
811 /* this code implements the seeking. It is a good example
812 * handling all cases.
814 * A seek updates the currently configured segment.start
815 * and segment.stop values based on the SEEK_TYPE. If the
816 * segment.start value is updated, a seek to this new position
817 * should be performed.
819 * The seek can only be executed when we are not currently
820 * streaming any data, to make sure that this is the case, we
821 * acquire the STREAM_LOCK which is taken when we are in the
822 * _loop() function or when a getrange() is called. Normally
823 * we will not receive a seek if we are operating in pull mode
826 * When we are in the loop() function, we might be in the middle
827 * of pushing a buffer, which might block in a sink. To make sure
828 * that the push gets unblocked we push out a FLUSH_START event.
829 * Our loop function will get a WRONG_STATE return value from
830 * the push and will pause, effectively releasing the STREAM_LOCK.
832 * For a non-flushing seek, we pause the task, which might eventually
833 * release the STREAM_LOCK. We say eventually because when the sink
834 * blocks on the sample we might wait a very long time until the sink
835 * unblocks the sample. In any case we acquire the STREAM_LOCK and
836 * can continue the seek. A non-flushing seek is normally done in a
837 * running pipeline to perform seamless playback.
838 * In the case of a non-flushing seek we need to make sure that the
839 * data we output after the seek is continuous with the previous data,
840 * this is because a non-flushing seek does not reset the stream-time
841 * to 0. We do this by closing the currently running segment, ie. sending
842 * a new_segment event with the stop position set to the last processed
845 * After updating the segment.start/stop values, we prepare for
846 * streaming again. We push out a FLUSH_STOP to make the peer pad
847 * accept data again and we start our task again.
849 * A segment seek posts a message on the bus saying that the playback
850 * of the segment started. We store the segment flag internally because
851 * when we reach the segment.stop we have to post a segment.done
852 * instead of EOS when doing a segment seek.
854 /* FIXME (0.11), we have the unlock gboolean here because most current
855 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
856 * the streaming thread isn't running, resulting in bogus unlocks later when it
857 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
858 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
862 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
868 GstSeekType cur_type, stop_type;
872 GstSegment seeksegment;
874 GST_DEBUG_OBJECT (src, "doing seek");
877 gst_event_parse_seek (event, &rate, &format, &flags,
878 &cur_type, &cur, &stop_type, &stop);
880 /* we have to have a format as the segment format. Try to convert
882 if (src->segment.format != format) {
885 fmt = src->segment.format;
887 if (cur_type != GST_SEEK_TYPE_NONE)
888 res = gst_pad_query_convert (src->srcpad, format, cur, &fmt, &cur);
889 if (res && stop_type != GST_SEEK_TYPE_NONE)
890 res = gst_pad_query_convert (src->srcpad, format, stop, &fmt, &stop);
900 flush = flags & GST_SEEK_FLAG_FLUSH;
902 /* send flush start */
904 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
906 gst_pad_pause_task (src->srcpad);
908 /* unblock streaming thread */
910 gst_base_src_unlock (src);
912 /* grab streaming lock, this should eventually be possible, either
913 * because the task is paused or our streaming thread stopped
914 * because our peer is flushing. */
915 GST_PAD_STREAM_LOCK (src->srcpad);
918 gst_base_src_unlock_stop (src);
920 /* make copy into temp structure, we can only update the main one
921 * when the subclass actually could do the seek. */
922 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
924 /* now configure the seek segment */
926 gst_segment_set_seek (&seeksegment, rate, format, flags,
927 cur_type, cur, stop_type, stop, &update);
930 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
931 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
932 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
934 /* do the seek, segment.last_stop contains new position. */
935 res = gst_base_src_do_seek (src, &seeksegment);
937 /* and prepare to continue streaming */
939 /* send flush stop, peer will accept data and events again. We
940 * are not yet providing data as we still have the STREAM_LOCK. */
941 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
942 } else if (res && src->data.ABI.running) {
943 /* we are running the current segment and doing a non-flushing seek,
944 * close the segment first based on the last_stop. */
945 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
946 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
948 /* queue the segment for sending in the stream thread */
949 if (src->priv->close_segment)
950 gst_event_unref (src->priv->close_segment);
951 src->priv->close_segment =
952 gst_event_new_new_segment_full (TRUE,
953 src->segment.rate, src->segment.applied_rate, src->segment.format,
954 src->segment.start, src->segment.last_stop, src->segment.time);
957 /* if successfull seek, we update our real segment and push
958 * out the new segment. */
960 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
962 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
963 gst_element_post_message (GST_ELEMENT (src),
964 gst_message_new_segment_start (GST_OBJECT (src),
965 src->segment.format, src->segment.last_stop));
968 /* for deriving a stop position for the playback segment form the seek
969 * segment, we must take the duration when the stop is not set */
970 if ((stop = src->segment.stop) == -1)
971 stop = src->segment.duration;
973 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
974 " to %" G_GINT64_FORMAT, src->segment.start, stop);
976 /* now replace the old segment so that we send it in the stream thread the
977 * next time it is scheduled. */
978 if (src->priv->start_segment)
979 gst_event_unref (src->priv->start_segment);
980 src->priv->start_segment =
981 gst_event_new_new_segment_full (FALSE,
982 src->segment.rate, src->segment.applied_rate, src->segment.format,
983 src->segment.last_stop, stop, src->segment.time);
986 src->priv->discont = TRUE;
987 src->data.ABI.running = TRUE;
988 /* and restart the task in case it got paused explicitely or by
989 * the FLUSH_START event we pushed out. */
990 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
993 /* and release the lock again so we can continue streaming */
994 GST_PAD_STREAM_UNLOCK (src->srcpad);
1001 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1006 static const GstQueryType *
1007 gst_base_src_get_query_types (GstElement * element)
1009 static const GstQueryType query_types[] = {
1025 /* all events send to this element directly
1028 gst_base_src_send_event (GstElement * element, GstEvent * event)
1031 gboolean result = FALSE;
1033 src = GST_BASE_SRC (element);
1035 switch (GST_EVENT_TYPE (event)) {
1036 case GST_EVENT_FLUSH_START:
1037 case GST_EVENT_FLUSH_STOP:
1038 /* sending random flushes downstream can break stuff,
1039 * especially sync since all segment info will get flushed */
1042 /* FIXME, queue EOS and make sure the task or pull function
1043 * perform the EOS actions. */
1045 case GST_EVENT_NEWSEGMENT:
1046 /* sending random NEWSEGMENT downstream can break sync. */
1049 case GST_EVENT_BUFFERSIZE:
1053 case GST_EVENT_SEEK:
1057 GST_OBJECT_LOCK (src->srcpad);
1058 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1060 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1061 GST_OBJECT_UNLOCK (src->srcpad);
1064 /* when we are running in push mode, we can execute the
1065 * seek right now, we need to unlock. */
1066 result = gst_base_src_perform_seek (src, event, TRUE);
1070 /* else we store the event and execute the seek when we
1072 GST_OBJECT_LOCK (src);
1073 event_p = &src->data.ABI.pending_seek;
1074 gst_event_replace ((GstEvent **) event_p, event);
1075 GST_OBJECT_UNLOCK (src);
1076 /* assume the seek will work */
1081 case GST_EVENT_NAVIGATION:
1087 gst_event_unref (event);
1094 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1095 GST_OBJECT_UNLOCK (src->srcpad);
1102 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1106 switch (GST_EVENT_TYPE (event)) {
1107 case GST_EVENT_SEEK:
1108 /* is normally called when in push mode */
1112 result = gst_base_src_perform_seek (src, event, TRUE);
1114 case GST_EVENT_FLUSH_START:
1115 /* cancel any blocking getrange, is normally called
1116 * when in pull mode. */
1117 result = gst_base_src_unlock (src);
1119 case GST_EVENT_FLUSH_STOP:
1120 result = gst_base_src_unlock_stop (src);
1131 GST_DEBUG_OBJECT (src, "is not seekable");
1137 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1140 GstBaseSrcClass *bclass;
1141 gboolean result = FALSE;
1143 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1144 bclass = GST_BASE_SRC_GET_CLASS (src);
1146 if (bclass->event) {
1147 if (!(result = bclass->event (src, event)))
1148 goto subclass_failed;
1152 gst_event_unref (event);
1153 gst_object_unref (src);
1160 GST_DEBUG_OBJECT (src, "subclass refused event");
1166 gst_base_src_set_property (GObject * object, guint prop_id,
1167 const GValue * value, GParamSpec * pspec)
1171 src = GST_BASE_SRC (object);
1174 case PROP_BLOCKSIZE:
1175 src->blocksize = g_value_get_ulong (value);
1177 case PROP_NUM_BUFFERS:
1178 src->num_buffers = g_value_get_int (value);
1181 src->data.ABI.typefind = g_value_get_boolean (value);
1184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1190 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1195 src = GST_BASE_SRC (object);
1198 case PROP_BLOCKSIZE:
1199 g_value_set_ulong (value, src->blocksize);
1201 case PROP_NUM_BUFFERS:
1202 g_value_set_int (value, src->num_buffers);
1205 g_value_set_boolean (value, src->data.ABI.typefind);
1208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1213 /* with STREAM_LOCK and LOCK */
1214 static GstClockReturn
1215 gst_base_src_wait (GstBaseSrc * basesrc, GstClockTime time)
1221 /* get clock, if no clock, we don't sync */
1222 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1223 return GST_CLOCK_OK;
1225 id = gst_clock_new_single_shot_id (clock, time);
1227 basesrc->clock_id = id;
1228 /* release the object lock while waiting */
1229 GST_OBJECT_UNLOCK (basesrc);
1231 ret = gst_clock_id_wait (id, NULL);
1233 GST_OBJECT_LOCK (basesrc);
1234 gst_clock_id_unref (id);
1235 basesrc->clock_id = NULL;
1240 /* perform synchronisation on a buffer.
1243 static GstClockReturn
1244 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1246 GstClockReturn result;
1247 GstClockTime start, end;
1248 GstBaseSrcClass *bclass;
1249 GstClockTime base_time;
1251 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1254 if (bclass->get_times)
1255 bclass->get_times (basesrc, buffer, &start, &end);
1257 /* if we don't have a timestamp, we don't sync */
1258 if (!GST_CLOCK_TIME_IS_VALID (start))
1261 /* now do clocking */
1262 GST_OBJECT_LOCK (basesrc);
1263 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1265 GST_LOG_OBJECT (basesrc,
1266 "waiting for clock, base time %" GST_TIME_FORMAT
1267 ", stream_start %" GST_TIME_FORMAT,
1268 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1270 result = gst_base_src_wait (basesrc, start + base_time);
1271 GST_OBJECT_UNLOCK (basesrc);
1273 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1280 GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1281 return GST_CLOCK_OK;
1286 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1288 guint64 size, maxsize;
1289 GstBaseSrcClass *bclass;
1291 bclass = GST_BASE_SRC_GET_CLASS (src);
1293 /* only operate if we are working with bytes */
1294 if (src->segment.format != GST_FORMAT_BYTES)
1297 /* get total file size */
1298 size = (guint64) src->segment.duration;
1300 /* the max amount of bytes to read is the total size or
1301 * up to the segment.stop if present. */
1302 if (src->segment.stop != -1)
1303 maxsize = MIN (size, src->segment.stop);
1307 GST_DEBUG_OBJECT (src,
1308 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1309 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1310 *length, size, src->segment.stop, maxsize);
1312 /* check size if we have one */
1313 if (maxsize != -1) {
1314 /* if we run past the end, check if the file became bigger and
1316 if (G_UNLIKELY (offset + *length >= maxsize)) {
1317 /* see if length of the file changed */
1318 if (bclass->get_size)
1319 if (!bclass->get_size (src, &size))
1322 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1324 /* make sure we don't exceed the configured segment stop
1326 if (src->segment.stop != -1)
1327 maxsize = MIN (size, src->segment.stop);
1331 /* if we are at or past the end, EOS */
1332 if (G_UNLIKELY (offset >= maxsize))
1333 goto unexpected_length;
1335 /* else we can clip to the end */
1336 if (G_UNLIKELY (offset + *length >= maxsize))
1337 *length = maxsize - offset;
1342 /* keep track of current position. segment is in bytes, we checked
1344 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1355 static GstFlowReturn
1356 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1360 GstBaseSrcClass *bclass;
1361 GstClockReturn status;
1363 bclass = GST_BASE_SRC_GET_CLASS (src);
1365 ret = gst_base_src_wait_playing (src);
1366 if (ret != GST_FLOW_OK)
1369 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1372 if (G_UNLIKELY (!bclass->create))
1375 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1376 goto unexpected_length;
1378 /* normally we don't count buffers */
1379 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1380 if (src->num_buffers_left == 0)
1381 goto reached_num_buffers;
1383 src->num_buffers_left--;
1386 GST_DEBUG_OBJECT (src,
1387 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1388 G_GINT64_FORMAT, offset, length, src->segment.time);
1390 ret = bclass->create (src, offset, length, buf);
1391 if (G_UNLIKELY (ret != GST_FLOW_OK))
1394 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1395 if (offset == 0 && src->segment.time == 0
1396 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1397 GST_BUFFER_TIMESTAMP (*buf) = 0;
1399 /* now sync before pushing the buffer */
1400 status = gst_base_src_do_sync (src, *buf);
1402 case GST_CLOCK_EARLY:
1403 /* the buffer is too late. We currently don't drop the buffer. */
1404 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1407 /* buffer synchronised properly */
1408 GST_DEBUG_OBJECT (src, "buffer ok");
1410 case GST_CLOCK_UNSCHEDULED:
1411 /* this case is triggered when we were waiting for the clock and
1412 * it got unlocked because we did a state change. We return
1413 * WRONG_STATE in this case to stop the dataflow also get rid of the
1414 * produced buffer. */
1415 GST_DEBUG_OBJECT (src,
1416 "clock was unscheduled (%d), returning WRONG_STATE", status);
1417 gst_buffer_unref (*buf);
1419 ret = GST_FLOW_WRONG_STATE;
1422 /* all other result values are unexpected and errors */
1423 GST_ELEMENT_ERROR (src, CORE, CLOCK,
1424 (_("Internal clock error.")),
1425 ("clock returned unexpected return value %d", status));
1426 gst_buffer_unref (*buf);
1428 ret = GST_FLOW_ERROR;
1437 GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
1442 GST_DEBUG_OBJECT (src, "getrange but not started");
1443 return GST_FLOW_WRONG_STATE;
1447 GST_DEBUG_OBJECT (src, "no create function");
1448 return GST_FLOW_ERROR;
1452 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1453 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1454 return GST_FLOW_UNEXPECTED;
1456 reached_num_buffers:
1458 GST_DEBUG_OBJECT (src, "sent all buffers");
1459 return GST_FLOW_UNEXPECTED;
1463 static GstFlowReturn
1464 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1470 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1472 res = gst_base_src_get_range (src, offset, length, buf);
1474 gst_object_unref (src);
1480 gst_base_src_default_check_get_range (GstBaseSrc * src)
1484 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1485 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1486 if (G_LIKELY (gst_base_src_start (src)))
1487 gst_base_src_stop (src);
1490 /* we can operate in getrange mode if the native format is bytes
1491 * and we are seekable, this condition is set in the random_access
1492 * flag and is set in the _start() method. */
1493 res = src->random_access;
1499 gst_base_src_check_get_range (GstBaseSrc * src)
1501 GstBaseSrcClass *bclass;
1504 bclass = GST_BASE_SRC_GET_CLASS (src);
1506 if (bclass->check_get_range == NULL)
1509 res = bclass->check_get_range (src);
1510 GST_LOG_OBJECT (src, "%s() returned %d",
1511 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1518 GST_WARNING_OBJECT (src, "no check_get_range function set");
1524 gst_base_src_pad_check_get_range (GstPad * pad)
1529 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1531 res = gst_base_src_check_get_range (src);
1533 gst_object_unref (src);
1539 gst_base_src_loop (GstPad * pad)
1542 GstBuffer *buf = NULL;
1549 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1551 src->priv->last_sent_eos = FALSE;
1553 /* push events to close/start our segment before we do the real work. */
1554 if (src->priv->close_segment) {
1555 gst_pad_push_event (pad, src->priv->close_segment);
1556 src->priv->close_segment = NULL;
1558 if (src->priv->start_segment) {
1559 gst_pad_push_event (pad, src->priv->start_segment);
1560 src->priv->start_segment = NULL;
1563 /* if we operate in bytes, we can calculate an offset */
1564 if (src->segment.format == GST_FORMAT_BYTES)
1565 position = src->segment.last_stop;
1569 ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1570 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1571 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
1572 gst_flow_get_name (ret));
1575 if (G_UNLIKELY (buf == NULL))
1578 /* figure out the new position */
1579 switch (src->segment.format) {
1580 case GST_FORMAT_BYTES:
1581 position += GST_BUFFER_SIZE (buf);
1583 case GST_FORMAT_TIME:
1585 GstClockTime start, duration;
1587 start = GST_BUFFER_TIMESTAMP (buf);
1588 duration = GST_BUFFER_DURATION (buf);
1590 if (GST_CLOCK_TIME_IS_VALID (start))
1593 position = src->segment.last_stop;
1595 if (GST_CLOCK_TIME_IS_VALID (duration))
1596 position += duration;
1599 case GST_FORMAT_DEFAULT:
1600 position = GST_BUFFER_OFFSET_END (buf);
1606 if (position != -1) {
1607 if (src->segment.stop != -1) {
1608 if (position >= src->segment.stop) {
1610 position = src->segment.stop;
1613 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1616 if (G_UNLIKELY (src->priv->discont)) {
1617 buf = gst_buffer_make_metadata_writable (buf);
1618 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1619 src->priv->discont = FALSE;
1622 ret = gst_pad_push (pad, buf);
1623 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1624 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
1625 gst_flow_get_name (ret));
1630 GST_INFO_OBJECT (src, "pausing after EOS");
1631 ret = GST_FLOW_UNEXPECTED;
1636 gst_object_unref (src);
1642 const gchar *reason = gst_flow_get_name (ret);
1644 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1645 src->data.ABI.running = FALSE;
1646 gst_pad_pause_task (pad);
1647 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1648 if (ret == GST_FLOW_UNEXPECTED) {
1649 /* perform EOS logic */
1650 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1651 gst_element_post_message (GST_ELEMENT_CAST (src),
1652 gst_message_new_segment_done (GST_OBJECT_CAST (src),
1653 src->segment.format, src->segment.last_stop));
1655 gst_pad_push_event (pad, gst_event_new_eos ());
1656 src->priv->last_sent_eos = TRUE;
1659 /* for fatal errors we post an error message, post the error
1660 * first so the app knows about the error first. */
1661 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1662 (_("Internal data flow error.")),
1663 ("streaming task paused, reason %s (%d)", reason, ret));
1664 gst_pad_push_event (pad, gst_event_new_eos ());
1665 src->priv->last_sent_eos = TRUE;
1672 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1673 (_("Internal data flow error.")), ("element returned NULL buffer"));
1674 /* we finished the segment on error */
1675 src->data.ABI.running = FALSE;
1676 gst_pad_pause_task (pad);
1677 gst_pad_push_event (pad, gst_event_new_eos ());
1678 src->priv->last_sent_eos = TRUE;
1683 /* this will always be called between start() and stop(). So you can rely on
1684 * resources allocated by start() and freed from stop(). This needs to be added
1685 * to the docs at some point. */
1687 gst_base_src_unlock (GstBaseSrc * basesrc)
1689 GstBaseSrcClass *bclass;
1690 gboolean result = TRUE;
1692 GST_DEBUG ("unlock");
1693 /* unblock whatever the subclass is doing */
1694 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1696 result = bclass->unlock (basesrc);
1698 GST_DEBUG ("unschedule clock");
1699 /* and unblock the clock as well, if any */
1700 GST_OBJECT_LOCK (basesrc);
1701 if (basesrc->clock_id) {
1702 gst_clock_id_unschedule (basesrc->clock_id);
1704 GST_OBJECT_UNLOCK (basesrc);
1706 GST_DEBUG ("unlock done");
1711 /* this will always be called between start() and stop(). So you can rely on
1712 * resources allocated by start() and freed from stop(). This needs to be added
1713 * to the docs at some point. */
1715 gst_base_src_unlock_stop (GstBaseSrc * basesrc)
1717 GstBaseSrcClass *bclass;
1718 gboolean result = TRUE;
1720 GST_DEBUG_OBJECT (basesrc, "unlock stop");
1722 /* Finish a previous unblock request, allowing subclasses to flush command
1723 * queues or whatever they need to do */
1724 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1725 if (bclass->unlock_stop)
1726 result = bclass->unlock_stop (basesrc);
1728 GST_DEBUG_OBJECT (basesrc, "unlock stop done");
1733 /* default negotiation code.
1735 * Take intersection between src and sink pads, take first
1739 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1742 GstCaps *caps = NULL;
1743 GstCaps *peercaps = NULL;
1744 gboolean result = FALSE;
1746 /* first see what is possible on our source pad */
1747 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
1748 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1749 /* nothing or anything is allowed, we're done */
1750 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1751 goto no_nego_needed;
1753 /* get the peer caps */
1754 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
1755 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1759 /* get intersection */
1760 icaps = gst_caps_intersect (thiscaps, peercaps);
1761 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
1762 gst_caps_unref (thiscaps);
1763 gst_caps_unref (peercaps);
1765 /* take first (and best, since they are sorted) possibility */
1766 caps = gst_caps_copy_nth (icaps, 0);
1767 gst_caps_unref (icaps);
1770 /* no peer, work with our own caps then */
1774 caps = gst_caps_make_writable (caps);
1775 gst_caps_truncate (caps);
1778 if (!gst_caps_is_empty (caps)) {
1779 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
1780 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1782 if (gst_caps_is_any (caps)) {
1783 /* hmm, still anything, so element can do anything and
1784 * nego is not needed */
1786 } else if (gst_caps_is_fixed (caps)) {
1787 /* yay, fixed caps, use those then */
1788 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1792 gst_caps_unref (caps);
1798 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1800 gst_caps_unref (thiscaps);
1806 gst_base_src_negotiate (GstBaseSrc * basesrc)
1808 GstBaseSrcClass *bclass;
1809 gboolean result = TRUE;
1811 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1813 if (bclass->negotiate)
1814 result = bclass->negotiate (basesrc);
1820 gst_base_src_start (GstBaseSrc * basesrc)
1822 GstBaseSrcClass *bclass;
1826 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1829 GST_DEBUG_OBJECT (basesrc, "starting source");
1831 basesrc->num_buffers_left = basesrc->num_buffers;
1833 gst_segment_init (&basesrc->segment, basesrc->segment.format);
1834 basesrc->data.ABI.running = FALSE;
1836 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1838 result = bclass->start (basesrc);
1843 goto could_not_start;
1845 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
1847 /* figure out the size */
1848 if (basesrc->segment.format == GST_FORMAT_BYTES) {
1849 if (bclass->get_size) {
1850 if (!(result = bclass->get_size (basesrc, &size)))
1856 /* only update the size when operating in bytes, subclass is supposed
1857 * to set duration in the start method for other formats */
1858 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
1863 GST_DEBUG_OBJECT (basesrc,
1864 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
1865 G_GINT64_FORMAT, basesrc->segment.format, result, size,
1866 basesrc->segment.duration);
1868 /* check if we can seek */
1869 if (bclass->is_seekable)
1870 basesrc->seekable = bclass->is_seekable (basesrc);
1872 basesrc->seekable = FALSE;
1874 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
1876 /* update for random access flag */
1877 basesrc->random_access = basesrc->seekable &&
1878 basesrc->segment.format == GST_FORMAT_BYTES;
1880 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
1882 /* run typefind if we are random_access and the typefinding is enabled. */
1883 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
1886 caps = gst_type_find_helper (basesrc->srcpad, size);
1887 gst_pad_set_caps (basesrc->srcpad, caps);
1888 gst_caps_unref (caps);
1890 /* use class or default negotiate function */
1891 if (!gst_base_src_negotiate (basesrc))
1892 goto could_not_negotiate;
1900 GST_DEBUG_OBJECT (basesrc, "could not start");
1901 /* subclass is supposed to post a message. We don't have to call _stop. */
1904 could_not_negotiate:
1906 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
1907 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
1908 ("Could not negotiate format"), ("Check your filtered caps, if any"));
1909 /* we must call stop */
1910 gst_base_src_stop (basesrc);
1916 gst_base_src_stop (GstBaseSrc * basesrc)
1918 GstBaseSrcClass *bclass;
1919 gboolean result = TRUE;
1921 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1924 GST_DEBUG_OBJECT (basesrc, "stopping source");
1926 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1928 result = bclass->stop (basesrc);
1931 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
1937 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
1941 GST_LIVE_LOCK (basesrc);
1942 basesrc->live_running = TRUE;
1943 GST_LIVE_SIGNAL (basesrc);
1944 GST_LIVE_UNLOCK (basesrc);
1946 /* step 1, unblock clock sync (if any) */
1947 result = gst_base_src_unlock (basesrc);
1949 /* step 2, make sure streaming finishes */
1950 result &= gst_pad_stop_task (pad);
1952 /* step 3, clear the unblock condition */
1953 result &= gst_base_src_unlock_stop (basesrc);
1959 gst_base_src_activate_push (GstPad * pad, gboolean active)
1961 GstBaseSrc *basesrc;
1964 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1966 /* prepare subclass first */
1968 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1970 if (G_UNLIKELY (!basesrc->can_activate_push))
1971 goto no_push_activation;
1973 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1976 basesrc->priv->last_sent_eos = FALSE;
1978 /* do initial seek, which will start the task */
1979 GST_OBJECT_LOCK (basesrc);
1980 event = basesrc->data.ABI.pending_seek;
1981 basesrc->data.ABI.pending_seek = NULL;
1982 GST_OBJECT_UNLOCK (basesrc);
1984 /* no need to unlock anything, the task is certainly
1985 * not running here. The perform seek code will start the task when
1987 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
1991 gst_event_unref (event);
1993 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1994 /* call the unlock function and stop the task */
1995 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
1996 goto deactivate_failed;
1998 /* now we can stop the source */
1999 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2007 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2012 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2017 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2018 gst_base_src_stop (basesrc);
2020 gst_event_unref (event);
2025 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
2030 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2036 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2038 GstBaseSrc *basesrc;
2040 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2042 /* prepare subclass first */
2044 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2045 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2048 /* if not random_access, we cannot operate in pull mode for now */
2049 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2052 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2053 /* call the unlock function. We have no task to stop. */
2054 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
2055 goto deactivate_failed;
2057 /* don't send EOS when going from PAUSED => READY when in pull mode */
2058 basesrc->priv->last_sent_eos = TRUE;
2060 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2068 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2073 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2074 gst_base_src_stop (basesrc);
2079 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
2084 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2089 static GstStateChangeReturn
2090 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2092 GstBaseSrc *basesrc;
2093 GstStateChangeReturn result;
2094 gboolean no_preroll = FALSE;
2096 basesrc = GST_BASE_SRC (element);
2098 switch (transition) {
2099 case GST_STATE_CHANGE_NULL_TO_READY:
2101 case GST_STATE_CHANGE_READY_TO_PAUSED:
2102 GST_LIVE_LOCK (element);
2103 if (basesrc->is_live) {
2105 basesrc->live_running = FALSE;
2107 basesrc->priv->last_sent_eos = FALSE;
2108 basesrc->priv->discont = TRUE;
2109 GST_LIVE_UNLOCK (element);
2111 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2112 GST_LIVE_LOCK (element);
2113 if (basesrc->is_live) {
2114 basesrc->live_running = TRUE;
2115 GST_LIVE_SIGNAL (element);
2117 GST_LIVE_UNLOCK (element);
2124 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2125 transition)) == GST_STATE_CHANGE_FAILURE)
2128 switch (transition) {
2129 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2130 GST_LIVE_LOCK (element);
2131 if (basesrc->is_live) {
2133 basesrc->live_running = FALSE;
2135 GST_LIVE_UNLOCK (element);
2137 case GST_STATE_CHANGE_PAUSED_TO_READY:
2141 /* FIXME, deprecate this behaviour, it is very dangerous.
2142 * the prefered way of sending EOS downstream is by sending
2143 * the EOS event to the element */
2144 if (!basesrc->priv->last_sent_eos) {
2145 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2146 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2147 basesrc->priv->last_sent_eos = TRUE;
2149 event_p = &basesrc->data.ABI.pending_seek;
2150 gst_event_replace (event_p, NULL);
2151 event_p = &basesrc->priv->close_segment;
2152 gst_event_replace (event_p, NULL);
2153 event_p = &basesrc->priv->start_segment;
2154 gst_event_replace (event_p, NULL);
2157 case GST_STATE_CHANGE_READY_TO_NULL:
2163 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2164 result = GST_STATE_CHANGE_NO_PREROLL;
2171 GST_DEBUG_OBJECT (basesrc, "parent failed state change");