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.
26 * @short_description: Base class for getrange based source elements
27 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
29 * This is a generic base class for source elements. The following
30 * types of sources are supported:
32 * * random access sources like files
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 * * The format is set to %GST_FORMAT_BYTES (default).
45 * * #GstBaseSrcClass.is_seekable() returns %TRUE.
47 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
48 * automatically seekable in push mode as well. The following conditions must
49 * be met to make the element seekable in push mode when the format is not
52 * * #GstBaseSrcClass.is_seekable() returns %TRUE.
53 * * #GstBaseSrcClass.query() can convert all supported seek formats to the
54 * internal format as set with gst_base_src_set_format().
55 * * #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
58 * When the element does not meet the requirements to operate in pull mode, the
59 * offset and length in the #GstBaseSrcClass.create() method should be ignored.
60 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
61 * element can operate in pull mode but only with specific offsets and
62 * lengths, it is allowed to generate an error when the wrong values are passed
63 * to the #GstBaseSrcClass.create() function.
65 * #GstBaseSrc has support for live sources. Live sources are sources that when
66 * paused discard data, such as audio or video capture devices. A typical live
67 * source also produces data at a fixed rate and thus provides a clock to publish
69 * Use gst_base_src_set_live() to activate the live source mode.
71 * A live source does not produce data in the PAUSED state. This means that the
72 * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
73 * PLAYING. To signal the pipeline that the element will not produce data, the
74 * return value from the READY to PAUSED state will be
75 * %GST_STATE_CHANGE_NO_PREROLL.
77 * A typical live source will timestamp the buffers it creates with the
78 * current running time of the pipeline. This is one reason why a live source
79 * can only produce data in the PLAYING state, when the clock is actually
80 * distributed and running.
82 * Live sources that synchronize and block on the clock (an audio source, for
83 * example) can use gst_base_src_wait_playing() when the
84 * #GstBaseSrcClass.create() function was interrupted by a state change to
87 * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
88 * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
89 * function if the source is a live source. The #GstBaseSrcClass.get_times()
90 * function should return timestamps starting from 0, as if it were a non-live
91 * source. The base class will make sure that the timestamps are transformed
92 * into the current running_time. The base source will then wait for the
93 * calculated running_time before pushing out the buffer.
95 * For live sources, the base class will by default report a latency of 0.
96 * For pseudo live sources, the base class will by default measure the difference
97 * between the first buffer timestamp and the start time of get_times and will
98 * report this value as the latency.
99 * Subclasses should override the query function when this behaviour is not
102 * There is only support in #GstBaseSrc for exactly one source pad, which
103 * should be named "src". A source implementation (subclass of #GstBaseSrc)
104 * should install a pad template in its class_init function, like so:
105 * |[<!-- language="C" -->
107 * my_element_class_init (GstMyElementClass *klass)
109 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
110 * // srctemplate should be a #GstStaticPadTemplate with direction
111 * // %GST_PAD_SRC and name "src"
112 * gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
114 * gst_element_class_set_static_metadata (gstelement_class,
117 * "My Source element",
118 * "The author <my.sink@my.email>");
122 * ## Controlled shutdown of live sources in applications
124 * Applications that record from a live source may want to stop recording
125 * in a controlled way, so that the recording is stopped, but the data
126 * already in the pipeline is processed to the end (remember that many live
127 * sources would go on recording forever otherwise). For that to happen the
128 * application needs to make the source stop recording and send an EOS
129 * event down the pipeline. The application would then wait for an
130 * EOS message posted on the pipeline's bus to know when all data has
131 * been processed and the pipeline can safely be stopped.
133 * An application may send an EOS event to a source element to make it
134 * perform the EOS logic (send EOS event downstream or post a
135 * %GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
136 * with the gst_element_send_event() function on the element or its parent bin.
138 * After the EOS has been sent to the element, the application should wait for
139 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
140 * received, it may safely shut down the entire pipeline.
151 #include <gst/gst_private.h>
152 #include <gst/glib-compat-private.h>
154 #include "gstbasesrc.h"
155 #include "gsttypefindhelper.h"
156 #include <gst/gst-i18n-lib.h>
158 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
159 #define GST_CAT_DEFAULT gst_base_src_debug
161 #define GST_LIVE_GET_LOCK(elem) (&GST_BASE_SRC_CAST(elem)->live_lock)
162 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
163 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
164 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
165 #define GST_LIVE_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->live_cond)
166 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
167 #define GST_LIVE_WAIT_UNTIL(elem, end_time) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
168 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
169 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
172 #define GST_ASYNC_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->priv->async_cond)
173 #define GST_ASYNC_WAIT(elem) g_cond_wait (GST_ASYNC_GET_COND (elem), GST_OBJECT_GET_LOCK (elem))
174 #define GST_ASYNC_SIGNAL(elem) g_cond_signal (GST_ASYNC_GET_COND (elem));
176 #define CLEAR_PENDING_EOS(bsrc) \
178 g_atomic_int_set (&bsrc->priv->has_pending_eos, FALSE); \
179 gst_event_replace (&bsrc->priv->pending_eos, NULL); \
183 /* BaseSrc signals and args */
190 #define DEFAULT_BLOCKSIZE 4096
191 #define DEFAULT_NUM_BUFFERS -1
192 #define DEFAULT_TYPEFIND FALSE
193 #define DEFAULT_DO_TIMESTAMP FALSE
204 #define GST_BASE_SRC_GET_PRIVATE(obj) \
205 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
207 struct _GstBaseSrcPrivate
212 GstFlowReturn start_result;
215 /* if a stream-start event should be sent */
216 gboolean stream_start_pending;
218 /* if segment should be sent and a
219 * seqnum if it was originated by a seek */
220 gboolean segment_pending;
221 guint32 segment_seqnum;
223 /* if EOS is pending (atomic) */
224 GstEvent *pending_eos;
225 gint has_pending_eos;
227 /* if the eos was caused by a forced eos from the application */
230 /* startup latency is the time it takes between going to PLAYING and producing
231 * the first BUFFER with running_time 0. This value is included in the latency
233 GstClockTime latency;
234 /* timestamp offset, this is the offset add to the values of gst_times for
235 * pseudo live sources */
236 GstClockTimeDiff ts_offset;
238 gboolean do_timestamp;
239 volatile gint dynamic_size;
240 volatile gint automatic_eos;
242 /* stream sequence number */
245 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
246 * pushed in the data stream */
247 GList *pending_events;
248 volatile gint have_events;
250 /* QoS *//* with LOCK */
251 gboolean qos_enabled;
253 GstClockTime earliest_time;
256 GstAllocator *allocator;
257 GstAllocationParams params;
262 static GstElementClass *parent_class = NULL;
264 static void gst_base_src_class_init (GstBaseSrcClass * klass);
265 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
266 static void gst_base_src_finalize (GObject * object);
270 gst_base_src_get_type (void)
272 static volatile gsize base_src_type = 0;
274 if (g_once_init_enter (&base_src_type)) {
276 static const GTypeInfo base_src_info = {
277 sizeof (GstBaseSrcClass),
280 (GClassInitFunc) gst_base_src_class_init,
285 (GInstanceInitFunc) gst_base_src_init,
288 _type = g_type_register_static (GST_TYPE_ELEMENT,
289 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
290 g_once_init_leave (&base_src_type, _type);
292 return base_src_type;
295 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
297 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
298 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
300 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
301 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
302 GstPadMode mode, gboolean active);
303 static void gst_base_src_set_property (GObject * object, guint prop_id,
304 const GValue * value, GParamSpec * pspec);
305 static void gst_base_src_get_property (GObject * object, guint prop_id,
306 GValue * value, GParamSpec * pspec);
307 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
309 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
310 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
312 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
315 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
317 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
318 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
319 GstSegment * segment);
320 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
321 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
322 GstEvent * event, GstSegment * segment);
323 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
324 guint64 offset, guint size, GstBuffer ** buf);
325 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
326 guint64 offset, guint size, GstBuffer ** buf);
327 static gboolean gst_base_src_decide_allocation_default (GstBaseSrc * basesrc,
330 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
331 gboolean flushing, gboolean live_play, gboolean * playing);
333 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
334 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
336 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
337 GstStateChange transition);
339 static void gst_base_src_loop (GstPad * pad);
340 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
341 guint64 offset, guint length, GstBuffer ** buf);
342 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
343 guint length, GstBuffer ** buf);
344 static gboolean gst_base_src_seekable (GstBaseSrc * src);
345 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
346 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
347 guint * length, gboolean force);
350 gst_base_src_class_init (GstBaseSrcClass * klass)
352 GObjectClass *gobject_class;
353 GstElementClass *gstelement_class;
355 gobject_class = G_OBJECT_CLASS (klass);
356 gstelement_class = GST_ELEMENT_CLASS (klass);
358 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
360 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
362 parent_class = g_type_class_peek_parent (klass);
364 gobject_class->finalize = gst_base_src_finalize;
365 gobject_class->set_property = gst_base_src_set_property;
366 gobject_class->get_property = gst_base_src_get_property;
368 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
369 g_param_spec_uint ("blocksize", "Block size",
370 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
371 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
373 g_param_spec_int ("num-buffers", "num-buffers",
374 "Number of buffers to output before sending EOS (-1 = unlimited)",
375 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
376 G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
378 g_param_spec_boolean ("typefind", "Typefind",
379 "Run typefind before negotiating", DEFAULT_TYPEFIND,
380 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
382 g_param_spec_boolean ("do-timestamp", "Do timestamp",
383 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 gstelement_class->change_state =
387 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
388 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
390 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
391 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
392 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
393 klass->prepare_seek_segment =
394 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
395 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
396 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
397 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
398 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
399 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
400 klass->decide_allocation =
401 GST_DEBUG_FUNCPTR (gst_base_src_decide_allocation_default);
403 /* Registering debug symbols for function pointers */
404 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
405 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
406 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
407 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
408 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
412 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
415 GstPadTemplate *pad_template;
417 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
419 basesrc->is_live = FALSE;
420 g_mutex_init (&basesrc->live_lock);
421 g_cond_init (&basesrc->live_cond);
422 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
423 basesrc->num_buffers_left = -1;
424 basesrc->priv->automatic_eos = TRUE;
426 basesrc->can_activate_push = TRUE;
429 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
430 g_return_if_fail (pad_template != NULL);
432 GST_DEBUG_OBJECT (basesrc, "creating src pad");
433 pad = gst_pad_new_from_template (pad_template, "src");
435 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
436 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
437 gst_pad_set_event_function (pad, gst_base_src_event);
438 gst_pad_set_query_function (pad, gst_base_src_query);
439 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
441 /* hold pointer to pad */
442 basesrc->srcpad = pad;
443 GST_DEBUG_OBJECT (basesrc, "adding src pad");
444 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
446 basesrc->blocksize = DEFAULT_BLOCKSIZE;
447 basesrc->clock_id = NULL;
448 /* we operate in BYTES by default */
449 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
450 basesrc->typefind = DEFAULT_TYPEFIND;
451 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
452 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
454 g_cond_init (&basesrc->priv->async_cond);
455 basesrc->priv->start_result = GST_FLOW_FLUSHING;
456 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
457 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
458 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
460 GST_DEBUG_OBJECT (basesrc, "init done");
464 gst_base_src_finalize (GObject * object)
469 basesrc = GST_BASE_SRC (object);
471 g_mutex_clear (&basesrc->live_lock);
472 g_cond_clear (&basesrc->live_cond);
473 g_cond_clear (&basesrc->priv->async_cond);
475 event_p = &basesrc->pending_seek;
476 gst_event_replace (event_p, NULL);
478 if (basesrc->priv->pending_events) {
479 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
481 g_list_free (basesrc->priv->pending_events);
484 G_OBJECT_CLASS (parent_class)->finalize (object);
488 * gst_base_src_wait_playing:
491 * If the #GstBaseSrcClass.create() method performs its own synchronisation
492 * against the clock it must unblock when going from PLAYING to the PAUSED state
493 * and call this method before continuing to produce the remaining data.
495 * This function will block until a state change to PLAYING happens (in which
496 * case this function returns %GST_FLOW_OK) or the processing must be stopped due
497 * to a state change to READY or a FLUSH event (in which case this function
498 * returns %GST_FLOW_FLUSHING).
500 * Returns: %GST_FLOW_OK if @src is PLAYING and processing can
501 * continue. Any other return value should be returned from the create vmethod.
504 gst_base_src_wait_playing (GstBaseSrc * src)
506 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
509 /* block until the state changes, or we get a flush, or something */
510 GST_DEBUG_OBJECT (src, "live source waiting for running state");
512 GST_DEBUG_OBJECT (src, "live source unlocked");
513 if (src->priv->flushing)
515 } while (G_UNLIKELY (!src->live_running));
522 GST_DEBUG_OBJECT (src, "we are flushing");
523 return GST_FLOW_FLUSHING;
528 * gst_base_src_set_live:
529 * @src: base source instance
530 * @live: new live-mode
532 * If the element listens to a live source, @live should
535 * A live source will not produce data in the PAUSED state and
536 * will therefore not be able to participate in the PREROLL phase
537 * of a pipeline. To signal this fact to the application and the
538 * pipeline, the state change return value of the live source will
539 * be GST_STATE_CHANGE_NO_PREROLL.
542 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
544 g_return_if_fail (GST_IS_BASE_SRC (src));
546 GST_OBJECT_LOCK (src);
548 GST_OBJECT_UNLOCK (src);
552 * gst_base_src_is_live:
553 * @src: base source instance
555 * Check if an element is in live mode.
557 * Returns: %TRUE if element is in live mode.
560 gst_base_src_is_live (GstBaseSrc * src)
564 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
566 GST_OBJECT_LOCK (src);
567 result = src->is_live;
568 GST_OBJECT_UNLOCK (src);
574 * gst_base_src_set_format:
575 * @src: base source instance
576 * @format: the format to use
578 * Sets the default format of the source. This will be the format used
579 * for sending SEGMENT events and for performing seeks.
581 * If a format of GST_FORMAT_BYTES is set, the element will be able to
582 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns %TRUE.
584 * This function must only be called in states < %GST_STATE_PAUSED.
587 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
589 g_return_if_fail (GST_IS_BASE_SRC (src));
590 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
592 GST_OBJECT_LOCK (src);
593 gst_segment_init (&src->segment, format);
594 GST_OBJECT_UNLOCK (src);
598 * gst_base_src_set_dynamic_size:
599 * @src: base source instance
600 * @dynamic: new dynamic size mode
602 * If not @dynamic, size is only updated when needed, such as when trying to
603 * read past current tracked size. Otherwise, size is checked for upon each
607 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
609 g_return_if_fail (GST_IS_BASE_SRC (src));
611 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
615 * gst_base_src_set_automatic_eos:
616 * @src: base source instance
617 * @automatic_eos: automatic eos
619 * If @automatic_eos is %TRUE, @src will automatically go EOS if a buffer
620 * after the total size is returned. By default this is %TRUE but sources
621 * that can't return an authoritative size and only know that they're EOS
622 * when trying to read more should set this to %FALSE.
627 gst_base_src_set_automatic_eos (GstBaseSrc * src, gboolean automatic_eos)
629 g_return_if_fail (GST_IS_BASE_SRC (src));
631 g_atomic_int_set (&src->priv->automatic_eos, automatic_eos);
635 * gst_base_src_set_async:
636 * @src: base source instance
637 * @async: new async mode
639 * Configure async behaviour in @src, no state change will block. The open,
640 * close, start, stop, play and pause virtual methods will be executed in a
641 * different thread and are thus allowed to perform blocking operations. Any
642 * blocking operation should be unblocked with the unlock vmethod.
645 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
647 g_return_if_fail (GST_IS_BASE_SRC (src));
649 GST_OBJECT_LOCK (src);
650 src->priv->async = async;
651 GST_OBJECT_UNLOCK (src);
655 * gst_base_src_is_async:
656 * @src: base source instance
658 * Get the current async behaviour of @src. See also gst_base_src_set_async().
660 * Returns: %TRUE if @src is operating in async mode.
663 gst_base_src_is_async (GstBaseSrc * src)
667 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
669 GST_OBJECT_LOCK (src);
670 res = src->priv->async;
671 GST_OBJECT_UNLOCK (src);
678 * gst_base_src_query_latency:
680 * @live: (out) (allow-none): if the source is live
681 * @min_latency: (out) (allow-none): the min latency of the source
682 * @max_latency: (out) (allow-none): the max latency of the source
684 * Query the source for the latency parameters. @live will be %TRUE when @src is
685 * configured as a live source. @min_latency and @max_latency will be set
686 * to the difference between the running time and the timestamp of the first
689 * This function is mostly used by subclasses.
691 * Returns: %TRUE if the query succeeded.
694 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
695 GstClockTime * min_latency, GstClockTime * max_latency)
699 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
701 GST_OBJECT_LOCK (src);
703 *live = src->is_live;
705 /* if we have a startup latency, report this one, else report 0. Subclasses
706 * are supposed to override the query function if they want something
708 if (src->priv->latency != -1)
709 min = src->priv->latency;
718 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
719 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
720 GST_TIME_ARGS (min));
721 GST_OBJECT_UNLOCK (src);
727 * gst_base_src_set_blocksize:
729 * @blocksize: the new blocksize in bytes
731 * Set the number of bytes that @src will push out with each buffer. When
732 * @blocksize is set to -1, a default length will be used.
735 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
737 g_return_if_fail (GST_IS_BASE_SRC (src));
739 GST_OBJECT_LOCK (src);
740 src->blocksize = blocksize;
741 GST_OBJECT_UNLOCK (src);
745 * gst_base_src_get_blocksize:
748 * Get the number of bytes that @src will push out with each buffer.
750 * Returns: the number of bytes pushed with each buffer.
753 gst_base_src_get_blocksize (GstBaseSrc * src)
757 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
759 GST_OBJECT_LOCK (src);
760 res = src->blocksize;
761 GST_OBJECT_UNLOCK (src);
768 * gst_base_src_set_do_timestamp:
770 * @timestamp: enable or disable timestamping
772 * Configure @src to automatically timestamp outgoing buffers based on the
773 * current running_time of the pipeline. This property is mostly useful for live
777 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
779 g_return_if_fail (GST_IS_BASE_SRC (src));
781 GST_OBJECT_LOCK (src);
782 src->priv->do_timestamp = timestamp;
783 if (timestamp && src->segment.format != GST_FORMAT_TIME)
784 gst_segment_init (&src->segment, GST_FORMAT_TIME);
785 GST_OBJECT_UNLOCK (src);
789 * gst_base_src_get_do_timestamp:
792 * Query if @src timestamps outgoing buffers based on the current running_time.
794 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
797 gst_base_src_get_do_timestamp (GstBaseSrc * src)
801 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
803 GST_OBJECT_LOCK (src);
804 res = src->priv->do_timestamp;
805 GST_OBJECT_UNLOCK (src);
811 * gst_base_src_new_seamless_segment:
813 * @start: The new start value for the segment
814 * @stop: Stop value for the new segment
815 * @time: The new time value for the start of the new segment
817 * Prepare a new seamless segment for emission downstream. This function must
818 * only be called by derived sub-classes, and only from the create() function,
819 * as the stream-lock needs to be held.
821 * The format for the new segment will be the current format of the source, as
822 * configured with gst_base_src_set_format()
824 * Returns: %TRUE if preparation of the seamless segment succeeded.
827 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
832 GST_OBJECT_LOCK (src);
834 src->segment.base = gst_segment_to_running_time (&src->segment,
835 src->segment.format, src->segment.position);
836 src->segment.position = src->segment.start = start;
837 src->segment.stop = stop;
838 src->segment.time = time;
840 /* Mark pending segment. Will be sent before next data */
841 src->priv->segment_pending = TRUE;
842 src->priv->segment_seqnum = gst_util_seqnum_next ();
844 GST_DEBUG_OBJECT (src,
845 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
846 GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
847 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
848 GST_TIME_ARGS (src->segment.base));
850 GST_OBJECT_UNLOCK (src);
852 src->priv->discont = TRUE;
859 gst_base_src_send_stream_start (GstBaseSrc * src)
863 if (src->priv->stream_start_pending) {
868 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
870 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
871 event = gst_event_new_stream_start (stream_id);
872 gst_event_set_group_id (event, gst_util_group_id_next ());
874 ret = gst_pad_push_event (src->srcpad, event);
875 src->priv->stream_start_pending = FALSE;
883 * gst_base_src_set_caps:
884 * @src: a #GstBaseSrc
885 * @caps: (transfer none): a #GstCaps
887 * Set new caps on the basesrc source pad.
889 * Returns: %TRUE if the caps could be set
892 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
894 GstBaseSrcClass *bclass;
896 GstCaps *current_caps;
898 bclass = GST_BASE_SRC_GET_CLASS (src);
900 gst_base_src_send_stream_start (src);
902 current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (src));
903 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
904 GST_DEBUG_OBJECT (src, "New caps equal to old ones: %" GST_PTR_FORMAT,
908 if (bclass->set_caps)
909 res = bclass->set_caps (src, caps);
912 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
916 gst_caps_unref (current_caps);
922 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
924 GstCaps *caps = NULL;
925 GstPadTemplate *pad_template;
926 GstBaseSrcClass *bclass;
928 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
931 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
933 if (pad_template != NULL) {
934 caps = gst_pad_template_get_caps (pad_template);
937 GstCaps *intersection;
940 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
941 gst_caps_unref (caps);
949 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
951 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
952 return gst_caps_fixate (caps);
956 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
958 GstBaseSrcClass *bclass;
960 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
963 caps = bclass->fixate (bsrc, caps);
969 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
973 switch (GST_QUERY_TYPE (query)) {
974 case GST_QUERY_POSITION:
978 gst_query_parse_position (query, &format, NULL);
980 GST_DEBUG_OBJECT (src, "position query in format %s",
981 gst_format_get_name (format));
984 case GST_FORMAT_PERCENT:
990 GST_OBJECT_LOCK (src);
991 position = src->segment.position;
992 duration = src->segment.duration;
993 GST_OBJECT_UNLOCK (src);
995 if (position != -1 && duration != -1) {
996 if (position < duration)
997 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1000 percent = GST_FORMAT_PERCENT_MAX;
1004 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1011 GstFormat seg_format;
1013 GST_OBJECT_LOCK (src);
1015 gst_segment_to_stream_time (&src->segment, src->segment.format,
1016 src->segment.position);
1017 seg_format = src->segment.format;
1018 GST_OBJECT_UNLOCK (src);
1020 if (position != -1) {
1021 /* convert to requested format */
1023 gst_pad_query_convert (src->srcpad, seg_format,
1024 position, format, &position);
1029 gst_query_set_position (query, format, position);
1036 case GST_QUERY_DURATION:
1040 gst_query_parse_duration (query, &format, NULL);
1042 GST_DEBUG_OBJECT (src, "duration query in format %s",
1043 gst_format_get_name (format));
1046 case GST_FORMAT_PERCENT:
1047 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1048 GST_FORMAT_PERCENT_MAX);
1054 GstFormat seg_format;
1057 /* may have to refresh duration */
1058 gst_base_src_update_length (src, 0, &length,
1059 g_atomic_int_get (&src->priv->dynamic_size));
1061 /* this is the duration as configured by the subclass. */
1062 GST_OBJECT_LOCK (src);
1063 duration = src->segment.duration;
1064 seg_format = src->segment.format;
1065 GST_OBJECT_UNLOCK (src);
1067 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1068 duration, gst_format_get_name (seg_format));
1070 if (duration != -1) {
1071 /* convert to requested format, if this fails, we have a duration
1072 * but we cannot answer the query, we must return FALSE. */
1074 gst_pad_query_convert (src->srcpad, seg_format,
1075 duration, format, &duration);
1077 /* The subclass did not configure a duration, we assume that the
1078 * media has an unknown duration then and we return TRUE to report
1079 * this. Note that this is not the same as returning FALSE, which
1080 * means that we cannot report the duration at all. */
1085 gst_query_set_duration (query, format, duration);
1093 case GST_QUERY_SEEKING:
1095 GstFormat format, seg_format;
1098 GST_OBJECT_LOCK (src);
1099 duration = src->segment.duration;
1100 seg_format = src->segment.format;
1101 GST_OBJECT_UNLOCK (src);
1103 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1104 if (format == seg_format) {
1105 gst_query_set_seeking (query, seg_format,
1106 gst_base_src_seekable (src), 0, duration);
1109 /* FIXME 2.0: return TRUE + seekable=FALSE for SEEKING query here */
1110 /* Don't reply to the query to make up for demuxers which don't
1111 * handle the SEEKING query yet. Players like Totem will fall back
1112 * to the duration when the SEEKING query isn't answered. */
1117 case GST_QUERY_SEGMENT:
1122 GST_OBJECT_LOCK (src);
1124 format = src->segment.format;
1127 gst_segment_to_stream_time (&src->segment, format,
1128 src->segment.start);
1129 if ((stop = src->segment.stop) == -1)
1130 stop = src->segment.duration;
1132 stop = gst_segment_to_stream_time (&src->segment, format, stop);
1134 gst_query_set_segment (query, src->segment.rate, format, start, stop);
1136 GST_OBJECT_UNLOCK (src);
1141 case GST_QUERY_FORMATS:
1143 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1144 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1148 case GST_QUERY_CONVERT:
1150 GstFormat src_fmt, dest_fmt;
1151 gint64 src_val, dest_val;
1153 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1155 /* we can only convert between equal formats... */
1156 if (src_fmt == dest_fmt) {
1162 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1165 case GST_QUERY_LATENCY:
1167 GstClockTime min, max;
1170 /* Subclasses should override and implement something useful */
1171 res = gst_base_src_query_latency (src, &live, &min, &max);
1173 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1174 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1175 GST_TIME_ARGS (max));
1177 gst_query_set_latency (query, live, min, max);
1180 case GST_QUERY_JITTER:
1181 case GST_QUERY_RATE:
1184 case GST_QUERY_BUFFERING:
1186 GstFormat format, seg_format;
1187 gint64 start, stop, estimated;
1189 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1191 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1192 gst_format_get_name (format));
1194 GST_OBJECT_LOCK (src);
1195 if (src->random_access) {
1198 if (format == GST_FORMAT_PERCENT)
1199 stop = GST_FORMAT_PERCENT_MAX;
1201 stop = src->segment.duration;
1207 seg_format = src->segment.format;
1208 GST_OBJECT_UNLOCK (src);
1210 /* convert to required format. When the conversion fails, we can't answer
1211 * the query. When the value is unknown, we can don't perform conversion
1212 * but report TRUE. */
1213 if (format != GST_FORMAT_PERCENT && stop != -1) {
1214 res = gst_pad_query_convert (src->srcpad, seg_format,
1215 stop, format, &stop);
1219 if (res && format != GST_FORMAT_PERCENT && start != -1)
1220 res = gst_pad_query_convert (src->srcpad, seg_format,
1221 start, format, &start);
1223 gst_query_set_buffering_range (query, format, start, stop, estimated);
1226 case GST_QUERY_SCHEDULING:
1228 gboolean random_access;
1230 random_access = gst_base_src_is_random_access (src);
1232 /* we can operate in getrange mode if the native format is bytes
1233 * and we are seekable, this condition is set in the random_access
1234 * flag and is set in the _start() method. */
1235 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1237 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1238 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1243 case GST_QUERY_CAPS:
1245 GstBaseSrcClass *bclass;
1246 GstCaps *caps, *filter;
1248 bclass = GST_BASE_SRC_GET_CLASS (src);
1249 if (bclass->get_caps) {
1250 gst_query_parse_caps (query, &filter);
1251 if ((caps = bclass->get_caps (src, filter))) {
1252 gst_query_set_caps_result (query, caps);
1253 gst_caps_unref (caps);
1262 case GST_QUERY_URI:{
1263 if (GST_IS_URI_HANDLER (src)) {
1264 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1267 gst_query_set_uri (query, uri);
1282 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1289 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1292 GstBaseSrcClass *bclass;
1293 gboolean result = FALSE;
1295 src = GST_BASE_SRC (parent);
1296 bclass = GST_BASE_SRC_GET_CLASS (src);
1299 result = bclass->query (src, query);
1305 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1307 gboolean res = TRUE;
1309 /* update our offset if the start/stop position was updated */
1310 if (segment->format == GST_FORMAT_BYTES) {
1311 segment->time = segment->start;
1312 } else if (segment->start == 0) {
1313 /* seek to start, we can implement a default for this. */
1317 GST_INFO_OBJECT (src, "Can't do a default seek");
1324 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1326 GstBaseSrcClass *bclass;
1327 gboolean result = FALSE;
1329 bclass = GST_BASE_SRC_GET_CLASS (src);
1331 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1333 if (bclass->do_seek)
1334 result = bclass->do_seek (src, segment);
1339 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1342 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1343 GstSegment * segment)
1345 /* By default, we try one of 2 things:
1346 * - For absolute seek positions, convert the requested position to our
1347 * configured processing format and place it in the output segment \
1348 * - For relative seek positions, convert our current (input) values to the
1349 * seek format, adjust by the relative seek offset and then convert back to
1350 * the processing format
1352 GstSeekType start_type, stop_type;
1355 GstFormat seek_format, dest_format;
1358 gboolean res = TRUE;
1360 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1361 &start_type, &start, &stop_type, &stop);
1362 dest_format = segment->format;
1364 if (seek_format == dest_format) {
1365 gst_segment_do_seek (segment, rate, seek_format, flags,
1366 start_type, start, stop_type, stop, &update);
1370 if (start_type != GST_SEEK_TYPE_NONE) {
1371 /* FIXME: Handle seek_end by converting the input segment vals */
1373 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1375 start_type = GST_SEEK_TYPE_SET;
1378 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1379 /* FIXME: Handle seek_end by converting the input segment vals */
1381 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1383 stop_type = GST_SEEK_TYPE_SET;
1386 /* And finally, configure our output segment in the desired format */
1387 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1388 stop_type, stop, &update);
1397 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1403 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1404 GstSegment * seeksegment)
1406 GstBaseSrcClass *bclass;
1407 gboolean result = FALSE;
1409 bclass = GST_BASE_SRC_GET_CLASS (src);
1411 if (bclass->prepare_seek_segment)
1412 result = bclass->prepare_seek_segment (src, event, seeksegment);
1417 static GstFlowReturn
1418 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1419 guint size, GstBuffer ** buffer)
1422 GstBaseSrcPrivate *priv = src->priv;
1423 GstBufferPool *pool = NULL;
1424 GstAllocator *allocator = NULL;
1425 GstAllocationParams params;
1427 GST_OBJECT_LOCK (src);
1429 pool = gst_object_ref (priv->pool);
1430 } else if (priv->allocator) {
1431 allocator = gst_object_ref (priv->allocator);
1433 params = priv->params;
1434 GST_OBJECT_UNLOCK (src);
1437 ret = gst_buffer_pool_acquire_buffer (pool, buffer, NULL);
1438 } else if (size != -1) {
1439 *buffer = gst_buffer_new_allocate (allocator, size, ¶ms);
1440 if (G_UNLIKELY (*buffer == NULL))
1445 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1452 gst_object_unref (pool);
1454 gst_object_unref (allocator);
1461 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1462 ret = GST_FLOW_ERROR;
1467 static GstFlowReturn
1468 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1469 guint size, GstBuffer ** buffer)
1471 GstBaseSrcClass *bclass;
1475 bclass = GST_BASE_SRC_GET_CLASS (src);
1477 if (G_UNLIKELY (!bclass->alloc))
1479 if (G_UNLIKELY (!bclass->fill))
1482 if (*buffer == NULL) {
1483 /* downstream did not provide us with a buffer to fill, allocate one
1485 ret = bclass->alloc (src, offset, size, &res_buf);
1486 if (G_UNLIKELY (ret != GST_FLOW_OK))
1492 if (G_LIKELY (size > 0)) {
1493 /* only call fill when there is a size */
1494 ret = bclass->fill (src, offset, size, res_buf);
1495 if (G_UNLIKELY (ret != GST_FLOW_OK))
1506 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1507 return GST_FLOW_NOT_SUPPORTED;
1511 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1516 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1517 gst_flow_get_name (ret));
1518 if (*buffer == NULL)
1519 gst_buffer_unref (res_buf);
1524 /* this code implements the seeking. It is a good example
1525 * handling all cases.
1527 * A seek updates the currently configured segment.start
1528 * and segment.stop values based on the SEEK_TYPE. If the
1529 * segment.start value is updated, a seek to this new position
1530 * should be performed.
1532 * The seek can only be executed when we are not currently
1533 * streaming any data, to make sure that this is the case, we
1534 * acquire the STREAM_LOCK which is taken when we are in the
1535 * _loop() function or when a getrange() is called. Normally
1536 * we will not receive a seek if we are operating in pull mode
1537 * though. When we operate as a live source we might block on the live
1538 * cond, which does not release the STREAM_LOCK. Therefore we will try
1539 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1540 * safe to perform the seek.
1542 * When we are in the loop() function, we might be in the middle
1543 * of pushing a buffer, which might block in a sink. To make sure
1544 * that the push gets unblocked we push out a FLUSH_START event.
1545 * Our loop function will get a FLUSHING return value from
1546 * the push and will pause, effectively releasing the STREAM_LOCK.
1548 * For a non-flushing seek, we pause the task, which might eventually
1549 * release the STREAM_LOCK. We say eventually because when the sink
1550 * blocks on the sample we might wait a very long time until the sink
1551 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1552 * can continue the seek. A non-flushing seek is normally done in a
1553 * running pipeline to perform seamless playback, this means that the sink is
1554 * PLAYING and will return from its chain function.
1555 * In the case of a non-flushing seek we need to make sure that the
1556 * data we output after the seek is continuous with the previous data,
1557 * this is because a non-flushing seek does not reset the running-time
1558 * to 0. We do this by closing the currently running segment, ie. sending
1559 * a new_segment event with the stop position set to the last processed
1562 * After updating the segment.start/stop values, we prepare for
1563 * streaming again. We push out a FLUSH_STOP to make the peer pad
1564 * accept data again and we start our task again.
1566 * A segment seek posts a message on the bus saying that the playback
1567 * of the segment started. We store the segment flag internally because
1568 * when we reach the segment.stop we have to post a segment.done
1569 * instead of EOS when doing a segment seek.
1572 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1574 gboolean res = TRUE, tres;
1576 GstFormat seek_format, dest_format;
1578 GstSeekType start_type, stop_type;
1580 gboolean flush, playing;
1582 gboolean relative_seek = FALSE;
1583 gboolean seekseg_configured = FALSE;
1584 GstSegment seeksegment;
1588 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1590 GST_OBJECT_LOCK (src);
1591 dest_format = src->segment.format;
1592 GST_OBJECT_UNLOCK (src);
1595 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1596 &start_type, &start, &stop_type, &stop);
1598 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1599 SEEK_TYPE_IS_RELATIVE (stop_type);
1601 if (dest_format != seek_format && !relative_seek) {
1602 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1603 * here before taking the stream lock, otherwise we must convert it later,
1604 * once we have the stream lock and can read the last configures segment
1605 * start and stop positions */
1606 gst_segment_init (&seeksegment, dest_format);
1608 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1609 goto prepare_failed;
1611 seekseg_configured = TRUE;
1614 flush = flags & GST_SEEK_FLAG_FLUSH;
1615 seqnum = gst_event_get_seqnum (event);
1618 /* get next seqnum */
1619 seqnum = gst_util_seqnum_next ();
1622 /* send flush start */
1624 tevent = gst_event_new_flush_start ();
1625 gst_event_set_seqnum (tevent, seqnum);
1626 gst_pad_push_event (src->srcpad, tevent);
1628 gst_pad_pause_task (src->srcpad);
1630 /* unblock streaming thread. */
1632 gst_base_src_set_flushing (src, TRUE, FALSE, &playing);
1634 /* grab streaming lock, this should eventually be possible, either
1635 * because the task is paused, our streaming thread stopped
1636 * or because our peer is flushing. */
1637 GST_PAD_STREAM_LOCK (src->srcpad);
1638 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1639 /* we have seen this event before, issue a warning for now */
1640 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1643 src->priv->seqnum = seqnum;
1644 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1648 gst_base_src_set_flushing (src, FALSE, playing, NULL);
1650 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1651 * copy the current segment info into the temp segment that we can actually
1652 * attempt the seek with. We only update the real segment if the seek succeeds. */
1653 if (!seekseg_configured) {
1654 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1656 /* now configure the final seek segment */
1658 if (seeksegment.format != seek_format) {
1659 /* OK, here's where we give the subclass a chance to convert the relative
1660 * seek into an absolute one in the processing format. We set up any
1661 * absolute seek above, before taking the stream lock. */
1662 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1663 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1668 /* The seek format matches our processing format, no need to ask the
1669 * the subclass to configure the segment. */
1670 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1671 start_type, start, stop_type, stop, &update);
1674 /* Else, no seek event passed, so we're just (re)starting the
1679 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1680 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1681 seeksegment.start, seeksegment.stop, seeksegment.position);
1683 /* do the seek, segment.position contains the new position. */
1684 res = gst_base_src_do_seek (src, &seeksegment);
1687 /* and prepare to continue streaming */
1689 tevent = gst_event_new_flush_stop (TRUE);
1690 gst_event_set_seqnum (tevent, seqnum);
1691 /* send flush stop, peer will accept data and events again. We
1692 * are not yet providing data as we still have the STREAM_LOCK. */
1693 gst_pad_push_event (src->srcpad, tevent);
1696 /* The subclass must have converted the segment to the processing format
1698 if (res && seeksegment.format != dest_format) {
1699 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1700 "in the correct format. Aborting seek.");
1704 /* if the seek was successful, we update our real segment and push
1705 * out the new segment. */
1707 GST_OBJECT_LOCK (src);
1708 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1709 GST_OBJECT_UNLOCK (src);
1711 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1712 GstMessage *message;
1714 message = gst_message_new_segment_start (GST_OBJECT (src),
1715 seeksegment.format, seeksegment.position);
1716 gst_message_set_seqnum (message, seqnum);
1718 gst_element_post_message (GST_ELEMENT (src), message);
1721 src->priv->segment_pending = TRUE;
1722 src->priv->segment_seqnum = seqnum;
1725 src->priv->discont = TRUE;
1726 src->running = TRUE;
1727 /* and restart the task in case it got paused explicitly or by
1728 * the FLUSH_START event we pushed out. */
1729 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1734 /* and release the lock again so we can continue streaming */
1735 GST_PAD_STREAM_UNLOCK (src->srcpad);
1741 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1746 /* all events send to this element directly. This is mainly done from the
1750 gst_base_src_send_event (GstElement * element, GstEvent * event)
1753 gboolean result = FALSE;
1754 GstBaseSrcClass *bclass;
1756 src = GST_BASE_SRC (element);
1757 bclass = GST_BASE_SRC_GET_CLASS (src);
1759 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1761 switch (GST_EVENT_TYPE (event)) {
1762 /* bidirectional events */
1763 case GST_EVENT_FLUSH_START:
1764 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1765 result = gst_pad_push_event (src->srcpad, event);
1766 /* also unblock the create function */
1767 gst_base_src_activate_pool (src, FALSE);
1768 /* unlock any subclasses, we need to do this before grabbing the
1769 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
1770 * unlock to the params because of backwards compat (see seek handler)*/
1772 bclass->unlock (src);
1774 /* the live lock is released when we are blocked, waiting for playing or
1775 * when we sync to the clock. */
1776 GST_LIVE_LOCK (src);
1777 src->priv->flushing = TRUE;
1778 /* clear pending EOS if any */
1779 if (g_atomic_int_get (&src->priv->has_pending_eos)) {
1780 GST_OBJECT_LOCK (src);
1781 CLEAR_PENDING_EOS (src);
1782 src->priv->forced_eos = FALSE;
1783 GST_OBJECT_UNLOCK (src);
1785 if (bclass->unlock_stop)
1786 bclass->unlock_stop (src);
1788 gst_clock_id_unschedule (src->clock_id);
1789 GST_DEBUG_OBJECT (src, "signal");
1790 GST_LIVE_SIGNAL (src);
1791 GST_LIVE_UNLOCK (src);
1794 case GST_EVENT_FLUSH_STOP:
1798 GST_LIVE_LOCK (src);
1799 src->priv->segment_pending = TRUE;
1800 src->priv->flushing = FALSE;
1801 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1802 result = gst_pad_push_event (src->srcpad, event);
1804 gst_base_src_activate_pool (src, TRUE);
1806 GST_OBJECT_LOCK (src->srcpad);
1807 start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1808 GST_OBJECT_UNLOCK (src->srcpad);
1811 if (!src->live_running)
1816 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1818 GST_LIVE_UNLOCK (src);
1823 /* downstream serialized events */
1826 /* queue EOS and make sure the task or pull function performs the EOS
1829 * We have two possibilities:
1831 * - Before we are to enter the _create function, we check the has_pending_eos
1832 * first and do EOS instead of entering it.
1833 * - If we are in the _create function or we did not manage to set the
1834 * flag fast enough and we are about to enter the _create function,
1835 * we unlock it so that we exit with FLUSHING immediately. We then
1836 * check the EOS flag and do the EOS logic.
1838 GST_OBJECT_LOCK (src);
1839 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1840 if (src->priv->pending_eos)
1841 gst_event_unref (src->priv->pending_eos);
1842 src->priv->pending_eos = event;
1844 GST_OBJECT_UNLOCK (src);
1846 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1848 /* unlock the _create function so that we can check the has_pending_eos flag
1849 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1850 * that we can grab it and stop the unlock again. We don't take the stream
1851 * lock so that this operation is guaranteed to never block. */
1852 gst_base_src_activate_pool (src, FALSE);
1854 bclass->unlock (src);
1856 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1858 GST_LIVE_LOCK (src);
1859 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1860 /* now stop the unlock of the streaming thread again. Grabbing the live
1861 * lock is enough because that protects the create function. */
1862 if (bclass->unlock_stop)
1863 bclass->unlock_stop (src);
1864 gst_base_src_activate_pool (src, TRUE);
1865 GST_LIVE_UNLOCK (src);
1870 case GST_EVENT_SEGMENT:
1871 /* sending random SEGMENT downstream can break sync. */
1874 case GST_EVENT_CUSTOM_DOWNSTREAM:
1875 case GST_EVENT_CUSTOM_BOTH:
1876 case GST_EVENT_PROTECTION:
1877 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH, PROTECTION in the dataflow */
1878 GST_OBJECT_LOCK (src);
1879 src->priv->pending_events =
1880 g_list_append (src->priv->pending_events, event);
1881 g_atomic_int_set (&src->priv->have_events, TRUE);
1882 GST_OBJECT_UNLOCK (src);
1886 case GST_EVENT_BUFFERSIZE:
1887 /* does not seem to make much sense currently */
1890 /* upstream events */
1892 /* elements should override send_event and do something */
1894 case GST_EVENT_SEEK:
1898 GST_OBJECT_LOCK (src->srcpad);
1899 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1901 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1902 GST_OBJECT_UNLOCK (src->srcpad);
1905 GST_DEBUG_OBJECT (src, "performing seek");
1906 /* when we are running in push mode, we can execute the
1907 * seek right now. */
1908 result = gst_base_src_perform_seek (src, event, TRUE);
1912 /* else we store the event and execute the seek when we
1914 GST_OBJECT_LOCK (src);
1915 GST_DEBUG_OBJECT (src, "queueing seek");
1916 event_p = &src->pending_seek;
1917 gst_event_replace ((GstEvent **) event_p, event);
1918 GST_OBJECT_UNLOCK (src);
1919 /* assume the seek will work */
1924 case GST_EVENT_NAVIGATION:
1925 /* could make sense for elements that do something with navigation events
1926 * but then they would need to override the send_event function */
1928 case GST_EVENT_LATENCY:
1929 /* does not seem to make sense currently */
1933 case GST_EVENT_CUSTOM_UPSTREAM:
1934 /* override send_event if you want this */
1936 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1937 case GST_EVENT_CUSTOM_BOTH_OOB:
1938 /* insert a random custom event into the pipeline */
1939 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1940 result = gst_pad_push_event (src->srcpad, event);
1941 /* we gave away the ref to the event in the push */
1948 /* if we still have a ref to the event, unref it now */
1950 gst_event_unref (event);
1957 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1958 GST_OBJECT_UNLOCK (src->srcpad);
1965 gst_base_src_seekable (GstBaseSrc * src)
1967 GstBaseSrcClass *bclass;
1968 bclass = GST_BASE_SRC_GET_CLASS (src);
1969 if (bclass->is_seekable)
1970 return bclass->is_seekable (src);
1976 gst_base_src_update_qos (GstBaseSrc * src,
1977 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1979 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1980 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1981 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1983 GST_OBJECT_LOCK (src);
1984 src->priv->proportion = proportion;
1985 src->priv->earliest_time = timestamp + diff;
1986 GST_OBJECT_UNLOCK (src);
1991 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1995 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1997 switch (GST_EVENT_TYPE (event)) {
1998 case GST_EVENT_SEEK:
1999 /* is normally called when in push mode */
2000 if (!gst_base_src_seekable (src))
2003 result = gst_base_src_perform_seek (src, event, TRUE);
2005 case GST_EVENT_FLUSH_START:
2006 /* cancel any blocking getrange, is normally called
2007 * when in pull mode. */
2008 result = gst_base_src_set_flushing (src, TRUE, FALSE, NULL);
2010 case GST_EVENT_FLUSH_STOP:
2011 result = gst_base_src_set_flushing (src, FALSE, TRUE, NULL);
2016 GstClockTimeDiff diff;
2017 GstClockTime timestamp;
2019 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2020 gst_base_src_update_qos (src, proportion, diff, timestamp);
2024 case GST_EVENT_RECONFIGURE:
2027 case GST_EVENT_LATENCY:
2039 GST_DEBUG_OBJECT (src, "is not seekable");
2045 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2048 GstBaseSrcClass *bclass;
2049 gboolean result = FALSE;
2051 src = GST_BASE_SRC (parent);
2052 bclass = GST_BASE_SRC_GET_CLASS (src);
2054 if (bclass->event) {
2055 if (!(result = bclass->event (src, event)))
2056 goto subclass_failed;
2060 gst_event_unref (event);
2067 GST_DEBUG_OBJECT (src, "subclass refused event");
2073 gst_base_src_set_property (GObject * object, guint prop_id,
2074 const GValue * value, GParamSpec * pspec)
2078 src = GST_BASE_SRC (object);
2081 case PROP_BLOCKSIZE:
2082 gst_base_src_set_blocksize (src, g_value_get_uint (value));
2084 case PROP_NUM_BUFFERS:
2085 src->num_buffers = g_value_get_int (value);
2088 src->typefind = g_value_get_boolean (value);
2090 case PROP_DO_TIMESTAMP:
2091 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2094 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2100 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2105 src = GST_BASE_SRC (object);
2108 case PROP_BLOCKSIZE:
2109 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2111 case PROP_NUM_BUFFERS:
2112 g_value_set_int (value, src->num_buffers);
2115 g_value_set_boolean (value, src->typefind);
2117 case PROP_DO_TIMESTAMP:
2118 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2126 /* with STREAM_LOCK and LOCK */
2127 static GstClockReturn
2128 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2133 id = gst_clock_new_single_shot_id (clock, time);
2135 basesrc->clock_id = id;
2136 /* release the live lock while waiting */
2137 GST_LIVE_UNLOCK (basesrc);
2139 ret = gst_clock_id_wait (id, NULL);
2141 GST_LIVE_LOCK (basesrc);
2142 gst_clock_id_unref (id);
2143 basesrc->clock_id = NULL;
2148 /* perform synchronisation on a buffer.
2151 static GstClockReturn
2152 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2154 GstClockReturn result;
2155 GstClockTime start, end;
2156 GstBaseSrcClass *bclass;
2157 GstClockTime base_time;
2159 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2160 gboolean do_timestamp, first, pseudo_live, is_live;
2162 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2165 if (bclass->get_times)
2166 bclass->get_times (basesrc, buffer, &start, &end);
2168 /* get buffer timestamp */
2169 dts = GST_BUFFER_DTS (buffer);
2170 pts = GST_BUFFER_PTS (buffer);
2172 if (GST_CLOCK_TIME_IS_VALID (dts))
2177 /* grab the lock to prepare for clocking and calculate the startup
2179 GST_OBJECT_LOCK (basesrc);
2181 is_live = basesrc->is_live;
2182 /* if we are asked to sync against the clock we are a pseudo live element */
2183 pseudo_live = (start != -1 && is_live);
2184 /* check for the first buffer */
2185 first = (basesrc->priv->latency == -1);
2187 if (timestamp != -1 && pseudo_live) {
2188 GstClockTime latency;
2190 /* we have a timestamp and a sync time, latency is the diff */
2191 if (timestamp <= start)
2192 latency = start - timestamp;
2197 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2198 GST_TIME_ARGS (latency));
2199 /* first time we calculate latency, just configure */
2200 basesrc->priv->latency = latency;
2202 if (basesrc->priv->latency != latency) {
2203 /* we have a new latency, FIXME post latency message */
2204 basesrc->priv->latency = latency;
2205 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2206 GST_TIME_ARGS (latency));
2210 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2211 is_live, start != -1);
2212 basesrc->priv->latency = 0;
2215 /* get clock, if no clock, we can't sync or do timestamps */
2216 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2219 gst_object_ref (clock);
2221 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2223 do_timestamp = basesrc->priv->do_timestamp;
2224 GST_OBJECT_UNLOCK (basesrc);
2226 /* first buffer, calculate the timestamp offset */
2228 GstClockTime running_time;
2230 now = gst_clock_get_time (clock);
2231 running_time = now - base_time;
2233 GST_LOG_OBJECT (basesrc,
2234 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2235 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2236 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2238 if (pseudo_live && timestamp != -1) {
2239 /* live source and we need to sync, add startup latency to all timestamps
2240 * to get the real running_time. Live sources should always timestamp
2241 * according to the current running time. */
2242 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2244 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2245 GST_TIME_ARGS (basesrc->priv->ts_offset));
2247 basesrc->priv->ts_offset = 0;
2248 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2251 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2254 } else if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2255 if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2256 dts = basesrc->segment.start;
2261 GST_BUFFER_DTS (buffer) = dts;
2263 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2264 GST_TIME_ARGS (dts));
2267 /* not the first buffer, the timestamp is the diff between the clock and
2269 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2270 now = gst_clock_get_time (clock);
2272 dts = now - base_time;
2273 GST_BUFFER_DTS (buffer) = dts;
2275 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2276 GST_TIME_ARGS (dts));
2279 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2280 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2283 GST_BUFFER_PTS (buffer) = dts;
2285 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2286 GST_TIME_ARGS (pts));
2289 /* if we don't have a buffer timestamp, we don't sync */
2290 if (!GST_CLOCK_TIME_IS_VALID (start))
2294 /* for pseudo live sources, add our ts_offset to the timestamp */
2295 if (GST_CLOCK_TIME_IS_VALID (pts))
2296 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2297 if (GST_CLOCK_TIME_IS_VALID (dts))
2298 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2299 start += basesrc->priv->ts_offset;
2302 GST_LOG_OBJECT (basesrc,
2303 "waiting for clock, base time %" GST_TIME_FORMAT
2304 ", stream_start %" GST_TIME_FORMAT,
2305 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2307 result = gst_base_src_wait (basesrc, clock, start + base_time);
2309 gst_object_unref (clock);
2311 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2318 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2319 GST_OBJECT_UNLOCK (basesrc);
2320 return GST_CLOCK_OK;
2324 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2325 gst_object_unref (clock);
2326 return GST_CLOCK_OK;
2330 /* Called with STREAM_LOCK and LIVE_LOCK */
2332 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2335 guint64 size, maxsize;
2336 GstBaseSrcClass *bclass;
2339 /* only operate if we are working with bytes */
2340 if (src->segment.format != GST_FORMAT_BYTES)
2343 bclass = GST_BASE_SRC_GET_CLASS (src);
2345 stop = src->segment.stop;
2346 /* get total file size */
2347 size = src->segment.duration;
2349 /* when not doing automatic EOS, just use the stop position. We don't use
2350 * the size to check for EOS */
2351 if (!g_atomic_int_get (&src->priv->automatic_eos))
2353 /* Otherwise, the max amount of bytes to read is the total
2354 * size or up to the segment.stop if present. */
2355 else if (stop != -1)
2356 maxsize = size != -1 ? MIN (size, stop) : stop;
2360 GST_DEBUG_OBJECT (src,
2361 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2362 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2363 *length, size, stop, maxsize);
2365 /* check size if we have one */
2366 if (maxsize != -1) {
2367 /* if we run past the end, check if the file became bigger and
2368 * retry. Mind wrap when checking. */
2369 if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2370 /* see if length of the file changed */
2371 if (bclass->get_size)
2372 if (!bclass->get_size (src, &size))
2375 /* when not doing automatic EOS, just use the stop position. We don't use
2376 * the size to check for EOS */
2377 if (!g_atomic_int_get (&src->priv->automatic_eos))
2379 /* Otherwise, the max amount of bytes to read is the total
2380 * size or up to the segment.stop if present. */
2381 else if (stop != -1)
2382 maxsize = size != -1 ? MIN (size, stop) : stop;
2386 if (maxsize != -1) {
2387 /* if we are at or past the end, EOS */
2388 if (G_UNLIKELY (offset >= maxsize))
2389 goto unexpected_length;
2391 /* else we can clip to the end */
2392 if (G_UNLIKELY (offset + *length >= maxsize))
2393 *length = maxsize - offset;
2398 /* keep track of current duration. segment is in bytes, we checked
2400 GST_OBJECT_LOCK (src);
2401 src->segment.duration = size;
2402 GST_OBJECT_UNLOCK (src);
2409 GST_WARNING_OBJECT (src, "processing at or past EOS");
2414 /* must be called with LIVE_LOCK */
2415 static GstFlowReturn
2416 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2420 GstBaseSrcClass *bclass;
2421 GstClockReturn status;
2425 bclass = GST_BASE_SRC_GET_CLASS (src);
2429 if (G_UNLIKELY (!src->live_running)) {
2430 ret = gst_base_src_wait_playing (src);
2431 if (ret != GST_FLOW_OK)
2436 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2437 && !GST_BASE_SRC_IS_STARTING (src)))
2440 if (G_UNLIKELY (!bclass->create))
2443 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2444 goto unexpected_length;
2446 /* track position */
2447 GST_OBJECT_LOCK (src);
2448 if (src->segment.format == GST_FORMAT_BYTES)
2449 src->segment.position = offset;
2450 GST_OBJECT_UNLOCK (src);
2452 /* normally we don't count buffers */
2453 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2454 if (src->num_buffers_left == 0)
2455 goto reached_num_buffers;
2457 src->num_buffers_left--;
2460 /* don't enter the create function if a pending EOS event was set. For the
2461 * logic of the has_pending_eos, check the event function of this class. */
2462 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2463 src->priv->forced_eos = TRUE;
2467 GST_DEBUG_OBJECT (src,
2468 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2469 G_GINT64_FORMAT, offset, length, src->segment.time);
2471 res_buf = in_buf = *buf;
2473 ret = bclass->create (src, offset, length, &res_buf);
2475 /* The create function could be unlocked because we have a pending EOS. It's
2476 * possible that we have a valid buffer from create that we need to
2477 * discard when the create function returned _OK. */
2478 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2479 if (ret == GST_FLOW_OK) {
2481 gst_buffer_unref (res_buf);
2483 src->priv->forced_eos = TRUE;
2487 if (G_UNLIKELY (ret != GST_FLOW_OK))
2490 /* fallback in case the create function didn't fill a provided buffer */
2491 if (in_buf != NULL && res_buf != in_buf) {
2495 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2496 "fill the provided buffer, copying");
2498 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2501 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2502 gst_buffer_unmap (in_buf, &info);
2503 gst_buffer_set_size (in_buf, copied_size);
2505 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2507 gst_buffer_unref (res_buf);
2511 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2512 if (offset == 0 && src->segment.time == 0
2513 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2514 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2515 res_buf = gst_buffer_make_writable (res_buf);
2516 GST_BUFFER_DTS (res_buf) = 0;
2519 /* now sync before pushing the buffer */
2520 status = gst_base_src_do_sync (src, res_buf);
2522 /* waiting for the clock could have made us flushing */
2523 if (G_UNLIKELY (src->priv->flushing))
2527 case GST_CLOCK_EARLY:
2528 /* the buffer is too late. We currently don't drop the buffer. */
2529 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2532 /* buffer synchronised properly */
2533 GST_DEBUG_OBJECT (src, "buffer ok");
2535 case GST_CLOCK_UNSCHEDULED:
2536 /* this case is triggered when we were waiting for the clock and
2537 * it got unlocked because we did a state change. In any case, get rid of
2540 gst_buffer_unref (res_buf);
2542 if (!src->live_running) {
2543 /* We return FLUSHING when we are not running to stop the dataflow also
2544 * get rid of the produced buffer. */
2545 GST_DEBUG_OBJECT (src,
2546 "clock was unscheduled (%d), returning FLUSHING", status);
2547 ret = GST_FLOW_FLUSHING;
2549 /* If we are running when this happens, we quickly switched between
2550 * pause and playing. We try to produce a new buffer */
2551 GST_DEBUG_OBJECT (src,
2552 "clock was unscheduled (%d), but we are running", status);
2557 /* all other result values are unexpected and errors */
2558 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2559 (_("Internal clock error.")),
2560 ("clock returned unexpected return value %d", status));
2562 gst_buffer_unref (res_buf);
2563 ret = GST_FLOW_ERROR;
2566 if (G_LIKELY (ret == GST_FLOW_OK))
2574 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2575 gst_flow_get_name (ret));
2580 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2581 gst_flow_get_name (ret));
2586 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2587 (_("Failed to map buffer.")),
2588 ("failed to map result buffer in WRITE mode"));
2590 gst_buffer_unref (res_buf);
2591 return GST_FLOW_ERROR;
2595 GST_DEBUG_OBJECT (src, "getrange but not started");
2596 return GST_FLOW_FLUSHING;
2600 GST_DEBUG_OBJECT (src, "no create function");
2601 return GST_FLOW_NOT_SUPPORTED;
2605 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2606 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2607 return GST_FLOW_EOS;
2609 reached_num_buffers:
2611 GST_DEBUG_OBJECT (src, "sent all buffers");
2612 return GST_FLOW_EOS;
2616 GST_DEBUG_OBJECT (src, "we are flushing");
2618 gst_buffer_unref (res_buf);
2619 return GST_FLOW_FLUSHING;
2623 GST_DEBUG_OBJECT (src, "we are EOS");
2624 return GST_FLOW_EOS;
2628 static GstFlowReturn
2629 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2630 guint length, GstBuffer ** buf)
2635 src = GST_BASE_SRC_CAST (parent);
2637 GST_LIVE_LOCK (src);
2638 if (G_UNLIKELY (src->priv->flushing))
2641 res = gst_base_src_get_range (src, offset, length, buf);
2644 GST_LIVE_UNLOCK (src);
2651 GST_DEBUG_OBJECT (src, "we are flushing");
2652 res = GST_FLOW_FLUSHING;
2658 gst_base_src_is_random_access (GstBaseSrc * src)
2660 /* we need to start the basesrc to check random access */
2661 if (!GST_BASE_SRC_IS_STARTED (src)) {
2662 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2663 if (G_LIKELY (gst_base_src_start (src))) {
2664 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2666 gst_base_src_stop (src);
2670 return src->random_access;
2675 GST_DEBUG_OBJECT (src, "failed to start");
2681 gst_base_src_loop (GstPad * pad)
2684 GstBuffer *buf = NULL;
2689 GList *pending_events = NULL, *tmp;
2693 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2695 /* Just leave immediately if we're flushing */
2696 GST_LIVE_LOCK (src);
2697 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2699 GST_LIVE_UNLOCK (src);
2701 gst_base_src_send_stream_start (src);
2703 /* The stream-start event could've caused something to flush us */
2704 GST_LIVE_LOCK (src);
2705 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2707 GST_LIVE_UNLOCK (src);
2709 /* check if we need to renegotiate */
2710 if (gst_pad_check_reconfigure (pad)) {
2711 if (!gst_base_src_negotiate (src)) {
2712 gst_pad_mark_reconfigure (pad);
2713 if (GST_PAD_IS_FLUSHING (pad)) {
2714 GST_LIVE_LOCK (src);
2717 goto negotiate_failed;
2722 GST_LIVE_LOCK (src);
2724 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2727 blocksize = src->blocksize;
2729 /* if we operate in bytes, we can calculate an offset */
2730 if (src->segment.format == GST_FORMAT_BYTES) {
2731 position = src->segment.position;
2732 /* for negative rates, start with subtracting the blocksize */
2733 if (src->segment.rate < 0.0) {
2734 /* we cannot go below segment.start */
2735 if (position > src->segment.start + blocksize)
2736 position -= blocksize;
2738 /* last block, remainder up to segment.start */
2739 blocksize = position - src->segment.start;
2740 position = src->segment.start;
2746 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2747 GST_TIME_ARGS (position), blocksize);
2749 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2750 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2751 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2752 gst_flow_get_name (ret));
2753 GST_LIVE_UNLOCK (src);
2756 /* this should not happen */
2757 if (G_UNLIKELY (buf == NULL))
2760 /* push events to close/start our segment before we push the buffer. */
2761 if (G_UNLIKELY (src->priv->segment_pending)) {
2762 GstEvent *seg_event = gst_event_new_segment (&src->segment);
2764 gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2765 src->priv->segment_seqnum = gst_util_seqnum_next ();
2766 gst_pad_push_event (pad, seg_event);
2767 src->priv->segment_pending = FALSE;
2770 if (g_atomic_int_get (&src->priv->have_events)) {
2771 GST_OBJECT_LOCK (src);
2772 /* take the events */
2773 pending_events = src->priv->pending_events;
2774 src->priv->pending_events = NULL;
2775 g_atomic_int_set (&src->priv->have_events, FALSE);
2776 GST_OBJECT_UNLOCK (src);
2779 /* Push out pending events if any */
2780 if (G_UNLIKELY (pending_events != NULL)) {
2781 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2782 GstEvent *ev = (GstEvent *) tmp->data;
2783 gst_pad_push_event (pad, ev);
2785 g_list_free (pending_events);
2788 /* figure out the new position */
2789 switch (src->segment.format) {
2790 case GST_FORMAT_BYTES:
2792 guint bufsize = gst_buffer_get_size (buf);
2794 /* we subtracted above for negative rates */
2795 if (src->segment.rate >= 0.0)
2796 position += bufsize;
2799 case GST_FORMAT_TIME:
2801 GstClockTime start, duration;
2803 start = GST_BUFFER_TIMESTAMP (buf);
2804 duration = GST_BUFFER_DURATION (buf);
2806 if (GST_CLOCK_TIME_IS_VALID (start))
2809 position = src->segment.position;
2811 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2812 if (src->segment.rate >= 0.0)
2813 position += duration;
2814 else if (position > duration)
2815 position -= duration;
2821 case GST_FORMAT_DEFAULT:
2822 if (src->segment.rate >= 0.0)
2823 position = GST_BUFFER_OFFSET_END (buf);
2825 position = GST_BUFFER_OFFSET (buf);
2831 if (position != -1) {
2832 if (src->segment.rate >= 0.0) {
2833 /* positive rate, check if we reached the stop */
2834 if (src->segment.stop != -1) {
2835 if (position >= src->segment.stop) {
2837 position = src->segment.stop;
2841 /* negative rate, check if we reached the start. start is always set to
2842 * something different from -1 */
2843 if (position <= src->segment.start) {
2845 position = src->segment.start;
2847 /* when going reverse, all buffers are DISCONT */
2848 src->priv->discont = TRUE;
2850 GST_OBJECT_LOCK (src);
2851 src->segment.position = position;
2852 GST_OBJECT_UNLOCK (src);
2855 if (G_UNLIKELY (src->priv->discont)) {
2856 GST_INFO_OBJECT (src, "marking pending DISCONT");
2857 buf = gst_buffer_make_writable (buf);
2858 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2859 src->priv->discont = FALSE;
2861 GST_LIVE_UNLOCK (src);
2863 ret = gst_pad_push (pad, buf);
2864 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2865 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2866 goto not_negotiated;
2868 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2869 gst_flow_get_name (ret));
2873 /* Segment pending means that a new segment was configured
2874 * during this loop run */
2875 if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2876 GST_INFO_OBJECT (src, "pausing after end of segment");
2887 if (gst_pad_needs_reconfigure (pad)) {
2888 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2891 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2892 * a pending negotiation request on our srcpad */
2896 GST_DEBUG_OBJECT (src, "Not negotiated");
2897 ret = GST_FLOW_NOT_NEGOTIATED;
2902 GST_DEBUG_OBJECT (src, "we are flushing");
2903 GST_LIVE_UNLOCK (src);
2904 ret = GST_FLOW_FLUSHING;
2909 const gchar *reason = gst_flow_get_name (ret);
2912 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2913 src->running = FALSE;
2914 gst_pad_pause_task (pad);
2915 if (ret == GST_FLOW_EOS) {
2916 gboolean flag_segment;
2920 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2921 format = src->segment.format;
2922 position = src->segment.position;
2924 /* perform EOS logic */
2925 if (src->priv->forced_eos) {
2926 g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
2927 GST_OBJECT_LOCK (src);
2928 event = src->priv->pending_eos;
2929 src->priv->pending_eos = NULL;
2930 GST_OBJECT_UNLOCK (src);
2932 } else if (flag_segment) {
2933 GstMessage *message;
2935 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2937 gst_message_set_seqnum (message, src->priv->seqnum);
2938 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2939 event = gst_event_new_segment_done (format, position);
2940 gst_event_set_seqnum (event, src->priv->seqnum);
2943 event = gst_event_new_eos ();
2944 gst_event_set_seqnum (event, src->priv->seqnum);
2947 gst_pad_push_event (pad, event);
2948 src->priv->forced_eos = FALSE;
2950 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2951 event = gst_event_new_eos ();
2952 gst_event_set_seqnum (event, src->priv->seqnum);
2953 /* for fatal errors we post an error message, post the error
2954 * first so the app knows about the error first.
2955 * Also don't do this for FLUSHING because it happens
2956 * due to flushing and posting an error message because of
2957 * that is the wrong thing to do, e.g. when we're doing
2958 * a flushing seek. */
2959 GST_ELEMENT_FLOW_ERROR (src, ret);
2960 gst_pad_push_event (pad, event);
2966 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2967 (_("Internal data flow error.")), ("element returned NULL buffer"));
2968 GST_LIVE_UNLOCK (src);
2974 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2975 GstAllocator * allocator, GstAllocationParams * params)
2977 GstAllocator *oldalloc;
2978 GstBufferPool *oldpool;
2979 GstBaseSrcPrivate *priv = basesrc->priv;
2982 GST_DEBUG_OBJECT (basesrc, "activate pool");
2983 if (!gst_buffer_pool_set_active (pool, TRUE))
2984 goto activate_failed;
2987 GST_OBJECT_LOCK (basesrc);
2988 oldpool = priv->pool;
2991 oldalloc = priv->allocator;
2992 priv->allocator = allocator;
2995 gst_object_ref (priv->pool);
2996 if (priv->allocator)
2997 gst_object_ref (priv->allocator);
3000 priv->params = *params;
3002 gst_allocation_params_init (&priv->params);
3003 GST_OBJECT_UNLOCK (basesrc);
3006 /* only deactivate if the pool is not the one we're using */
3007 if (oldpool != pool) {
3008 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
3009 gst_buffer_pool_set_active (oldpool, FALSE);
3011 gst_object_unref (oldpool);
3014 gst_object_unref (oldalloc);
3021 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
3027 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
3029 GstBaseSrcPrivate *priv = basesrc->priv;
3030 GstBufferPool *pool;
3031 gboolean res = TRUE;
3033 GST_OBJECT_LOCK (basesrc);
3034 if ((pool = priv->pool))
3035 pool = gst_object_ref (pool);
3036 GST_OBJECT_UNLOCK (basesrc);
3039 res = gst_buffer_pool_set_active (pool, active);
3040 gst_object_unref (pool);
3047 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3050 GstBufferPool *pool;
3051 guint size, min, max;
3052 GstAllocator *allocator;
3053 GstAllocationParams params;
3054 GstStructure *config;
3055 gboolean update_allocator;
3057 gst_query_parse_allocation (query, &outcaps, NULL);
3059 /* we got configuration from our peer or the decide_allocation method,
3061 if (gst_query_get_n_allocation_params (query) > 0) {
3062 /* try the allocator */
3063 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3064 update_allocator = TRUE;
3067 gst_allocation_params_init (¶ms);
3068 update_allocator = FALSE;
3071 if (gst_query_get_n_allocation_pools (query) > 0) {
3072 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3075 /* no pool, we can make our own */
3076 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3077 pool = gst_buffer_pool_new ();
3081 size = min = max = 0;
3086 config = gst_buffer_pool_get_config (pool);
3087 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3088 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3090 /* buffer pool may have to do some changes */
3091 if (!gst_buffer_pool_set_config (pool, config)) {
3092 config = gst_buffer_pool_get_config (pool);
3094 /* If change are not acceptable, fallback to generic pool */
3095 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3097 GST_DEBUG_OBJECT (basesrc, "unsupported pool, making new pool");
3099 gst_object_unref (pool);
3100 pool = gst_buffer_pool_new ();
3101 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3102 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3105 if (!gst_buffer_pool_set_config (pool, config))
3110 if (update_allocator)
3111 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
3113 gst_query_add_allocation_param (query, allocator, ¶ms);
3115 gst_object_unref (allocator);
3118 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3119 gst_object_unref (pool);
3125 GST_ELEMENT_ERROR (basesrc, RESOURCE, SETTINGS,
3126 ("Failed to configure the buffer pool"),
3127 ("Configuration is most likely invalid, please report this issue."));
3128 gst_object_unref (pool);
3133 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3135 GstBaseSrcClass *bclass;
3136 gboolean result = TRUE;
3138 GstBufferPool *pool = NULL;
3139 GstAllocator *allocator = NULL;
3140 GstAllocationParams params;
3142 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3144 /* make query and let peer pad answer, we don't really care if it worked or
3145 * not, if it failed, the allocation query would contain defaults and the
3146 * subclass would then set better values if needed */
3147 query = gst_query_new_allocation (caps, TRUE);
3148 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3149 /* not a problem, just debug a little */
3150 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3153 g_assert (bclass->decide_allocation != NULL);
3154 result = bclass->decide_allocation (basesrc, query);
3156 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3160 goto no_decide_allocation;
3162 /* we got configuration from our peer or the decide_allocation method,
3164 if (gst_query_get_n_allocation_params (query) > 0) {
3165 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3168 gst_allocation_params_init (¶ms);
3171 if (gst_query_get_n_allocation_pools (query) > 0)
3172 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3174 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
3177 gst_object_unref (allocator);
3179 gst_object_unref (pool);
3181 gst_query_unref (query);
3186 no_decide_allocation:
3188 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3189 gst_query_unref (query);
3195 /* default negotiation code.
3197 * Take intersection between src and sink pads, take first
3201 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3204 GstCaps *caps = NULL;
3205 GstCaps *peercaps = NULL;
3206 gboolean result = FALSE;
3208 /* first see what is possible on our source pad */
3209 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3210 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3211 /* nothing or anything is allowed, we're done */
3212 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3213 goto no_nego_needed;
3215 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3218 /* get the peer caps */
3219 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3220 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3222 /* The result is already a subset of our caps */
3224 gst_caps_unref (thiscaps);
3226 /* no peer, work with our own caps then */
3229 if (caps && !gst_caps_is_empty (caps)) {
3231 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3232 if (gst_caps_is_any (caps)) {
3233 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3234 /* hmm, still anything, so element can do anything and
3235 * nego is not needed */
3238 caps = gst_base_src_fixate (basesrc, caps);
3239 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3240 if (gst_caps_is_fixed (caps)) {
3241 /* yay, fixed caps, use those then, it's possible that the subclass does
3242 * not accept this caps after all and we have to fail. */
3243 result = gst_base_src_set_caps (basesrc, caps);
3246 gst_caps_unref (caps);
3249 gst_caps_unref (caps);
3250 GST_DEBUG_OBJECT (basesrc, "no common caps");
3256 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3258 gst_caps_unref (thiscaps);
3263 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3264 ("No supported formats found"),
3265 ("This element did not produce valid caps"));
3267 gst_caps_unref (thiscaps);
3273 gst_base_src_negotiate (GstBaseSrc * basesrc)
3275 GstBaseSrcClass *bclass;
3278 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3280 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3282 if (G_LIKELY (bclass->negotiate))
3283 result = bclass->negotiate (basesrc);
3287 if (G_LIKELY (result)) {
3290 caps = gst_pad_get_current_caps (basesrc->srcpad);
3292 result = gst_base_src_prepare_allocation (basesrc, caps);
3295 gst_caps_unref (caps);
3301 gst_base_src_start (GstBaseSrc * basesrc)
3303 GstBaseSrcClass *bclass;
3306 GST_LIVE_LOCK (basesrc);
3308 GST_OBJECT_LOCK (basesrc);
3309 if (GST_BASE_SRC_IS_STARTING (basesrc))
3311 if (GST_BASE_SRC_IS_STARTED (basesrc))
3314 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3315 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3316 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3317 GST_OBJECT_UNLOCK (basesrc);
3319 basesrc->num_buffers_left = basesrc->num_buffers;
3320 basesrc->running = FALSE;
3321 basesrc->priv->segment_pending = FALSE;
3322 basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3323 basesrc->priv->forced_eos = FALSE;
3324 GST_LIVE_UNLOCK (basesrc);
3326 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3328 result = bclass->start (basesrc);
3333 goto could_not_start;
3335 if (!gst_base_src_is_async (basesrc)) {
3336 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3337 /* not really waiting here, we call this to get the result
3338 * from the start_complete call */
3339 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3347 GST_DEBUG_OBJECT (basesrc, "was starting");
3348 GST_OBJECT_UNLOCK (basesrc);
3349 GST_LIVE_UNLOCK (basesrc);
3354 GST_DEBUG_OBJECT (basesrc, "was started");
3355 GST_OBJECT_UNLOCK (basesrc);
3356 GST_LIVE_UNLOCK (basesrc);
3361 GST_DEBUG_OBJECT (basesrc, "could not start");
3362 /* subclass is supposed to post a message but we post one as a fallback
3363 * just in case. We don't have to call _stop. */
3364 GST_ELEMENT_ERROR (basesrc, CORE, STATE_CHANGE, (NULL),
3365 ("Failed to start"));
3366 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3372 * gst_base_src_start_complete:
3373 * @basesrc: base source instance
3374 * @ret: a #GstFlowReturn
3376 * Complete an asynchronous start operation. When the subclass overrides the
3377 * start method, it should call gst_base_src_start_complete() when the start
3378 * operation completes either from the same thread or from an asynchronous
3382 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3391 if (ret != GST_FLOW_OK)
3394 GST_DEBUG_OBJECT (basesrc, "starting source");
3395 format = basesrc->segment.format;
3397 /* figure out the size */
3400 if (format == GST_FORMAT_BYTES) {
3401 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3403 if (bclass->get_size) {
3404 if (!(have_size = bclass->get_size (basesrc, &size)))
3407 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3408 /* only update the size when operating in bytes, subclass is supposed
3409 * to set duration in the start method for other formats */
3410 GST_OBJECT_LOCK (basesrc);
3411 basesrc->segment.duration = size;
3412 GST_OBJECT_UNLOCK (basesrc);
3415 GST_DEBUG_OBJECT (basesrc,
3416 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3417 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3418 basesrc->segment.duration);
3420 seekable = gst_base_src_seekable (basesrc);
3421 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3423 /* update for random access flag */
3424 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3426 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3428 /* stop flushing now but for live sources, still block in the LIVE lock when
3429 * we are not yet PLAYING */
3430 gst_base_src_set_flushing (basesrc, FALSE, FALSE, NULL);
3432 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3434 GST_OBJECT_LOCK (basesrc->srcpad);
3435 mode = GST_PAD_MODE (basesrc->srcpad);
3436 GST_OBJECT_UNLOCK (basesrc->srcpad);
3438 /* take the stream lock here, we only want to let the task run when we have
3439 * set the STARTED flag */
3440 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3442 case GST_PAD_MODE_PUSH:
3443 /* do initial seek, which will start the task */
3444 GST_OBJECT_LOCK (basesrc);
3445 event = basesrc->pending_seek;
3446 basesrc->pending_seek = NULL;
3447 GST_OBJECT_UNLOCK (basesrc);
3449 /* The perform seek code will start the task when finished. We don't have to
3450 * unlock the streaming thread because it is not running yet */
3451 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3455 gst_event_unref (event);
3457 case GST_PAD_MODE_PULL:
3458 /* if not random_access, we cannot operate in pull mode for now */
3459 if (G_UNLIKELY (!basesrc->random_access))
3463 goto not_activated_yet;
3467 GST_OBJECT_LOCK (basesrc);
3468 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3469 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3470 basesrc->priv->start_result = ret;
3471 GST_ASYNC_SIGNAL (basesrc);
3472 GST_OBJECT_UNLOCK (basesrc);
3474 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3480 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3481 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3482 gst_base_src_stop (basesrc);
3484 gst_event_unref (event);
3485 ret = GST_FLOW_ERROR;
3490 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3491 gst_base_src_stop (basesrc);
3492 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3493 ret = GST_FLOW_ERROR;
3498 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3499 gst_base_src_stop (basesrc);
3500 GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3501 ret = GST_FLOW_ERROR;
3506 GST_OBJECT_LOCK (basesrc);
3507 basesrc->priv->start_result = ret;
3508 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3509 GST_ASYNC_SIGNAL (basesrc);
3510 GST_OBJECT_UNLOCK (basesrc);
3516 * gst_base_src_start_wait:
3517 * @basesrc: base source instance
3519 * Wait until the start operation completes.
3521 * Returns: a #GstFlowReturn.
3524 gst_base_src_start_wait (GstBaseSrc * basesrc)
3526 GstFlowReturn result;
3528 GST_OBJECT_LOCK (basesrc);
3529 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3530 GST_ASYNC_WAIT (basesrc);
3532 result = basesrc->priv->start_result;
3533 GST_OBJECT_UNLOCK (basesrc);
3535 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3541 gst_base_src_stop (GstBaseSrc * basesrc)
3543 GstBaseSrcClass *bclass;
3544 gboolean result = TRUE;
3546 GST_DEBUG_OBJECT (basesrc, "stopping source");
3549 gst_base_src_set_flushing (basesrc, TRUE, FALSE, NULL);
3551 gst_pad_stop_task (basesrc->srcpad);
3553 GST_OBJECT_LOCK (basesrc);
3554 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3557 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3558 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3559 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3560 GST_ASYNC_SIGNAL (basesrc);
3561 GST_OBJECT_UNLOCK (basesrc);
3563 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3565 result = bclass->stop (basesrc);
3567 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3573 GST_DEBUG_OBJECT (basesrc, "was stopped");
3574 GST_OBJECT_UNLOCK (basesrc);
3579 /* start or stop flushing dataprocessing
3582 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3583 gboolean flushing, gboolean live_play, gboolean * playing)
3585 GstBaseSrcClass *bclass;
3587 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3589 GST_DEBUG_OBJECT (basesrc, "flushing %d, live_play %d", flushing, live_play);
3592 gst_base_src_activate_pool (basesrc, FALSE);
3593 /* unlock any subclasses, we need to do this before grabbing the
3594 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3595 * unlock to the params because of backwards compat (see seek handler)*/
3597 bclass->unlock (basesrc);
3600 /* the live lock is released when we are blocked, waiting for playing or
3601 * when we sync to the clock. */
3602 GST_LIVE_LOCK (basesrc);
3604 *playing = basesrc->live_running;
3605 basesrc->priv->flushing = flushing;
3607 /* if we are locked in the live lock, signal it to make it flush */
3608 basesrc->live_running = TRUE;
3610 /* clear pending EOS if any */
3611 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3612 GST_OBJECT_LOCK (basesrc);
3613 CLEAR_PENDING_EOS (basesrc);
3614 basesrc->priv->forced_eos = FALSE;
3615 GST_OBJECT_UNLOCK (basesrc);
3618 /* step 1, now that we have the LIVE lock, clear our unlock request */
3619 if (bclass->unlock_stop)
3620 bclass->unlock_stop (basesrc);
3622 /* step 2, unblock clock sync (if any) or any other blocking thing */
3623 if (basesrc->clock_id)
3624 gst_clock_id_unschedule (basesrc->clock_id);
3626 /* signal the live source that it can start playing */
3627 basesrc->live_running = live_play;
3629 gst_base_src_activate_pool (basesrc, TRUE);
3631 /* Drop all delayed events */
3632 GST_OBJECT_LOCK (basesrc);
3633 if (basesrc->priv->pending_events) {
3634 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3636 g_list_free (basesrc->priv->pending_events);
3637 basesrc->priv->pending_events = NULL;
3638 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3640 GST_OBJECT_UNLOCK (basesrc);
3642 GST_LIVE_SIGNAL (basesrc);
3643 GST_LIVE_UNLOCK (basesrc);
3648 /* the purpose of this function is to make sure that a live source blocks in the
3649 * LIVE lock or leaves the LIVE lock and continues playing. */
3651 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3653 GstBaseSrcClass *bclass;
3655 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3657 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3659 GST_DEBUG_OBJECT (basesrc, "unlock");
3661 bclass->unlock (basesrc);
3664 /* we are now able to grab the LIVE lock, when we get it, we can be
3665 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3667 GST_LIVE_LOCK (basesrc);
3668 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3670 /* unblock clock sync (if any) */
3671 if (basesrc->clock_id)
3672 gst_clock_id_unschedule (basesrc->clock_id);
3674 /* configure what to do when we get to the LIVE lock. */
3675 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3676 basesrc->live_running = live_play;
3681 /* clear our unlock request when going to PLAYING */
3682 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3683 if (bclass->unlock_stop)
3684 bclass->unlock_stop (basesrc);
3686 /* for live sources we restart the timestamp correction */
3687 GST_OBJECT_LOCK (basesrc);
3688 basesrc->priv->latency = -1;
3689 GST_OBJECT_UNLOCK (basesrc);
3690 /* have to restart the task in case it stopped because of the unlock when
3691 * we went to PAUSED. Only do this if we operating in push mode. */
3692 GST_OBJECT_LOCK (basesrc->srcpad);
3693 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3694 GST_OBJECT_UNLOCK (basesrc->srcpad);
3696 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3697 basesrc->srcpad, NULL);
3698 GST_DEBUG_OBJECT (basesrc, "signal");
3699 GST_LIVE_SIGNAL (basesrc);
3701 GST_LIVE_UNLOCK (basesrc);
3707 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3709 GstBaseSrc *basesrc;
3711 basesrc = GST_BASE_SRC (parent);
3713 /* prepare subclass first */
3715 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3717 if (G_UNLIKELY (!basesrc->can_activate_push))
3718 goto no_push_activation;
3720 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3723 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3724 /* now we can stop the source */
3725 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3733 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3738 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3743 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3749 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3751 GstBaseSrc *basesrc;
3753 basesrc = GST_BASE_SRC (parent);
3755 /* prepare subclass first */
3757 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3758 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3761 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3762 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3770 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3775 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3781 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3782 GstPadMode mode, gboolean active)
3785 GstBaseSrc *src = GST_BASE_SRC (parent);
3787 src->priv->stream_start_pending = FALSE;
3789 GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3792 case GST_PAD_MODE_PULL:
3793 res = gst_base_src_activate_pull (pad, parent, active);
3795 case GST_PAD_MODE_PUSH:
3796 src->priv->stream_start_pending = active;
3797 res = gst_base_src_activate_push (pad, parent, active);
3800 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3808 static GstStateChangeReturn
3809 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3811 GstBaseSrc *basesrc;
3812 GstStateChangeReturn result;
3813 gboolean no_preroll = FALSE;
3815 basesrc = GST_BASE_SRC (element);
3817 switch (transition) {
3818 case GST_STATE_CHANGE_NULL_TO_READY:
3820 case GST_STATE_CHANGE_READY_TO_PAUSED:
3821 no_preroll = gst_base_src_is_live (basesrc);
3823 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3824 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3825 if (gst_base_src_is_live (basesrc)) {
3826 /* now we can start playback */
3827 gst_base_src_set_playing (basesrc, TRUE);
3835 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3836 transition)) == GST_STATE_CHANGE_FAILURE)
3839 switch (transition) {
3840 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3841 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3842 if (gst_base_src_is_live (basesrc)) {
3843 /* make sure we block in the live lock in PAUSED */
3844 gst_base_src_set_playing (basesrc, FALSE);
3848 case GST_STATE_CHANGE_PAUSED_TO_READY:
3850 /* we don't need to unblock anything here, the pad deactivation code
3851 * already did this */
3852 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3853 GST_OBJECT_LOCK (basesrc);
3854 CLEAR_PENDING_EOS (basesrc);
3855 GST_OBJECT_UNLOCK (basesrc);
3857 gst_event_replace (&basesrc->pending_seek, NULL);
3860 case GST_STATE_CHANGE_READY_TO_NULL:
3866 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3867 result = GST_STATE_CHANGE_NO_PREROLL;
3874 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3880 * gst_base_src_get_buffer_pool:
3881 * @src: a #GstBaseSrc
3883 * Returns: (transfer full): the instance of the #GstBufferPool used
3884 * by the src; unref it after usage.
3887 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3889 GstBufferPool *ret = NULL;
3891 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3893 GST_OBJECT_LOCK (src);
3894 if (src->priv->pool)
3895 ret = gst_object_ref (src->priv->pool);
3896 GST_OBJECT_UNLOCK (src);
3902 * gst_base_src_get_allocator:
3903 * @src: a #GstBaseSrc
3904 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3906 * @params: (out) (allow-none) (transfer full): the
3907 * #GstAllocationParams of @allocator
3909 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3910 * used by the base class and its @params.
3912 * Unref the @allocator after usage.
3915 gst_base_src_get_allocator (GstBaseSrc * src,
3916 GstAllocator ** allocator, GstAllocationParams * params)
3918 g_return_if_fail (GST_IS_BASE_SRC (src));
3920 GST_OBJECT_LOCK (src);
3922 *allocator = src->priv->allocator ?
3923 gst_object_ref (src->priv->allocator) : NULL;
3926 *params = src->priv->params;
3927 GST_OBJECT_UNLOCK (src);