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.
662 gst_base_src_set_automatic_eos (GstBaseSrc * src, gboolean automatic_eos)
664 g_return_if_fail (GST_IS_BASE_SRC (src));
666 g_atomic_int_set (&src->priv->automatic_eos, automatic_eos);
670 * gst_base_src_set_async:
671 * @src: base source instance
672 * @async: new async mode
674 * Configure async behaviour in @src, no state change will block. The open,
675 * close, start, stop, play and pause virtual methods will be executed in a
676 * different thread and are thus allowed to perform blocking operations. Any
677 * blocking operation should be unblocked with the unlock vmethod.
680 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
682 g_return_if_fail (GST_IS_BASE_SRC (src));
684 GST_OBJECT_LOCK (src);
685 src->priv->async = async;
686 GST_OBJECT_UNLOCK (src);
690 * gst_base_src_is_async:
691 * @src: base source instance
693 * Get the current async behaviour of @src. See also gst_base_src_set_async().
695 * Returns: %TRUE if @src is operating in async mode.
698 gst_base_src_is_async (GstBaseSrc * src)
702 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
704 GST_OBJECT_LOCK (src);
705 res = src->priv->async;
706 GST_OBJECT_UNLOCK (src);
713 * gst_base_src_query_latency:
715 * @live: (out) (allow-none): if the source is live
716 * @min_latency: (out) (allow-none): the min latency of the source
717 * @max_latency: (out) (allow-none): the max latency of the source
719 * Query the source for the latency parameters. @live will be %TRUE when @src is
720 * configured as a live source. @min_latency and @max_latency will be set
721 * to the difference between the running time and the timestamp of the first
724 * This function is mostly used by subclasses.
726 * Returns: %TRUE if the query succeeded.
729 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
730 GstClockTime * min_latency, GstClockTime * max_latency)
734 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
736 GST_OBJECT_LOCK (src);
738 *live = src->is_live;
740 /* if we have a startup latency, report this one, else report 0. Subclasses
741 * are supposed to override the query function if they want something
743 if (src->priv->latency != -1)
744 min = src->priv->latency;
753 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
754 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
755 GST_TIME_ARGS (min));
756 GST_OBJECT_UNLOCK (src);
762 * gst_base_src_set_blocksize:
764 * @blocksize: the new blocksize in bytes
766 * Set the number of bytes that @src will push out with each buffer. When
767 * @blocksize is set to -1, a default length will be used.
770 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
772 g_return_if_fail (GST_IS_BASE_SRC (src));
774 GST_OBJECT_LOCK (src);
775 src->blocksize = blocksize;
776 GST_OBJECT_UNLOCK (src);
780 * gst_base_src_get_blocksize:
783 * Get the number of bytes that @src will push out with each buffer.
785 * Returns: the number of bytes pushed with each buffer.
788 gst_base_src_get_blocksize (GstBaseSrc * src)
792 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
794 GST_OBJECT_LOCK (src);
795 res = src->blocksize;
796 GST_OBJECT_UNLOCK (src);
803 * gst_base_src_set_do_timestamp:
805 * @timestamp: enable or disable timestamping
807 * Configure @src to automatically timestamp outgoing buffers based on the
808 * current running_time of the pipeline. This property is mostly useful for live
812 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
814 g_return_if_fail (GST_IS_BASE_SRC (src));
816 GST_OBJECT_LOCK (src);
817 src->priv->do_timestamp = timestamp;
818 if (timestamp && src->segment.format != GST_FORMAT_TIME)
819 gst_segment_init (&src->segment, GST_FORMAT_TIME);
820 GST_OBJECT_UNLOCK (src);
824 * gst_base_src_get_do_timestamp:
827 * Query if @src timestamps outgoing buffers based on the current running_time.
829 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
832 gst_base_src_get_do_timestamp (GstBaseSrc * src)
836 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
838 GST_OBJECT_LOCK (src);
839 res = src->priv->do_timestamp;
840 GST_OBJECT_UNLOCK (src);
846 * gst_base_src_new_seamless_segment:
848 * @start: The new start value for the segment
849 * @stop: Stop value for the new segment
850 * @time: The new time value for the start of the new segment
852 * Prepare a new seamless segment for emission downstream. This function must
853 * only be called by derived sub-classes, and only from the create() function,
854 * as the stream-lock needs to be held.
856 * The format for the new segment will be the current format of the source, as
857 * configured with gst_base_src_set_format()
859 * Returns: %TRUE if preparation of the seamless segment succeeded.
862 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
867 GST_OBJECT_LOCK (src);
869 src->segment.base = gst_segment_to_running_time (&src->segment,
870 src->segment.format, src->segment.position);
871 src->segment.position = src->segment.start = start;
872 src->segment.stop = stop;
873 src->segment.time = time;
875 /* Mark pending segment. Will be sent before next data */
876 src->priv->segment_pending = TRUE;
877 src->priv->segment_seqnum = gst_util_seqnum_next ();
879 GST_DEBUG_OBJECT (src,
880 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
881 GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
882 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
883 GST_TIME_ARGS (src->segment.base));
885 GST_OBJECT_UNLOCK (src);
887 src->priv->discont = TRUE;
893 /* called with STREAM_LOCK */
895 gst_base_src_send_stream_start (GstBaseSrc * src)
899 if (src->priv->stream_start_pending) {
904 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
906 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
907 event = gst_event_new_stream_start (stream_id);
908 gst_event_set_group_id (event, gst_util_group_id_next ());
910 ret = gst_pad_push_event (src->srcpad, event);
911 src->priv->stream_start_pending = FALSE;
919 * gst_base_src_set_caps:
920 * @src: a #GstBaseSrc
921 * @caps: (transfer none): a #GstCaps
923 * Set new caps on the basesrc source pad.
925 * Returns: %TRUE if the caps could be set
928 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
930 GstBaseSrcClass *bclass;
932 GstCaps *current_caps;
934 bclass = GST_BASE_SRC_GET_CLASS (src);
936 gst_base_src_send_stream_start (src);
938 current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (src));
939 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
940 GST_DEBUG_OBJECT (src, "New caps equal to old ones: %" GST_PTR_FORMAT,
944 if (bclass->set_caps)
945 res = bclass->set_caps (src, caps);
948 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
952 gst_caps_unref (current_caps);
958 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
960 GstCaps *caps = NULL;
961 GstPadTemplate *pad_template;
962 GstBaseSrcClass *bclass;
964 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
967 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
969 if (pad_template != NULL) {
970 caps = gst_pad_template_get_caps (pad_template);
973 GstCaps *intersection;
976 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
977 gst_caps_unref (caps);
985 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
987 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
988 return gst_caps_fixate (caps);
992 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
994 GstBaseSrcClass *bclass;
996 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
999 caps = bclass->fixate (bsrc, caps);
1005 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
1009 switch (GST_QUERY_TYPE (query)) {
1010 case GST_QUERY_POSITION:
1014 gst_query_parse_position (query, &format, NULL);
1016 GST_DEBUG_OBJECT (src, "position query in format %s",
1017 gst_format_get_name (format));
1020 case GST_FORMAT_PERCENT:
1026 GST_OBJECT_LOCK (src);
1027 position = src->segment.position;
1028 duration = src->segment.duration;
1029 GST_OBJECT_UNLOCK (src);
1031 if (position != -1 && duration != -1) {
1032 if (position < duration)
1033 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1036 percent = GST_FORMAT_PERCENT_MAX;
1040 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1047 GstFormat seg_format;
1049 GST_OBJECT_LOCK (src);
1051 gst_segment_to_stream_time (&src->segment, src->segment.format,
1052 src->segment.position);
1053 seg_format = src->segment.format;
1054 GST_OBJECT_UNLOCK (src);
1056 if (position != -1) {
1057 /* convert to requested format */
1059 gst_pad_query_convert (src->srcpad, seg_format,
1060 position, format, &position);
1065 gst_query_set_position (query, format, position);
1072 case GST_QUERY_DURATION:
1076 gst_query_parse_duration (query, &format, NULL);
1078 GST_DEBUG_OBJECT (src, "duration query in format %s",
1079 gst_format_get_name (format));
1082 case GST_FORMAT_PERCENT:
1083 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1084 GST_FORMAT_PERCENT_MAX);
1090 GstFormat seg_format;
1093 /* may have to refresh duration */
1094 gst_base_src_update_length (src, 0, &length,
1095 g_atomic_int_get (&src->priv->dynamic_size));
1097 /* this is the duration as configured by the subclass. */
1098 GST_OBJECT_LOCK (src);
1099 duration = src->segment.duration;
1100 seg_format = src->segment.format;
1101 GST_OBJECT_UNLOCK (src);
1103 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1104 duration, gst_format_get_name (seg_format));
1106 if (duration != -1) {
1107 /* convert to requested format, if this fails, we have a duration
1108 * but we cannot answer the query, we must return FALSE. */
1110 gst_pad_query_convert (src->srcpad, seg_format,
1111 duration, format, &duration);
1113 /* The subclass did not configure a duration, we assume that the
1114 * media has an unknown duration then and we return TRUE to report
1115 * this. Note that this is not the same as returning FALSE, which
1116 * means that we cannot report the duration at all. */
1121 gst_query_set_duration (query, format, duration);
1129 case GST_QUERY_SEEKING:
1131 GstFormat format, seg_format;
1134 GST_OBJECT_LOCK (src);
1135 duration = src->segment.duration;
1136 seg_format = src->segment.format;
1137 GST_OBJECT_UNLOCK (src);
1139 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1140 if (format == seg_format) {
1141 gst_query_set_seeking (query, seg_format,
1142 gst_base_src_seekable (src), 0, duration);
1145 /* FIXME 2.0: return TRUE + seekable=FALSE for SEEKING query here */
1146 /* Don't reply to the query to make up for demuxers which don't
1147 * handle the SEEKING query yet. Players like Totem will fall back
1148 * to the duration when the SEEKING query isn't answered. */
1153 case GST_QUERY_SEGMENT:
1158 GST_OBJECT_LOCK (src);
1160 format = src->segment.format;
1163 gst_segment_to_stream_time (&src->segment, format,
1164 src->segment.start);
1165 if ((stop = src->segment.stop) == -1)
1166 stop = src->segment.duration;
1168 stop = gst_segment_to_stream_time (&src->segment, format, stop);
1170 gst_query_set_segment (query, src->segment.rate, format, start, stop);
1172 GST_OBJECT_UNLOCK (src);
1177 case GST_QUERY_FORMATS:
1179 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1180 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1184 case GST_QUERY_CONVERT:
1186 GstFormat src_fmt, dest_fmt;
1187 gint64 src_val, dest_val;
1189 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1191 /* we can only convert between equal formats... */
1192 if (src_fmt == dest_fmt) {
1198 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1201 case GST_QUERY_LATENCY:
1203 GstClockTime min, max;
1206 /* Subclasses should override and implement something useful */
1207 res = gst_base_src_query_latency (src, &live, &min, &max);
1209 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1210 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1211 GST_TIME_ARGS (max));
1213 gst_query_set_latency (query, live, min, max);
1216 case GST_QUERY_JITTER:
1217 case GST_QUERY_RATE:
1220 case GST_QUERY_BUFFERING:
1222 GstFormat format, seg_format;
1223 gint64 start, stop, estimated;
1225 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1227 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1228 gst_format_get_name (format));
1230 GST_OBJECT_LOCK (src);
1231 if (src->random_access) {
1234 if (format == GST_FORMAT_PERCENT)
1235 stop = GST_FORMAT_PERCENT_MAX;
1237 stop = src->segment.duration;
1243 seg_format = src->segment.format;
1244 GST_OBJECT_UNLOCK (src);
1246 /* convert to required format. When the conversion fails, we can't answer
1247 * the query. When the value is unknown, we can don't perform conversion
1248 * but report TRUE. */
1249 if (format != GST_FORMAT_PERCENT && stop != -1) {
1250 res = gst_pad_query_convert (src->srcpad, seg_format,
1251 stop, format, &stop);
1255 if (res && format != GST_FORMAT_PERCENT && start != -1)
1256 res = gst_pad_query_convert (src->srcpad, seg_format,
1257 start, format, &start);
1259 gst_query_set_buffering_range (query, format, start, stop, estimated);
1262 case GST_QUERY_SCHEDULING:
1264 gboolean random_access;
1266 random_access = gst_base_src_is_random_access (src);
1268 /* we can operate in getrange mode if the native format is bytes
1269 * and we are seekable, this condition is set in the random_access
1270 * flag and is set in the _start() method. */
1271 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1273 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1274 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1279 case GST_QUERY_CAPS:
1281 GstBaseSrcClass *bclass;
1282 GstCaps *caps, *filter;
1284 bclass = GST_BASE_SRC_GET_CLASS (src);
1285 if (bclass->get_caps) {
1286 gst_query_parse_caps (query, &filter);
1287 if ((caps = bclass->get_caps (src, filter))) {
1288 gst_query_set_caps_result (query, caps);
1289 gst_caps_unref (caps);
1298 case GST_QUERY_URI:{
1299 if (GST_IS_URI_HANDLER (src)) {
1300 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1303 gst_query_set_uri (query, uri);
1318 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1325 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1328 GstBaseSrcClass *bclass;
1329 gboolean result = FALSE;
1331 src = GST_BASE_SRC (parent);
1332 bclass = GST_BASE_SRC_GET_CLASS (src);
1335 result = bclass->query (src, query);
1341 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1343 gboolean res = TRUE;
1345 /* update our offset if the start/stop position was updated */
1346 if (segment->format == GST_FORMAT_BYTES) {
1347 segment->time = segment->start;
1348 } else if (segment->start == 0) {
1349 /* seek to start, we can implement a default for this. */
1353 GST_INFO_OBJECT (src, "Can't do a default seek");
1360 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1362 GstBaseSrcClass *bclass;
1363 gboolean result = FALSE;
1365 bclass = GST_BASE_SRC_GET_CLASS (src);
1367 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1369 if (bclass->do_seek)
1370 result = bclass->do_seek (src, segment);
1375 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1378 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1379 GstSegment * segment)
1381 /* By default, we try one of 2 things:
1382 * - For absolute seek positions, convert the requested position to our
1383 * configured processing format and place it in the output segment \
1384 * - For relative seek positions, convert our current (input) values to the
1385 * seek format, adjust by the relative seek offset and then convert back to
1386 * the processing format
1388 GstSeekType start_type, stop_type;
1391 GstFormat seek_format, dest_format;
1394 gboolean res = TRUE;
1396 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1397 &start_type, &start, &stop_type, &stop);
1398 dest_format = segment->format;
1400 if (seek_format == dest_format) {
1401 gst_segment_do_seek (segment, rate, seek_format, flags,
1402 start_type, start, stop_type, stop, &update);
1406 if (start_type != GST_SEEK_TYPE_NONE) {
1407 /* FIXME: Handle seek_end by converting the input segment vals */
1409 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1411 start_type = GST_SEEK_TYPE_SET;
1414 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1415 /* FIXME: Handle seek_end by converting the input segment vals */
1417 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1419 stop_type = GST_SEEK_TYPE_SET;
1422 /* And finally, configure our output segment in the desired format */
1423 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1424 stop_type, stop, &update);
1433 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1439 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1440 GstSegment * seeksegment)
1442 GstBaseSrcClass *bclass;
1443 gboolean result = FALSE;
1445 bclass = GST_BASE_SRC_GET_CLASS (src);
1447 if (bclass->prepare_seek_segment)
1448 result = bclass->prepare_seek_segment (src, event, seeksegment);
1453 static GstFlowReturn
1454 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1455 guint size, GstBuffer ** buffer)
1458 GstBaseSrcPrivate *priv = src->priv;
1459 GstBufferPool *pool = NULL;
1460 GstAllocator *allocator = NULL;
1461 GstAllocationParams params;
1463 GST_OBJECT_LOCK (src);
1465 pool = gst_object_ref (priv->pool);
1466 } else if (priv->allocator) {
1467 allocator = gst_object_ref (priv->allocator);
1469 params = priv->params;
1470 GST_OBJECT_UNLOCK (src);
1473 ret = gst_buffer_pool_acquire_buffer (pool, buffer, NULL);
1474 } else if (size != -1) {
1475 *buffer = gst_buffer_new_allocate (allocator, size, ¶ms);
1476 if (G_UNLIKELY (*buffer == NULL))
1481 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1488 gst_object_unref (pool);
1490 gst_object_unref (allocator);
1497 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1498 ret = GST_FLOW_ERROR;
1503 static GstFlowReturn
1504 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1505 guint size, GstBuffer ** buffer)
1507 GstBaseSrcClass *bclass;
1511 bclass = GST_BASE_SRC_GET_CLASS (src);
1513 if (G_UNLIKELY (!bclass->alloc))
1515 if (G_UNLIKELY (!bclass->fill))
1518 if (*buffer == NULL) {
1519 /* downstream did not provide us with a buffer to fill, allocate one
1521 ret = bclass->alloc (src, offset, size, &res_buf);
1522 if (G_UNLIKELY (ret != GST_FLOW_OK))
1528 if (G_LIKELY (size > 0)) {
1529 /* only call fill when there is a size */
1530 ret = bclass->fill (src, offset, size, res_buf);
1531 if (G_UNLIKELY (ret != GST_FLOW_OK))
1542 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1543 return GST_FLOW_NOT_SUPPORTED;
1547 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1552 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1553 gst_flow_get_name (ret));
1554 if (*buffer == NULL)
1555 gst_buffer_unref (res_buf);
1560 /* this code implements the seeking. It is a good example
1561 * handling all cases.
1563 * A seek updates the currently configured segment.start
1564 * and segment.stop values based on the SEEK_TYPE. If the
1565 * segment.start value is updated, a seek to this new position
1566 * should be performed.
1568 * The seek can only be executed when we are not currently
1569 * streaming any data, to make sure that this is the case, we
1570 * acquire the STREAM_LOCK which is taken when we are in the
1571 * _loop() function or when a getrange() is called. Normally
1572 * we will not receive a seek if we are operating in pull mode
1573 * though. When we operate as a live source we might block on the live
1574 * cond, which does not release the STREAM_LOCK. Therefore we will try
1575 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1576 * safe to perform the seek.
1578 * When we are in the loop() function, we might be in the middle
1579 * of pushing a buffer, which might block in a sink. To make sure
1580 * that the push gets unblocked we push out a FLUSH_START event.
1581 * Our loop function will get a FLUSHING return value from
1582 * the push and will pause, effectively releasing the STREAM_LOCK.
1584 * For a non-flushing seek, we pause the task, which might eventually
1585 * release the STREAM_LOCK. We say eventually because when the sink
1586 * blocks on the sample we might wait a very long time until the sink
1587 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1588 * can continue the seek. A non-flushing seek is normally done in a
1589 * running pipeline to perform seamless playback, this means that the sink is
1590 * PLAYING and will return from its chain function.
1591 * In the case of a non-flushing seek we need to make sure that the
1592 * data we output after the seek is continuous with the previous data,
1593 * this is because a non-flushing seek does not reset the running-time
1594 * to 0. We do this by closing the currently running segment, ie. sending
1595 * a new_segment event with the stop position set to the last processed
1598 * After updating the segment.start/stop values, we prepare for
1599 * streaming again. We push out a FLUSH_STOP to make the peer pad
1600 * accept data again and we start our task again.
1602 * A segment seek posts a message on the bus saying that the playback
1603 * of the segment started. We store the segment flag internally because
1604 * when we reach the segment.stop we have to post a segment.done
1605 * instead of EOS when doing a segment seek.
1608 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1610 gboolean res = TRUE, tres;
1612 GstFormat seek_format, dest_format;
1614 GstSeekType start_type, stop_type;
1618 gboolean relative_seek = FALSE;
1619 gboolean seekseg_configured = FALSE;
1620 GstSegment seeksegment;
1624 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1626 GST_OBJECT_LOCK (src);
1627 dest_format = src->segment.format;
1628 GST_OBJECT_UNLOCK (src);
1631 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1632 &start_type, &start, &stop_type, &stop);
1634 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1635 SEEK_TYPE_IS_RELATIVE (stop_type);
1637 if (dest_format != seek_format && !relative_seek) {
1638 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1639 * here before taking the stream lock, otherwise we must convert it later,
1640 * once we have the stream lock and can read the last configures segment
1641 * start and stop positions */
1642 gst_segment_init (&seeksegment, dest_format);
1644 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1645 goto prepare_failed;
1647 seekseg_configured = TRUE;
1650 flush = flags & GST_SEEK_FLAG_FLUSH;
1651 seqnum = gst_event_get_seqnum (event);
1654 /* get next seqnum */
1655 seqnum = gst_util_seqnum_next ();
1658 /* send flush start */
1660 tevent = gst_event_new_flush_start ();
1661 gst_event_set_seqnum (tevent, seqnum);
1662 gst_pad_push_event (src->srcpad, tevent);
1664 gst_pad_pause_task (src->srcpad);
1666 /* unblock streaming thread. */
1668 gst_base_src_set_flushing (src, TRUE);
1670 /* grab streaming lock, this should eventually be possible, either
1671 * because the task is paused, our streaming thread stopped
1672 * or because our peer is flushing. */
1673 GST_PAD_STREAM_LOCK (src->srcpad);
1674 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1675 /* we have seen this event before, issue a warning for now */
1676 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1679 src->priv->seqnum = seqnum;
1680 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1684 gst_base_src_set_flushing (src, FALSE);
1686 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1687 * copy the current segment info into the temp segment that we can actually
1688 * attempt the seek with. We only update the real segment if the seek succeeds. */
1689 if (!seekseg_configured) {
1690 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1692 /* now configure the final seek segment */
1694 if (seeksegment.format != seek_format) {
1695 /* OK, here's where we give the subclass a chance to convert the relative
1696 * seek into an absolute one in the processing format. We set up any
1697 * absolute seek above, before taking the stream lock. */
1698 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1699 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1704 /* The seek format matches our processing format, no need to ask the
1705 * the subclass to configure the segment. */
1706 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1707 start_type, start, stop_type, stop, &update);
1710 /* Else, no seek event passed, so we're just (re)starting the
1715 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1716 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1717 seeksegment.start, seeksegment.stop, seeksegment.position);
1719 /* do the seek, segment.position contains the new position. */
1720 res = gst_base_src_do_seek (src, &seeksegment);
1723 /* and prepare to continue streaming */
1725 tevent = gst_event_new_flush_stop (TRUE);
1726 gst_event_set_seqnum (tevent, seqnum);
1727 /* send flush stop, peer will accept data and events again. We
1728 * are not yet providing data as we still have the STREAM_LOCK. */
1729 gst_pad_push_event (src->srcpad, tevent);
1732 /* The subclass must have converted the segment to the processing format
1734 if (res && seeksegment.format != dest_format) {
1735 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1736 "in the correct format. Aborting seek.");
1740 /* if the seek was successful, we update our real segment and push
1741 * out the new segment. */
1743 GST_OBJECT_LOCK (src);
1744 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1745 GST_OBJECT_UNLOCK (src);
1747 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1748 GstMessage *message;
1750 message = gst_message_new_segment_start (GST_OBJECT (src),
1751 seeksegment.format, seeksegment.position);
1752 gst_message_set_seqnum (message, seqnum);
1754 gst_element_post_message (GST_ELEMENT (src), message);
1757 src->priv->segment_pending = TRUE;
1758 src->priv->segment_seqnum = seqnum;
1761 src->priv->discont = TRUE;
1762 src->running = TRUE;
1763 /* and restart the task in case it got paused explicitly or by
1764 * the FLUSH_START event we pushed out. */
1765 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1770 /* and release the lock again so we can continue streaming */
1771 GST_PAD_STREAM_UNLOCK (src->srcpad);
1777 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1782 /* all events send to this element directly. This is mainly done from the
1786 gst_base_src_send_event (GstElement * element, GstEvent * event)
1789 gboolean result = FALSE;
1790 GstBaseSrcClass *bclass;
1792 src = GST_BASE_SRC (element);
1793 bclass = GST_BASE_SRC_GET_CLASS (src);
1795 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1797 switch (GST_EVENT_TYPE (event)) {
1798 /* bidirectional events */
1799 case GST_EVENT_FLUSH_START:
1800 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1802 result = gst_pad_push_event (src->srcpad, event);
1803 gst_base_src_set_flushing (src, TRUE);
1806 case GST_EVENT_FLUSH_STOP:
1810 GST_PAD_STREAM_LOCK (src->srcpad);
1811 gst_base_src_set_flushing (src, FALSE);
1813 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1814 result = gst_pad_push_event (src->srcpad, event);
1816 /* For external flush, restart the task .. */
1817 GST_LIVE_LOCK (src);
1818 src->priv->segment_pending = TRUE;
1820 GST_OBJECT_LOCK (src->srcpad);
1821 start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1822 GST_OBJECT_UNLOCK (src->srcpad);
1824 /* ... and for live sources, only if in playing state */
1826 if (!src->live_running)
1831 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1834 GST_LIVE_UNLOCK (src);
1835 GST_PAD_STREAM_UNLOCK (src->srcpad);
1841 /* downstream serialized events */
1846 /* queue EOS and make sure the task or pull function performs the EOS
1849 * For push mode, This will be done in 3 steps. It is required to not
1850 * block here as gst_element_send_event() will hold the STATE_LOCK, hence
1851 * blocking would prevent asynchronous state change to complete.
1853 * 1. We stop the streaming thread
1854 * 2. We set the pending eos
1855 * 3. We start the streaming thread again, so it is performed
1858 * For pull mode, we simply mark the pending EOS without flushing.
1861 GST_OBJECT_LOCK (src->srcpad);
1862 push_mode = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1863 GST_OBJECT_UNLOCK (src->srcpad);
1866 gst_base_src_set_flushing (src, TRUE);
1868 GST_PAD_STREAM_LOCK (src->srcpad);
1869 gst_base_src_set_flushing (src, FALSE);
1871 GST_OBJECT_LOCK (src);
1872 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1873 if (src->priv->pending_eos)
1874 gst_event_unref (src->priv->pending_eos);
1875 src->priv->pending_eos = event;
1876 GST_OBJECT_UNLOCK (src);
1878 GST_DEBUG_OBJECT (src,
1879 "EOS marked, start task for asynchronous handling");
1880 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1883 GST_PAD_STREAM_UNLOCK (src->srcpad);
1885 /* In pull mode, we need not to return flushing to downstream, though
1886 * the stream lock is not kept after getrange was unblocked */
1887 GST_OBJECT_LOCK (src);
1888 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1889 if (src->priv->pending_eos)
1890 gst_event_unref (src->priv->pending_eos);
1891 src->priv->pending_eos = event;
1892 GST_OBJECT_UNLOCK (src);
1894 gst_base_src_set_pool_flushing (src, TRUE);
1896 bclass->unlock (src);
1898 GST_PAD_STREAM_LOCK (src->srcpad);
1899 if (bclass->unlock_stop)
1900 bclass->unlock_stop (src);
1901 gst_base_src_set_pool_flushing (src, TRUE);
1902 GST_PAD_STREAM_UNLOCK (src->srcpad);
1910 case GST_EVENT_SEGMENT:
1911 /* sending random SEGMENT downstream can break sync. */
1914 case GST_EVENT_SINK_MESSAGE:
1915 case GST_EVENT_CUSTOM_DOWNSTREAM:
1916 case GST_EVENT_CUSTOM_BOTH:
1917 case GST_EVENT_PROTECTION:
1918 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH, PROTECTION in the dataflow */
1919 GST_OBJECT_LOCK (src);
1920 src->priv->pending_events =
1921 g_list_append (src->priv->pending_events, event);
1922 g_atomic_int_set (&src->priv->have_events, TRUE);
1923 GST_OBJECT_UNLOCK (src);
1927 case GST_EVENT_BUFFERSIZE:
1928 /* does not seem to make much sense currently */
1931 /* upstream events */
1933 /* elements should override send_event and do something */
1935 case GST_EVENT_SEEK:
1939 GST_OBJECT_LOCK (src->srcpad);
1940 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1942 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1943 GST_OBJECT_UNLOCK (src->srcpad);
1946 GST_DEBUG_OBJECT (src, "performing seek");
1947 /* when we are running in push mode, we can execute the
1948 * seek right now. */
1949 result = gst_base_src_perform_seek (src, event, TRUE);
1953 /* else we store the event and execute the seek when we
1955 GST_OBJECT_LOCK (src);
1956 GST_DEBUG_OBJECT (src, "queueing seek");
1957 event_p = &src->pending_seek;
1958 gst_event_replace ((GstEvent **) event_p, event);
1959 GST_OBJECT_UNLOCK (src);
1960 /* assume the seek will work */
1965 case GST_EVENT_NAVIGATION:
1966 /* could make sense for elements that do something with navigation events
1967 * but then they would need to override the send_event function */
1969 case GST_EVENT_LATENCY:
1970 /* does not seem to make sense currently */
1974 case GST_EVENT_CUSTOM_UPSTREAM:
1975 /* override send_event if you want this */
1977 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1978 case GST_EVENT_CUSTOM_BOTH_OOB:
1979 /* insert a random custom event into the pipeline */
1980 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1981 result = gst_pad_push_event (src->srcpad, event);
1982 /* we gave away the ref to the event in the push */
1989 /* if we still have a ref to the event, unref it now */
1991 gst_event_unref (event);
1998 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1999 GST_OBJECT_UNLOCK (src->srcpad);
2006 gst_base_src_seekable (GstBaseSrc * src)
2008 GstBaseSrcClass *bclass;
2009 bclass = GST_BASE_SRC_GET_CLASS (src);
2010 if (bclass->is_seekable)
2011 return bclass->is_seekable (src);
2017 gst_base_src_update_qos (GstBaseSrc * src,
2018 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
2020 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
2021 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2022 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
2024 GST_OBJECT_LOCK (src);
2025 src->priv->proportion = proportion;
2026 src->priv->earliest_time = timestamp + diff;
2027 GST_OBJECT_UNLOCK (src);
2032 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
2036 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
2038 switch (GST_EVENT_TYPE (event)) {
2039 case GST_EVENT_SEEK:
2040 /* is normally called when in push mode */
2041 if (!gst_base_src_seekable (src))
2044 result = gst_base_src_perform_seek (src, event, TRUE);
2046 case GST_EVENT_FLUSH_START:
2047 /* cancel any blocking getrange, is normally called
2048 * when in pull mode. */
2049 result = gst_base_src_set_flushing (src, TRUE);
2051 case GST_EVENT_FLUSH_STOP:
2052 result = gst_base_src_set_flushing (src, FALSE);
2057 GstClockTimeDiff diff;
2058 GstClockTime timestamp;
2060 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
2061 gst_base_src_update_qos (src, proportion, diff, timestamp);
2065 case GST_EVENT_RECONFIGURE:
2068 case GST_EVENT_LATENCY:
2080 GST_DEBUG_OBJECT (src, "is not seekable");
2086 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2089 GstBaseSrcClass *bclass;
2090 gboolean result = FALSE;
2092 src = GST_BASE_SRC (parent);
2093 bclass = GST_BASE_SRC_GET_CLASS (src);
2095 if (bclass->event) {
2096 if (!(result = bclass->event (src, event)))
2097 goto subclass_failed;
2101 gst_event_unref (event);
2108 GST_DEBUG_OBJECT (src, "subclass refused event");
2114 gst_base_src_set_property (GObject * object, guint prop_id,
2115 const GValue * value, GParamSpec * pspec)
2119 src = GST_BASE_SRC (object);
2122 case PROP_BLOCKSIZE:
2123 gst_base_src_set_blocksize (src, g_value_get_uint (value));
2125 case PROP_NUM_BUFFERS:
2126 src->num_buffers = g_value_get_int (value);
2128 #ifndef GST_REMOVE_DEPRECATED
2130 src->typefind = g_value_get_boolean (value);
2133 case PROP_DO_TIMESTAMP:
2134 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2143 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2148 src = GST_BASE_SRC (object);
2151 case PROP_BLOCKSIZE:
2152 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2154 case PROP_NUM_BUFFERS:
2155 g_value_set_int (value, src->num_buffers);
2157 #ifndef GST_REMOVE_DEPRECATED
2159 g_value_set_boolean (value, src->typefind);
2162 case PROP_DO_TIMESTAMP:
2163 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2171 /* with STREAM_LOCK and LOCK */
2172 static GstClockReturn
2173 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2178 id = gst_clock_new_single_shot_id (clock, time);
2180 basesrc->clock_id = id;
2181 /* release the live lock while waiting */
2182 GST_LIVE_UNLOCK (basesrc);
2184 ret = gst_clock_id_wait (id, NULL);
2186 GST_LIVE_LOCK (basesrc);
2187 gst_clock_id_unref (id);
2188 basesrc->clock_id = NULL;
2193 /* perform synchronisation on a buffer.
2196 static GstClockReturn
2197 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2199 GstClockReturn result;
2200 GstClockTime start, end;
2201 GstBaseSrcClass *bclass;
2202 GstClockTime base_time;
2204 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2205 gboolean do_timestamp, first, pseudo_live, is_live;
2207 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2210 if (bclass->get_times)
2211 bclass->get_times (basesrc, buffer, &start, &end);
2213 /* get buffer timestamp */
2214 dts = GST_BUFFER_DTS (buffer);
2215 pts = GST_BUFFER_PTS (buffer);
2217 if (GST_CLOCK_TIME_IS_VALID (dts))
2222 /* grab the lock to prepare for clocking and calculate the startup
2224 GST_OBJECT_LOCK (basesrc);
2226 is_live = basesrc->is_live;
2227 /* if we are asked to sync against the clock we are a pseudo live element */
2228 pseudo_live = (start != -1 && is_live);
2229 /* check for the first buffer */
2230 first = (basesrc->priv->latency == -1);
2232 if (timestamp != -1 && pseudo_live) {
2233 GstClockTime latency;
2235 /* we have a timestamp and a sync time, latency is the diff */
2236 if (timestamp <= start)
2237 latency = start - timestamp;
2242 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2243 GST_TIME_ARGS (latency));
2244 /* first time we calculate latency, just configure */
2245 basesrc->priv->latency = latency;
2247 if (basesrc->priv->latency != latency) {
2248 /* we have a new latency, FIXME post latency message */
2249 basesrc->priv->latency = latency;
2250 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2251 GST_TIME_ARGS (latency));
2255 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2256 is_live, start != -1);
2257 basesrc->priv->latency = 0;
2260 /* get clock, if no clock, we can't sync or do timestamps */
2261 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2264 gst_object_ref (clock);
2266 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2268 do_timestamp = basesrc->priv->do_timestamp;
2269 GST_OBJECT_UNLOCK (basesrc);
2271 /* first buffer, calculate the timestamp offset */
2273 GstClockTime running_time;
2275 now = gst_clock_get_time (clock);
2276 running_time = now - base_time;
2278 GST_LOG_OBJECT (basesrc,
2279 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2280 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2281 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2283 if (pseudo_live && timestamp != -1) {
2284 /* live source and we need to sync, add startup latency to all timestamps
2285 * to get the real running_time. Live sources should always timestamp
2286 * according to the current running time. */
2287 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2289 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2290 GST_TIME_ARGS (basesrc->priv->ts_offset));
2292 basesrc->priv->ts_offset = 0;
2293 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2296 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2299 } else if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2300 if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2301 dts = basesrc->segment.start;
2306 GST_BUFFER_DTS (buffer) = dts;
2308 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2309 GST_TIME_ARGS (dts));
2312 /* not the first buffer, the timestamp is the diff between the clock and
2314 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2315 now = gst_clock_get_time (clock);
2317 dts = now - base_time;
2318 GST_BUFFER_DTS (buffer) = dts;
2320 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2321 GST_TIME_ARGS (dts));
2324 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2325 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2328 GST_BUFFER_PTS (buffer) = dts;
2330 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2331 GST_TIME_ARGS (pts));
2334 /* if we don't have a buffer timestamp, we don't sync */
2335 if (!GST_CLOCK_TIME_IS_VALID (start))
2339 /* for pseudo live sources, add our ts_offset to the timestamp */
2340 if (GST_CLOCK_TIME_IS_VALID (pts))
2341 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2342 if (GST_CLOCK_TIME_IS_VALID (dts))
2343 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2344 start += basesrc->priv->ts_offset;
2347 GST_LOG_OBJECT (basesrc,
2348 "waiting for clock, base time %" GST_TIME_FORMAT
2349 ", stream_start %" GST_TIME_FORMAT,
2350 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2352 result = gst_base_src_wait (basesrc, clock, start + base_time);
2354 gst_object_unref (clock);
2356 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2363 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2364 GST_OBJECT_UNLOCK (basesrc);
2365 return GST_CLOCK_OK;
2369 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2370 gst_object_unref (clock);
2371 return GST_CLOCK_OK;
2375 /* Called with STREAM_LOCK and LIVE_LOCK */
2377 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2380 guint64 size, maxsize;
2381 GstBaseSrcClass *bclass;
2384 /* only operate if we are working with bytes */
2385 if (src->segment.format != GST_FORMAT_BYTES)
2388 bclass = GST_BASE_SRC_GET_CLASS (src);
2390 stop = src->segment.stop;
2391 /* get total file size */
2392 size = src->segment.duration;
2394 /* when not doing automatic EOS, just use the stop position. We don't use
2395 * the size to check for EOS */
2396 if (!g_atomic_int_get (&src->priv->automatic_eos))
2398 /* Otherwise, the max amount of bytes to read is the total
2399 * size or up to the segment.stop if present. */
2400 else if (stop != -1)
2401 maxsize = size != -1 ? MIN (size, stop) : stop;
2405 GST_DEBUG_OBJECT (src,
2406 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2407 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2408 *length, size, stop, maxsize);
2410 /* check size if we have one */
2411 if (maxsize != -1) {
2412 /* if we run past the end, check if the file became bigger and
2413 * retry. Mind wrap when checking. */
2414 if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2415 /* see if length of the file changed */
2416 if (bclass->get_size)
2417 if (!bclass->get_size (src, &size))
2420 /* when not doing automatic EOS, just use the stop position. We don't use
2421 * the size to check for EOS */
2422 if (!g_atomic_int_get (&src->priv->automatic_eos))
2424 /* Otherwise, the max amount of bytes to read is the total
2425 * size or up to the segment.stop if present. */
2426 else if (stop != -1)
2427 maxsize = size != -1 ? MIN (size, stop) : stop;
2431 if (maxsize != -1) {
2432 /* if we are at or past the end, EOS */
2433 if (G_UNLIKELY (offset >= maxsize))
2434 goto unexpected_length;
2436 /* else we can clip to the end */
2437 if (G_UNLIKELY (offset + *length >= maxsize))
2438 *length = maxsize - offset;
2443 /* keep track of current duration. segment is in bytes, we checked
2445 GST_OBJECT_LOCK (src);
2446 src->segment.duration = size;
2447 GST_OBJECT_UNLOCK (src);
2454 GST_WARNING_OBJECT (src, "processing at or past EOS");
2459 /* must be called with LIVE_LOCK */
2460 static GstFlowReturn
2461 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2465 GstBaseSrcClass *bclass;
2466 GstClockReturn status;
2469 gboolean own_res_buf;
2471 bclass = GST_BASE_SRC_GET_CLASS (src);
2475 if (G_UNLIKELY (!src->live_running)) {
2476 ret = gst_base_src_wait_playing_unlocked (src);
2477 if (ret != GST_FLOW_OK)
2482 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2483 && !GST_BASE_SRC_IS_STARTING (src)))
2486 if (G_UNLIKELY (!bclass->create))
2489 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2490 goto unexpected_length;
2492 /* track position */
2493 GST_OBJECT_LOCK (src);
2494 if (src->segment.format == GST_FORMAT_BYTES)
2495 src->segment.position = offset;
2496 GST_OBJECT_UNLOCK (src);
2498 /* normally we don't count buffers */
2499 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2500 if (src->num_buffers_left == 0)
2501 goto reached_num_buffers;
2503 src->num_buffers_left--;
2506 /* don't enter the create function if a pending EOS event was set. For the
2507 * logic of the has_pending_eos, check the event function of this class. */
2508 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2509 src->priv->forced_eos = TRUE;
2513 GST_DEBUG_OBJECT (src,
2514 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2515 G_GINT64_FORMAT, offset, length, src->segment.time);
2517 res_buf = in_buf = *buf;
2518 own_res_buf = (*buf == NULL);
2520 GST_LIVE_UNLOCK (src);
2521 ret = bclass->create (src, offset, length, &res_buf);
2522 GST_LIVE_LOCK (src);
2524 /* As we released the LIVE_LOCK, the state may have changed */
2526 if (G_UNLIKELY (!src->live_running)) {
2527 GstFlowReturn wait_ret;
2528 wait_ret = gst_base_src_wait_playing_unlocked (src);
2529 if (wait_ret != GST_FLOW_OK) {
2530 if (ret == GST_FLOW_OK && own_res_buf)
2531 gst_buffer_unref (res_buf);
2538 /* The create function could be unlocked because we have a pending EOS. It's
2539 * possible that we have a valid buffer from create that we need to
2540 * discard when the create function returned _OK. */
2541 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2542 if (ret == GST_FLOW_OK) {
2544 gst_buffer_unref (res_buf);
2546 src->priv->forced_eos = TRUE;
2550 if (G_UNLIKELY (ret != GST_FLOW_OK))
2553 /* fallback in case the create function didn't fill a provided buffer */
2554 if (in_buf != NULL && res_buf != in_buf) {
2558 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2559 "fill the provided buffer, copying");
2561 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2564 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2565 gst_buffer_unmap (in_buf, &info);
2566 gst_buffer_set_size (in_buf, copied_size);
2568 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2570 gst_buffer_unref (res_buf);
2574 if (res_buf == NULL) {
2575 GstBufferList *pending_list = src->priv->pending_bufferlist;
2577 if (pending_list == NULL || gst_buffer_list_length (pending_list) == 0)
2580 res_buf = gst_buffer_list_get_writable (pending_list, 0);
2581 own_res_buf = FALSE;
2584 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2585 if (offset == 0 && src->segment.time == 0
2586 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2587 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2588 res_buf = gst_buffer_make_writable (res_buf);
2589 GST_BUFFER_DTS (res_buf) = 0;
2592 /* now sync before pushing the buffer */
2593 status = gst_base_src_do_sync (src, res_buf);
2595 /* waiting for the clock could have made us flushing */
2596 if (G_UNLIKELY (src->priv->flushing))
2600 case GST_CLOCK_EARLY:
2601 /* the buffer is too late. We currently don't drop the buffer. */
2602 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2605 /* buffer synchronised properly */
2606 GST_DEBUG_OBJECT (src, "buffer ok");
2608 case GST_CLOCK_UNSCHEDULED:
2609 /* this case is triggered when we were waiting for the clock and
2610 * it got unlocked because we did a state change. In any case, get rid of
2613 gst_buffer_unref (res_buf);
2615 if (!src->live_running) {
2616 /* We return FLUSHING when we are not running to stop the dataflow also
2617 * get rid of the produced buffer. */
2618 GST_DEBUG_OBJECT (src,
2619 "clock was unscheduled (%d), returning FLUSHING", status);
2620 ret = GST_FLOW_FLUSHING;
2622 /* If we are running when this happens, we quickly switched between
2623 * pause and playing. We try to produce a new buffer */
2624 GST_DEBUG_OBJECT (src,
2625 "clock was unscheduled (%d), but we are running", status);
2630 /* all other result values are unexpected and errors */
2631 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2632 (_("Internal clock error.")),
2633 ("clock returned unexpected return value %d", status));
2635 gst_buffer_unref (res_buf);
2636 ret = GST_FLOW_ERROR;
2639 if (G_LIKELY (ret == GST_FLOW_OK))
2647 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2648 gst_flow_get_name (ret));
2653 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2654 gst_flow_get_name (ret));
2659 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2660 (_("Failed to map buffer.")),
2661 ("failed to map result buffer in WRITE mode"));
2663 gst_buffer_unref (res_buf);
2664 return GST_FLOW_ERROR;
2668 GST_DEBUG_OBJECT (src, "getrange but not started");
2669 return GST_FLOW_FLUSHING;
2673 GST_DEBUG_OBJECT (src, "no create function");
2674 return GST_FLOW_NOT_SUPPORTED;
2678 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2679 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2680 return GST_FLOW_EOS;
2682 reached_num_buffers:
2684 GST_DEBUG_OBJECT (src, "sent all buffers");
2685 return GST_FLOW_EOS;
2689 GST_DEBUG_OBJECT (src, "we are flushing");
2691 gst_buffer_unref (res_buf);
2692 return GST_FLOW_FLUSHING;
2696 GST_DEBUG_OBJECT (src, "we are EOS");
2697 return GST_FLOW_EOS;
2701 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2702 (_("Internal data flow error.")),
2703 ("Subclass %s neither returned a buffer nor submitted a buffer list "
2704 "from its create function", G_OBJECT_TYPE_NAME (src)));
2705 return GST_FLOW_ERROR;
2709 static GstFlowReturn
2710 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2711 guint length, GstBuffer ** buf)
2716 src = GST_BASE_SRC_CAST (parent);
2718 GST_LIVE_LOCK (src);
2719 if (G_UNLIKELY (src->priv->flushing))
2722 res = gst_base_src_get_range (src, offset, length, buf);
2725 GST_LIVE_UNLOCK (src);
2732 GST_DEBUG_OBJECT (src, "we are flushing");
2733 res = GST_FLOW_FLUSHING;
2739 gst_base_src_is_random_access (GstBaseSrc * src)
2741 /* we need to start the basesrc to check random access */
2742 if (!GST_BASE_SRC_IS_STARTED (src)) {
2743 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2744 if (G_LIKELY (gst_base_src_start (src))) {
2745 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2747 gst_base_src_stop (src);
2751 return src->random_access;
2756 GST_DEBUG_OBJECT (src, "failed to start");
2761 /* Called with STREAM_LOCK */
2763 gst_base_src_loop (GstPad * pad)
2766 GstBuffer *buf = NULL;
2771 GList *pending_events = NULL, *tmp;
2775 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2777 /* Just leave immediately if we're flushing */
2778 GST_LIVE_LOCK (src);
2779 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2781 GST_LIVE_UNLOCK (src);
2783 /* Just return if EOS is pushed again, as the app might be unaware that an
2784 * EOS have been sent already */
2785 if (GST_PAD_IS_EOS (pad)) {
2786 GST_DEBUG_OBJECT (src, "Pad is marked as EOS, pause the task");
2787 gst_pad_pause_task (pad);
2791 gst_base_src_send_stream_start (src);
2793 /* The stream-start event could've caused something to flush us */
2794 GST_LIVE_LOCK (src);
2795 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2797 GST_LIVE_UNLOCK (src);
2799 /* check if we need to renegotiate */
2800 if (gst_pad_check_reconfigure (pad)) {
2801 if (!gst_base_src_negotiate (src)) {
2802 gst_pad_mark_reconfigure (pad);
2803 if (GST_PAD_IS_FLUSHING (pad)) {
2804 GST_LIVE_LOCK (src);
2807 goto negotiate_failed;
2812 GST_LIVE_LOCK (src);
2814 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2817 blocksize = src->blocksize;
2819 /* if we operate in bytes, we can calculate an offset */
2820 if (src->segment.format == GST_FORMAT_BYTES) {
2821 position = src->segment.position;
2822 /* for negative rates, start with subtracting the blocksize */
2823 if (src->segment.rate < 0.0) {
2824 /* we cannot go below segment.start */
2825 if (position > src->segment.start + blocksize)
2826 position -= blocksize;
2828 /* last block, remainder up to segment.start */
2829 blocksize = position - src->segment.start;
2830 position = src->segment.start;
2836 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2837 GST_TIME_ARGS (position), blocksize);
2839 /* clean up just in case we got interrupted or so last time round */
2840 if (src->priv->pending_bufferlist != NULL) {
2841 gst_buffer_list_unref (src->priv->pending_bufferlist);
2842 src->priv->pending_bufferlist = NULL;
2845 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2846 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2847 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2848 gst_flow_get_name (ret));
2849 GST_LIVE_UNLOCK (src);
2853 /* Note: at this point buf might be a single buf returned which we own or
2854 * the first buf of a pending buffer list submitted via submit_buffer_list(),
2855 * in which case the buffer is owned by the pending buffer list and not us. */
2856 g_assert (buf != NULL);
2858 /* push events to close/start our segment before we push the buffer. */
2859 if (G_UNLIKELY (src->priv->segment_pending)) {
2860 GstEvent *seg_event = gst_event_new_segment (&src->segment);
2862 gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2863 src->priv->segment_seqnum = gst_util_seqnum_next ();
2864 gst_pad_push_event (pad, seg_event);
2865 src->priv->segment_pending = FALSE;
2868 if (g_atomic_int_get (&src->priv->have_events)) {
2869 GST_OBJECT_LOCK (src);
2870 /* take the events */
2871 pending_events = src->priv->pending_events;
2872 src->priv->pending_events = NULL;
2873 g_atomic_int_set (&src->priv->have_events, FALSE);
2874 GST_OBJECT_UNLOCK (src);
2877 /* Push out pending events if any */
2878 if (G_UNLIKELY (pending_events != NULL)) {
2879 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2880 GstEvent *ev = (GstEvent *) tmp->data;
2881 gst_pad_push_event (pad, ev);
2883 g_list_free (pending_events);
2886 /* figure out the new position */
2887 switch (src->segment.format) {
2888 case GST_FORMAT_BYTES:
2890 guint bufsize = gst_buffer_get_size (buf);
2892 /* we subtracted above for negative rates */
2893 if (src->segment.rate >= 0.0)
2894 position += bufsize;
2897 case GST_FORMAT_TIME:
2899 GstClockTime start, duration;
2901 start = GST_BUFFER_TIMESTAMP (buf);
2902 duration = GST_BUFFER_DURATION (buf);
2904 if (GST_CLOCK_TIME_IS_VALID (start))
2907 position = src->segment.position;
2909 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2910 if (src->segment.rate >= 0.0)
2911 position += duration;
2912 else if (position > duration)
2913 position -= duration;
2919 case GST_FORMAT_DEFAULT:
2920 if (src->segment.rate >= 0.0)
2921 position = GST_BUFFER_OFFSET_END (buf);
2923 position = GST_BUFFER_OFFSET (buf);
2929 if (position != -1) {
2930 if (src->segment.rate >= 0.0) {
2931 /* positive rate, check if we reached the stop */
2932 if (src->segment.stop != -1) {
2933 if (position >= src->segment.stop) {
2935 position = src->segment.stop;
2939 /* negative rate, check if we reached the start. start is always set to
2940 * something different from -1 */
2941 if (position <= src->segment.start) {
2943 position = src->segment.start;
2945 /* when going reverse, all buffers are DISCONT */
2946 src->priv->discont = TRUE;
2948 GST_OBJECT_LOCK (src);
2949 src->segment.position = position;
2950 GST_OBJECT_UNLOCK (src);
2953 if (G_UNLIKELY (src->priv->discont)) {
2954 GST_INFO_OBJECT (src, "marking pending DISCONT");
2955 buf = gst_buffer_make_writable (buf);
2956 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2957 src->priv->discont = FALSE;
2959 GST_LIVE_UNLOCK (src);
2961 /* push buffer or buffer list */
2962 if (src->priv->pending_bufferlist != NULL) {
2963 ret = gst_pad_push_list (pad, src->priv->pending_bufferlist);
2964 src->priv->pending_bufferlist = NULL;
2966 ret = gst_pad_push (pad, buf);
2969 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2970 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2971 goto not_negotiated;
2973 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2974 gst_flow_get_name (ret));
2978 /* Segment pending means that a new segment was configured
2979 * during this loop run */
2980 if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2981 GST_INFO_OBJECT (src, "pausing after end of segment");
2992 if (gst_pad_needs_reconfigure (pad)) {
2993 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2996 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2997 * a pending negotiation request on our srcpad */
3001 GST_DEBUG_OBJECT (src, "Not negotiated");
3002 ret = GST_FLOW_NOT_NEGOTIATED;
3007 GST_DEBUG_OBJECT (src, "we are flushing");
3008 GST_LIVE_UNLOCK (src);
3009 ret = GST_FLOW_FLUSHING;
3014 const gchar *reason = gst_flow_get_name (ret);
3017 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
3018 src->running = FALSE;
3019 gst_pad_pause_task (pad);
3020 if (ret == GST_FLOW_EOS) {
3021 gboolean flag_segment;
3025 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
3026 format = src->segment.format;
3027 position = src->segment.position;
3029 /* perform EOS logic */
3030 if (src->priv->forced_eos) {
3031 g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
3032 GST_OBJECT_LOCK (src);
3033 event = src->priv->pending_eos;
3034 src->priv->pending_eos = NULL;
3035 GST_OBJECT_UNLOCK (src);
3037 } else if (flag_segment) {
3038 GstMessage *message;
3040 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
3042 gst_message_set_seqnum (message, src->priv->seqnum);
3043 gst_element_post_message (GST_ELEMENT_CAST (src), message);
3044 event = gst_event_new_segment_done (format, position);
3045 gst_event_set_seqnum (event, src->priv->seqnum);
3048 event = gst_event_new_eos ();
3049 gst_event_set_seqnum (event, src->priv->seqnum);
3052 gst_pad_push_event (pad, event);
3053 src->priv->forced_eos = FALSE;
3055 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
3056 event = gst_event_new_eos ();
3057 gst_event_set_seqnum (event, src->priv->seqnum);
3058 /* for fatal errors we post an error message, post the error
3059 * first so the app knows about the error first.
3060 * Also don't do this for FLUSHING because it happens
3061 * due to flushing and posting an error message because of
3062 * that is the wrong thing to do, e.g. when we're doing
3063 * a flushing seek. */
3064 GST_ELEMENT_FLOW_ERROR (src, ret);
3065 gst_pad_push_event (pad, event);
3072 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
3073 GstAllocator * allocator, GstAllocationParams * params)
3075 GstAllocator *oldalloc;
3076 GstBufferPool *oldpool;
3077 GstBaseSrcPrivate *priv = basesrc->priv;
3080 GST_DEBUG_OBJECT (basesrc, "activate pool");
3081 if (!gst_buffer_pool_set_active (pool, TRUE))
3082 goto activate_failed;
3085 GST_OBJECT_LOCK (basesrc);
3086 oldpool = priv->pool;
3089 oldalloc = priv->allocator;
3090 priv->allocator = allocator;
3093 gst_object_ref (priv->pool);
3094 if (priv->allocator)
3095 gst_object_ref (priv->allocator);
3098 priv->params = *params;
3100 gst_allocation_params_init (&priv->params);
3101 GST_OBJECT_UNLOCK (basesrc);
3104 /* only deactivate if the pool is not the one we're using */
3105 if (oldpool != pool) {
3106 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
3107 gst_buffer_pool_set_active (oldpool, FALSE);
3109 gst_object_unref (oldpool);
3112 gst_object_unref (oldalloc);
3119 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
3125 gst_base_src_set_pool_flushing (GstBaseSrc * basesrc, gboolean flushing)
3127 GstBaseSrcPrivate *priv = basesrc->priv;
3128 GstBufferPool *pool;
3130 GST_OBJECT_LOCK (basesrc);
3131 if ((pool = priv->pool))
3132 pool = gst_object_ref (pool);
3133 GST_OBJECT_UNLOCK (basesrc);
3136 gst_buffer_pool_set_flushing (pool, flushing);
3137 gst_object_unref (pool);
3143 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3146 GstBufferPool *pool;
3147 guint size, min, max;
3148 GstAllocator *allocator;
3149 GstAllocationParams params;
3150 GstStructure *config;
3151 gboolean update_allocator;
3153 gst_query_parse_allocation (query, &outcaps, NULL);
3155 /* we got configuration from our peer or the decide_allocation method,
3157 if (gst_query_get_n_allocation_params (query) > 0) {
3158 /* try the allocator */
3159 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3160 update_allocator = TRUE;
3163 gst_allocation_params_init (¶ms);
3164 update_allocator = FALSE;
3167 if (gst_query_get_n_allocation_pools (query) > 0) {
3168 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3171 /* no pool, we can make our own */
3172 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3173 pool = gst_buffer_pool_new ();
3177 size = min = max = 0;
3182 config = gst_buffer_pool_get_config (pool);
3183 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3184 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3186 /* buffer pool may have to do some changes */
3187 if (!gst_buffer_pool_set_config (pool, config)) {
3188 config = gst_buffer_pool_get_config (pool);
3190 /* If change are not acceptable, fallback to generic pool */
3191 if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3193 GST_DEBUG_OBJECT (basesrc, "unsupported pool, making new pool");
3195 gst_object_unref (pool);
3196 pool = gst_buffer_pool_new ();
3197 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3198 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3201 if (!gst_buffer_pool_set_config (pool, config))
3206 if (update_allocator)
3207 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
3209 gst_query_add_allocation_param (query, allocator, ¶ms);
3211 gst_object_unref (allocator);
3214 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3215 gst_object_unref (pool);
3221 GST_ELEMENT_ERROR (basesrc, RESOURCE, SETTINGS,
3222 ("Failed to configure the buffer pool"),
3223 ("Configuration is most likely invalid, please report this issue."));
3224 gst_object_unref (pool);
3229 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3231 GstBaseSrcClass *bclass;
3232 gboolean result = TRUE;
3234 GstBufferPool *pool = NULL;
3235 GstAllocator *allocator = NULL;
3236 GstAllocationParams params;
3238 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3240 /* make query and let peer pad answer, we don't really care if it worked or
3241 * not, if it failed, the allocation query would contain defaults and the
3242 * subclass would then set better values if needed */
3243 query = gst_query_new_allocation (caps, TRUE);
3244 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3245 /* not a problem, just debug a little */
3246 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3249 g_assert (bclass->decide_allocation != NULL);
3250 result = bclass->decide_allocation (basesrc, query);
3252 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3256 goto no_decide_allocation;
3258 /* we got configuration from our peer or the decide_allocation method,
3260 if (gst_query_get_n_allocation_params (query) > 0) {
3261 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3264 gst_allocation_params_init (¶ms);
3267 if (gst_query_get_n_allocation_pools (query) > 0)
3268 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3270 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
3273 gst_object_unref (allocator);
3275 gst_object_unref (pool);
3277 gst_query_unref (query);
3282 no_decide_allocation:
3284 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3285 gst_query_unref (query);
3291 /* default negotiation code.
3293 * Take intersection between src and sink pads, take first
3297 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3300 GstCaps *caps = NULL;
3301 GstCaps *peercaps = NULL;
3302 gboolean result = FALSE;
3304 /* first see what is possible on our source pad */
3305 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3306 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3307 /* nothing or anything is allowed, we're done */
3308 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3309 goto no_nego_needed;
3311 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3314 /* get the peer caps */
3315 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3316 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3318 /* The result is already a subset of our caps */
3320 gst_caps_unref (thiscaps);
3322 /* no peer, work with our own caps then */
3325 if (caps && !gst_caps_is_empty (caps)) {
3327 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3328 if (gst_caps_is_any (caps)) {
3329 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3330 /* hmm, still anything, so element can do anything and
3331 * nego is not needed */
3334 caps = gst_base_src_fixate (basesrc, caps);
3335 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3336 if (gst_caps_is_fixed (caps)) {
3337 /* yay, fixed caps, use those then, it's possible that the subclass does
3338 * not accept this caps after all and we have to fail. */
3339 result = gst_base_src_set_caps (basesrc, caps);
3342 gst_caps_unref (caps);
3345 gst_caps_unref (caps);
3346 GST_DEBUG_OBJECT (basesrc, "no common caps");
3352 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3354 gst_caps_unref (thiscaps);
3359 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3360 ("No supported formats found"),
3361 ("This element did not produce valid caps"));
3363 gst_caps_unref (thiscaps);
3369 gst_base_src_negotiate (GstBaseSrc * basesrc)
3371 GstBaseSrcClass *bclass;
3374 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3376 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3378 if (G_LIKELY (bclass->negotiate))
3379 result = bclass->negotiate (basesrc);
3383 if (G_LIKELY (result)) {
3386 caps = gst_pad_get_current_caps (basesrc->srcpad);
3388 result = gst_base_src_prepare_allocation (basesrc, caps);
3391 gst_caps_unref (caps);
3397 gst_base_src_start (GstBaseSrc * basesrc)
3399 GstBaseSrcClass *bclass;
3402 GST_LIVE_LOCK (basesrc);
3404 GST_OBJECT_LOCK (basesrc);
3405 if (GST_BASE_SRC_IS_STARTING (basesrc))
3407 if (GST_BASE_SRC_IS_STARTED (basesrc))
3410 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3411 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3412 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3413 GST_OBJECT_UNLOCK (basesrc);
3415 basesrc->num_buffers_left = basesrc->num_buffers;
3416 basesrc->running = FALSE;
3417 basesrc->priv->segment_pending = FALSE;
3418 basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3419 basesrc->priv->forced_eos = FALSE;
3420 GST_LIVE_UNLOCK (basesrc);
3422 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3424 result = bclass->start (basesrc);
3429 goto could_not_start;
3431 if (!gst_base_src_is_async (basesrc)) {
3432 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3433 /* not really waiting here, we call this to get the result
3434 * from the start_complete call */
3435 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3443 GST_DEBUG_OBJECT (basesrc, "was starting");
3444 GST_OBJECT_UNLOCK (basesrc);
3445 GST_LIVE_UNLOCK (basesrc);
3450 GST_DEBUG_OBJECT (basesrc, "was started");
3451 GST_OBJECT_UNLOCK (basesrc);
3452 GST_LIVE_UNLOCK (basesrc);
3457 GST_DEBUG_OBJECT (basesrc, "could not start");
3458 /* subclass is supposed to post a message but we post one as a fallback
3459 * just in case. We don't have to call _stop. */
3460 GST_ELEMENT_ERROR (basesrc, CORE, STATE_CHANGE, (NULL),
3461 ("Failed to start"));
3462 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3468 * gst_base_src_start_complete:
3469 * @basesrc: base source instance
3470 * @ret: a #GstFlowReturn
3472 * Complete an asynchronous start operation. When the subclass overrides the
3473 * start method, it should call gst_base_src_start_complete() when the start
3474 * operation completes either from the same thread or from an asynchronous
3478 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3487 if (ret != GST_FLOW_OK)
3490 GST_DEBUG_OBJECT (basesrc, "starting source");
3491 format = basesrc->segment.format;
3493 /* figure out the size */
3496 if (format == GST_FORMAT_BYTES) {
3497 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3499 if (bclass->get_size) {
3500 if (!(have_size = bclass->get_size (basesrc, &size)))
3503 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3504 /* only update the size when operating in bytes, subclass is supposed
3505 * to set duration in the start method for other formats */
3506 GST_OBJECT_LOCK (basesrc);
3507 basesrc->segment.duration = size;
3508 GST_OBJECT_UNLOCK (basesrc);
3511 GST_DEBUG_OBJECT (basesrc,
3512 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3513 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3514 basesrc->segment.duration);
3516 seekable = gst_base_src_seekable (basesrc);
3517 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3519 /* update for random access flag */
3520 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3522 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3524 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3526 GST_OBJECT_LOCK (basesrc->srcpad);
3527 mode = GST_PAD_MODE (basesrc->srcpad);
3528 GST_OBJECT_UNLOCK (basesrc->srcpad);
3530 /* take the stream lock here, we only want to let the task run when we have
3531 * set the STARTED flag */
3532 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3534 case GST_PAD_MODE_PUSH:
3535 /* do initial seek, which will start the task */
3536 GST_OBJECT_LOCK (basesrc);
3537 event = basesrc->pending_seek;
3538 basesrc->pending_seek = NULL;
3539 GST_OBJECT_UNLOCK (basesrc);
3541 /* The perform seek code will start the task when finished. We don't have to
3542 * unlock the streaming thread because it is not running yet */
3543 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3547 gst_event_unref (event);
3549 case GST_PAD_MODE_PULL:
3550 /* if not random_access, we cannot operate in pull mode for now */
3551 if (G_UNLIKELY (!basesrc->random_access))
3555 goto not_activated_yet;
3559 GST_OBJECT_LOCK (basesrc);
3560 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3561 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3562 basesrc->priv->start_result = ret;
3563 GST_ASYNC_SIGNAL (basesrc);
3564 GST_OBJECT_UNLOCK (basesrc);
3566 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3572 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3573 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3574 gst_base_src_stop (basesrc);
3576 gst_event_unref (event);
3577 ret = GST_FLOW_ERROR;
3582 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3583 gst_base_src_stop (basesrc);
3584 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3585 ret = GST_FLOW_ERROR;
3590 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3591 gst_base_src_stop (basesrc);
3592 GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3593 ret = GST_FLOW_ERROR;
3598 GST_OBJECT_LOCK (basesrc);
3599 basesrc->priv->start_result = ret;
3600 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3601 GST_ASYNC_SIGNAL (basesrc);
3602 GST_OBJECT_UNLOCK (basesrc);
3608 * gst_base_src_start_wait:
3609 * @basesrc: base source instance
3611 * Wait until the start operation completes.
3613 * Returns: a #GstFlowReturn.
3616 gst_base_src_start_wait (GstBaseSrc * basesrc)
3618 GstFlowReturn result;
3620 GST_OBJECT_LOCK (basesrc);
3621 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3622 GST_ASYNC_WAIT (basesrc);
3624 result = basesrc->priv->start_result;
3625 GST_OBJECT_UNLOCK (basesrc);
3627 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3633 gst_base_src_stop (GstBaseSrc * basesrc)
3635 GstBaseSrcClass *bclass;
3636 gboolean result = TRUE;
3638 GST_DEBUG_OBJECT (basesrc, "stopping source");
3641 gst_base_src_set_flushing (basesrc, TRUE);
3644 gst_pad_stop_task (basesrc->srcpad);
3645 /* stop flushing, this will balance unlock/unlock_stop calls */
3646 gst_base_src_set_flushing (basesrc, FALSE);
3648 GST_OBJECT_LOCK (basesrc);
3649 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3652 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3653 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3654 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3655 GST_ASYNC_SIGNAL (basesrc);
3656 GST_OBJECT_UNLOCK (basesrc);
3658 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3660 result = bclass->stop (basesrc);
3662 if (basesrc->priv->pending_bufferlist != NULL) {
3663 gst_buffer_list_unref (basesrc->priv->pending_bufferlist);
3664 basesrc->priv->pending_bufferlist = NULL;
3667 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3673 GST_DEBUG_OBJECT (basesrc, "was stopped");
3674 GST_OBJECT_UNLOCK (basesrc);
3679 /* start or stop flushing dataprocessing
3682 gst_base_src_set_flushing (GstBaseSrc * basesrc, gboolean flushing)
3684 GstBaseSrcClass *bclass;
3686 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3688 GST_DEBUG_OBJECT (basesrc, "flushing %d", flushing);
3691 gst_base_src_set_pool_flushing (basesrc, TRUE);
3692 /* unlock any subclasses to allow turning off the streaming thread */
3694 bclass->unlock (basesrc);
3697 /* the live lock is released when we are blocked, waiting for playing,
3698 * when we sync to the clock or creating a buffer */
3699 GST_LIVE_LOCK (basesrc);
3700 basesrc->priv->flushing = flushing;
3702 /* clear pending EOS if any */
3703 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3704 GST_OBJECT_LOCK (basesrc);
3705 CLEAR_PENDING_EOS (basesrc);
3706 basesrc->priv->forced_eos = FALSE;
3707 GST_OBJECT_UNLOCK (basesrc);
3710 /* unblock clock sync (if any) or any other blocking thing */
3711 if (basesrc->clock_id)
3712 gst_clock_id_unschedule (basesrc->clock_id);
3714 gst_base_src_set_pool_flushing (basesrc, FALSE);
3716 /* Drop all delayed events */
3717 GST_OBJECT_LOCK (basesrc);
3718 if (basesrc->priv->pending_events) {
3719 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3721 g_list_free (basesrc->priv->pending_events);
3722 basesrc->priv->pending_events = NULL;
3723 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3725 GST_OBJECT_UNLOCK (basesrc);
3728 GST_LIVE_SIGNAL (basesrc);
3729 GST_LIVE_UNLOCK (basesrc);
3732 /* Now wait for the stream lock to be released and clear our unlock request */
3733 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3734 if (bclass->unlock_stop)
3735 bclass->unlock_stop (basesrc);
3736 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3742 /* the purpose of this function is to make sure that a live source blocks in the
3743 * LIVE lock or leaves the LIVE lock and continues playing. */
3745 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3747 /* we are now able to grab the LIVE lock, when we get it, we can be
3748 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3750 GST_LIVE_LOCK (basesrc);
3751 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3753 /* unblock clock sync (if any) */
3754 if (basesrc->clock_id)
3755 gst_clock_id_unschedule (basesrc->clock_id);
3757 /* configure what to do when we get to the LIVE lock. */
3758 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3759 basesrc->live_running = live_play;
3764 /* for live sources we restart the timestamp correction */
3765 GST_OBJECT_LOCK (basesrc);
3766 basesrc->priv->latency = -1;
3767 GST_OBJECT_UNLOCK (basesrc);
3768 /* have to restart the task in case it stopped because of the unlock when
3769 * we went to PAUSED. Only do this if we operating in push mode. */
3770 GST_OBJECT_LOCK (basesrc->srcpad);
3771 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3772 GST_OBJECT_UNLOCK (basesrc->srcpad);
3774 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3775 basesrc->srcpad, NULL);
3776 GST_DEBUG_OBJECT (basesrc, "signal");
3777 GST_LIVE_SIGNAL (basesrc);
3779 GST_LIVE_UNLOCK (basesrc);
3785 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3787 GstBaseSrc *basesrc;
3789 basesrc = GST_BASE_SRC (parent);
3791 /* prepare subclass first */
3793 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3795 if (G_UNLIKELY (!basesrc->can_activate_push))
3796 goto no_push_activation;
3798 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3801 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3802 /* now we can stop the source */
3803 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3811 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3816 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3821 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3827 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3829 GstBaseSrc *basesrc;
3831 basesrc = GST_BASE_SRC (parent);
3833 /* prepare subclass first */
3835 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3836 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3839 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3840 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3848 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3853 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3859 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3860 GstPadMode mode, gboolean active)
3863 GstBaseSrc *src = GST_BASE_SRC (parent);
3865 src->priv->stream_start_pending = FALSE;
3867 GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3870 case GST_PAD_MODE_PULL:
3871 res = gst_base_src_activate_pull (pad, parent, active);
3873 case GST_PAD_MODE_PUSH:
3874 src->priv->stream_start_pending = active;
3875 res = gst_base_src_activate_push (pad, parent, active);
3878 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3886 static GstStateChangeReturn
3887 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3889 GstBaseSrc *basesrc;
3890 GstStateChangeReturn result;
3891 gboolean no_preroll = FALSE;
3893 basesrc = GST_BASE_SRC (element);
3895 switch (transition) {
3896 case GST_STATE_CHANGE_NULL_TO_READY:
3898 case GST_STATE_CHANGE_READY_TO_PAUSED:
3899 no_preroll = gst_base_src_is_live (basesrc);
3901 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3902 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3903 if (gst_base_src_is_live (basesrc)) {
3904 /* now we can start playback */
3905 gst_base_src_set_playing (basesrc, TRUE);
3913 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3914 transition)) == GST_STATE_CHANGE_FAILURE)
3917 switch (transition) {
3918 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3919 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3920 if (gst_base_src_is_live (basesrc)) {
3921 /* make sure we block in the live cond in PAUSED */
3922 gst_base_src_set_playing (basesrc, FALSE);
3926 case GST_STATE_CHANGE_PAUSED_TO_READY:
3928 /* we don't need to unblock anything here, the pad deactivation code
3929 * already did this */
3930 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3931 GST_OBJECT_LOCK (basesrc);
3932 CLEAR_PENDING_EOS (basesrc);
3933 GST_OBJECT_UNLOCK (basesrc);
3935 gst_event_replace (&basesrc->pending_seek, NULL);
3938 case GST_STATE_CHANGE_READY_TO_NULL:
3944 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3945 result = GST_STATE_CHANGE_NO_PREROLL;
3952 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3958 * gst_base_src_get_buffer_pool:
3959 * @src: a #GstBaseSrc
3961 * Returns: (transfer full): the instance of the #GstBufferPool used
3962 * by the src; unref it after usage.
3965 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3967 GstBufferPool *ret = NULL;
3969 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3971 GST_OBJECT_LOCK (src);
3972 if (src->priv->pool)
3973 ret = gst_object_ref (src->priv->pool);
3974 GST_OBJECT_UNLOCK (src);
3980 * gst_base_src_get_allocator:
3981 * @src: a #GstBaseSrc
3982 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3984 * @params: (out) (allow-none) (transfer full): the
3985 * #GstAllocationParams of @allocator
3987 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3988 * used by the base class and its @params.
3990 * Unref the @allocator after usage.
3993 gst_base_src_get_allocator (GstBaseSrc * src,
3994 GstAllocator ** allocator, GstAllocationParams * params)
3996 g_return_if_fail (GST_IS_BASE_SRC (src));
3998 GST_OBJECT_LOCK (src);
4000 *allocator = src->priv->allocator ?
4001 gst_object_ref (src->priv->allocator) : NULL;
4004 *params = src->priv->params;
4005 GST_OBJECT_UNLOCK (src);
4009 * gst_base_src_submit_buffer_list:
4010 * @src: a #GstBaseSrc
4011 * @buffer_list: (transfer full): a #GstBufferList
4013 * Subclasses can call this from their create virtual method implementation
4014 * to submit a buffer list to be pushed out later. This is useful in
4015 * cases where the create function wants to produce multiple buffers to be
4016 * pushed out in one go in form of a #GstBufferList, which can reduce overhead
4017 * drastically, especially for packetised inputs (for data streams where
4018 * the packetisation/chunking is not important it is usually more efficient
4019 * to return larger buffers instead).
4021 * Subclasses that use this function from their create function must return
4022 * %GST_FLOW_OK and no buffer from their create virtual method implementation.
4023 * If a buffer is returned after a buffer list has also been submitted via this
4024 * function the behaviour is undefined.
4026 * Subclasses must only call this function once per create function call and
4027 * subclasses must only call this function when the source operates in push
4033 gst_base_src_submit_buffer_list (GstBaseSrc * src, GstBufferList * buffer_list)
4035 g_return_if_fail (GST_IS_BASE_SRC (src));
4036 g_return_if_fail (GST_IS_BUFFER_LIST (buffer_list));
4037 g_return_if_fail (BASE_SRC_HAS_PENDING_BUFFER_LIST (src) == FALSE);
4039 src->priv->pending_bufferlist = buffer_list;
4041 GST_LOG_OBJECT (src, "%u buffers submitted in buffer list",
4042 gst_buffer_list_length (buffer_list));