2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 * @short_description: Base class for getrange based source elements
26 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
28 * This is a generic base class for source elements. The following
29 * types of sources are supported:
31 * <listitem><para>random access sources like files</para></listitem>
32 * <listitem><para>seekable sources</para></listitem>
33 * <listitem><para>live sources</para></listitem>
36 * The source can be configured to operate in any #GstFormat with the
37 * gst_base_src_set_format() method. The currently set format determines
38 * the format of the internal #GstSegment and any #GST_EVENT_SEGMENT
39 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
41 * #GstBaseSrc always supports push mode scheduling. If the following
42 * conditions are met, it also supports pull mode scheduling:
44 * <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
46 * <listitem><para>#GstBaseSrcClass.is_seekable() returns %TRUE.</para>
50 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
51 * automatically seekable in push mode as well. The following conditions must
52 * be met to make the element seekable in push mode when the format is not
56 * #GstBaseSrcClass.is_seekable() returns %TRUE.
59 * #GstBaseSrcClass.query() can convert all supported seek formats to the
60 * internal format as set with gst_base_src_set_format().
63 * #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
68 * When the element does not meet the requirements to operate in pull mode, the
69 * offset and length in the #GstBaseSrcClass.create() method should be ignored.
70 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
71 * element can operate in pull mode but only with specific offsets and
72 * lengths, it is allowed to generate an error when the wrong values are passed
73 * to the #GstBaseSrcClass.create() function.
75 * #GstBaseSrc has support for live sources. Live sources are sources that when
76 * paused discard data, such as audio or video capture devices. A typical live
77 * source also produces data at a fixed rate and thus provides a clock to publish
79 * Use gst_base_src_set_live() to activate the live source mode.
81 * A live source does not produce data in the PAUSED state. This means that the
82 * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
83 * PLAYING. To signal the pipeline that the element will not produce data, the
84 * return value from the READY to PAUSED state will be
85 * #GST_STATE_CHANGE_NO_PREROLL.
87 * A typical live source will timestamp the buffers it creates with the
88 * current running time of the pipeline. This is one reason why a live source
89 * can only produce data in the PLAYING state, when the clock is actually
90 * distributed and running.
92 * Live sources that synchronize and block on the clock (an audio source, for
93 * example) can use gst_base_src_wait_playing() when the
94 * #GstBaseSrcClass.create() function was interrupted by a state change to
97 * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
98 * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
99 * function if the source is a live source. The #GstBaseSrcClass.get_times()
100 * function should return timestamps starting from 0, as if it were a non-live
101 * source. The base class will make sure that the timestamps are transformed
102 * into the current running_time. The base source will then wait for the
103 * calculated running_time before pushing out the buffer.
105 * For live sources, the base class will by default report a latency of 0.
106 * For pseudo live sources, the base class will by default measure the difference
107 * between the first buffer timestamp and the start time of get_times and will
108 * report this value as the latency.
109 * Subclasses should override the query function when this behaviour is not
112 * There is only support in #GstBaseSrc for exactly one source pad, which
113 * should be named "src". A source implementation (subclass of #GstBaseSrc)
114 * should install a pad template in its class_init function, like so:
117 * my_element_class_init (GstMyElementClass *klass)
119 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
120 * // srctemplate should be a #GstStaticPadTemplate with direction
121 * // #GST_PAD_SRC and name "src"
122 * gst_element_class_add_pad_template (gstelement_class,
123 * gst_static_pad_template_get (&srctemplate));
125 * gst_element_class_set_static_metadata (gstelement_class,
128 * "My Source element",
129 * "The author <my.sink@my.email>");
134 * <title>Controlled shutdown of live sources in applications</title>
136 * Applications that record from a live source may want to stop recording
137 * in a controlled way, so that the recording is stopped, but the data
138 * already in the pipeline is processed to the end (remember that many live
139 * sources would go on recording forever otherwise). For that to happen the
140 * application needs to make the source stop recording and send an EOS
141 * event down the pipeline. The application would then wait for an
142 * EOS message posted on the pipeline's bus to know when all data has
143 * been processed and the pipeline can safely be stopped.
145 * An application may send an EOS event to a source element to make it
146 * perform the EOS logic (send EOS event downstream or post a
147 * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
148 * with the gst_element_send_event() function on the element or its parent bin.
150 * After the EOS has been sent to the element, the application should wait for
151 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
152 * received, it may safely shut down the entire pipeline.
164 #include <gst/gst_private.h>
165 #include <gst/glib-compat-private.h>
167 #include "gstbasesrc.h"
168 #include "gsttypefindhelper.h"
169 #include <gst/gst-i18n-lib.h>
171 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
172 #define GST_CAT_DEFAULT gst_base_src_debug
174 #define GST_LIVE_GET_LOCK(elem) (&GST_BASE_SRC_CAST(elem)->live_lock)
175 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
176 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
177 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
178 #define GST_LIVE_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->live_cond)
179 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
180 #define GST_LIVE_WAIT_UNTIL(elem, end_time) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
181 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
182 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
185 #define GST_ASYNC_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->priv->async_cond)
186 #define GST_ASYNC_WAIT(elem) g_cond_wait (GST_ASYNC_GET_COND (elem), GST_OBJECT_GET_LOCK (elem))
187 #define GST_ASYNC_SIGNAL(elem) g_cond_signal (GST_ASYNC_GET_COND (elem));
189 #define CLEAR_PENDING_EOS(bsrc) \
191 g_atomic_int_set (&bsrc->priv->has_pending_eos, FALSE); \
192 gst_event_replace (&bsrc->priv->pending_eos, NULL); \
196 /* BaseSrc signals and args */
203 #define DEFAULT_BLOCKSIZE 4096
204 #define DEFAULT_NUM_BUFFERS -1
205 #define DEFAULT_TYPEFIND FALSE
206 #define DEFAULT_DO_TIMESTAMP FALSE
217 #define GST_BASE_SRC_GET_PRIVATE(obj) \
218 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
220 struct _GstBaseSrcPrivate
225 GstFlowReturn start_result;
228 /* if a stream-start event should be sent */
229 gboolean stream_start_pending;
231 /* if segment should be sent and a
232 * seqnum if it was originated by a seek */
233 gboolean segment_pending;
234 guint32 segment_seqnum;
236 /* if EOS is pending (atomic) */
237 GstEvent *pending_eos;
238 gint has_pending_eos;
240 /* if the eos was caused by a forced eos from the application */
243 /* startup latency is the time it takes between going to PLAYING and producing
244 * the first BUFFER with running_time 0. This value is included in the latency
246 GstClockTime latency;
247 /* timestamp offset, this is the offset add to the values of gst_times for
248 * pseudo live sources */
249 GstClockTimeDiff ts_offset;
251 gboolean do_timestamp;
252 volatile gint dynamic_size;
253 volatile gint automatic_eos;
255 /* stream sequence number */
258 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
259 * pushed in the data stream */
260 GList *pending_events;
261 volatile gint have_events;
263 /* QoS *//* with LOCK */
264 gboolean qos_enabled;
266 GstClockTime earliest_time;
269 GstAllocator *allocator;
270 GstAllocationParams params;
275 static GstElementClass *parent_class = NULL;
277 static void gst_base_src_class_init (GstBaseSrcClass * klass);
278 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
279 static void gst_base_src_finalize (GObject * object);
283 gst_base_src_get_type (void)
285 static volatile gsize base_src_type = 0;
287 if (g_once_init_enter (&base_src_type)) {
289 static const GTypeInfo base_src_info = {
290 sizeof (GstBaseSrcClass),
293 (GClassInitFunc) gst_base_src_class_init,
298 (GInstanceInitFunc) gst_base_src_init,
301 _type = g_type_register_static (GST_TYPE_ELEMENT,
302 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
303 g_once_init_leave (&base_src_type, _type);
305 return base_src_type;
308 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
310 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
311 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
313 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
314 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
315 GstPadMode mode, gboolean active);
316 static void gst_base_src_set_property (GObject * object, guint prop_id,
317 const GValue * value, GParamSpec * pspec);
318 static void gst_base_src_get_property (GObject * object, guint prop_id,
319 GValue * value, GParamSpec * pspec);
320 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
322 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
323 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
325 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
328 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
330 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
331 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
332 GstSegment * segment);
333 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
334 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
335 GstEvent * event, GstSegment * segment);
336 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
337 guint64 offset, guint size, GstBuffer ** buf);
338 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
339 guint64 offset, guint size, GstBuffer ** buf);
340 static gboolean gst_base_src_decide_allocation_default (GstBaseSrc * basesrc,
343 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
344 gboolean flushing, gboolean live_play, gboolean * playing);
346 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
347 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
349 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
350 GstStateChange transition);
352 static void gst_base_src_loop (GstPad * pad);
353 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
354 guint64 offset, guint length, GstBuffer ** buf);
355 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
356 guint length, GstBuffer ** buf);
357 static gboolean gst_base_src_seekable (GstBaseSrc * src);
358 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
359 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
360 guint * length, gboolean force);
363 gst_base_src_class_init (GstBaseSrcClass * klass)
365 GObjectClass *gobject_class;
366 GstElementClass *gstelement_class;
368 gobject_class = G_OBJECT_CLASS (klass);
369 gstelement_class = GST_ELEMENT_CLASS (klass);
371 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
373 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
375 parent_class = g_type_class_peek_parent (klass);
377 gobject_class->finalize = gst_base_src_finalize;
378 gobject_class->set_property = gst_base_src_set_property;
379 gobject_class->get_property = gst_base_src_get_property;
381 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
382 g_param_spec_uint ("blocksize", "Block size",
383 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
384 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
386 g_param_spec_int ("num-buffers", "num-buffers",
387 "Number of buffers to output before sending EOS (-1 = unlimited)",
388 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
389 G_PARAM_STATIC_STRINGS));
390 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
391 g_param_spec_boolean ("typefind", "Typefind",
392 "Run typefind before negotiating", DEFAULT_TYPEFIND,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
395 g_param_spec_boolean ("do-timestamp", "Do timestamp",
396 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 gstelement_class->change_state =
400 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
401 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
403 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
404 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
405 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
406 klass->prepare_seek_segment =
407 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
408 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
409 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
410 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
411 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
412 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
413 klass->decide_allocation =
414 GST_DEBUG_FUNCPTR (gst_base_src_decide_allocation_default);
416 /* Registering debug symbols for function pointers */
417 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
418 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
420 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
421 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
425 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
428 GstPadTemplate *pad_template;
430 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
432 basesrc->is_live = FALSE;
433 g_mutex_init (&basesrc->live_lock);
434 g_cond_init (&basesrc->live_cond);
435 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
436 basesrc->num_buffers_left = -1;
437 basesrc->priv->automatic_eos = TRUE;
439 basesrc->can_activate_push = TRUE;
442 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
443 g_return_if_fail (pad_template != NULL);
445 GST_DEBUG_OBJECT (basesrc, "creating src pad");
446 pad = gst_pad_new_from_template (pad_template, "src");
448 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
449 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
450 gst_pad_set_event_function (pad, gst_base_src_event);
451 gst_pad_set_query_function (pad, gst_base_src_query);
452 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
454 /* hold pointer to pad */
455 basesrc->srcpad = pad;
456 GST_DEBUG_OBJECT (basesrc, "adding src pad");
457 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
459 basesrc->blocksize = DEFAULT_BLOCKSIZE;
460 basesrc->clock_id = NULL;
461 /* we operate in BYTES by default */
462 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
463 basesrc->typefind = DEFAULT_TYPEFIND;
464 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
465 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
467 g_cond_init (&basesrc->priv->async_cond);
468 basesrc->priv->start_result = GST_FLOW_FLUSHING;
469 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
470 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
471 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
473 GST_DEBUG_OBJECT (basesrc, "init done");
477 gst_base_src_finalize (GObject * object)
482 basesrc = GST_BASE_SRC (object);
484 g_mutex_clear (&basesrc->live_lock);
485 g_cond_clear (&basesrc->live_cond);
486 g_cond_clear (&basesrc->priv->async_cond);
488 event_p = &basesrc->pending_seek;
489 gst_event_replace (event_p, NULL);
491 if (basesrc->priv->pending_events) {
492 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
494 g_list_free (basesrc->priv->pending_events);
497 G_OBJECT_CLASS (parent_class)->finalize (object);
501 * gst_base_src_wait_playing:
504 * If the #GstBaseSrcClass.create() method performs its own synchronisation
505 * against the clock it must unblock when going from PLAYING to the PAUSED state
506 * and call this method before continuing to produce the remaining data.
508 * This function will block until a state change to PLAYING happens (in which
509 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
510 * to a state change to READY or a FLUSH event (in which case this function
511 * returns #GST_FLOW_FLUSHING).
513 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
514 * continue. Any other return value should be returned from the create vmethod.
517 gst_base_src_wait_playing (GstBaseSrc * src)
519 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
522 /* block until the state changes, or we get a flush, or something */
523 GST_DEBUG_OBJECT (src, "live source waiting for running state");
525 GST_DEBUG_OBJECT (src, "live source unlocked");
526 if (src->priv->flushing)
528 } while (G_UNLIKELY (!src->live_running));
535 GST_DEBUG_OBJECT (src, "we are flushing");
536 return GST_FLOW_FLUSHING;
541 * gst_base_src_set_live:
542 * @src: base source instance
543 * @live: new live-mode
545 * If the element listens to a live source, @live should
548 * A live source will not produce data in the PAUSED state and
549 * will therefore not be able to participate in the PREROLL phase
550 * of a pipeline. To signal this fact to the application and the
551 * pipeline, the state change return value of the live source will
552 * be GST_STATE_CHANGE_NO_PREROLL.
555 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
557 g_return_if_fail (GST_IS_BASE_SRC (src));
559 GST_OBJECT_LOCK (src);
561 GST_OBJECT_UNLOCK (src);
565 * gst_base_src_is_live:
566 * @src: base source instance
568 * Check if an element is in live mode.
570 * Returns: %TRUE if element is in live mode.
573 gst_base_src_is_live (GstBaseSrc * src)
577 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
579 GST_OBJECT_LOCK (src);
580 result = src->is_live;
581 GST_OBJECT_UNLOCK (src);
587 * gst_base_src_set_format:
588 * @src: base source instance
589 * @format: the format to use
591 * Sets the default format of the source. This will be the format used
592 * for sending SEGMENT events and for performing seeks.
594 * If a format of GST_FORMAT_BYTES is set, the element will be able to
595 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
597 * This function must only be called in states < %GST_STATE_PAUSED.
600 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
602 g_return_if_fail (GST_IS_BASE_SRC (src));
603 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
605 GST_OBJECT_LOCK (src);
606 gst_segment_init (&src->segment, format);
607 GST_OBJECT_UNLOCK (src);
611 * gst_base_src_set_dynamic_size:
612 * @src: base source instance
613 * @dynamic: new dynamic size mode
615 * If not @dynamic, size is only updated when needed, such as when trying to
616 * read past current tracked size. Otherwise, size is checked for upon each
620 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
622 g_return_if_fail (GST_IS_BASE_SRC (src));
624 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
628 * gst_base_src_set_automatic_eos:
629 * @src: base source instance
630 * @automatic_eos: automatic eos
632 * If @automatic_eos is %TRUE, basesrc will automatically go EOS if a buffer
633 * after the total size is returned. By default this is %TRUE but sources
634 * that can't return an authoritative size and only know that they're EOS
635 * when trying to read more should set this to %FALSE.
640 gst_base_src_set_automatic_eos (GstBaseSrc * src, gboolean automatic_eos)
642 g_return_if_fail (GST_IS_BASE_SRC (src));
644 g_atomic_int_set (&src->priv->automatic_eos, automatic_eos);
648 * gst_base_src_set_async:
649 * @src: base source instance
650 * @async: new async mode
652 * Configure async behaviour in @src, no state change will block. The open,
653 * close, start, stop, play and pause virtual methods will be executed in a
654 * different thread and are thus allowed to perform blocking operations. Any
655 * blocking operation should be unblocked with the unlock vmethod.
658 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
660 g_return_if_fail (GST_IS_BASE_SRC (src));
662 GST_OBJECT_LOCK (src);
663 src->priv->async = async;
664 GST_OBJECT_UNLOCK (src);
668 * gst_base_src_is_async:
669 * @src: base source instance
671 * Get the current async behaviour of @src. See also gst_base_src_set_async().
673 * Returns: %TRUE if @src is operating in async mode.
676 gst_base_src_is_async (GstBaseSrc * src)
680 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
682 GST_OBJECT_LOCK (src);
683 res = src->priv->async;
684 GST_OBJECT_UNLOCK (src);
691 * gst_base_src_query_latency:
693 * @live: (out) (allow-none): if the source is live
694 * @min_latency: (out) (allow-none): the min latency of the source
695 * @max_latency: (out) (allow-none): the max latency of the source
697 * Query the source for the latency parameters. @live will be TRUE when @src is
698 * configured as a live source. @min_latency will be set to the difference
699 * between the running time and the timestamp of the first buffer.
700 * @max_latency is always the undefined value of -1.
702 * This function is mostly used by subclasses.
704 * Returns: TRUE if the query succeeded.
707 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
708 GstClockTime * min_latency, GstClockTime * max_latency)
712 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
714 GST_OBJECT_LOCK (src);
716 *live = src->is_live;
718 /* if we have a startup latency, report this one, else report 0. Subclasses
719 * are supposed to override the query function if they want something
721 if (src->priv->latency != -1)
722 min = src->priv->latency;
731 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
732 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
734 GST_OBJECT_UNLOCK (src);
740 * gst_base_src_set_blocksize:
742 * @blocksize: the new blocksize in bytes
744 * Set the number of bytes that @src will push out with each buffer. When
745 * @blocksize is set to -1, a default length will be used.
748 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
750 g_return_if_fail (GST_IS_BASE_SRC (src));
752 GST_OBJECT_LOCK (src);
753 src->blocksize = blocksize;
754 GST_OBJECT_UNLOCK (src);
758 * gst_base_src_get_blocksize:
761 * Get the number of bytes that @src will push out with each buffer.
763 * Returns: the number of bytes pushed with each buffer.
766 gst_base_src_get_blocksize (GstBaseSrc * src)
770 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
772 GST_OBJECT_LOCK (src);
773 res = src->blocksize;
774 GST_OBJECT_UNLOCK (src);
781 * gst_base_src_set_do_timestamp:
783 * @timestamp: enable or disable timestamping
785 * Configure @src to automatically timestamp outgoing buffers based on the
786 * current running_time of the pipeline. This property is mostly useful for live
790 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
792 g_return_if_fail (GST_IS_BASE_SRC (src));
794 GST_OBJECT_LOCK (src);
795 src->priv->do_timestamp = timestamp;
796 if (timestamp && src->segment.format != GST_FORMAT_TIME)
797 gst_segment_init (&src->segment, GST_FORMAT_TIME);
798 GST_OBJECT_UNLOCK (src);
802 * gst_base_src_get_do_timestamp:
805 * Query if @src timestamps outgoing buffers based on the current running_time.
807 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
810 gst_base_src_get_do_timestamp (GstBaseSrc * src)
814 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
816 GST_OBJECT_LOCK (src);
817 res = src->priv->do_timestamp;
818 GST_OBJECT_UNLOCK (src);
824 * gst_base_src_new_seamless_segment:
826 * @start: The new start value for the segment
827 * @stop: Stop value for the new segment
828 * @time: The new time value for the start of the new segment
830 * Prepare a new seamless segment for emission downstream. This function must
831 * only be called by derived sub-classes, and only from the create() function,
832 * as the stream-lock needs to be held.
834 * The format for the new segment will be the current format of the source, as
835 * configured with gst_base_src_set_format()
837 * Returns: %TRUE if preparation of the seamless segment succeeded.
840 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
845 GST_OBJECT_LOCK (src);
847 src->segment.base = gst_segment_to_running_time (&src->segment,
848 src->segment.format, src->segment.position);
849 src->segment.position = src->segment.start = start;
850 src->segment.stop = stop;
851 src->segment.time = time;
853 /* Mark pending segment. Will be sent before next data */
854 src->priv->segment_pending = TRUE;
855 src->priv->segment_seqnum = gst_util_seqnum_next ();
857 GST_DEBUG_OBJECT (src,
858 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
859 GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
860 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
861 GST_TIME_ARGS (src->segment.base));
863 GST_OBJECT_UNLOCK (src);
865 src->priv->discont = TRUE;
872 gst_base_src_send_stream_start (GstBaseSrc * src)
876 if (src->priv->stream_start_pending) {
881 gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
883 GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
884 event = gst_event_new_stream_start (stream_id);
885 gst_event_set_group_id (event, gst_util_group_id_next ());
887 ret = gst_pad_push_event (src->srcpad, event);
888 src->priv->stream_start_pending = FALSE;
896 * gst_base_src_set_caps:
897 * @src: a #GstBaseSrc
900 * Set new caps on the basesrc source pad.
902 * Returns: %TRUE if the caps could be set
905 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
907 GstBaseSrcClass *bclass;
910 bclass = GST_BASE_SRC_GET_CLASS (src);
912 gst_base_src_send_stream_start (src);
914 if (bclass->set_caps)
915 res = bclass->set_caps (src, caps);
918 res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
924 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
926 GstCaps *caps = NULL;
927 GstPadTemplate *pad_template;
928 GstBaseSrcClass *bclass;
930 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
933 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
935 if (pad_template != NULL) {
936 caps = gst_pad_template_get_caps (pad_template);
939 GstCaps *intersection;
942 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
943 gst_caps_unref (caps);
951 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
953 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
954 return gst_caps_fixate (caps);
958 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
960 GstBaseSrcClass *bclass;
962 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
965 caps = bclass->fixate (bsrc, caps);
971 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
975 switch (GST_QUERY_TYPE (query)) {
976 case GST_QUERY_POSITION:
980 gst_query_parse_position (query, &format, NULL);
982 GST_DEBUG_OBJECT (src, "position query in format %s",
983 gst_format_get_name (format));
986 case GST_FORMAT_PERCENT:
992 GST_OBJECT_LOCK (src);
993 position = src->segment.position;
994 duration = src->segment.duration;
995 GST_OBJECT_UNLOCK (src);
997 if (position != -1 && duration != -1) {
998 if (position < duration)
999 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1002 percent = GST_FORMAT_PERCENT_MAX;
1006 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1013 GstFormat seg_format;
1015 GST_OBJECT_LOCK (src);
1017 gst_segment_to_stream_time (&src->segment, src->segment.format,
1018 src->segment.position);
1019 seg_format = src->segment.format;
1020 GST_OBJECT_UNLOCK (src);
1022 if (position != -1) {
1023 /* convert to requested format */
1025 gst_pad_query_convert (src->srcpad, seg_format,
1026 position, format, &position);
1030 gst_query_set_position (query, format, position);
1036 case GST_QUERY_DURATION:
1040 gst_query_parse_duration (query, &format, NULL);
1042 GST_DEBUG_OBJECT (src, "duration query in format %s",
1043 gst_format_get_name (format));
1046 case GST_FORMAT_PERCENT:
1047 gst_query_set_duration (query, GST_FORMAT_PERCENT,
1048 GST_FORMAT_PERCENT_MAX);
1054 GstFormat seg_format;
1057 /* may have to refresh duration */
1058 gst_base_src_update_length (src, 0, &length,
1059 g_atomic_int_get (&src->priv->dynamic_size));
1061 /* this is the duration as configured by the subclass. */
1062 GST_OBJECT_LOCK (src);
1063 duration = src->segment.duration;
1064 seg_format = src->segment.format;
1065 GST_OBJECT_UNLOCK (src);
1067 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1068 duration, gst_format_get_name (seg_format));
1070 if (duration != -1) {
1071 /* convert to requested format, if this fails, we have a duration
1072 * but we cannot answer the query, we must return FALSE. */
1074 gst_pad_query_convert (src->srcpad, seg_format,
1075 duration, format, &duration);
1077 /* The subclass did not configure a duration, we assume that the
1078 * media has an unknown duration then and we return TRUE to report
1079 * this. Note that this is not the same as returning FALSE, which
1080 * means that we cannot report the duration at all. */
1083 gst_query_set_duration (query, format, duration);
1090 case GST_QUERY_SEEKING:
1092 GstFormat format, seg_format;
1095 GST_OBJECT_LOCK (src);
1096 duration = src->segment.duration;
1097 seg_format = src->segment.format;
1098 GST_OBJECT_UNLOCK (src);
1100 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1101 if (format == seg_format) {
1102 gst_query_set_seeking (query, seg_format,
1103 gst_base_src_seekable (src), 0, duration);
1106 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1107 /* Don't reply to the query to make up for demuxers which don't
1108 * handle the SEEKING query yet. Players like Totem will fall back
1109 * to the duration when the SEEKING query isn't answered. */
1114 case GST_QUERY_SEGMENT:
1119 GST_OBJECT_LOCK (src);
1121 format = src->segment.format;
1124 gst_segment_to_stream_time (&src->segment, format,
1125 src->segment.start);
1126 if ((stop = src->segment.stop) == -1)
1127 stop = src->segment.duration;
1129 stop = gst_segment_to_stream_time (&src->segment, format, stop);
1131 gst_query_set_segment (query, src->segment.rate, format, start, stop);
1133 GST_OBJECT_UNLOCK (src);
1138 case GST_QUERY_FORMATS:
1140 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1141 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1145 case GST_QUERY_CONVERT:
1147 GstFormat src_fmt, dest_fmt;
1148 gint64 src_val, dest_val;
1150 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1152 /* we can only convert between equal formats... */
1153 if (src_fmt == dest_fmt) {
1159 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1162 case GST_QUERY_LATENCY:
1164 GstClockTime min, max;
1167 /* Subclasses should override and implement something useful */
1168 res = gst_base_src_query_latency (src, &live, &min, &max);
1170 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1171 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1172 GST_TIME_ARGS (max));
1174 gst_query_set_latency (query, live, min, max);
1177 case GST_QUERY_JITTER:
1178 case GST_QUERY_RATE:
1181 case GST_QUERY_BUFFERING:
1183 GstFormat format, seg_format;
1184 gint64 start, stop, estimated;
1186 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1188 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1189 gst_format_get_name (format));
1191 GST_OBJECT_LOCK (src);
1192 if (src->random_access) {
1195 if (format == GST_FORMAT_PERCENT)
1196 stop = GST_FORMAT_PERCENT_MAX;
1198 stop = src->segment.duration;
1204 seg_format = src->segment.format;
1205 GST_OBJECT_UNLOCK (src);
1207 /* convert to required format. When the conversion fails, we can't answer
1208 * the query. When the value is unknown, we can don't perform conversion
1209 * but report TRUE. */
1210 if (format != GST_FORMAT_PERCENT && stop != -1) {
1211 res = gst_pad_query_convert (src->srcpad, seg_format,
1212 stop, format, &stop);
1216 if (res && format != GST_FORMAT_PERCENT && start != -1)
1217 res = gst_pad_query_convert (src->srcpad, seg_format,
1218 start, format, &start);
1220 gst_query_set_buffering_range (query, format, start, stop, estimated);
1223 case GST_QUERY_SCHEDULING:
1225 gboolean random_access;
1227 random_access = gst_base_src_is_random_access (src);
1229 /* we can operate in getrange mode if the native format is bytes
1230 * and we are seekable, this condition is set in the random_access
1231 * flag and is set in the _start() method. */
1232 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1234 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1235 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1240 case GST_QUERY_CAPS:
1242 GstBaseSrcClass *bclass;
1243 GstCaps *caps, *filter;
1245 bclass = GST_BASE_SRC_GET_CLASS (src);
1246 if (bclass->get_caps) {
1247 gst_query_parse_caps (query, &filter);
1248 if ((caps = bclass->get_caps (src, filter))) {
1249 gst_query_set_caps_result (query, caps);
1250 gst_caps_unref (caps);
1259 case GST_QUERY_URI:{
1260 if (GST_IS_URI_HANDLER (src)) {
1261 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1264 gst_query_set_uri (query, uri);
1279 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1286 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1289 GstBaseSrcClass *bclass;
1290 gboolean result = FALSE;
1292 src = GST_BASE_SRC (parent);
1293 bclass = GST_BASE_SRC_GET_CLASS (src);
1296 result = bclass->query (src, query);
1302 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1304 gboolean res = TRUE;
1306 /* update our offset if the start/stop position was updated */
1307 if (segment->format == GST_FORMAT_BYTES) {
1308 segment->time = segment->start;
1309 } else if (segment->start == 0) {
1310 /* seek to start, we can implement a default for this. */
1314 GST_INFO_OBJECT (src, "Can't do a default seek");
1321 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1323 GstBaseSrcClass *bclass;
1324 gboolean result = FALSE;
1326 bclass = GST_BASE_SRC_GET_CLASS (src);
1328 GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1330 if (bclass->do_seek)
1331 result = bclass->do_seek (src, segment);
1336 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1339 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1340 GstSegment * segment)
1342 /* By default, we try one of 2 things:
1343 * - For absolute seek positions, convert the requested position to our
1344 * configured processing format and place it in the output segment \
1345 * - For relative seek positions, convert our current (input) values to the
1346 * seek format, adjust by the relative seek offset and then convert back to
1347 * the processing format
1349 GstSeekType start_type, stop_type;
1352 GstFormat seek_format, dest_format;
1355 gboolean res = TRUE;
1357 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1358 &start_type, &start, &stop_type, &stop);
1359 dest_format = segment->format;
1361 if (seek_format == dest_format) {
1362 gst_segment_do_seek (segment, rate, seek_format, flags,
1363 start_type, start, stop_type, stop, &update);
1367 if (start_type != GST_SEEK_TYPE_NONE) {
1368 /* FIXME: Handle seek_end by converting the input segment vals */
1370 gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1372 start_type = GST_SEEK_TYPE_SET;
1375 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1376 /* FIXME: Handle seek_end by converting the input segment vals */
1378 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1380 stop_type = GST_SEEK_TYPE_SET;
1383 /* And finally, configure our output segment in the desired format */
1384 gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1385 stop_type, stop, &update);
1394 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1400 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1401 GstSegment * seeksegment)
1403 GstBaseSrcClass *bclass;
1404 gboolean result = FALSE;
1406 bclass = GST_BASE_SRC_GET_CLASS (src);
1408 if (bclass->prepare_seek_segment)
1409 result = bclass->prepare_seek_segment (src, event, seeksegment);
1414 static GstFlowReturn
1415 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1416 guint size, GstBuffer ** buffer)
1419 GstBaseSrcPrivate *priv = src->priv;
1422 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1423 } else if (size != -1) {
1424 *buffer = gst_buffer_new_allocate (priv->allocator, size, &priv->params);
1425 if (G_UNLIKELY (*buffer == NULL))
1430 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1439 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1440 return GST_FLOW_ERROR;
1444 static GstFlowReturn
1445 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1446 guint size, GstBuffer ** buffer)
1448 GstBaseSrcClass *bclass;
1452 bclass = GST_BASE_SRC_GET_CLASS (src);
1454 if (G_UNLIKELY (!bclass->alloc))
1456 if (G_UNLIKELY (!bclass->fill))
1459 if (*buffer == NULL) {
1460 /* downstream did not provide us with a buffer to fill, allocate one
1462 ret = bclass->alloc (src, offset, size, &res_buf);
1463 if (G_UNLIKELY (ret != GST_FLOW_OK))
1469 if (G_LIKELY (size > 0)) {
1470 /* only call fill when there is a size */
1471 ret = bclass->fill (src, offset, size, res_buf);
1472 if (G_UNLIKELY (ret != GST_FLOW_OK))
1483 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1484 return GST_FLOW_NOT_SUPPORTED;
1488 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1493 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1494 gst_flow_get_name (ret));
1495 if (*buffer == NULL)
1496 gst_buffer_unref (res_buf);
1501 /* this code implements the seeking. It is a good example
1502 * handling all cases.
1504 * A seek updates the currently configured segment.start
1505 * and segment.stop values based on the SEEK_TYPE. If the
1506 * segment.start value is updated, a seek to this new position
1507 * should be performed.
1509 * The seek can only be executed when we are not currently
1510 * streaming any data, to make sure that this is the case, we
1511 * acquire the STREAM_LOCK which is taken when we are in the
1512 * _loop() function or when a getrange() is called. Normally
1513 * we will not receive a seek if we are operating in pull mode
1514 * though. When we operate as a live source we might block on the live
1515 * cond, which does not release the STREAM_LOCK. Therefore we will try
1516 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1517 * safe to perform the seek.
1519 * When we are in the loop() function, we might be in the middle
1520 * of pushing a buffer, which might block in a sink. To make sure
1521 * that the push gets unblocked we push out a FLUSH_START event.
1522 * Our loop function will get a FLUSHING return value from
1523 * the push and will pause, effectively releasing the STREAM_LOCK.
1525 * For a non-flushing seek, we pause the task, which might eventually
1526 * release the STREAM_LOCK. We say eventually because when the sink
1527 * blocks on the sample we might wait a very long time until the sink
1528 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1529 * can continue the seek. A non-flushing seek is normally done in a
1530 * running pipeline to perform seamless playback, this means that the sink is
1531 * PLAYING and will return from its chain function.
1532 * In the case of a non-flushing seek we need to make sure that the
1533 * data we output after the seek is continuous with the previous data,
1534 * this is because a non-flushing seek does not reset the running-time
1535 * to 0. We do this by closing the currently running segment, ie. sending
1536 * a new_segment event with the stop position set to the last processed
1539 * After updating the segment.start/stop values, we prepare for
1540 * streaming again. We push out a FLUSH_STOP to make the peer pad
1541 * accept data again and we start our task again.
1543 * A segment seek posts a message on the bus saying that the playback
1544 * of the segment started. We store the segment flag internally because
1545 * when we reach the segment.stop we have to post a segment.done
1546 * instead of EOS when doing a segment seek.
1549 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1551 gboolean res = TRUE, tres;
1553 GstFormat seek_format, dest_format;
1555 GstSeekType start_type, stop_type;
1557 gboolean flush, playing;
1559 gboolean relative_seek = FALSE;
1560 gboolean seekseg_configured = FALSE;
1561 GstSegment seeksegment;
1565 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1567 GST_OBJECT_LOCK (src);
1568 dest_format = src->segment.format;
1569 GST_OBJECT_UNLOCK (src);
1572 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1573 &start_type, &start, &stop_type, &stop);
1575 relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1576 SEEK_TYPE_IS_RELATIVE (stop_type);
1578 if (dest_format != seek_format && !relative_seek) {
1579 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1580 * here before taking the stream lock, otherwise we must convert it later,
1581 * once we have the stream lock and can read the last configures segment
1582 * start and stop positions */
1583 gst_segment_init (&seeksegment, dest_format);
1585 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1586 goto prepare_failed;
1588 seekseg_configured = TRUE;
1591 flush = flags & GST_SEEK_FLAG_FLUSH;
1592 seqnum = gst_event_get_seqnum (event);
1595 /* get next seqnum */
1596 seqnum = gst_util_seqnum_next ();
1599 /* send flush start */
1601 tevent = gst_event_new_flush_start ();
1602 gst_event_set_seqnum (tevent, seqnum);
1603 gst_pad_push_event (src->srcpad, tevent);
1605 gst_pad_pause_task (src->srcpad);
1607 /* unblock streaming thread. */
1609 gst_base_src_set_flushing (src, TRUE, FALSE, &playing);
1611 /* grab streaming lock, this should eventually be possible, either
1612 * because the task is paused, our streaming thread stopped
1613 * or because our peer is flushing. */
1614 GST_PAD_STREAM_LOCK (src->srcpad);
1615 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1616 /* we have seen this event before, issue a warning for now */
1617 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1620 src->priv->seqnum = seqnum;
1621 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1625 gst_base_src_set_flushing (src, FALSE, playing, NULL);
1627 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1628 * copy the current segment info into the temp segment that we can actually
1629 * attempt the seek with. We only update the real segment if the seek succeeds. */
1630 if (!seekseg_configured) {
1631 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1633 /* now configure the final seek segment */
1635 if (seeksegment.format != seek_format) {
1636 /* OK, here's where we give the subclass a chance to convert the relative
1637 * seek into an absolute one in the processing format. We set up any
1638 * absolute seek above, before taking the stream lock. */
1639 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1640 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1645 /* The seek format matches our processing format, no need to ask the
1646 * the subclass to configure the segment. */
1647 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1648 start_type, start, stop_type, stop, &update);
1651 /* Else, no seek event passed, so we're just (re)starting the
1656 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1657 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1658 seeksegment.start, seeksegment.stop, seeksegment.position);
1660 /* do the seek, segment.position contains the new position. */
1661 res = gst_base_src_do_seek (src, &seeksegment);
1664 /* and prepare to continue streaming */
1666 tevent = gst_event_new_flush_stop (TRUE);
1667 gst_event_set_seqnum (tevent, seqnum);
1668 /* send flush stop, peer will accept data and events again. We
1669 * are not yet providing data as we still have the STREAM_LOCK. */
1670 gst_pad_push_event (src->srcpad, tevent);
1673 /* The subclass must have converted the segment to the processing format
1675 if (res && seeksegment.format != dest_format) {
1676 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1677 "in the correct format. Aborting seek.");
1681 /* if the seek was successful, we update our real segment and push
1682 * out the new segment. */
1684 GST_OBJECT_LOCK (src);
1685 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1686 GST_OBJECT_UNLOCK (src);
1688 if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1689 GstMessage *message;
1691 message = gst_message_new_segment_start (GST_OBJECT (src),
1692 seeksegment.format, seeksegment.position);
1693 gst_message_set_seqnum (message, seqnum);
1695 gst_element_post_message (GST_ELEMENT (src), message);
1698 /* for deriving a stop position for the playback segment from the seek
1699 * segment, we must take the duration when the stop is not set */
1700 /* FIXME: This is never used below */
1701 if ((stop = seeksegment.stop) == -1)
1702 stop = seeksegment.duration;
1704 src->priv->segment_pending = TRUE;
1705 src->priv->segment_seqnum = seqnum;
1708 src->priv->discont = TRUE;
1709 src->running = TRUE;
1710 /* and restart the task in case it got paused explicitly or by
1711 * the FLUSH_START event we pushed out. */
1712 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1717 /* and release the lock again so we can continue streaming */
1718 GST_PAD_STREAM_UNLOCK (src->srcpad);
1724 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1729 /* all events send to this element directly. This is mainly done from the
1733 gst_base_src_send_event (GstElement * element, GstEvent * event)
1736 gboolean result = FALSE;
1737 GstBaseSrcClass *bclass;
1739 src = GST_BASE_SRC (element);
1740 bclass = GST_BASE_SRC_GET_CLASS (src);
1742 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1744 switch (GST_EVENT_TYPE (event)) {
1745 /* bidirectional events */
1746 case GST_EVENT_FLUSH_START:
1747 GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1748 result = gst_pad_push_event (src->srcpad, event);
1749 /* also unblock the create function */
1750 gst_base_src_activate_pool (src, FALSE);
1751 /* unlock any subclasses, we need to do this before grabbing the
1752 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
1753 * unlock to the params because of backwards compat (see seek handler)*/
1755 bclass->unlock (src);
1757 /* the live lock is released when we are blocked, waiting for playing or
1758 * when we sync to the clock. */
1759 GST_LIVE_LOCK (src);
1760 src->priv->flushing = TRUE;
1761 /* clear pending EOS if any */
1762 if (g_atomic_int_get (&src->priv->has_pending_eos)) {
1763 GST_OBJECT_LOCK (src);
1764 CLEAR_PENDING_EOS (src);
1765 src->priv->forced_eos = FALSE;
1766 GST_OBJECT_UNLOCK (src);
1768 if (bclass->unlock_stop)
1769 bclass->unlock_stop (src);
1771 gst_clock_id_unschedule (src->clock_id);
1772 GST_DEBUG_OBJECT (src, "signal");
1773 GST_LIVE_SIGNAL (src);
1774 GST_LIVE_UNLOCK (src);
1777 case GST_EVENT_FLUSH_STOP:
1781 GST_LIVE_LOCK (src);
1782 src->priv->segment_pending = TRUE;
1783 src->priv->flushing = FALSE;
1784 GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1785 result = gst_pad_push_event (src->srcpad, event);
1787 gst_base_src_activate_pool (src, TRUE);
1789 GST_OBJECT_LOCK (src->srcpad);
1790 start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1791 GST_OBJECT_UNLOCK (src->srcpad);
1793 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1795 GST_LIVE_UNLOCK (src);
1800 /* downstream serialized events */
1803 /* queue EOS and make sure the task or pull function performs the EOS
1806 * We have two possibilities:
1808 * - Before we are to enter the _create function, we check the has_pending_eos
1809 * first and do EOS instead of entering it.
1810 * - If we are in the _create function or we did not manage to set the
1811 * flag fast enough and we are about to enter the _create function,
1812 * we unlock it so that we exit with FLUSHING immediately. We then
1813 * check the EOS flag and do the EOS logic.
1815 GST_OBJECT_LOCK (src);
1816 g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1817 if (src->priv->pending_eos)
1818 gst_event_unref (src->priv->pending_eos);
1819 src->priv->pending_eos = event;
1821 GST_OBJECT_UNLOCK (src);
1823 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1825 /* unlock the _create function so that we can check the has_pending_eos flag
1826 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1827 * that we can grab it and stop the unlock again. We don't take the stream
1828 * lock so that this operation is guaranteed to never block. */
1829 gst_base_src_activate_pool (src, FALSE);
1831 bclass->unlock (src);
1833 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1835 GST_LIVE_LOCK (src);
1836 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1837 /* now stop the unlock of the streaming thread again. Grabbing the live
1838 * lock is enough because that protects the create function. */
1839 if (bclass->unlock_stop)
1840 bclass->unlock_stop (src);
1841 gst_base_src_activate_pool (src, TRUE);
1842 GST_LIVE_UNLOCK (src);
1847 case GST_EVENT_SEGMENT:
1848 /* sending random SEGMENT downstream can break sync. */
1851 case GST_EVENT_CUSTOM_DOWNSTREAM:
1852 case GST_EVENT_CUSTOM_BOTH:
1853 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1854 GST_OBJECT_LOCK (src);
1855 src->priv->pending_events =
1856 g_list_append (src->priv->pending_events, event);
1857 g_atomic_int_set (&src->priv->have_events, TRUE);
1858 GST_OBJECT_UNLOCK (src);
1862 case GST_EVENT_BUFFERSIZE:
1863 /* does not seem to make much sense currently */
1866 /* upstream events */
1868 /* elements should override send_event and do something */
1870 case GST_EVENT_SEEK:
1874 GST_OBJECT_LOCK (src->srcpad);
1875 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1877 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1878 GST_OBJECT_UNLOCK (src->srcpad);
1881 GST_DEBUG_OBJECT (src, "performing seek");
1882 /* when we are running in push mode, we can execute the
1883 * seek right now. */
1884 result = gst_base_src_perform_seek (src, event, TRUE);
1888 /* else we store the event and execute the seek when we
1890 GST_OBJECT_LOCK (src);
1891 GST_DEBUG_OBJECT (src, "queueing seek");
1892 event_p = &src->pending_seek;
1893 gst_event_replace ((GstEvent **) event_p, event);
1894 GST_OBJECT_UNLOCK (src);
1895 /* assume the seek will work */
1900 case GST_EVENT_NAVIGATION:
1901 /* could make sense for elements that do something with navigation events
1902 * but then they would need to override the send_event function */
1904 case GST_EVENT_LATENCY:
1905 /* does not seem to make sense currently */
1909 case GST_EVENT_CUSTOM_UPSTREAM:
1910 /* override send_event if you want this */
1912 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1913 case GST_EVENT_CUSTOM_BOTH_OOB:
1914 /* insert a random custom event into the pipeline */
1915 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1916 result = gst_pad_push_event (src->srcpad, event);
1917 /* we gave away the ref to the event in the push */
1924 /* if we still have a ref to the event, unref it now */
1926 gst_event_unref (event);
1933 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1934 GST_OBJECT_UNLOCK (src->srcpad);
1941 gst_base_src_seekable (GstBaseSrc * src)
1943 GstBaseSrcClass *bclass;
1944 bclass = GST_BASE_SRC_GET_CLASS (src);
1945 if (bclass->is_seekable)
1946 return bclass->is_seekable (src);
1952 gst_base_src_update_qos (GstBaseSrc * src,
1953 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1955 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1956 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1957 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1959 GST_OBJECT_LOCK (src);
1960 src->priv->proportion = proportion;
1961 src->priv->earliest_time = timestamp + diff;
1962 GST_OBJECT_UNLOCK (src);
1967 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1971 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1973 switch (GST_EVENT_TYPE (event)) {
1974 case GST_EVENT_SEEK:
1975 /* is normally called when in push mode */
1976 if (!gst_base_src_seekable (src))
1979 result = gst_base_src_perform_seek (src, event, TRUE);
1981 case GST_EVENT_FLUSH_START:
1982 /* cancel any blocking getrange, is normally called
1983 * when in pull mode. */
1984 result = gst_base_src_set_flushing (src, TRUE, FALSE, NULL);
1986 case GST_EVENT_FLUSH_STOP:
1987 result = gst_base_src_set_flushing (src, FALSE, TRUE, NULL);
1992 GstClockTimeDiff diff;
1993 GstClockTime timestamp;
1995 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1996 gst_base_src_update_qos (src, proportion, diff, timestamp);
2000 case GST_EVENT_RECONFIGURE:
2003 case GST_EVENT_LATENCY:
2015 GST_DEBUG_OBJECT (src, "is not seekable");
2021 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2024 GstBaseSrcClass *bclass;
2025 gboolean result = FALSE;
2027 src = GST_BASE_SRC (parent);
2028 bclass = GST_BASE_SRC_GET_CLASS (src);
2030 if (bclass->event) {
2031 if (!(result = bclass->event (src, event)))
2032 goto subclass_failed;
2036 gst_event_unref (event);
2043 GST_DEBUG_OBJECT (src, "subclass refused event");
2049 gst_base_src_set_property (GObject * object, guint prop_id,
2050 const GValue * value, GParamSpec * pspec)
2054 src = GST_BASE_SRC (object);
2057 case PROP_BLOCKSIZE:
2058 gst_base_src_set_blocksize (src, g_value_get_uint (value));
2060 case PROP_NUM_BUFFERS:
2061 src->num_buffers = g_value_get_int (value);
2064 src->typefind = g_value_get_boolean (value);
2066 case PROP_DO_TIMESTAMP:
2067 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2070 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2076 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2081 src = GST_BASE_SRC (object);
2084 case PROP_BLOCKSIZE:
2085 g_value_set_uint (value, gst_base_src_get_blocksize (src));
2087 case PROP_NUM_BUFFERS:
2088 g_value_set_int (value, src->num_buffers);
2091 g_value_set_boolean (value, src->typefind);
2093 case PROP_DO_TIMESTAMP:
2094 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2097 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2102 /* with STREAM_LOCK and LOCK */
2103 static GstClockReturn
2104 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2109 id = gst_clock_new_single_shot_id (clock, time);
2111 basesrc->clock_id = id;
2112 /* release the live lock while waiting */
2113 GST_LIVE_UNLOCK (basesrc);
2115 ret = gst_clock_id_wait (id, NULL);
2117 GST_LIVE_LOCK (basesrc);
2118 gst_clock_id_unref (id);
2119 basesrc->clock_id = NULL;
2124 /* perform synchronisation on a buffer.
2127 static GstClockReturn
2128 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2130 GstClockReturn result;
2131 GstClockTime start, end;
2132 GstBaseSrcClass *bclass;
2133 GstClockTime base_time;
2135 GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2136 gboolean do_timestamp, first, pseudo_live, is_live;
2138 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2141 if (bclass->get_times)
2142 bclass->get_times (basesrc, buffer, &start, &end);
2144 /* get buffer timestamp */
2145 dts = GST_BUFFER_DTS (buffer);
2146 pts = GST_BUFFER_PTS (buffer);
2148 if (GST_CLOCK_TIME_IS_VALID (dts))
2153 /* grab the lock to prepare for clocking and calculate the startup
2155 GST_OBJECT_LOCK (basesrc);
2157 is_live = basesrc->is_live;
2158 /* if we are asked to sync against the clock we are a pseudo live element */
2159 pseudo_live = (start != -1 && is_live);
2160 /* check for the first buffer */
2161 first = (basesrc->priv->latency == -1);
2163 if (timestamp != -1 && pseudo_live) {
2164 GstClockTime latency;
2166 /* we have a timestamp and a sync time, latency is the diff */
2167 if (timestamp <= start)
2168 latency = start - timestamp;
2173 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2174 GST_TIME_ARGS (latency));
2175 /* first time we calculate latency, just configure */
2176 basesrc->priv->latency = latency;
2178 if (basesrc->priv->latency != latency) {
2179 /* we have a new latency, FIXME post latency message */
2180 basesrc->priv->latency = latency;
2181 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2182 GST_TIME_ARGS (latency));
2186 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2187 is_live, start != -1);
2188 basesrc->priv->latency = 0;
2191 /* get clock, if no clock, we can't sync or do timestamps */
2192 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2195 gst_object_ref (clock);
2197 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2199 do_timestamp = basesrc->priv->do_timestamp;
2200 GST_OBJECT_UNLOCK (basesrc);
2202 /* first buffer, calculate the timestamp offset */
2204 GstClockTime running_time;
2206 now = gst_clock_get_time (clock);
2207 running_time = now - base_time;
2209 GST_LOG_OBJECT (basesrc,
2210 "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2211 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2212 GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2214 if (pseudo_live && timestamp != -1) {
2215 /* live source and we need to sync, add startup latency to all timestamps
2216 * to get the real running_time. Live sources should always timestamp
2217 * according to the current running time. */
2218 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2220 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2221 GST_TIME_ARGS (basesrc->priv->ts_offset));
2223 basesrc->priv->ts_offset = 0;
2224 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2227 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2231 if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2232 dts = basesrc->segment.start;
2237 GST_BUFFER_DTS (buffer) = dts;
2239 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2240 GST_TIME_ARGS (dts));
2243 /* not the first buffer, the timestamp is the diff between the clock and
2245 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2246 now = gst_clock_get_time (clock);
2248 dts = now - base_time;
2249 GST_BUFFER_DTS (buffer) = dts;
2251 GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2252 GST_TIME_ARGS (dts));
2255 if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2256 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2259 GST_BUFFER_PTS (buffer) = dts;
2261 GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2262 GST_TIME_ARGS (pts));
2265 /* if we don't have a buffer timestamp, we don't sync */
2266 if (!GST_CLOCK_TIME_IS_VALID (start))
2270 /* for pseudo live sources, add our ts_offset to the timestamp */
2271 if (GST_CLOCK_TIME_IS_VALID (pts))
2272 GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2273 if (GST_CLOCK_TIME_IS_VALID (dts))
2274 GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2275 start += basesrc->priv->ts_offset;
2278 GST_LOG_OBJECT (basesrc,
2279 "waiting for clock, base time %" GST_TIME_FORMAT
2280 ", stream_start %" GST_TIME_FORMAT,
2281 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2283 result = gst_base_src_wait (basesrc, clock, start + base_time);
2285 gst_object_unref (clock);
2287 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2294 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2295 GST_OBJECT_UNLOCK (basesrc);
2296 return GST_CLOCK_OK;
2300 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2301 gst_object_unref (clock);
2302 return GST_CLOCK_OK;
2306 /* Called with STREAM_LOCK and LIVE_LOCK */
2308 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2311 guint64 size, maxsize;
2312 GstBaseSrcClass *bclass;
2316 bclass = GST_BASE_SRC_GET_CLASS (src);
2318 format = src->segment.format;
2319 stop = src->segment.stop;
2320 /* get total file size */
2321 size = src->segment.duration;
2323 /* only operate if we are working with bytes */
2324 if (format != GST_FORMAT_BYTES)
2327 /* when not doing automatic EOS, just use the stop position. We don't use
2328 * the size to check for EOS */
2329 if (!g_atomic_int_get (&src->priv->automatic_eos))
2331 /* Otherwise, the max amount of bytes to read is the total
2332 * size or up to the segment.stop if present. */
2333 else if (stop != -1)
2334 maxsize = size != -1 ? MIN (size, stop) : stop;
2338 GST_DEBUG_OBJECT (src,
2339 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2340 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2341 *length, size, stop, maxsize);
2343 /* check size if we have one */
2344 if (maxsize != -1) {
2345 /* if we run past the end, check if the file became bigger and
2346 * retry. Mind wrap when checking. */
2347 if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2348 /* see if length of the file changed */
2349 if (bclass->get_size)
2350 if (!bclass->get_size (src, &size))
2353 /* make sure we don't exceed the configured segment stop
2356 maxsize = MIN (size, stop);
2360 /* if we are at or past the end, EOS */
2361 if (G_UNLIKELY (offset >= maxsize))
2362 goto unexpected_length;
2364 /* else we can clip to the end */
2365 if (G_UNLIKELY (offset + *length >= maxsize))
2366 *length = maxsize - offset;
2371 /* keep track of current duration. segment is in bytes, we checked
2373 GST_OBJECT_LOCK (src);
2374 src->segment.duration = size;
2375 GST_OBJECT_UNLOCK (src);
2386 /* must be called with LIVE_LOCK */
2387 static GstFlowReturn
2388 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2392 GstBaseSrcClass *bclass;
2393 GstClockReturn status;
2397 bclass = GST_BASE_SRC_GET_CLASS (src);
2401 if (G_UNLIKELY (!src->live_running)) {
2402 ret = gst_base_src_wait_playing (src);
2403 if (ret != GST_FLOW_OK)
2408 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2409 && !GST_BASE_SRC_IS_STARTING (src)))
2412 if (G_UNLIKELY (!bclass->create))
2415 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2416 goto unexpected_length;
2418 /* track position */
2419 GST_OBJECT_LOCK (src);
2420 if (src->segment.format == GST_FORMAT_BYTES)
2421 src->segment.position = offset;
2422 GST_OBJECT_UNLOCK (src);
2424 /* normally we don't count buffers */
2425 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2426 if (src->num_buffers_left == 0)
2427 goto reached_num_buffers;
2429 src->num_buffers_left--;
2432 /* don't enter the create function if a pending EOS event was set. For the
2433 * logic of the has_pending_eos, check the event function of this class. */
2434 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2435 src->priv->forced_eos = TRUE;
2439 GST_DEBUG_OBJECT (src,
2440 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2441 G_GINT64_FORMAT, offset, length, src->segment.time);
2443 res_buf = in_buf = *buf;
2445 ret = bclass->create (src, offset, length, &res_buf);
2447 /* The create function could be unlocked because we have a pending EOS. It's
2448 * possible that we have a valid buffer from create that we need to
2449 * discard when the create function returned _OK. */
2450 if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2451 if (ret == GST_FLOW_OK) {
2453 gst_buffer_unref (res_buf);
2455 src->priv->forced_eos = TRUE;
2459 if (G_UNLIKELY (ret != GST_FLOW_OK))
2462 /* fallback in case the create function didn't fill a provided buffer */
2463 if (in_buf != NULL && res_buf != in_buf) {
2467 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2468 "fill the provided buffer, copying");
2470 if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2473 copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2474 gst_buffer_unmap (in_buf, &info);
2475 gst_buffer_set_size (in_buf, copied_size);
2477 gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2479 gst_buffer_unref (res_buf);
2483 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2484 if (offset == 0 && src->segment.time == 0
2485 && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2486 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2487 res_buf = gst_buffer_make_writable (res_buf);
2488 GST_BUFFER_DTS (res_buf) = 0;
2491 /* now sync before pushing the buffer */
2492 status = gst_base_src_do_sync (src, res_buf);
2494 /* waiting for the clock could have made us flushing */
2495 if (G_UNLIKELY (src->priv->flushing))
2499 case GST_CLOCK_EARLY:
2500 /* the buffer is too late. We currently don't drop the buffer. */
2501 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2504 /* buffer synchronised properly */
2505 GST_DEBUG_OBJECT (src, "buffer ok");
2507 case GST_CLOCK_UNSCHEDULED:
2508 /* this case is triggered when we were waiting for the clock and
2509 * it got unlocked because we did a state change. In any case, get rid of
2512 gst_buffer_unref (res_buf);
2514 if (!src->live_running) {
2515 /* We return FLUSHING when we are not running to stop the dataflow also
2516 * get rid of the produced buffer. */
2517 GST_DEBUG_OBJECT (src,
2518 "clock was unscheduled (%d), returning FLUSHING", status);
2519 ret = GST_FLOW_FLUSHING;
2521 /* If we are running when this happens, we quickly switched between
2522 * pause and playing. We try to produce a new buffer */
2523 GST_DEBUG_OBJECT (src,
2524 "clock was unscheduled (%d), but we are running", status);
2529 /* all other result values are unexpected and errors */
2530 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2531 (_("Internal clock error.")),
2532 ("clock returned unexpected return value %d", status));
2534 gst_buffer_unref (res_buf);
2535 ret = GST_FLOW_ERROR;
2538 if (G_LIKELY (ret == GST_FLOW_OK))
2546 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2547 gst_flow_get_name (ret));
2552 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2553 gst_flow_get_name (ret));
2558 GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2559 (_("Failed to map buffer.")),
2560 ("failed to map result buffer in WRITE mode"));
2562 gst_buffer_unref (res_buf);
2563 return GST_FLOW_ERROR;
2567 GST_DEBUG_OBJECT (src, "getrange but not started");
2568 return GST_FLOW_FLUSHING;
2572 GST_DEBUG_OBJECT (src, "no create function");
2573 return GST_FLOW_NOT_SUPPORTED;
2577 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2578 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2579 return GST_FLOW_EOS;
2581 reached_num_buffers:
2583 GST_DEBUG_OBJECT (src, "sent all buffers");
2584 return GST_FLOW_EOS;
2588 GST_DEBUG_OBJECT (src, "we are flushing");
2590 gst_buffer_unref (res_buf);
2591 return GST_FLOW_FLUSHING;
2595 GST_DEBUG_OBJECT (src, "we are EOS");
2596 return GST_FLOW_EOS;
2600 static GstFlowReturn
2601 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2602 guint length, GstBuffer ** buf)
2607 src = GST_BASE_SRC_CAST (parent);
2609 GST_LIVE_LOCK (src);
2610 if (G_UNLIKELY (src->priv->flushing))
2613 res = gst_base_src_get_range (src, offset, length, buf);
2616 GST_LIVE_UNLOCK (src);
2623 GST_DEBUG_OBJECT (src, "we are flushing");
2624 res = GST_FLOW_FLUSHING;
2630 gst_base_src_is_random_access (GstBaseSrc * src)
2632 /* we need to start the basesrc to check random access */
2633 if (!GST_BASE_SRC_IS_STARTED (src)) {
2634 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2635 if (G_LIKELY (gst_base_src_start (src))) {
2636 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2638 gst_base_src_stop (src);
2642 return src->random_access;
2647 GST_DEBUG_OBJECT (src, "failed to start");
2653 gst_base_src_loop (GstPad * pad)
2656 GstBuffer *buf = NULL;
2661 GList *pending_events = NULL, *tmp;
2665 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2667 /* Just leave immediately if we're flushing */
2668 GST_LIVE_LOCK (src);
2669 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2671 GST_LIVE_UNLOCK (src);
2673 gst_base_src_send_stream_start (src);
2675 /* The stream-start event could've caused something to flush us */
2676 GST_LIVE_LOCK (src);
2677 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2679 GST_LIVE_UNLOCK (src);
2681 /* check if we need to renegotiate */
2682 if (gst_pad_check_reconfigure (pad)) {
2683 if (!gst_base_src_negotiate (src)) {
2684 gst_pad_mark_reconfigure (pad);
2685 if (GST_PAD_IS_FLUSHING (pad)) {
2686 GST_LIVE_LOCK (src);
2689 goto negotiate_failed;
2694 GST_LIVE_LOCK (src);
2696 if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2699 blocksize = src->blocksize;
2701 /* if we operate in bytes, we can calculate an offset */
2702 if (src->segment.format == GST_FORMAT_BYTES) {
2703 position = src->segment.position;
2704 /* for negative rates, start with subtracting the blocksize */
2705 if (src->segment.rate < 0.0) {
2706 /* we cannot go below segment.start */
2707 if (position > src->segment.start + blocksize)
2708 position -= blocksize;
2710 /* last block, remainder up to segment.start */
2711 blocksize = position - src->segment.start;
2712 position = src->segment.start;
2718 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2719 GST_TIME_ARGS (position), blocksize);
2721 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2722 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2723 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2724 gst_flow_get_name (ret));
2725 GST_LIVE_UNLOCK (src);
2728 /* this should not happen */
2729 if (G_UNLIKELY (buf == NULL))
2732 /* push events to close/start our segment before we push the buffer. */
2733 if (G_UNLIKELY (src->priv->segment_pending)) {
2734 GstEvent *seg_event = gst_event_new_segment (&src->segment);
2736 gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2737 src->priv->segment_seqnum = gst_util_seqnum_next ();
2738 gst_pad_push_event (pad, seg_event);
2739 src->priv->segment_pending = FALSE;
2742 if (g_atomic_int_get (&src->priv->have_events)) {
2743 GST_OBJECT_LOCK (src);
2744 /* take the events */
2745 pending_events = src->priv->pending_events;
2746 src->priv->pending_events = NULL;
2747 g_atomic_int_set (&src->priv->have_events, FALSE);
2748 GST_OBJECT_UNLOCK (src);
2751 /* Push out pending events if any */
2752 if (G_UNLIKELY (pending_events != NULL)) {
2753 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2754 GstEvent *ev = (GstEvent *) tmp->data;
2755 gst_pad_push_event (pad, ev);
2757 g_list_free (pending_events);
2760 /* figure out the new position */
2761 switch (src->segment.format) {
2762 case GST_FORMAT_BYTES:
2764 guint bufsize = gst_buffer_get_size (buf);
2766 /* we subtracted above for negative rates */
2767 if (src->segment.rate >= 0.0)
2768 position += bufsize;
2771 case GST_FORMAT_TIME:
2773 GstClockTime start, duration;
2775 start = GST_BUFFER_TIMESTAMP (buf);
2776 duration = GST_BUFFER_DURATION (buf);
2778 if (GST_CLOCK_TIME_IS_VALID (start))
2781 position = src->segment.position;
2783 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2784 if (src->segment.rate >= 0.0)
2785 position += duration;
2786 else if (position > duration)
2787 position -= duration;
2793 case GST_FORMAT_DEFAULT:
2794 if (src->segment.rate >= 0.0)
2795 position = GST_BUFFER_OFFSET_END (buf);
2797 position = GST_BUFFER_OFFSET (buf);
2803 if (position != -1) {
2804 if (src->segment.rate >= 0.0) {
2805 /* positive rate, check if we reached the stop */
2806 if (src->segment.stop != -1) {
2807 if (position >= src->segment.stop) {
2809 position = src->segment.stop;
2813 /* negative rate, check if we reached the start. start is always set to
2814 * something different from -1 */
2815 if (position <= src->segment.start) {
2817 position = src->segment.start;
2819 /* when going reverse, all buffers are DISCONT */
2820 src->priv->discont = TRUE;
2822 GST_OBJECT_LOCK (src);
2823 src->segment.position = position;
2824 GST_OBJECT_UNLOCK (src);
2827 if (G_UNLIKELY (src->priv->discont)) {
2828 GST_INFO_OBJECT (src, "marking pending DISCONT");
2829 buf = gst_buffer_make_writable (buf);
2830 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2831 src->priv->discont = FALSE;
2833 GST_LIVE_UNLOCK (src);
2835 ret = gst_pad_push (pad, buf);
2836 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2837 if (ret == GST_FLOW_NOT_NEGOTIATED) {
2838 goto not_negotiated;
2840 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2841 gst_flow_get_name (ret));
2845 /* Segment pending means that a new segment was configured
2846 * during this loop run */
2847 if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2848 GST_INFO_OBJECT (src, "pausing after end of segment");
2859 if (gst_pad_needs_reconfigure (pad)) {
2860 GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2863 /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2864 * a pending negotiation request on our srcpad */
2868 GST_DEBUG_OBJECT (src, "Not negotiated");
2869 ret = GST_FLOW_NOT_NEGOTIATED;
2874 GST_DEBUG_OBJECT (src, "we are flushing");
2875 GST_LIVE_UNLOCK (src);
2876 ret = GST_FLOW_FLUSHING;
2881 const gchar *reason = gst_flow_get_name (ret);
2884 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2885 src->running = FALSE;
2886 gst_pad_pause_task (pad);
2887 if (ret == GST_FLOW_EOS) {
2888 gboolean flag_segment;
2892 flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2893 format = src->segment.format;
2894 position = src->segment.position;
2896 /* perform EOS logic */
2897 if (src->priv->forced_eos) {
2898 g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
2899 GST_OBJECT_LOCK (src);
2900 event = src->priv->pending_eos;
2901 src->priv->pending_eos = NULL;
2902 GST_OBJECT_UNLOCK (src);
2904 } else if (flag_segment) {
2905 GstMessage *message;
2907 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2909 gst_message_set_seqnum (message, src->priv->seqnum);
2910 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2911 event = gst_event_new_segment_done (format, position);
2912 gst_event_set_seqnum (event, src->priv->seqnum);
2915 event = gst_event_new_eos ();
2916 gst_event_set_seqnum (event, src->priv->seqnum);
2919 gst_pad_push_event (pad, event);
2920 src->priv->forced_eos = FALSE;
2922 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2923 event = gst_event_new_eos ();
2924 gst_event_set_seqnum (event, src->priv->seqnum);
2925 /* for fatal errors we post an error message, post the error
2926 * first so the app knows about the error first.
2927 * Also don't do this for FLUSHING because it happens
2928 * due to flushing and posting an error message because of
2929 * that is the wrong thing to do, e.g. when we're doing
2930 * a flushing seek. */
2931 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2932 (_("Internal data flow error.")),
2933 ("streaming task paused, reason %s (%d)", reason, ret));
2934 gst_pad_push_event (pad, event);
2940 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2941 (_("Internal data flow error.")), ("element returned NULL buffer"));
2942 GST_LIVE_UNLOCK (src);
2948 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2949 GstAllocator * allocator, GstAllocationParams * params)
2951 GstAllocator *oldalloc;
2952 GstBufferPool *oldpool;
2953 GstBaseSrcPrivate *priv = basesrc->priv;
2956 GST_DEBUG_OBJECT (basesrc, "activate pool");
2957 if (!gst_buffer_pool_set_active (pool, TRUE))
2958 goto activate_failed;
2961 GST_OBJECT_LOCK (basesrc);
2962 oldpool = priv->pool;
2965 oldalloc = priv->allocator;
2966 priv->allocator = allocator;
2969 priv->params = *params;
2971 gst_allocation_params_init (&priv->params);
2972 GST_OBJECT_UNLOCK (basesrc);
2975 /* only deactivate if the pool is not the one we're using */
2976 if (oldpool != pool) {
2977 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2978 gst_buffer_pool_set_active (oldpool, FALSE);
2980 gst_object_unref (oldpool);
2983 gst_object_unref (oldalloc);
2990 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2996 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2998 GstBaseSrcPrivate *priv = basesrc->priv;
2999 GstBufferPool *pool;
3000 gboolean res = TRUE;
3002 GST_OBJECT_LOCK (basesrc);
3003 if ((pool = priv->pool))
3004 pool = gst_object_ref (pool);
3005 GST_OBJECT_UNLOCK (basesrc);
3008 res = gst_buffer_pool_set_active (pool, active);
3009 gst_object_unref (pool);
3016 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3019 GstBufferPool *pool;
3020 guint size, min, max;
3021 GstAllocator *allocator;
3022 GstAllocationParams params;
3023 GstStructure *config;
3024 gboolean update_allocator;
3026 gst_query_parse_allocation (query, &outcaps, NULL);
3028 /* we got configuration from our peer or the decide_allocation method,
3030 if (gst_query_get_n_allocation_params (query) > 0) {
3031 /* try the allocator */
3032 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3033 update_allocator = TRUE;
3036 gst_allocation_params_init (¶ms);
3037 update_allocator = FALSE;
3040 if (gst_query_get_n_allocation_pools (query) > 0) {
3041 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3044 /* no pool, we can make our own */
3045 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3046 pool = gst_buffer_pool_new ();
3050 size = min = max = 0;
3055 config = gst_buffer_pool_get_config (pool);
3056 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3057 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
3058 gst_buffer_pool_set_config (pool, config);
3061 if (update_allocator)
3062 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
3064 gst_query_add_allocation_param (query, allocator, ¶ms);
3066 gst_object_unref (allocator);
3069 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3070 gst_object_unref (pool);
3077 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3079 GstBaseSrcClass *bclass;
3080 gboolean result = TRUE;
3082 GstBufferPool *pool = NULL;
3083 GstAllocator *allocator = NULL;
3084 GstAllocationParams params;
3086 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3088 /* make query and let peer pad answer, we don't really care if it worked or
3089 * not, if it failed, the allocation query would contain defaults and the
3090 * subclass would then set better values if needed */
3091 query = gst_query_new_allocation (caps, TRUE);
3092 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3093 /* not a problem, just debug a little */
3094 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3097 g_assert (bclass->decide_allocation != NULL);
3098 result = bclass->decide_allocation (basesrc, query);
3100 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3104 goto no_decide_allocation;
3106 /* we got configuration from our peer or the decide_allocation method,
3108 if (gst_query_get_n_allocation_params (query) > 0) {
3109 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
3112 gst_allocation_params_init (¶ms);
3115 if (gst_query_get_n_allocation_pools (query) > 0)
3116 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3118 result = gst_base_src_set_allocation (basesrc, pool, allocator, ¶ms);
3120 gst_query_unref (query);
3125 no_decide_allocation:
3127 GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3128 gst_query_unref (query);
3134 /* default negotiation code.
3136 * Take intersection between src and sink pads, take first
3140 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3143 GstCaps *caps = NULL;
3144 GstCaps *peercaps = NULL;
3145 gboolean result = FALSE;
3147 /* first see what is possible on our source pad */
3148 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3149 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3150 /* nothing or anything is allowed, we're done */
3151 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3152 goto no_nego_needed;
3154 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3157 /* get the peer caps */
3158 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3159 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3161 /* The result is already a subset of our caps */
3163 gst_caps_unref (thiscaps);
3165 /* no peer, work with our own caps then */
3168 if (caps && !gst_caps_is_empty (caps)) {
3170 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3171 if (gst_caps_is_any (caps)) {
3172 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3173 /* hmm, still anything, so element can do anything and
3174 * nego is not needed */
3177 caps = gst_base_src_fixate (basesrc, caps);
3178 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3179 if (gst_caps_is_fixed (caps)) {
3180 /* yay, fixed caps, use those then, it's possible that the subclass does
3181 * not accept this caps after all and we have to fail. */
3182 result = gst_base_src_set_caps (basesrc, caps);
3185 gst_caps_unref (caps);
3188 gst_caps_unref (caps);
3189 GST_DEBUG_OBJECT (basesrc, "no common caps");
3195 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3197 gst_caps_unref (thiscaps);
3202 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3203 ("No supported formats found"),
3204 ("This element did not produce valid caps"));
3206 gst_caps_unref (thiscaps);
3212 gst_base_src_negotiate (GstBaseSrc * basesrc)
3214 GstBaseSrcClass *bclass;
3217 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3219 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3221 if (G_LIKELY (bclass->negotiate))
3222 result = bclass->negotiate (basesrc);
3226 if (G_LIKELY (result)) {
3229 caps = gst_pad_get_current_caps (basesrc->srcpad);
3231 result = gst_base_src_prepare_allocation (basesrc, caps);
3234 gst_caps_unref (caps);
3240 gst_base_src_start (GstBaseSrc * basesrc)
3242 GstBaseSrcClass *bclass;
3245 GST_LIVE_LOCK (basesrc);
3247 GST_OBJECT_LOCK (basesrc);
3248 if (GST_BASE_SRC_IS_STARTING (basesrc))
3250 if (GST_BASE_SRC_IS_STARTED (basesrc))
3253 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3254 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3255 gst_segment_init (&basesrc->segment, basesrc->segment.format);
3256 GST_OBJECT_UNLOCK (basesrc);
3258 basesrc->num_buffers_left = basesrc->num_buffers;
3259 basesrc->running = FALSE;
3260 basesrc->priv->segment_pending = FALSE;
3261 basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3262 basesrc->priv->forced_eos = FALSE;
3263 GST_LIVE_UNLOCK (basesrc);
3265 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3267 result = bclass->start (basesrc);
3272 goto could_not_start;
3274 if (!gst_base_src_is_async (basesrc)) {
3275 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3276 /* not really waiting here, we call this to get the result
3277 * from the start_complete call */
3278 result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3286 GST_DEBUG_OBJECT (basesrc, "was starting");
3287 GST_OBJECT_UNLOCK (basesrc);
3288 GST_LIVE_UNLOCK (basesrc);
3293 GST_DEBUG_OBJECT (basesrc, "was started");
3294 GST_OBJECT_UNLOCK (basesrc);
3295 GST_LIVE_UNLOCK (basesrc);
3300 GST_DEBUG_OBJECT (basesrc, "could not start");
3301 /* subclass is supposed to post a message. We don't have to call _stop. */
3302 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3308 * gst_base_src_start_complete:
3309 * @basesrc: base source instance
3310 * @ret: a #GstFlowReturn
3312 * Complete an asynchronous start operation. When the subclass overrides the
3313 * start method, it should call gst_base_src_start_complete() when the start
3314 * operation completes either from the same thread or from an asynchronous
3318 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3327 if (ret != GST_FLOW_OK)
3330 GST_DEBUG_OBJECT (basesrc, "starting source");
3331 format = basesrc->segment.format;
3333 /* figure out the size */
3336 if (format == GST_FORMAT_BYTES) {
3337 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3339 if (bclass->get_size) {
3340 if (!(have_size = bclass->get_size (basesrc, &size)))
3343 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3344 /* only update the size when operating in bytes, subclass is supposed
3345 * to set duration in the start method for other formats */
3346 GST_OBJECT_LOCK (basesrc);
3347 basesrc->segment.duration = size;
3348 GST_OBJECT_UNLOCK (basesrc);
3351 GST_DEBUG_OBJECT (basesrc,
3352 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3353 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3354 basesrc->segment.duration);
3356 seekable = gst_base_src_seekable (basesrc);
3357 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3359 /* update for random access flag */
3360 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3362 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3364 /* stop flushing now but for live sources, still block in the LIVE lock when
3365 * we are not yet PLAYING */
3366 gst_base_src_set_flushing (basesrc, FALSE, FALSE, NULL);
3368 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3370 GST_OBJECT_LOCK (basesrc->srcpad);
3371 mode = GST_PAD_MODE (basesrc->srcpad);
3372 GST_OBJECT_UNLOCK (basesrc->srcpad);
3374 /* take the stream lock here, we only want to let the task run when we have
3375 * set the STARTED flag */
3376 GST_PAD_STREAM_LOCK (basesrc->srcpad);
3378 case GST_PAD_MODE_PUSH:
3379 /* do initial seek, which will start the task */
3380 GST_OBJECT_LOCK (basesrc);
3381 event = basesrc->pending_seek;
3382 basesrc->pending_seek = NULL;
3383 GST_OBJECT_UNLOCK (basesrc);
3385 /* The perform seek code will start the task when finished. We don't have to
3386 * unlock the streaming thread because it is not running yet */
3387 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3391 gst_event_unref (event);
3393 case GST_PAD_MODE_PULL:
3394 /* if not random_access, we cannot operate in pull mode for now */
3395 if (G_UNLIKELY (!basesrc->random_access))
3399 goto not_activated_yet;
3403 GST_OBJECT_LOCK (basesrc);
3404 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3405 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3406 basesrc->priv->start_result = ret;
3407 GST_ASYNC_SIGNAL (basesrc);
3408 GST_OBJECT_UNLOCK (basesrc);
3410 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3416 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3417 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3418 gst_base_src_stop (basesrc);
3420 gst_event_unref (event);
3421 ret = GST_FLOW_ERROR;
3426 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3427 gst_base_src_stop (basesrc);
3428 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3429 ret = GST_FLOW_ERROR;
3434 GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3435 gst_base_src_stop (basesrc);
3436 GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3437 ret = GST_FLOW_ERROR;
3442 GST_OBJECT_LOCK (basesrc);
3443 basesrc->priv->start_result = ret;
3444 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3445 GST_ASYNC_SIGNAL (basesrc);
3446 GST_OBJECT_UNLOCK (basesrc);
3452 * gst_base_src_start_wait:
3453 * @basesrc: base source instance
3455 * Wait until the start operation completes.
3457 * Returns: a #GstFlowReturn.
3460 gst_base_src_start_wait (GstBaseSrc * basesrc)
3462 GstFlowReturn result;
3464 GST_OBJECT_LOCK (basesrc);
3465 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3466 GST_ASYNC_WAIT (basesrc);
3468 result = basesrc->priv->start_result;
3469 GST_OBJECT_UNLOCK (basesrc);
3471 GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3477 gst_base_src_stop (GstBaseSrc * basesrc)
3479 GstBaseSrcClass *bclass;
3480 gboolean result = TRUE;
3482 GST_DEBUG_OBJECT (basesrc, "stopping source");
3485 gst_base_src_set_flushing (basesrc, TRUE, FALSE, NULL);
3487 gst_pad_stop_task (basesrc->srcpad);
3489 GST_OBJECT_LOCK (basesrc);
3490 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3493 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3494 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3495 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3496 GST_ASYNC_SIGNAL (basesrc);
3497 GST_OBJECT_UNLOCK (basesrc);
3499 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3501 result = bclass->stop (basesrc);
3503 gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3509 GST_DEBUG_OBJECT (basesrc, "was stopped");
3510 GST_OBJECT_UNLOCK (basesrc);
3515 /* start or stop flushing dataprocessing
3518 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3519 gboolean flushing, gboolean live_play, gboolean * playing)
3521 GstBaseSrcClass *bclass;
3523 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3525 GST_DEBUG_OBJECT (basesrc, "flushing %d, live_play %d", flushing, live_play);
3528 gst_base_src_activate_pool (basesrc, FALSE);
3529 /* unlock any subclasses, we need to do this before grabbing the
3530 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3531 * unlock to the params because of backwards compat (see seek handler)*/
3533 bclass->unlock (basesrc);
3536 /* the live lock is released when we are blocked, waiting for playing or
3537 * when we sync to the clock. */
3538 GST_LIVE_LOCK (basesrc);
3540 *playing = basesrc->live_running;
3541 basesrc->priv->flushing = flushing;
3543 /* if we are locked in the live lock, signal it to make it flush */
3544 basesrc->live_running = TRUE;
3546 /* clear pending EOS if any */
3547 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3548 GST_OBJECT_LOCK (basesrc);
3549 CLEAR_PENDING_EOS (basesrc);
3550 basesrc->priv->forced_eos = FALSE;
3551 GST_OBJECT_UNLOCK (basesrc);
3554 /* step 1, now that we have the LIVE lock, clear our unlock request */
3555 if (bclass->unlock_stop)
3556 bclass->unlock_stop (basesrc);
3558 /* step 2, unblock clock sync (if any) or any other blocking thing */
3559 if (basesrc->clock_id)
3560 gst_clock_id_unschedule (basesrc->clock_id);
3562 /* signal the live source that it can start playing */
3563 basesrc->live_running = live_play;
3565 gst_base_src_activate_pool (basesrc, TRUE);
3567 /* Drop all delayed events */
3568 GST_OBJECT_LOCK (basesrc);
3569 if (basesrc->priv->pending_events) {
3570 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3572 g_list_free (basesrc->priv->pending_events);
3573 basesrc->priv->pending_events = NULL;
3574 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3576 GST_OBJECT_UNLOCK (basesrc);
3578 GST_LIVE_SIGNAL (basesrc);
3579 GST_LIVE_UNLOCK (basesrc);
3584 /* the purpose of this function is to make sure that a live source blocks in the
3585 * LIVE lock or leaves the LIVE lock and continues playing. */
3587 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3589 GstBaseSrcClass *bclass;
3591 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3593 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3595 GST_DEBUG_OBJECT (basesrc, "unlock");
3597 bclass->unlock (basesrc);
3600 /* we are now able to grab the LIVE lock, when we get it, we can be
3601 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3603 GST_LIVE_LOCK (basesrc);
3604 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3606 /* unblock clock sync (if any) */
3607 if (basesrc->clock_id)
3608 gst_clock_id_unschedule (basesrc->clock_id);
3610 /* configure what to do when we get to the LIVE lock. */
3611 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3612 basesrc->live_running = live_play;
3617 /* clear our unlock request when going to PLAYING */
3618 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3619 if (bclass->unlock_stop)
3620 bclass->unlock_stop (basesrc);
3622 /* for live sources we restart the timestamp correction */
3623 basesrc->priv->latency = -1;
3624 /* have to restart the task in case it stopped because of the unlock when
3625 * we went to PAUSED. Only do this if we operating in push mode. */
3626 GST_OBJECT_LOCK (basesrc->srcpad);
3627 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3628 GST_OBJECT_UNLOCK (basesrc->srcpad);
3630 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3631 basesrc->srcpad, NULL);
3632 GST_DEBUG_OBJECT (basesrc, "signal");
3633 GST_LIVE_SIGNAL (basesrc);
3635 GST_LIVE_UNLOCK (basesrc);
3641 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3643 GstBaseSrc *basesrc;
3645 basesrc = GST_BASE_SRC (parent);
3647 /* prepare subclass first */
3649 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3651 if (G_UNLIKELY (!basesrc->can_activate_push))
3652 goto no_push_activation;
3654 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3657 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3658 /* now we can stop the source */
3659 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3667 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3672 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3677 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3683 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3685 GstBaseSrc *basesrc;
3687 basesrc = GST_BASE_SRC (parent);
3689 /* prepare subclass first */
3691 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3692 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3695 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3696 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3704 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3709 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3715 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3716 GstPadMode mode, gboolean active)
3719 GstBaseSrc *src = GST_BASE_SRC (parent);
3721 src->priv->stream_start_pending = FALSE;
3723 GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3726 case GST_PAD_MODE_PULL:
3727 res = gst_base_src_activate_pull (pad, parent, active);
3729 case GST_PAD_MODE_PUSH:
3730 src->priv->stream_start_pending = active;
3731 res = gst_base_src_activate_push (pad, parent, active);
3734 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3742 static GstStateChangeReturn
3743 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3745 GstBaseSrc *basesrc;
3746 GstStateChangeReturn result;
3747 gboolean no_preroll = FALSE;
3749 basesrc = GST_BASE_SRC (element);
3751 switch (transition) {
3752 case GST_STATE_CHANGE_NULL_TO_READY:
3754 case GST_STATE_CHANGE_READY_TO_PAUSED:
3755 no_preroll = gst_base_src_is_live (basesrc);
3757 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3758 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3759 if (gst_base_src_is_live (basesrc)) {
3760 /* now we can start playback */
3761 gst_base_src_set_playing (basesrc, TRUE);
3769 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3770 transition)) == GST_STATE_CHANGE_FAILURE)
3773 switch (transition) {
3774 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3775 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3776 if (gst_base_src_is_live (basesrc)) {
3777 /* make sure we block in the live lock in PAUSED */
3778 gst_base_src_set_playing (basesrc, FALSE);
3782 case GST_STATE_CHANGE_PAUSED_TO_READY:
3784 /* we don't need to unblock anything here, the pad deactivation code
3785 * already did this */
3786 if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3787 GST_OBJECT_LOCK (basesrc);
3788 CLEAR_PENDING_EOS (basesrc);
3789 GST_OBJECT_UNLOCK (basesrc);
3791 gst_event_replace (&basesrc->pending_seek, NULL);
3794 case GST_STATE_CHANGE_READY_TO_NULL:
3800 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3801 result = GST_STATE_CHANGE_NO_PREROLL;
3808 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3814 * gst_base_src_get_buffer_pool:
3815 * @src: a #GstBaseSrc
3817 * Returns: (transfer full): the instance of the #GstBufferPool used
3818 * by the src; free it after use it
3821 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3823 g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3825 if (src->priv->pool)
3826 return gst_object_ref (src->priv->pool);
3832 * gst_base_src_get_allocator:
3833 * @src: a #GstBaseSrc
3834 * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3836 * @params: (out) (allow-none) (transfer full): the
3837 * #GstAllocationParams of @allocator
3839 * Lets #GstBaseSrc sub-classes to know the memory @allocator
3840 * used by the base class and its @params.
3842 * Unref the @allocator after use it.
3845 gst_base_src_get_allocator (GstBaseSrc * src,
3846 GstAllocator ** allocator, GstAllocationParams * params)
3848 g_return_if_fail (GST_IS_BASE_SRC (src));
3851 *allocator = src->priv->allocator ?
3852 gst_object_ref (src->priv->allocator) : NULL;
3855 *params = src->priv->params;