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>
36 * The source can be configured to operate in any #GstFormat with the
37 * gst_base_src_set_format() method. The currently set format determines
38 * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT
39 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
41 * #GstBaseSrc always supports push mode scheduling. If the following
42 * conditions are met, it also supports pull mode scheduling:
44 * <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
46 * <listitem><para>#GstBaseSrcClass.is_seekable() returns %TRUE.</para>
50 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
51 * automatically seekable in push mode as well. The following conditions must
52 * be met to make the element seekable in push mode when the format is not
56 * #GstBaseSrcClass.is_seekable() returns %TRUE.
59 * #GstBaseSrcClass.query() can convert all supported seek formats to the
60 * internal format as set with gst_base_src_set_format().
63 * #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
68 * When the element does not meet the requirements to operate in pull mode, the
69 * offset and length in the #GstBaseSrcClass.create() method should be ignored.
70 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
71 * element can operate in pull mode but only with specific offsets and
72 * lengths, it is allowed to generate an error when the wrong values are passed
73 * to the #GstBaseSrcClass.create() function.
75 * #GstBaseSrc has support for live sources. Live sources are sources that when
76 * paused discard data, such as audio or video capture devices. A typical live
77 * source also produces data at a fixed rate and thus provides a clock to publish
79 * Use gst_base_src_set_live() to activate the live source mode.
81 * A live source does not produce data in the PAUSED state. This means that the
82 * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
83 * PLAYING. To signal the pipeline that the element will not produce data, the
84 * return value from the READY to PAUSED state will be
85 * #GST_STATE_CHANGE_NO_PREROLL.
87 * A typical live source will timestamp the buffers it creates with the
88 * current running time of the pipeline. This is one reason why a live source
89 * can only produce data in the PLAYING state, when the clock is actually
90 * distributed and running.
92 * Live sources that synchronize and block on the clock (an audio source, for
93 * example) can since 0.10.12 use gst_base_src_wait_playing() when the
94 * #GstBaseSrcClass.create() function was interrupted by a state change to
97 * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
98 * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
99 * function if the source is a live source. The #GstBaseSrcClass.get_times()
100 * function should return timestamps starting from 0, as if it were a non-live
101 * source. The base class will make sure that the timestamps are transformed
102 * into the current running_time. The base source will then wait for the
103 * calculated running_time before pushing out the buffer.
105 * For live sources, the base class will by default report a latency of 0.
106 * For pseudo live sources, the base class will by default measure the difference
107 * between the first buffer timestamp and the start time of get_times and will
108 * report this value as the latency.
109 * Subclasses should override the query function when this behaviour is not
112 * There is only support in #GstBaseSrc for exactly one source pad, which
113 * should be named "src". A source implementation (subclass of #GstBaseSrc)
114 * should install a pad template in its class_init function, like so:
117 * my_element_class_init (GstMyElementClass *klass)
119 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
120 * // srctemplate should be a #GstStaticPadTemplate with direction
121 * // #GST_PAD_SRC and name "src"
122 * gst_element_class_add_pad_template (gstelement_class,
123 * gst_static_pad_template_get (&srctemplate));
124 * // see #GstElementDetails
125 * gst_element_class_set_details (gstelement_class, &details);
130 * <title>Controlled shutdown of live sources in applications</title>
132 * Applications that record from a live source may want to stop recording
133 * in a controlled way, so that the recording is stopped, but the data
134 * already in the pipeline is processed to the end (remember that many live
135 * sources would go on recording forever otherwise). For that to happen the
136 * application needs to make the source stop recording and send an EOS
137 * event down the pipeline. The application would then wait for an
138 * EOS message posted on the pipeline's bus to know when all data has
139 * been processed and the pipeline can safely be stopped.
141 * Since GStreamer 0.10.16 an application may send an EOS event to a source
142 * element to make it perform the EOS logic (send EOS event downstream or post a
143 * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
144 * with the gst_element_send_event() function on the element or its parent bin.
146 * After the EOS has been sent to the element, the application should wait for
147 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
148 * received, it may safely shut down the entire pipeline.
150 * Last reviewed on 2007-12-19 (0.10.16)
162 #include <gst/gst_private.h>
163 #include <gst/glib-compat-private.h>
165 #include "gstbasesrc.h"
166 #include "gsttypefindhelper.h"
167 #include <gst/gst-i18n-lib.h>
169 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
170 #define GST_CAT_DEFAULT gst_base_src_debug
172 #define GST_LIVE_GET_LOCK(elem) (&GST_BASE_SRC_CAST(elem)->live_lock)
173 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
174 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
175 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
176 #define GST_LIVE_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->live_cond)
177 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
178 #define GST_LIVE_WAIT_UNTIL(elem, end_time) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
179 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
180 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
182 /* BaseSrc signals and args */
189 #define DEFAULT_BLOCKSIZE 4096
190 #define DEFAULT_NUM_BUFFERS -1
191 #define DEFAULT_TYPEFIND FALSE
192 #define DEFAULT_DO_TIMESTAMP FALSE
203 #define GST_BASE_SRC_GET_PRIVATE(obj) \
204 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
206 struct _GstBaseSrcPrivate
211 GstFlowReturn start_result;
214 /* if a stream-start event should be sent */
215 gboolean stream_start_pending;
217 /* if segment should be sent */
218 gboolean segment_pending;
220 /* if EOS is pending (atomic) */
223 /* startup latency is the time it takes between going to PLAYING and producing
224 * the first BUFFER with running_time 0. This value is included in the latency
226 GstClockTime latency;
227 /* timestamp offset, this is the offset add to the values of gst_times for
228 * pseudo live sources */
229 GstClockTimeDiff ts_offset;
231 gboolean do_timestamp;
232 volatile gint dynamic_size;
234 /* stream sequence number */
237 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
238 * pushed in the data stream */
239 GList *pending_events;
240 volatile gint have_events;
242 /* QoS *//* with LOCK */
243 gboolean qos_enabled;
245 GstClockTime earliest_time;
248 GstAllocator *allocator;
249 GstAllocationParams params;
252 static GstElementClass *parent_class = NULL;
254 static void gst_base_src_class_init (GstBaseSrcClass * klass);
255 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
256 static void gst_base_src_finalize (GObject * object);
260 gst_base_src_get_type (void)
262 static volatile gsize base_src_type = 0;
264 if (g_once_init_enter (&base_src_type)) {
266 static const GTypeInfo base_src_info = {
267 sizeof (GstBaseSrcClass),
270 (GClassInitFunc) gst_base_src_class_init,
275 (GInstanceInitFunc) gst_base_src_init,
278 _type = g_type_register_static (GST_TYPE_ELEMENT,
279 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
280 g_once_init_leave (&base_src_type, _type);
282 return base_src_type;
285 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
287 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
288 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
290 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
291 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
292 GstPadMode mode, gboolean active);
293 static void gst_base_src_set_property (GObject * object, guint prop_id,
294 const GValue * value, GParamSpec * pspec);
295 static void gst_base_src_get_property (GObject * object, guint prop_id,
296 GValue * value, GParamSpec * pspec);
297 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
299 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
300 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
302 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
305 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
307 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
308 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
309 GstSegment * segment);
310 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
311 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
312 GstEvent * event, GstSegment * segment);
313 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
314 guint64 offset, guint size, GstBuffer ** buf);
315 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
316 guint64 offset, guint size, GstBuffer ** buf);
318 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
319 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
321 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
322 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
324 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
325 GstStateChange transition);
327 static void gst_base_src_loop (GstPad * pad);
328 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
329 guint64 offset, guint length, GstBuffer ** buf);
330 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
331 guint length, GstBuffer ** buf);
332 static gboolean gst_base_src_seekable (GstBaseSrc * src);
333 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
334 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
338 gst_base_src_class_init (GstBaseSrcClass * klass)
340 GObjectClass *gobject_class;
341 GstElementClass *gstelement_class;
343 gobject_class = G_OBJECT_CLASS (klass);
344 gstelement_class = GST_ELEMENT_CLASS (klass);
346 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
348 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
350 parent_class = g_type_class_peek_parent (klass);
352 gobject_class->finalize = gst_base_src_finalize;
353 gobject_class->set_property = gst_base_src_set_property;
354 gobject_class->get_property = gst_base_src_get_property;
356 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
357 g_param_spec_uint ("blocksize", "Block size",
358 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
359 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
361 g_param_spec_int ("num-buffers", "num-buffers",
362 "Number of buffers to output before sending EOS (-1 = unlimited)",
363 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
364 G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
366 g_param_spec_boolean ("typefind", "Typefind",
367 "Run typefind before negotiating", DEFAULT_TYPEFIND,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
370 g_param_spec_boolean ("do-timestamp", "Do timestamp",
371 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 gstelement_class->change_state =
375 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
376 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
378 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
379 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
380 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
381 klass->prepare_seek_segment =
382 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
383 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
384 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
385 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
386 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
387 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
389 /* Registering debug symbols for function pointers */
390 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
391 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
392 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
393 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
394 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
398 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
401 GstPadTemplate *pad_template;
403 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
405 basesrc->is_live = FALSE;
406 g_mutex_init (&basesrc->live_lock);
407 g_cond_init (&basesrc->live_cond);
408 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
409 basesrc->num_buffers_left = -1;
411 basesrc->can_activate_push = TRUE;
414 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
415 g_return_if_fail (pad_template != NULL);
417 GST_DEBUG_OBJECT (basesrc, "creating src pad");
418 pad = gst_pad_new_from_template (pad_template, "src");
420 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
421 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
422 gst_pad_set_event_function (pad, gst_base_src_event);
423 gst_pad_set_query_function (pad, gst_base_src_query);
424 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
426 /* hold pointer to pad */
427 basesrc->srcpad = pad;
428 GST_DEBUG_OBJECT (basesrc, "adding src pad");
429 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
431 basesrc->blocksize = DEFAULT_BLOCKSIZE;
432 basesrc->clock_id = NULL;
433 /* we operate in BYTES by default */
434 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
435 basesrc->typefind = DEFAULT_TYPEFIND;
436 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
437 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
439 basesrc->priv->start_result = GST_FLOW_FLUSHING;
440 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
441 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
442 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
444 GST_DEBUG_OBJECT (basesrc, "init done");
448 gst_base_src_finalize (GObject * object)
453 basesrc = GST_BASE_SRC (object);
455 g_mutex_clear (&basesrc->live_lock);
456 g_cond_clear (&basesrc->live_cond);
458 event_p = &basesrc->pending_seek;
459 gst_event_replace (event_p, NULL);
461 if (basesrc->priv->pending_events) {
462 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
464 g_list_free (basesrc->priv->pending_events);
467 G_OBJECT_CLASS (parent_class)->finalize (object);
471 * gst_base_src_wait_playing:
474 * If the #GstBaseSrcClass.create() method performs its own synchronisation
475 * against the clock it must unblock when going from PLAYING to the PAUSED state
476 * and call this method before continuing to produce the remaining data.
478 * This function will block until a state change to PLAYING happens (in which
479 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
480 * to a state change to READY or a FLUSH event (in which case this function
481 * returns #GST_FLOW_FLUSHING).
485 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
486 * continue. Any other return value should be returned from the create vmethod.
489 gst_base_src_wait_playing (GstBaseSrc * src)
491 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
494 /* block until the state changes, or we get a flush, or something */
495 GST_DEBUG_OBJECT (src, "live source waiting for running state");
497 GST_DEBUG_OBJECT (src, "live source unlocked");
498 if (src->priv->flushing)
500 } while (G_UNLIKELY (!src->live_running));
507 GST_DEBUG_OBJECT (src, "we are flushing");
508 return GST_FLOW_FLUSHING;
513 * gst_base_src_set_live:
514 * @src: base source instance
515 * @live: new live-mode
517 * If the element listens to a live source, @live should
520 * A live source will not produce data in the PAUSED state and
521 * will therefore not be able to participate in the PREROLL phase
522 * of a pipeline. To signal this fact to the application and the
523 * pipeline, the state change return value of the live source will
524 * be GST_STATE_CHANGE_NO_PREROLL.
527 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
529 g_return_if_fail (GST_IS_BASE_SRC (src));
531 GST_OBJECT_LOCK (src);
533 GST_OBJECT_UNLOCK (src);
537 * gst_base_src_is_live:
538 * @src: base source instance
540 * Check if an element is in live mode.
542 * Returns: %TRUE if element is in live mode.
545 gst_base_src_is_live (GstBaseSrc * src)
549 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
551 GST_OBJECT_LOCK (src);
552 result = src->is_live;
553 GST_OBJECT_UNLOCK (src);
559 * gst_base_src_set_format:
560 * @src: base source instance
561 * @format: the format to use
563 * Sets the default format of the source. This will be the format used
564 * for sending NEW_SEGMENT events and for performing seeks.
566 * If a format of GST_FORMAT_BYTES is set, the element will be able to
567 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
569 * This function must only be called in states < %GST_STATE_PAUSED.
574 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
576 g_return_if_fail (GST_IS_BASE_SRC (src));
577 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
579 GST_OBJECT_LOCK (src);
580 gst_segment_init (&src->segment, format);
581 GST_OBJECT_UNLOCK (src);
585 * gst_base_src_set_dynamic_size:
586 * @src: base source instance
587 * @dynamic: new dynamic size mode
589 * If not @dynamic, size is only updated when needed, such as when trying to
590 * read past current tracked size. Otherwise, size is checked for upon each
596 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
598 g_return_if_fail (GST_IS_BASE_SRC (src));
600 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
604 * gst_base_src_set_async:
605 * @src: base source instance
606 * @async: new async mode
608 * Configure async behaviour in @src, no state change will block. The open,
609 * close, start, stop, play and pause virtual methods will be executed in a
610 * different thread and are thus allowed to perform blocking operations. Any
611 * blocking operation should be unblocked with the unlock vmethod.
614 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
616 g_return_if_fail (GST_IS_BASE_SRC (src));
618 GST_OBJECT_LOCK (src);
619 src->priv->async = async;
620 GST_OBJECT_UNLOCK (src);
624 * gst_base_src_is_async:
625 * @src: base source instance
627 * Get the current async behaviour of @src. See also gst_base_src_set_async().
629 * Returns: %TRUE if @src is operating in async mode.
632 gst_base_src_is_async (GstBaseSrc * src)
636 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
638 GST_OBJECT_LOCK (src);
639 res = src->priv->async;
640 GST_OBJECT_UNLOCK (src);
647 * gst_base_src_query_latency:
649 * @live: (out) (allow-none): if the source is live
650 * @min_latency: (out) (allow-none): the min latency of the source
651 * @max_latency: (out) (allow-none): the max latency of the source
653 * Query the source for the latency parameters. @live will be TRUE when @src is
654 * configured as a live source. @min_latency will be set to the difference
655 * between the running time and the timestamp of the first buffer.
656 * @max_latency is always the undefined value of -1.
658 * This function is mostly used by subclasses.
660 * Returns: TRUE if the query succeeded.
665 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
666 GstClockTime * min_latency, GstClockTime * max_latency)
670 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
672 GST_OBJECT_LOCK (src);
674 *live = src->is_live;
676 /* if we have a startup latency, report this one, else report 0. Subclasses
677 * are supposed to override the query function if they want something
679 if (src->priv->latency != -1)
680 min = src->priv->latency;
689 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
690 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
692 GST_OBJECT_UNLOCK (src);
698 * gst_base_src_set_blocksize:
700 * @blocksize: the new blocksize in bytes
702 * Set the number of bytes that @src will push out with each buffer. When
703 * @blocksize is set to -1, a default length will be used.
708 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
710 g_return_if_fail (GST_IS_BASE_SRC (src));
712 GST_OBJECT_LOCK (src);
713 src->blocksize = blocksize;
714 GST_OBJECT_UNLOCK (src);
718 * gst_base_src_get_blocksize:
721 * Get the number of bytes that @src will push out with each buffer.
723 * Returns: the number of bytes pushed with each buffer.
728 gst_base_src_get_blocksize (GstBaseSrc * src)
732 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
734 GST_OBJECT_LOCK (src);
735 res = src->blocksize;
736 GST_OBJECT_UNLOCK (src);
743 * gst_base_src_set_do_timestamp:
745 * @timestamp: enable or disable timestamping
747 * Configure @src to automatically timestamp outgoing buffers based on the
748 * current running_time of the pipeline. This property is mostly useful for live
754 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
756 g_return_if_fail (GST_IS_BASE_SRC (src));
758 GST_OBJECT_LOCK (src);
759 src->priv->do_timestamp = timestamp;
760 GST_OBJECT_UNLOCK (src);
764 * gst_base_src_get_do_timestamp:
767 * Query if @src timestamps outgoing buffers based on the current running_time.
769 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
774 gst_base_src_get_do_timestamp (GstBaseSrc * src)
778 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
780 GST_OBJECT_LOCK (src);
781 res = src->priv->do_timestamp;
782 GST_OBJECT_UNLOCK (src);
788 * gst_base_src_new_seamless_segment:
790 * @start: The new start value for the segment
791 * @stop: Stop value for the new segment
792 * @position: The position value for the new segent
794 * Prepare a new seamless segment for emission downstream. This function must
795 * only be called by derived sub-classes, and only from the create() function,
796 * as the stream-lock needs to be held.
798 * The format for the new segment will be the current format of the source, as
799 * configured with gst_base_src_set_format()
801 * Returns: %TRUE if preparation of the seamless segment succeeded.
806 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
811 GST_DEBUG_OBJECT (src,
812 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
813 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
814 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
816 GST_OBJECT_LOCK (src);
818 src->segment.base = gst_segment_to_running_time (&src->segment,
819 src->segment.format, src->segment.position);
820 src->segment.start = start;
821 src->segment.stop = stop;
822 src->segment.position = position;
824 /* forward, we send data from position to stop */
825 src->priv->segment_pending = TRUE;
826 GST_OBJECT_UNLOCK (src);
828 src->priv->discont = TRUE;
835 gst_base_src_send_stream_start (GstBaseSrc * src)
839 if (src->priv->stream_start_pending) {
840 ret = gst_pad_push_event (src->srcpad, gst_event_new_stream_start ());
841 src->priv->stream_start_pending = FALSE;
848 * gst_base_src_set_caps:
849 * @src: a #GstBaseSrc
852 * Set new caps on the basesrc source pad.
854 * Returns: %TRUE if the caps could be set
857 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
859 GstBaseSrcClass *bclass;
862 bclass = GST_BASE_SRC_GET_CLASS (src);
864 gst_base_src_send_stream_start (src);
865 gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
867 if (bclass->set_caps)
868 res = bclass->set_caps (src, caps);
874 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
876 GstCaps *caps = NULL;
877 GstPadTemplate *pad_template;
878 GstBaseSrcClass *bclass;
880 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
883 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
885 if (pad_template != NULL) {
886 caps = gst_pad_template_get_caps (pad_template);
889 GstCaps *intersection;
892 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
893 gst_caps_unref (caps);
901 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
903 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
904 return gst_caps_fixate (caps);
908 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
910 GstBaseSrcClass *bclass;
912 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
915 caps = bclass->fixate (bsrc, caps);
921 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
925 switch (GST_QUERY_TYPE (query)) {
926 case GST_QUERY_POSITION:
930 gst_query_parse_position (query, &format, NULL);
932 GST_DEBUG_OBJECT (src, "position query in format %s",
933 gst_format_get_name (format));
936 case GST_FORMAT_PERCENT:
942 GST_OBJECT_LOCK (src);
943 position = src->segment.position;
944 duration = src->segment.duration;
945 GST_OBJECT_UNLOCK (src);
947 if (position != -1 && duration != -1) {
948 if (position < duration)
949 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
952 percent = GST_FORMAT_PERCENT_MAX;
956 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
963 GstFormat seg_format;
965 GST_OBJECT_LOCK (src);
967 gst_segment_to_stream_time (&src->segment, src->segment.format,
968 src->segment.position);
969 seg_format = src->segment.format;
970 GST_OBJECT_UNLOCK (src);
972 if (position != -1) {
973 /* convert to requested format */
975 gst_pad_query_convert (src->srcpad, seg_format,
976 position, format, &position);
980 gst_query_set_position (query, format, position);
986 case GST_QUERY_DURATION:
990 gst_query_parse_duration (query, &format, NULL);
992 GST_DEBUG_OBJECT (src, "duration query in format %s",
993 gst_format_get_name (format));
996 case GST_FORMAT_PERCENT:
997 gst_query_set_duration (query, GST_FORMAT_PERCENT,
998 GST_FORMAT_PERCENT_MAX);
1004 GstFormat seg_format;
1007 /* may have to refresh duration */
1008 if (g_atomic_int_get (&src->priv->dynamic_size))
1009 gst_base_src_update_length (src, 0, &length);
1011 /* this is the duration as configured by the subclass. */
1012 GST_OBJECT_LOCK (src);
1013 duration = src->segment.duration;
1014 seg_format = src->segment.format;
1015 GST_OBJECT_UNLOCK (src);
1017 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1018 duration, gst_format_get_name (seg_format));
1020 if (duration != -1) {
1021 /* convert to requested format, if this fails, we have a duration
1022 * but we cannot answer the query, we must return FALSE. */
1024 gst_pad_query_convert (src->srcpad, seg_format,
1025 duration, format, &duration);
1027 /* The subclass did not configure a duration, we assume that the
1028 * media has an unknown duration then and we return TRUE to report
1029 * this. Note that this is not the same as returning FALSE, which
1030 * means that we cannot report the duration at all. */
1033 gst_query_set_duration (query, format, duration);
1040 case GST_QUERY_SEEKING:
1042 GstFormat format, seg_format;
1045 GST_OBJECT_LOCK (src);
1046 duration = src->segment.duration;
1047 seg_format = src->segment.format;
1048 GST_OBJECT_UNLOCK (src);
1050 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1051 if (format == seg_format) {
1052 gst_query_set_seeking (query, seg_format,
1053 gst_base_src_seekable (src), 0, duration);
1056 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1057 /* Don't reply to the query to make up for demuxers which don't
1058 * handle the SEEKING query yet. Players like Totem will fall back
1059 * to the duration when the SEEKING query isn't answered. */
1064 case GST_QUERY_SEGMENT:
1068 GST_OBJECT_LOCK (src);
1069 /* no end segment configured, current duration then */
1070 if ((stop = src->segment.stop) == -1)
1071 stop = src->segment.duration;
1072 start = src->segment.start;
1074 /* adjust to stream time */
1075 if (src->segment.time != -1) {
1076 start -= src->segment.time;
1078 stop -= src->segment.time;
1081 gst_query_set_segment (query, src->segment.rate, src->segment.format,
1083 GST_OBJECT_UNLOCK (src);
1088 case GST_QUERY_FORMATS:
1090 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1091 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1095 case GST_QUERY_CONVERT:
1097 GstFormat src_fmt, dest_fmt;
1098 gint64 src_val, dest_val;
1100 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1102 /* we can only convert between equal formats... */
1103 if (src_fmt == dest_fmt) {
1109 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1112 case GST_QUERY_LATENCY:
1114 GstClockTime min, max;
1117 /* Subclasses should override and implement something useful */
1118 res = gst_base_src_query_latency (src, &live, &min, &max);
1120 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1121 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1122 GST_TIME_ARGS (max));
1124 gst_query_set_latency (query, live, min, max);
1127 case GST_QUERY_JITTER:
1128 case GST_QUERY_RATE:
1131 case GST_QUERY_BUFFERING:
1133 GstFormat format, seg_format;
1134 gint64 start, stop, estimated;
1136 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1138 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1139 gst_format_get_name (format));
1141 GST_OBJECT_LOCK (src);
1142 if (src->random_access) {
1145 if (format == GST_FORMAT_PERCENT)
1146 stop = GST_FORMAT_PERCENT_MAX;
1148 stop = src->segment.duration;
1154 seg_format = src->segment.format;
1155 GST_OBJECT_UNLOCK (src);
1157 /* convert to required format. When the conversion fails, we can't answer
1158 * the query. When the value is unknown, we can don't perform conversion
1159 * but report TRUE. */
1160 if (format != GST_FORMAT_PERCENT && stop != -1) {
1161 res = gst_pad_query_convert (src->srcpad, seg_format,
1162 stop, format, &stop);
1166 if (res && format != GST_FORMAT_PERCENT && start != -1)
1167 res = gst_pad_query_convert (src->srcpad, seg_format,
1168 start, format, &start);
1170 gst_query_set_buffering_range (query, format, start, stop, estimated);
1173 case GST_QUERY_SCHEDULING:
1175 gboolean random_access;
1177 random_access = gst_base_src_is_random_access (src);
1179 /* we can operate in getrange mode if the native format is bytes
1180 * and we are seekable, this condition is set in the random_access
1181 * flag and is set in the _start() method. */
1182 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1184 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1185 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1190 case GST_QUERY_CAPS:
1192 GstBaseSrcClass *bclass;
1193 GstCaps *caps, *filter;
1195 bclass = GST_BASE_SRC_GET_CLASS (src);
1196 if (bclass->get_caps) {
1197 gst_query_parse_caps (query, &filter);
1198 if ((caps = bclass->get_caps (src, filter))) {
1199 gst_query_set_caps_result (query, caps);
1200 gst_caps_unref (caps);
1213 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1220 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1223 GstBaseSrcClass *bclass;
1224 gboolean result = FALSE;
1226 src = GST_BASE_SRC (parent);
1227 bclass = GST_BASE_SRC_GET_CLASS (src);
1230 result = bclass->query (src, query);
1236 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1238 gboolean res = TRUE;
1240 /* update our offset if the start/stop position was updated */
1241 if (segment->format == GST_FORMAT_BYTES) {
1242 segment->time = segment->start;
1243 } else if (segment->start == 0) {
1244 /* seek to start, we can implement a default for this. */
1248 GST_INFO_OBJECT (src, "Can't do a default seek");
1255 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1257 GstBaseSrcClass *bclass;
1258 gboolean result = FALSE;
1260 bclass = GST_BASE_SRC_GET_CLASS (src);
1262 if (bclass->do_seek)
1263 result = bclass->do_seek (src, segment);
1268 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1271 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1272 GstSegment * segment)
1274 /* By default, we try one of 2 things:
1275 * - For absolute seek positions, convert the requested position to our
1276 * configured processing format and place it in the output segment \
1277 * - For relative seek positions, convert our current (input) values to the
1278 * seek format, adjust by the relative seek offset and then convert back to
1279 * the processing format
1281 GstSeekType cur_type, stop_type;
1284 GstFormat seek_format, dest_format;
1287 gboolean res = TRUE;
1289 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1290 &cur_type, &cur, &stop_type, &stop);
1291 dest_format = segment->format;
1293 if (seek_format == dest_format) {
1294 gst_segment_do_seek (segment, rate, seek_format, flags,
1295 cur_type, cur, stop_type, stop, &update);
1299 if (cur_type != GST_SEEK_TYPE_NONE) {
1300 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1302 gst_pad_query_convert (src->srcpad, seek_format, cur, dest_format,
1304 cur_type = GST_SEEK_TYPE_SET;
1307 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1308 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1310 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1312 stop_type = GST_SEEK_TYPE_SET;
1315 /* And finally, configure our output segment in the desired format */
1316 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
1317 stop_type, stop, &update);
1326 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1332 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1333 GstSegment * seeksegment)
1335 GstBaseSrcClass *bclass;
1336 gboolean result = FALSE;
1338 bclass = GST_BASE_SRC_GET_CLASS (src);
1340 if (bclass->prepare_seek_segment)
1341 result = bclass->prepare_seek_segment (src, event, seeksegment);
1346 static GstFlowReturn
1347 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1348 guint size, GstBuffer ** buffer)
1351 GstBaseSrcPrivate *priv = src->priv;
1354 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1355 } else if (size != -1) {
1356 *buffer = gst_buffer_new_allocate (priv->allocator, size, &priv->params);
1357 if (G_UNLIKELY (*buffer == NULL))
1362 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1371 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1372 return GST_FLOW_ERROR;
1376 static GstFlowReturn
1377 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1378 guint size, GstBuffer ** buffer)
1380 GstBaseSrcClass *bclass;
1384 bclass = GST_BASE_SRC_GET_CLASS (src);
1386 if (G_UNLIKELY (!bclass->alloc))
1388 if (G_UNLIKELY (!bclass->fill))
1391 if (*buffer == NULL) {
1392 /* downstream did not provide us with a buffer to fill, allocate one
1394 ret = bclass->alloc (src, offset, size, &res_buf);
1395 if (G_UNLIKELY (ret != GST_FLOW_OK))
1401 if (G_LIKELY (size > 0)) {
1402 /* only call fill when there is a size */
1403 ret = bclass->fill (src, offset, size, res_buf);
1404 if (G_UNLIKELY (ret != GST_FLOW_OK))
1415 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1416 return GST_FLOW_NOT_SUPPORTED;
1420 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1425 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1426 gst_flow_get_name (ret));
1427 if (*buffer == NULL)
1428 gst_buffer_unref (res_buf);
1433 /* this code implements the seeking. It is a good example
1434 * handling all cases.
1436 * A seek updates the currently configured segment.start
1437 * and segment.stop values based on the SEEK_TYPE. If the
1438 * segment.start value is updated, a seek to this new position
1439 * should be performed.
1441 * The seek can only be executed when we are not currently
1442 * streaming any data, to make sure that this is the case, we
1443 * acquire the STREAM_LOCK which is taken when we are in the
1444 * _loop() function or when a getrange() is called. Normally
1445 * we will not receive a seek if we are operating in pull mode
1446 * though. When we operate as a live source we might block on the live
1447 * cond, which does not release the STREAM_LOCK. Therefore we will try
1448 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1449 * safe to perform the seek.
1451 * When we are in the loop() function, we might be in the middle
1452 * of pushing a buffer, which might block in a sink. To make sure
1453 * that the push gets unblocked we push out a FLUSH_START event.
1454 * Our loop function will get a WRONG_STATE return value from
1455 * the push and will pause, effectively releasing the STREAM_LOCK.
1457 * For a non-flushing seek, we pause the task, which might eventually
1458 * release the STREAM_LOCK. We say eventually because when the sink
1459 * blocks on the sample we might wait a very long time until the sink
1460 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1461 * can continue the seek. A non-flushing seek is normally done in a
1462 * running pipeline to perform seamless playback, this means that the sink is
1463 * PLAYING and will return from its chain function.
1464 * In the case of a non-flushing seek we need to make sure that the
1465 * data we output after the seek is continuous with the previous data,
1466 * this is because a non-flushing seek does not reset the running-time
1467 * to 0. We do this by closing the currently running segment, ie. sending
1468 * a new_segment event with the stop position set to the last processed
1471 * After updating the segment.start/stop values, we prepare for
1472 * streaming again. We push out a FLUSH_STOP to make the peer pad
1473 * accept data again and we start our task again.
1475 * A segment seek posts a message on the bus saying that the playback
1476 * of the segment started. We store the segment flag internally because
1477 * when we reach the segment.stop we have to post a segment.done
1478 * instead of EOS when doing a segment seek.
1480 /* FIXME (0.11), we have the unlock gboolean here because most current
1481 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1482 * the streaming thread isn't running, resulting in bogus unlocks later when it
1483 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1484 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1488 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1490 gboolean res = TRUE, tres;
1492 GstFormat seek_format, dest_format;
1494 GstSeekType cur_type, stop_type;
1496 gboolean flush, playing;
1498 gboolean relative_seek = FALSE;
1499 gboolean seekseg_configured = FALSE;
1500 GstSegment seeksegment;
1504 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1506 GST_OBJECT_LOCK (src);
1507 dest_format = src->segment.format;
1508 GST_OBJECT_UNLOCK (src);
1511 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1512 &cur_type, &cur, &stop_type, &stop);
1514 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1515 SEEK_TYPE_IS_RELATIVE (stop_type);
1517 if (dest_format != seek_format && !relative_seek) {
1518 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1519 * here before taking the stream lock, otherwise we must convert it later,
1520 * once we have the stream lock and can read the last configures segment
1521 * start and stop positions */
1522 gst_segment_init (&seeksegment, dest_format);
1524 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1525 goto prepare_failed;
1527 seekseg_configured = TRUE;
1530 flush = flags & GST_SEEK_FLAG_FLUSH;
1531 seqnum = gst_event_get_seqnum (event);
1534 /* get next seqnum */
1535 seqnum = gst_util_seqnum_next ();
1538 /* send flush start */
1540 tevent = gst_event_new_flush_start ();
1541 gst_event_set_seqnum (tevent, seqnum);
1542 gst_pad_push_event (src->srcpad, tevent);
1544 gst_pad_pause_task (src->srcpad);
1546 /* unblock streaming thread. */
1547 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1549 /* grab streaming lock, this should eventually be possible, either
1550 * because the task is paused, our streaming thread stopped
1551 * or because our peer is flushing. */
1552 GST_PAD_STREAM_LOCK (src->srcpad);
1553 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1554 /* we have seen this event before, issue a warning for now */
1555 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1558 src->priv->seqnum = seqnum;
1559 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1562 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1564 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1565 * copy the current segment info into the temp segment that we can actually
1566 * attempt the seek with. We only update the real segment if the seek succeeds. */
1567 if (!seekseg_configured) {
1568 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1570 /* now configure the final seek segment */
1572 if (seeksegment.format != seek_format) {
1573 /* OK, here's where we give the subclass a chance to convert the relative
1574 * seek into an absolute one in the processing format. We set up any
1575 * absolute seek above, before taking the stream lock. */
1576 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1577 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1582 /* The seek format matches our processing format, no need to ask the
1583 * the subclass to configure the segment. */
1584 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1585 cur_type, cur, stop_type, stop, &update);
1588 /* Else, no seek event passed, so we're just (re)starting the
1593 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1594 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1595 seeksegment.start, seeksegment.stop, seeksegment.position);
1597 /* do the seek, segment.position contains the new position. */
1598 res = gst_base_src_do_seek (src, &seeksegment);
1601 /* and prepare to continue streaming */
1603 tevent = gst_event_new_flush_stop (TRUE);
1604 gst_event_set_seqnum (tevent, seqnum);
1605 /* send flush stop, peer will accept data and events again. We
1606 * are not yet providing data as we still have the STREAM_LOCK. */
1607 gst_pad_push_event (src->srcpad, tevent);
1610 /* The subclass must have converted the segment to the processing format
1612 if (res && seeksegment.format != dest_format) {
1613 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1614 "in the correct format. Aborting seek.");
1618 /* if the seek was successful, we update our real segment and push
1619 * out the new segment. */
1621 GST_OBJECT_LOCK (src);
1622 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1623 GST_OBJECT_UNLOCK (src);
1625 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1626 GstMessage *message;
1628 message = gst_message_new_segment_start (GST_OBJECT (src),
1629 seeksegment.format, seeksegment.position);
1630 gst_message_set_seqnum (message, seqnum);
1632 gst_element_post_message (GST_ELEMENT (src), message);
1635 /* for deriving a stop position for the playback segment from the seek
1636 * segment, we must take the duration when the stop is not set */
1637 if ((stop = seeksegment.stop) == -1)
1638 stop = seeksegment.duration;
1640 src->priv->segment_pending = TRUE;
1643 src->priv->discont = TRUE;
1644 src->running = TRUE;
1645 /* and restart the task in case it got paused explicitly or by
1646 * the FLUSH_START event we pushed out. */
1647 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1652 /* and release the lock again so we can continue streaming */
1653 GST_PAD_STREAM_UNLOCK (src->srcpad);
1659 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1664 /* all events send to this element directly. This is mainly done from the
1668 gst_base_src_send_event (GstElement * element, GstEvent * event)
1671 gboolean result = FALSE;
1673 src = GST_BASE_SRC (element);
1675 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1677 switch (GST_EVENT_TYPE (event)) {
1678 /* bidirectional events */
1679 case GST_EVENT_FLUSH_START:
1680 case GST_EVENT_FLUSH_STOP:
1681 /* sending random flushes downstream can break stuff,
1682 * especially sync since all segment info will get flushed */
1685 /* downstream serialized events */
1688 GstBaseSrcClass *bclass;
1690 bclass = GST_BASE_SRC_GET_CLASS (src);
1692 /* queue EOS and make sure the task or pull function performs the EOS
1695 * We have two possibilities:
1697 * - Before we are to enter the _create function, we check the pending_eos
1698 * first and do EOS instead of entering it.
1699 * - If we are in the _create function or we did not manage to set the
1700 * flag fast enough and we are about to enter the _create function,
1701 * we unlock it so that we exit with WRONG_STATE immediately. We then
1702 * check the EOS flag and do the EOS logic.
1704 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1705 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1708 /* unlock the _create function so that we can check the pending_eos flag
1709 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1710 * that we can grab it and stop the unlock again. We don't take the stream
1711 * lock so that this operation is guaranteed to never block. */
1712 gst_base_src_activate_pool (src, FALSE);
1714 bclass->unlock (src);
1716 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1718 GST_LIVE_LOCK (src);
1719 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1720 /* now stop the unlock of the streaming thread again. Grabbing the live
1721 * lock is enough because that protects the create function. */
1722 if (bclass->unlock_stop)
1723 bclass->unlock_stop (src);
1724 gst_base_src_activate_pool (src, TRUE);
1725 GST_LIVE_UNLOCK (src);
1730 case GST_EVENT_SEGMENT:
1731 /* sending random SEGMENT downstream can break sync. */
1734 case GST_EVENT_CUSTOM_DOWNSTREAM:
1735 case GST_EVENT_CUSTOM_BOTH:
1736 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1737 GST_OBJECT_LOCK (src);
1738 src->priv->pending_events =
1739 g_list_append (src->priv->pending_events, event);
1740 g_atomic_int_set (&src->priv->have_events, TRUE);
1741 GST_OBJECT_UNLOCK (src);
1745 case GST_EVENT_BUFFERSIZE:
1746 /* does not seem to make much sense currently */
1749 /* upstream events */
1751 /* elements should override send_event and do something */
1753 case GST_EVENT_SEEK:
1757 GST_OBJECT_LOCK (src->srcpad);
1758 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1760 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1761 GST_OBJECT_UNLOCK (src->srcpad);
1764 GST_DEBUG_OBJECT (src, "performing seek");
1765 /* when we are running in push mode, we can execute the
1766 * seek right now, we need to unlock. */
1767 result = gst_base_src_perform_seek (src, event, TRUE);
1771 /* else we store the event and execute the seek when we
1773 GST_OBJECT_LOCK (src);
1774 GST_DEBUG_OBJECT (src, "queueing seek");
1775 event_p = &src->pending_seek;
1776 gst_event_replace ((GstEvent **) event_p, event);
1777 GST_OBJECT_UNLOCK (src);
1778 /* assume the seek will work */
1783 case GST_EVENT_NAVIGATION:
1784 /* could make sense for elements that do something with navigation events
1785 * but then they would need to override the send_event function */
1787 case GST_EVENT_LATENCY:
1788 /* does not seem to make sense currently */
1792 case GST_EVENT_CUSTOM_UPSTREAM:
1793 /* override send_event if you want this */
1795 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1796 case GST_EVENT_CUSTOM_BOTH_OOB:
1797 /* insert a random custom event into the pipeline */
1798 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1799 result = gst_pad_push_event (src->srcpad, event);
1800 /* we gave away the ref to the event in the push */
1807 /* if we still have a ref to the event, unref it now */
1809 gst_event_unref (event);
1816 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1817 GST_OBJECT_UNLOCK (src->srcpad);
1824 gst_base_src_seekable (GstBaseSrc * src)
1826 GstBaseSrcClass *bclass;
1827 bclass = GST_BASE_SRC_GET_CLASS (src);
1828 if (bclass->is_seekable)
1829 return bclass->is_seekable (src);
1835 gst_base_src_update_qos (GstBaseSrc * src,
1836 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1838 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1839 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1840 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1842 GST_OBJECT_LOCK (src);
1843 src->priv->proportion = proportion;
1844 src->priv->earliest_time = timestamp + diff;
1845 GST_OBJECT_UNLOCK (src);
1850 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1854 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1856 switch (GST_EVENT_TYPE (event)) {
1857 case GST_EVENT_SEEK:
1858 /* is normally called when in push mode */
1859 if (!gst_base_src_seekable (src))
1862 result = gst_base_src_perform_seek (src, event, TRUE);
1864 case GST_EVENT_FLUSH_START:
1865 /* cancel any blocking getrange, is normally called
1866 * when in pull mode. */
1867 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1869 case GST_EVENT_FLUSH_STOP:
1870 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1875 GstClockTimeDiff diff;
1876 GstClockTime timestamp;
1878 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1879 gst_base_src_update_qos (src, proportion, diff, timestamp);
1883 case GST_EVENT_RECONFIGURE:
1886 case GST_EVENT_LATENCY:
1898 GST_DEBUG_OBJECT (src, "is not seekable");
1904 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1907 GstBaseSrcClass *bclass;
1908 gboolean result = FALSE;
1910 src = GST_BASE_SRC (parent);
1911 bclass = GST_BASE_SRC_GET_CLASS (src);
1913 if (bclass->event) {
1914 if (!(result = bclass->event (src, event)))
1915 goto subclass_failed;
1919 gst_event_unref (event);
1926 GST_DEBUG_OBJECT (src, "subclass refused event");
1932 gst_base_src_set_property (GObject * object, guint prop_id,
1933 const GValue * value, GParamSpec * pspec)
1937 src = GST_BASE_SRC (object);
1940 case PROP_BLOCKSIZE:
1941 gst_base_src_set_blocksize (src, g_value_get_uint (value));
1943 case PROP_NUM_BUFFERS:
1944 src->num_buffers = g_value_get_int (value);
1947 src->typefind = g_value_get_boolean (value);
1949 case PROP_DO_TIMESTAMP:
1950 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1953 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1959 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1964 src = GST_BASE_SRC (object);
1967 case PROP_BLOCKSIZE:
1968 g_value_set_uint (value, gst_base_src_get_blocksize (src));
1970 case PROP_NUM_BUFFERS:
1971 g_value_set_int (value, src->num_buffers);
1974 g_value_set_boolean (value, src->typefind);
1976 case PROP_DO_TIMESTAMP:
1977 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1980 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1985 /* with STREAM_LOCK and LOCK */
1986 static GstClockReturn
1987 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1992 id = gst_clock_new_single_shot_id (clock, time);
1994 basesrc->clock_id = id;
1995 /* release the live lock while waiting */
1996 GST_LIVE_UNLOCK (basesrc);
1998 ret = gst_clock_id_wait (id, NULL);
2000 GST_LIVE_LOCK (basesrc);
2001 gst_clock_id_unref (id);
2002 basesrc->clock_id = NULL;
2007 /* perform synchronisation on a buffer.
2010 static GstClockReturn
2011 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2013 GstClockReturn result;
2014 GstClockTime start, end;
2015 GstBaseSrcClass *bclass;
2016 GstClockTime base_time;
2018 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
2019 gboolean do_timestamp, first, pseudo_live;
2021 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2024 if (bclass->get_times)
2025 bclass->get_times (basesrc, buffer, &start, &end);
2027 /* get buffer timestamp */
2028 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2030 /* grab the lock to prepare for clocking and calculate the startup
2032 GST_OBJECT_LOCK (basesrc);
2034 /* if we are asked to sync against the clock we are a pseudo live element */
2035 pseudo_live = (start != -1 && basesrc->is_live);
2036 /* check for the first buffer */
2037 first = (basesrc->priv->latency == -1);
2039 if (timestamp != -1 && pseudo_live) {
2040 GstClockTime latency;
2042 /* we have a timestamp and a sync time, latency is the diff */
2043 if (timestamp <= start)
2044 latency = start - timestamp;
2049 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2050 GST_TIME_ARGS (latency));
2051 /* first time we calculate latency, just configure */
2052 basesrc->priv->latency = latency;
2054 if (basesrc->priv->latency != latency) {
2055 /* we have a new latency, FIXME post latency message */
2056 basesrc->priv->latency = latency;
2057 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2058 GST_TIME_ARGS (latency));
2062 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2063 basesrc->is_live, start != -1);
2064 basesrc->priv->latency = 0;
2067 /* get clock, if no clock, we can't sync or do timestamps */
2068 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2071 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2073 do_timestamp = basesrc->priv->do_timestamp;
2075 /* first buffer, calculate the timestamp offset */
2077 GstClockTime running_time;
2079 now = gst_clock_get_time (clock);
2080 running_time = now - base_time;
2082 GST_LOG_OBJECT (basesrc,
2083 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
2084 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
2085 GST_TIME_ARGS (running_time));
2087 if (pseudo_live && timestamp != -1) {
2088 /* live source and we need to sync, add startup latency to all timestamps
2089 * to get the real running_time. Live sources should always timestamp
2090 * according to the current running time. */
2091 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2093 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2094 GST_TIME_ARGS (basesrc->priv->ts_offset));
2096 basesrc->priv->ts_offset = 0;
2097 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2100 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
2102 timestamp = running_time;
2106 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
2108 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2109 GST_TIME_ARGS (timestamp));
2112 /* add the timestamp offset we need for sync */
2113 timestamp += basesrc->priv->ts_offset;
2115 /* not the first buffer, the timestamp is the diff between the clock and
2117 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
2118 now = gst_clock_get_time (clock);
2120 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
2122 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2123 GST_TIME_ARGS (now - base_time));
2127 /* if we don't have a buffer timestamp, we don't sync */
2128 if (!GST_CLOCK_TIME_IS_VALID (start))
2131 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
2132 /* for pseudo live sources, add our ts_offset to the timestamp */
2133 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
2134 start += basesrc->priv->ts_offset;
2137 GST_LOG_OBJECT (basesrc,
2138 "waiting for clock, base time %" GST_TIME_FORMAT
2139 ", stream_start %" GST_TIME_FORMAT,
2140 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2141 GST_OBJECT_UNLOCK (basesrc);
2143 result = gst_base_src_wait (basesrc, clock, start + base_time);
2145 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2152 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2153 GST_OBJECT_UNLOCK (basesrc);
2154 return GST_CLOCK_OK;
2158 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2159 GST_OBJECT_UNLOCK (basesrc);
2160 return GST_CLOCK_OK;
2164 /* Called with STREAM_LOCK and LIVE_LOCK */
2166 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
2168 guint64 size, maxsize;
2169 GstBaseSrcClass *bclass;
2174 bclass = GST_BASE_SRC_GET_CLASS (src);
2176 format = src->segment.format;
2177 stop = src->segment.stop;
2178 /* get total file size */
2179 size = src->segment.duration;
2181 /* only operate if we are working with bytes */
2182 if (format != GST_FORMAT_BYTES)
2185 /* the max amount of bytes to read is the total size or
2186 * up to the segment.stop if present. */
2188 maxsize = MIN (size, stop);
2192 GST_DEBUG_OBJECT (src,
2193 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2194 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2195 *length, size, stop, maxsize);
2197 dynamic = g_atomic_int_get (&src->priv->dynamic_size);
2198 GST_DEBUG_OBJECT (src, "dynamic size: %d", dynamic);
2200 /* check size if we have one */
2201 if (maxsize != -1) {
2202 /* if we run past the end, check if the file became bigger and
2204 if (G_UNLIKELY (offset + *length >= maxsize || dynamic)) {
2205 /* see if length of the file changed */
2206 if (bclass->get_size)
2207 if (!bclass->get_size (src, &size))
2210 /* make sure we don't exceed the configured segment stop
2213 maxsize = MIN (size, stop);
2217 /* if we are at or past the end, EOS */
2218 if (G_UNLIKELY (offset >= maxsize))
2219 goto unexpected_length;
2221 /* else we can clip to the end */
2222 if (G_UNLIKELY (offset + *length >= maxsize))
2223 *length = maxsize - offset;
2228 /* keep track of current duration.
2229 * segment is in bytes, we checked that above. */
2230 GST_OBJECT_LOCK (src);
2231 src->segment.duration = size;
2232 GST_OBJECT_UNLOCK (src);
2243 /* must be called with LIVE_LOCK */
2244 static GstFlowReturn
2245 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2249 GstBaseSrcClass *bclass;
2250 GstClockReturn status;
2253 bclass = GST_BASE_SRC_GET_CLASS (src);
2257 if (G_UNLIKELY (!src->live_running)) {
2258 ret = gst_base_src_wait_playing (src);
2259 if (ret != GST_FLOW_OK)
2264 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2265 && !GST_BASE_SRC_IS_STARTING (src)))
2268 if (G_UNLIKELY (!bclass->create))
2271 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2272 goto unexpected_length;
2274 /* track position */
2275 GST_OBJECT_LOCK (src);
2276 if (src->segment.format == GST_FORMAT_BYTES)
2277 src->segment.position = offset;
2278 GST_OBJECT_UNLOCK (src);
2280 /* normally we don't count buffers */
2281 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2282 if (src->num_buffers_left == 0)
2283 goto reached_num_buffers;
2285 src->num_buffers_left--;
2288 /* don't enter the create function if a pending EOS event was set. For the
2289 * logic of the pending_eos, check the event function of this class. */
2290 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2293 GST_DEBUG_OBJECT (src,
2294 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2295 G_GINT64_FORMAT, offset, length, src->segment.time);
2299 ret = bclass->create (src, offset, length, &res_buf);
2301 /* The create function could be unlocked because we have a pending EOS. It's
2302 * possible that we have a valid buffer from create that we need to
2303 * discard when the create function returned _OK. */
2304 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2305 if (ret == GST_FLOW_OK) {
2307 gst_buffer_unref (res_buf);
2312 if (G_UNLIKELY (ret != GST_FLOW_OK))
2315 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2316 if (offset == 0 && src->segment.time == 0
2317 && GST_BUFFER_TIMESTAMP (res_buf) == -1 && !src->is_live) {
2318 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2319 res_buf = gst_buffer_make_writable (res_buf);
2320 GST_BUFFER_TIMESTAMP (res_buf) = 0;
2323 /* now sync before pushing the buffer */
2324 status = gst_base_src_do_sync (src, res_buf);
2326 /* waiting for the clock could have made us flushing */
2327 if (G_UNLIKELY (src->priv->flushing))
2331 case GST_CLOCK_EARLY:
2332 /* the buffer is too late. We currently don't drop the buffer. */
2333 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2336 /* buffer synchronised properly */
2337 GST_DEBUG_OBJECT (src, "buffer ok");
2339 case GST_CLOCK_UNSCHEDULED:
2340 /* this case is triggered when we were waiting for the clock and
2341 * it got unlocked because we did a state change. In any case, get rid of
2344 gst_buffer_unref (res_buf);
2346 if (!src->live_running) {
2347 /* We return WRONG_STATE when we are not running to stop the dataflow also
2348 * get rid of the produced buffer. */
2349 GST_DEBUG_OBJECT (src,
2350 "clock was unscheduled (%d), returning WRONG_STATE", status);
2351 ret = GST_FLOW_FLUSHING;
2353 /* If we are running when this happens, we quickly switched between
2354 * pause and playing. We try to produce a new buffer */
2355 GST_DEBUG_OBJECT (src,
2356 "clock was unscheduled (%d), but we are running", status);
2361 /* all other result values are unexpected and errors */
2362 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2363 (_("Internal clock error.")),
2364 ("clock returned unexpected return value %d", status));
2366 gst_buffer_unref (res_buf);
2367 ret = GST_FLOW_ERROR;
2370 if (G_LIKELY (ret == GST_FLOW_OK))
2378 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2379 gst_flow_get_name (ret));
2384 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2385 gst_flow_get_name (ret));
2390 GST_DEBUG_OBJECT (src, "getrange but not started");
2391 return GST_FLOW_FLUSHING;
2395 GST_DEBUG_OBJECT (src, "no create function");
2396 return GST_FLOW_NOT_SUPPORTED;
2400 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2401 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2402 return GST_FLOW_EOS;
2404 reached_num_buffers:
2406 GST_DEBUG_OBJECT (src, "sent all buffers");
2407 return GST_FLOW_EOS;
2411 GST_DEBUG_OBJECT (src, "we are flushing");
2413 gst_buffer_unref (res_buf);
2414 return GST_FLOW_FLUSHING;
2418 GST_DEBUG_OBJECT (src, "we are EOS");
2419 return GST_FLOW_EOS;
2423 static GstFlowReturn
2424 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2425 guint length, GstBuffer ** buf)
2430 src = GST_BASE_SRC_CAST (parent);
2432 GST_LIVE_LOCK (src);
2433 if (G_UNLIKELY (src->priv->flushing))
2436 res = gst_base_src_get_range (src, offset, length, buf);
2439 GST_LIVE_UNLOCK (src);
2446 GST_DEBUG_OBJECT (src, "we are flushing");
2447 res = GST_FLOW_FLUSHING;
2453 gst_base_src_is_random_access (GstBaseSrc * src)
2455 /* we need to start the basesrc to check random access */
2456 if (!GST_BASE_SRC_IS_STARTED (src)) {
2457 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2458 if (G_LIKELY (gst_base_src_start (src))) {
2459 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2461 gst_base_src_stop (src);
2465 return src->random_access;
2470 GST_DEBUG_OBJECT (src, "failed to start");
2476 gst_base_src_loop (GstPad * pad)
2479 GstBuffer *buf = NULL;
2484 GList *pending_events = NULL, *tmp;
2488 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2490 gst_base_src_send_stream_start (src);
2492 /* check if we need to renegotiate */
2493 if (gst_pad_check_reconfigure (pad)) {
2494 if (!gst_base_src_negotiate (src))
2495 goto not_negotiated;
2498 GST_LIVE_LOCK (src);
2500 if (G_UNLIKELY (src->priv->flushing))
2503 blocksize = src->blocksize;
2505 /* if we operate in bytes, we can calculate an offset */
2506 if (src->segment.format == GST_FORMAT_BYTES) {
2507 position = src->segment.position;
2508 /* for negative rates, start with subtracting the blocksize */
2509 if (src->segment.rate < 0.0) {
2510 /* we cannot go below segment.start */
2511 if (position > src->segment.start + blocksize)
2512 position -= blocksize;
2514 /* last block, remainder up to segment.start */
2515 blocksize = position - src->segment.start;
2516 position = src->segment.start;
2522 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2523 GST_TIME_ARGS (position), blocksize);
2525 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2526 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2527 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2528 gst_flow_get_name (ret));
2529 GST_LIVE_UNLOCK (src);
2532 /* this should not happen */
2533 if (G_UNLIKELY (buf == NULL))
2536 /* push events to close/start our segment before we push the buffer. */
2537 if (G_UNLIKELY (src->priv->segment_pending)) {
2538 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2539 src->priv->segment_pending = FALSE;
2542 if (g_atomic_int_get (&src->priv->have_events)) {
2543 GST_OBJECT_LOCK (src);
2544 /* take the events */
2545 pending_events = src->priv->pending_events;
2546 src->priv->pending_events = NULL;
2547 g_atomic_int_set (&src->priv->have_events, FALSE);
2548 GST_OBJECT_UNLOCK (src);
2551 /* Push out pending events if any */
2552 if (G_UNLIKELY (pending_events != NULL)) {
2553 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2554 GstEvent *ev = (GstEvent *) tmp->data;
2555 gst_pad_push_event (pad, ev);
2557 g_list_free (pending_events);
2560 /* figure out the new position */
2561 switch (src->segment.format) {
2562 case GST_FORMAT_BYTES:
2564 guint bufsize = gst_buffer_get_size (buf);
2566 /* we subtracted above for negative rates */
2567 if (src->segment.rate >= 0.0)
2568 position += bufsize;
2571 case GST_FORMAT_TIME:
2573 GstClockTime start, duration;
2575 start = GST_BUFFER_TIMESTAMP (buf);
2576 duration = GST_BUFFER_DURATION (buf);
2578 if (GST_CLOCK_TIME_IS_VALID (start))
2581 position = src->segment.position;
2583 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2584 if (src->segment.rate >= 0.0)
2585 position += duration;
2586 else if (position > duration)
2587 position -= duration;
2593 case GST_FORMAT_DEFAULT:
2594 if (src->segment.rate >= 0.0)
2595 position = GST_BUFFER_OFFSET_END (buf);
2597 position = GST_BUFFER_OFFSET (buf);
2603 if (position != -1) {
2604 if (src->segment.rate >= 0.0) {
2605 /* positive rate, check if we reached the stop */
2606 if (src->segment.stop != -1) {
2607 if (position >= src->segment.stop) {
2609 position = src->segment.stop;
2613 /* negative rate, check if we reached the start. start is always set to
2614 * something different from -1 */
2615 if (position <= src->segment.start) {
2617 position = src->segment.start;
2619 /* when going reverse, all buffers are DISCONT */
2620 src->priv->discont = TRUE;
2622 GST_OBJECT_LOCK (src);
2623 src->segment.position = position;
2624 GST_OBJECT_UNLOCK (src);
2627 if (G_UNLIKELY (src->priv->discont)) {
2628 GST_INFO_OBJECT (src, "marking pending DISCONT");
2629 buf = gst_buffer_make_writable (buf);
2630 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2631 src->priv->discont = FALSE;
2633 GST_LIVE_UNLOCK (src);
2635 ret = gst_pad_push (pad, buf);
2636 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2637 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2638 gst_flow_get_name (ret));
2642 if (G_UNLIKELY (eos)) {
2643 GST_INFO_OBJECT (src, "pausing after end of segment");
2654 GST_DEBUG_OBJECT (src, "Failed to renegotiate");
2655 ret = GST_FLOW_NOT_NEGOTIATED;
2660 GST_DEBUG_OBJECT (src, "we are flushing");
2661 GST_LIVE_UNLOCK (src);
2662 ret = GST_FLOW_FLUSHING;
2667 const gchar *reason = gst_flow_get_name (ret);
2670 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2671 src->running = FALSE;
2672 gst_pad_pause_task (pad);
2673 if (ret == GST_FLOW_EOS) {
2674 gboolean flag_segment;
2678 /* perform EOS logic */
2679 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2680 format = src->segment.format;
2681 position = src->segment.position;
2684 GstMessage *message;
2686 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2688 gst_message_set_seqnum (message, src->priv->seqnum);
2689 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2691 event = gst_event_new_eos ();
2692 gst_event_set_seqnum (event, src->priv->seqnum);
2693 gst_pad_push_event (pad, event);
2695 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2696 event = gst_event_new_eos ();
2697 gst_event_set_seqnum (event, src->priv->seqnum);
2698 /* for fatal errors we post an error message, post the error
2699 * first so the app knows about the error first.
2700 * Also don't do this for WRONG_STATE because it happens
2701 * due to flushing and posting an error message because of
2702 * that is the wrong thing to do, e.g. when we're doing
2703 * a flushing seek. */
2704 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2705 (_("Internal data flow error.")),
2706 ("streaming task paused, reason %s (%d)", reason, ret));
2707 gst_pad_push_event (pad, event);
2713 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2714 (_("Internal data flow error.")), ("element returned NULL buffer"));
2715 GST_LIVE_UNLOCK (src);
2721 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2722 GstAllocator * allocator, GstAllocationParams * params)
2724 GstAllocator *oldalloc;
2725 GstBufferPool *oldpool;
2726 GstBaseSrcPrivate *priv = basesrc->priv;
2729 GST_DEBUG_OBJECT (basesrc, "activate pool");
2730 if (!gst_buffer_pool_set_active (pool, TRUE))
2731 goto activate_failed;
2734 GST_OBJECT_LOCK (basesrc);
2735 oldpool = priv->pool;
2738 oldalloc = priv->allocator;
2739 priv->allocator = allocator;
2742 priv->params = *params;
2744 gst_allocation_params_init (&priv->params);
2745 GST_OBJECT_UNLOCK (basesrc);
2748 /* only deactivate if the pool is not the one we're using */
2749 if (oldpool != pool) {
2750 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2751 gst_buffer_pool_set_active (oldpool, FALSE);
2753 gst_object_unref (oldpool);
2756 gst_allocator_unref (oldalloc);
2763 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2769 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2771 GstBaseSrcPrivate *priv = basesrc->priv;
2772 GstBufferPool *pool;
2773 gboolean res = TRUE;
2775 GST_OBJECT_LOCK (basesrc);
2776 if ((pool = priv->pool))
2777 pool = gst_object_ref (pool);
2778 GST_OBJECT_UNLOCK (basesrc);
2781 res = gst_buffer_pool_set_active (pool, active);
2782 gst_object_unref (pool);
2788 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
2790 GstBaseSrcClass *bclass;
2791 gboolean result = TRUE;
2793 GstBufferPool *pool = NULL;
2794 GstAllocator *allocator = NULL;
2795 guint size, min, max;
2796 GstAllocationParams params;
2798 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2800 /* make query and let peer pad answer, we don't really care if it worked or
2801 * not, if it failed, the allocation query would contain defaults and the
2802 * subclass would then set better values if needed */
2803 query = gst_query_new_allocation (caps, TRUE);
2804 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
2805 /* not a problem, just debug a little */
2806 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
2809 if (G_LIKELY (bclass->decide_allocation))
2810 result = bclass->decide_allocation (basesrc, query);
2812 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
2815 if (gst_query_get_n_allocation_params (query) > 0) {
2816 /* try the allocator */
2817 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2820 gst_allocation_params_init (¶ms);
2823 if (gst_query_get_n_allocation_pools (query) > 0) {
2824 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2827 /* no pool, just parameters, we can make our own */
2828 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
2829 pool = gst_buffer_pool_new ();
2833 size = min = max = 0;
2838 GstStructure *config;
2840 config = gst_buffer_pool_get_config (pool);
2841 gst_buffer_pool_config_set_params (config, caps, size, min, max);
2842 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2843 gst_buffer_pool_set_config (pool, config);
2846 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
2848 gst_query_unref (query);
2854 /* default negotiation code.
2856 * Take intersection between src and sink pads, take first
2860 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2863 GstCaps *caps = NULL;
2864 GstCaps *peercaps = NULL;
2865 gboolean result = FALSE;
2867 /* first see what is possible on our source pad */
2868 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
2869 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2870 /* nothing or anything is allowed, we're done */
2871 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2872 goto no_nego_needed;
2874 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2877 /* get the peer caps */
2878 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
2879 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2881 /* The result is already a subset of our caps */
2883 gst_caps_unref (thiscaps);
2885 /* no peer, work with our own caps then */
2888 if (caps && !gst_caps_is_empty (caps)) {
2890 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
2891 if (gst_caps_is_any (caps)) {
2892 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
2893 /* hmm, still anything, so element can do anything and
2894 * nego is not needed */
2897 caps = gst_base_src_fixate (basesrc, caps);
2898 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2899 if (gst_caps_is_fixed (caps)) {
2900 /* yay, fixed caps, use those then, it's possible that the subclass does
2901 * not accept this caps after all and we have to fail. */
2902 result = gst_base_src_set_caps (basesrc, caps);
2905 gst_caps_unref (caps);
2908 gst_caps_unref (caps);
2909 GST_DEBUG_OBJECT (basesrc, "no common caps");
2915 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2917 gst_caps_unref (thiscaps);
2922 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2923 ("No supported formats found"),
2924 ("This element did not produce valid caps"));
2926 gst_caps_unref (thiscaps);
2932 gst_base_src_negotiate (GstBaseSrc * basesrc)
2934 GstBaseSrcClass *bclass;
2937 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2939 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
2941 if (G_LIKELY (bclass->negotiate))
2942 result = bclass->negotiate (basesrc);
2946 if (G_LIKELY (result)) {
2949 caps = gst_pad_get_current_caps (basesrc->srcpad);
2951 result = gst_base_src_prepare_allocation (basesrc, caps);
2954 gst_caps_unref (caps);
2960 gst_base_src_start (GstBaseSrc * basesrc)
2962 GstBaseSrcClass *bclass;
2965 GST_LIVE_LOCK (basesrc);
2966 if (GST_BASE_SRC_IS_STARTING (basesrc))
2968 if (GST_BASE_SRC_IS_STARTED (basesrc))
2971 basesrc->priv->start_result = GST_FLOW_FLUSHING;
2972 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
2973 basesrc->num_buffers_left = basesrc->num_buffers;
2974 basesrc->running = FALSE;
2975 basesrc->priv->segment_pending = FALSE;
2976 GST_OBJECT_LOCK (basesrc);
2977 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2978 GST_OBJECT_UNLOCK (basesrc);
2979 GST_LIVE_UNLOCK (basesrc);
2981 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2983 result = bclass->start (basesrc);
2988 goto could_not_start;
2990 if (!gst_base_src_is_async (basesrc))
2991 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
2998 GST_DEBUG_OBJECT (basesrc, "was starting");
2999 GST_LIVE_UNLOCK (basesrc);
3004 GST_DEBUG_OBJECT (basesrc, "was started");
3005 GST_LIVE_UNLOCK (basesrc);
3010 GST_DEBUG_OBJECT (basesrc, "could not start");
3011 /* subclass is supposed to post a message. We don't have to call _stop. */
3012 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3018 * gst_base_src_start_complete:
3019 * @src: base source instance
3020 * @ret: a #GstFlowReturn
3022 * Complete an asynchronous start operation. When the subclass overrides the
3023 * start method, it should call gst_base_src_start_complete() when the start
3024 * operation completes either from the same thread or from an asynchronous
3028 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3037 if (ret != GST_FLOW_OK)
3040 GST_DEBUG_OBJECT (basesrc, "starting source");
3041 format = basesrc->segment.format;
3043 /* figure out the size */
3046 if (format == GST_FORMAT_BYTES) {
3047 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3049 if (bclass->get_size) {
3050 if (!(have_size = bclass->get_size (basesrc, &size)))
3053 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3054 /* only update the size when operating in bytes, subclass is supposed
3055 * to set duration in the start method for other formats */
3056 GST_OBJECT_LOCK (basesrc);
3057 basesrc->segment.duration = size;
3058 GST_OBJECT_UNLOCK (basesrc);
3061 GST_DEBUG_OBJECT (basesrc,
3062 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3063 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3064 basesrc->segment.duration);
3066 seekable = gst_base_src_seekable (basesrc);
3067 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3069 /* update for random access flag */
3070 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3072 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3074 /* stop flushing now but for live sources, still block in the LIVE lock when
3075 * we are not yet PLAYING */
3076 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
3078 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3080 GST_OBJECT_LOCK (basesrc->srcpad);
3081 mode = GST_PAD_MODE (basesrc->srcpad);
3082 GST_OBJECT_UNLOCK (basesrc->srcpad);
3084 if (mode == GST_PAD_MODE_PUSH) {
3085 /* do initial seek, which will start the task */
3086 GST_OBJECT_LOCK (basesrc);
3087 event = basesrc->pending_seek;
3088 basesrc->pending_seek = NULL;
3089 GST_OBJECT_UNLOCK (basesrc);
3091 /* no need to unlock anything, the task is certainly
3092 * not running here. The perform seek code will start the task when
3094 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3098 gst_event_unref (event);
3100 /* if not random_access, we cannot operate in pull mode for now */
3101 if (G_UNLIKELY (!basesrc->random_access))
3105 GST_LIVE_LOCK (basesrc);
3106 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3107 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3108 basesrc->priv->start_result = ret;
3109 GST_LIVE_SIGNAL (basesrc);
3110 GST_LIVE_UNLOCK (basesrc);
3116 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3117 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3119 gst_event_unref (event);
3120 ret = GST_FLOW_ERROR;
3125 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3126 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3127 ret = GST_FLOW_ERROR;
3132 GST_LIVE_LOCK (basesrc);
3133 basesrc->priv->start_result = ret;
3134 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3135 GST_LIVE_SIGNAL (basesrc);
3136 GST_LIVE_UNLOCK (basesrc);
3142 * gst_base_src_start_wait:
3143 * @src: base source instance
3144 * @ret: a #GstFlowReturn
3146 * Wait until the start operation completes.
3148 * Returns: a #GstFlowReturn.
3151 gst_base_src_start_wait (GstBaseSrc * basesrc)
3153 GstFlowReturn result;
3155 GST_LIVE_LOCK (basesrc);
3156 if (G_UNLIKELY (basesrc->priv->flushing))
3159 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3160 GST_LIVE_WAIT (basesrc);
3161 if (G_UNLIKELY (basesrc->priv->flushing))
3164 result = basesrc->priv->start_result;
3165 GST_LIVE_UNLOCK (basesrc);
3172 GST_DEBUG_OBJECT (basesrc, "we are flushing");
3173 GST_LIVE_UNLOCK (basesrc);
3174 return GST_FLOW_FLUSHING;
3179 gst_base_src_stop (GstBaseSrc * basesrc)
3181 GstBaseSrcClass *bclass;
3182 gboolean result = TRUE;
3184 GST_DEBUG_OBJECT (basesrc, "stopping source");
3187 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3189 gst_pad_stop_task (basesrc->srcpad);
3191 GST_LIVE_LOCK (basesrc);
3192 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3195 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3196 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3197 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3198 GST_LIVE_SIGNAL (basesrc);
3199 GST_LIVE_UNLOCK (basesrc);
3201 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3203 result = bclass->stop (basesrc);
3205 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3211 GST_DEBUG_OBJECT (basesrc, "was started");
3212 GST_LIVE_UNLOCK (basesrc);
3217 /* start or stop flushing dataprocessing
3220 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3221 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
3223 GstBaseSrcClass *bclass;
3225 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3227 if (flushing && unlock) {
3228 gst_base_src_activate_pool (basesrc, FALSE);
3229 /* unlock any subclasses, we need to do this before grabbing the
3230 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3231 * unlock to the params because of backwards compat (see seek handler)*/
3233 bclass->unlock (basesrc);
3236 /* the live lock is released when we are blocked, waiting for playing or
3237 * when we sync to the clock. */
3238 GST_LIVE_LOCK (basesrc);
3240 *playing = basesrc->live_running;
3241 basesrc->priv->flushing = flushing;
3243 /* if we are locked in the live lock, signal it to make it flush */
3244 basesrc->live_running = TRUE;
3246 /* clear pending EOS if any */
3247 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3249 /* step 1, now that we have the LIVE lock, clear our unlock request */
3250 if (bclass->unlock_stop)
3251 bclass->unlock_stop (basesrc);
3253 /* step 2, unblock clock sync (if any) or any other blocking thing */
3254 if (basesrc->clock_id)
3255 gst_clock_id_unschedule (basesrc->clock_id);
3257 /* signal the live source that it can start playing */
3258 basesrc->live_running = live_play;
3260 gst_base_src_activate_pool (basesrc, TRUE);
3262 /* When unlocking drop all delayed events */
3264 GST_OBJECT_LOCK (basesrc);
3265 if (basesrc->priv->pending_events) {
3266 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3268 g_list_free (basesrc->priv->pending_events);
3269 basesrc->priv->pending_events = NULL;
3270 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3272 GST_OBJECT_UNLOCK (basesrc);
3275 GST_LIVE_SIGNAL (basesrc);
3276 GST_LIVE_UNLOCK (basesrc);
3281 /* the purpose of this function is to make sure that a live source blocks in the
3282 * LIVE lock or leaves the LIVE lock and continues playing. */
3284 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3286 GstBaseSrcClass *bclass;
3288 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3290 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3292 GST_DEBUG_OBJECT (basesrc, "unlock");
3294 bclass->unlock (basesrc);
3297 /* we are now able to grab the LIVE lock, when we get it, we can be
3298 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3300 GST_LIVE_LOCK (basesrc);
3301 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3303 /* unblock clock sync (if any) */
3304 if (basesrc->clock_id)
3305 gst_clock_id_unschedule (basesrc->clock_id);
3307 /* configure what to do when we get to the LIVE lock. */
3308 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3309 basesrc->live_running = live_play;
3314 /* clear our unlock request when going to PLAYING */
3315 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3316 if (bclass->unlock_stop)
3317 bclass->unlock_stop (basesrc);
3319 /* for live sources we restart the timestamp correction */
3320 basesrc->priv->latency = -1;
3321 /* have to restart the task in case it stopped because of the unlock when
3322 * we went to PAUSED. Only do this if we operating in push mode. */
3323 GST_OBJECT_LOCK (basesrc->srcpad);
3324 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3325 GST_OBJECT_UNLOCK (basesrc->srcpad);
3327 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3329 GST_DEBUG_OBJECT (basesrc, "signal");
3330 GST_LIVE_SIGNAL (basesrc);
3332 GST_LIVE_UNLOCK (basesrc);
3338 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3340 GstBaseSrc *basesrc;
3342 basesrc = GST_BASE_SRC (parent);
3344 /* prepare subclass first */
3346 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3348 if (G_UNLIKELY (!basesrc->can_activate_push))
3349 goto no_push_activation;
3351 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3354 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3355 /* now we can stop the source */
3356 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3364 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3369 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3374 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3380 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3382 GstBaseSrc *basesrc;
3384 basesrc = GST_BASE_SRC (parent);
3386 /* prepare subclass first */
3388 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3389 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3392 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3393 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3401 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3406 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3412 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3413 GstPadMode mode, gboolean active)
3418 case GST_PAD_MODE_PULL:
3419 res = gst_base_src_activate_pull (pad, parent, active);
3421 case GST_PAD_MODE_PUSH:
3422 res = gst_base_src_activate_push (pad, parent, active);
3425 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3433 static GstStateChangeReturn
3434 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3436 GstBaseSrc *basesrc;
3437 GstStateChangeReturn result;
3438 gboolean no_preroll = FALSE;
3440 basesrc = GST_BASE_SRC (element);
3442 switch (transition) {
3443 case GST_STATE_CHANGE_NULL_TO_READY:
3445 case GST_STATE_CHANGE_READY_TO_PAUSED:
3446 basesrc->priv->stream_start_pending = TRUE;
3447 no_preroll = gst_base_src_is_live (basesrc);
3449 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3450 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3451 if (gst_base_src_is_live (basesrc)) {
3452 /* now we can start playback */
3453 gst_base_src_set_playing (basesrc, TRUE);
3461 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3462 transition)) == GST_STATE_CHANGE_FAILURE)
3465 switch (transition) {
3466 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3467 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3468 if (gst_base_src_is_live (basesrc)) {
3469 /* make sure we block in the live lock in PAUSED */
3470 gst_base_src_set_playing (basesrc, FALSE);
3474 case GST_STATE_CHANGE_PAUSED_TO_READY:
3476 /* we don't need to unblock anything here, the pad deactivation code
3477 * already did this */
3478 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3479 gst_event_replace (&basesrc->pending_seek, NULL);
3480 basesrc->priv->stream_start_pending = FALSE;
3483 case GST_STATE_CHANGE_READY_TO_NULL:
3489 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3490 result = GST_STATE_CHANGE_NO_PREROLL;
3497 GST_DEBUG_OBJECT (basesrc, "parent failed state change");