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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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_SEGMENT
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 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));
125 * gst_element_class_set_static_metadata (gstelement_class,
128 * "My Source element",
129 * "The author <my.sink@my.email>");
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.
145 * An application may send an EOS event to a source element to make it
146 * perform the EOS logic (send EOS event downstream or post a
147 * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
148 * with the gst_element_send_event() function on the element or its parent bin.
150 * After the EOS has been sent to the element, the application should wait for
151 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
152 * received, it may safely shut down the entire pipeline.
154 * Last reviewed on 2007-12-19 (0.10.16)
166 #include <gst/gst_private.h>
167 #include <gst/glib-compat-private.h>
169 #include "gstbasesrc.h"
170 #include "gsttypefindhelper.h"
171 #include <gst/gst-i18n-lib.h>
173 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
174 #define GST_CAT_DEFAULT gst_base_src_debug
176 #define GST_LIVE_GET_LOCK(elem) (&GST_BASE_SRC_CAST(elem)->live_lock)
177 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
178 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
179 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
180 #define GST_LIVE_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->live_cond)
181 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
182 #define GST_LIVE_WAIT_UNTIL(elem, end_time) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
183 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
184 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
187 #define GST_ASYNC_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->priv->async_cond)
188 #define GST_ASYNC_WAIT(elem) g_cond_wait (GST_ASYNC_GET_COND (elem), GST_OBJECT_GET_LOCK (elem))
189 #define GST_ASYNC_SIGNAL(elem) g_cond_signal (GST_ASYNC_GET_COND (elem));
192 /* BaseSrc signals and args */
199 #define DEFAULT_BLOCKSIZE 4096
200 #define DEFAULT_NUM_BUFFERS -1
201 #define DEFAULT_TYPEFIND FALSE
202 #define DEFAULT_DO_TIMESTAMP FALSE
213 #define GST_BASE_SRC_GET_PRIVATE(obj) \
214 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
216 struct _GstBaseSrcPrivate
221 GstFlowReturn start_result;
224 /* if a stream-start event should be sent */
225 gboolean stream_start_pending;
227 /* if segment should be sent */
228 gboolean segment_pending;
230 /* if EOS is pending (atomic) */
233 /* startup latency is the time it takes between going to PLAYING and producing
234 * the first BUFFER with running_time 0. This value is included in the latency
236 GstClockTime latency;
237 /* timestamp offset, this is the offset add to the values of gst_times for
238 * pseudo live sources */
239 GstClockTimeDiff ts_offset;
241 gboolean do_timestamp;
242 volatile gint dynamic_size;
244 /* stream sequence number */
247 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
248 * pushed in the data stream */
249 GList *pending_events;
250 volatile gint have_events;
252 /* QoS *//* with LOCK */
253 gboolean qos_enabled;
255 GstClockTime earliest_time;
258 GstAllocator *allocator;
259 GstAllocationParams params;
264 static GstElementClass *parent_class = NULL;
266 static void gst_base_src_class_init (GstBaseSrcClass * klass);
267 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
268 static void gst_base_src_finalize (GObject * object);
272 gst_base_src_get_type (void)
274 static volatile gsize base_src_type = 0;
276 if (g_once_init_enter (&base_src_type)) {
278 static const GTypeInfo base_src_info = {
279 sizeof (GstBaseSrcClass),
282 (GClassInitFunc) gst_base_src_class_init,
287 (GInstanceInitFunc) gst_base_src_init,
290 _type = g_type_register_static (GST_TYPE_ELEMENT,
291 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
292 g_once_init_leave (&base_src_type, _type);
294 return base_src_type;
297 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
299 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
300 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
302 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
303 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
304 GstPadMode mode, gboolean active);
305 static void gst_base_src_set_property (GObject * object, guint prop_id,
306 const GValue * value, GParamSpec * pspec);
307 static void gst_base_src_get_property (GObject * object, guint prop_id,
308 GValue * value, GParamSpec * pspec);
309 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
311 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
312 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
314 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
317 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
319 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
320 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
321 GstSegment * segment);
322 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
323 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
324 GstEvent * event, GstSegment * segment);
325 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
326 guint64 offset, guint size, GstBuffer ** buf);
327 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
328 guint64 offset, guint size, GstBuffer ** buf);
329 static gboolean gst_base_src_decide_allocation_default (GstBaseSrc * basesrc,
332 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
333 gboolean flushing, gboolean live_play, gboolean * playing);
335 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
336 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
338 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
339 GstStateChange transition);
341 static void gst_base_src_loop (GstPad * pad);
342 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
343 guint64 offset, guint length, GstBuffer ** buf);
344 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
345 guint length, GstBuffer ** buf);
346 static gboolean gst_base_src_seekable (GstBaseSrc * src);
347 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
348 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
349 guint * length, gboolean force);
352 gst_base_src_class_init (GstBaseSrcClass * klass)
354 GObjectClass *gobject_class;
355 GstElementClass *gstelement_class;
357 gobject_class = G_OBJECT_CLASS (klass);
358 gstelement_class = GST_ELEMENT_CLASS (klass);
360 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
362 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
364 parent_class = g_type_class_peek_parent (klass);
366 gobject_class->finalize = gst_base_src_finalize;
367 gobject_class->set_property = gst_base_src_set_property;
368 gobject_class->get_property = gst_base_src_get_property;
370 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
371 g_param_spec_uint ("blocksize", "Block size",
372 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
373 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
375 g_param_spec_int ("num-buffers", "num-buffers",
376 "Number of buffers to output before sending EOS (-1 = unlimited)",
377 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
378 G_PARAM_STATIC_STRINGS));
379 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
380 g_param_spec_boolean ("typefind", "Typefind",
381 "Run typefind before negotiating", DEFAULT_TYPEFIND,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
384 g_param_spec_boolean ("do-timestamp", "Do timestamp",
385 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
386 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 gstelement_class->change_state =
389 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
390 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
392 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
393 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
394 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
395 klass->prepare_seek_segment =
396 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
397 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
398 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
399 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
400 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
401 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
402 klass->decide_allocation =
403 GST_DEBUG_FUNCPTR (gst_base_src_decide_allocation_default);
405 /* Registering debug symbols for function pointers */
406 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
407 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
408 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
409 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
410 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
414 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
417 GstPadTemplate *pad_template;
419 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
421 basesrc->is_live = FALSE;
422 g_mutex_init (&basesrc->live_lock);
423 g_cond_init (&basesrc->live_cond);
424 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
425 basesrc->num_buffers_left = -1;
427 basesrc->can_activate_push = TRUE;
430 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
431 g_return_if_fail (pad_template != NULL);
433 GST_DEBUG_OBJECT (basesrc, "creating src pad");
434 pad = gst_pad_new_from_template (pad_template, "src");
436 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
437 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
438 gst_pad_set_event_function (pad, gst_base_src_event);
439 gst_pad_set_query_function (pad, gst_base_src_query);
440 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
442 /* hold pointer to pad */
443 basesrc->srcpad = pad;
444 GST_DEBUG_OBJECT (basesrc, "adding src pad");
445 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
447 basesrc->blocksize = DEFAULT_BLOCKSIZE;
448 basesrc->clock_id = NULL;
449 /* we operate in BYTES by default */
450 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
451 basesrc->typefind = DEFAULT_TYPEFIND;
452 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
453 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
455 g_cond_init (&basesrc->priv->async_cond);
456 basesrc->priv->start_result = GST_FLOW_FLUSHING;
457 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
458 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
459 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
461 GST_DEBUG_OBJECT (basesrc, "init done");
465 gst_base_src_finalize (GObject * object)
470 basesrc = GST_BASE_SRC (object);
472 g_mutex_clear (&basesrc->live_lock);
473 g_cond_clear (&basesrc->live_cond);
474 g_cond_clear (&basesrc->priv->async_cond);
476 event_p = &basesrc->pending_seek;
477 gst_event_replace (event_p, NULL);
479 if (basesrc->priv->pending_events) {
480 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
482 g_list_free (basesrc->priv->pending_events);
485 G_OBJECT_CLASS (parent_class)->finalize (object);
489 * gst_base_src_wait_playing:
492 * If the #GstBaseSrcClass.create() method performs its own synchronisation
493 * against the clock it must unblock when going from PLAYING to the PAUSED state
494 * and call this method before continuing to produce the remaining data.
496 * This function will block until a state change to PLAYING happens (in which
497 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
498 * to a state change to READY or a FLUSH event (in which case this function
499 * returns #GST_FLOW_FLUSHING).
501 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
502 * continue. Any other return value should be returned from the create vmethod.
505 gst_base_src_wait_playing (GstBaseSrc * src)
507 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
510 /* block until the state changes, or we get a flush, or something */
511 GST_DEBUG_OBJECT (src, "live source waiting for running state");
513 GST_DEBUG_OBJECT (src, "live source unlocked");
514 if (src->priv->flushing)
516 } while (G_UNLIKELY (!src->live_running));
523 GST_DEBUG_OBJECT (src, "we are flushing");
524 return GST_FLOW_FLUSHING;
529 * gst_base_src_set_live:
530 * @src: base source instance
531 * @live: new live-mode
533 * If the element listens to a live source, @live should
536 * A live source will not produce data in the PAUSED state and
537 * will therefore not be able to participate in the PREROLL phase
538 * of a pipeline. To signal this fact to the application and the
539 * pipeline, the state change return value of the live source will
540 * be GST_STATE_CHANGE_NO_PREROLL.
543 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
545 g_return_if_fail (GST_IS_BASE_SRC (src));
547 GST_OBJECT_LOCK (src);
549 GST_OBJECT_UNLOCK (src);
553 * gst_base_src_is_live:
554 * @src: base source instance
556 * Check if an element is in live mode.
558 * Returns: %TRUE if element is in live mode.
561 gst_base_src_is_live (GstBaseSrc * src)
565 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
567 GST_OBJECT_LOCK (src);
568 result = src->is_live;
569 GST_OBJECT_UNLOCK (src);
575 * gst_base_src_set_format:
576 * @src: base source instance
577 * @format: the format to use
579 * Sets the default format of the source. This will be the format used
580 * for sending SEGMENT events and for performing seeks.
582 * If a format of GST_FORMAT_BYTES is set, the element will be able to
583 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
585 * This function must only be called in states < %GST_STATE_PAUSED.
588 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
590 g_return_if_fail (GST_IS_BASE_SRC (src));
591 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
593 GST_OBJECT_LOCK (src);
594 gst_segment_init (&src->segment, format);
595 GST_OBJECT_UNLOCK (src);
599 * gst_base_src_set_dynamic_size:
600 * @src: base source instance
601 * @dynamic: new dynamic size mode
603 * If not @dynamic, size is only updated when needed, such as when trying to
604 * read past current tracked size. Otherwise, size is checked for upon each
608 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
610 g_return_if_fail (GST_IS_BASE_SRC (src));
612 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
616 * gst_base_src_set_async:
617 * @src: base source instance
618 * @async: new async mode
620 * Configure async behaviour in @src, no state change will block. The open,
621 * close, start, stop, play and pause virtual methods will be executed in a
622 * different thread and are thus allowed to perform blocking operations. Any
623 * blocking operation should be unblocked with the unlock vmethod.
626 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
628 g_return_if_fail (GST_IS_BASE_SRC (src));
630 GST_OBJECT_LOCK (src);
631 src->priv->async = async;
632 GST_OBJECT_UNLOCK (src);
636 * gst_base_src_is_async:
637 * @src: base source instance
639 * Get the current async behaviour of @src. See also gst_base_src_set_async().
641 * Returns: %TRUE if @src is operating in async mode.
644 gst_base_src_is_async (GstBaseSrc * src)
648 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
650 GST_OBJECT_LOCK (src);
651 res = src->priv->async;
652 GST_OBJECT_UNLOCK (src);
659 * gst_base_src_query_latency:
661 * @live: (out) (allow-none): if the source is live
662 * @min_latency: (out) (allow-none): the min latency of the source
663 * @max_latency: (out) (allow-none): the max latency of the source
665 * Query the source for the latency parameters. @live will be TRUE when @src is
666 * configured as a live source. @min_latency will be set to the difference
667 * between the running time and the timestamp of the first buffer.
668 * @max_latency is always the undefined value of -1.
670 * This function is mostly used by subclasses.
672 * Returns: TRUE if the query succeeded.
675 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
676 GstClockTime * min_latency, GstClockTime * max_latency)
680 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
682 GST_OBJECT_LOCK (src);
684 *live = src->is_live;
686 /* if we have a startup latency, report this one, else report 0. Subclasses
687 * are supposed to override the query function if they want something
689 if (src->priv->latency != -1)
690 min = src->priv->latency;
699 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
700 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
702 GST_OBJECT_UNLOCK (src);
708 * gst_base_src_set_blocksize:
710 * @blocksize: the new blocksize in bytes
712 * Set the number of bytes that @src will push out with each buffer. When
713 * @blocksize is set to -1, a default length will be used.
716 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
718 g_return_if_fail (GST_IS_BASE_SRC (src));
720 GST_OBJECT_LOCK (src);
721 src->blocksize = blocksize;
722 GST_OBJECT_UNLOCK (src);
726 * gst_base_src_get_blocksize:
729 * Get the number of bytes that @src will push out with each buffer.
731 * Returns: the number of bytes pushed with each buffer.
734 gst_base_src_get_blocksize (GstBaseSrc * src)
738 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
740 GST_OBJECT_LOCK (src);
741 res = src->blocksize;
742 GST_OBJECT_UNLOCK (src);
749 * gst_base_src_set_do_timestamp:
751 * @timestamp: enable or disable timestamping
753 * Configure @src to automatically timestamp outgoing buffers based on the
754 * current running_time of the pipeline. This property is mostly useful for live
758 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
760 g_return_if_fail (GST_IS_BASE_SRC (src));
762 GST_OBJECT_LOCK (src);
763 src->priv->do_timestamp = timestamp;
764 GST_OBJECT_UNLOCK (src);
768 * gst_base_src_get_do_timestamp:
771 * Query if @src timestamps outgoing buffers based on the current running_time.
773 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
776 gst_base_src_get_do_timestamp (GstBaseSrc * src)
780 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
782 GST_OBJECT_LOCK (src);
783 res = src->priv->do_timestamp;
784 GST_OBJECT_UNLOCK (src);
790 * gst_base_src_new_seamless_segment:
792 * @start: The new start value for the segment
793 * @stop: Stop value for the new segment
794 * @time: The new time value for the start of the new segent
796 * Prepare a new seamless segment for emission downstream. This function must
797 * only be called by derived sub-classes, and only from the create() function,
798 * as the stream-lock needs to be held.
800 * The format for the new segment will be the current format of the source, as
801 * configured with gst_base_src_set_format()
803 * Returns: %TRUE if preparation of the seamless segment succeeded.
806 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
811 GST_OBJECT_LOCK (src);
813 src->segment.base = gst_segment_to_running_time (&src->segment,
814 src->segment.format, src->segment.position);
815 src->segment.position = src->segment.start = start;
816 src->segment.stop = stop;
817 src->segment.time = time;
819 /* Mark pending segment. Will be sent before next data */
820 src->priv->segment_pending = TRUE;
822 GST_DEBUG_OBJECT (src,
823 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
824 GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
825 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
826 GST_TIME_ARGS (src->segment.base));
828 GST_OBJECT_UNLOCK (src);
830 src->priv->discont = TRUE;
837 gst_base_src_send_stream_start (GstBaseSrc * src)
841 if (src->priv->stream_start_pending) {
846 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
848 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
849 event = gst_event_new_stream_start (stream_id);
850 gst_event_set_group_id (event, gst_util_group_id_next ());
852 ret = gst_pad_push_event (src->srcpad, event);
853 src->priv->stream_start_pending = FALSE;
861 * gst_base_src_set_caps:
862 * @src: a #GstBaseSrc
865 * Set new caps on the basesrc source pad.
867 * Returns: %TRUE if the caps could be set
870 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
872 GstBaseSrcClass *bclass;
875 bclass = GST_BASE_SRC_GET_CLASS (src);
877 gst_base_src_send_stream_start (src);
879 if (bclass->set_caps)
880 res = bclass->set_caps (src, caps);
883 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
889 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
891 GstCaps *caps = NULL;
892 GstPadTemplate *pad_template;
893 GstBaseSrcClass *bclass;
895 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
898 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
900 if (pad_template != NULL) {
901 caps = gst_pad_template_get_caps (pad_template);
904 GstCaps *intersection;
907 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
908 gst_caps_unref (caps);
916 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
918 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
919 return gst_caps_fixate (caps);
923 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
925 GstBaseSrcClass *bclass;
927 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
930 caps = bclass->fixate (bsrc, caps);
936 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
940 switch (GST_QUERY_TYPE (query)) {
941 case GST_QUERY_POSITION:
945 gst_query_parse_position (query, &format, NULL);
947 GST_DEBUG_OBJECT (src, "position query in format %s",
948 gst_format_get_name (format));
951 case GST_FORMAT_PERCENT:
957 GST_OBJECT_LOCK (src);
958 position = src->segment.position;
959 duration = src->segment.duration;
960 GST_OBJECT_UNLOCK (src);
962 if (position != -1 && duration != -1) {
963 if (position < duration)
964 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
967 percent = GST_FORMAT_PERCENT_MAX;
971 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
978 GstFormat seg_format;
980 GST_OBJECT_LOCK (src);
982 gst_segment_to_stream_time (&src->segment, src->segment.format,
983 src->segment.position);
984 seg_format = src->segment.format;
985 GST_OBJECT_UNLOCK (src);
987 if (position != -1) {
988 /* convert to requested format */
990 gst_pad_query_convert (src->srcpad, seg_format,
991 position, format, &position);
995 gst_query_set_position (query, format, position);
1001 case GST_QUERY_DURATION:
1005 gst_query_parse_duration (query, &format, NULL);
1007 GST_DEBUG_OBJECT (src, "duration query in format %s",
1008 gst_format_get_name (format));
1011 case GST_FORMAT_PERCENT:
1012 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1013 GST_FORMAT_PERCENT_MAX);
1019 GstFormat seg_format;
1022 /* may have to refresh duration */
1023 gst_base_src_update_length (src, 0, &length,
1024 g_atomic_int_get (&src->priv->dynamic_size));
1026 /* this is the duration as configured by the subclass. */
1027 GST_OBJECT_LOCK (src);
1028 duration = src->segment.duration;
1029 seg_format = src->segment.format;
1030 GST_OBJECT_UNLOCK (src);
1032 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1033 duration, gst_format_get_name (seg_format));
1035 if (duration != -1) {
1036 /* convert to requested format, if this fails, we have a duration
1037 * but we cannot answer the query, we must return FALSE. */
1039 gst_pad_query_convert (src->srcpad, seg_format,
1040 duration, format, &duration);
1042 /* The subclass did not configure a duration, we assume that the
1043 * media has an unknown duration then and we return TRUE to report
1044 * this. Note that this is not the same as returning FALSE, which
1045 * means that we cannot report the duration at all. */
1048 gst_query_set_duration (query, format, duration);
1055 case GST_QUERY_SEEKING:
1057 GstFormat format, seg_format;
1060 GST_OBJECT_LOCK (src);
1061 duration = src->segment.duration;
1062 seg_format = src->segment.format;
1063 GST_OBJECT_UNLOCK (src);
1065 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1066 if (format == seg_format) {
1067 gst_query_set_seeking (query, seg_format,
1068 gst_base_src_seekable (src), 0, duration);
1071 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1072 /* Don't reply to the query to make up for demuxers which don't
1073 * handle the SEEKING query yet. Players like Totem will fall back
1074 * to the duration when the SEEKING query isn't answered. */
1079 case GST_QUERY_SEGMENT:
1084 GST_OBJECT_LOCK (src);
1086 format = src->segment.format;
1089 gst_segment_to_stream_time (&src->segment, format,
1090 src->segment.start);
1091 if ((stop = src->segment.stop) == -1)
1092 stop = src->segment.duration;
1094 stop = gst_segment_to_stream_time (&src->segment, format, stop);
1096 gst_query_set_segment (query, src->segment.rate, format, start, stop);
1098 GST_OBJECT_UNLOCK (src);
1103 case GST_QUERY_FORMATS:
1105 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1106 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1110 case GST_QUERY_CONVERT:
1112 GstFormat src_fmt, dest_fmt;
1113 gint64 src_val, dest_val;
1115 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1117 /* we can only convert between equal formats... */
1118 if (src_fmt == dest_fmt) {
1124 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1127 case GST_QUERY_LATENCY:
1129 GstClockTime min, max;
1132 /* Subclasses should override and implement something useful */
1133 res = gst_base_src_query_latency (src, &live, &min, &max);
1135 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1136 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1137 GST_TIME_ARGS (max));
1139 gst_query_set_latency (query, live, min, max);
1142 case GST_QUERY_JITTER:
1143 case GST_QUERY_RATE:
1146 case GST_QUERY_BUFFERING:
1148 GstFormat format, seg_format;
1149 gint64 start, stop, estimated;
1151 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1153 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1154 gst_format_get_name (format));
1156 GST_OBJECT_LOCK (src);
1157 if (src->random_access) {
1160 if (format == GST_FORMAT_PERCENT)
1161 stop = GST_FORMAT_PERCENT_MAX;
1163 stop = src->segment.duration;
1169 seg_format = src->segment.format;
1170 GST_OBJECT_UNLOCK (src);
1172 /* convert to required format. When the conversion fails, we can't answer
1173 * the query. When the value is unknown, we can don't perform conversion
1174 * but report TRUE. */
1175 if (format != GST_FORMAT_PERCENT && stop != -1) {
1176 res = gst_pad_query_convert (src->srcpad, seg_format,
1177 stop, format, &stop);
1181 if (res && format != GST_FORMAT_PERCENT && start != -1)
1182 res = gst_pad_query_convert (src->srcpad, seg_format,
1183 start, format, &start);
1185 gst_query_set_buffering_range (query, format, start, stop, estimated);
1188 case GST_QUERY_SCHEDULING:
1190 gboolean random_access;
1192 random_access = gst_base_src_is_random_access (src);
1194 /* we can operate in getrange mode if the native format is bytes
1195 * and we are seekable, this condition is set in the random_access
1196 * flag and is set in the _start() method. */
1197 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1199 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1200 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1205 case GST_QUERY_CAPS:
1207 GstBaseSrcClass *bclass;
1208 GstCaps *caps, *filter;
1210 bclass = GST_BASE_SRC_GET_CLASS (src);
1211 if (bclass->get_caps) {
1212 gst_query_parse_caps (query, &filter);
1213 if ((caps = bclass->get_caps (src, filter))) {
1214 gst_query_set_caps_result (query, caps);
1215 gst_caps_unref (caps);
1224 case GST_QUERY_URI:{
1225 if (GST_IS_URI_HANDLER (src)) {
1226 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1229 gst_query_set_uri (query, uri);
1244 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1251 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1254 GstBaseSrcClass *bclass;
1255 gboolean result = FALSE;
1257 src = GST_BASE_SRC (parent);
1258 bclass = GST_BASE_SRC_GET_CLASS (src);
1261 result = bclass->query (src, query);
1267 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1269 gboolean res = TRUE;
1271 /* update our offset if the start/stop position was updated */
1272 if (segment->format == GST_FORMAT_BYTES) {
1273 segment->time = segment->start;
1274 } else if (segment->start == 0) {
1275 /* seek to start, we can implement a default for this. */
1279 GST_INFO_OBJECT (src, "Can't do a default seek");
1286 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1288 GstBaseSrcClass *bclass;
1289 gboolean result = FALSE;
1291 bclass = GST_BASE_SRC_GET_CLASS (src);
1293 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1295 if (bclass->do_seek)
1296 result = bclass->do_seek (src, segment);
1301 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1304 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1305 GstSegment * segment)
1307 /* By default, we try one of 2 things:
1308 * - For absolute seek positions, convert the requested position to our
1309 * configured processing format and place it in the output segment \
1310 * - For relative seek positions, convert our current (input) values to the
1311 * seek format, adjust by the relative seek offset and then convert back to
1312 * the processing format
1314 GstSeekType start_type, stop_type;
1317 GstFormat seek_format, dest_format;
1320 gboolean res = TRUE;
1322 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1323 &start_type, &start, &stop_type, &stop);
1324 dest_format = segment->format;
1326 if (seek_format == dest_format) {
1327 gst_segment_do_seek (segment, rate, seek_format, flags,
1328 start_type, start, stop_type, stop, &update);
1332 if (start_type != GST_SEEK_TYPE_NONE) {
1333 /* FIXME: Handle seek_end by converting the input segment vals */
1335 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1337 start_type = GST_SEEK_TYPE_SET;
1340 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1341 /* FIXME: Handle seek_end by converting the input segment vals */
1343 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1345 stop_type = GST_SEEK_TYPE_SET;
1348 /* And finally, configure our output segment in the desired format */
1349 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1350 stop_type, stop, &update);
1359 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1365 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1366 GstSegment * seeksegment)
1368 GstBaseSrcClass *bclass;
1369 gboolean result = FALSE;
1371 bclass = GST_BASE_SRC_GET_CLASS (src);
1373 if (bclass->prepare_seek_segment)
1374 result = bclass->prepare_seek_segment (src, event, seeksegment);
1379 static GstFlowReturn
1380 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1381 guint size, GstBuffer ** buffer)
1384 GstBaseSrcPrivate *priv = src->priv;
1387 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1388 } else if (size != -1) {
1389 *buffer = gst_buffer_new_allocate (priv->allocator, size, &priv->params);
1390 if (G_UNLIKELY (*buffer == NULL))
1395 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1404 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1405 return GST_FLOW_ERROR;
1409 static GstFlowReturn
1410 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1411 guint size, GstBuffer ** buffer)
1413 GstBaseSrcClass *bclass;
1417 bclass = GST_BASE_SRC_GET_CLASS (src);
1419 if (G_UNLIKELY (!bclass->alloc))
1421 if (G_UNLIKELY (!bclass->fill))
1424 if (*buffer == NULL) {
1425 /* downstream did not provide us with a buffer to fill, allocate one
1427 ret = bclass->alloc (src, offset, size, &res_buf);
1428 if (G_UNLIKELY (ret != GST_FLOW_OK))
1434 if (G_LIKELY (size > 0)) {
1435 /* only call fill when there is a size */
1436 ret = bclass->fill (src, offset, size, res_buf);
1437 if (G_UNLIKELY (ret != GST_FLOW_OK))
1448 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1449 return GST_FLOW_NOT_SUPPORTED;
1453 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1458 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1459 gst_flow_get_name (ret));
1460 if (*buffer == NULL)
1461 gst_buffer_unref (res_buf);
1466 /* this code implements the seeking. It is a good example
1467 * handling all cases.
1469 * A seek updates the currently configured segment.start
1470 * and segment.stop values based on the SEEK_TYPE. If the
1471 * segment.start value is updated, a seek to this new position
1472 * should be performed.
1474 * The seek can only be executed when we are not currently
1475 * streaming any data, to make sure that this is the case, we
1476 * acquire the STREAM_LOCK which is taken when we are in the
1477 * _loop() function or when a getrange() is called. Normally
1478 * we will not receive a seek if we are operating in pull mode
1479 * though. When we operate as a live source we might block on the live
1480 * cond, which does not release the STREAM_LOCK. Therefore we will try
1481 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1482 * safe to perform the seek.
1484 * When we are in the loop() function, we might be in the middle
1485 * of pushing a buffer, which might block in a sink. To make sure
1486 * that the push gets unblocked we push out a FLUSH_START event.
1487 * Our loop function will get a FLUSHING return value from
1488 * the push and will pause, effectively releasing the STREAM_LOCK.
1490 * For a non-flushing seek, we pause the task, which might eventually
1491 * release the STREAM_LOCK. We say eventually because when the sink
1492 * blocks on the sample we might wait a very long time until the sink
1493 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1494 * can continue the seek. A non-flushing seek is normally done in a
1495 * running pipeline to perform seamless playback, this means that the sink is
1496 * PLAYING and will return from its chain function.
1497 * In the case of a non-flushing seek we need to make sure that the
1498 * data we output after the seek is continuous with the previous data,
1499 * this is because a non-flushing seek does not reset the running-time
1500 * to 0. We do this by closing the currently running segment, ie. sending
1501 * a new_segment event with the stop position set to the last processed
1504 * After updating the segment.start/stop values, we prepare for
1505 * streaming again. We push out a FLUSH_STOP to make the peer pad
1506 * accept data again and we start our task again.
1508 * A segment seek posts a message on the bus saying that the playback
1509 * of the segment started. We store the segment flag internally because
1510 * when we reach the segment.stop we have to post a segment.done
1511 * instead of EOS when doing a segment seek.
1514 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1516 gboolean res = TRUE, tres;
1518 GstFormat seek_format, dest_format;
1520 GstSeekType start_type, stop_type;
1522 gboolean flush, playing;
1524 gboolean relative_seek = FALSE;
1525 gboolean seekseg_configured = FALSE;
1526 GstSegment seeksegment;
1530 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1532 GST_OBJECT_LOCK (src);
1533 dest_format = src->segment.format;
1534 GST_OBJECT_UNLOCK (src);
1537 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1538 &start_type, &start, &stop_type, &stop);
1540 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1541 SEEK_TYPE_IS_RELATIVE (stop_type);
1543 if (dest_format != seek_format && !relative_seek) {
1544 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1545 * here before taking the stream lock, otherwise we must convert it later,
1546 * once we have the stream lock and can read the last configures segment
1547 * start and stop positions */
1548 gst_segment_init (&seeksegment, dest_format);
1550 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1551 goto prepare_failed;
1553 seekseg_configured = TRUE;
1556 flush = flags & GST_SEEK_FLAG_FLUSH;
1557 seqnum = gst_event_get_seqnum (event);
1560 /* get next seqnum */
1561 seqnum = gst_util_seqnum_next ();
1564 /* send flush start */
1566 tevent = gst_event_new_flush_start ();
1567 gst_event_set_seqnum (tevent, seqnum);
1568 gst_pad_push_event (src->srcpad, tevent);
1570 gst_pad_pause_task (src->srcpad);
1572 /* unblock streaming thread. */
1574 gst_base_src_set_flushing (src, TRUE, FALSE, &playing);
1576 /* grab streaming lock, this should eventually be possible, either
1577 * because the task is paused, our streaming thread stopped
1578 * or because our peer is flushing. */
1579 GST_PAD_STREAM_LOCK (src->srcpad);
1580 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1581 /* we have seen this event before, issue a warning for now */
1582 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1585 src->priv->seqnum = seqnum;
1586 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1590 gst_base_src_set_flushing (src, FALSE, playing, NULL);
1592 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1593 * copy the current segment info into the temp segment that we can actually
1594 * attempt the seek with. We only update the real segment if the seek succeeds. */
1595 if (!seekseg_configured) {
1596 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1598 /* now configure the final seek segment */
1600 if (seeksegment.format != seek_format) {
1601 /* OK, here's where we give the subclass a chance to convert the relative
1602 * seek into an absolute one in the processing format. We set up any
1603 * absolute seek above, before taking the stream lock. */
1604 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1605 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1610 /* The seek format matches our processing format, no need to ask the
1611 * the subclass to configure the segment. */
1612 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1613 start_type, start, stop_type, stop, &update);
1616 /* Else, no seek event passed, so we're just (re)starting the
1621 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1622 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1623 seeksegment.start, seeksegment.stop, seeksegment.position);
1625 /* do the seek, segment.position contains the new position. */
1626 res = gst_base_src_do_seek (src, &seeksegment);
1629 /* and prepare to continue streaming */
1631 tevent = gst_event_new_flush_stop (TRUE);
1632 gst_event_set_seqnum (tevent, seqnum);
1633 /* send flush stop, peer will accept data and events again. We
1634 * are not yet providing data as we still have the STREAM_LOCK. */
1635 gst_pad_push_event (src->srcpad, tevent);
1638 /* The subclass must have converted the segment to the processing format
1640 if (res && seeksegment.format != dest_format) {
1641 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1642 "in the correct format. Aborting seek.");
1646 /* if the seek was successful, we update our real segment and push
1647 * out the new segment. */
1649 GST_OBJECT_LOCK (src);
1650 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1651 GST_OBJECT_UNLOCK (src);
1653 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1654 GstMessage *message;
1656 message = gst_message_new_segment_start (GST_OBJECT (src),
1657 seeksegment.format, seeksegment.position);
1658 gst_message_set_seqnum (message, seqnum);
1660 gst_element_post_message (GST_ELEMENT (src), message);
1663 /* for deriving a stop position for the playback segment from the seek
1664 * segment, we must take the duration when the stop is not set */
1665 /* FIXME: This is never used below */
1666 if ((stop = seeksegment.stop) == -1)
1667 stop = seeksegment.duration;
1669 src->priv->segment_pending = TRUE;
1672 src->priv->discont = TRUE;
1673 src->running = TRUE;
1674 /* and restart the task in case it got paused explicitly or by
1675 * the FLUSH_START event we pushed out. */
1676 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1681 /* and release the lock again so we can continue streaming */
1682 GST_PAD_STREAM_UNLOCK (src->srcpad);
1688 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1693 /* all events send to this element directly. This is mainly done from the
1697 gst_base_src_send_event (GstElement * element, GstEvent * event)
1700 gboolean result = FALSE;
1701 GstBaseSrcClass *bclass;
1703 src = GST_BASE_SRC (element);
1704 bclass = GST_BASE_SRC_GET_CLASS (src);
1706 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1708 switch (GST_EVENT_TYPE (event)) {
1709 /* bidirectional events */
1710 case GST_EVENT_FLUSH_START:
1711 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1712 result = gst_pad_push_event (src->srcpad, event);
1713 /* also unblock the create function */
1714 gst_base_src_activate_pool (src, FALSE);
1715 /* unlock any subclasses, we need to do this before grabbing the
1716 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
1717 * unlock to the params because of backwards compat (see seek handler)*/
1719 bclass->unlock (src);
1721 /* the live lock is released when we are blocked, waiting for playing or
1722 * when we sync to the clock. */
1723 GST_LIVE_LOCK (src);
1724 src->priv->flushing = TRUE;
1725 /* clear pending EOS if any */
1726 g_atomic_int_set (&src->priv->pending_eos, FALSE);
1727 if (bclass->unlock_stop)
1728 bclass->unlock_stop (src);
1730 gst_clock_id_unschedule (src->clock_id);
1731 GST_DEBUG_OBJECT (src, "signal");
1732 GST_LIVE_SIGNAL (src);
1733 GST_LIVE_UNLOCK (src);
1736 case GST_EVENT_FLUSH_STOP:
1740 GST_LIVE_LOCK (src);
1741 src->priv->segment_pending = TRUE;
1742 src->priv->flushing = FALSE;
1743 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1744 result = gst_pad_push_event (src->srcpad, event);
1746 gst_base_src_activate_pool (src, TRUE);
1748 GST_OBJECT_LOCK (src->srcpad);
1749 start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1750 GST_OBJECT_UNLOCK (src->srcpad);
1752 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1754 GST_LIVE_UNLOCK (src);
1759 /* downstream serialized events */
1762 /* queue EOS and make sure the task or pull function performs the EOS
1765 * We have two possibilities:
1767 * - Before we are to enter the _create function, we check the pending_eos
1768 * first and do EOS instead of entering it.
1769 * - If we are in the _create function or we did not manage to set the
1770 * flag fast enough and we are about to enter the _create function,
1771 * we unlock it so that we exit with FLUSHING immediately. We then
1772 * check the EOS flag and do the EOS logic.
1774 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1775 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1778 /* unlock the _create function so that we can check the pending_eos flag
1779 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1780 * that we can grab it and stop the unlock again. We don't take the stream
1781 * lock so that this operation is guaranteed to never block. */
1782 gst_base_src_activate_pool (src, FALSE);
1784 bclass->unlock (src);
1786 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1788 GST_LIVE_LOCK (src);
1789 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1790 /* now stop the unlock of the streaming thread again. Grabbing the live
1791 * lock is enough because that protects the create function. */
1792 if (bclass->unlock_stop)
1793 bclass->unlock_stop (src);
1794 gst_base_src_activate_pool (src, TRUE);
1795 GST_LIVE_UNLOCK (src);
1800 case GST_EVENT_SEGMENT:
1801 /* sending random SEGMENT downstream can break sync. */
1804 case GST_EVENT_CUSTOM_DOWNSTREAM:
1805 case GST_EVENT_CUSTOM_BOTH:
1806 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1807 GST_OBJECT_LOCK (src);
1808 src->priv->pending_events =
1809 g_list_append (src->priv->pending_events, event);
1810 g_atomic_int_set (&src->priv->have_events, TRUE);
1811 GST_OBJECT_UNLOCK (src);
1815 case GST_EVENT_BUFFERSIZE:
1816 /* does not seem to make much sense currently */
1819 /* upstream events */
1821 /* elements should override send_event and do something */
1823 case GST_EVENT_SEEK:
1827 GST_OBJECT_LOCK (src->srcpad);
1828 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1830 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1831 GST_OBJECT_UNLOCK (src->srcpad);
1834 GST_DEBUG_OBJECT (src, "performing seek");
1835 /* when we are running in push mode, we can execute the
1836 * seek right now. */
1837 result = gst_base_src_perform_seek (src, event, TRUE);
1841 /* else we store the event and execute the seek when we
1843 GST_OBJECT_LOCK (src);
1844 GST_DEBUG_OBJECT (src, "queueing seek");
1845 event_p = &src->pending_seek;
1846 gst_event_replace ((GstEvent **) event_p, event);
1847 GST_OBJECT_UNLOCK (src);
1848 /* assume the seek will work */
1853 case GST_EVENT_NAVIGATION:
1854 /* could make sense for elements that do something with navigation events
1855 * but then they would need to override the send_event function */
1857 case GST_EVENT_LATENCY:
1858 /* does not seem to make sense currently */
1862 case GST_EVENT_CUSTOM_UPSTREAM:
1863 /* override send_event if you want this */
1865 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1866 case GST_EVENT_CUSTOM_BOTH_OOB:
1867 /* insert a random custom event into the pipeline */
1868 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1869 result = gst_pad_push_event (src->srcpad, event);
1870 /* we gave away the ref to the event in the push */
1877 /* if we still have a ref to the event, unref it now */
1879 gst_event_unref (event);
1886 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1887 GST_OBJECT_UNLOCK (src->srcpad);
1894 gst_base_src_seekable (GstBaseSrc * src)
1896 GstBaseSrcClass *bclass;
1897 bclass = GST_BASE_SRC_GET_CLASS (src);
1898 if (bclass->is_seekable)
1899 return bclass->is_seekable (src);
1905 gst_base_src_update_qos (GstBaseSrc * src,
1906 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1908 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1909 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1910 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1912 GST_OBJECT_LOCK (src);
1913 src->priv->proportion = proportion;
1914 src->priv->earliest_time = timestamp + diff;
1915 GST_OBJECT_UNLOCK (src);
1920 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1924 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1926 switch (GST_EVENT_TYPE (event)) {
1927 case GST_EVENT_SEEK:
1928 /* is normally called when in push mode */
1929 if (!gst_base_src_seekable (src))
1932 result = gst_base_src_perform_seek (src, event, TRUE);
1934 case GST_EVENT_FLUSH_START:
1935 /* cancel any blocking getrange, is normally called
1936 * when in pull mode. */
1937 result = gst_base_src_set_flushing (src, TRUE, FALSE, NULL);
1939 case GST_EVENT_FLUSH_STOP:
1940 result = gst_base_src_set_flushing (src, FALSE, TRUE, NULL);
1945 GstClockTimeDiff diff;
1946 GstClockTime timestamp;
1948 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1949 gst_base_src_update_qos (src, proportion, diff, timestamp);
1953 case GST_EVENT_RECONFIGURE:
1956 case GST_EVENT_LATENCY:
1968 GST_DEBUG_OBJECT (src, "is not seekable");
1974 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1977 GstBaseSrcClass *bclass;
1978 gboolean result = FALSE;
1980 src = GST_BASE_SRC (parent);
1981 bclass = GST_BASE_SRC_GET_CLASS (src);
1983 if (bclass->event) {
1984 if (!(result = bclass->event (src, event)))
1985 goto subclass_failed;
1989 gst_event_unref (event);
1996 GST_DEBUG_OBJECT (src, "subclass refused event");
2002 gst_base_src_set_property (GObject * object, guint prop_id,
2003 const GValue * value, GParamSpec * pspec)
2007 src = GST_BASE_SRC (object);
2010 case PROP_BLOCKSIZE:
2011 gst_base_src_set_blocksize (src, g_value_get_uint (value));
2013 case PROP_NUM_BUFFERS:
2014 src->num_buffers = g_value_get_int (value);
2017 src->typefind = g_value_get_boolean (value);
2019 case PROP_DO_TIMESTAMP:
2020 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2023 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2029 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2034 src = GST_BASE_SRC (object);
2037 case PROP_BLOCKSIZE:
2038 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2040 case PROP_NUM_BUFFERS:
2041 g_value_set_int (value, src->num_buffers);
2044 g_value_set_boolean (value, src->typefind);
2046 case PROP_DO_TIMESTAMP:
2047 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2050 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2055 /* with STREAM_LOCK and LOCK */
2056 static GstClockReturn
2057 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2062 id = gst_clock_new_single_shot_id (clock, time);
2064 basesrc->clock_id = id;
2065 /* release the live lock while waiting */
2066 GST_LIVE_UNLOCK (basesrc);
2068 ret = gst_clock_id_wait (id, NULL);
2070 GST_LIVE_LOCK (basesrc);
2071 gst_clock_id_unref (id);
2072 basesrc->clock_id = NULL;
2077 /* perform synchronisation on a buffer.
2080 static GstClockReturn
2081 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2083 GstClockReturn result;
2084 GstClockTime start, end;
2085 GstBaseSrcClass *bclass;
2086 GstClockTime base_time;
2088 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2089 gboolean do_timestamp, first, pseudo_live, is_live;
2091 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2094 if (bclass->get_times)
2095 bclass->get_times (basesrc, buffer, &start, &end);
2097 /* get buffer timestamp */
2098 dts = GST_BUFFER_DTS (buffer);
2099 pts = GST_BUFFER_PTS (buffer);
2101 if (GST_CLOCK_TIME_IS_VALID (dts))
2106 /* grab the lock to prepare for clocking and calculate the startup
2108 GST_OBJECT_LOCK (basesrc);
2110 is_live = basesrc->is_live;
2111 /* if we are asked to sync against the clock we are a pseudo live element */
2112 pseudo_live = (start != -1 && is_live);
2113 /* check for the first buffer */
2114 first = (basesrc->priv->latency == -1);
2116 if (timestamp != -1 && pseudo_live) {
2117 GstClockTime latency;
2119 /* we have a timestamp and a sync time, latency is the diff */
2120 if (timestamp <= start)
2121 latency = start - timestamp;
2126 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2127 GST_TIME_ARGS (latency));
2128 /* first time we calculate latency, just configure */
2129 basesrc->priv->latency = latency;
2131 if (basesrc->priv->latency != latency) {
2132 /* we have a new latency, FIXME post latency message */
2133 basesrc->priv->latency = latency;
2134 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2135 GST_TIME_ARGS (latency));
2139 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2140 is_live, start != -1);
2141 basesrc->priv->latency = 0;
2144 /* get clock, if no clock, we can't sync or do timestamps */
2145 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2148 gst_object_ref (clock);
2150 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2152 do_timestamp = basesrc->priv->do_timestamp;
2153 GST_OBJECT_UNLOCK (basesrc);
2155 /* first buffer, calculate the timestamp offset */
2157 GstClockTime running_time;
2159 now = gst_clock_get_time (clock);
2160 running_time = now - base_time;
2162 GST_LOG_OBJECT (basesrc,
2163 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2164 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2165 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2167 if (pseudo_live && timestamp != -1) {
2168 /* live source and we need to sync, add startup latency to all timestamps
2169 * to get the real running_time. Live sources should always timestamp
2170 * according to the current running time. */
2171 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2173 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2174 GST_TIME_ARGS (basesrc->priv->ts_offset));
2176 basesrc->priv->ts_offset = 0;
2177 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2180 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2186 GST_BUFFER_DTS (buffer) = dts;
2188 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2189 GST_TIME_ARGS (dts));
2192 /* not the first buffer, the timestamp is the diff between the clock and
2194 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2195 now = gst_clock_get_time (clock);
2197 dts = now - base_time;
2198 GST_BUFFER_DTS (buffer) = dts;
2200 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2201 GST_TIME_ARGS (dts));
2204 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2205 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2208 GST_BUFFER_PTS (buffer) = dts;
2210 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2211 GST_TIME_ARGS (pts));
2214 /* if we don't have a buffer timestamp, we don't sync */
2215 if (!GST_CLOCK_TIME_IS_VALID (start))
2219 /* for pseudo live sources, add our ts_offset to the timestamp */
2220 if (GST_CLOCK_TIME_IS_VALID (pts))
2221 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2222 if (GST_CLOCK_TIME_IS_VALID (dts))
2223 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2224 start += basesrc->priv->ts_offset;
2227 GST_LOG_OBJECT (basesrc,
2228 "waiting for clock, base time %" GST_TIME_FORMAT
2229 ", stream_start %" GST_TIME_FORMAT,
2230 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2232 result = gst_base_src_wait (basesrc, clock, start + base_time);
2234 gst_object_unref (clock);
2236 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2243 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2244 GST_OBJECT_UNLOCK (basesrc);
2245 return GST_CLOCK_OK;
2249 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2250 gst_object_unref (clock);
2251 return GST_CLOCK_OK;
2255 /* Called with STREAM_LOCK and LIVE_LOCK */
2257 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2260 guint64 size, maxsize;
2261 GstBaseSrcClass *bclass;
2265 bclass = GST_BASE_SRC_GET_CLASS (src);
2267 format = src->segment.format;
2268 stop = src->segment.stop;
2269 /* get total file size */
2270 size = src->segment.duration;
2272 /* only operate if we are working with bytes */
2273 if (format != GST_FORMAT_BYTES)
2276 /* the max amount of bytes to read is the total size or
2277 * up to the segment.stop if present. */
2279 maxsize = MIN (size, stop);
2283 GST_DEBUG_OBJECT (src,
2284 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2285 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2286 *length, size, stop, maxsize);
2288 /* check size if we have one */
2289 if (maxsize != -1) {
2290 /* if we run past the end, check if the file became bigger and
2292 if (G_UNLIKELY (offset + *length >= maxsize || force)) {
2293 /* see if length of the file changed */
2294 if (bclass->get_size)
2295 if (!bclass->get_size (src, &size))
2298 /* make sure we don't exceed the configured segment stop
2301 maxsize = MIN (size, stop);
2305 /* if we are at or past the end, EOS */
2306 if (G_UNLIKELY (offset >= maxsize))
2307 goto unexpected_length;
2309 /* else we can clip to the end */
2310 if (G_UNLIKELY (offset + *length >= maxsize))
2311 *length = maxsize - offset;
2316 /* keep track of current duration.
2317 * segment is in bytes, we checked that above. */
2318 GST_OBJECT_LOCK (src);
2319 src->segment.duration = size;
2320 GST_OBJECT_UNLOCK (src);
2331 /* must be called with LIVE_LOCK */
2332 static GstFlowReturn
2333 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2337 GstBaseSrcClass *bclass;
2338 GstClockReturn status;
2342 bclass = GST_BASE_SRC_GET_CLASS (src);
2346 if (G_UNLIKELY (!src->live_running)) {
2347 ret = gst_base_src_wait_playing (src);
2348 if (ret != GST_FLOW_OK)
2353 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2354 && !GST_BASE_SRC_IS_STARTING (src)))
2357 if (G_UNLIKELY (!bclass->create))
2360 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2361 goto unexpected_length;
2363 /* track position */
2364 GST_OBJECT_LOCK (src);
2365 if (src->segment.format == GST_FORMAT_BYTES)
2366 src->segment.position = offset;
2367 GST_OBJECT_UNLOCK (src);
2369 /* normally we don't count buffers */
2370 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2371 if (src->num_buffers_left == 0)
2372 goto reached_num_buffers;
2374 src->num_buffers_left--;
2377 /* don't enter the create function if a pending EOS event was set. For the
2378 * logic of the pending_eos, check the event function of this class. */
2379 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2382 GST_DEBUG_OBJECT (src,
2383 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2384 G_GINT64_FORMAT, offset, length, src->segment.time);
2386 res_buf = in_buf = *buf;
2388 ret = bclass->create (src, offset, length, &res_buf);
2390 /* The create function could be unlocked because we have a pending EOS. It's
2391 * possible that we have a valid buffer from create that we need to
2392 * discard when the create function returned _OK. */
2393 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2394 if (ret == GST_FLOW_OK) {
2396 gst_buffer_unref (res_buf);
2401 if (G_UNLIKELY (ret != GST_FLOW_OK))
2404 /* fallback in case the create function didn't fill a provided buffer */
2405 if (in_buf != NULL && res_buf != in_buf) {
2409 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2410 "fill the provided buffer, copying");
2412 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2415 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2416 gst_buffer_unmap (in_buf, &info);
2417 gst_buffer_set_size (in_buf, copied_size);
2419 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2421 gst_buffer_unref (res_buf);
2425 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2426 if (offset == 0 && src->segment.time == 0
2427 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2428 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2429 res_buf = gst_buffer_make_writable (res_buf);
2430 GST_BUFFER_DTS (res_buf) = 0;
2433 /* now sync before pushing the buffer */
2434 status = gst_base_src_do_sync (src, res_buf);
2436 /* waiting for the clock could have made us flushing */
2437 if (G_UNLIKELY (src->priv->flushing))
2441 case GST_CLOCK_EARLY:
2442 /* the buffer is too late. We currently don't drop the buffer. */
2443 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2446 /* buffer synchronised properly */
2447 GST_DEBUG_OBJECT (src, "buffer ok");
2449 case GST_CLOCK_UNSCHEDULED:
2450 /* this case is triggered when we were waiting for the clock and
2451 * it got unlocked because we did a state change. In any case, get rid of
2454 gst_buffer_unref (res_buf);
2456 if (!src->live_running) {
2457 /* We return FLUSHING when we are not running to stop the dataflow also
2458 * get rid of the produced buffer. */
2459 GST_DEBUG_OBJECT (src,
2460 "clock was unscheduled (%d), returning FLUSHING", status);
2461 ret = GST_FLOW_FLUSHING;
2463 /* If we are running when this happens, we quickly switched between
2464 * pause and playing. We try to produce a new buffer */
2465 GST_DEBUG_OBJECT (src,
2466 "clock was unscheduled (%d), but we are running", status);
2471 /* all other result values are unexpected and errors */
2472 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2473 (_("Internal clock error.")),
2474 ("clock returned unexpected return value %d", status));
2476 gst_buffer_unref (res_buf);
2477 ret = GST_FLOW_ERROR;
2480 if (G_LIKELY (ret == GST_FLOW_OK))
2488 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2489 gst_flow_get_name (ret));
2494 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2495 gst_flow_get_name (ret));
2500 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2501 (_("Failed to map buffer.")),
2502 ("failed to map result buffer in WRITE mode"));
2504 gst_buffer_unref (res_buf);
2505 return GST_FLOW_ERROR;
2509 GST_DEBUG_OBJECT (src, "getrange but not started");
2510 return GST_FLOW_FLUSHING;
2514 GST_DEBUG_OBJECT (src, "no create function");
2515 return GST_FLOW_NOT_SUPPORTED;
2519 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2520 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2521 return GST_FLOW_EOS;
2523 reached_num_buffers:
2525 GST_DEBUG_OBJECT (src, "sent all buffers");
2526 return GST_FLOW_EOS;
2530 GST_DEBUG_OBJECT (src, "we are flushing");
2532 gst_buffer_unref (res_buf);
2533 return GST_FLOW_FLUSHING;
2537 GST_DEBUG_OBJECT (src, "we are EOS");
2538 return GST_FLOW_EOS;
2542 static GstFlowReturn
2543 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2544 guint length, GstBuffer ** buf)
2549 src = GST_BASE_SRC_CAST (parent);
2551 GST_LIVE_LOCK (src);
2552 if (G_UNLIKELY (src->priv->flushing))
2555 res = gst_base_src_get_range (src, offset, length, buf);
2558 GST_LIVE_UNLOCK (src);
2565 GST_DEBUG_OBJECT (src, "we are flushing");
2566 res = GST_FLOW_FLUSHING;
2572 gst_base_src_is_random_access (GstBaseSrc * src)
2574 /* we need to start the basesrc to check random access */
2575 if (!GST_BASE_SRC_IS_STARTED (src)) {
2576 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2577 if (G_LIKELY (gst_base_src_start (src))) {
2578 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2580 gst_base_src_stop (src);
2584 return src->random_access;
2589 GST_DEBUG_OBJECT (src, "failed to start");
2595 gst_base_src_loop (GstPad * pad)
2598 GstBuffer *buf = NULL;
2603 GList *pending_events = NULL, *tmp;
2607 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2609 /* Just leave immediately if we're flushing */
2610 GST_LIVE_LOCK (src);
2611 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2613 GST_LIVE_UNLOCK (src);
2615 gst_base_src_send_stream_start (src);
2617 /* The stream-start event could've caused something to flush us */
2618 GST_LIVE_LOCK (src);
2619 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2621 GST_LIVE_UNLOCK (src);
2623 /* check if we need to renegotiate */
2624 if (gst_pad_check_reconfigure (pad)) {
2625 if (!gst_base_src_negotiate (src)) {
2626 gst_pad_mark_reconfigure (pad);
2627 if (GST_PAD_IS_FLUSHING (pad))
2630 goto negotiate_failed;
2634 GST_LIVE_LOCK (src);
2636 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2639 blocksize = src->blocksize;
2641 /* if we operate in bytes, we can calculate an offset */
2642 if (src->segment.format == GST_FORMAT_BYTES) {
2643 position = src->segment.position;
2644 /* for negative rates, start with subtracting the blocksize */
2645 if (src->segment.rate < 0.0) {
2646 /* we cannot go below segment.start */
2647 if (position > src->segment.start + blocksize)
2648 position -= blocksize;
2650 /* last block, remainder up to segment.start */
2651 blocksize = position - src->segment.start;
2652 position = src->segment.start;
2658 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2659 GST_TIME_ARGS (position), blocksize);
2661 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2662 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2663 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2664 gst_flow_get_name (ret));
2665 GST_LIVE_UNLOCK (src);
2668 /* this should not happen */
2669 if (G_UNLIKELY (buf == NULL))
2672 /* push events to close/start our segment before we push the buffer. */
2673 if (G_UNLIKELY (src->priv->segment_pending)) {
2674 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2675 src->priv->segment_pending = FALSE;
2678 if (g_atomic_int_get (&src->priv->have_events)) {
2679 GST_OBJECT_LOCK (src);
2680 /* take the events */
2681 pending_events = src->priv->pending_events;
2682 src->priv->pending_events = NULL;
2683 g_atomic_int_set (&src->priv->have_events, FALSE);
2684 GST_OBJECT_UNLOCK (src);
2687 /* Push out pending events if any */
2688 if (G_UNLIKELY (pending_events != NULL)) {
2689 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2690 GstEvent *ev = (GstEvent *) tmp->data;
2691 gst_pad_push_event (pad, ev);
2693 g_list_free (pending_events);
2696 /* figure out the new position */
2697 switch (src->segment.format) {
2698 case GST_FORMAT_BYTES:
2700 guint bufsize = gst_buffer_get_size (buf);
2702 /* we subtracted above for negative rates */
2703 if (src->segment.rate >= 0.0)
2704 position += bufsize;
2707 case GST_FORMAT_TIME:
2709 GstClockTime start, duration;
2711 start = GST_BUFFER_TIMESTAMP (buf);
2712 duration = GST_BUFFER_DURATION (buf);
2714 if (GST_CLOCK_TIME_IS_VALID (start))
2717 position = src->segment.position;
2719 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2720 if (src->segment.rate >= 0.0)
2721 position += duration;
2722 else if (position > duration)
2723 position -= duration;
2729 case GST_FORMAT_DEFAULT:
2730 if (src->segment.rate >= 0.0)
2731 position = GST_BUFFER_OFFSET_END (buf);
2733 position = GST_BUFFER_OFFSET (buf);
2739 if (position != -1) {
2740 if (src->segment.rate >= 0.0) {
2741 /* positive rate, check if we reached the stop */
2742 if (src->segment.stop != -1) {
2743 if (position >= src->segment.stop) {
2745 position = src->segment.stop;
2749 /* negative rate, check if we reached the start. start is always set to
2750 * something different from -1 */
2751 if (position <= src->segment.start) {
2753 position = src->segment.start;
2755 /* when going reverse, all buffers are DISCONT */
2756 src->priv->discont = TRUE;
2758 GST_OBJECT_LOCK (src);
2759 src->segment.position = position;
2760 GST_OBJECT_UNLOCK (src);
2763 if (G_UNLIKELY (src->priv->discont)) {
2764 GST_INFO_OBJECT (src, "marking pending DISCONT");
2765 buf = gst_buffer_make_writable (buf);
2766 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2767 src->priv->discont = FALSE;
2769 GST_LIVE_UNLOCK (src);
2771 ret = gst_pad_push (pad, buf);
2772 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2773 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2774 goto not_negotiated;
2776 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2777 gst_flow_get_name (ret));
2781 if (G_UNLIKELY (eos)) {
2782 GST_INFO_OBJECT (src, "pausing after end of segment");
2793 if (gst_pad_needs_reconfigure (pad)) {
2794 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2797 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2798 * a pending negotiation request on our srcpad */
2802 GST_DEBUG_OBJECT (src, "Not negotiated");
2803 ret = GST_FLOW_NOT_NEGOTIATED;
2808 GST_DEBUG_OBJECT (src, "we are flushing");
2809 GST_LIVE_UNLOCK (src);
2810 ret = GST_FLOW_FLUSHING;
2815 const gchar *reason = gst_flow_get_name (ret);
2818 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2819 src->running = FALSE;
2820 gst_pad_pause_task (pad);
2821 if (ret == GST_FLOW_EOS) {
2822 gboolean flag_segment;
2826 /* perform EOS logic */
2827 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2828 format = src->segment.format;
2829 position = src->segment.position;
2832 GstMessage *message;
2834 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2836 gst_message_set_seqnum (message, src->priv->seqnum);
2837 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2838 event = gst_event_new_segment_done (format, position);
2839 gst_event_set_seqnum (event, src->priv->seqnum);
2840 gst_pad_push_event (pad, event);
2842 event = gst_event_new_eos ();
2843 gst_event_set_seqnum (event, src->priv->seqnum);
2844 gst_pad_push_event (pad, event);
2846 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2847 event = gst_event_new_eos ();
2848 gst_event_set_seqnum (event, src->priv->seqnum);
2849 /* for fatal errors we post an error message, post the error
2850 * first so the app knows about the error first.
2851 * Also don't do this for FLUSHING because it happens
2852 * due to flushing and posting an error message because of
2853 * that is the wrong thing to do, e.g. when we're doing
2854 * a flushing seek. */
2855 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2856 (_("Internal data flow error.")),
2857 ("streaming task paused, reason %s (%d)", reason, ret));
2858 gst_pad_push_event (pad, event);
2864 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2865 (_("Internal data flow error.")), ("element returned NULL buffer"));
2866 GST_LIVE_UNLOCK (src);
2872 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2873 GstAllocator * allocator, GstAllocationParams * params)
2875 GstAllocator *oldalloc;
2876 GstBufferPool *oldpool;
2877 GstBaseSrcPrivate *priv = basesrc->priv;
2880 GST_DEBUG_OBJECT (basesrc, "activate pool");
2881 if (!gst_buffer_pool_set_active (pool, TRUE))
2882 goto activate_failed;
2885 GST_OBJECT_LOCK (basesrc);
2886 oldpool = priv->pool;
2889 oldalloc = priv->allocator;
2890 priv->allocator = allocator;
2893 priv->params = *params;
2895 gst_allocation_params_init (&priv->params);
2896 GST_OBJECT_UNLOCK (basesrc);
2899 /* only deactivate if the pool is not the one we're using */
2900 if (oldpool != pool) {
2901 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2902 gst_buffer_pool_set_active (oldpool, FALSE);
2904 gst_object_unref (oldpool);
2907 gst_object_unref (oldalloc);
2914 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2920 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2922 GstBaseSrcPrivate *priv = basesrc->priv;
2923 GstBufferPool *pool;
2924 gboolean res = TRUE;
2926 GST_OBJECT_LOCK (basesrc);
2927 if ((pool = priv->pool))
2928 pool = gst_object_ref (pool);
2929 GST_OBJECT_UNLOCK (basesrc);
2932 res = gst_buffer_pool_set_active (pool, active);
2933 gst_object_unref (pool);
2940 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
2943 GstBufferPool *pool;
2944 guint size, min, max;
2945 GstAllocator *allocator;
2946 GstAllocationParams params;
2947 GstStructure *config;
2948 gboolean update_allocator;
2950 gst_query_parse_allocation (query, &outcaps, NULL);
2952 /* we got configuration from our peer or the decide_allocation method,
2954 if (gst_query_get_n_allocation_params (query) > 0) {
2955 /* try the allocator */
2956 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2957 update_allocator = TRUE;
2960 gst_allocation_params_init (¶ms);
2961 update_allocator = FALSE;
2964 if (gst_query_get_n_allocation_pools (query) > 0) {
2965 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2968 /* no pool, we can make our own */
2969 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
2970 pool = gst_buffer_pool_new ();
2974 size = min = max = 0;
2979 config = gst_buffer_pool_get_config (pool);
2980 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
2981 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2982 gst_buffer_pool_set_config (pool, config);
2985 if (update_allocator)
2986 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
2988 gst_query_add_allocation_param (query, allocator, ¶ms);
2990 gst_object_unref (allocator);
2993 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2994 gst_object_unref (pool);
3001 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3003 GstBaseSrcClass *bclass;
3004 gboolean result = TRUE;
3006 GstBufferPool *pool = NULL;
3007 GstAllocator *allocator = NULL;
3008 GstAllocationParams params;
3010 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3012 /* make query and let peer pad answer, we don't really care if it worked or
3013 * not, if it failed, the allocation query would contain defaults and the
3014 * subclass would then set better values if needed */
3015 query = gst_query_new_allocation (caps, TRUE);
3016 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3017 /* not a problem, just debug a little */
3018 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3021 g_assert (bclass->decide_allocation != NULL);
3022 result = bclass->decide_allocation (basesrc, query);
3024 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3028 goto no_decide_allocation;
3030 /* we got configuration from our peer or the decide_allocation method,
3032 if (gst_query_get_n_allocation_params (query) > 0) {
3033 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3036 gst_allocation_params_init (¶ms);
3039 if (gst_query_get_n_allocation_pools (query) > 0)
3040 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3042 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
3044 gst_query_unref (query);
3049 no_decide_allocation:
3051 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3052 gst_query_unref (query);
3058 /* default negotiation code.
3060 * Take intersection between src and sink pads, take first
3064 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3067 GstCaps *caps = NULL;
3068 GstCaps *peercaps = NULL;
3069 gboolean result = FALSE;
3071 /* first see what is possible on our source pad */
3072 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3073 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3074 /* nothing or anything is allowed, we're done */
3075 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3076 goto no_nego_needed;
3078 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3081 /* get the peer caps */
3082 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3083 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3085 /* The result is already a subset of our caps */
3087 gst_caps_unref (thiscaps);
3089 /* no peer, work with our own caps then */
3092 if (caps && !gst_caps_is_empty (caps)) {
3094 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3095 if (gst_caps_is_any (caps)) {
3096 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3097 /* hmm, still anything, so element can do anything and
3098 * nego is not needed */
3101 caps = gst_base_src_fixate (basesrc, caps);
3102 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3103 if (gst_caps_is_fixed (caps)) {
3104 /* yay, fixed caps, use those then, it's possible that the subclass does
3105 * not accept this caps after all and we have to fail. */
3106 result = gst_base_src_set_caps (basesrc, caps);
3109 gst_caps_unref (caps);
3112 gst_caps_unref (caps);
3113 GST_DEBUG_OBJECT (basesrc, "no common caps");
3119 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3121 gst_caps_unref (thiscaps);
3126 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3127 ("No supported formats found"),
3128 ("This element did not produce valid caps"));
3130 gst_caps_unref (thiscaps);
3136 gst_base_src_negotiate (GstBaseSrc * basesrc)
3138 GstBaseSrcClass *bclass;
3141 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3143 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3145 if (G_LIKELY (bclass->negotiate))
3146 result = bclass->negotiate (basesrc);
3150 if (G_LIKELY (result)) {
3153 caps = gst_pad_get_current_caps (basesrc->srcpad);
3155 result = gst_base_src_prepare_allocation (basesrc, caps);
3158 gst_caps_unref (caps);
3164 gst_base_src_start (GstBaseSrc * basesrc)
3166 GstBaseSrcClass *bclass;
3169 GST_LIVE_LOCK (basesrc);
3171 GST_OBJECT_LOCK (basesrc);
3172 if (GST_BASE_SRC_IS_STARTING (basesrc))
3174 if (GST_BASE_SRC_IS_STARTED (basesrc))
3177 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3178 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3179 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3180 GST_OBJECT_UNLOCK (basesrc);
3182 basesrc->num_buffers_left = basesrc->num_buffers;
3183 basesrc->running = FALSE;
3184 basesrc->priv->segment_pending = FALSE;
3185 GST_LIVE_UNLOCK (basesrc);
3187 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3189 result = bclass->start (basesrc);
3194 goto could_not_start;
3196 if (!gst_base_src_is_async (basesrc)) {
3197 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3198 /* not really waiting here, we call this to get the result
3199 * from the start_complete call */
3200 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3208 GST_DEBUG_OBJECT (basesrc, "was starting");
3209 GST_OBJECT_UNLOCK (basesrc);
3210 GST_LIVE_UNLOCK (basesrc);
3215 GST_DEBUG_OBJECT (basesrc, "was started");
3216 GST_OBJECT_UNLOCK (basesrc);
3217 GST_LIVE_UNLOCK (basesrc);
3222 GST_DEBUG_OBJECT (basesrc, "could not start");
3223 /* subclass is supposed to post a message. We don't have to call _stop. */
3224 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3230 * gst_base_src_start_complete:
3231 * @basesrc: base source instance
3232 * @ret: a #GstFlowReturn
3234 * Complete an asynchronous start operation. When the subclass overrides the
3235 * start method, it should call gst_base_src_start_complete() when the start
3236 * operation completes either from the same thread or from an asynchronous
3240 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3249 if (ret != GST_FLOW_OK)
3252 GST_DEBUG_OBJECT (basesrc, "starting source");
3253 format = basesrc->segment.format;
3255 /* figure out the size */
3258 if (format == GST_FORMAT_BYTES) {
3259 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3261 if (bclass->get_size) {
3262 if (!(have_size = bclass->get_size (basesrc, &size)))
3265 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3266 /* only update the size when operating in bytes, subclass is supposed
3267 * to set duration in the start method for other formats */
3268 GST_OBJECT_LOCK (basesrc);
3269 basesrc->segment.duration = size;
3270 GST_OBJECT_UNLOCK (basesrc);
3273 GST_DEBUG_OBJECT (basesrc,
3274 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3275 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3276 basesrc->segment.duration);
3278 seekable = gst_base_src_seekable (basesrc);
3279 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3281 /* update for random access flag */
3282 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3284 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3286 /* stop flushing now but for live sources, still block in the LIVE lock when
3287 * we are not yet PLAYING */
3288 gst_base_src_set_flushing (basesrc, FALSE, FALSE, NULL);
3290 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3292 GST_OBJECT_LOCK (basesrc->srcpad);
3293 mode = GST_PAD_MODE (basesrc->srcpad);
3294 GST_OBJECT_UNLOCK (basesrc->srcpad);
3296 /* take the stream lock here, we only want to let the task run when we have
3297 * set the STARTED flag */
3298 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3299 if (mode == GST_PAD_MODE_PUSH) {
3300 /* do initial seek, which will start the task */
3301 GST_OBJECT_LOCK (basesrc);
3302 event = basesrc->pending_seek;
3303 basesrc->pending_seek = NULL;
3304 GST_OBJECT_UNLOCK (basesrc);
3306 /* The perform seek code will start the task when finished. We don't have to
3307 * unlock the streaming thread because it is not running yet */
3308 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3312 gst_event_unref (event);
3314 /* if not random_access, we cannot operate in pull mode for now */
3315 if (G_UNLIKELY (!basesrc->random_access))
3319 GST_OBJECT_LOCK (basesrc);
3320 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3321 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3322 basesrc->priv->start_result = ret;
3323 GST_ASYNC_SIGNAL (basesrc);
3324 GST_OBJECT_UNLOCK (basesrc);
3326 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3332 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3333 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3334 gst_base_src_stop (basesrc);
3336 gst_event_unref (event);
3337 ret = GST_FLOW_ERROR;
3342 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3343 gst_base_src_stop (basesrc);
3344 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3345 ret = GST_FLOW_ERROR;
3350 GST_OBJECT_LOCK (basesrc);
3351 basesrc->priv->start_result = ret;
3352 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3353 GST_ASYNC_SIGNAL (basesrc);
3354 GST_OBJECT_UNLOCK (basesrc);
3360 * gst_base_src_start_wait:
3361 * @basesrc: base source instance
3363 * Wait until the start operation completes.
3365 * Returns: a #GstFlowReturn.
3368 gst_base_src_start_wait (GstBaseSrc * basesrc)
3370 GstFlowReturn result;
3372 GST_OBJECT_LOCK (basesrc);
3373 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3374 GST_ASYNC_WAIT (basesrc);
3376 result = basesrc->priv->start_result;
3377 GST_OBJECT_UNLOCK (basesrc);
3379 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3385 gst_base_src_stop (GstBaseSrc * basesrc)
3387 GstBaseSrcClass *bclass;
3388 gboolean result = TRUE;
3390 GST_DEBUG_OBJECT (basesrc, "stopping source");
3393 gst_base_src_set_flushing (basesrc, TRUE, FALSE, NULL);
3395 gst_pad_stop_task (basesrc->srcpad);
3397 GST_OBJECT_LOCK (basesrc);
3398 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3401 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3402 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3403 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3404 GST_ASYNC_SIGNAL (basesrc);
3405 GST_OBJECT_UNLOCK (basesrc);
3407 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3409 result = bclass->stop (basesrc);
3411 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3417 GST_DEBUG_OBJECT (basesrc, "was stopped");
3418 GST_OBJECT_UNLOCK (basesrc);
3423 /* start or stop flushing dataprocessing
3426 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3427 gboolean flushing, gboolean live_play, gboolean * playing)
3429 GstBaseSrcClass *bclass;
3431 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3433 GST_DEBUG_OBJECT (basesrc, "flushing %d, live_play %d", flushing, live_play);
3436 gst_base_src_activate_pool (basesrc, FALSE);
3437 /* unlock any subclasses, we need to do this before grabbing the
3438 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3439 * unlock to the params because of backwards compat (see seek handler)*/
3441 bclass->unlock (basesrc);
3444 /* the live lock is released when we are blocked, waiting for playing or
3445 * when we sync to the clock. */
3446 GST_LIVE_LOCK (basesrc);
3448 *playing = basesrc->live_running;
3449 basesrc->priv->flushing = flushing;
3451 /* if we are locked in the live lock, signal it to make it flush */
3452 basesrc->live_running = TRUE;
3454 /* clear pending EOS if any */
3455 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3457 /* step 1, now that we have the LIVE lock, clear our unlock request */
3458 if (bclass->unlock_stop)
3459 bclass->unlock_stop (basesrc);
3461 /* step 2, unblock clock sync (if any) or any other blocking thing */
3462 if (basesrc->clock_id)
3463 gst_clock_id_unschedule (basesrc->clock_id);
3465 /* signal the live source that it can start playing */
3466 basesrc->live_running = live_play;
3468 gst_base_src_activate_pool (basesrc, TRUE);
3470 /* Drop all delayed events */
3471 GST_OBJECT_LOCK (basesrc);
3472 if (basesrc->priv->pending_events) {
3473 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3475 g_list_free (basesrc->priv->pending_events);
3476 basesrc->priv->pending_events = NULL;
3477 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3479 GST_OBJECT_UNLOCK (basesrc);
3481 GST_LIVE_SIGNAL (basesrc);
3482 GST_LIVE_UNLOCK (basesrc);
3487 /* the purpose of this function is to make sure that a live source blocks in the
3488 * LIVE lock or leaves the LIVE lock and continues playing. */
3490 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3492 GstBaseSrcClass *bclass;
3494 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3496 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3498 GST_DEBUG_OBJECT (basesrc, "unlock");
3500 bclass->unlock (basesrc);
3503 /* we are now able to grab the LIVE lock, when we get it, we can be
3504 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3506 GST_LIVE_LOCK (basesrc);
3507 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3509 /* unblock clock sync (if any) */
3510 if (basesrc->clock_id)
3511 gst_clock_id_unschedule (basesrc->clock_id);
3513 /* configure what to do when we get to the LIVE lock. */
3514 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3515 basesrc->live_running = live_play;
3520 /* clear our unlock request when going to PLAYING */
3521 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3522 if (bclass->unlock_stop)
3523 bclass->unlock_stop (basesrc);
3525 /* for live sources we restart the timestamp correction */
3526 basesrc->priv->latency = -1;
3527 /* have to restart the task in case it stopped because of the unlock when
3528 * we went to PAUSED. Only do this if we operating in push mode. */
3529 GST_OBJECT_LOCK (basesrc->srcpad);
3530 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3531 GST_OBJECT_UNLOCK (basesrc->srcpad);
3533 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3534 basesrc->srcpad, NULL);
3535 GST_DEBUG_OBJECT (basesrc, "signal");
3536 GST_LIVE_SIGNAL (basesrc);
3538 GST_LIVE_UNLOCK (basesrc);
3544 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3546 GstBaseSrc *basesrc;
3548 basesrc = GST_BASE_SRC (parent);
3550 /* prepare subclass first */
3552 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3554 if (G_UNLIKELY (!basesrc->can_activate_push))
3555 goto no_push_activation;
3557 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3560 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3561 /* now we can stop the source */
3562 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3570 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3575 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3580 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3586 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3588 GstBaseSrc *basesrc;
3590 basesrc = GST_BASE_SRC (parent);
3592 /* prepare subclass first */
3594 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3595 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3598 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3599 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3607 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3612 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3618 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3619 GstPadMode mode, gboolean active)
3622 GstBaseSrc *src = GST_BASE_SRC (parent);
3624 src->priv->stream_start_pending = FALSE;
3627 case GST_PAD_MODE_PULL:
3628 res = gst_base_src_activate_pull (pad, parent, active);
3630 case GST_PAD_MODE_PUSH:
3631 src->priv->stream_start_pending = active;
3632 res = gst_base_src_activate_push (pad, parent, active);
3635 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3643 static GstStateChangeReturn
3644 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3646 GstBaseSrc *basesrc;
3647 GstStateChangeReturn result;
3648 gboolean no_preroll = FALSE;
3650 basesrc = GST_BASE_SRC (element);
3652 switch (transition) {
3653 case GST_STATE_CHANGE_NULL_TO_READY:
3655 case GST_STATE_CHANGE_READY_TO_PAUSED:
3656 no_preroll = gst_base_src_is_live (basesrc);
3658 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3659 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3660 if (gst_base_src_is_live (basesrc)) {
3661 /* now we can start playback */
3662 gst_base_src_set_playing (basesrc, TRUE);
3670 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3671 transition)) == GST_STATE_CHANGE_FAILURE)
3674 switch (transition) {
3675 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3676 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3677 if (gst_base_src_is_live (basesrc)) {
3678 /* make sure we block in the live lock in PAUSED */
3679 gst_base_src_set_playing (basesrc, FALSE);
3683 case GST_STATE_CHANGE_PAUSED_TO_READY:
3685 /* we don't need to unblock anything here, the pad deactivation code
3686 * already did this */
3687 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3688 gst_event_replace (&basesrc->pending_seek, NULL);
3691 case GST_STATE_CHANGE_READY_TO_NULL:
3697 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3698 result = GST_STATE_CHANGE_NO_PREROLL;
3705 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3711 * gst_base_src_get_buffer_pool:
3712 * @src: a #GstBaseSrc
3714 * Returns: (transfer full): the instance of the #GstBufferPool used
3715 * by the src; free it after use it
3718 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3720 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3722 if (src->priv->pool)
3723 return gst_object_ref (src->priv->pool);
3729 * gst_base_src_get_allocator:
3730 * @src: a #GstBaseSrc
3731 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3733 * @params: (out) (allow-none) (transfer full): the
3734 * #GstAllocatorParams of @allocator
3736 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3737 * used by the base class and its @params.
3739 * Unref the @allocator after use it.
3742 gst_base_src_get_allocator (GstBaseSrc * src,
3743 GstAllocator ** allocator, GstAllocationParams * params)
3745 g_return_if_fail (GST_IS_BASE_SRC (src));
3748 *allocator = src->priv->allocator ?
3749 gst_object_ref (src->priv->allocator) : NULL;
3752 *params = src->priv->params;