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) {
845 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
847 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
849 gst_pad_push_event (src->srcpad,
850 gst_event_new_stream_start (stream_id));
851 src->priv->stream_start_pending = FALSE;
859 * gst_base_src_set_caps:
860 * @src: a #GstBaseSrc
863 * Set new caps on the basesrc source pad.
865 * Returns: %TRUE if the caps could be set
868 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
870 GstBaseSrcClass *bclass;
873 bclass = GST_BASE_SRC_GET_CLASS (src);
875 gst_base_src_send_stream_start (src);
877 if (bclass->set_caps)
878 res = bclass->set_caps (src, caps);
881 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
887 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
889 GstCaps *caps = NULL;
890 GstPadTemplate *pad_template;
891 GstBaseSrcClass *bclass;
893 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
896 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
898 if (pad_template != NULL) {
899 caps = gst_pad_template_get_caps (pad_template);
902 GstCaps *intersection;
905 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
906 gst_caps_unref (caps);
914 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
916 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
917 return gst_caps_fixate (caps);
921 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
923 GstBaseSrcClass *bclass;
925 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
928 caps = bclass->fixate (bsrc, caps);
934 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
938 switch (GST_QUERY_TYPE (query)) {
939 case GST_QUERY_POSITION:
943 gst_query_parse_position (query, &format, NULL);
945 GST_DEBUG_OBJECT (src, "position query in format %s",
946 gst_format_get_name (format));
949 case GST_FORMAT_PERCENT:
955 GST_OBJECT_LOCK (src);
956 position = src->segment.position;
957 duration = src->segment.duration;
958 GST_OBJECT_UNLOCK (src);
960 if (position != -1 && duration != -1) {
961 if (position < duration)
962 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
965 percent = GST_FORMAT_PERCENT_MAX;
969 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
976 GstFormat seg_format;
978 GST_OBJECT_LOCK (src);
980 gst_segment_to_stream_time (&src->segment, src->segment.format,
981 src->segment.position);
982 seg_format = src->segment.format;
983 GST_OBJECT_UNLOCK (src);
985 if (position != -1) {
986 /* convert to requested format */
988 gst_pad_query_convert (src->srcpad, seg_format,
989 position, format, &position);
993 gst_query_set_position (query, format, position);
999 case GST_QUERY_DURATION:
1003 gst_query_parse_duration (query, &format, NULL);
1005 GST_DEBUG_OBJECT (src, "duration query in format %s",
1006 gst_format_get_name (format));
1009 case GST_FORMAT_PERCENT:
1010 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1011 GST_FORMAT_PERCENT_MAX);
1017 GstFormat seg_format;
1020 /* may have to refresh duration */
1021 gst_base_src_update_length (src, 0, &length,
1022 g_atomic_int_get (&src->priv->dynamic_size));
1024 /* this is the duration as configured by the subclass. */
1025 GST_OBJECT_LOCK (src);
1026 duration = src->segment.duration;
1027 seg_format = src->segment.format;
1028 GST_OBJECT_UNLOCK (src);
1030 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1031 duration, gst_format_get_name (seg_format));
1033 if (duration != -1) {
1034 /* convert to requested format, if this fails, we have a duration
1035 * but we cannot answer the query, we must return FALSE. */
1037 gst_pad_query_convert (src->srcpad, seg_format,
1038 duration, format, &duration);
1040 /* The subclass did not configure a duration, we assume that the
1041 * media has an unknown duration then and we return TRUE to report
1042 * this. Note that this is not the same as returning FALSE, which
1043 * means that we cannot report the duration at all. */
1046 gst_query_set_duration (query, format, duration);
1053 case GST_QUERY_SEEKING:
1055 GstFormat format, seg_format;
1058 GST_OBJECT_LOCK (src);
1059 duration = src->segment.duration;
1060 seg_format = src->segment.format;
1061 GST_OBJECT_UNLOCK (src);
1063 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1064 if (format == seg_format) {
1065 gst_query_set_seeking (query, seg_format,
1066 gst_base_src_seekable (src), 0, duration);
1069 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1070 /* Don't reply to the query to make up for demuxers which don't
1071 * handle the SEEKING query yet. Players like Totem will fall back
1072 * to the duration when the SEEKING query isn't answered. */
1077 case GST_QUERY_SEGMENT:
1081 GST_OBJECT_LOCK (src);
1082 /* no end segment configured, current duration then */
1083 if ((stop = src->segment.stop) == -1)
1084 stop = src->segment.duration;
1085 start = src->segment.start;
1087 /* adjust to stream time */
1088 if (src->segment.time != -1) {
1089 start -= src->segment.time;
1091 stop -= src->segment.time;
1094 gst_query_set_segment (query, src->segment.rate, src->segment.format,
1096 GST_OBJECT_UNLOCK (src);
1101 case GST_QUERY_FORMATS:
1103 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1104 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1108 case GST_QUERY_CONVERT:
1110 GstFormat src_fmt, dest_fmt;
1111 gint64 src_val, dest_val;
1113 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1115 /* we can only convert between equal formats... */
1116 if (src_fmt == dest_fmt) {
1122 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1125 case GST_QUERY_LATENCY:
1127 GstClockTime min, max;
1130 /* Subclasses should override and implement something useful */
1131 res = gst_base_src_query_latency (src, &live, &min, &max);
1133 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1134 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1135 GST_TIME_ARGS (max));
1137 gst_query_set_latency (query, live, min, max);
1140 case GST_QUERY_JITTER:
1141 case GST_QUERY_RATE:
1144 case GST_QUERY_BUFFERING:
1146 GstFormat format, seg_format;
1147 gint64 start, stop, estimated;
1149 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1151 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1152 gst_format_get_name (format));
1154 GST_OBJECT_LOCK (src);
1155 if (src->random_access) {
1158 if (format == GST_FORMAT_PERCENT)
1159 stop = GST_FORMAT_PERCENT_MAX;
1161 stop = src->segment.duration;
1167 seg_format = src->segment.format;
1168 GST_OBJECT_UNLOCK (src);
1170 /* convert to required format. When the conversion fails, we can't answer
1171 * the query. When the value is unknown, we can don't perform conversion
1172 * but report TRUE. */
1173 if (format != GST_FORMAT_PERCENT && stop != -1) {
1174 res = gst_pad_query_convert (src->srcpad, seg_format,
1175 stop, format, &stop);
1179 if (res && format != GST_FORMAT_PERCENT && start != -1)
1180 res = gst_pad_query_convert (src->srcpad, seg_format,
1181 start, format, &start);
1183 gst_query_set_buffering_range (query, format, start, stop, estimated);
1186 case GST_QUERY_SCHEDULING:
1188 gboolean random_access;
1190 random_access = gst_base_src_is_random_access (src);
1192 /* we can operate in getrange mode if the native format is bytes
1193 * and we are seekable, this condition is set in the random_access
1194 * flag and is set in the _start() method. */
1195 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1197 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1198 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1203 case GST_QUERY_CAPS:
1205 GstBaseSrcClass *bclass;
1206 GstCaps *caps, *filter;
1208 bclass = GST_BASE_SRC_GET_CLASS (src);
1209 if (bclass->get_caps) {
1210 gst_query_parse_caps (query, &filter);
1211 if ((caps = bclass->get_caps (src, filter))) {
1212 gst_query_set_caps_result (query, caps);
1213 gst_caps_unref (caps);
1222 case GST_QUERY_URI:{
1223 if (GST_IS_URI_HANDLER (src)) {
1224 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1227 gst_query_set_uri (query, uri);
1242 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1249 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1252 GstBaseSrcClass *bclass;
1253 gboolean result = FALSE;
1255 src = GST_BASE_SRC (parent);
1256 bclass = GST_BASE_SRC_GET_CLASS (src);
1259 result = bclass->query (src, query);
1265 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1267 gboolean res = TRUE;
1269 /* update our offset if the start/stop position was updated */
1270 if (segment->format == GST_FORMAT_BYTES) {
1271 segment->time = segment->start;
1272 } else if (segment->start == 0) {
1273 /* seek to start, we can implement a default for this. */
1277 GST_INFO_OBJECT (src, "Can't do a default seek");
1284 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1286 GstBaseSrcClass *bclass;
1287 gboolean result = FALSE;
1289 bclass = GST_BASE_SRC_GET_CLASS (src);
1291 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1293 if (bclass->do_seek)
1294 result = bclass->do_seek (src, segment);
1299 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1302 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1303 GstSegment * segment)
1305 /* By default, we try one of 2 things:
1306 * - For absolute seek positions, convert the requested position to our
1307 * configured processing format and place it in the output segment \
1308 * - For relative seek positions, convert our current (input) values to the
1309 * seek format, adjust by the relative seek offset and then convert back to
1310 * the processing format
1312 GstSeekType start_type, stop_type;
1315 GstFormat seek_format, dest_format;
1318 gboolean res = TRUE;
1320 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1321 &start_type, &start, &stop_type, &stop);
1322 dest_format = segment->format;
1324 if (seek_format == dest_format) {
1325 gst_segment_do_seek (segment, rate, seek_format, flags,
1326 start_type, start, stop_type, stop, &update);
1330 if (start_type != GST_SEEK_TYPE_NONE) {
1331 /* FIXME: Handle seek_end by converting the input segment vals */
1333 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1335 start_type = GST_SEEK_TYPE_SET;
1338 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1339 /* FIXME: Handle seek_end by converting the input segment vals */
1341 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1343 stop_type = GST_SEEK_TYPE_SET;
1346 /* And finally, configure our output segment in the desired format */
1347 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1348 stop_type, stop, &update);
1357 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1363 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1364 GstSegment * seeksegment)
1366 GstBaseSrcClass *bclass;
1367 gboolean result = FALSE;
1369 bclass = GST_BASE_SRC_GET_CLASS (src);
1371 if (bclass->prepare_seek_segment)
1372 result = bclass->prepare_seek_segment (src, event, seeksegment);
1377 static GstFlowReturn
1378 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1379 guint size, GstBuffer ** buffer)
1382 GstBaseSrcPrivate *priv = src->priv;
1385 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1386 } else if (size != -1) {
1387 *buffer = gst_buffer_new_allocate (priv->allocator, size, &priv->params);
1388 if (G_UNLIKELY (*buffer == NULL))
1393 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1402 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1403 return GST_FLOW_ERROR;
1407 static GstFlowReturn
1408 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1409 guint size, GstBuffer ** buffer)
1411 GstBaseSrcClass *bclass;
1415 bclass = GST_BASE_SRC_GET_CLASS (src);
1417 if (G_UNLIKELY (!bclass->alloc))
1419 if (G_UNLIKELY (!bclass->fill))
1422 if (*buffer == NULL) {
1423 /* downstream did not provide us with a buffer to fill, allocate one
1425 ret = bclass->alloc (src, offset, size, &res_buf);
1426 if (G_UNLIKELY (ret != GST_FLOW_OK))
1432 if (G_LIKELY (size > 0)) {
1433 /* only call fill when there is a size */
1434 ret = bclass->fill (src, offset, size, res_buf);
1435 if (G_UNLIKELY (ret != GST_FLOW_OK))
1446 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1447 return GST_FLOW_NOT_SUPPORTED;
1451 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1456 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1457 gst_flow_get_name (ret));
1458 if (*buffer == NULL)
1459 gst_buffer_unref (res_buf);
1464 /* this code implements the seeking. It is a good example
1465 * handling all cases.
1467 * A seek updates the currently configured segment.start
1468 * and segment.stop values based on the SEEK_TYPE. If the
1469 * segment.start value is updated, a seek to this new position
1470 * should be performed.
1472 * The seek can only be executed when we are not currently
1473 * streaming any data, to make sure that this is the case, we
1474 * acquire the STREAM_LOCK which is taken when we are in the
1475 * _loop() function or when a getrange() is called. Normally
1476 * we will not receive a seek if we are operating in pull mode
1477 * though. When we operate as a live source we might block on the live
1478 * cond, which does not release the STREAM_LOCK. Therefore we will try
1479 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1480 * safe to perform the seek.
1482 * When we are in the loop() function, we might be in the middle
1483 * of pushing a buffer, which might block in a sink. To make sure
1484 * that the push gets unblocked we push out a FLUSH_START event.
1485 * Our loop function will get a FLUSHING return value from
1486 * the push and will pause, effectively releasing the STREAM_LOCK.
1488 * For a non-flushing seek, we pause the task, which might eventually
1489 * release the STREAM_LOCK. We say eventually because when the sink
1490 * blocks on the sample we might wait a very long time until the sink
1491 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1492 * can continue the seek. A non-flushing seek is normally done in a
1493 * running pipeline to perform seamless playback, this means that the sink is
1494 * PLAYING and will return from its chain function.
1495 * In the case of a non-flushing seek we need to make sure that the
1496 * data we output after the seek is continuous with the previous data,
1497 * this is because a non-flushing seek does not reset the running-time
1498 * to 0. We do this by closing the currently running segment, ie. sending
1499 * a new_segment event with the stop position set to the last processed
1502 * After updating the segment.start/stop values, we prepare for
1503 * streaming again. We push out a FLUSH_STOP to make the peer pad
1504 * accept data again and we start our task again.
1506 * A segment seek posts a message on the bus saying that the playback
1507 * of the segment started. We store the segment flag internally because
1508 * when we reach the segment.stop we have to post a segment.done
1509 * instead of EOS when doing a segment seek.
1512 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1514 gboolean res = TRUE, tres;
1516 GstFormat seek_format, dest_format;
1518 GstSeekType start_type, stop_type;
1520 gboolean flush, playing;
1522 gboolean relative_seek = FALSE;
1523 gboolean seekseg_configured = FALSE;
1524 GstSegment seeksegment;
1528 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1530 GST_OBJECT_LOCK (src);
1531 dest_format = src->segment.format;
1532 GST_OBJECT_UNLOCK (src);
1535 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1536 &start_type, &start, &stop_type, &stop);
1538 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1539 SEEK_TYPE_IS_RELATIVE (stop_type);
1541 if (dest_format != seek_format && !relative_seek) {
1542 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1543 * here before taking the stream lock, otherwise we must convert it later,
1544 * once we have the stream lock and can read the last configures segment
1545 * start and stop positions */
1546 gst_segment_init (&seeksegment, dest_format);
1548 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1549 goto prepare_failed;
1551 seekseg_configured = TRUE;
1554 flush = flags & GST_SEEK_FLAG_FLUSH;
1555 seqnum = gst_event_get_seqnum (event);
1558 /* get next seqnum */
1559 seqnum = gst_util_seqnum_next ();
1562 /* send flush start */
1564 tevent = gst_event_new_flush_start ();
1565 gst_event_set_seqnum (tevent, seqnum);
1566 gst_pad_push_event (src->srcpad, tevent);
1568 gst_pad_pause_task (src->srcpad);
1570 /* unblock streaming thread. */
1572 gst_base_src_set_flushing (src, TRUE, FALSE, &playing);
1574 /* grab streaming lock, this should eventually be possible, either
1575 * because the task is paused, our streaming thread stopped
1576 * or because our peer is flushing. */
1577 GST_PAD_STREAM_LOCK (src->srcpad);
1578 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1579 /* we have seen this event before, issue a warning for now */
1580 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1583 src->priv->seqnum = seqnum;
1584 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1588 gst_base_src_set_flushing (src, FALSE, playing, NULL);
1590 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1591 * copy the current segment info into the temp segment that we can actually
1592 * attempt the seek with. We only update the real segment if the seek succeeds. */
1593 if (!seekseg_configured) {
1594 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1596 /* now configure the final seek segment */
1598 if (seeksegment.format != seek_format) {
1599 /* OK, here's where we give the subclass a chance to convert the relative
1600 * seek into an absolute one in the processing format. We set up any
1601 * absolute seek above, before taking the stream lock. */
1602 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1603 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1608 /* The seek format matches our processing format, no need to ask the
1609 * the subclass to configure the segment. */
1610 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1611 start_type, start, stop_type, stop, &update);
1614 /* Else, no seek event passed, so we're just (re)starting the
1619 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1620 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1621 seeksegment.start, seeksegment.stop, seeksegment.position);
1623 /* do the seek, segment.position contains the new position. */
1624 res = gst_base_src_do_seek (src, &seeksegment);
1627 /* and prepare to continue streaming */
1629 tevent = gst_event_new_flush_stop (TRUE);
1630 gst_event_set_seqnum (tevent, seqnum);
1631 /* send flush stop, peer will accept data and events again. We
1632 * are not yet providing data as we still have the STREAM_LOCK. */
1633 gst_pad_push_event (src->srcpad, tevent);
1636 /* The subclass must have converted the segment to the processing format
1638 if (res && seeksegment.format != dest_format) {
1639 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1640 "in the correct format. Aborting seek.");
1644 /* if the seek was successful, we update our real segment and push
1645 * out the new segment. */
1647 GST_OBJECT_LOCK (src);
1648 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1649 GST_OBJECT_UNLOCK (src);
1651 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1652 GstMessage *message;
1654 message = gst_message_new_segment_start (GST_OBJECT (src),
1655 seeksegment.format, seeksegment.position);
1656 gst_message_set_seqnum (message, seqnum);
1658 gst_element_post_message (GST_ELEMENT (src), message);
1661 /* for deriving a stop position for the playback segment from the seek
1662 * segment, we must take the duration when the stop is not set */
1663 /* FIXME: This is never used below */
1664 if ((stop = seeksegment.stop) == -1)
1665 stop = seeksegment.duration;
1667 src->priv->segment_pending = TRUE;
1670 src->priv->discont = TRUE;
1671 src->running = TRUE;
1672 /* and restart the task in case it got paused explicitly or by
1673 * the FLUSH_START event we pushed out. */
1674 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1679 /* and release the lock again so we can continue streaming */
1680 GST_PAD_STREAM_UNLOCK (src->srcpad);
1686 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1691 /* all events send to this element directly. This is mainly done from the
1695 gst_base_src_send_event (GstElement * element, GstEvent * event)
1698 gboolean result = FALSE;
1700 src = GST_BASE_SRC (element);
1702 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1704 switch (GST_EVENT_TYPE (event)) {
1705 /* bidirectional events */
1706 case GST_EVENT_FLUSH_START:
1707 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1708 result = gst_pad_push_event (src->srcpad, event);
1711 case GST_EVENT_FLUSH_STOP:
1712 GST_LIVE_LOCK (src->srcpad);
1713 src->priv->segment_pending = TRUE;
1714 /* sending random flushes downstream can break stuff,
1715 * especially sync since all segment info will get flushed */
1716 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1717 result = gst_pad_push_event (src->srcpad, event);
1718 GST_LIVE_UNLOCK (src->srcpad);
1722 /* downstream serialized events */
1725 GstBaseSrcClass *bclass;
1727 bclass = GST_BASE_SRC_GET_CLASS (src);
1729 /* queue EOS and make sure the task or pull function performs the EOS
1732 * We have two possibilities:
1734 * - Before we are to enter the _create function, we check the pending_eos
1735 * first and do EOS instead of entering it.
1736 * - If we are in the _create function or we did not manage to set the
1737 * flag fast enough and we are about to enter the _create function,
1738 * we unlock it so that we exit with FLUSHING immediately. We then
1739 * check the EOS flag and do the EOS logic.
1741 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1742 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1745 /* unlock the _create function so that we can check the pending_eos flag
1746 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1747 * that we can grab it and stop the unlock again. We don't take the stream
1748 * lock so that this operation is guaranteed to never block. */
1749 gst_base_src_activate_pool (src, FALSE);
1751 bclass->unlock (src);
1753 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1755 GST_LIVE_LOCK (src);
1756 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1757 /* now stop the unlock of the streaming thread again. Grabbing the live
1758 * lock is enough because that protects the create function. */
1759 if (bclass->unlock_stop)
1760 bclass->unlock_stop (src);
1761 gst_base_src_activate_pool (src, TRUE);
1762 GST_LIVE_UNLOCK (src);
1767 case GST_EVENT_SEGMENT:
1768 /* sending random SEGMENT downstream can break sync. */
1771 case GST_EVENT_CUSTOM_DOWNSTREAM:
1772 case GST_EVENT_CUSTOM_BOTH:
1773 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1774 GST_OBJECT_LOCK (src);
1775 src->priv->pending_events =
1776 g_list_append (src->priv->pending_events, event);
1777 g_atomic_int_set (&src->priv->have_events, TRUE);
1778 GST_OBJECT_UNLOCK (src);
1782 case GST_EVENT_BUFFERSIZE:
1783 /* does not seem to make much sense currently */
1786 /* upstream events */
1788 /* elements should override send_event and do something */
1790 case GST_EVENT_SEEK:
1794 GST_OBJECT_LOCK (src->srcpad);
1795 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1797 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1798 GST_OBJECT_UNLOCK (src->srcpad);
1801 GST_DEBUG_OBJECT (src, "performing seek");
1802 /* when we are running in push mode, we can execute the
1803 * seek right now. */
1804 result = gst_base_src_perform_seek (src, event, TRUE);
1808 /* else we store the event and execute the seek when we
1810 GST_OBJECT_LOCK (src);
1811 GST_DEBUG_OBJECT (src, "queueing seek");
1812 event_p = &src->pending_seek;
1813 gst_event_replace ((GstEvent **) event_p, event);
1814 GST_OBJECT_UNLOCK (src);
1815 /* assume the seek will work */
1820 case GST_EVENT_NAVIGATION:
1821 /* could make sense for elements that do something with navigation events
1822 * but then they would need to override the send_event function */
1824 case GST_EVENT_LATENCY:
1825 /* does not seem to make sense currently */
1829 case GST_EVENT_CUSTOM_UPSTREAM:
1830 /* override send_event if you want this */
1832 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1833 case GST_EVENT_CUSTOM_BOTH_OOB:
1834 /* insert a random custom event into the pipeline */
1835 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1836 result = gst_pad_push_event (src->srcpad, event);
1837 /* we gave away the ref to the event in the push */
1844 /* if we still have a ref to the event, unref it now */
1846 gst_event_unref (event);
1853 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1854 GST_OBJECT_UNLOCK (src->srcpad);
1861 gst_base_src_seekable (GstBaseSrc * src)
1863 GstBaseSrcClass *bclass;
1864 bclass = GST_BASE_SRC_GET_CLASS (src);
1865 if (bclass->is_seekable)
1866 return bclass->is_seekable (src);
1872 gst_base_src_update_qos (GstBaseSrc * src,
1873 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1875 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1876 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1877 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1879 GST_OBJECT_LOCK (src);
1880 src->priv->proportion = proportion;
1881 src->priv->earliest_time = timestamp + diff;
1882 GST_OBJECT_UNLOCK (src);
1887 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1891 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1893 switch (GST_EVENT_TYPE (event)) {
1894 case GST_EVENT_SEEK:
1895 /* is normally called when in push mode */
1896 if (!gst_base_src_seekable (src))
1899 result = gst_base_src_perform_seek (src, event, TRUE);
1901 case GST_EVENT_FLUSH_START:
1902 /* cancel any blocking getrange, is normally called
1903 * when in pull mode. */
1904 result = gst_base_src_set_flushing (src, TRUE, FALSE, NULL);
1906 case GST_EVENT_FLUSH_STOP:
1907 result = gst_base_src_set_flushing (src, FALSE, TRUE, NULL);
1912 GstClockTimeDiff diff;
1913 GstClockTime timestamp;
1915 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1916 gst_base_src_update_qos (src, proportion, diff, timestamp);
1920 case GST_EVENT_RECONFIGURE:
1923 case GST_EVENT_LATENCY:
1935 GST_DEBUG_OBJECT (src, "is not seekable");
1941 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1944 GstBaseSrcClass *bclass;
1945 gboolean result = FALSE;
1947 src = GST_BASE_SRC (parent);
1948 bclass = GST_BASE_SRC_GET_CLASS (src);
1950 if (bclass->event) {
1951 if (!(result = bclass->event (src, event)))
1952 goto subclass_failed;
1956 gst_event_unref (event);
1963 GST_DEBUG_OBJECT (src, "subclass refused event");
1969 gst_base_src_set_property (GObject * object, guint prop_id,
1970 const GValue * value, GParamSpec * pspec)
1974 src = GST_BASE_SRC (object);
1977 case PROP_BLOCKSIZE:
1978 gst_base_src_set_blocksize (src, g_value_get_uint (value));
1980 case PROP_NUM_BUFFERS:
1981 src->num_buffers = g_value_get_int (value);
1984 src->typefind = g_value_get_boolean (value);
1986 case PROP_DO_TIMESTAMP:
1987 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1990 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1996 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2001 src = GST_BASE_SRC (object);
2004 case PROP_BLOCKSIZE:
2005 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2007 case PROP_NUM_BUFFERS:
2008 g_value_set_int (value, src->num_buffers);
2011 g_value_set_boolean (value, src->typefind);
2013 case PROP_DO_TIMESTAMP:
2014 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2017 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2022 /* with STREAM_LOCK and LOCK */
2023 static GstClockReturn
2024 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2029 id = gst_clock_new_single_shot_id (clock, time);
2031 basesrc->clock_id = id;
2032 /* release the live lock while waiting */
2033 GST_LIVE_UNLOCK (basesrc);
2035 ret = gst_clock_id_wait (id, NULL);
2037 GST_LIVE_LOCK (basesrc);
2038 gst_clock_id_unref (id);
2039 basesrc->clock_id = NULL;
2044 /* perform synchronisation on a buffer.
2047 static GstClockReturn
2048 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2050 GstClockReturn result;
2051 GstClockTime start, end;
2052 GstBaseSrcClass *bclass;
2053 GstClockTime base_time;
2055 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2056 gboolean do_timestamp, first, pseudo_live, is_live;
2058 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2061 if (bclass->get_times)
2062 bclass->get_times (basesrc, buffer, &start, &end);
2064 /* get buffer timestamp */
2065 dts = GST_BUFFER_DTS (buffer);
2066 pts = GST_BUFFER_PTS (buffer);
2068 if (GST_CLOCK_TIME_IS_VALID (dts))
2073 /* grab the lock to prepare for clocking and calculate the startup
2075 GST_OBJECT_LOCK (basesrc);
2077 is_live = basesrc->is_live;
2078 /* if we are asked to sync against the clock we are a pseudo live element */
2079 pseudo_live = (start != -1 && is_live);
2080 /* check for the first buffer */
2081 first = (basesrc->priv->latency == -1);
2083 if (timestamp != -1 && pseudo_live) {
2084 GstClockTime latency;
2086 /* we have a timestamp and a sync time, latency is the diff */
2087 if (timestamp <= start)
2088 latency = start - timestamp;
2093 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2094 GST_TIME_ARGS (latency));
2095 /* first time we calculate latency, just configure */
2096 basesrc->priv->latency = latency;
2098 if (basesrc->priv->latency != latency) {
2099 /* we have a new latency, FIXME post latency message */
2100 basesrc->priv->latency = latency;
2101 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2102 GST_TIME_ARGS (latency));
2106 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2107 is_live, start != -1);
2108 basesrc->priv->latency = 0;
2111 /* get clock, if no clock, we can't sync or do timestamps */
2112 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2115 gst_object_ref (clock);
2117 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2119 do_timestamp = basesrc->priv->do_timestamp;
2120 GST_OBJECT_UNLOCK (basesrc);
2122 /* first buffer, calculate the timestamp offset */
2124 GstClockTime running_time;
2126 now = gst_clock_get_time (clock);
2127 running_time = now - base_time;
2129 GST_LOG_OBJECT (basesrc,
2130 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2131 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2132 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2134 if (pseudo_live && timestamp != -1) {
2135 /* live source and we need to sync, add startup latency to all timestamps
2136 * to get the real running_time. Live sources should always timestamp
2137 * according to the current running time. */
2138 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2140 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2141 GST_TIME_ARGS (basesrc->priv->ts_offset));
2143 basesrc->priv->ts_offset = 0;
2144 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2147 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2153 GST_BUFFER_DTS (buffer) = dts;
2155 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2156 GST_TIME_ARGS (dts));
2159 /* not the first buffer, the timestamp is the diff between the clock and
2161 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2162 now = gst_clock_get_time (clock);
2164 dts = now - base_time;
2165 GST_BUFFER_DTS (buffer) = dts;
2167 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2168 GST_TIME_ARGS (dts));
2171 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2172 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2175 GST_BUFFER_PTS (buffer) = dts;
2177 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2178 GST_TIME_ARGS (pts));
2181 /* if we don't have a buffer timestamp, we don't sync */
2182 if (!GST_CLOCK_TIME_IS_VALID (start))
2186 /* for pseudo live sources, add our ts_offset to the timestamp */
2187 if (GST_CLOCK_TIME_IS_VALID (pts))
2188 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2189 if (GST_CLOCK_TIME_IS_VALID (dts))
2190 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2191 start += basesrc->priv->ts_offset;
2194 GST_LOG_OBJECT (basesrc,
2195 "waiting for clock, base time %" GST_TIME_FORMAT
2196 ", stream_start %" GST_TIME_FORMAT,
2197 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2199 result = gst_base_src_wait (basesrc, clock, start + base_time);
2201 gst_object_unref (clock);
2203 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2210 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2211 GST_OBJECT_UNLOCK (basesrc);
2212 return GST_CLOCK_OK;
2216 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2217 gst_object_unref (clock);
2218 return GST_CLOCK_OK;
2222 /* Called with STREAM_LOCK and LIVE_LOCK */
2224 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2227 guint64 size, maxsize;
2228 GstBaseSrcClass *bclass;
2232 bclass = GST_BASE_SRC_GET_CLASS (src);
2234 format = src->segment.format;
2235 stop = src->segment.stop;
2236 /* get total file size */
2237 size = src->segment.duration;
2239 /* only operate if we are working with bytes */
2240 if (format != GST_FORMAT_BYTES)
2243 /* the max amount of bytes to read is the total size or
2244 * up to the segment.stop if present. */
2246 maxsize = MIN (size, stop);
2250 GST_DEBUG_OBJECT (src,
2251 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2252 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2253 *length, size, stop, maxsize);
2255 /* check size if we have one */
2256 if (maxsize != -1) {
2257 /* if we run past the end, check if the file became bigger and
2259 if (G_UNLIKELY (offset + *length >= maxsize || force)) {
2260 /* see if length of the file changed */
2261 if (bclass->get_size)
2262 if (!bclass->get_size (src, &size))
2265 /* make sure we don't exceed the configured segment stop
2268 maxsize = MIN (size, stop);
2272 /* if we are at or past the end, EOS */
2273 if (G_UNLIKELY (offset >= maxsize))
2274 goto unexpected_length;
2276 /* else we can clip to the end */
2277 if (G_UNLIKELY (offset + *length >= maxsize))
2278 *length = maxsize - offset;
2283 /* keep track of current duration.
2284 * segment is in bytes, we checked that above. */
2285 GST_OBJECT_LOCK (src);
2286 src->segment.duration = size;
2287 GST_OBJECT_UNLOCK (src);
2298 /* must be called with LIVE_LOCK */
2299 static GstFlowReturn
2300 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2304 GstBaseSrcClass *bclass;
2305 GstClockReturn status;
2309 bclass = GST_BASE_SRC_GET_CLASS (src);
2313 if (G_UNLIKELY (!src->live_running)) {
2314 ret = gst_base_src_wait_playing (src);
2315 if (ret != GST_FLOW_OK)
2320 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2321 && !GST_BASE_SRC_IS_STARTING (src)))
2324 if (G_UNLIKELY (!bclass->create))
2327 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2328 goto unexpected_length;
2330 /* track position */
2331 GST_OBJECT_LOCK (src);
2332 if (src->segment.format == GST_FORMAT_BYTES)
2333 src->segment.position = offset;
2334 GST_OBJECT_UNLOCK (src);
2336 /* normally we don't count buffers */
2337 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2338 if (src->num_buffers_left == 0)
2339 goto reached_num_buffers;
2341 src->num_buffers_left--;
2344 /* don't enter the create function if a pending EOS event was set. For the
2345 * logic of the pending_eos, check the event function of this class. */
2346 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2349 GST_DEBUG_OBJECT (src,
2350 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2351 G_GINT64_FORMAT, offset, length, src->segment.time);
2353 res_buf = in_buf = *buf;
2355 ret = bclass->create (src, offset, length, &res_buf);
2357 /* The create function could be unlocked because we have a pending EOS. It's
2358 * possible that we have a valid buffer from create that we need to
2359 * discard when the create function returned _OK. */
2360 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2361 if (ret == GST_FLOW_OK) {
2363 gst_buffer_unref (res_buf);
2368 if (G_UNLIKELY (ret != GST_FLOW_OK))
2371 /* fallback in case the create function didn't fill a provided buffer */
2372 if (in_buf != NULL && res_buf != in_buf) {
2376 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2377 "fill the provided buffer, copying");
2379 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2382 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2383 gst_buffer_unmap (in_buf, &info);
2384 gst_buffer_set_size (in_buf, copied_size);
2386 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2388 gst_buffer_unref (res_buf);
2392 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2393 if (offset == 0 && src->segment.time == 0
2394 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2395 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2396 res_buf = gst_buffer_make_writable (res_buf);
2397 GST_BUFFER_DTS (res_buf) = 0;
2400 /* now sync before pushing the buffer */
2401 status = gst_base_src_do_sync (src, res_buf);
2403 /* waiting for the clock could have made us flushing */
2404 if (G_UNLIKELY (src->priv->flushing))
2408 case GST_CLOCK_EARLY:
2409 /* the buffer is too late. We currently don't drop the buffer. */
2410 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2413 /* buffer synchronised properly */
2414 GST_DEBUG_OBJECT (src, "buffer ok");
2416 case GST_CLOCK_UNSCHEDULED:
2417 /* this case is triggered when we were waiting for the clock and
2418 * it got unlocked because we did a state change. In any case, get rid of
2421 gst_buffer_unref (res_buf);
2423 if (!src->live_running) {
2424 /* We return FLUSHING when we are not running to stop the dataflow also
2425 * get rid of the produced buffer. */
2426 GST_DEBUG_OBJECT (src,
2427 "clock was unscheduled (%d), returning FLUSHING", status);
2428 ret = GST_FLOW_FLUSHING;
2430 /* If we are running when this happens, we quickly switched between
2431 * pause and playing. We try to produce a new buffer */
2432 GST_DEBUG_OBJECT (src,
2433 "clock was unscheduled (%d), but we are running", status);
2438 /* all other result values are unexpected and errors */
2439 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2440 (_("Internal clock error.")),
2441 ("clock returned unexpected return value %d", status));
2443 gst_buffer_unref (res_buf);
2444 ret = GST_FLOW_ERROR;
2447 if (G_LIKELY (ret == GST_FLOW_OK))
2455 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2456 gst_flow_get_name (ret));
2461 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2462 gst_flow_get_name (ret));
2467 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2468 (_("Failed to map buffer.")),
2469 ("failed to map result buffer in WRITE mode"));
2471 gst_buffer_unref (res_buf);
2472 return GST_FLOW_ERROR;
2476 GST_DEBUG_OBJECT (src, "getrange but not started");
2477 return GST_FLOW_FLUSHING;
2481 GST_DEBUG_OBJECT (src, "no create function");
2482 return GST_FLOW_NOT_SUPPORTED;
2486 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2487 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2488 return GST_FLOW_EOS;
2490 reached_num_buffers:
2492 GST_DEBUG_OBJECT (src, "sent all buffers");
2493 return GST_FLOW_EOS;
2497 GST_DEBUG_OBJECT (src, "we are flushing");
2499 gst_buffer_unref (res_buf);
2500 return GST_FLOW_FLUSHING;
2504 GST_DEBUG_OBJECT (src, "we are EOS");
2505 return GST_FLOW_EOS;
2509 static GstFlowReturn
2510 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2511 guint length, GstBuffer ** buf)
2516 src = GST_BASE_SRC_CAST (parent);
2518 GST_LIVE_LOCK (src);
2519 if (G_UNLIKELY (src->priv->flushing))
2522 res = gst_base_src_get_range (src, offset, length, buf);
2525 GST_LIVE_UNLOCK (src);
2532 GST_DEBUG_OBJECT (src, "we are flushing");
2533 res = GST_FLOW_FLUSHING;
2539 gst_base_src_is_random_access (GstBaseSrc * src)
2541 /* we need to start the basesrc to check random access */
2542 if (!GST_BASE_SRC_IS_STARTED (src)) {
2543 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2544 if (G_LIKELY (gst_base_src_start (src))) {
2545 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2547 gst_base_src_stop (src);
2551 return src->random_access;
2556 GST_DEBUG_OBJECT (src, "failed to start");
2562 gst_base_src_loop (GstPad * pad)
2565 GstBuffer *buf = NULL;
2570 GList *pending_events = NULL, *tmp;
2574 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2576 gst_base_src_send_stream_start (src);
2578 /* check if we need to renegotiate */
2579 if (gst_pad_check_reconfigure (pad)) {
2580 if (!gst_base_src_negotiate (src)) {
2581 gst_pad_mark_reconfigure (pad);
2582 if (GST_PAD_IS_FLUSHING (pad))
2585 goto negotiate_failed;
2589 GST_LIVE_LOCK (src);
2591 if (G_UNLIKELY (src->priv->flushing))
2594 blocksize = src->blocksize;
2596 /* if we operate in bytes, we can calculate an offset */
2597 if (src->segment.format == GST_FORMAT_BYTES) {
2598 position = src->segment.position;
2599 /* for negative rates, start with subtracting the blocksize */
2600 if (src->segment.rate < 0.0) {
2601 /* we cannot go below segment.start */
2602 if (position > src->segment.start + blocksize)
2603 position -= blocksize;
2605 /* last block, remainder up to segment.start */
2606 blocksize = position - src->segment.start;
2607 position = src->segment.start;
2613 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2614 GST_TIME_ARGS (position), blocksize);
2616 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2617 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2618 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2619 gst_flow_get_name (ret));
2620 GST_LIVE_UNLOCK (src);
2623 /* this should not happen */
2624 if (G_UNLIKELY (buf == NULL))
2627 /* push events to close/start our segment before we push the buffer. */
2628 if (G_UNLIKELY (src->priv->segment_pending)) {
2629 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2630 src->priv->segment_pending = FALSE;
2633 if (g_atomic_int_get (&src->priv->have_events)) {
2634 GST_OBJECT_LOCK (src);
2635 /* take the events */
2636 pending_events = src->priv->pending_events;
2637 src->priv->pending_events = NULL;
2638 g_atomic_int_set (&src->priv->have_events, FALSE);
2639 GST_OBJECT_UNLOCK (src);
2642 /* Push out pending events if any */
2643 if (G_UNLIKELY (pending_events != NULL)) {
2644 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2645 GstEvent *ev = (GstEvent *) tmp->data;
2646 gst_pad_push_event (pad, ev);
2648 g_list_free (pending_events);
2651 /* figure out the new position */
2652 switch (src->segment.format) {
2653 case GST_FORMAT_BYTES:
2655 guint bufsize = gst_buffer_get_size (buf);
2657 /* we subtracted above for negative rates */
2658 if (src->segment.rate >= 0.0)
2659 position += bufsize;
2662 case GST_FORMAT_TIME:
2664 GstClockTime start, duration;
2666 start = GST_BUFFER_TIMESTAMP (buf);
2667 duration = GST_BUFFER_DURATION (buf);
2669 if (GST_CLOCK_TIME_IS_VALID (start))
2672 position = src->segment.position;
2674 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2675 if (src->segment.rate >= 0.0)
2676 position += duration;
2677 else if (position > duration)
2678 position -= duration;
2684 case GST_FORMAT_DEFAULT:
2685 if (src->segment.rate >= 0.0)
2686 position = GST_BUFFER_OFFSET_END (buf);
2688 position = GST_BUFFER_OFFSET (buf);
2694 if (position != -1) {
2695 if (src->segment.rate >= 0.0) {
2696 /* positive rate, check if we reached the stop */
2697 if (src->segment.stop != -1) {
2698 if (position >= src->segment.stop) {
2700 position = src->segment.stop;
2704 /* negative rate, check if we reached the start. start is always set to
2705 * something different from -1 */
2706 if (position <= src->segment.start) {
2708 position = src->segment.start;
2710 /* when going reverse, all buffers are DISCONT */
2711 src->priv->discont = TRUE;
2713 GST_OBJECT_LOCK (src);
2714 src->segment.position = position;
2715 GST_OBJECT_UNLOCK (src);
2718 if (G_UNLIKELY (src->priv->discont)) {
2719 GST_INFO_OBJECT (src, "marking pending DISCONT");
2720 buf = gst_buffer_make_writable (buf);
2721 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2722 src->priv->discont = FALSE;
2724 GST_LIVE_UNLOCK (src);
2726 ret = gst_pad_push (pad, buf);
2727 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2728 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2729 goto not_negotiated;
2731 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2732 gst_flow_get_name (ret));
2736 if (G_UNLIKELY (eos)) {
2737 GST_INFO_OBJECT (src, "pausing after end of segment");
2748 if (gst_pad_needs_reconfigure (pad)) {
2749 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2752 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2753 * a pending negotiation request on our srcpad */
2757 GST_DEBUG_OBJECT (src, "Not negotiated");
2758 ret = GST_FLOW_NOT_NEGOTIATED;
2763 GST_DEBUG_OBJECT (src, "we are flushing");
2764 GST_LIVE_UNLOCK (src);
2765 ret = GST_FLOW_FLUSHING;
2770 const gchar *reason = gst_flow_get_name (ret);
2773 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2774 src->running = FALSE;
2775 gst_pad_pause_task (pad);
2776 if (ret == GST_FLOW_EOS) {
2777 gboolean flag_segment;
2781 /* perform EOS logic */
2782 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2783 format = src->segment.format;
2784 position = src->segment.position;
2787 GstMessage *message;
2789 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2791 gst_message_set_seqnum (message, src->priv->seqnum);
2792 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2793 event = gst_event_new_segment_done (format, position);
2794 gst_event_set_seqnum (event, src->priv->seqnum);
2795 gst_pad_push_event (pad, event);
2797 event = gst_event_new_eos ();
2798 gst_event_set_seqnum (event, src->priv->seqnum);
2799 gst_pad_push_event (pad, event);
2801 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2802 event = gst_event_new_eos ();
2803 gst_event_set_seqnum (event, src->priv->seqnum);
2804 /* for fatal errors we post an error message, post the error
2805 * first so the app knows about the error first.
2806 * Also don't do this for FLUSHING because it happens
2807 * due to flushing and posting an error message because of
2808 * that is the wrong thing to do, e.g. when we're doing
2809 * a flushing seek. */
2810 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2811 (_("Internal data flow error.")),
2812 ("streaming task paused, reason %s (%d)", reason, ret));
2813 gst_pad_push_event (pad, event);
2819 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2820 (_("Internal data flow error.")), ("element returned NULL buffer"));
2821 GST_LIVE_UNLOCK (src);
2827 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2828 GstAllocator * allocator, GstAllocationParams * params)
2830 GstAllocator *oldalloc;
2831 GstBufferPool *oldpool;
2832 GstBaseSrcPrivate *priv = basesrc->priv;
2835 GST_DEBUG_OBJECT (basesrc, "activate pool");
2836 if (!gst_buffer_pool_set_active (pool, TRUE))
2837 goto activate_failed;
2840 GST_OBJECT_LOCK (basesrc);
2841 oldpool = priv->pool;
2844 oldalloc = priv->allocator;
2845 priv->allocator = allocator;
2848 priv->params = *params;
2850 gst_allocation_params_init (&priv->params);
2851 GST_OBJECT_UNLOCK (basesrc);
2854 /* only deactivate if the pool is not the one we're using */
2855 if (oldpool != pool) {
2856 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2857 gst_buffer_pool_set_active (oldpool, FALSE);
2859 gst_object_unref (oldpool);
2862 gst_object_unref (oldalloc);
2869 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2875 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2877 GstBaseSrcPrivate *priv = basesrc->priv;
2878 GstBufferPool *pool;
2879 gboolean res = TRUE;
2881 GST_OBJECT_LOCK (basesrc);
2882 if ((pool = priv->pool))
2883 pool = gst_object_ref (pool);
2884 GST_OBJECT_UNLOCK (basesrc);
2887 res = gst_buffer_pool_set_active (pool, active);
2888 gst_object_unref (pool);
2895 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
2898 GstBufferPool *pool;
2899 guint size, min, max;
2900 GstAllocator *allocator;
2901 GstAllocationParams params;
2902 GstStructure *config;
2903 gboolean update_allocator;
2905 gst_query_parse_allocation (query, &outcaps, NULL);
2907 /* we got configuration from our peer or the decide_allocation method,
2909 if (gst_query_get_n_allocation_params (query) > 0) {
2910 /* try the allocator */
2911 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2912 update_allocator = TRUE;
2915 gst_allocation_params_init (¶ms);
2916 update_allocator = FALSE;
2919 if (gst_query_get_n_allocation_pools (query) > 0) {
2920 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
2923 /* no pool, we can make our own */
2924 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
2925 pool = gst_buffer_pool_new ();
2929 size = min = max = 0;
2934 config = gst_buffer_pool_get_config (pool);
2935 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
2936 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
2937 gst_buffer_pool_set_config (pool, config);
2940 if (update_allocator)
2941 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
2943 gst_query_add_allocation_param (query, allocator, ¶ms);
2945 gst_object_unref (allocator);
2948 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
2949 gst_object_unref (pool);
2956 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
2958 GstBaseSrcClass *bclass;
2959 gboolean result = TRUE;
2961 GstBufferPool *pool = NULL;
2962 GstAllocator *allocator = NULL;
2963 GstAllocationParams params;
2965 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2967 /* make query and let peer pad answer, we don't really care if it worked or
2968 * not, if it failed, the allocation query would contain defaults and the
2969 * subclass would then set better values if needed */
2970 query = gst_query_new_allocation (caps, TRUE);
2971 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
2972 /* not a problem, just debug a little */
2973 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
2976 g_assert (bclass->decide_allocation != NULL);
2977 result = bclass->decide_allocation (basesrc, query);
2979 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
2983 goto no_decide_allocation;
2985 /* we got configuration from our peer or the decide_allocation method,
2987 if (gst_query_get_n_allocation_params (query) > 0) {
2988 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
2991 gst_allocation_params_init (¶ms);
2994 if (gst_query_get_n_allocation_pools (query) > 0)
2995 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
2997 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
2999 gst_query_unref (query);
3004 no_decide_allocation:
3006 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3007 gst_query_unref (query);
3013 /* default negotiation code.
3015 * Take intersection between src and sink pads, take first
3019 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3022 GstCaps *caps = NULL;
3023 GstCaps *peercaps = NULL;
3024 gboolean result = FALSE;
3026 /* first see what is possible on our source pad */
3027 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3028 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3029 /* nothing or anything is allowed, we're done */
3030 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3031 goto no_nego_needed;
3033 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3036 /* get the peer caps */
3037 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3038 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3040 /* The result is already a subset of our caps */
3042 gst_caps_unref (thiscaps);
3044 /* no peer, work with our own caps then */
3047 if (caps && !gst_caps_is_empty (caps)) {
3049 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3050 if (gst_caps_is_any (caps)) {
3051 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3052 /* hmm, still anything, so element can do anything and
3053 * nego is not needed */
3056 caps = gst_base_src_fixate (basesrc, caps);
3057 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3058 if (gst_caps_is_fixed (caps)) {
3059 /* yay, fixed caps, use those then, it's possible that the subclass does
3060 * not accept this caps after all and we have to fail. */
3061 result = gst_base_src_set_caps (basesrc, caps);
3064 gst_caps_unref (caps);
3067 gst_caps_unref (caps);
3068 GST_DEBUG_OBJECT (basesrc, "no common caps");
3074 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3076 gst_caps_unref (thiscaps);
3081 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3082 ("No supported formats found"),
3083 ("This element did not produce valid caps"));
3085 gst_caps_unref (thiscaps);
3091 gst_base_src_negotiate (GstBaseSrc * basesrc)
3093 GstBaseSrcClass *bclass;
3096 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3098 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3100 if (G_LIKELY (bclass->negotiate))
3101 result = bclass->negotiate (basesrc);
3105 if (G_LIKELY (result)) {
3108 caps = gst_pad_get_current_caps (basesrc->srcpad);
3110 result = gst_base_src_prepare_allocation (basesrc, caps);
3113 gst_caps_unref (caps);
3119 gst_base_src_start (GstBaseSrc * basesrc)
3121 GstBaseSrcClass *bclass;
3124 GST_LIVE_LOCK (basesrc);
3126 GST_OBJECT_LOCK (basesrc);
3127 if (GST_BASE_SRC_IS_STARTING (basesrc))
3129 if (GST_BASE_SRC_IS_STARTED (basesrc))
3132 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3133 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3134 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3135 GST_OBJECT_UNLOCK (basesrc);
3137 basesrc->num_buffers_left = basesrc->num_buffers;
3138 basesrc->running = FALSE;
3139 basesrc->priv->segment_pending = FALSE;
3140 GST_LIVE_UNLOCK (basesrc);
3142 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3144 result = bclass->start (basesrc);
3149 goto could_not_start;
3151 if (!gst_base_src_is_async (basesrc)) {
3152 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3153 /* not really waiting here, we call this to get the result
3154 * from the start_complete call */
3155 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3163 GST_DEBUG_OBJECT (basesrc, "was starting");
3164 GST_OBJECT_UNLOCK (basesrc);
3165 GST_LIVE_UNLOCK (basesrc);
3170 GST_DEBUG_OBJECT (basesrc, "was started");
3171 GST_OBJECT_UNLOCK (basesrc);
3172 GST_LIVE_UNLOCK (basesrc);
3177 GST_DEBUG_OBJECT (basesrc, "could not start");
3178 /* subclass is supposed to post a message. We don't have to call _stop. */
3179 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3185 * gst_base_src_start_complete:
3186 * @basesrc: base source instance
3187 * @ret: a #GstFlowReturn
3189 * Complete an asynchronous start operation. When the subclass overrides the
3190 * start method, it should call gst_base_src_start_complete() when the start
3191 * operation completes either from the same thread or from an asynchronous
3195 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3204 if (ret != GST_FLOW_OK)
3207 GST_DEBUG_OBJECT (basesrc, "starting source");
3208 format = basesrc->segment.format;
3210 /* figure out the size */
3213 if (format == GST_FORMAT_BYTES) {
3214 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3216 if (bclass->get_size) {
3217 if (!(have_size = bclass->get_size (basesrc, &size)))
3220 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3221 /* only update the size when operating in bytes, subclass is supposed
3222 * to set duration in the start method for other formats */
3223 GST_OBJECT_LOCK (basesrc);
3224 basesrc->segment.duration = size;
3225 GST_OBJECT_UNLOCK (basesrc);
3228 GST_DEBUG_OBJECT (basesrc,
3229 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3230 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3231 basesrc->segment.duration);
3233 seekable = gst_base_src_seekable (basesrc);
3234 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3236 /* update for random access flag */
3237 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3239 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3241 /* stop flushing now but for live sources, still block in the LIVE lock when
3242 * we are not yet PLAYING */
3243 gst_base_src_set_flushing (basesrc, FALSE, FALSE, NULL);
3245 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3247 GST_OBJECT_LOCK (basesrc->srcpad);
3248 mode = GST_PAD_MODE (basesrc->srcpad);
3249 GST_OBJECT_UNLOCK (basesrc->srcpad);
3251 /* take the stream lock here, we only want to let the task run when we have
3252 * set the STARTED flag */
3253 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3254 if (mode == GST_PAD_MODE_PUSH) {
3255 /* do initial seek, which will start the task */
3256 GST_OBJECT_LOCK (basesrc);
3257 event = basesrc->pending_seek;
3258 basesrc->pending_seek = NULL;
3259 GST_OBJECT_UNLOCK (basesrc);
3261 /* The perform seek code will start the task when finished. We don't have to
3262 * unlock the streaming thread because it is not running yet */
3263 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3267 gst_event_unref (event);
3269 /* if not random_access, we cannot operate in pull mode for now */
3270 if (G_UNLIKELY (!basesrc->random_access))
3274 GST_OBJECT_LOCK (basesrc);
3275 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3276 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3277 basesrc->priv->start_result = ret;
3278 GST_ASYNC_SIGNAL (basesrc);
3279 GST_OBJECT_UNLOCK (basesrc);
3281 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3287 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3288 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3289 gst_base_src_stop (basesrc);
3291 gst_event_unref (event);
3292 ret = GST_FLOW_ERROR;
3297 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3298 gst_base_src_stop (basesrc);
3299 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3300 ret = GST_FLOW_ERROR;
3305 GST_OBJECT_LOCK (basesrc);
3306 basesrc->priv->start_result = ret;
3307 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3308 GST_ASYNC_SIGNAL (basesrc);
3309 GST_OBJECT_UNLOCK (basesrc);
3315 * gst_base_src_start_wait:
3316 * @basesrc: base source instance
3318 * Wait until the start operation completes.
3320 * Returns: a #GstFlowReturn.
3323 gst_base_src_start_wait (GstBaseSrc * basesrc)
3325 GstFlowReturn result;
3327 GST_OBJECT_LOCK (basesrc);
3328 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3329 GST_ASYNC_WAIT (basesrc);
3331 result = basesrc->priv->start_result;
3332 GST_OBJECT_UNLOCK (basesrc);
3334 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3340 gst_base_src_stop (GstBaseSrc * basesrc)
3342 GstBaseSrcClass *bclass;
3343 gboolean result = TRUE;
3345 GST_DEBUG_OBJECT (basesrc, "stopping source");
3348 gst_base_src_set_flushing (basesrc, TRUE, FALSE, NULL);
3350 gst_pad_stop_task (basesrc->srcpad);
3352 GST_OBJECT_LOCK (basesrc);
3353 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3356 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3357 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3358 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3359 GST_ASYNC_SIGNAL (basesrc);
3360 GST_OBJECT_UNLOCK (basesrc);
3362 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3364 result = bclass->stop (basesrc);
3366 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3372 GST_DEBUG_OBJECT (basesrc, "was stopped");
3373 GST_OBJECT_UNLOCK (basesrc);
3378 /* start or stop flushing dataprocessing
3381 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3382 gboolean flushing, gboolean live_play, gboolean * playing)
3384 GstBaseSrcClass *bclass;
3386 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3388 GST_DEBUG_OBJECT (basesrc, "flushing %d, live_play %d", flushing, live_play);
3391 gst_base_src_activate_pool (basesrc, FALSE);
3392 /* unlock any subclasses, we need to do this before grabbing the
3393 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3394 * unlock to the params because of backwards compat (see seek handler)*/
3396 bclass->unlock (basesrc);
3399 /* the live lock is released when we are blocked, waiting for playing or
3400 * when we sync to the clock. */
3401 GST_LIVE_LOCK (basesrc);
3403 *playing = basesrc->live_running;
3404 basesrc->priv->flushing = flushing;
3406 /* if we are locked in the live lock, signal it to make it flush */
3407 basesrc->live_running = TRUE;
3409 /* clear pending EOS if any */
3410 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3412 /* step 1, now that we have the LIVE lock, clear our unlock request */
3413 if (bclass->unlock_stop)
3414 bclass->unlock_stop (basesrc);
3416 /* step 2, unblock clock sync (if any) or any other blocking thing */
3417 if (basesrc->clock_id)
3418 gst_clock_id_unschedule (basesrc->clock_id);
3420 /* signal the live source that it can start playing */
3421 basesrc->live_running = live_play;
3423 gst_base_src_activate_pool (basesrc, TRUE);
3425 /* Drop all delayed events */
3426 GST_OBJECT_LOCK (basesrc);
3427 if (basesrc->priv->pending_events) {
3428 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3430 g_list_free (basesrc->priv->pending_events);
3431 basesrc->priv->pending_events = NULL;
3432 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3434 GST_OBJECT_UNLOCK (basesrc);
3436 GST_LIVE_SIGNAL (basesrc);
3437 GST_LIVE_UNLOCK (basesrc);
3442 /* the purpose of this function is to make sure that a live source blocks in the
3443 * LIVE lock or leaves the LIVE lock and continues playing. */
3445 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3447 GstBaseSrcClass *bclass;
3449 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3451 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3453 GST_DEBUG_OBJECT (basesrc, "unlock");
3455 bclass->unlock (basesrc);
3458 /* we are now able to grab the LIVE lock, when we get it, we can be
3459 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3461 GST_LIVE_LOCK (basesrc);
3462 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3464 /* unblock clock sync (if any) */
3465 if (basesrc->clock_id)
3466 gst_clock_id_unschedule (basesrc->clock_id);
3468 /* configure what to do when we get to the LIVE lock. */
3469 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3470 basesrc->live_running = live_play;
3475 /* clear our unlock request when going to PLAYING */
3476 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3477 if (bclass->unlock_stop)
3478 bclass->unlock_stop (basesrc);
3480 /* for live sources we restart the timestamp correction */
3481 basesrc->priv->latency = -1;
3482 /* have to restart the task in case it stopped because of the unlock when
3483 * we went to PAUSED. Only do this if we operating in push mode. */
3484 GST_OBJECT_LOCK (basesrc->srcpad);
3485 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3486 GST_OBJECT_UNLOCK (basesrc->srcpad);
3488 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3489 basesrc->srcpad, NULL);
3490 GST_DEBUG_OBJECT (basesrc, "signal");
3491 GST_LIVE_SIGNAL (basesrc);
3493 GST_LIVE_UNLOCK (basesrc);
3499 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3501 GstBaseSrc *basesrc;
3503 basesrc = GST_BASE_SRC (parent);
3505 /* prepare subclass first */
3507 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3509 if (G_UNLIKELY (!basesrc->can_activate_push))
3510 goto no_push_activation;
3512 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3515 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3516 /* now we can stop the source */
3517 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3525 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3530 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3535 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3541 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3543 GstBaseSrc *basesrc;
3545 basesrc = GST_BASE_SRC (parent);
3547 /* prepare subclass first */
3549 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3550 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3553 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3554 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3562 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3567 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3573 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3574 GstPadMode mode, gboolean active)
3577 GstBaseSrc *src = GST_BASE_SRC (parent);
3579 src->priv->stream_start_pending = FALSE;
3582 case GST_PAD_MODE_PULL:
3583 res = gst_base_src_activate_pull (pad, parent, active);
3585 case GST_PAD_MODE_PUSH:
3586 src->priv->stream_start_pending = active;
3587 res = gst_base_src_activate_push (pad, parent, active);
3590 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3598 static GstStateChangeReturn
3599 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3601 GstBaseSrc *basesrc;
3602 GstStateChangeReturn result;
3603 gboolean no_preroll = FALSE;
3605 basesrc = GST_BASE_SRC (element);
3607 switch (transition) {
3608 case GST_STATE_CHANGE_NULL_TO_READY:
3610 case GST_STATE_CHANGE_READY_TO_PAUSED:
3611 no_preroll = gst_base_src_is_live (basesrc);
3613 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3614 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3615 if (gst_base_src_is_live (basesrc)) {
3616 /* now we can start playback */
3617 gst_base_src_set_playing (basesrc, TRUE);
3625 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3626 transition)) == GST_STATE_CHANGE_FAILURE)
3629 switch (transition) {
3630 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3631 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3632 if (gst_base_src_is_live (basesrc)) {
3633 /* make sure we block in the live lock in PAUSED */
3634 gst_base_src_set_playing (basesrc, FALSE);
3638 case GST_STATE_CHANGE_PAUSED_TO_READY:
3640 /* we don't need to unblock anything here, the pad deactivation code
3641 * already did this */
3642 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3643 gst_event_replace (&basesrc->pending_seek, NULL);
3646 case GST_STATE_CHANGE_READY_TO_NULL:
3652 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3653 result = GST_STATE_CHANGE_NO_PREROLL;
3660 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3666 * gst_base_src_get_buffer_pool:
3667 * @src: a #GstBaseSrc
3669 * Returns: (transfer full): the instance of the #GstBufferPool used
3670 * by the src; free it after use it
3673 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3675 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3677 if (src->priv->pool)
3678 return gst_object_ref (src->priv->pool);
3684 * gst_base_src_get_allocator:
3685 * @src: a #GstBaseSrc
3686 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3688 * @params: (out) (allow-none) (transfer full): the
3689 * #GstAllocatorParams of @allocator
3691 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3692 * used by the base class and its @params.
3694 * Unref the @allocator after use it.
3697 gst_base_src_get_allocator (GstBaseSrc * src,
3698 GstAllocator ** allocator, GstAllocationParams * params)
3700 g_return_if_fail (GST_IS_BASE_SRC (src));
3703 *allocator = src->priv->allocator ?
3704 gst_object_ref (src->priv->allocator) : NULL;
3707 *params = src->priv->params;