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 /* The basesrc implementation need to respect the following locking order:
209 struct _GstBaseSrcPrivate
211 gboolean discont; /* STREAM_LOCK */
212 gboolean flushing; /* LIVE_LOCK */
214 GstFlowReturn start_result; /* OBJECT_LOCK */
215 gboolean async; /* OBJECT_LOCK */
217 /* if a stream-start event should be sent */
218 gboolean stream_start_pending; /* STREAM_LOCK */
220 /* if segment should be sent and a
221 * seqnum if it was originated by a seek */
222 gboolean segment_pending; /* OBJECT_LOCK */
223 guint32 segment_seqnum; /* OBJECT_LOCK */
225 /* if EOS is pending (atomic) */
226 GstEvent *pending_eos; /* OBJECT_LOCK */
227 gint has_pending_eos; /* atomic */
229 /* if the eos was caused by a forced eos from the application */
230 gboolean forced_eos; /* LIVE_LOCK */
232 /* startup latency is the time it takes between going to PLAYING and producing
233 * the first BUFFER with running_time 0. This value is included in the latency
235 GstClockTime latency; /* OBJECT_LOCK */
236 /* timestamp offset, this is the offset add to the values of gst_times for
237 * pseudo live sources */
238 GstClockTimeDiff ts_offset; /* OBJECT_LOCK */
240 gboolean do_timestamp; /* OBJECT_LOCK */
241 volatile gint dynamic_size; /* atomic */
242 volatile gint automatic_eos; /* atomic */
244 /* stream sequence number */
245 guint32 seqnum; /* STREAM_LOCK */
247 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
248 * pushed in the data stream */
249 GList *pending_events; /* OBJECT_LOCK */
250 volatile gint have_events; /* OBJECT_LOCK */
252 /* QoS *//* with LOCK */
253 gdouble proportion; /* OBJECT_LOCK */
254 GstClockTime earliest_time; /* OBJECT_LOCK */
256 GstBufferPool *pool; /* OBJECT_LOCK */
257 GstAllocator *allocator; /* OBJECT_LOCK */
258 GstAllocationParams params; /* OBJECT_LOCK */
260 GCond async_cond; /* OBJECT_LOCK */
262 /* for _submit_buffer_list() */
263 GstBufferList *pending_bufferlist;
266 #define BASE_SRC_HAS_PENDING_BUFFER_LIST(src) \
267 ((src)->priv->pending_bufferlist != NULL)
269 static GstElementClass *parent_class = NULL;
270 static gint private_offset = 0;
272 static void gst_base_src_class_init (GstBaseSrcClass * klass);
273 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
274 static void gst_base_src_finalize (GObject * object);
278 gst_base_src_get_type (void)
280 static volatile gsize base_src_type = 0;
282 if (g_once_init_enter (&base_src_type)) {
284 static const GTypeInfo base_src_info = {
285 sizeof (GstBaseSrcClass),
288 (GClassInitFunc) gst_base_src_class_init,
293 (GInstanceInitFunc) gst_base_src_init,
296 _type = g_type_register_static (GST_TYPE_ELEMENT,
297 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
300 g_type_add_instance_private (_type, sizeof (GstBaseSrcPrivate));
302 g_once_init_leave (&base_src_type, _type);
304 return base_src_type;
307 static inline GstBaseSrcPrivate *
308 gst_base_src_get_instance_private (GstBaseSrc * self)
310 return (G_STRUCT_MEMBER_P (self, private_offset));
313 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
315 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
316 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
318 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
319 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
320 GstPadMode mode, gboolean active);
321 static void gst_base_src_set_property (GObject * object, guint prop_id,
322 const GValue * value, GParamSpec * pspec);
323 static void gst_base_src_get_property (GObject * object, guint prop_id,
324 GValue * value, GParamSpec * pspec);
325 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
327 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
328 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
330 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
333 static void gst_base_src_set_pool_flushing (GstBaseSrc * basesrc,
335 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
336 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
337 GstSegment * segment);
338 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
339 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
340 GstEvent * event, GstSegment * segment);
341 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
342 guint64 offset, guint size, GstBuffer ** buf);
343 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
344 guint64 offset, guint size, GstBuffer ** buf);
345 static gboolean gst_base_src_decide_allocation_default (GstBaseSrc * basesrc,
348 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
351 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
352 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
354 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
355 GstStateChange transition);
357 static void gst_base_src_loop (GstPad * pad);
358 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
359 guint64 offset, guint length, GstBuffer ** buf);
360 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
361 guint length, GstBuffer ** buf);
362 static gboolean gst_base_src_seekable (GstBaseSrc * src);
363 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
364 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
365 guint * length, gboolean force);
368 gst_base_src_class_init (GstBaseSrcClass * klass)
370 GObjectClass *gobject_class;
371 GstElementClass *gstelement_class;
373 gobject_class = G_OBJECT_CLASS (klass);
374 gstelement_class = GST_ELEMENT_CLASS (klass);
376 if (private_offset != 0)
377 g_type_class_adjust_private_offset (klass, &private_offset);
379 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
381 parent_class = g_type_class_peek_parent (klass);
383 gobject_class->finalize = gst_base_src_finalize;
384 gobject_class->set_property = gst_base_src_set_property;
385 gobject_class->get_property = gst_base_src_get_property;
387 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
388 g_param_spec_uint ("blocksize", "Block size",
389 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
390 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
392 g_param_spec_int ("num-buffers", "num-buffers",
393 "Number of buffers to output before sending EOS (-1 = unlimited)",
394 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
395 G_PARAM_STATIC_STRINGS));
396 #ifndef GST_REMOVE_DEPRECATED
397 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
398 g_param_spec_boolean ("typefind", "Typefind",
399 "Run typefind before negotiating (deprecated, non-functional)", FALSE,
400 G_PARAM_READWRITE | G_PARAM_DEPRECATED | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
403 g_param_spec_boolean ("do-timestamp", "Do timestamp",
404 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 gstelement_class->change_state =
408 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
409 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
411 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
412 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
413 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
414 klass->prepare_seek_segment =
415 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
416 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
417 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
418 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
419 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
420 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
421 klass->decide_allocation =
422 GST_DEBUG_FUNCPTR (gst_base_src_decide_allocation_default);
424 /* Registering debug symbols for function pointers */
425 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
426 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
427 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
428 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
429 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
433 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
436 GstPadTemplate *pad_template;
438 basesrc->priv = gst_base_src_get_instance_private (basesrc);
440 basesrc->is_live = FALSE;
441 g_mutex_init (&basesrc->live_lock);
442 g_cond_init (&basesrc->live_cond);
443 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
444 basesrc->num_buffers_left = -1;
445 basesrc->priv->automatic_eos = TRUE;
447 basesrc->can_activate_push = TRUE;
450 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
451 g_return_if_fail (pad_template != NULL);
453 GST_DEBUG_OBJECT (basesrc, "creating src pad");
454 pad = gst_pad_new_from_template (pad_template, "src");
456 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
457 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
458 gst_pad_set_event_function (pad, gst_base_src_event);
459 gst_pad_set_query_function (pad, gst_base_src_query);
460 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
462 /* hold pointer to pad */
463 basesrc->srcpad = pad;
464 GST_DEBUG_OBJECT (basesrc, "adding src pad");
465 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
467 basesrc->blocksize = DEFAULT_BLOCKSIZE;
468 basesrc->clock_id = NULL;
469 /* we operate in BYTES by default */
470 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
471 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
472 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
474 g_cond_init (&basesrc->priv->async_cond);
475 basesrc->priv->start_result = GST_FLOW_FLUSHING;
476 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
477 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
478 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
480 GST_DEBUG_OBJECT (basesrc, "init done");
484 gst_base_src_finalize (GObject * object)
489 basesrc = GST_BASE_SRC (object);
491 g_mutex_clear (&basesrc->live_lock);
492 g_cond_clear (&basesrc->live_cond);
493 g_cond_clear (&basesrc->priv->async_cond);
495 event_p = &basesrc->pending_seek;
496 gst_event_replace (event_p, NULL);
498 if (basesrc->priv->pending_events) {
499 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
501 g_list_free (basesrc->priv->pending_events);
504 G_OBJECT_CLASS (parent_class)->finalize (object);
507 /* Call with LIVE_LOCK held */
509 gst_base_src_wait_playing_unlocked (GstBaseSrc * src)
511 while (G_UNLIKELY (!src->live_running && !src->priv->flushing)) {
512 /* block until the state changes, or we get a flush, or something */
513 GST_DEBUG_OBJECT (src, "live source waiting for running state");
515 GST_DEBUG_OBJECT (src, "live source unlocked");
518 if (src->priv->flushing)
526 GST_DEBUG_OBJECT (src, "we are flushing");
527 return GST_FLOW_FLUSHING;
533 * gst_base_src_wait_playing:
536 * If the #GstBaseSrcClass.create() method performs its own synchronisation
537 * against the clock it must unblock when going from PLAYING to the PAUSED state
538 * and call this method before continuing to produce the remaining data.
540 * This function will block until a state change to PLAYING happens (in which
541 * case this function returns %GST_FLOW_OK) or the processing must be stopped due
542 * to a state change to READY or a FLUSH event (in which case this function
543 * returns %GST_FLOW_FLUSHING).
545 * Returns: %GST_FLOW_OK if @src is PLAYING and processing can
546 * continue. Any other return value should be returned from the create vmethod.
549 gst_base_src_wait_playing (GstBaseSrc * src)
553 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
556 ret = gst_base_src_wait_playing_unlocked (src);
557 GST_LIVE_UNLOCK (src);
563 * gst_base_src_set_live:
564 * @src: base source instance
565 * @live: new live-mode
567 * If the element listens to a live source, @live should
570 * A live source will not produce data in the PAUSED state and
571 * will therefore not be able to participate in the PREROLL phase
572 * of a pipeline. To signal this fact to the application and the
573 * pipeline, the state change return value of the live source will
574 * be GST_STATE_CHANGE_NO_PREROLL.
577 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
579 g_return_if_fail (GST_IS_BASE_SRC (src));
581 GST_OBJECT_LOCK (src);
583 GST_OBJECT_UNLOCK (src);
587 * gst_base_src_is_live:
588 * @src: base source instance
590 * Check if an element is in live mode.
592 * Returns: %TRUE if element is in live mode.
595 gst_base_src_is_live (GstBaseSrc * src)
599 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
601 GST_OBJECT_LOCK (src);
602 result = src->is_live;
603 GST_OBJECT_UNLOCK (src);
609 * gst_base_src_set_format:
610 * @src: base source instance
611 * @format: the format to use
613 * Sets the default format of the source. This will be the format used
614 * for sending SEGMENT events and for performing seeks.
616 * If a format of GST_FORMAT_BYTES is set, the element will be able to
617 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns %TRUE.
619 * This function must only be called in states < %GST_STATE_PAUSED.
622 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
624 g_return_if_fail (GST_IS_BASE_SRC (src));
625 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
627 GST_OBJECT_LOCK (src);
628 gst_segment_init (&src->segment, format);
629 GST_OBJECT_UNLOCK (src);
633 * gst_base_src_set_dynamic_size:
634 * @src: base source instance
635 * @dynamic: new dynamic size mode
637 * If not @dynamic, size is only updated when needed, such as when trying to
638 * read past current tracked size. Otherwise, size is checked for upon each
642 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
644 g_return_if_fail (GST_IS_BASE_SRC (src));
646 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
650 * gst_base_src_set_automatic_eos:
651 * @src: base source instance
652 * @automatic_eos: automatic eos
654 * If @automatic_eos is %TRUE, @src will automatically go EOS if a buffer
655 * after the total size is returned. By default this is %TRUE but sources
656 * that can't return an authoritative size and only know that they're EOS
657 * when trying to read more should set this to %FALSE.
659 * When @src operates in %GST_FORMAT_TIME, #GstBaseSrc will send an EOS
660 * when a buffer outside of the currently configured segment is pushed if
661 * @automatic_eos is %TRUE. Since 1.16, if @automatic_eos is %FALSE an
662 * EOS will be pushed only when the #GstBaseSrc.create implementation
663 * returns %GST_FLOW_EOS.
668 gst_base_src_set_automatic_eos (GstBaseSrc * src, gboolean automatic_eos)
670 g_return_if_fail (GST_IS_BASE_SRC (src));
672 g_atomic_int_set (&src->priv->automatic_eos, automatic_eos);
676 * gst_base_src_set_async:
677 * @src: base source instance
678 * @async: new async mode
680 * Configure async behaviour in @src, no state change will block. The open,
681 * close, start, stop, play and pause virtual methods will be executed in a
682 * different thread and are thus allowed to perform blocking operations. Any
683 * blocking operation should be unblocked with the unlock vmethod.
686 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
688 g_return_if_fail (GST_IS_BASE_SRC (src));
690 GST_OBJECT_LOCK (src);
691 src->priv->async = async;
692 GST_OBJECT_UNLOCK (src);
696 * gst_base_src_is_async:
697 * @src: base source instance
699 * Get the current async behaviour of @src. See also gst_base_src_set_async().
701 * Returns: %TRUE if @src is operating in async mode.
704 gst_base_src_is_async (GstBaseSrc * src)
708 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
710 GST_OBJECT_LOCK (src);
711 res = src->priv->async;
712 GST_OBJECT_UNLOCK (src);
719 * gst_base_src_query_latency:
721 * @live: (out) (allow-none): if the source is live
722 * @min_latency: (out) (allow-none): the min latency of the source
723 * @max_latency: (out) (allow-none): the max latency of the source
725 * Query the source for the latency parameters. @live will be %TRUE when @src is
726 * configured as a live source. @min_latency and @max_latency will be set
727 * to the difference between the running time and the timestamp of the first
730 * This function is mostly used by subclasses.
732 * Returns: %TRUE if the query succeeded.
735 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
736 GstClockTime * min_latency, GstClockTime * max_latency)
740 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
742 GST_OBJECT_LOCK (src);
744 *live = src->is_live;
746 /* if we have a startup latency, report this one, else report 0. Subclasses
747 * are supposed to override the query function if they want something
749 if (src->priv->latency != -1)
750 min = src->priv->latency;
759 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
760 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
761 GST_TIME_ARGS (min));
762 GST_OBJECT_UNLOCK (src);
768 * gst_base_src_set_blocksize:
770 * @blocksize: the new blocksize in bytes
772 * Set the number of bytes that @src will push out with each buffer. When
773 * @blocksize is set to -1, a default length will be used.
776 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
778 g_return_if_fail (GST_IS_BASE_SRC (src));
780 GST_OBJECT_LOCK (src);
781 src->blocksize = blocksize;
782 GST_OBJECT_UNLOCK (src);
786 * gst_base_src_get_blocksize:
789 * Get the number of bytes that @src will push out with each buffer.
791 * Returns: the number of bytes pushed with each buffer.
794 gst_base_src_get_blocksize (GstBaseSrc * src)
798 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
800 GST_OBJECT_LOCK (src);
801 res = src->blocksize;
802 GST_OBJECT_UNLOCK (src);
809 * gst_base_src_set_do_timestamp:
811 * @timestamp: enable or disable timestamping
813 * Configure @src to automatically timestamp outgoing buffers based on the
814 * current running_time of the pipeline. This property is mostly useful for live
818 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
820 g_return_if_fail (GST_IS_BASE_SRC (src));
822 GST_OBJECT_LOCK (src);
823 src->priv->do_timestamp = timestamp;
824 if (timestamp && src->segment.format != GST_FORMAT_TIME)
825 gst_segment_init (&src->segment, GST_FORMAT_TIME);
826 GST_OBJECT_UNLOCK (src);
830 * gst_base_src_get_do_timestamp:
833 * Query if @src timestamps outgoing buffers based on the current running_time.
835 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
838 gst_base_src_get_do_timestamp (GstBaseSrc * src)
842 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
844 GST_OBJECT_LOCK (src);
845 res = src->priv->do_timestamp;
846 GST_OBJECT_UNLOCK (src);
852 * gst_base_src_new_seamless_segment:
854 * @start: The new start value for the segment
855 * @stop: Stop value for the new segment
856 * @time: The new time value for the start of the new segment
858 * Prepare a new seamless segment for emission downstream. This function must
859 * only be called by derived sub-classes, and only from the create() function,
860 * as the stream-lock needs to be held.
862 * The format for the new segment will be the current format of the source, as
863 * configured with gst_base_src_set_format()
865 * Returns: %TRUE if preparation of the seamless segment succeeded.
868 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
873 GST_OBJECT_LOCK (src);
875 src->segment.base = gst_segment_to_running_time (&src->segment,
876 src->segment.format, src->segment.position);
877 src->segment.position = src->segment.start = start;
878 src->segment.stop = stop;
879 src->segment.time = time;
881 /* Mark pending segment. Will be sent before next data */
882 src->priv->segment_pending = TRUE;
883 src->priv->segment_seqnum = gst_util_seqnum_next ();
885 GST_DEBUG_OBJECT (src,
886 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
887 GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
888 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
889 GST_TIME_ARGS (src->segment.base));
891 GST_OBJECT_UNLOCK (src);
893 src->priv->discont = TRUE;
899 /* called with STREAM_LOCK */
901 gst_base_src_send_stream_start (GstBaseSrc * src)
905 if (src->priv->stream_start_pending) {
910 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
912 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
913 event = gst_event_new_stream_start (stream_id);
914 gst_event_set_group_id (event, gst_util_group_id_next ());
916 ret = gst_pad_push_event (src->srcpad, event);
917 src->priv->stream_start_pending = FALSE;
925 * gst_base_src_set_caps:
926 * @src: a #GstBaseSrc
927 * @caps: (transfer none): a #GstCaps
929 * Set new caps on the basesrc source pad.
931 * Returns: %TRUE if the caps could be set
934 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
936 GstBaseSrcClass *bclass;
938 GstCaps *current_caps;
940 bclass = GST_BASE_SRC_GET_CLASS (src);
942 gst_base_src_send_stream_start (src);
944 current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (src));
945 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
946 GST_DEBUG_OBJECT (src, "New caps equal to old ones: %" GST_PTR_FORMAT,
950 if (bclass->set_caps)
951 res = bclass->set_caps (src, caps);
954 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
958 gst_caps_unref (current_caps);
964 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
966 GstCaps *caps = NULL;
967 GstPadTemplate *pad_template;
968 GstBaseSrcClass *bclass;
970 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
973 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
975 if (pad_template != NULL) {
976 caps = gst_pad_template_get_caps (pad_template);
979 GstCaps *intersection;
982 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
983 gst_caps_unref (caps);
991 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
993 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
994 return gst_caps_fixate (caps);
998 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
1000 GstBaseSrcClass *bclass;
1002 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
1005 caps = bclass->fixate (bsrc, caps);
1011 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
1015 switch (GST_QUERY_TYPE (query)) {
1016 case GST_QUERY_POSITION:
1020 gst_query_parse_position (query, &format, NULL);
1022 GST_DEBUG_OBJECT (src, "position query in format %s",
1023 gst_format_get_name (format));
1026 case GST_FORMAT_PERCENT:
1032 GST_OBJECT_LOCK (src);
1033 position = src->segment.position;
1034 duration = src->segment.duration;
1035 GST_OBJECT_UNLOCK (src);
1037 if (position != -1 && duration != -1) {
1038 if (position < duration)
1039 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1042 percent = GST_FORMAT_PERCENT_MAX;
1046 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1053 GstFormat seg_format;
1055 GST_OBJECT_LOCK (src);
1057 gst_segment_to_stream_time (&src->segment, src->segment.format,
1058 src->segment.position);
1059 seg_format = src->segment.format;
1060 GST_OBJECT_UNLOCK (src);
1062 if (position != -1) {
1063 /* convert to requested format */
1065 gst_pad_query_convert (src->srcpad, seg_format,
1066 position, format, &position);
1071 gst_query_set_position (query, format, position);
1078 case GST_QUERY_DURATION:
1082 gst_query_parse_duration (query, &format, NULL);
1084 GST_DEBUG_OBJECT (src, "duration query in format %s",
1085 gst_format_get_name (format));
1088 case GST_FORMAT_PERCENT:
1089 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1090 GST_FORMAT_PERCENT_MAX);
1096 GstFormat seg_format;
1099 /* may have to refresh duration */
1100 gst_base_src_update_length (src, 0, &length,
1101 g_atomic_int_get (&src->priv->dynamic_size));
1103 /* this is the duration as configured by the subclass. */
1104 GST_OBJECT_LOCK (src);
1105 duration = src->segment.duration;
1106 seg_format = src->segment.format;
1107 GST_OBJECT_UNLOCK (src);
1109 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1110 duration, gst_format_get_name (seg_format));
1112 if (duration != -1) {
1113 /* convert to requested format, if this fails, we have a duration
1114 * but we cannot answer the query, we must return FALSE. */
1116 gst_pad_query_convert (src->srcpad, seg_format,
1117 duration, format, &duration);
1119 /* The subclass did not configure a duration, we assume that the
1120 * media has an unknown duration then and we return TRUE to report
1121 * this. Note that this is not the same as returning FALSE, which
1122 * means that we cannot report the duration at all. */
1127 gst_query_set_duration (query, format, duration);
1135 case GST_QUERY_SEEKING:
1137 GstFormat format, seg_format;
1140 GST_OBJECT_LOCK (src);
1141 duration = src->segment.duration;
1142 seg_format = src->segment.format;
1143 GST_OBJECT_UNLOCK (src);
1145 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1146 if (format == seg_format) {
1147 gst_query_set_seeking (query, seg_format,
1148 gst_base_src_seekable (src), 0, duration);
1151 /* FIXME 2.0: return TRUE + seekable=FALSE for SEEKING query here */
1152 /* Don't reply to the query to make up for demuxers which don't
1153 * handle the SEEKING query yet. Players like Totem will fall back
1154 * to the duration when the SEEKING query isn't answered. */
1159 case GST_QUERY_SEGMENT:
1164 GST_OBJECT_LOCK (src);
1166 format = src->segment.format;
1169 gst_segment_to_stream_time (&src->segment, format,
1170 src->segment.start);
1171 if ((stop = src->segment.stop) == -1)
1172 stop = src->segment.duration;
1174 stop = gst_segment_to_stream_time (&src->segment, format, stop);
1176 gst_query_set_segment (query, src->segment.rate, format, start, stop);
1178 GST_OBJECT_UNLOCK (src);
1183 case GST_QUERY_FORMATS:
1185 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1186 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1190 case GST_QUERY_CONVERT:
1192 GstFormat src_fmt, dest_fmt;
1193 gint64 src_val, dest_val;
1195 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1197 /* we can only convert between equal formats... */
1198 if (src_fmt == dest_fmt) {
1204 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1207 case GST_QUERY_LATENCY:
1209 GstClockTime min, max;
1212 /* Subclasses should override and implement something useful */
1213 res = gst_base_src_query_latency (src, &live, &min, &max);
1215 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1216 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1217 GST_TIME_ARGS (max));
1219 gst_query_set_latency (query, live, min, max);
1222 case GST_QUERY_JITTER:
1223 case GST_QUERY_RATE:
1226 case GST_QUERY_BUFFERING:
1228 GstFormat format, seg_format;
1229 gint64 start, stop, estimated;
1231 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1233 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1234 gst_format_get_name (format));
1236 GST_OBJECT_LOCK (src);
1237 if (src->random_access) {
1240 if (format == GST_FORMAT_PERCENT)
1241 stop = GST_FORMAT_PERCENT_MAX;
1243 stop = src->segment.duration;
1249 seg_format = src->segment.format;
1250 GST_OBJECT_UNLOCK (src);
1252 /* convert to required format. When the conversion fails, we can't answer
1253 * the query. When the value is unknown, we can don't perform conversion
1254 * but report TRUE. */
1255 if (format != GST_FORMAT_PERCENT && stop != -1) {
1256 res = gst_pad_query_convert (src->srcpad, seg_format,
1257 stop, format, &stop);
1261 if (res && format != GST_FORMAT_PERCENT && start != -1)
1262 res = gst_pad_query_convert (src->srcpad, seg_format,
1263 start, format, &start);
1265 gst_query_set_buffering_range (query, format, start, stop, estimated);
1268 case GST_QUERY_SCHEDULING:
1270 gboolean random_access;
1272 random_access = gst_base_src_is_random_access (src);
1274 /* we can operate in getrange mode if the native format is bytes
1275 * and we are seekable, this condition is set in the random_access
1276 * flag and is set in the _start() method. */
1277 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1279 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1280 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1285 case GST_QUERY_CAPS:
1287 GstBaseSrcClass *bclass;
1288 GstCaps *caps, *filter;
1290 bclass = GST_BASE_SRC_GET_CLASS (src);
1291 if (bclass->get_caps) {
1292 gst_query_parse_caps (query, &filter);
1293 if ((caps = bclass->get_caps (src, filter))) {
1294 gst_query_set_caps_result (query, caps);
1295 gst_caps_unref (caps);
1304 case GST_QUERY_URI:{
1305 if (GST_IS_URI_HANDLER (src)) {
1306 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1309 gst_query_set_uri (query, uri);
1324 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1331 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1334 GstBaseSrcClass *bclass;
1335 gboolean result = FALSE;
1337 src = GST_BASE_SRC (parent);
1338 bclass = GST_BASE_SRC_GET_CLASS (src);
1341 result = bclass->query (src, query);
1347 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1349 gboolean res = TRUE;
1351 /* update our offset if the start/stop position was updated */
1352 if (segment->format == GST_FORMAT_BYTES) {
1353 segment->time = segment->start;
1354 } else if (segment->start == 0) {
1355 /* seek to start, we can implement a default for this. */
1359 GST_INFO_OBJECT (src, "Can't do a default seek");
1366 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1368 GstBaseSrcClass *bclass;
1369 gboolean result = FALSE;
1371 bclass = GST_BASE_SRC_GET_CLASS (src);
1373 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1375 if (bclass->do_seek)
1376 result = bclass->do_seek (src, segment);
1381 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1384 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1385 GstSegment * segment)
1387 /* By default, we try one of 2 things:
1388 * - For absolute seek positions, convert the requested position to our
1389 * configured processing format and place it in the output segment \
1390 * - For relative seek positions, convert our current (input) values to the
1391 * seek format, adjust by the relative seek offset and then convert back to
1392 * the processing format
1394 GstSeekType start_type, stop_type;
1397 GstFormat seek_format, dest_format;
1400 gboolean res = TRUE;
1402 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1403 &start_type, &start, &stop_type, &stop);
1404 dest_format = segment->format;
1406 if (seek_format == dest_format) {
1407 gst_segment_do_seek (segment, rate, seek_format, flags,
1408 start_type, start, stop_type, stop, &update);
1412 if (start_type != GST_SEEK_TYPE_NONE) {
1413 /* FIXME: Handle seek_end by converting the input segment vals */
1415 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1417 start_type = GST_SEEK_TYPE_SET;
1420 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1421 /* FIXME: Handle seek_end by converting the input segment vals */
1423 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1425 stop_type = GST_SEEK_TYPE_SET;
1428 /* And finally, configure our output segment in the desired format */
1429 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1430 stop_type, stop, &update);
1439 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1445 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1446 GstSegment * seeksegment)
1448 GstBaseSrcClass *bclass;
1449 gboolean result = FALSE;
1451 bclass = GST_BASE_SRC_GET_CLASS (src);
1453 if (bclass->prepare_seek_segment)
1454 result = bclass->prepare_seek_segment (src, event, seeksegment);
1459 static GstFlowReturn
1460 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1461 guint size, GstBuffer ** buffer)
1464 GstBaseSrcPrivate *priv = src->priv;
1465 GstBufferPool *pool = NULL;
1466 GstAllocator *allocator = NULL;
1467 GstAllocationParams params;
1469 GST_OBJECT_LOCK (src);
1471 pool = gst_object_ref (priv->pool);
1472 } else if (priv->allocator) {
1473 allocator = gst_object_ref (priv->allocator);
1475 params = priv->params;
1476 GST_OBJECT_UNLOCK (src);
1479 ret = gst_buffer_pool_acquire_buffer (pool, buffer, NULL);
1480 } else if (size != -1) {
1481 *buffer = gst_buffer_new_allocate (allocator, size, ¶ms);
1482 if (G_UNLIKELY (*buffer == NULL))
1487 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1494 gst_object_unref (pool);
1496 gst_object_unref (allocator);
1503 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1504 ret = GST_FLOW_ERROR;
1509 static GstFlowReturn
1510 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1511 guint size, GstBuffer ** buffer)
1513 GstBaseSrcClass *bclass;
1517 bclass = GST_BASE_SRC_GET_CLASS (src);
1519 if (G_UNLIKELY (!bclass->alloc))
1521 if (G_UNLIKELY (!bclass->fill))
1524 if (*buffer == NULL) {
1525 /* downstream did not provide us with a buffer to fill, allocate one
1527 ret = bclass->alloc (src, offset, size, &res_buf);
1528 if (G_UNLIKELY (ret != GST_FLOW_OK))
1534 if (G_LIKELY (size > 0)) {
1535 /* only call fill when there is a size */
1536 ret = bclass->fill (src, offset, size, res_buf);
1537 if (G_UNLIKELY (ret != GST_FLOW_OK))
1548 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1549 return GST_FLOW_NOT_SUPPORTED;
1553 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1558 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1559 gst_flow_get_name (ret));
1560 if (*buffer == NULL)
1561 gst_buffer_unref (res_buf);
1566 /* this code implements the seeking. It is a good example
1567 * handling all cases.
1569 * A seek updates the currently configured segment.start
1570 * and segment.stop values based on the SEEK_TYPE. If the
1571 * segment.start value is updated, a seek to this new position
1572 * should be performed.
1574 * The seek can only be executed when we are not currently
1575 * streaming any data, to make sure that this is the case, we
1576 * acquire the STREAM_LOCK which is taken when we are in the
1577 * _loop() function or when a getrange() is called. Normally
1578 * we will not receive a seek if we are operating in pull mode
1579 * though. When we operate as a live source we might block on the live
1580 * cond, which does not release the STREAM_LOCK. Therefore we will try
1581 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1582 * safe to perform the seek.
1584 * When we are in the loop() function, we might be in the middle
1585 * of pushing a buffer, which might block in a sink. To make sure
1586 * that the push gets unblocked we push out a FLUSH_START event.
1587 * Our loop function will get a FLUSHING return value from
1588 * the push and will pause, effectively releasing the STREAM_LOCK.
1590 * For a non-flushing seek, we pause the task, which might eventually
1591 * release the STREAM_LOCK. We say eventually because when the sink
1592 * blocks on the sample we might wait a very long time until the sink
1593 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1594 * can continue the seek. A non-flushing seek is normally done in a
1595 * running pipeline to perform seamless playback, this means that the sink is
1596 * PLAYING and will return from its chain function.
1597 * In the case of a non-flushing seek we need to make sure that the
1598 * data we output after the seek is continuous with the previous data,
1599 * this is because a non-flushing seek does not reset the running-time
1600 * to 0. We do this by closing the currently running segment, ie. sending
1601 * a new_segment event with the stop position set to the last processed
1604 * After updating the segment.start/stop values, we prepare for
1605 * streaming again. We push out a FLUSH_STOP to make the peer pad
1606 * accept data again and we start our task again.
1608 * A segment seek posts a message on the bus saying that the playback
1609 * of the segment started. We store the segment flag internally because
1610 * when we reach the segment.stop we have to post a segment.done
1611 * instead of EOS when doing a segment seek.
1614 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1616 gboolean res = TRUE, tres;
1618 GstFormat seek_format, dest_format;
1620 GstSeekType start_type, stop_type;
1624 gboolean relative_seek = FALSE;
1625 gboolean seekseg_configured = FALSE;
1626 GstSegment seeksegment;
1630 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1632 GST_OBJECT_LOCK (src);
1633 dest_format = src->segment.format;
1634 GST_OBJECT_UNLOCK (src);
1637 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1638 &start_type, &start, &stop_type, &stop);
1640 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1641 SEEK_TYPE_IS_RELATIVE (stop_type);
1643 if (dest_format != seek_format && !relative_seek) {
1644 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1645 * here before taking the stream lock, otherwise we must convert it later,
1646 * once we have the stream lock and can read the last configures segment
1647 * start and stop positions */
1648 gst_segment_init (&seeksegment, dest_format);
1650 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1651 goto prepare_failed;
1653 seekseg_configured = TRUE;
1656 flush = flags & GST_SEEK_FLAG_FLUSH;
1657 seqnum = gst_event_get_seqnum (event);
1660 /* get next seqnum */
1661 seqnum = gst_util_seqnum_next ();
1664 /* send flush start */
1666 tevent = gst_event_new_flush_start ();
1667 gst_event_set_seqnum (tevent, seqnum);
1668 gst_pad_push_event (src->srcpad, tevent);
1670 gst_pad_pause_task (src->srcpad);
1672 /* unblock streaming thread. */
1674 gst_base_src_set_flushing (src, TRUE);
1676 /* grab streaming lock, this should eventually be possible, either
1677 * because the task is paused, our streaming thread stopped
1678 * or because our peer is flushing. */
1679 GST_PAD_STREAM_LOCK (src->srcpad);
1680 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1681 /* we have seen this event before, issue a warning for now */
1682 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1685 src->priv->seqnum = seqnum;
1686 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1690 gst_base_src_set_flushing (src, FALSE);
1692 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1693 * copy the current segment info into the temp segment that we can actually
1694 * attempt the seek with. We only update the real segment if the seek succeeds. */
1695 if (!seekseg_configured) {
1696 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1698 /* now configure the final seek segment */
1700 if (seeksegment.format != seek_format) {
1701 /* OK, here's where we give the subclass a chance to convert the relative
1702 * seek into an absolute one in the processing format. We set up any
1703 * absolute seek above, before taking the stream lock. */
1704 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1705 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1710 /* The seek format matches our processing format, no need to ask the
1711 * the subclass to configure the segment. */
1712 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1713 start_type, start, stop_type, stop, &update);
1716 /* Else, no seek event passed, so we're just (re)starting the
1721 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1722 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1723 seeksegment.start, seeksegment.stop, seeksegment.position);
1725 /* do the seek, segment.position contains the new position. */
1726 res = gst_base_src_do_seek (src, &seeksegment);
1729 /* and prepare to continue streaming */
1731 tevent = gst_event_new_flush_stop (TRUE);
1732 gst_event_set_seqnum (tevent, seqnum);
1733 /* send flush stop, peer will accept data and events again. We
1734 * are not yet providing data as we still have the STREAM_LOCK. */
1735 gst_pad_push_event (src->srcpad, tevent);
1738 /* The subclass must have converted the segment to the processing format
1740 if (res && seeksegment.format != dest_format) {
1741 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1742 "in the correct format. Aborting seek.");
1746 /* if the seek was successful, we update our real segment and push
1747 * out the new segment. */
1749 GST_OBJECT_LOCK (src);
1750 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1751 GST_OBJECT_UNLOCK (src);
1753 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1754 GstMessage *message;
1756 message = gst_message_new_segment_start (GST_OBJECT (src),
1757 seeksegment.format, seeksegment.position);
1758 gst_message_set_seqnum (message, seqnum);
1760 gst_element_post_message (GST_ELEMENT (src), message);
1763 src->priv->segment_pending = TRUE;
1764 src->priv->segment_seqnum = seqnum;
1767 src->priv->discont = TRUE;
1768 src->running = TRUE;
1769 /* and restart the task in case it got paused explicitly or by
1770 * the FLUSH_START event we pushed out. */
1771 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1776 /* and release the lock again so we can continue streaming */
1777 GST_PAD_STREAM_UNLOCK (src->srcpad);
1783 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1788 /* all events send to this element directly. This is mainly done from the
1792 gst_base_src_send_event (GstElement * element, GstEvent * event)
1795 gboolean result = FALSE;
1796 GstBaseSrcClass *bclass;
1798 src = GST_BASE_SRC (element);
1799 bclass = GST_BASE_SRC_GET_CLASS (src);
1801 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1803 switch (GST_EVENT_TYPE (event)) {
1804 /* bidirectional events */
1805 case GST_EVENT_FLUSH_START:
1806 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1808 result = gst_pad_push_event (src->srcpad, event);
1809 gst_base_src_set_flushing (src, TRUE);
1812 case GST_EVENT_FLUSH_STOP:
1816 GST_PAD_STREAM_LOCK (src->srcpad);
1817 gst_base_src_set_flushing (src, FALSE);
1819 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1820 result = gst_pad_push_event (src->srcpad, event);
1822 /* For external flush, restart the task .. */
1823 GST_LIVE_LOCK (src);
1824 src->priv->segment_pending = TRUE;
1826 GST_OBJECT_LOCK (src->srcpad);
1827 start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1828 GST_OBJECT_UNLOCK (src->srcpad);
1830 /* ... and for live sources, only if in playing state */
1832 if (!src->live_running)
1837 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1840 GST_LIVE_UNLOCK (src);
1841 GST_PAD_STREAM_UNLOCK (src->srcpad);
1847 /* downstream serialized events */
1852 /* queue EOS and make sure the task or pull function performs the EOS
1855 * For push mode, This will be done in 3 steps. It is required to not
1856 * block here as gst_element_send_event() will hold the STATE_LOCK, hence
1857 * blocking would prevent asynchronous state change to complete.
1859 * 1. We stop the streaming thread
1860 * 2. We set the pending eos
1861 * 3. We start the streaming thread again, so it is performed
1864 * For pull mode, we simply mark the pending EOS without flushing.
1867 GST_OBJECT_LOCK (src->srcpad);
1868 push_mode = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1869 GST_OBJECT_UNLOCK (src->srcpad);
1872 gst_base_src_set_flushing (src, TRUE);
1874 GST_PAD_STREAM_LOCK (src->srcpad);
1875 gst_base_src_set_flushing (src, FALSE);
1877 GST_OBJECT_LOCK (src);
1878 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1879 if (src->priv->pending_eos)
1880 gst_event_unref (src->priv->pending_eos);
1881 src->priv->pending_eos = event;
1882 GST_OBJECT_UNLOCK (src);
1884 GST_DEBUG_OBJECT (src,
1885 "EOS marked, start task for asynchronous handling");
1886 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1889 GST_PAD_STREAM_UNLOCK (src->srcpad);
1891 /* In pull mode, we need not to return flushing to downstream, though
1892 * the stream lock is not kept after getrange was unblocked */
1893 GST_OBJECT_LOCK (src);
1894 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1895 if (src->priv->pending_eos)
1896 gst_event_unref (src->priv->pending_eos);
1897 src->priv->pending_eos = event;
1898 GST_OBJECT_UNLOCK (src);
1900 gst_base_src_set_pool_flushing (src, TRUE);
1902 bclass->unlock (src);
1904 GST_PAD_STREAM_LOCK (src->srcpad);
1905 if (bclass->unlock_stop)
1906 bclass->unlock_stop (src);
1907 gst_base_src_set_pool_flushing (src, TRUE);
1908 GST_PAD_STREAM_UNLOCK (src->srcpad);
1916 case GST_EVENT_SEGMENT:
1917 /* sending random SEGMENT downstream can break sync. */
1920 case GST_EVENT_SINK_MESSAGE:
1921 case GST_EVENT_CUSTOM_DOWNSTREAM:
1922 case GST_EVENT_CUSTOM_BOTH:
1923 case GST_EVENT_PROTECTION:
1924 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH, PROTECTION in the dataflow */
1925 GST_OBJECT_LOCK (src);
1926 src->priv->pending_events =
1927 g_list_append (src->priv->pending_events, event);
1928 g_atomic_int_set (&src->priv->have_events, TRUE);
1929 GST_OBJECT_UNLOCK (src);
1933 case GST_EVENT_BUFFERSIZE:
1934 /* does not seem to make much sense currently */
1937 /* upstream events */
1939 /* elements should override send_event and do something */
1941 case GST_EVENT_SEEK:
1945 GST_OBJECT_LOCK (src->srcpad);
1946 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1948 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1949 GST_OBJECT_UNLOCK (src->srcpad);
1952 GST_DEBUG_OBJECT (src, "performing seek");
1953 /* when we are running in push mode, we can execute the
1954 * seek right now. */
1955 result = gst_base_src_perform_seek (src, event, TRUE);
1959 /* else we store the event and execute the seek when we
1961 GST_OBJECT_LOCK (src);
1962 GST_DEBUG_OBJECT (src, "queueing seek");
1963 event_p = &src->pending_seek;
1964 gst_event_replace ((GstEvent **) event_p, event);
1965 GST_OBJECT_UNLOCK (src);
1966 /* assume the seek will work */
1971 case GST_EVENT_NAVIGATION:
1972 /* could make sense for elements that do something with navigation events
1973 * but then they would need to override the send_event function */
1975 case GST_EVENT_LATENCY:
1976 /* does not seem to make sense currently */
1980 case GST_EVENT_CUSTOM_UPSTREAM:
1981 /* override send_event if you want this */
1983 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1984 case GST_EVENT_CUSTOM_BOTH_OOB:
1985 /* insert a random custom event into the pipeline */
1986 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1987 result = gst_pad_push_event (src->srcpad, event);
1988 /* we gave away the ref to the event in the push */
1995 /* if we still have a ref to the event, unref it now */
1997 gst_event_unref (event);
2004 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
2005 GST_OBJECT_UNLOCK (src->srcpad);
2012 gst_base_src_seekable (GstBaseSrc * src)
2014 GstBaseSrcClass *bclass;
2015 bclass = GST_BASE_SRC_GET_CLASS (src);
2016 if (bclass->is_seekable)
2017 return bclass->is_seekable (src);
2023 gst_base_src_update_qos (GstBaseSrc * src,
2024 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2026 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
2027 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2028 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2030 GST_OBJECT_LOCK (src);
2031 src->priv->proportion = proportion;
2032 src->priv->earliest_time = timestamp + diff;
2033 GST_OBJECT_UNLOCK (src);
2038 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
2042 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
2044 switch (GST_EVENT_TYPE (event)) {
2045 case GST_EVENT_SEEK:
2046 /* is normally called when in push mode */
2047 if (!gst_base_src_seekable (src))
2050 result = gst_base_src_perform_seek (src, event, TRUE);
2052 case GST_EVENT_FLUSH_START:
2053 /* cancel any blocking getrange, is normally called
2054 * when in pull mode. */
2055 result = gst_base_src_set_flushing (src, TRUE);
2057 case GST_EVENT_FLUSH_STOP:
2058 result = gst_base_src_set_flushing (src, FALSE);
2063 GstClockTimeDiff diff;
2064 GstClockTime timestamp;
2066 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2067 gst_base_src_update_qos (src, proportion, diff, timestamp);
2071 case GST_EVENT_RECONFIGURE:
2074 case GST_EVENT_LATENCY:
2086 GST_DEBUG_OBJECT (src, "is not seekable");
2092 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2095 GstBaseSrcClass *bclass;
2096 gboolean result = FALSE;
2098 src = GST_BASE_SRC (parent);
2099 bclass = GST_BASE_SRC_GET_CLASS (src);
2101 if (bclass->event) {
2102 if (!(result = bclass->event (src, event)))
2103 goto subclass_failed;
2107 gst_event_unref (event);
2114 GST_DEBUG_OBJECT (src, "subclass refused event");
2120 gst_base_src_set_property (GObject * object, guint prop_id,
2121 const GValue * value, GParamSpec * pspec)
2125 src = GST_BASE_SRC (object);
2128 case PROP_BLOCKSIZE:
2129 gst_base_src_set_blocksize (src, g_value_get_uint (value));
2131 case PROP_NUM_BUFFERS:
2132 src->num_buffers = g_value_get_int (value);
2134 #ifndef GST_REMOVE_DEPRECATED
2136 src->typefind = g_value_get_boolean (value);
2139 case PROP_DO_TIMESTAMP:
2140 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2149 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2154 src = GST_BASE_SRC (object);
2157 case PROP_BLOCKSIZE:
2158 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2160 case PROP_NUM_BUFFERS:
2161 g_value_set_int (value, src->num_buffers);
2163 #ifndef GST_REMOVE_DEPRECATED
2165 g_value_set_boolean (value, src->typefind);
2168 case PROP_DO_TIMESTAMP:
2169 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2177 /* with STREAM_LOCK and LOCK */
2178 static GstClockReturn
2179 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2184 id = gst_clock_new_single_shot_id (clock, time);
2186 basesrc->clock_id = id;
2187 /* release the live lock while waiting */
2188 GST_LIVE_UNLOCK (basesrc);
2190 ret = gst_clock_id_wait (id, NULL);
2192 GST_LIVE_LOCK (basesrc);
2193 gst_clock_id_unref (id);
2194 basesrc->clock_id = NULL;
2199 /* perform synchronisation on a buffer.
2202 static GstClockReturn
2203 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2205 GstClockReturn result;
2206 GstClockTime start, end;
2207 GstBaseSrcClass *bclass;
2208 GstClockTime base_time;
2210 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2211 gboolean do_timestamp, first, pseudo_live, is_live;
2213 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2216 if (bclass->get_times)
2217 bclass->get_times (basesrc, buffer, &start, &end);
2219 /* get buffer timestamp */
2220 dts = GST_BUFFER_DTS (buffer);
2221 pts = GST_BUFFER_PTS (buffer);
2223 if (GST_CLOCK_TIME_IS_VALID (dts))
2228 /* grab the lock to prepare for clocking and calculate the startup
2230 GST_OBJECT_LOCK (basesrc);
2232 is_live = basesrc->is_live;
2233 /* if we are asked to sync against the clock we are a pseudo live element */
2234 pseudo_live = (start != -1 && is_live);
2235 /* check for the first buffer */
2236 first = (basesrc->priv->latency == -1);
2238 if (timestamp != -1 && pseudo_live) {
2239 GstClockTime latency;
2241 /* we have a timestamp and a sync time, latency is the diff */
2242 if (timestamp <= start)
2243 latency = start - timestamp;
2248 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2249 GST_TIME_ARGS (latency));
2250 /* first time we calculate latency, just configure */
2251 basesrc->priv->latency = latency;
2253 if (basesrc->priv->latency != latency) {
2254 /* we have a new latency, FIXME post latency message */
2255 basesrc->priv->latency = latency;
2256 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2257 GST_TIME_ARGS (latency));
2261 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2262 is_live, start != -1);
2263 basesrc->priv->latency = 0;
2266 /* get clock, if no clock, we can't sync or do timestamps */
2267 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2270 gst_object_ref (clock);
2272 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2274 do_timestamp = basesrc->priv->do_timestamp;
2275 GST_OBJECT_UNLOCK (basesrc);
2277 /* first buffer, calculate the timestamp offset */
2279 GstClockTime running_time;
2281 now = gst_clock_get_time (clock);
2282 running_time = now - base_time;
2284 GST_LOG_OBJECT (basesrc,
2285 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2286 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2287 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2289 if (pseudo_live && timestamp != -1) {
2290 /* live source and we need to sync, add startup latency to all timestamps
2291 * to get the real running_time. Live sources should always timestamp
2292 * according to the current running time. */
2293 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2295 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2296 GST_TIME_ARGS (basesrc->priv->ts_offset));
2298 basesrc->priv->ts_offset = 0;
2299 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2302 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2305 } else if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2306 if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2307 dts = basesrc->segment.start;
2312 GST_BUFFER_DTS (buffer) = dts;
2314 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2315 GST_TIME_ARGS (dts));
2318 /* not the first buffer, the timestamp is the diff between the clock and
2320 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2321 now = gst_clock_get_time (clock);
2323 dts = now - base_time;
2324 GST_BUFFER_DTS (buffer) = dts;
2326 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2327 GST_TIME_ARGS (dts));
2330 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2331 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2334 GST_BUFFER_PTS (buffer) = dts;
2336 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2337 GST_TIME_ARGS (pts));
2340 /* if we don't have a buffer timestamp, we don't sync */
2341 if (!GST_CLOCK_TIME_IS_VALID (start))
2345 /* for pseudo live sources, add our ts_offset to the timestamp */
2346 if (GST_CLOCK_TIME_IS_VALID (pts))
2347 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2348 if (GST_CLOCK_TIME_IS_VALID (dts))
2349 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2350 start += basesrc->priv->ts_offset;
2353 GST_LOG_OBJECT (basesrc,
2354 "waiting for clock, base time %" GST_TIME_FORMAT
2355 ", stream_start %" GST_TIME_FORMAT,
2356 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2358 result = gst_base_src_wait (basesrc, clock, start + base_time);
2360 gst_object_unref (clock);
2362 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2369 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2370 GST_OBJECT_UNLOCK (basesrc);
2371 return GST_CLOCK_OK;
2375 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2376 gst_object_unref (clock);
2377 return GST_CLOCK_OK;
2381 /* Called with STREAM_LOCK and LIVE_LOCK */
2383 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2386 guint64 size, maxsize;
2387 GstBaseSrcClass *bclass;
2390 /* only operate if we are working with bytes */
2391 if (src->segment.format != GST_FORMAT_BYTES)
2394 bclass = GST_BASE_SRC_GET_CLASS (src);
2396 stop = src->segment.stop;
2397 /* get total file size */
2398 size = src->segment.duration;
2400 /* when not doing automatic EOS, just use the stop position. We don't use
2401 * the size to check for EOS */
2402 if (!g_atomic_int_get (&src->priv->automatic_eos))
2404 /* Otherwise, the max amount of bytes to read is the total
2405 * size or up to the segment.stop if present. */
2406 else if (stop != -1)
2407 maxsize = size != -1 ? MIN (size, stop) : stop;
2411 GST_DEBUG_OBJECT (src,
2412 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2413 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2414 *length, size, stop, maxsize);
2416 /* check size if we have one */
2417 if (maxsize != -1) {
2418 /* if we run past the end, check if the file became bigger and
2419 * retry. Mind wrap when checking. */
2420 if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2421 /* see if length of the file changed */
2422 if (bclass->get_size)
2423 if (!bclass->get_size (src, &size))
2426 /* when not doing automatic EOS, just use the stop position. We don't use
2427 * the size to check for EOS */
2428 if (!g_atomic_int_get (&src->priv->automatic_eos))
2430 /* Otherwise, the max amount of bytes to read is the total
2431 * size or up to the segment.stop if present. */
2432 else if (stop != -1)
2433 maxsize = size != -1 ? MIN (size, stop) : stop;
2437 if (maxsize != -1) {
2438 /* if we are at or past the end, EOS */
2439 if (G_UNLIKELY (offset >= maxsize))
2440 goto unexpected_length;
2442 /* else we can clip to the end */
2443 if (G_UNLIKELY (offset + *length >= maxsize))
2444 *length = maxsize - offset;
2449 /* keep track of current duration. segment is in bytes, we checked
2451 GST_OBJECT_LOCK (src);
2452 src->segment.duration = size;
2453 GST_OBJECT_UNLOCK (src);
2460 GST_WARNING_OBJECT (src, "processing at or past EOS");
2465 /* must be called with LIVE_LOCK */
2466 static GstFlowReturn
2467 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2471 GstBaseSrcClass *bclass;
2472 GstClockReturn status;
2475 gboolean own_res_buf;
2477 bclass = GST_BASE_SRC_GET_CLASS (src);
2481 if (G_UNLIKELY (!src->live_running)) {
2482 ret = gst_base_src_wait_playing_unlocked (src);
2483 if (ret != GST_FLOW_OK)
2488 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2489 && !GST_BASE_SRC_IS_STARTING (src)))
2492 if (G_UNLIKELY (!bclass->create))
2495 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2496 goto unexpected_length;
2498 /* track position */
2499 GST_OBJECT_LOCK (src);
2500 if (src->segment.format == GST_FORMAT_BYTES)
2501 src->segment.position = offset;
2502 GST_OBJECT_UNLOCK (src);
2504 /* normally we don't count buffers */
2505 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2506 if (src->num_buffers_left == 0)
2507 goto reached_num_buffers;
2509 src->num_buffers_left--;
2512 /* don't enter the create function if a pending EOS event was set. For the
2513 * logic of the has_pending_eos, check the event function of this class. */
2514 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2515 src->priv->forced_eos = TRUE;
2519 GST_DEBUG_OBJECT (src,
2520 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2521 G_GINT64_FORMAT, offset, length, src->segment.time);
2523 res_buf = in_buf = *buf;
2524 own_res_buf = (*buf == NULL);
2526 GST_LIVE_UNLOCK (src);
2527 ret = bclass->create (src, offset, length, &res_buf);
2528 GST_LIVE_LOCK (src);
2530 /* As we released the LIVE_LOCK, the state may have changed */
2532 if (G_UNLIKELY (!src->live_running)) {
2533 GstFlowReturn wait_ret;
2534 wait_ret = gst_base_src_wait_playing_unlocked (src);
2535 if (wait_ret != GST_FLOW_OK) {
2536 if (ret == GST_FLOW_OK && own_res_buf)
2537 gst_buffer_unref (res_buf);
2544 /* The create function could be unlocked because we have a pending EOS. It's
2545 * possible that we have a valid buffer from create that we need to
2546 * discard when the create function returned _OK. */
2547 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2548 if (ret == GST_FLOW_OK) {
2550 gst_buffer_unref (res_buf);
2552 src->priv->forced_eos = TRUE;
2556 if (G_UNLIKELY (ret != GST_FLOW_OK))
2559 /* fallback in case the create function didn't fill a provided buffer */
2560 if (in_buf != NULL && res_buf != in_buf) {
2564 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2565 "fill the provided buffer, copying");
2567 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2570 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2571 gst_buffer_unmap (in_buf, &info);
2572 gst_buffer_set_size (in_buf, copied_size);
2574 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2576 gst_buffer_unref (res_buf);
2580 if (res_buf == NULL) {
2581 GstBufferList *pending_list = src->priv->pending_bufferlist;
2583 if (pending_list == NULL || gst_buffer_list_length (pending_list) == 0)
2586 res_buf = gst_buffer_list_get_writable (pending_list, 0);
2587 own_res_buf = FALSE;
2590 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2591 if (offset == 0 && src->segment.time == 0
2592 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2593 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2594 res_buf = gst_buffer_make_writable (res_buf);
2595 GST_BUFFER_DTS (res_buf) = 0;
2598 /* now sync before pushing the buffer */
2599 status = gst_base_src_do_sync (src, res_buf);
2601 /* waiting for the clock could have made us flushing */
2602 if (G_UNLIKELY (src->priv->flushing))
2606 case GST_CLOCK_EARLY:
2607 /* the buffer is too late. We currently don't drop the buffer. */
2608 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2611 /* buffer synchronised properly */
2612 GST_DEBUG_OBJECT (src, "buffer ok");
2614 case GST_CLOCK_UNSCHEDULED:
2615 /* this case is triggered when we were waiting for the clock and
2616 * it got unlocked because we did a state change. In any case, get rid of
2619 gst_buffer_unref (res_buf);
2621 if (!src->live_running) {
2622 /* We return FLUSHING when we are not running to stop the dataflow also
2623 * get rid of the produced buffer. */
2624 GST_DEBUG_OBJECT (src,
2625 "clock was unscheduled (%d), returning FLUSHING", status);
2626 ret = GST_FLOW_FLUSHING;
2628 /* If we are running when this happens, we quickly switched between
2629 * pause and playing. We try to produce a new buffer */
2630 GST_DEBUG_OBJECT (src,
2631 "clock was unscheduled (%d), but we are running", status);
2636 /* all other result values are unexpected and errors */
2637 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2638 (_("Internal clock error.")),
2639 ("clock returned unexpected return value %d", status));
2641 gst_buffer_unref (res_buf);
2642 ret = GST_FLOW_ERROR;
2645 if (G_LIKELY (ret == GST_FLOW_OK))
2653 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2654 gst_flow_get_name (ret));
2659 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2660 gst_flow_get_name (ret));
2665 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2666 (_("Failed to map buffer.")),
2667 ("failed to map result buffer in WRITE mode"));
2669 gst_buffer_unref (res_buf);
2670 return GST_FLOW_ERROR;
2674 GST_DEBUG_OBJECT (src, "getrange but not started");
2675 return GST_FLOW_FLUSHING;
2679 GST_DEBUG_OBJECT (src, "no create function");
2680 return GST_FLOW_NOT_SUPPORTED;
2684 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2685 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2686 return GST_FLOW_EOS;
2688 reached_num_buffers:
2690 GST_DEBUG_OBJECT (src, "sent all buffers");
2691 return GST_FLOW_EOS;
2695 GST_DEBUG_OBJECT (src, "we are flushing");
2697 gst_buffer_unref (res_buf);
2698 return GST_FLOW_FLUSHING;
2702 GST_DEBUG_OBJECT (src, "we are EOS");
2703 return GST_FLOW_EOS;
2707 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2708 (_("Internal data flow error.")),
2709 ("Subclass %s neither returned a buffer nor submitted a buffer list "
2710 "from its create function", G_OBJECT_TYPE_NAME (src)));
2711 return GST_FLOW_ERROR;
2715 static GstFlowReturn
2716 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2717 guint length, GstBuffer ** buf)
2722 src = GST_BASE_SRC_CAST (parent);
2724 GST_LIVE_LOCK (src);
2725 if (G_UNLIKELY (src->priv->flushing))
2728 res = gst_base_src_get_range (src, offset, length, buf);
2731 GST_LIVE_UNLOCK (src);
2738 GST_DEBUG_OBJECT (src, "we are flushing");
2739 res = GST_FLOW_FLUSHING;
2745 gst_base_src_is_random_access (GstBaseSrc * src)
2747 /* we need to start the basesrc to check random access */
2748 if (!GST_BASE_SRC_IS_STARTED (src)) {
2749 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2750 if (G_LIKELY (gst_base_src_start (src))) {
2751 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2753 gst_base_src_stop (src);
2757 return src->random_access;
2762 GST_DEBUG_OBJECT (src, "failed to start");
2767 /* Called with STREAM_LOCK */
2769 gst_base_src_loop (GstPad * pad)
2772 GstBuffer *buf = NULL;
2777 GList *pending_events = NULL, *tmp;
2781 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2783 /* Just leave immediately if we're flushing */
2784 GST_LIVE_LOCK (src);
2785 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2787 GST_LIVE_UNLOCK (src);
2789 /* Just return if EOS is pushed again, as the app might be unaware that an
2790 * EOS have been sent already */
2791 if (GST_PAD_IS_EOS (pad)) {
2792 GST_DEBUG_OBJECT (src, "Pad is marked as EOS, pause the task");
2793 gst_pad_pause_task (pad);
2797 gst_base_src_send_stream_start (src);
2799 /* The stream-start event could've caused something to flush us */
2800 GST_LIVE_LOCK (src);
2801 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2803 GST_LIVE_UNLOCK (src);
2805 /* check if we need to renegotiate */
2806 if (gst_pad_check_reconfigure (pad)) {
2807 if (!gst_base_src_negotiate (src)) {
2808 gst_pad_mark_reconfigure (pad);
2809 if (GST_PAD_IS_FLUSHING (pad)) {
2810 GST_LIVE_LOCK (src);
2813 goto negotiate_failed;
2818 GST_LIVE_LOCK (src);
2820 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2823 blocksize = src->blocksize;
2825 /* if we operate in bytes, we can calculate an offset */
2826 if (src->segment.format == GST_FORMAT_BYTES) {
2827 position = src->segment.position;
2828 /* for negative rates, start with subtracting the blocksize */
2829 if (src->segment.rate < 0.0) {
2830 /* we cannot go below segment.start */
2831 if (position > src->segment.start + blocksize)
2832 position -= blocksize;
2834 /* last block, remainder up to segment.start */
2835 blocksize = position - src->segment.start;
2836 position = src->segment.start;
2842 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2843 GST_TIME_ARGS (position), blocksize);
2845 /* clean up just in case we got interrupted or so last time round */
2846 if (src->priv->pending_bufferlist != NULL) {
2847 gst_buffer_list_unref (src->priv->pending_bufferlist);
2848 src->priv->pending_bufferlist = NULL;
2851 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2852 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2853 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2854 gst_flow_get_name (ret));
2855 GST_LIVE_UNLOCK (src);
2859 /* Note: at this point buf might be a single buf returned which we own or
2860 * the first buf of a pending buffer list submitted via submit_buffer_list(),
2861 * in which case the buffer is owned by the pending buffer list and not us. */
2862 g_assert (buf != NULL);
2864 /* push events to close/start our segment before we push the buffer. */
2865 if (G_UNLIKELY (src->priv->segment_pending)) {
2866 GstEvent *seg_event = gst_event_new_segment (&src->segment);
2868 gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2869 src->priv->segment_seqnum = gst_util_seqnum_next ();
2870 gst_pad_push_event (pad, seg_event);
2871 src->priv->segment_pending = FALSE;
2874 if (g_atomic_int_get (&src->priv->have_events)) {
2875 GST_OBJECT_LOCK (src);
2876 /* take the events */
2877 pending_events = src->priv->pending_events;
2878 src->priv->pending_events = NULL;
2879 g_atomic_int_set (&src->priv->have_events, FALSE);
2880 GST_OBJECT_UNLOCK (src);
2883 /* Push out pending events if any */
2884 if (G_UNLIKELY (pending_events != NULL)) {
2885 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2886 GstEvent *ev = (GstEvent *) tmp->data;
2887 gst_pad_push_event (pad, ev);
2889 g_list_free (pending_events);
2892 /* figure out the new position */
2893 switch (src->segment.format) {
2894 case GST_FORMAT_BYTES:
2896 guint bufsize = gst_buffer_get_size (buf);
2898 /* we subtracted above for negative rates */
2899 if (src->segment.rate >= 0.0)
2900 position += bufsize;
2903 case GST_FORMAT_TIME:
2905 GstClockTime start, duration;
2907 start = GST_BUFFER_TIMESTAMP (buf);
2908 duration = GST_BUFFER_DURATION (buf);
2910 if (GST_CLOCK_TIME_IS_VALID (start))
2913 position = src->segment.position;
2915 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2916 if (src->segment.rate >= 0.0)
2917 position += duration;
2918 else if (position > duration)
2919 position -= duration;
2925 case GST_FORMAT_DEFAULT:
2926 if (src->segment.rate >= 0.0)
2927 position = GST_BUFFER_OFFSET_END (buf);
2929 position = GST_BUFFER_OFFSET (buf);
2935 if (position != -1) {
2936 if (src->segment.rate >= 0.0) {
2937 /* positive rate, check if we reached the stop */
2938 if (src->segment.stop != -1) {
2939 if (position >= src->segment.stop) {
2940 if (g_atomic_int_get (&src->priv->automatic_eos))
2942 position = src->segment.stop;
2946 /* negative rate, check if we reached the start. start is always set to
2947 * something different from -1 */
2948 if (position <= src->segment.start) {
2949 if (g_atomic_int_get (&src->priv->automatic_eos))
2951 position = src->segment.start;
2953 /* when going reverse, all buffers are DISCONT */
2954 src->priv->discont = TRUE;
2956 GST_OBJECT_LOCK (src);
2957 src->segment.position = position;
2958 GST_OBJECT_UNLOCK (src);
2961 if (G_UNLIKELY (src->priv->discont)) {
2962 GST_INFO_OBJECT (src, "marking pending DISCONT");
2963 buf = gst_buffer_make_writable (buf);
2964 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2965 src->priv->discont = FALSE;
2967 GST_LIVE_UNLOCK (src);
2969 /* push buffer or buffer list */
2970 if (src->priv->pending_bufferlist != NULL) {
2971 ret = gst_pad_push_list (pad, src->priv->pending_bufferlist);
2972 src->priv->pending_bufferlist = NULL;
2974 ret = gst_pad_push (pad, buf);
2977 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2978 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2979 goto not_negotiated;
2981 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2982 gst_flow_get_name (ret));
2986 /* Segment pending means that a new segment was configured
2987 * during this loop run */
2988 if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2989 GST_INFO_OBJECT (src, "pausing after end of segment");
3000 if (gst_pad_needs_reconfigure (pad)) {
3001 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
3004 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
3005 * a pending negotiation request on our srcpad */
3009 GST_DEBUG_OBJECT (src, "Not negotiated");
3010 ret = GST_FLOW_NOT_NEGOTIATED;
3015 GST_DEBUG_OBJECT (src, "we are flushing");
3016 GST_LIVE_UNLOCK (src);
3017 ret = GST_FLOW_FLUSHING;
3022 const gchar *reason = gst_flow_get_name (ret);
3025 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
3026 src->running = FALSE;
3027 gst_pad_pause_task (pad);
3028 if (ret == GST_FLOW_EOS) {
3029 gboolean flag_segment;
3033 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
3034 format = src->segment.format;
3035 position = src->segment.position;
3037 /* perform EOS logic */
3038 if (src->priv->forced_eos) {
3039 g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
3040 GST_OBJECT_LOCK (src);
3041 event = src->priv->pending_eos;
3042 src->priv->pending_eos = NULL;
3043 GST_OBJECT_UNLOCK (src);
3045 } else if (flag_segment) {
3046 GstMessage *message;
3048 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
3050 gst_message_set_seqnum (message, src->priv->seqnum);
3051 gst_element_post_message (GST_ELEMENT_CAST (src), message);
3052 event = gst_event_new_segment_done (format, position);
3053 gst_event_set_seqnum (event, src->priv->seqnum);
3056 event = gst_event_new_eos ();
3057 gst_event_set_seqnum (event, src->priv->seqnum);
3060 gst_pad_push_event (pad, event);
3061 src->priv->forced_eos = FALSE;
3063 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
3064 event = gst_event_new_eos ();
3065 gst_event_set_seqnum (event, src->priv->seqnum);
3066 /* for fatal errors we post an error message, post the error
3067 * first so the app knows about the error first.
3068 * Also don't do this for FLUSHING because it happens
3069 * due to flushing and posting an error message because of
3070 * that is the wrong thing to do, e.g. when we're doing
3071 * a flushing seek. */
3072 GST_ELEMENT_FLOW_ERROR (src, ret);
3073 gst_pad_push_event (pad, event);
3080 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
3081 GstAllocator * allocator, GstAllocationParams * params)
3083 GstAllocator *oldalloc;
3084 GstBufferPool *oldpool;
3085 GstBaseSrcPrivate *priv = basesrc->priv;
3088 GST_DEBUG_OBJECT (basesrc, "activate pool");
3089 if (!gst_buffer_pool_set_active (pool, TRUE))
3090 goto activate_failed;
3093 GST_OBJECT_LOCK (basesrc);
3094 oldpool = priv->pool;
3097 oldalloc = priv->allocator;
3098 priv->allocator = allocator;
3101 gst_object_ref (priv->pool);
3102 if (priv->allocator)
3103 gst_object_ref (priv->allocator);
3106 priv->params = *params;
3108 gst_allocation_params_init (&priv->params);
3109 GST_OBJECT_UNLOCK (basesrc);
3112 /* only deactivate if the pool is not the one we're using */
3113 if (oldpool != pool) {
3114 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
3115 gst_buffer_pool_set_active (oldpool, FALSE);
3117 gst_object_unref (oldpool);
3120 gst_object_unref (oldalloc);
3127 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
3133 gst_base_src_set_pool_flushing (GstBaseSrc * basesrc, gboolean flushing)
3135 GstBaseSrcPrivate *priv = basesrc->priv;
3136 GstBufferPool *pool;
3138 GST_OBJECT_LOCK (basesrc);
3139 if ((pool = priv->pool))
3140 pool = gst_object_ref (pool);
3141 GST_OBJECT_UNLOCK (basesrc);
3144 gst_buffer_pool_set_flushing (pool, flushing);
3145 gst_object_unref (pool);
3151 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3154 GstBufferPool *pool;
3155 guint size, min, max;
3156 GstAllocator *allocator;
3157 GstAllocationParams params;
3158 GstStructure *config;
3159 gboolean update_allocator;
3161 gst_query_parse_allocation (query, &outcaps, NULL);
3163 /* we got configuration from our peer or the decide_allocation method,
3165 if (gst_query_get_n_allocation_params (query) > 0) {
3166 /* try the allocator */
3167 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3168 update_allocator = TRUE;
3171 gst_allocation_params_init (¶ms);
3172 update_allocator = FALSE;
3175 if (gst_query_get_n_allocation_pools (query) > 0) {
3176 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3179 /* no pool, we can make our own */
3180 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3181 pool = gst_buffer_pool_new ();
3185 size = min = max = 0;
3190 config = gst_buffer_pool_get_config (pool);
3191 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3192 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3194 /* buffer pool may have to do some changes */
3195 if (!gst_buffer_pool_set_config (pool, config)) {
3196 config = gst_buffer_pool_get_config (pool);
3198 /* If change are not acceptable, fallback to generic pool */
3199 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3201 GST_DEBUG_OBJECT (basesrc, "unsupported pool, making new pool");
3203 gst_object_unref (pool);
3204 pool = gst_buffer_pool_new ();
3205 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3206 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3209 if (!gst_buffer_pool_set_config (pool, config))
3214 if (update_allocator)
3215 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
3217 gst_query_add_allocation_param (query, allocator, ¶ms);
3219 gst_object_unref (allocator);
3222 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3223 gst_object_unref (pool);
3229 GST_ELEMENT_ERROR (basesrc, RESOURCE, SETTINGS,
3230 ("Failed to configure the buffer pool"),
3231 ("Configuration is most likely invalid, please report this issue."));
3232 gst_object_unref (pool);
3237 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3239 GstBaseSrcClass *bclass;
3240 gboolean result = TRUE;
3242 GstBufferPool *pool = NULL;
3243 GstAllocator *allocator = NULL;
3244 GstAllocationParams params;
3246 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3248 /* make query and let peer pad answer, we don't really care if it worked or
3249 * not, if it failed, the allocation query would contain defaults and the
3250 * subclass would then set better values if needed */
3251 query = gst_query_new_allocation (caps, TRUE);
3252 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3253 /* not a problem, just debug a little */
3254 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3257 g_assert (bclass->decide_allocation != NULL);
3258 result = bclass->decide_allocation (basesrc, query);
3260 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3264 goto no_decide_allocation;
3266 /* we got configuration from our peer or the decide_allocation method,
3268 if (gst_query_get_n_allocation_params (query) > 0) {
3269 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3272 gst_allocation_params_init (¶ms);
3275 if (gst_query_get_n_allocation_pools (query) > 0)
3276 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3278 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
3281 gst_object_unref (allocator);
3283 gst_object_unref (pool);
3285 gst_query_unref (query);
3290 no_decide_allocation:
3292 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3293 gst_query_unref (query);
3299 /* default negotiation code.
3301 * Take intersection between src and sink pads, take first
3305 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3308 GstCaps *caps = NULL;
3309 GstCaps *peercaps = NULL;
3310 gboolean result = FALSE;
3312 /* first see what is possible on our source pad */
3313 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3314 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3315 /* nothing or anything is allowed, we're done */
3316 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3317 goto no_nego_needed;
3319 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3322 /* get the peer caps */
3323 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3324 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3326 /* The result is already a subset of our caps */
3328 gst_caps_unref (thiscaps);
3330 /* no peer, work with our own caps then */
3333 if (caps && !gst_caps_is_empty (caps)) {
3335 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3336 if (gst_caps_is_any (caps)) {
3337 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3338 /* hmm, still anything, so element can do anything and
3339 * nego is not needed */
3342 caps = gst_base_src_fixate (basesrc, caps);
3343 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3344 if (gst_caps_is_fixed (caps)) {
3345 /* yay, fixed caps, use those then, it's possible that the subclass does
3346 * not accept this caps after all and we have to fail. */
3347 result = gst_base_src_set_caps (basesrc, caps);
3350 gst_caps_unref (caps);
3353 gst_caps_unref (caps);
3354 GST_DEBUG_OBJECT (basesrc, "no common caps");
3360 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3362 gst_caps_unref (thiscaps);
3367 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3368 ("No supported formats found"),
3369 ("This element did not produce valid caps"));
3371 gst_caps_unref (thiscaps);
3377 gst_base_src_negotiate (GstBaseSrc * basesrc)
3379 GstBaseSrcClass *bclass;
3382 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3384 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3386 if (G_LIKELY (bclass->negotiate))
3387 result = bclass->negotiate (basesrc);
3391 if (G_LIKELY (result)) {
3394 caps = gst_pad_get_current_caps (basesrc->srcpad);
3396 result = gst_base_src_prepare_allocation (basesrc, caps);
3399 gst_caps_unref (caps);
3405 gst_base_src_start (GstBaseSrc * basesrc)
3407 GstBaseSrcClass *bclass;
3410 GST_LIVE_LOCK (basesrc);
3412 GST_OBJECT_LOCK (basesrc);
3413 if (GST_BASE_SRC_IS_STARTING (basesrc))
3415 if (GST_BASE_SRC_IS_STARTED (basesrc))
3418 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3419 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3420 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3421 GST_OBJECT_UNLOCK (basesrc);
3423 basesrc->num_buffers_left = basesrc->num_buffers;
3424 basesrc->running = FALSE;
3425 basesrc->priv->segment_pending = FALSE;
3426 basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3427 basesrc->priv->forced_eos = FALSE;
3428 GST_LIVE_UNLOCK (basesrc);
3430 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3432 result = bclass->start (basesrc);
3437 goto could_not_start;
3439 if (!gst_base_src_is_async (basesrc)) {
3440 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3441 /* not really waiting here, we call this to get the result
3442 * from the start_complete call */
3443 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3451 GST_DEBUG_OBJECT (basesrc, "was starting");
3452 GST_OBJECT_UNLOCK (basesrc);
3453 GST_LIVE_UNLOCK (basesrc);
3458 GST_DEBUG_OBJECT (basesrc, "was started");
3459 GST_OBJECT_UNLOCK (basesrc);
3460 GST_LIVE_UNLOCK (basesrc);
3465 GST_DEBUG_OBJECT (basesrc, "could not start");
3466 /* subclass is supposed to post a message but we post one as a fallback
3467 * just in case. We don't have to call _stop. */
3468 GST_ELEMENT_ERROR (basesrc, CORE, STATE_CHANGE, (NULL),
3469 ("Failed to start"));
3470 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3476 * gst_base_src_start_complete:
3477 * @basesrc: base source instance
3478 * @ret: a #GstFlowReturn
3480 * Complete an asynchronous start operation. When the subclass overrides the
3481 * start method, it should call gst_base_src_start_complete() when the start
3482 * operation completes either from the same thread or from an asynchronous
3486 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3495 if (ret != GST_FLOW_OK)
3498 GST_DEBUG_OBJECT (basesrc, "starting source");
3499 format = basesrc->segment.format;
3501 /* figure out the size */
3504 if (format == GST_FORMAT_BYTES) {
3505 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3507 if (bclass->get_size) {
3508 if (!(have_size = bclass->get_size (basesrc, &size)))
3511 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3512 /* only update the size when operating in bytes, subclass is supposed
3513 * to set duration in the start method for other formats */
3514 GST_OBJECT_LOCK (basesrc);
3515 basesrc->segment.duration = size;
3516 GST_OBJECT_UNLOCK (basesrc);
3519 GST_DEBUG_OBJECT (basesrc,
3520 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3521 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3522 basesrc->segment.duration);
3524 seekable = gst_base_src_seekable (basesrc);
3525 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3527 /* update for random access flag */
3528 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3530 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3532 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3534 GST_OBJECT_LOCK (basesrc->srcpad);
3535 mode = GST_PAD_MODE (basesrc->srcpad);
3536 GST_OBJECT_UNLOCK (basesrc->srcpad);
3538 /* take the stream lock here, we only want to let the task run when we have
3539 * set the STARTED flag */
3540 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3542 case GST_PAD_MODE_PUSH:
3543 /* do initial seek, which will start the task */
3544 GST_OBJECT_LOCK (basesrc);
3545 event = basesrc->pending_seek;
3546 basesrc->pending_seek = NULL;
3547 GST_OBJECT_UNLOCK (basesrc);
3549 /* The perform seek code will start the task when finished. We don't have to
3550 * unlock the streaming thread because it is not running yet */
3551 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3555 gst_event_unref (event);
3557 case GST_PAD_MODE_PULL:
3558 /* if not random_access, we cannot operate in pull mode for now */
3559 if (G_UNLIKELY (!basesrc->random_access))
3563 goto not_activated_yet;
3567 GST_OBJECT_LOCK (basesrc);
3568 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3569 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3570 basesrc->priv->start_result = ret;
3571 GST_ASYNC_SIGNAL (basesrc);
3572 GST_OBJECT_UNLOCK (basesrc);
3574 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3580 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3581 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3582 gst_base_src_stop (basesrc);
3584 gst_event_unref (event);
3585 ret = GST_FLOW_ERROR;
3590 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3591 gst_base_src_stop (basesrc);
3592 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3593 ret = GST_FLOW_ERROR;
3598 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3599 gst_base_src_stop (basesrc);
3600 GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3601 ret = GST_FLOW_ERROR;
3606 GST_OBJECT_LOCK (basesrc);
3607 basesrc->priv->start_result = ret;
3608 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3609 GST_ASYNC_SIGNAL (basesrc);
3610 GST_OBJECT_UNLOCK (basesrc);
3616 * gst_base_src_start_wait:
3617 * @basesrc: base source instance
3619 * Wait until the start operation completes.
3621 * Returns: a #GstFlowReturn.
3624 gst_base_src_start_wait (GstBaseSrc * basesrc)
3626 GstFlowReturn result;
3628 GST_OBJECT_LOCK (basesrc);
3629 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3630 GST_ASYNC_WAIT (basesrc);
3632 result = basesrc->priv->start_result;
3633 GST_OBJECT_UNLOCK (basesrc);
3635 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3641 gst_base_src_stop (GstBaseSrc * basesrc)
3643 GstBaseSrcClass *bclass;
3644 gboolean result = TRUE;
3646 GST_DEBUG_OBJECT (basesrc, "stopping source");
3649 gst_base_src_set_flushing (basesrc, TRUE);
3652 gst_pad_stop_task (basesrc->srcpad);
3653 /* stop flushing, this will balance unlock/unlock_stop calls */
3654 gst_base_src_set_flushing (basesrc, FALSE);
3656 GST_OBJECT_LOCK (basesrc);
3657 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3660 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3661 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3662 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3663 GST_ASYNC_SIGNAL (basesrc);
3664 GST_OBJECT_UNLOCK (basesrc);
3666 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3668 result = bclass->stop (basesrc);
3670 if (basesrc->priv->pending_bufferlist != NULL) {
3671 gst_buffer_list_unref (basesrc->priv->pending_bufferlist);
3672 basesrc->priv->pending_bufferlist = NULL;
3675 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3681 GST_DEBUG_OBJECT (basesrc, "was stopped");
3682 GST_OBJECT_UNLOCK (basesrc);
3687 /* start or stop flushing dataprocessing
3690 gst_base_src_set_flushing (GstBaseSrc * basesrc, gboolean flushing)
3692 GstBaseSrcClass *bclass;
3694 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3696 GST_DEBUG_OBJECT (basesrc, "flushing %d", flushing);
3699 gst_base_src_set_pool_flushing (basesrc, TRUE);
3700 /* unlock any subclasses to allow turning off the streaming thread */
3702 bclass->unlock (basesrc);
3705 /* the live lock is released when we are blocked, waiting for playing,
3706 * when we sync to the clock or creating a buffer */
3707 GST_LIVE_LOCK (basesrc);
3708 basesrc->priv->flushing = flushing;
3710 /* clear pending EOS if any */
3711 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3712 GST_OBJECT_LOCK (basesrc);
3713 CLEAR_PENDING_EOS (basesrc);
3714 basesrc->priv->forced_eos = FALSE;
3715 GST_OBJECT_UNLOCK (basesrc);
3718 /* unblock clock sync (if any) or any other blocking thing */
3719 if (basesrc->clock_id)
3720 gst_clock_id_unschedule (basesrc->clock_id);
3722 gst_base_src_set_pool_flushing (basesrc, FALSE);
3724 /* Drop all delayed events */
3725 GST_OBJECT_LOCK (basesrc);
3726 if (basesrc->priv->pending_events) {
3727 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3729 g_list_free (basesrc->priv->pending_events);
3730 basesrc->priv->pending_events = NULL;
3731 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3733 GST_OBJECT_UNLOCK (basesrc);
3736 GST_LIVE_SIGNAL (basesrc);
3737 GST_LIVE_UNLOCK (basesrc);
3740 /* Now wait for the stream lock to be released and clear our unlock request */
3741 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3742 if (bclass->unlock_stop)
3743 bclass->unlock_stop (basesrc);
3744 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3750 /* the purpose of this function is to make sure that a live source blocks in the
3751 * LIVE lock or leaves the LIVE lock and continues playing. */
3753 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3755 /* we are now able to grab the LIVE lock, when we get it, we can be
3756 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3758 GST_LIVE_LOCK (basesrc);
3759 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3761 /* unblock clock sync (if any) */
3762 if (basesrc->clock_id)
3763 gst_clock_id_unschedule (basesrc->clock_id);
3765 /* configure what to do when we get to the LIVE lock. */
3766 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3767 basesrc->live_running = live_play;
3772 /* for live sources we restart the timestamp correction */
3773 GST_OBJECT_LOCK (basesrc);
3774 basesrc->priv->latency = -1;
3775 GST_OBJECT_UNLOCK (basesrc);
3776 /* have to restart the task in case it stopped because of the unlock when
3777 * we went to PAUSED. Only do this if we operating in push mode. */
3778 GST_OBJECT_LOCK (basesrc->srcpad);
3779 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3780 GST_OBJECT_UNLOCK (basesrc->srcpad);
3782 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3783 basesrc->srcpad, NULL);
3784 GST_DEBUG_OBJECT (basesrc, "signal");
3785 GST_LIVE_SIGNAL (basesrc);
3787 GST_LIVE_UNLOCK (basesrc);
3793 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3795 GstBaseSrc *basesrc;
3797 basesrc = GST_BASE_SRC (parent);
3799 /* prepare subclass first */
3801 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3803 if (G_UNLIKELY (!basesrc->can_activate_push))
3804 goto no_push_activation;
3806 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3809 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3810 /* now we can stop the source */
3811 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3819 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3824 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3829 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3835 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3837 GstBaseSrc *basesrc;
3839 basesrc = GST_BASE_SRC (parent);
3841 /* prepare subclass first */
3843 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3844 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3847 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3848 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3856 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3861 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3867 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3868 GstPadMode mode, gboolean active)
3871 GstBaseSrc *src = GST_BASE_SRC (parent);
3873 src->priv->stream_start_pending = FALSE;
3875 GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3878 case GST_PAD_MODE_PULL:
3879 res = gst_base_src_activate_pull (pad, parent, active);
3881 case GST_PAD_MODE_PUSH:
3882 src->priv->stream_start_pending = active;
3883 res = gst_base_src_activate_push (pad, parent, active);
3886 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3894 static GstStateChangeReturn
3895 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3897 GstBaseSrc *basesrc;
3898 GstStateChangeReturn result;
3899 gboolean no_preroll = FALSE;
3901 basesrc = GST_BASE_SRC (element);
3903 switch (transition) {
3904 case GST_STATE_CHANGE_NULL_TO_READY:
3906 case GST_STATE_CHANGE_READY_TO_PAUSED:
3907 no_preroll = gst_base_src_is_live (basesrc);
3909 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3910 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3911 if (gst_base_src_is_live (basesrc)) {
3912 /* now we can start playback */
3913 gst_base_src_set_playing (basesrc, TRUE);
3921 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3922 transition)) == GST_STATE_CHANGE_FAILURE)
3925 switch (transition) {
3926 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3927 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3928 if (gst_base_src_is_live (basesrc)) {
3929 /* make sure we block in the live cond in PAUSED */
3930 gst_base_src_set_playing (basesrc, FALSE);
3934 case GST_STATE_CHANGE_PAUSED_TO_READY:
3936 /* we don't need to unblock anything here, the pad deactivation code
3937 * already did this */
3938 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3939 GST_OBJECT_LOCK (basesrc);
3940 CLEAR_PENDING_EOS (basesrc);
3941 GST_OBJECT_UNLOCK (basesrc);
3943 gst_event_replace (&basesrc->pending_seek, NULL);
3946 case GST_STATE_CHANGE_READY_TO_NULL:
3952 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3953 result = GST_STATE_CHANGE_NO_PREROLL;
3960 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3966 * gst_base_src_get_buffer_pool:
3967 * @src: a #GstBaseSrc
3969 * Returns: (transfer full): the instance of the #GstBufferPool used
3970 * by the src; unref it after usage.
3973 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3975 GstBufferPool *ret = NULL;
3977 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3979 GST_OBJECT_LOCK (src);
3980 if (src->priv->pool)
3981 ret = gst_object_ref (src->priv->pool);
3982 GST_OBJECT_UNLOCK (src);
3988 * gst_base_src_get_allocator:
3989 * @src: a #GstBaseSrc
3990 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3992 * @params: (out) (allow-none) (transfer full): the
3993 * #GstAllocationParams of @allocator
3995 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3996 * used by the base class and its @params.
3998 * Unref the @allocator after usage.
4001 gst_base_src_get_allocator (GstBaseSrc * src,
4002 GstAllocator ** allocator, GstAllocationParams * params)
4004 g_return_if_fail (GST_IS_BASE_SRC (src));
4006 GST_OBJECT_LOCK (src);
4008 *allocator = src->priv->allocator ?
4009 gst_object_ref (src->priv->allocator) : NULL;
4012 *params = src->priv->params;
4013 GST_OBJECT_UNLOCK (src);
4017 * gst_base_src_submit_buffer_list:
4018 * @src: a #GstBaseSrc
4019 * @buffer_list: (transfer full): a #GstBufferList
4021 * Subclasses can call this from their create virtual method implementation
4022 * to submit a buffer list to be pushed out later. This is useful in
4023 * cases where the create function wants to produce multiple buffers to be
4024 * pushed out in one go in form of a #GstBufferList, which can reduce overhead
4025 * drastically, especially for packetised inputs (for data streams where
4026 * the packetisation/chunking is not important it is usually more efficient
4027 * to return larger buffers instead).
4029 * Subclasses that use this function from their create function must return
4030 * %GST_FLOW_OK and no buffer from their create virtual method implementation.
4031 * If a buffer is returned after a buffer list has also been submitted via this
4032 * function the behaviour is undefined.
4034 * Subclasses must only call this function once per create function call and
4035 * subclasses must only call this function when the source operates in push
4041 gst_base_src_submit_buffer_list (GstBaseSrc * src, GstBufferList * buffer_list)
4043 g_return_if_fail (GST_IS_BASE_SRC (src));
4044 g_return_if_fail (GST_IS_BUFFER_LIST (buffer_list));
4045 g_return_if_fail (BASE_SRC_HAS_PENDING_BUFFER_LIST (src) == FALSE);
4047 /* we need it to be writable later in get_range() where we use get_writable */
4048 src->priv->pending_bufferlist = gst_buffer_list_make_writable (buffer_list);
4050 GST_LOG_OBJECT (src, "%u buffers submitted in buffer list",
4051 gst_buffer_list_length (buffer_list));