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 <gst/gst-i18n-lib.h>
157 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
158 #define GST_CAT_DEFAULT gst_base_src_debug
160 #define GST_LIVE_GET_LOCK(elem) (&GST_BASE_SRC_CAST(elem)->live_lock)
161 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
162 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
163 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
164 #define GST_LIVE_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->live_cond)
165 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
166 #define GST_LIVE_WAIT_UNTIL(elem, end_time) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
167 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
168 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
171 #define GST_ASYNC_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->priv->async_cond)
172 #define GST_ASYNC_WAIT(elem) g_cond_wait (GST_ASYNC_GET_COND (elem), GST_OBJECT_GET_LOCK (elem))
173 #define GST_ASYNC_SIGNAL(elem) g_cond_signal (GST_ASYNC_GET_COND (elem));
175 #define CLEAR_PENDING_EOS(bsrc) \
177 g_atomic_int_set (&bsrc->priv->has_pending_eos, FALSE); \
178 gst_event_replace (&bsrc->priv->pending_eos, NULL); \
182 /* BaseSrc signals and args */
189 #define DEFAULT_BLOCKSIZE 4096
190 #define DEFAULT_NUM_BUFFERS -1
191 #define DEFAULT_DO_TIMESTAMP FALSE
198 #ifndef GST_REMOVE_DEPRECATED
204 #define GST_BASE_SRC_GET_PRIVATE(obj) \
205 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
207 /* The basesrc implementation need to respect the following locking order:
212 struct _GstBaseSrcPrivate
214 gboolean discont; /* STREAM_LOCK */
215 gboolean flushing; /* LIVE_LOCK */
217 GstFlowReturn start_result; /* OBJECT_LOCK */
218 gboolean async; /* OBJECT_LOCK */
220 /* if a stream-start event should be sent */
221 gboolean stream_start_pending; /* STREAM_LOCK */
223 /* if segment should be sent and a
224 * seqnum if it was originated by a seek */
225 gboolean segment_pending; /* OBJECT_LOCK */
226 guint32 segment_seqnum; /* OBJECT_LOCK */
228 /* if EOS is pending (atomic) */
229 GstEvent *pending_eos; /* OBJECT_LOCK */
230 gint has_pending_eos; /* atomic */
232 /* if the eos was caused by a forced eos from the application */
233 gboolean forced_eos; /* LIVE_LOCK */
235 /* startup latency is the time it takes between going to PLAYING and producing
236 * the first BUFFER with running_time 0. This value is included in the latency
238 GstClockTime latency; /* OBJECT_LOCK */
239 /* timestamp offset, this is the offset add to the values of gst_times for
240 * pseudo live sources */
241 GstClockTimeDiff ts_offset; /* OBJECT_LOCK */
243 gboolean do_timestamp; /* OBJECT_LOCK */
244 volatile gint dynamic_size; /* atomic */
245 volatile gint automatic_eos; /* atomic */
247 /* stream sequence number */
248 guint32 seqnum; /* STREAM_LOCK */
250 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
251 * pushed in the data stream */
252 GList *pending_events; /* OBJECT_LOCK */
253 volatile gint have_events; /* OBJECT_LOCK */
255 /* QoS *//* with LOCK */
256 gdouble proportion; /* OBJECT_LOCK */
257 GstClockTime earliest_time; /* OBJECT_LOCK */
259 GstBufferPool *pool; /* OBJECT_LOCK */
260 GstAllocator *allocator; /* OBJECT_LOCK */
261 GstAllocationParams params; /* OBJECT_LOCK */
263 GCond async_cond; /* OBJECT_LOCK */
266 static GstElementClass *parent_class = NULL;
268 static void gst_base_src_class_init (GstBaseSrcClass * klass);
269 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
270 static void gst_base_src_finalize (GObject * object);
274 gst_base_src_get_type (void)
276 static volatile gsize base_src_type = 0;
278 if (g_once_init_enter (&base_src_type)) {
280 static const GTypeInfo base_src_info = {
281 sizeof (GstBaseSrcClass),
284 (GClassInitFunc) gst_base_src_class_init,
289 (GInstanceInitFunc) gst_base_src_init,
292 _type = g_type_register_static (GST_TYPE_ELEMENT,
293 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
294 g_once_init_leave (&base_src_type, _type);
296 return base_src_type;
299 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
301 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
302 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
304 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
305 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
306 GstPadMode mode, gboolean active);
307 static void gst_base_src_set_property (GObject * object, guint prop_id,
308 const GValue * value, GParamSpec * pspec);
309 static void gst_base_src_get_property (GObject * object, guint prop_id,
310 GValue * value, GParamSpec * pspec);
311 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
313 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
314 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
316 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
319 static void gst_base_src_set_pool_flushing (GstBaseSrc * basesrc,
321 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
322 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
323 GstSegment * segment);
324 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
325 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
326 GstEvent * event, GstSegment * segment);
327 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
328 guint64 offset, guint size, GstBuffer ** buf);
329 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
330 guint64 offset, guint size, GstBuffer ** buf);
331 static gboolean gst_base_src_decide_allocation_default (GstBaseSrc * basesrc,
334 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
337 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
338 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
340 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
341 GstStateChange transition);
343 static void gst_base_src_loop (GstPad * pad);
344 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
345 guint64 offset, guint length, GstBuffer ** buf);
346 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
347 guint length, GstBuffer ** buf);
348 static gboolean gst_base_src_seekable (GstBaseSrc * src);
349 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
350 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
351 guint * length, gboolean force);
354 gst_base_src_class_init (GstBaseSrcClass * klass)
356 GObjectClass *gobject_class;
357 GstElementClass *gstelement_class;
359 gobject_class = G_OBJECT_CLASS (klass);
360 gstelement_class = GST_ELEMENT_CLASS (klass);
362 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
364 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
366 parent_class = g_type_class_peek_parent (klass);
368 gobject_class->finalize = gst_base_src_finalize;
369 gobject_class->set_property = gst_base_src_set_property;
370 gobject_class->get_property = gst_base_src_get_property;
372 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
373 g_param_spec_uint ("blocksize", "Block size",
374 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
375 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
377 g_param_spec_int ("num-buffers", "num-buffers",
378 "Number of buffers to output before sending EOS (-1 = unlimited)",
379 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
380 G_PARAM_STATIC_STRINGS));
381 #ifndef GST_REMOVE_DEPRECATED
382 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
383 g_param_spec_boolean ("typefind", "Typefind",
384 "Run typefind before negotiating (deprecated, non-functional)", FALSE,
385 G_PARAM_READWRITE | G_PARAM_DEPRECATED | G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
388 g_param_spec_boolean ("do-timestamp", "Do timestamp",
389 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 gstelement_class->change_state =
393 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
394 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
396 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
397 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
398 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
399 klass->prepare_seek_segment =
400 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
401 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
402 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
403 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
404 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
405 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
406 klass->decide_allocation =
407 GST_DEBUG_FUNCPTR (gst_base_src_decide_allocation_default);
409 /* Registering debug symbols for function pointers */
410 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
411 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
412 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
413 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
414 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
418 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
421 GstPadTemplate *pad_template;
423 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
425 basesrc->is_live = FALSE;
426 g_mutex_init (&basesrc->live_lock);
427 g_cond_init (&basesrc->live_cond);
428 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
429 basesrc->num_buffers_left = -1;
430 basesrc->priv->automatic_eos = TRUE;
432 basesrc->can_activate_push = TRUE;
435 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
436 g_return_if_fail (pad_template != NULL);
438 GST_DEBUG_OBJECT (basesrc, "creating src pad");
439 pad = gst_pad_new_from_template (pad_template, "src");
441 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
442 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
443 gst_pad_set_event_function (pad, gst_base_src_event);
444 gst_pad_set_query_function (pad, gst_base_src_query);
445 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
447 /* hold pointer to pad */
448 basesrc->srcpad = pad;
449 GST_DEBUG_OBJECT (basesrc, "adding src pad");
450 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
452 basesrc->blocksize = DEFAULT_BLOCKSIZE;
453 basesrc->clock_id = NULL;
454 /* we operate in BYTES by default */
455 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
456 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
457 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
459 g_cond_init (&basesrc->priv->async_cond);
460 basesrc->priv->start_result = GST_FLOW_FLUSHING;
461 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
462 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
463 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
465 GST_DEBUG_OBJECT (basesrc, "init done");
469 gst_base_src_finalize (GObject * object)
474 basesrc = GST_BASE_SRC (object);
476 g_mutex_clear (&basesrc->live_lock);
477 g_cond_clear (&basesrc->live_cond);
478 g_cond_clear (&basesrc->priv->async_cond);
480 event_p = &basesrc->pending_seek;
481 gst_event_replace (event_p, NULL);
483 if (basesrc->priv->pending_events) {
484 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
486 g_list_free (basesrc->priv->pending_events);
489 G_OBJECT_CLASS (parent_class)->finalize (object);
492 /* Call with LIVE_LOCK held */
494 gst_base_src_wait_playing_unlocked (GstBaseSrc * src)
496 while (G_UNLIKELY (!src->live_running && !src->priv->flushing)) {
497 /* block until the state changes, or we get a flush, or something */
498 GST_DEBUG_OBJECT (src, "live source waiting for running state");
500 GST_DEBUG_OBJECT (src, "live source unlocked");
503 if (src->priv->flushing)
511 GST_DEBUG_OBJECT (src, "we are flushing");
512 return GST_FLOW_FLUSHING;
518 * gst_base_src_wait_playing:
521 * If the #GstBaseSrcClass.create() method performs its own synchronisation
522 * against the clock it must unblock when going from PLAYING to the PAUSED state
523 * and call this method before continuing to produce the remaining data.
525 * This function will block until a state change to PLAYING happens (in which
526 * case this function returns %GST_FLOW_OK) or the processing must be stopped due
527 * to a state change to READY or a FLUSH event (in which case this function
528 * returns %GST_FLOW_FLUSHING).
530 * Returns: %GST_FLOW_OK if @src is PLAYING and processing can
531 * continue. Any other return value should be returned from the create vmethod.
534 gst_base_src_wait_playing (GstBaseSrc * src)
538 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
541 ret = gst_base_src_wait_playing_unlocked (src);
542 GST_LIVE_UNLOCK (src);
548 * gst_base_src_set_live:
549 * @src: base source instance
550 * @live: new live-mode
552 * If the element listens to a live source, @live should
555 * A live source will not produce data in the PAUSED state and
556 * will therefore not be able to participate in the PREROLL phase
557 * of a pipeline. To signal this fact to the application and the
558 * pipeline, the state change return value of the live source will
559 * be GST_STATE_CHANGE_NO_PREROLL.
562 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
564 g_return_if_fail (GST_IS_BASE_SRC (src));
566 GST_OBJECT_LOCK (src);
568 GST_OBJECT_UNLOCK (src);
572 * gst_base_src_is_live:
573 * @src: base source instance
575 * Check if an element is in live mode.
577 * Returns: %TRUE if element is in live mode.
580 gst_base_src_is_live (GstBaseSrc * src)
584 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
586 GST_OBJECT_LOCK (src);
587 result = src->is_live;
588 GST_OBJECT_UNLOCK (src);
594 * gst_base_src_set_format:
595 * @src: base source instance
596 * @format: the format to use
598 * Sets the default format of the source. This will be the format used
599 * for sending SEGMENT events and for performing seeks.
601 * If a format of GST_FORMAT_BYTES is set, the element will be able to
602 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns %TRUE.
604 * This function must only be called in states < %GST_STATE_PAUSED.
607 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
609 g_return_if_fail (GST_IS_BASE_SRC (src));
610 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
612 GST_OBJECT_LOCK (src);
613 gst_segment_init (&src->segment, format);
614 GST_OBJECT_UNLOCK (src);
618 * gst_base_src_set_dynamic_size:
619 * @src: base source instance
620 * @dynamic: new dynamic size mode
622 * If not @dynamic, size is only updated when needed, such as when trying to
623 * read past current tracked size. Otherwise, size is checked for upon each
627 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
629 g_return_if_fail (GST_IS_BASE_SRC (src));
631 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
635 * gst_base_src_set_automatic_eos:
636 * @src: base source instance
637 * @automatic_eos: automatic eos
639 * If @automatic_eos is %TRUE, @src will automatically go EOS if a buffer
640 * after the total size is returned. By default this is %TRUE but sources
641 * that can't return an authoritative size and only know that they're EOS
642 * when trying to read more should set this to %FALSE.
647 gst_base_src_set_automatic_eos (GstBaseSrc * src, gboolean automatic_eos)
649 g_return_if_fail (GST_IS_BASE_SRC (src));
651 g_atomic_int_set (&src->priv->automatic_eos, automatic_eos);
655 * gst_base_src_set_async:
656 * @src: base source instance
657 * @async: new async mode
659 * Configure async behaviour in @src, no state change will block. The open,
660 * close, start, stop, play and pause virtual methods will be executed in a
661 * different thread and are thus allowed to perform blocking operations. Any
662 * blocking operation should be unblocked with the unlock vmethod.
665 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
667 g_return_if_fail (GST_IS_BASE_SRC (src));
669 GST_OBJECT_LOCK (src);
670 src->priv->async = async;
671 GST_OBJECT_UNLOCK (src);
675 * gst_base_src_is_async:
676 * @src: base source instance
678 * Get the current async behaviour of @src. See also gst_base_src_set_async().
680 * Returns: %TRUE if @src is operating in async mode.
683 gst_base_src_is_async (GstBaseSrc * src)
687 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
689 GST_OBJECT_LOCK (src);
690 res = src->priv->async;
691 GST_OBJECT_UNLOCK (src);
698 * gst_base_src_query_latency:
700 * @live: (out) (allow-none): if the source is live
701 * @min_latency: (out) (allow-none): the min latency of the source
702 * @max_latency: (out) (allow-none): the max latency of the source
704 * Query the source for the latency parameters. @live will be %TRUE when @src is
705 * configured as a live source. @min_latency and @max_latency will be set
706 * to the difference between the running time and the timestamp of the first
709 * This function is mostly used by subclasses.
711 * Returns: %TRUE if the query succeeded.
714 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
715 GstClockTime * min_latency, GstClockTime * max_latency)
719 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
721 GST_OBJECT_LOCK (src);
723 *live = src->is_live;
725 /* if we have a startup latency, report this one, else report 0. Subclasses
726 * are supposed to override the query function if they want something
728 if (src->priv->latency != -1)
729 min = src->priv->latency;
738 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
739 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
740 GST_TIME_ARGS (min));
741 GST_OBJECT_UNLOCK (src);
747 * gst_base_src_set_blocksize:
749 * @blocksize: the new blocksize in bytes
751 * Set the number of bytes that @src will push out with each buffer. When
752 * @blocksize is set to -1, a default length will be used.
755 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
757 g_return_if_fail (GST_IS_BASE_SRC (src));
759 GST_OBJECT_LOCK (src);
760 src->blocksize = blocksize;
761 GST_OBJECT_UNLOCK (src);
765 * gst_base_src_get_blocksize:
768 * Get the number of bytes that @src will push out with each buffer.
770 * Returns: the number of bytes pushed with each buffer.
773 gst_base_src_get_blocksize (GstBaseSrc * src)
777 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
779 GST_OBJECT_LOCK (src);
780 res = src->blocksize;
781 GST_OBJECT_UNLOCK (src);
788 * gst_base_src_set_do_timestamp:
790 * @timestamp: enable or disable timestamping
792 * Configure @src to automatically timestamp outgoing buffers based on the
793 * current running_time of the pipeline. This property is mostly useful for live
797 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
799 g_return_if_fail (GST_IS_BASE_SRC (src));
801 GST_OBJECT_LOCK (src);
802 src->priv->do_timestamp = timestamp;
803 if (timestamp && src->segment.format != GST_FORMAT_TIME)
804 gst_segment_init (&src->segment, GST_FORMAT_TIME);
805 GST_OBJECT_UNLOCK (src);
809 * gst_base_src_get_do_timestamp:
812 * Query if @src timestamps outgoing buffers based on the current running_time.
814 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
817 gst_base_src_get_do_timestamp (GstBaseSrc * src)
821 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
823 GST_OBJECT_LOCK (src);
824 res = src->priv->do_timestamp;
825 GST_OBJECT_UNLOCK (src);
831 * gst_base_src_new_seamless_segment:
833 * @start: The new start value for the segment
834 * @stop: Stop value for the new segment
835 * @time: The new time value for the start of the new segment
837 * Prepare a new seamless segment for emission downstream. This function must
838 * only be called by derived sub-classes, and only from the create() function,
839 * as the stream-lock needs to be held.
841 * The format for the new segment will be the current format of the source, as
842 * configured with gst_base_src_set_format()
844 * Returns: %TRUE if preparation of the seamless segment succeeded.
847 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
852 GST_OBJECT_LOCK (src);
854 src->segment.base = gst_segment_to_running_time (&src->segment,
855 src->segment.format, src->segment.position);
856 src->segment.position = src->segment.start = start;
857 src->segment.stop = stop;
858 src->segment.time = time;
860 /* Mark pending segment. Will be sent before next data */
861 src->priv->segment_pending = TRUE;
862 src->priv->segment_seqnum = gst_util_seqnum_next ();
864 GST_DEBUG_OBJECT (src,
865 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
866 GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
867 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
868 GST_TIME_ARGS (src->segment.base));
870 GST_OBJECT_UNLOCK (src);
872 src->priv->discont = TRUE;
878 /* called with STREAM_LOCK */
880 gst_base_src_send_stream_start (GstBaseSrc * src)
884 if (src->priv->stream_start_pending) {
889 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
891 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
892 event = gst_event_new_stream_start (stream_id);
893 gst_event_set_group_id (event, gst_util_group_id_next ());
895 ret = gst_pad_push_event (src->srcpad, event);
896 src->priv->stream_start_pending = FALSE;
904 * gst_base_src_set_caps:
905 * @src: a #GstBaseSrc
906 * @caps: (transfer none): a #GstCaps
908 * Set new caps on the basesrc source pad.
910 * Returns: %TRUE if the caps could be set
913 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
915 GstBaseSrcClass *bclass;
917 GstCaps *current_caps;
919 bclass = GST_BASE_SRC_GET_CLASS (src);
921 gst_base_src_send_stream_start (src);
923 current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (src));
924 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
925 GST_DEBUG_OBJECT (src, "New caps equal to old ones: %" GST_PTR_FORMAT,
929 if (bclass->set_caps)
930 res = bclass->set_caps (src, caps);
933 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
937 gst_caps_unref (current_caps);
943 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
945 GstCaps *caps = NULL;
946 GstPadTemplate *pad_template;
947 GstBaseSrcClass *bclass;
949 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
952 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
954 if (pad_template != NULL) {
955 caps = gst_pad_template_get_caps (pad_template);
958 GstCaps *intersection;
961 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
962 gst_caps_unref (caps);
970 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
972 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
973 return gst_caps_fixate (caps);
977 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
979 GstBaseSrcClass *bclass;
981 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
984 caps = bclass->fixate (bsrc, caps);
990 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
994 switch (GST_QUERY_TYPE (query)) {
995 case GST_QUERY_POSITION:
999 gst_query_parse_position (query, &format, NULL);
1001 GST_DEBUG_OBJECT (src, "position query in format %s",
1002 gst_format_get_name (format));
1005 case GST_FORMAT_PERCENT:
1011 GST_OBJECT_LOCK (src);
1012 position = src->segment.position;
1013 duration = src->segment.duration;
1014 GST_OBJECT_UNLOCK (src);
1016 if (position != -1 && duration != -1) {
1017 if (position < duration)
1018 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1021 percent = GST_FORMAT_PERCENT_MAX;
1025 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1032 GstFormat seg_format;
1034 GST_OBJECT_LOCK (src);
1036 gst_segment_to_stream_time (&src->segment, src->segment.format,
1037 src->segment.position);
1038 seg_format = src->segment.format;
1039 GST_OBJECT_UNLOCK (src);
1041 if (position != -1) {
1042 /* convert to requested format */
1044 gst_pad_query_convert (src->srcpad, seg_format,
1045 position, format, &position);
1050 gst_query_set_position (query, format, position);
1057 case GST_QUERY_DURATION:
1061 gst_query_parse_duration (query, &format, NULL);
1063 GST_DEBUG_OBJECT (src, "duration query in format %s",
1064 gst_format_get_name (format));
1067 case GST_FORMAT_PERCENT:
1068 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1069 GST_FORMAT_PERCENT_MAX);
1075 GstFormat seg_format;
1078 /* may have to refresh duration */
1079 gst_base_src_update_length (src, 0, &length,
1080 g_atomic_int_get (&src->priv->dynamic_size));
1082 /* this is the duration as configured by the subclass. */
1083 GST_OBJECT_LOCK (src);
1084 duration = src->segment.duration;
1085 seg_format = src->segment.format;
1086 GST_OBJECT_UNLOCK (src);
1088 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1089 duration, gst_format_get_name (seg_format));
1091 if (duration != -1) {
1092 /* convert to requested format, if this fails, we have a duration
1093 * but we cannot answer the query, we must return FALSE. */
1095 gst_pad_query_convert (src->srcpad, seg_format,
1096 duration, format, &duration);
1098 /* The subclass did not configure a duration, we assume that the
1099 * media has an unknown duration then and we return TRUE to report
1100 * this. Note that this is not the same as returning FALSE, which
1101 * means that we cannot report the duration at all. */
1106 gst_query_set_duration (query, format, duration);
1114 case GST_QUERY_SEEKING:
1116 GstFormat format, seg_format;
1119 GST_OBJECT_LOCK (src);
1120 duration = src->segment.duration;
1121 seg_format = src->segment.format;
1122 GST_OBJECT_UNLOCK (src);
1124 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1125 if (format == seg_format) {
1126 gst_query_set_seeking (query, seg_format,
1127 gst_base_src_seekable (src), 0, duration);
1130 /* FIXME 2.0: return TRUE + seekable=FALSE for SEEKING query here */
1131 /* Don't reply to the query to make up for demuxers which don't
1132 * handle the SEEKING query yet. Players like Totem will fall back
1133 * to the duration when the SEEKING query isn't answered. */
1138 case GST_QUERY_SEGMENT:
1143 GST_OBJECT_LOCK (src);
1145 format = src->segment.format;
1148 gst_segment_to_stream_time (&src->segment, format,
1149 src->segment.start);
1150 if ((stop = src->segment.stop) == -1)
1151 stop = src->segment.duration;
1153 stop = gst_segment_to_stream_time (&src->segment, format, stop);
1155 gst_query_set_segment (query, src->segment.rate, format, start, stop);
1157 GST_OBJECT_UNLOCK (src);
1162 case GST_QUERY_FORMATS:
1164 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1165 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1169 case GST_QUERY_CONVERT:
1171 GstFormat src_fmt, dest_fmt;
1172 gint64 src_val, dest_val;
1174 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1176 /* we can only convert between equal formats... */
1177 if (src_fmt == dest_fmt) {
1183 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1186 case GST_QUERY_LATENCY:
1188 GstClockTime min, max;
1191 /* Subclasses should override and implement something useful */
1192 res = gst_base_src_query_latency (src, &live, &min, &max);
1194 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1195 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1196 GST_TIME_ARGS (max));
1198 gst_query_set_latency (query, live, min, max);
1201 case GST_QUERY_JITTER:
1202 case GST_QUERY_RATE:
1205 case GST_QUERY_BUFFERING:
1207 GstFormat format, seg_format;
1208 gint64 start, stop, estimated;
1210 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1212 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1213 gst_format_get_name (format));
1215 GST_OBJECT_LOCK (src);
1216 if (src->random_access) {
1219 if (format == GST_FORMAT_PERCENT)
1220 stop = GST_FORMAT_PERCENT_MAX;
1222 stop = src->segment.duration;
1228 seg_format = src->segment.format;
1229 GST_OBJECT_UNLOCK (src);
1231 /* convert to required format. When the conversion fails, we can't answer
1232 * the query. When the value is unknown, we can don't perform conversion
1233 * but report TRUE. */
1234 if (format != GST_FORMAT_PERCENT && stop != -1) {
1235 res = gst_pad_query_convert (src->srcpad, seg_format,
1236 stop, format, &stop);
1240 if (res && format != GST_FORMAT_PERCENT && start != -1)
1241 res = gst_pad_query_convert (src->srcpad, seg_format,
1242 start, format, &start);
1244 gst_query_set_buffering_range (query, format, start, stop, estimated);
1247 case GST_QUERY_SCHEDULING:
1249 gboolean random_access;
1251 random_access = gst_base_src_is_random_access (src);
1253 /* we can operate in getrange mode if the native format is bytes
1254 * and we are seekable, this condition is set in the random_access
1255 * flag and is set in the _start() method. */
1256 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1258 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1259 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1264 case GST_QUERY_CAPS:
1266 GstBaseSrcClass *bclass;
1267 GstCaps *caps, *filter;
1269 bclass = GST_BASE_SRC_GET_CLASS (src);
1270 if (bclass->get_caps) {
1271 gst_query_parse_caps (query, &filter);
1272 if ((caps = bclass->get_caps (src, filter))) {
1273 gst_query_set_caps_result (query, caps);
1274 gst_caps_unref (caps);
1283 case GST_QUERY_URI:{
1284 if (GST_IS_URI_HANDLER (src)) {
1285 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1288 gst_query_set_uri (query, uri);
1303 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1310 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1313 GstBaseSrcClass *bclass;
1314 gboolean result = FALSE;
1316 src = GST_BASE_SRC (parent);
1317 bclass = GST_BASE_SRC_GET_CLASS (src);
1320 result = bclass->query (src, query);
1326 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1328 gboolean res = TRUE;
1330 /* update our offset if the start/stop position was updated */
1331 if (segment->format == GST_FORMAT_BYTES) {
1332 segment->time = segment->start;
1333 } else if (segment->start == 0) {
1334 /* seek to start, we can implement a default for this. */
1338 GST_INFO_OBJECT (src, "Can't do a default seek");
1345 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1347 GstBaseSrcClass *bclass;
1348 gboolean result = FALSE;
1350 bclass = GST_BASE_SRC_GET_CLASS (src);
1352 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1354 if (bclass->do_seek)
1355 result = bclass->do_seek (src, segment);
1360 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1363 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1364 GstSegment * segment)
1366 /* By default, we try one of 2 things:
1367 * - For absolute seek positions, convert the requested position to our
1368 * configured processing format and place it in the output segment \
1369 * - For relative seek positions, convert our current (input) values to the
1370 * seek format, adjust by the relative seek offset and then convert back to
1371 * the processing format
1373 GstSeekType start_type, stop_type;
1376 GstFormat seek_format, dest_format;
1379 gboolean res = TRUE;
1381 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1382 &start_type, &start, &stop_type, &stop);
1383 dest_format = segment->format;
1385 if (seek_format == dest_format) {
1386 gst_segment_do_seek (segment, rate, seek_format, flags,
1387 start_type, start, stop_type, stop, &update);
1391 if (start_type != GST_SEEK_TYPE_NONE) {
1392 /* FIXME: Handle seek_end by converting the input segment vals */
1394 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1396 start_type = GST_SEEK_TYPE_SET;
1399 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1400 /* FIXME: Handle seek_end by converting the input segment vals */
1402 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1404 stop_type = GST_SEEK_TYPE_SET;
1407 /* And finally, configure our output segment in the desired format */
1408 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1409 stop_type, stop, &update);
1418 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1424 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1425 GstSegment * seeksegment)
1427 GstBaseSrcClass *bclass;
1428 gboolean result = FALSE;
1430 bclass = GST_BASE_SRC_GET_CLASS (src);
1432 if (bclass->prepare_seek_segment)
1433 result = bclass->prepare_seek_segment (src, event, seeksegment);
1438 static GstFlowReturn
1439 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1440 guint size, GstBuffer ** buffer)
1443 GstBaseSrcPrivate *priv = src->priv;
1444 GstBufferPool *pool = NULL;
1445 GstAllocator *allocator = NULL;
1446 GstAllocationParams params;
1448 GST_OBJECT_LOCK (src);
1450 pool = gst_object_ref (priv->pool);
1451 } else if (priv->allocator) {
1452 allocator = gst_object_ref (priv->allocator);
1454 params = priv->params;
1455 GST_OBJECT_UNLOCK (src);
1458 ret = gst_buffer_pool_acquire_buffer (pool, buffer, NULL);
1459 } else if (size != -1) {
1460 *buffer = gst_buffer_new_allocate (allocator, size, ¶ms);
1461 if (G_UNLIKELY (*buffer == NULL))
1466 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1473 gst_object_unref (pool);
1475 gst_object_unref (allocator);
1482 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1483 ret = GST_FLOW_ERROR;
1488 static GstFlowReturn
1489 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1490 guint size, GstBuffer ** buffer)
1492 GstBaseSrcClass *bclass;
1496 bclass = GST_BASE_SRC_GET_CLASS (src);
1498 if (G_UNLIKELY (!bclass->alloc))
1500 if (G_UNLIKELY (!bclass->fill))
1503 if (*buffer == NULL) {
1504 /* downstream did not provide us with a buffer to fill, allocate one
1506 ret = bclass->alloc (src, offset, size, &res_buf);
1507 if (G_UNLIKELY (ret != GST_FLOW_OK))
1513 if (G_LIKELY (size > 0)) {
1514 /* only call fill when there is a size */
1515 ret = bclass->fill (src, offset, size, res_buf);
1516 if (G_UNLIKELY (ret != GST_FLOW_OK))
1527 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1528 return GST_FLOW_NOT_SUPPORTED;
1532 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1537 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1538 gst_flow_get_name (ret));
1539 if (*buffer == NULL)
1540 gst_buffer_unref (res_buf);
1545 /* this code implements the seeking. It is a good example
1546 * handling all cases.
1548 * A seek updates the currently configured segment.start
1549 * and segment.stop values based on the SEEK_TYPE. If the
1550 * segment.start value is updated, a seek to this new position
1551 * should be performed.
1553 * The seek can only be executed when we are not currently
1554 * streaming any data, to make sure that this is the case, we
1555 * acquire the STREAM_LOCK which is taken when we are in the
1556 * _loop() function or when a getrange() is called. Normally
1557 * we will not receive a seek if we are operating in pull mode
1558 * though. When we operate as a live source we might block on the live
1559 * cond, which does not release the STREAM_LOCK. Therefore we will try
1560 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1561 * safe to perform the seek.
1563 * When we are in the loop() function, we might be in the middle
1564 * of pushing a buffer, which might block in a sink. To make sure
1565 * that the push gets unblocked we push out a FLUSH_START event.
1566 * Our loop function will get a FLUSHING return value from
1567 * the push and will pause, effectively releasing the STREAM_LOCK.
1569 * For a non-flushing seek, we pause the task, which might eventually
1570 * release the STREAM_LOCK. We say eventually because when the sink
1571 * blocks on the sample we might wait a very long time until the sink
1572 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1573 * can continue the seek. A non-flushing seek is normally done in a
1574 * running pipeline to perform seamless playback, this means that the sink is
1575 * PLAYING and will return from its chain function.
1576 * In the case of a non-flushing seek we need to make sure that the
1577 * data we output after the seek is continuous with the previous data,
1578 * this is because a non-flushing seek does not reset the running-time
1579 * to 0. We do this by closing the currently running segment, ie. sending
1580 * a new_segment event with the stop position set to the last processed
1583 * After updating the segment.start/stop values, we prepare for
1584 * streaming again. We push out a FLUSH_STOP to make the peer pad
1585 * accept data again and we start our task again.
1587 * A segment seek posts a message on the bus saying that the playback
1588 * of the segment started. We store the segment flag internally because
1589 * when we reach the segment.stop we have to post a segment.done
1590 * instead of EOS when doing a segment seek.
1593 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1595 gboolean res = TRUE, tres;
1597 GstFormat seek_format, dest_format;
1599 GstSeekType start_type, stop_type;
1603 gboolean relative_seek = FALSE;
1604 gboolean seekseg_configured = FALSE;
1605 GstSegment seeksegment;
1609 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1611 GST_OBJECT_LOCK (src);
1612 dest_format = src->segment.format;
1613 GST_OBJECT_UNLOCK (src);
1616 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1617 &start_type, &start, &stop_type, &stop);
1619 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1620 SEEK_TYPE_IS_RELATIVE (stop_type);
1622 if (dest_format != seek_format && !relative_seek) {
1623 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1624 * here before taking the stream lock, otherwise we must convert it later,
1625 * once we have the stream lock and can read the last configures segment
1626 * start and stop positions */
1627 gst_segment_init (&seeksegment, dest_format);
1629 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1630 goto prepare_failed;
1632 seekseg_configured = TRUE;
1635 flush = flags & GST_SEEK_FLAG_FLUSH;
1636 seqnum = gst_event_get_seqnum (event);
1639 /* get next seqnum */
1640 seqnum = gst_util_seqnum_next ();
1643 /* send flush start */
1645 tevent = gst_event_new_flush_start ();
1646 gst_event_set_seqnum (tevent, seqnum);
1647 gst_pad_push_event (src->srcpad, tevent);
1649 gst_pad_pause_task (src->srcpad);
1651 /* unblock streaming thread. */
1653 gst_base_src_set_flushing (src, TRUE);
1655 /* grab streaming lock, this should eventually be possible, either
1656 * because the task is paused, our streaming thread stopped
1657 * or because our peer is flushing. */
1658 GST_PAD_STREAM_LOCK (src->srcpad);
1659 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1660 /* we have seen this event before, issue a warning for now */
1661 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1664 src->priv->seqnum = seqnum;
1665 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1669 gst_base_src_set_flushing (src, FALSE);
1671 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1672 * copy the current segment info into the temp segment that we can actually
1673 * attempt the seek with. We only update the real segment if the seek succeeds. */
1674 if (!seekseg_configured) {
1675 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1677 /* now configure the final seek segment */
1679 if (seeksegment.format != seek_format) {
1680 /* OK, here's where we give the subclass a chance to convert the relative
1681 * seek into an absolute one in the processing format. We set up any
1682 * absolute seek above, before taking the stream lock. */
1683 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1684 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1689 /* The seek format matches our processing format, no need to ask the
1690 * the subclass to configure the segment. */
1691 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1692 start_type, start, stop_type, stop, &update);
1695 /* Else, no seek event passed, so we're just (re)starting the
1700 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1701 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1702 seeksegment.start, seeksegment.stop, seeksegment.position);
1704 /* do the seek, segment.position contains the new position. */
1705 res = gst_base_src_do_seek (src, &seeksegment);
1708 /* and prepare to continue streaming */
1710 tevent = gst_event_new_flush_stop (TRUE);
1711 gst_event_set_seqnum (tevent, seqnum);
1712 /* send flush stop, peer will accept data and events again. We
1713 * are not yet providing data as we still have the STREAM_LOCK. */
1714 gst_pad_push_event (src->srcpad, tevent);
1717 /* The subclass must have converted the segment to the processing format
1719 if (res && seeksegment.format != dest_format) {
1720 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1721 "in the correct format. Aborting seek.");
1725 /* if the seek was successful, we update our real segment and push
1726 * out the new segment. */
1728 GST_OBJECT_LOCK (src);
1729 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1730 GST_OBJECT_UNLOCK (src);
1732 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1733 GstMessage *message;
1735 message = gst_message_new_segment_start (GST_OBJECT (src),
1736 seeksegment.format, seeksegment.position);
1737 gst_message_set_seqnum (message, seqnum);
1739 gst_element_post_message (GST_ELEMENT (src), message);
1742 src->priv->segment_pending = TRUE;
1743 src->priv->segment_seqnum = seqnum;
1746 src->priv->discont = TRUE;
1747 src->running = TRUE;
1748 /* and restart the task in case it got paused explicitly or by
1749 * the FLUSH_START event we pushed out. */
1750 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1755 /* and release the lock again so we can continue streaming */
1756 GST_PAD_STREAM_UNLOCK (src->srcpad);
1762 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1767 /* all events send to this element directly. This is mainly done from the
1771 gst_base_src_send_event (GstElement * element, GstEvent * event)
1774 gboolean result = FALSE;
1775 GstBaseSrcClass *bclass;
1777 src = GST_BASE_SRC (element);
1778 bclass = GST_BASE_SRC_GET_CLASS (src);
1780 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1782 switch (GST_EVENT_TYPE (event)) {
1783 /* bidirectional events */
1784 case GST_EVENT_FLUSH_START:
1785 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1787 result = gst_pad_push_event (src->srcpad, event);
1788 gst_base_src_set_flushing (src, TRUE);
1791 case GST_EVENT_FLUSH_STOP:
1795 GST_PAD_STREAM_LOCK (src->srcpad);
1796 gst_base_src_set_flushing (src, FALSE);
1798 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1799 result = gst_pad_push_event (src->srcpad, event);
1801 /* For external flush, restart the task .. */
1802 GST_LIVE_LOCK (src);
1803 src->priv->segment_pending = TRUE;
1805 GST_OBJECT_LOCK (src->srcpad);
1806 start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1807 GST_OBJECT_UNLOCK (src->srcpad);
1809 /* ... and for live sources, only if in playing state */
1811 if (!src->live_running)
1816 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1819 GST_LIVE_UNLOCK (src);
1820 GST_PAD_STREAM_UNLOCK (src->srcpad);
1826 /* downstream serialized events */
1831 /* queue EOS and make sure the task or pull function performs the EOS
1834 * For push mode, This will be done in 3 steps. It is required to not
1835 * block here as gst_element_send_event() will hold the STATE_LOCK, hence
1836 * blocking would prevent asynchronous state change to complete.
1838 * 1. We stop the streaming thread
1839 * 2. We set the pending eos
1840 * 3. We start the streaming thread again, so it is performed
1843 * For pull mode, we simply mark the pending EOS without flushing.
1846 GST_OBJECT_LOCK (src->srcpad);
1847 push_mode = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1848 GST_OBJECT_UNLOCK (src->srcpad);
1851 gst_base_src_set_flushing (src, TRUE);
1853 GST_PAD_STREAM_LOCK (src->srcpad);
1854 gst_base_src_set_flushing (src, FALSE);
1856 GST_OBJECT_LOCK (src);
1857 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1858 if (src->priv->pending_eos)
1859 gst_event_unref (src->priv->pending_eos);
1860 src->priv->pending_eos = event;
1861 GST_OBJECT_UNLOCK (src);
1863 GST_DEBUG_OBJECT (src,
1864 "EOS marked, start task for asynchronous handling");
1865 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1868 GST_PAD_STREAM_UNLOCK (src->srcpad);
1870 /* In pull mode, we need not to return flushing to downstream, though
1871 * the stream lock is not kept after getrange was unblocked */
1872 GST_OBJECT_LOCK (src);
1873 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1874 if (src->priv->pending_eos)
1875 gst_event_unref (src->priv->pending_eos);
1876 src->priv->pending_eos = event;
1877 GST_OBJECT_UNLOCK (src);
1879 gst_base_src_set_pool_flushing (src, TRUE);
1881 bclass->unlock (src);
1883 GST_PAD_STREAM_LOCK (src->srcpad);
1884 if (bclass->unlock_stop)
1885 bclass->unlock_stop (src);
1886 gst_base_src_set_pool_flushing (src, TRUE);
1887 GST_PAD_STREAM_UNLOCK (src->srcpad);
1895 case GST_EVENT_SEGMENT:
1896 /* sending random SEGMENT downstream can break sync. */
1899 case GST_EVENT_SINK_MESSAGE:
1900 case GST_EVENT_CUSTOM_DOWNSTREAM:
1901 case GST_EVENT_CUSTOM_BOTH:
1902 case GST_EVENT_PROTECTION:
1903 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH, PROTECTION in the dataflow */
1904 GST_OBJECT_LOCK (src);
1905 src->priv->pending_events =
1906 g_list_append (src->priv->pending_events, event);
1907 g_atomic_int_set (&src->priv->have_events, TRUE);
1908 GST_OBJECT_UNLOCK (src);
1912 case GST_EVENT_BUFFERSIZE:
1913 /* does not seem to make much sense currently */
1916 /* upstream events */
1918 /* elements should override send_event and do something */
1920 case GST_EVENT_SEEK:
1924 GST_OBJECT_LOCK (src->srcpad);
1925 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1927 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1928 GST_OBJECT_UNLOCK (src->srcpad);
1931 GST_DEBUG_OBJECT (src, "performing seek");
1932 /* when we are running in push mode, we can execute the
1933 * seek right now. */
1934 result = gst_base_src_perform_seek (src, event, TRUE);
1938 /* else we store the event and execute the seek when we
1940 GST_OBJECT_LOCK (src);
1941 GST_DEBUG_OBJECT (src, "queueing seek");
1942 event_p = &src->pending_seek;
1943 gst_event_replace ((GstEvent **) event_p, event);
1944 GST_OBJECT_UNLOCK (src);
1945 /* assume the seek will work */
1950 case GST_EVENT_NAVIGATION:
1951 /* could make sense for elements that do something with navigation events
1952 * but then they would need to override the send_event function */
1954 case GST_EVENT_LATENCY:
1955 /* does not seem to make sense currently */
1959 case GST_EVENT_CUSTOM_UPSTREAM:
1960 /* override send_event if you want this */
1962 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1963 case GST_EVENT_CUSTOM_BOTH_OOB:
1964 /* insert a random custom event into the pipeline */
1965 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1966 result = gst_pad_push_event (src->srcpad, event);
1967 /* we gave away the ref to the event in the push */
1974 /* if we still have a ref to the event, unref it now */
1976 gst_event_unref (event);
1983 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1984 GST_OBJECT_UNLOCK (src->srcpad);
1991 gst_base_src_seekable (GstBaseSrc * src)
1993 GstBaseSrcClass *bclass;
1994 bclass = GST_BASE_SRC_GET_CLASS (src);
1995 if (bclass->is_seekable)
1996 return bclass->is_seekable (src);
2002 gst_base_src_update_qos (GstBaseSrc * src,
2003 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2005 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
2006 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2007 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2009 GST_OBJECT_LOCK (src);
2010 src->priv->proportion = proportion;
2011 src->priv->earliest_time = timestamp + diff;
2012 GST_OBJECT_UNLOCK (src);
2017 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
2021 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
2023 switch (GST_EVENT_TYPE (event)) {
2024 case GST_EVENT_SEEK:
2025 /* is normally called when in push mode */
2026 if (!gst_base_src_seekable (src))
2029 result = gst_base_src_perform_seek (src, event, TRUE);
2031 case GST_EVENT_FLUSH_START:
2032 /* cancel any blocking getrange, is normally called
2033 * when in pull mode. */
2034 result = gst_base_src_set_flushing (src, TRUE);
2036 case GST_EVENT_FLUSH_STOP:
2037 result = gst_base_src_set_flushing (src, FALSE);
2042 GstClockTimeDiff diff;
2043 GstClockTime timestamp;
2045 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2046 gst_base_src_update_qos (src, proportion, diff, timestamp);
2050 case GST_EVENT_RECONFIGURE:
2053 case GST_EVENT_LATENCY:
2065 GST_DEBUG_OBJECT (src, "is not seekable");
2071 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2074 GstBaseSrcClass *bclass;
2075 gboolean result = FALSE;
2077 src = GST_BASE_SRC (parent);
2078 bclass = GST_BASE_SRC_GET_CLASS (src);
2080 if (bclass->event) {
2081 if (!(result = bclass->event (src, event)))
2082 goto subclass_failed;
2086 gst_event_unref (event);
2093 GST_DEBUG_OBJECT (src, "subclass refused event");
2099 gst_base_src_set_property (GObject * object, guint prop_id,
2100 const GValue * value, GParamSpec * pspec)
2104 src = GST_BASE_SRC (object);
2107 case PROP_BLOCKSIZE:
2108 gst_base_src_set_blocksize (src, g_value_get_uint (value));
2110 case PROP_NUM_BUFFERS:
2111 src->num_buffers = g_value_get_int (value);
2113 #ifndef GST_REMOVE_DEPRECATED
2115 src->typefind = g_value_get_boolean (value);
2118 case PROP_DO_TIMESTAMP:
2119 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2128 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2133 src = GST_BASE_SRC (object);
2136 case PROP_BLOCKSIZE:
2137 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2139 case PROP_NUM_BUFFERS:
2140 g_value_set_int (value, src->num_buffers);
2142 #ifndef GST_REMOVE_DEPRECATED
2144 g_value_set_boolean (value, src->typefind);
2147 case PROP_DO_TIMESTAMP:
2148 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2156 /* with STREAM_LOCK and LOCK */
2157 static GstClockReturn
2158 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2163 id = gst_clock_new_single_shot_id (clock, time);
2165 basesrc->clock_id = id;
2166 /* release the live lock while waiting */
2167 GST_LIVE_UNLOCK (basesrc);
2169 ret = gst_clock_id_wait (id, NULL);
2171 GST_LIVE_LOCK (basesrc);
2172 gst_clock_id_unref (id);
2173 basesrc->clock_id = NULL;
2178 /* perform synchronisation on a buffer.
2181 static GstClockReturn
2182 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2184 GstClockReturn result;
2185 GstClockTime start, end;
2186 GstBaseSrcClass *bclass;
2187 GstClockTime base_time;
2189 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2190 gboolean do_timestamp, first, pseudo_live, is_live;
2192 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2195 if (bclass->get_times)
2196 bclass->get_times (basesrc, buffer, &start, &end);
2198 /* get buffer timestamp */
2199 dts = GST_BUFFER_DTS (buffer);
2200 pts = GST_BUFFER_PTS (buffer);
2202 if (GST_CLOCK_TIME_IS_VALID (dts))
2207 /* grab the lock to prepare for clocking and calculate the startup
2209 GST_OBJECT_LOCK (basesrc);
2211 is_live = basesrc->is_live;
2212 /* if we are asked to sync against the clock we are a pseudo live element */
2213 pseudo_live = (start != -1 && is_live);
2214 /* check for the first buffer */
2215 first = (basesrc->priv->latency == -1);
2217 if (timestamp != -1 && pseudo_live) {
2218 GstClockTime latency;
2220 /* we have a timestamp and a sync time, latency is the diff */
2221 if (timestamp <= start)
2222 latency = start - timestamp;
2227 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2228 GST_TIME_ARGS (latency));
2229 /* first time we calculate latency, just configure */
2230 basesrc->priv->latency = latency;
2232 if (basesrc->priv->latency != latency) {
2233 /* we have a new latency, FIXME post latency message */
2234 basesrc->priv->latency = latency;
2235 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2236 GST_TIME_ARGS (latency));
2240 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2241 is_live, start != -1);
2242 basesrc->priv->latency = 0;
2245 /* get clock, if no clock, we can't sync or do timestamps */
2246 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2249 gst_object_ref (clock);
2251 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2253 do_timestamp = basesrc->priv->do_timestamp;
2254 GST_OBJECT_UNLOCK (basesrc);
2256 /* first buffer, calculate the timestamp offset */
2258 GstClockTime running_time;
2260 now = gst_clock_get_time (clock);
2261 running_time = now - base_time;
2263 GST_LOG_OBJECT (basesrc,
2264 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2265 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2266 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2268 if (pseudo_live && timestamp != -1) {
2269 /* live source and we need to sync, add startup latency to all timestamps
2270 * to get the real running_time. Live sources should always timestamp
2271 * according to the current running time. */
2272 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2274 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2275 GST_TIME_ARGS (basesrc->priv->ts_offset));
2277 basesrc->priv->ts_offset = 0;
2278 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2281 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2284 } else if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2285 if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2286 dts = basesrc->segment.start;
2291 GST_BUFFER_DTS (buffer) = dts;
2293 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2294 GST_TIME_ARGS (dts));
2297 /* not the first buffer, the timestamp is the diff between the clock and
2299 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2300 now = gst_clock_get_time (clock);
2302 dts = now - base_time;
2303 GST_BUFFER_DTS (buffer) = dts;
2305 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2306 GST_TIME_ARGS (dts));
2309 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2310 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2313 GST_BUFFER_PTS (buffer) = dts;
2315 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2316 GST_TIME_ARGS (pts));
2319 /* if we don't have a buffer timestamp, we don't sync */
2320 if (!GST_CLOCK_TIME_IS_VALID (start))
2324 /* for pseudo live sources, add our ts_offset to the timestamp */
2325 if (GST_CLOCK_TIME_IS_VALID (pts))
2326 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2327 if (GST_CLOCK_TIME_IS_VALID (dts))
2328 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2329 start += basesrc->priv->ts_offset;
2332 GST_LOG_OBJECT (basesrc,
2333 "waiting for clock, base time %" GST_TIME_FORMAT
2334 ", stream_start %" GST_TIME_FORMAT,
2335 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2337 result = gst_base_src_wait (basesrc, clock, start + base_time);
2339 gst_object_unref (clock);
2341 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2348 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2349 GST_OBJECT_UNLOCK (basesrc);
2350 return GST_CLOCK_OK;
2354 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2355 gst_object_unref (clock);
2356 return GST_CLOCK_OK;
2360 /* Called with STREAM_LOCK and LIVE_LOCK */
2362 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2365 guint64 size, maxsize;
2366 GstBaseSrcClass *bclass;
2369 /* only operate if we are working with bytes */
2370 if (src->segment.format != GST_FORMAT_BYTES)
2373 bclass = GST_BASE_SRC_GET_CLASS (src);
2375 stop = src->segment.stop;
2376 /* get total file size */
2377 size = src->segment.duration;
2379 /* when not doing automatic EOS, just use the stop position. We don't use
2380 * the size to check for EOS */
2381 if (!g_atomic_int_get (&src->priv->automatic_eos))
2383 /* Otherwise, the max amount of bytes to read is the total
2384 * size or up to the segment.stop if present. */
2385 else if (stop != -1)
2386 maxsize = size != -1 ? MIN (size, stop) : stop;
2390 GST_DEBUG_OBJECT (src,
2391 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2392 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2393 *length, size, stop, maxsize);
2395 /* check size if we have one */
2396 if (maxsize != -1) {
2397 /* if we run past the end, check if the file became bigger and
2398 * retry. Mind wrap when checking. */
2399 if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2400 /* see if length of the file changed */
2401 if (bclass->get_size)
2402 if (!bclass->get_size (src, &size))
2405 /* when not doing automatic EOS, just use the stop position. We don't use
2406 * the size to check for EOS */
2407 if (!g_atomic_int_get (&src->priv->automatic_eos))
2409 /* Otherwise, the max amount of bytes to read is the total
2410 * size or up to the segment.stop if present. */
2411 else if (stop != -1)
2412 maxsize = size != -1 ? MIN (size, stop) : stop;
2416 if (maxsize != -1) {
2417 /* if we are at or past the end, EOS */
2418 if (G_UNLIKELY (offset >= maxsize))
2419 goto unexpected_length;
2421 /* else we can clip to the end */
2422 if (G_UNLIKELY (offset + *length >= maxsize))
2423 *length = maxsize - offset;
2428 /* keep track of current duration. segment is in bytes, we checked
2430 GST_OBJECT_LOCK (src);
2431 src->segment.duration = size;
2432 GST_OBJECT_UNLOCK (src);
2439 GST_WARNING_OBJECT (src, "processing at or past EOS");
2444 /* must be called with LIVE_LOCK */
2445 static GstFlowReturn
2446 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2450 GstBaseSrcClass *bclass;
2451 GstClockReturn status;
2455 bclass = GST_BASE_SRC_GET_CLASS (src);
2459 if (G_UNLIKELY (!src->live_running)) {
2460 ret = gst_base_src_wait_playing_unlocked (src);
2461 if (ret != GST_FLOW_OK)
2466 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2467 && !GST_BASE_SRC_IS_STARTING (src)))
2470 if (G_UNLIKELY (!bclass->create))
2473 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2474 goto unexpected_length;
2476 /* track position */
2477 GST_OBJECT_LOCK (src);
2478 if (src->segment.format == GST_FORMAT_BYTES)
2479 src->segment.position = offset;
2480 GST_OBJECT_UNLOCK (src);
2482 /* normally we don't count buffers */
2483 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2484 if (src->num_buffers_left == 0)
2485 goto reached_num_buffers;
2487 src->num_buffers_left--;
2490 /* don't enter the create function if a pending EOS event was set. For the
2491 * logic of the has_pending_eos, check the event function of this class. */
2492 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2493 src->priv->forced_eos = TRUE;
2497 GST_DEBUG_OBJECT (src,
2498 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2499 G_GINT64_FORMAT, offset, length, src->segment.time);
2501 res_buf = in_buf = *buf;
2503 GST_LIVE_UNLOCK (src);
2504 ret = bclass->create (src, offset, length, &res_buf);
2505 GST_LIVE_LOCK (src);
2507 /* As we released the LIVE_LOCK, the state may have changed */
2509 if (G_UNLIKELY (!src->live_running)) {
2510 GstFlowReturn wait_ret;
2511 wait_ret = gst_base_src_wait_playing_unlocked (src);
2512 if (wait_ret != GST_FLOW_OK) {
2513 if (ret == GST_FLOW_OK && *buf == NULL)
2514 gst_buffer_unref (res_buf);
2521 /* The create function could be unlocked because we have a pending EOS. It's
2522 * possible that we have a valid buffer from create that we need to
2523 * discard when the create function returned _OK. */
2524 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2525 if (ret == GST_FLOW_OK) {
2527 gst_buffer_unref (res_buf);
2529 src->priv->forced_eos = TRUE;
2533 if (G_UNLIKELY (ret != GST_FLOW_OK))
2536 /* fallback in case the create function didn't fill a provided buffer */
2537 if (in_buf != NULL && res_buf != in_buf) {
2541 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2542 "fill the provided buffer, copying");
2544 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2547 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2548 gst_buffer_unmap (in_buf, &info);
2549 gst_buffer_set_size (in_buf, copied_size);
2551 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2553 gst_buffer_unref (res_buf);
2557 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2558 if (offset == 0 && src->segment.time == 0
2559 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2560 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2561 res_buf = gst_buffer_make_writable (res_buf);
2562 GST_BUFFER_DTS (res_buf) = 0;
2565 /* now sync before pushing the buffer */
2566 status = gst_base_src_do_sync (src, res_buf);
2568 /* waiting for the clock could have made us flushing */
2569 if (G_UNLIKELY (src->priv->flushing))
2573 case GST_CLOCK_EARLY:
2574 /* the buffer is too late. We currently don't drop the buffer. */
2575 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2578 /* buffer synchronised properly */
2579 GST_DEBUG_OBJECT (src, "buffer ok");
2581 case GST_CLOCK_UNSCHEDULED:
2582 /* this case is triggered when we were waiting for the clock and
2583 * it got unlocked because we did a state change. In any case, get rid of
2586 gst_buffer_unref (res_buf);
2588 if (!src->live_running) {
2589 /* We return FLUSHING when we are not running to stop the dataflow also
2590 * get rid of the produced buffer. */
2591 GST_DEBUG_OBJECT (src,
2592 "clock was unscheduled (%d), returning FLUSHING", status);
2593 ret = GST_FLOW_FLUSHING;
2595 /* If we are running when this happens, we quickly switched between
2596 * pause and playing. We try to produce a new buffer */
2597 GST_DEBUG_OBJECT (src,
2598 "clock was unscheduled (%d), but we are running", status);
2603 /* all other result values are unexpected and errors */
2604 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2605 (_("Internal clock error.")),
2606 ("clock returned unexpected return value %d", status));
2608 gst_buffer_unref (res_buf);
2609 ret = GST_FLOW_ERROR;
2612 if (G_LIKELY (ret == GST_FLOW_OK))
2620 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2621 gst_flow_get_name (ret));
2626 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2627 gst_flow_get_name (ret));
2632 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2633 (_("Failed to map buffer.")),
2634 ("failed to map result buffer in WRITE mode"));
2636 gst_buffer_unref (res_buf);
2637 return GST_FLOW_ERROR;
2641 GST_DEBUG_OBJECT (src, "getrange but not started");
2642 return GST_FLOW_FLUSHING;
2646 GST_DEBUG_OBJECT (src, "no create function");
2647 return GST_FLOW_NOT_SUPPORTED;
2651 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2652 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2653 return GST_FLOW_EOS;
2655 reached_num_buffers:
2657 GST_DEBUG_OBJECT (src, "sent all buffers");
2658 return GST_FLOW_EOS;
2662 GST_DEBUG_OBJECT (src, "we are flushing");
2664 gst_buffer_unref (res_buf);
2665 return GST_FLOW_FLUSHING;
2669 GST_DEBUG_OBJECT (src, "we are EOS");
2670 return GST_FLOW_EOS;
2674 static GstFlowReturn
2675 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2676 guint length, GstBuffer ** buf)
2681 src = GST_BASE_SRC_CAST (parent);
2683 GST_LIVE_LOCK (src);
2684 if (G_UNLIKELY (src->priv->flushing))
2687 res = gst_base_src_get_range (src, offset, length, buf);
2690 GST_LIVE_UNLOCK (src);
2697 GST_DEBUG_OBJECT (src, "we are flushing");
2698 res = GST_FLOW_FLUSHING;
2704 gst_base_src_is_random_access (GstBaseSrc * src)
2706 /* we need to start the basesrc to check random access */
2707 if (!GST_BASE_SRC_IS_STARTED (src)) {
2708 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2709 if (G_LIKELY (gst_base_src_start (src))) {
2710 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2712 gst_base_src_stop (src);
2716 return src->random_access;
2721 GST_DEBUG_OBJECT (src, "failed to start");
2726 /* Called with STREAM_LOCK */
2728 gst_base_src_loop (GstPad * pad)
2731 GstBuffer *buf = NULL;
2736 GList *pending_events = NULL, *tmp;
2740 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2742 /* Just leave immediately if we're flushing */
2743 GST_LIVE_LOCK (src);
2744 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2746 GST_LIVE_UNLOCK (src);
2748 /* Just return if EOS is pushed again, as the app might be unaware that an
2749 * EOS have been sent already */
2750 if (GST_PAD_IS_EOS (pad)) {
2751 GST_DEBUG_OBJECT (src, "Pad is marked as EOS, pause the task");
2752 gst_pad_pause_task (pad);
2756 gst_base_src_send_stream_start (src);
2758 /* The stream-start event could've caused something to flush us */
2759 GST_LIVE_LOCK (src);
2760 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2762 GST_LIVE_UNLOCK (src);
2764 /* check if we need to renegotiate */
2765 if (gst_pad_check_reconfigure (pad)) {
2766 if (!gst_base_src_negotiate (src)) {
2767 gst_pad_mark_reconfigure (pad);
2768 if (GST_PAD_IS_FLUSHING (pad)) {
2769 GST_LIVE_LOCK (src);
2772 goto negotiate_failed;
2777 GST_LIVE_LOCK (src);
2779 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2782 blocksize = src->blocksize;
2784 /* if we operate in bytes, we can calculate an offset */
2785 if (src->segment.format == GST_FORMAT_BYTES) {
2786 position = src->segment.position;
2787 /* for negative rates, start with subtracting the blocksize */
2788 if (src->segment.rate < 0.0) {
2789 /* we cannot go below segment.start */
2790 if (position > src->segment.start + blocksize)
2791 position -= blocksize;
2793 /* last block, remainder up to segment.start */
2794 blocksize = position - src->segment.start;
2795 position = src->segment.start;
2801 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2802 GST_TIME_ARGS (position), blocksize);
2804 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2805 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2806 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2807 gst_flow_get_name (ret));
2808 GST_LIVE_UNLOCK (src);
2811 /* this should not happen */
2812 if (G_UNLIKELY (buf == NULL))
2815 /* push events to close/start our segment before we push the buffer. */
2816 if (G_UNLIKELY (src->priv->segment_pending)) {
2817 GstEvent *seg_event = gst_event_new_segment (&src->segment);
2819 gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2820 src->priv->segment_seqnum = gst_util_seqnum_next ();
2821 gst_pad_push_event (pad, seg_event);
2822 src->priv->segment_pending = FALSE;
2825 if (g_atomic_int_get (&src->priv->have_events)) {
2826 GST_OBJECT_LOCK (src);
2827 /* take the events */
2828 pending_events = src->priv->pending_events;
2829 src->priv->pending_events = NULL;
2830 g_atomic_int_set (&src->priv->have_events, FALSE);
2831 GST_OBJECT_UNLOCK (src);
2834 /* Push out pending events if any */
2835 if (G_UNLIKELY (pending_events != NULL)) {
2836 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2837 GstEvent *ev = (GstEvent *) tmp->data;
2838 gst_pad_push_event (pad, ev);
2840 g_list_free (pending_events);
2843 /* figure out the new position */
2844 switch (src->segment.format) {
2845 case GST_FORMAT_BYTES:
2847 guint bufsize = gst_buffer_get_size (buf);
2849 /* we subtracted above for negative rates */
2850 if (src->segment.rate >= 0.0)
2851 position += bufsize;
2854 case GST_FORMAT_TIME:
2856 GstClockTime start, duration;
2858 start = GST_BUFFER_TIMESTAMP (buf);
2859 duration = GST_BUFFER_DURATION (buf);
2861 if (GST_CLOCK_TIME_IS_VALID (start))
2864 position = src->segment.position;
2866 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2867 if (src->segment.rate >= 0.0)
2868 position += duration;
2869 else if (position > duration)
2870 position -= duration;
2876 case GST_FORMAT_DEFAULT:
2877 if (src->segment.rate >= 0.0)
2878 position = GST_BUFFER_OFFSET_END (buf);
2880 position = GST_BUFFER_OFFSET (buf);
2886 if (position != -1) {
2887 if (src->segment.rate >= 0.0) {
2888 /* positive rate, check if we reached the stop */
2889 if (src->segment.stop != -1) {
2890 if (position >= src->segment.stop) {
2892 position = src->segment.stop;
2896 /* negative rate, check if we reached the start. start is always set to
2897 * something different from -1 */
2898 if (position <= src->segment.start) {
2900 position = src->segment.start;
2902 /* when going reverse, all buffers are DISCONT */
2903 src->priv->discont = TRUE;
2905 GST_OBJECT_LOCK (src);
2906 src->segment.position = position;
2907 GST_OBJECT_UNLOCK (src);
2910 if (G_UNLIKELY (src->priv->discont)) {
2911 GST_INFO_OBJECT (src, "marking pending DISCONT");
2912 buf = gst_buffer_make_writable (buf);
2913 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2914 src->priv->discont = FALSE;
2916 GST_LIVE_UNLOCK (src);
2918 ret = gst_pad_push (pad, buf);
2919 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2920 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2921 goto not_negotiated;
2923 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2924 gst_flow_get_name (ret));
2928 /* Segment pending means that a new segment was configured
2929 * during this loop run */
2930 if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2931 GST_INFO_OBJECT (src, "pausing after end of segment");
2942 if (gst_pad_needs_reconfigure (pad)) {
2943 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2946 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2947 * a pending negotiation request on our srcpad */
2951 GST_DEBUG_OBJECT (src, "Not negotiated");
2952 ret = GST_FLOW_NOT_NEGOTIATED;
2957 GST_DEBUG_OBJECT (src, "we are flushing");
2958 GST_LIVE_UNLOCK (src);
2959 ret = GST_FLOW_FLUSHING;
2964 const gchar *reason = gst_flow_get_name (ret);
2967 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2968 src->running = FALSE;
2969 gst_pad_pause_task (pad);
2970 if (ret == GST_FLOW_EOS) {
2971 gboolean flag_segment;
2975 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2976 format = src->segment.format;
2977 position = src->segment.position;
2979 /* perform EOS logic */
2980 if (src->priv->forced_eos) {
2981 g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
2982 GST_OBJECT_LOCK (src);
2983 event = src->priv->pending_eos;
2984 src->priv->pending_eos = NULL;
2985 GST_OBJECT_UNLOCK (src);
2987 } else if (flag_segment) {
2988 GstMessage *message;
2990 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2992 gst_message_set_seqnum (message, src->priv->seqnum);
2993 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2994 event = gst_event_new_segment_done (format, position);
2995 gst_event_set_seqnum (event, src->priv->seqnum);
2998 event = gst_event_new_eos ();
2999 gst_event_set_seqnum (event, src->priv->seqnum);
3002 gst_pad_push_event (pad, event);
3003 src->priv->forced_eos = FALSE;
3005 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
3006 event = gst_event_new_eos ();
3007 gst_event_set_seqnum (event, src->priv->seqnum);
3008 /* for fatal errors we post an error message, post the error
3009 * first so the app knows about the error first.
3010 * Also don't do this for FLUSHING because it happens
3011 * due to flushing and posting an error message because of
3012 * that is the wrong thing to do, e.g. when we're doing
3013 * a flushing seek. */
3014 GST_ELEMENT_FLOW_ERROR (src, ret);
3015 gst_pad_push_event (pad, event);
3021 GST_ELEMENT_ERROR (src, STREAM, FAILED,
3022 (_("Internal data flow error.")), ("element returned NULL buffer"));
3023 GST_LIVE_UNLOCK (src);
3029 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
3030 GstAllocator * allocator, GstAllocationParams * params)
3032 GstAllocator *oldalloc;
3033 GstBufferPool *oldpool;
3034 GstBaseSrcPrivate *priv = basesrc->priv;
3037 GST_DEBUG_OBJECT (basesrc, "activate pool");
3038 if (!gst_buffer_pool_set_active (pool, TRUE))
3039 goto activate_failed;
3042 GST_OBJECT_LOCK (basesrc);
3043 oldpool = priv->pool;
3046 oldalloc = priv->allocator;
3047 priv->allocator = allocator;
3050 gst_object_ref (priv->pool);
3051 if (priv->allocator)
3052 gst_object_ref (priv->allocator);
3055 priv->params = *params;
3057 gst_allocation_params_init (&priv->params);
3058 GST_OBJECT_UNLOCK (basesrc);
3061 /* only deactivate if the pool is not the one we're using */
3062 if (oldpool != pool) {
3063 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
3064 gst_buffer_pool_set_active (oldpool, FALSE);
3066 gst_object_unref (oldpool);
3069 gst_object_unref (oldalloc);
3076 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
3082 gst_base_src_set_pool_flushing (GstBaseSrc * basesrc, gboolean flushing)
3084 GstBaseSrcPrivate *priv = basesrc->priv;
3085 GstBufferPool *pool;
3087 GST_OBJECT_LOCK (basesrc);
3088 if ((pool = priv->pool))
3089 pool = gst_object_ref (pool);
3090 GST_OBJECT_UNLOCK (basesrc);
3093 gst_buffer_pool_set_flushing (pool, flushing);
3094 gst_object_unref (pool);
3100 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3103 GstBufferPool *pool;
3104 guint size, min, max;
3105 GstAllocator *allocator;
3106 GstAllocationParams params;
3107 GstStructure *config;
3108 gboolean update_allocator;
3110 gst_query_parse_allocation (query, &outcaps, NULL);
3112 /* we got configuration from our peer or the decide_allocation method,
3114 if (gst_query_get_n_allocation_params (query) > 0) {
3115 /* try the allocator */
3116 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3117 update_allocator = TRUE;
3120 gst_allocation_params_init (¶ms);
3121 update_allocator = FALSE;
3124 if (gst_query_get_n_allocation_pools (query) > 0) {
3125 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3128 /* no pool, we can make our own */
3129 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3130 pool = gst_buffer_pool_new ();
3134 size = min = max = 0;
3139 config = gst_buffer_pool_get_config (pool);
3140 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3141 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3143 /* buffer pool may have to do some changes */
3144 if (!gst_buffer_pool_set_config (pool, config)) {
3145 config = gst_buffer_pool_get_config (pool);
3147 /* If change are not acceptable, fallback to generic pool */
3148 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3150 GST_DEBUG_OBJECT (basesrc, "unsupported pool, making new pool");
3152 gst_object_unref (pool);
3153 pool = gst_buffer_pool_new ();
3154 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3155 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3158 if (!gst_buffer_pool_set_config (pool, config))
3163 if (update_allocator)
3164 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
3166 gst_query_add_allocation_param (query, allocator, ¶ms);
3168 gst_object_unref (allocator);
3171 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3172 gst_object_unref (pool);
3178 GST_ELEMENT_ERROR (basesrc, RESOURCE, SETTINGS,
3179 ("Failed to configure the buffer pool"),
3180 ("Configuration is most likely invalid, please report this issue."));
3181 gst_object_unref (pool);
3186 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3188 GstBaseSrcClass *bclass;
3189 gboolean result = TRUE;
3191 GstBufferPool *pool = NULL;
3192 GstAllocator *allocator = NULL;
3193 GstAllocationParams params;
3195 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3197 /* make query and let peer pad answer, we don't really care if it worked or
3198 * not, if it failed, the allocation query would contain defaults and the
3199 * subclass would then set better values if needed */
3200 query = gst_query_new_allocation (caps, TRUE);
3201 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3202 /* not a problem, just debug a little */
3203 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3206 g_assert (bclass->decide_allocation != NULL);
3207 result = bclass->decide_allocation (basesrc, query);
3209 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3213 goto no_decide_allocation;
3215 /* we got configuration from our peer or the decide_allocation method,
3217 if (gst_query_get_n_allocation_params (query) > 0) {
3218 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3221 gst_allocation_params_init (¶ms);
3224 if (gst_query_get_n_allocation_pools (query) > 0)
3225 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3227 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
3230 gst_object_unref (allocator);
3232 gst_object_unref (pool);
3234 gst_query_unref (query);
3239 no_decide_allocation:
3241 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3242 gst_query_unref (query);
3248 /* default negotiation code.
3250 * Take intersection between src and sink pads, take first
3254 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3257 GstCaps *caps = NULL;
3258 GstCaps *peercaps = NULL;
3259 gboolean result = FALSE;
3261 /* first see what is possible on our source pad */
3262 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3263 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3264 /* nothing or anything is allowed, we're done */
3265 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3266 goto no_nego_needed;
3268 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3271 /* get the peer caps */
3272 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3273 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3275 /* The result is already a subset of our caps */
3277 gst_caps_unref (thiscaps);
3279 /* no peer, work with our own caps then */
3282 if (caps && !gst_caps_is_empty (caps)) {
3284 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3285 if (gst_caps_is_any (caps)) {
3286 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3287 /* hmm, still anything, so element can do anything and
3288 * nego is not needed */
3291 caps = gst_base_src_fixate (basesrc, caps);
3292 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3293 if (gst_caps_is_fixed (caps)) {
3294 /* yay, fixed caps, use those then, it's possible that the subclass does
3295 * not accept this caps after all and we have to fail. */
3296 result = gst_base_src_set_caps (basesrc, caps);
3299 gst_caps_unref (caps);
3302 gst_caps_unref (caps);
3303 GST_DEBUG_OBJECT (basesrc, "no common caps");
3309 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3311 gst_caps_unref (thiscaps);
3316 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3317 ("No supported formats found"),
3318 ("This element did not produce valid caps"));
3320 gst_caps_unref (thiscaps);
3326 gst_base_src_negotiate (GstBaseSrc * basesrc)
3328 GstBaseSrcClass *bclass;
3331 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3333 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3335 if (G_LIKELY (bclass->negotiate))
3336 result = bclass->negotiate (basesrc);
3340 if (G_LIKELY (result)) {
3343 caps = gst_pad_get_current_caps (basesrc->srcpad);
3345 result = gst_base_src_prepare_allocation (basesrc, caps);
3348 gst_caps_unref (caps);
3354 gst_base_src_start (GstBaseSrc * basesrc)
3356 GstBaseSrcClass *bclass;
3359 GST_LIVE_LOCK (basesrc);
3361 GST_OBJECT_LOCK (basesrc);
3362 if (GST_BASE_SRC_IS_STARTING (basesrc))
3364 if (GST_BASE_SRC_IS_STARTED (basesrc))
3367 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3368 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3369 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3370 GST_OBJECT_UNLOCK (basesrc);
3372 basesrc->num_buffers_left = basesrc->num_buffers;
3373 basesrc->running = FALSE;
3374 basesrc->priv->segment_pending = FALSE;
3375 basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3376 basesrc->priv->forced_eos = FALSE;
3377 GST_LIVE_UNLOCK (basesrc);
3379 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3381 result = bclass->start (basesrc);
3386 goto could_not_start;
3388 if (!gst_base_src_is_async (basesrc)) {
3389 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3390 /* not really waiting here, we call this to get the result
3391 * from the start_complete call */
3392 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3400 GST_DEBUG_OBJECT (basesrc, "was starting");
3401 GST_OBJECT_UNLOCK (basesrc);
3402 GST_LIVE_UNLOCK (basesrc);
3407 GST_DEBUG_OBJECT (basesrc, "was started");
3408 GST_OBJECT_UNLOCK (basesrc);
3409 GST_LIVE_UNLOCK (basesrc);
3414 GST_DEBUG_OBJECT (basesrc, "could not start");
3415 /* subclass is supposed to post a message but we post one as a fallback
3416 * just in case. We don't have to call _stop. */
3417 GST_ELEMENT_ERROR (basesrc, CORE, STATE_CHANGE, (NULL),
3418 ("Failed to start"));
3419 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3425 * gst_base_src_start_complete:
3426 * @basesrc: base source instance
3427 * @ret: a #GstFlowReturn
3429 * Complete an asynchronous start operation. When the subclass overrides the
3430 * start method, it should call gst_base_src_start_complete() when the start
3431 * operation completes either from the same thread or from an asynchronous
3435 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3444 if (ret != GST_FLOW_OK)
3447 GST_DEBUG_OBJECT (basesrc, "starting source");
3448 format = basesrc->segment.format;
3450 /* figure out the size */
3453 if (format == GST_FORMAT_BYTES) {
3454 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3456 if (bclass->get_size) {
3457 if (!(have_size = bclass->get_size (basesrc, &size)))
3460 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3461 /* only update the size when operating in bytes, subclass is supposed
3462 * to set duration in the start method for other formats */
3463 GST_OBJECT_LOCK (basesrc);
3464 basesrc->segment.duration = size;
3465 GST_OBJECT_UNLOCK (basesrc);
3468 GST_DEBUG_OBJECT (basesrc,
3469 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3470 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3471 basesrc->segment.duration);
3473 seekable = gst_base_src_seekable (basesrc);
3474 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3476 /* update for random access flag */
3477 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3479 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3481 /* stop flushing now but for live sources, still block in the LIVE lock when
3482 * we are not yet PLAYING */
3483 gst_base_src_set_flushing (basesrc, FALSE);
3485 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3487 GST_OBJECT_LOCK (basesrc->srcpad);
3488 mode = GST_PAD_MODE (basesrc->srcpad);
3489 GST_OBJECT_UNLOCK (basesrc->srcpad);
3491 /* take the stream lock here, we only want to let the task run when we have
3492 * set the STARTED flag */
3493 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3495 case GST_PAD_MODE_PUSH:
3496 /* do initial seek, which will start the task */
3497 GST_OBJECT_LOCK (basesrc);
3498 event = basesrc->pending_seek;
3499 basesrc->pending_seek = NULL;
3500 GST_OBJECT_UNLOCK (basesrc);
3502 /* The perform seek code will start the task when finished. We don't have to
3503 * unlock the streaming thread because it is not running yet */
3504 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3508 gst_event_unref (event);
3510 case GST_PAD_MODE_PULL:
3511 /* if not random_access, we cannot operate in pull mode for now */
3512 if (G_UNLIKELY (!basesrc->random_access))
3516 goto not_activated_yet;
3520 GST_OBJECT_LOCK (basesrc);
3521 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3522 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3523 basesrc->priv->start_result = ret;
3524 GST_ASYNC_SIGNAL (basesrc);
3525 GST_OBJECT_UNLOCK (basesrc);
3527 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3533 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3534 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3535 gst_base_src_stop (basesrc);
3537 gst_event_unref (event);
3538 ret = GST_FLOW_ERROR;
3543 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3544 gst_base_src_stop (basesrc);
3545 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3546 ret = GST_FLOW_ERROR;
3551 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3552 gst_base_src_stop (basesrc);
3553 GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3554 ret = GST_FLOW_ERROR;
3559 GST_OBJECT_LOCK (basesrc);
3560 basesrc->priv->start_result = ret;
3561 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3562 GST_ASYNC_SIGNAL (basesrc);
3563 GST_OBJECT_UNLOCK (basesrc);
3569 * gst_base_src_start_wait:
3570 * @basesrc: base source instance
3572 * Wait until the start operation completes.
3574 * Returns: a #GstFlowReturn.
3577 gst_base_src_start_wait (GstBaseSrc * basesrc)
3579 GstFlowReturn result;
3581 GST_OBJECT_LOCK (basesrc);
3582 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3583 GST_ASYNC_WAIT (basesrc);
3585 result = basesrc->priv->start_result;
3586 GST_OBJECT_UNLOCK (basesrc);
3588 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3594 gst_base_src_stop (GstBaseSrc * basesrc)
3596 GstBaseSrcClass *bclass;
3597 gboolean result = TRUE;
3599 GST_DEBUG_OBJECT (basesrc, "stopping source");
3602 gst_base_src_set_flushing (basesrc, TRUE);
3604 gst_pad_stop_task (basesrc->srcpad);
3606 GST_OBJECT_LOCK (basesrc);
3607 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3610 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3611 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3612 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3613 GST_ASYNC_SIGNAL (basesrc);
3614 GST_OBJECT_UNLOCK (basesrc);
3616 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3618 result = bclass->stop (basesrc);
3620 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3626 GST_DEBUG_OBJECT (basesrc, "was stopped");
3627 GST_OBJECT_UNLOCK (basesrc);
3632 /* start or stop flushing dataprocessing
3635 gst_base_src_set_flushing (GstBaseSrc * basesrc, gboolean flushing)
3637 GstBaseSrcClass *bclass;
3639 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3641 GST_DEBUG_OBJECT (basesrc, "flushing %d", flushing);
3644 gst_base_src_set_pool_flushing (basesrc, TRUE);
3645 /* unlock any subclasses to allow turning off the streaming thread */
3647 bclass->unlock (basesrc);
3650 /* the live lock is released when we are blocked, waiting for playing,
3651 * when we sync to the clock or creating a buffer */
3652 GST_LIVE_LOCK (basesrc);
3653 basesrc->priv->flushing = flushing;
3655 /* clear pending EOS if any */
3656 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3657 GST_OBJECT_LOCK (basesrc);
3658 CLEAR_PENDING_EOS (basesrc);
3659 basesrc->priv->forced_eos = FALSE;
3660 GST_OBJECT_UNLOCK (basesrc);
3663 /* unblock clock sync (if any) or any other blocking thing */
3664 if (basesrc->clock_id)
3665 gst_clock_id_unschedule (basesrc->clock_id);
3667 gst_base_src_set_pool_flushing (basesrc, FALSE);
3669 /* Drop all delayed events */
3670 GST_OBJECT_LOCK (basesrc);
3671 if (basesrc->priv->pending_events) {
3672 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3674 g_list_free (basesrc->priv->pending_events);
3675 basesrc->priv->pending_events = NULL;
3676 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3678 GST_OBJECT_UNLOCK (basesrc);
3681 GST_LIVE_SIGNAL (basesrc);
3682 GST_LIVE_UNLOCK (basesrc);
3685 /* Now wait for the stream lock to be released and clear our unlock request */
3686 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3687 if (bclass->unlock_stop)
3688 bclass->unlock_stop (basesrc);
3689 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3695 /* the purpose of this function is to make sure that a live source blocks in the
3696 * LIVE lock or leaves the LIVE lock and continues playing. */
3698 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3700 /* we are now able to grab the LIVE lock, when we get it, we can be
3701 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3703 GST_LIVE_LOCK (basesrc);
3704 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3706 /* unblock clock sync (if any) */
3707 if (basesrc->clock_id)
3708 gst_clock_id_unschedule (basesrc->clock_id);
3710 /* configure what to do when we get to the LIVE lock. */
3711 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3712 basesrc->live_running = live_play;
3717 /* for live sources we restart the timestamp correction */
3718 GST_OBJECT_LOCK (basesrc);
3719 basesrc->priv->latency = -1;
3720 GST_OBJECT_UNLOCK (basesrc);
3721 /* have to restart the task in case it stopped because of the unlock when
3722 * we went to PAUSED. Only do this if we operating in push mode. */
3723 GST_OBJECT_LOCK (basesrc->srcpad);
3724 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3725 GST_OBJECT_UNLOCK (basesrc->srcpad);
3727 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3728 basesrc->srcpad, NULL);
3729 GST_DEBUG_OBJECT (basesrc, "signal");
3730 GST_LIVE_SIGNAL (basesrc);
3732 GST_LIVE_UNLOCK (basesrc);
3738 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3740 GstBaseSrc *basesrc;
3742 basesrc = GST_BASE_SRC (parent);
3744 /* prepare subclass first */
3746 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3748 if (G_UNLIKELY (!basesrc->can_activate_push))
3749 goto no_push_activation;
3751 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3754 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3755 /* now we can stop the source */
3756 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3764 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3769 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3774 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3780 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3782 GstBaseSrc *basesrc;
3784 basesrc = GST_BASE_SRC (parent);
3786 /* prepare subclass first */
3788 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3789 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3792 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3793 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3801 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3806 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3812 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3813 GstPadMode mode, gboolean active)
3816 GstBaseSrc *src = GST_BASE_SRC (parent);
3818 src->priv->stream_start_pending = FALSE;
3820 GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3823 case GST_PAD_MODE_PULL:
3824 res = gst_base_src_activate_pull (pad, parent, active);
3826 case GST_PAD_MODE_PUSH:
3827 src->priv->stream_start_pending = active;
3828 res = gst_base_src_activate_push (pad, parent, active);
3831 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3839 static GstStateChangeReturn
3840 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3842 GstBaseSrc *basesrc;
3843 GstStateChangeReturn result;
3844 gboolean no_preroll = FALSE;
3846 basesrc = GST_BASE_SRC (element);
3848 switch (transition) {
3849 case GST_STATE_CHANGE_NULL_TO_READY:
3851 case GST_STATE_CHANGE_READY_TO_PAUSED:
3852 no_preroll = gst_base_src_is_live (basesrc);
3854 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3855 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3856 if (gst_base_src_is_live (basesrc)) {
3857 /* now we can start playback */
3858 gst_base_src_set_playing (basesrc, TRUE);
3866 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3867 transition)) == GST_STATE_CHANGE_FAILURE)
3870 switch (transition) {
3871 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3872 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3873 if (gst_base_src_is_live (basesrc)) {
3874 /* make sure we block in the live cond in PAUSED */
3875 gst_base_src_set_playing (basesrc, FALSE);
3879 case GST_STATE_CHANGE_PAUSED_TO_READY:
3881 /* we don't need to unblock anything here, the pad deactivation code
3882 * already did this */
3883 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3884 GST_OBJECT_LOCK (basesrc);
3885 CLEAR_PENDING_EOS (basesrc);
3886 GST_OBJECT_UNLOCK (basesrc);
3888 gst_event_replace (&basesrc->pending_seek, NULL);
3891 case GST_STATE_CHANGE_READY_TO_NULL:
3897 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3898 result = GST_STATE_CHANGE_NO_PREROLL;
3905 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3911 * gst_base_src_get_buffer_pool:
3912 * @src: a #GstBaseSrc
3914 * Returns: (transfer full): the instance of the #GstBufferPool used
3915 * by the src; unref it after usage.
3918 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3920 GstBufferPool *ret = NULL;
3922 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3924 GST_OBJECT_LOCK (src);
3925 if (src->priv->pool)
3926 ret = gst_object_ref (src->priv->pool);
3927 GST_OBJECT_UNLOCK (src);
3933 * gst_base_src_get_allocator:
3934 * @src: a #GstBaseSrc
3935 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3937 * @params: (out) (allow-none) (transfer full): the
3938 * #GstAllocationParams of @allocator
3940 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3941 * used by the base class and its @params.
3943 * Unref the @allocator after usage.
3946 gst_base_src_get_allocator (GstBaseSrc * src,
3947 GstAllocator ** allocator, GstAllocationParams * params)
3949 g_return_if_fail (GST_IS_BASE_SRC (src));
3951 GST_OBJECT_LOCK (src);
3953 *allocator = src->priv->allocator ?
3954 gst_object_ref (src->priv->allocator) : NULL;
3957 *params = src->priv->params;
3958 GST_OBJECT_UNLOCK (src);