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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Base class for getrange based source elements
26 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
28 * This is a generice 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_NEWSEGMENT
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 since 0.10.12 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));
124 * // see #GstElementDetails
125 * gst_element_class_set_details (gstelement_class, &details);
130 * <title>Controlled shutdown of live sources in applications</title>
132 * Applications that record from a live source may want to stop recording
133 * in a controlled way, so that the recording is stopped, but the data
134 * already in the pipeline is processed to the end (remember that many live
135 * sources would go on recording forever otherwise). For that to happen the
136 * application needs to make the source stop recording and send an EOS
137 * event down the pipeline. The application would then wait for an
138 * EOS message posted on the pipeline's bus to know when all data has
139 * been processed and the pipeline can safely be stopped.
141 * Since GStreamer 0.10.16 an application may send an EOS event to a source
142 * element to make it perform the EOS logic (send EOS event downstream or post a
143 * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
144 * with the gst_element_send_event() function on the element or its parent bin.
146 * After the EOS has been sent to the element, the application should wait for
147 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
148 * received, it may safely shut down the entire pipeline.
150 * Last reviewed on 2007-12-19 (0.10.16)
162 #include <gst/gst_private.h>
163 #include <gst/glib-compat-private.h>
165 #include "gstbasesrc.h"
166 #include "gsttypefindhelper.h"
167 #include <gst/gstmarshal.h>
168 #include <gst/gst-i18n-lib.h>
170 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
171 #define GST_CAT_DEFAULT gst_base_src_debug
173 #define GST_LIVE_GET_LOCK(elem) (&GST_BASE_SRC_CAST(elem)->live_lock)
174 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
175 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
176 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
177 #define GST_LIVE_GET_COND(elem) (&GST_BASE_SRC_CAST(elem)->live_cond)
178 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
179 #define GST_LIVE_WAIT_UNTIL(elem, end_time) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
180 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
181 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
183 /* BaseSrc signals and args */
190 #define DEFAULT_BLOCKSIZE 4096
191 #define DEFAULT_NUM_BUFFERS -1
192 #define DEFAULT_TYPEFIND FALSE
193 #define DEFAULT_DO_TIMESTAMP FALSE
204 #define GST_BASE_SRC_GET_PRIVATE(obj) \
205 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
207 struct _GstBaseSrcPrivate
212 GstFlowReturn start_result;
215 /* if a stream-start event should be sent */
216 gboolean stream_start_pending;
218 /* if segment should be sent */
219 gboolean segment_pending;
221 /* if EOS is pending (atomic) */
224 /* startup latency is the time it takes between going to PLAYING and producing
225 * the first BUFFER with running_time 0. This value is included in the latency
227 GstClockTime latency;
228 /* timestamp offset, this is the offset add to the values of gst_times for
229 * pseudo live sources */
230 GstClockTimeDiff ts_offset;
232 gboolean do_timestamp;
233 volatile gint dynamic_size;
235 /* stream sequence number */
238 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
239 * pushed in the data stream */
240 GList *pending_events;
241 volatile gint have_events;
243 /* QoS *//* with LOCK */
244 gboolean qos_enabled;
246 GstClockTime earliest_time;
249 GstAllocator *allocator;
254 static GstElementClass *parent_class = NULL;
256 static void gst_base_src_class_init (GstBaseSrcClass * klass);
257 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
258 static void gst_base_src_finalize (GObject * object);
262 gst_base_src_get_type (void)
264 static volatile gsize base_src_type = 0;
266 if (g_once_init_enter (&base_src_type)) {
268 static const GTypeInfo base_src_info = {
269 sizeof (GstBaseSrcClass),
272 (GClassInitFunc) gst_base_src_class_init,
277 (GInstanceInitFunc) gst_base_src_init,
280 _type = g_type_register_static (GST_TYPE_ELEMENT,
281 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
282 g_once_init_leave (&base_src_type, _type);
284 return base_src_type;
287 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
289 static void gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
290 static void gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
292 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
293 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
294 GstPadMode mode, gboolean active);
295 static void gst_base_src_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec);
297 static void gst_base_src_get_property (GObject * object, guint prop_id,
298 GValue * value, GParamSpec * pspec);
299 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
301 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
302 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
304 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
307 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
309 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
310 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
311 GstSegment * segment);
312 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
313 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
314 GstEvent * event, GstSegment * segment);
315 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
316 guint64 offset, guint size, GstBuffer ** buf);
317 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
318 guint64 offset, guint size, GstBuffer ** buf);
320 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
321 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
323 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
324 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
326 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
327 GstStateChange transition);
329 static void gst_base_src_loop (GstPad * pad);
330 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
331 guint64 offset, guint length, GstBuffer ** buf);
332 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
333 guint length, GstBuffer ** buf);
334 static gboolean gst_base_src_seekable (GstBaseSrc * src);
335 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
336 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
340 gst_base_src_class_init (GstBaseSrcClass * klass)
342 GObjectClass *gobject_class;
343 GstElementClass *gstelement_class;
345 gobject_class = G_OBJECT_CLASS (klass);
346 gstelement_class = GST_ELEMENT_CLASS (klass);
348 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
350 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
352 parent_class = g_type_class_peek_parent (klass);
354 gobject_class->finalize = gst_base_src_finalize;
355 gobject_class->set_property = gst_base_src_set_property;
356 gobject_class->get_property = gst_base_src_get_property;
358 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
359 g_param_spec_uint ("blocksize", "Block size",
360 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
361 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
363 g_param_spec_int ("num-buffers", "num-buffers",
364 "Number of buffers to output before sending EOS (-1 = unlimited)",
365 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
366 G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
368 g_param_spec_boolean ("typefind", "Typefind",
369 "Run typefind before negotiating", DEFAULT_TYPEFIND,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
372 g_param_spec_boolean ("do-timestamp", "Do timestamp",
373 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 gstelement_class->change_state =
377 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
378 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
380 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
381 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
382 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
383 klass->prepare_seek_segment =
384 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
385 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
386 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
387 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
388 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
389 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
391 /* Registering debug symbols for function pointers */
392 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
393 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
394 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
395 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
396 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
400 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
403 GstPadTemplate *pad_template;
405 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
407 basesrc->is_live = FALSE;
408 g_mutex_init (&basesrc->live_lock);
409 g_cond_init (&basesrc->live_cond);
410 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
411 basesrc->num_buffers_left = -1;
413 basesrc->can_activate_push = TRUE;
416 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
417 g_return_if_fail (pad_template != NULL);
419 GST_DEBUG_OBJECT (basesrc, "creating src pad");
420 pad = gst_pad_new_from_template (pad_template, "src");
422 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
423 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
424 gst_pad_set_event_function (pad, gst_base_src_event);
425 gst_pad_set_query_function (pad, gst_base_src_query);
426 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
428 /* hold pointer to pad */
429 basesrc->srcpad = pad;
430 GST_DEBUG_OBJECT (basesrc, "adding src pad");
431 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
433 basesrc->blocksize = DEFAULT_BLOCKSIZE;
434 basesrc->clock_id = NULL;
435 /* we operate in BYTES by default */
436 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
437 basesrc->typefind = DEFAULT_TYPEFIND;
438 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
439 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
441 basesrc->priv->start_result = GST_FLOW_FLUSHING;
442 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
443 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
444 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
446 GST_DEBUG_OBJECT (basesrc, "init done");
450 gst_base_src_finalize (GObject * object)
455 basesrc = GST_BASE_SRC (object);
457 g_mutex_clear (&basesrc->live_lock);
458 g_cond_clear (&basesrc->live_cond);
460 event_p = &basesrc->pending_seek;
461 gst_event_replace (event_p, NULL);
463 if (basesrc->priv->pending_events) {
464 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
466 g_list_free (basesrc->priv->pending_events);
469 G_OBJECT_CLASS (parent_class)->finalize (object);
473 * gst_base_src_wait_playing:
476 * If the #GstBaseSrcClass.create() method performs its own synchronisation
477 * against the clock it must unblock when going from PLAYING to the PAUSED state
478 * and call this method before continuing to produce the remaining data.
480 * This function will block until a state change to PLAYING happens (in which
481 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
482 * to a state change to READY or a FLUSH event (in which case this function
483 * returns #GST_FLOW_FLUSHING).
487 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
488 * continue. Any other return value should be returned from the create vmethod.
491 gst_base_src_wait_playing (GstBaseSrc * src)
493 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
496 /* block until the state changes, or we get a flush, or something */
497 GST_DEBUG_OBJECT (src, "live source waiting for running state");
499 GST_DEBUG_OBJECT (src, "live source unlocked");
500 if (src->priv->flushing)
502 } while (G_UNLIKELY (!src->live_running));
509 GST_DEBUG_OBJECT (src, "we are flushing");
510 return GST_FLOW_FLUSHING;
515 * gst_base_src_set_live:
516 * @src: base source instance
517 * @live: new live-mode
519 * If the element listens to a live source, @live should
522 * A live source will not produce data in the PAUSED state and
523 * will therefore not be able to participate in the PREROLL phase
524 * of a pipeline. To signal this fact to the application and the
525 * pipeline, the state change return value of the live source will
526 * be GST_STATE_CHANGE_NO_PREROLL.
529 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
531 g_return_if_fail (GST_IS_BASE_SRC (src));
533 GST_OBJECT_LOCK (src);
535 GST_OBJECT_UNLOCK (src);
539 * gst_base_src_is_live:
540 * @src: base source instance
542 * Check if an element is in live mode.
544 * Returns: %TRUE if element is in live mode.
547 gst_base_src_is_live (GstBaseSrc * src)
551 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
553 GST_OBJECT_LOCK (src);
554 result = src->is_live;
555 GST_OBJECT_UNLOCK (src);
561 * gst_base_src_set_format:
562 * @src: base source instance
563 * @format: the format to use
565 * Sets the default format of the source. This will be the format used
566 * for sending NEW_SEGMENT events and for performing seeks.
568 * If a format of GST_FORMAT_BYTES is set, the element will be able to
569 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
571 * This function must only be called in states < %GST_STATE_PAUSED.
576 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
578 g_return_if_fail (GST_IS_BASE_SRC (src));
579 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
581 GST_OBJECT_LOCK (src);
582 gst_segment_init (&src->segment, format);
583 GST_OBJECT_UNLOCK (src);
587 * gst_base_src_set_dynamic_size:
588 * @src: base source instance
589 * @dynamic: new dynamic size mode
591 * If not @dynamic, size is only updated when needed, such as when trying to
592 * read past current tracked size. Otherwise, size is checked for upon each
598 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
600 g_return_if_fail (GST_IS_BASE_SRC (src));
602 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
606 * gst_base_src_set_async:
607 * @src: base source instance
608 * @async: new async mode
610 * Configure async behaviour in @src, no state change will block. The open,
611 * close, start, stop, play and pause virtual methods will be executed in a
612 * different thread and are thus allowed to perform blocking operations. Any
613 * blocking operation should be unblocked with the unlock vmethod.
616 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
618 g_return_if_fail (GST_IS_BASE_SRC (src));
620 GST_OBJECT_LOCK (src);
621 src->priv->async = async;
622 GST_OBJECT_UNLOCK (src);
626 * gst_base_src_is_async:
627 * @src: base source instance
629 * Get the current async behaviour of @src. See also gst_base_src_set_async().
631 * Returns: %TRUE if @src is operating in async mode.
634 gst_base_src_is_async (GstBaseSrc * src)
638 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
640 GST_OBJECT_LOCK (src);
641 res = src->priv->async;
642 GST_OBJECT_UNLOCK (src);
649 * gst_base_src_query_latency:
651 * @live: (out) (allow-none): if the source is live
652 * @min_latency: (out) (allow-none): the min latency of the source
653 * @max_latency: (out) (allow-none): the max latency of the source
655 * Query the source for the latency parameters. @live will be TRUE when @src is
656 * configured as a live source. @min_latency will be set to the difference
657 * between the running time and the timestamp of the first buffer.
658 * @max_latency is always the undefined value of -1.
660 * This function is mostly used by subclasses.
662 * Returns: TRUE if the query succeeded.
667 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
668 GstClockTime * min_latency, GstClockTime * max_latency)
672 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
674 GST_OBJECT_LOCK (src);
676 *live = src->is_live;
678 /* if we have a startup latency, report this one, else report 0. Subclasses
679 * are supposed to override the query function if they want something
681 if (src->priv->latency != -1)
682 min = src->priv->latency;
691 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
692 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
694 GST_OBJECT_UNLOCK (src);
700 * gst_base_src_set_blocksize:
702 * @blocksize: the new blocksize in bytes
704 * Set the number of bytes that @src will push out with each buffer. When
705 * @blocksize is set to -1, a default length will be used.
710 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
712 g_return_if_fail (GST_IS_BASE_SRC (src));
714 GST_OBJECT_LOCK (src);
715 src->blocksize = blocksize;
716 GST_OBJECT_UNLOCK (src);
720 * gst_base_src_get_blocksize:
723 * Get the number of bytes that @src will push out with each buffer.
725 * Returns: the number of bytes pushed with each buffer.
730 gst_base_src_get_blocksize (GstBaseSrc * src)
734 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
736 GST_OBJECT_LOCK (src);
737 res = src->blocksize;
738 GST_OBJECT_UNLOCK (src);
745 * gst_base_src_set_do_timestamp:
747 * @timestamp: enable or disable timestamping
749 * Configure @src to automatically timestamp outgoing buffers based on the
750 * current running_time of the pipeline. This property is mostly useful for live
756 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
758 g_return_if_fail (GST_IS_BASE_SRC (src));
760 GST_OBJECT_LOCK (src);
761 src->priv->do_timestamp = timestamp;
762 GST_OBJECT_UNLOCK (src);
766 * gst_base_src_get_do_timestamp:
769 * Query if @src timestamps outgoing buffers based on the current running_time.
771 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
776 gst_base_src_get_do_timestamp (GstBaseSrc * src)
780 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
782 GST_OBJECT_LOCK (src);
783 res = src->priv->do_timestamp;
784 GST_OBJECT_UNLOCK (src);
790 * gst_base_src_new_seamless_segment:
792 * @start: The new start value for the segment
793 * @stop: Stop value for the new segment
794 * @position: The position value for the new segent
796 * Prepare a new seamless segment for emission downstream. This function must
797 * only be called by derived sub-classes, and only from the create() function,
798 * as the stream-lock needs to be held.
800 * The format for the new segment will be the current format of the source, as
801 * configured with gst_base_src_set_format()
803 * Returns: %TRUE if preparation of the seamless segment succeeded.
808 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
813 GST_DEBUG_OBJECT (src,
814 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
815 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
816 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
818 GST_OBJECT_LOCK (src);
820 src->segment.base = gst_segment_to_running_time (&src->segment,
821 src->segment.format, src->segment.position);
822 src->segment.start = start;
823 src->segment.stop = stop;
824 src->segment.position = position;
826 /* forward, we send data from position to stop */
827 src->priv->segment_pending = TRUE;
828 GST_OBJECT_UNLOCK (src);
830 src->priv->discont = TRUE;
837 gst_base_src_send_stream_start (GstBaseSrc * src)
841 if (src->priv->stream_start_pending) {
842 ret = gst_pad_push_event (src->srcpad, gst_event_new_stream_start ());
843 src->priv->stream_start_pending = FALSE;
850 * gst_base_src_set_caps:
851 * @src: a #GstBaseSrc
854 * Set new caps on the basesrc source pad.
856 * Returns: %TRUE if the caps could be set
859 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
861 GstBaseSrcClass *bclass;
864 bclass = GST_BASE_SRC_GET_CLASS (src);
866 gst_base_src_send_stream_start (src);
867 gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
869 if (bclass->set_caps)
870 res = bclass->set_caps (src, caps);
876 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
878 GstCaps *caps = NULL;
879 GstPadTemplate *pad_template;
880 GstBaseSrcClass *bclass;
882 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
885 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
887 if (pad_template != NULL) {
888 caps = gst_pad_template_get_caps (pad_template);
891 GstCaps *intersection;
894 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
895 gst_caps_unref (caps);
903 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
905 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
906 gst_caps_fixate (caps);
910 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
912 GstBaseSrcClass *bclass;
914 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
917 bclass->fixate (bsrc, caps);
921 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
925 switch (GST_QUERY_TYPE (query)) {
926 case GST_QUERY_POSITION:
930 gst_query_parse_position (query, &format, NULL);
932 GST_DEBUG_OBJECT (src, "position query in format %s",
933 gst_format_get_name (format));
936 case GST_FORMAT_PERCENT:
942 GST_OBJECT_LOCK (src);
943 position = src->segment.position;
944 duration = src->segment.duration;
945 GST_OBJECT_UNLOCK (src);
947 if (position != -1 && duration != -1) {
948 if (position < duration)
949 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
952 percent = GST_FORMAT_PERCENT_MAX;
956 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
963 GstFormat seg_format;
965 GST_OBJECT_LOCK (src);
967 gst_segment_to_stream_time (&src->segment, src->segment.format,
968 src->segment.position);
969 seg_format = src->segment.format;
970 GST_OBJECT_UNLOCK (src);
972 if (position != -1) {
973 /* convert to requested format */
975 gst_pad_query_convert (src->srcpad, seg_format,
976 position, format, &position);
980 gst_query_set_position (query, format, position);
986 case GST_QUERY_DURATION:
990 gst_query_parse_duration (query, &format, NULL);
992 GST_DEBUG_OBJECT (src, "duration query in format %s",
993 gst_format_get_name (format));
996 case GST_FORMAT_PERCENT:
997 gst_query_set_duration (query, GST_FORMAT_PERCENT,
998 GST_FORMAT_PERCENT_MAX);
1004 GstFormat seg_format;
1007 /* may have to refresh duration */
1008 if (g_atomic_int_get (&src->priv->dynamic_size))
1009 gst_base_src_update_length (src, 0, &length);
1011 /* this is the duration as configured by the subclass. */
1012 GST_OBJECT_LOCK (src);
1013 duration = src->segment.duration;
1014 seg_format = src->segment.format;
1015 GST_OBJECT_UNLOCK (src);
1017 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1018 duration, gst_format_get_name (seg_format));
1020 if (duration != -1) {
1021 /* convert to requested format, if this fails, we have a duration
1022 * but we cannot answer the query, we must return FALSE. */
1024 gst_pad_query_convert (src->srcpad, seg_format,
1025 duration, format, &duration);
1027 /* The subclass did not configure a duration, we assume that the
1028 * media has an unknown duration then and we return TRUE to report
1029 * this. Note that this is not the same as returning FALSE, which
1030 * means that we cannot report the duration at all. */
1033 gst_query_set_duration (query, format, duration);
1040 case GST_QUERY_SEEKING:
1042 GstFormat format, seg_format;
1045 GST_OBJECT_LOCK (src);
1046 duration = src->segment.duration;
1047 seg_format = src->segment.format;
1048 GST_OBJECT_UNLOCK (src);
1050 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1051 if (format == seg_format) {
1052 gst_query_set_seeking (query, seg_format,
1053 gst_base_src_seekable (src), 0, duration);
1056 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1057 /* Don't reply to the query to make up for demuxers which don't
1058 * handle the SEEKING query yet. Players like Totem will fall back
1059 * to the duration when the SEEKING query isn't answered. */
1064 case GST_QUERY_SEGMENT:
1068 GST_OBJECT_LOCK (src);
1069 /* no end segment configured, current duration then */
1070 if ((stop = src->segment.stop) == -1)
1071 stop = src->segment.duration;
1072 start = src->segment.start;
1074 /* adjust to stream time */
1075 if (src->segment.time != -1) {
1076 start -= src->segment.time;
1078 stop -= src->segment.time;
1081 gst_query_set_segment (query, src->segment.rate, src->segment.format,
1083 GST_OBJECT_UNLOCK (src);
1088 case GST_QUERY_FORMATS:
1090 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1091 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1095 case GST_QUERY_CONVERT:
1097 GstFormat src_fmt, dest_fmt;
1098 gint64 src_val, dest_val;
1100 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1102 /* we can only convert between equal formats... */
1103 if (src_fmt == dest_fmt) {
1109 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1112 case GST_QUERY_LATENCY:
1114 GstClockTime min, max;
1117 /* Subclasses should override and implement something useful */
1118 res = gst_base_src_query_latency (src, &live, &min, &max);
1120 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1121 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1122 GST_TIME_ARGS (max));
1124 gst_query_set_latency (query, live, min, max);
1127 case GST_QUERY_JITTER:
1128 case GST_QUERY_RATE:
1131 case GST_QUERY_BUFFERING:
1133 GstFormat format, seg_format;
1134 gint64 start, stop, estimated;
1136 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1138 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1139 gst_format_get_name (format));
1141 GST_OBJECT_LOCK (src);
1142 if (src->random_access) {
1145 if (format == GST_FORMAT_PERCENT)
1146 stop = GST_FORMAT_PERCENT_MAX;
1148 stop = src->segment.duration;
1154 seg_format = src->segment.format;
1155 GST_OBJECT_UNLOCK (src);
1157 /* convert to required format. When the conversion fails, we can't answer
1158 * the query. When the value is unknown, we can don't perform conversion
1159 * but report TRUE. */
1160 if (format != GST_FORMAT_PERCENT && stop != -1) {
1161 res = gst_pad_query_convert (src->srcpad, seg_format,
1162 stop, format, &stop);
1166 if (res && format != GST_FORMAT_PERCENT && start != -1)
1167 res = gst_pad_query_convert (src->srcpad, seg_format,
1168 start, format, &start);
1170 gst_query_set_buffering_range (query, format, start, stop, estimated);
1173 case GST_QUERY_SCHEDULING:
1175 gboolean random_access;
1177 random_access = gst_base_src_is_random_access (src);
1179 /* we can operate in getrange mode if the native format is bytes
1180 * and we are seekable, this condition is set in the random_access
1181 * flag and is set in the _start() method. */
1182 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1184 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1185 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1190 case GST_QUERY_CAPS:
1192 GstBaseSrcClass *bclass;
1193 GstCaps *caps, *filter;
1195 bclass = GST_BASE_SRC_GET_CLASS (src);
1196 if (bclass->get_caps) {
1197 gst_query_parse_caps (query, &filter);
1198 if ((caps = bclass->get_caps (src, filter))) {
1199 gst_query_set_caps_result (query, caps);
1200 gst_caps_unref (caps);
1213 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1220 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1223 GstBaseSrcClass *bclass;
1224 gboolean result = FALSE;
1226 src = GST_BASE_SRC (parent);
1227 bclass = GST_BASE_SRC_GET_CLASS (src);
1230 result = bclass->query (src, query);
1236 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1238 gboolean res = TRUE;
1240 /* update our offset if the start/stop position was updated */
1241 if (segment->format == GST_FORMAT_BYTES) {
1242 segment->time = segment->start;
1243 } else if (segment->start == 0) {
1244 /* seek to start, we can implement a default for this. */
1248 GST_INFO_OBJECT (src, "Can't do a default seek");
1255 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1257 GstBaseSrcClass *bclass;
1258 gboolean result = FALSE;
1260 bclass = GST_BASE_SRC_GET_CLASS (src);
1262 if (bclass->do_seek)
1263 result = bclass->do_seek (src, segment);
1268 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1271 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1272 GstSegment * segment)
1274 /* By default, we try one of 2 things:
1275 * - For absolute seek positions, convert the requested position to our
1276 * configured processing format and place it in the output segment \
1277 * - For relative seek positions, convert our current (input) values to the
1278 * seek format, adjust by the relative seek offset and then convert back to
1279 * the processing format
1281 GstSeekType cur_type, stop_type;
1284 GstFormat seek_format, dest_format;
1287 gboolean res = TRUE;
1289 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1290 &cur_type, &cur, &stop_type, &stop);
1291 dest_format = segment->format;
1293 if (seek_format == dest_format) {
1294 gst_segment_do_seek (segment, rate, seek_format, flags,
1295 cur_type, cur, stop_type, stop, &update);
1299 if (cur_type != GST_SEEK_TYPE_NONE) {
1300 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1302 gst_pad_query_convert (src->srcpad, seek_format, cur, dest_format,
1304 cur_type = GST_SEEK_TYPE_SET;
1307 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1308 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1310 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1312 stop_type = GST_SEEK_TYPE_SET;
1315 /* And finally, configure our output segment in the desired format */
1316 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
1317 stop_type, stop, &update);
1326 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1332 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1333 GstSegment * seeksegment)
1335 GstBaseSrcClass *bclass;
1336 gboolean result = FALSE;
1338 bclass = GST_BASE_SRC_GET_CLASS (src);
1340 if (bclass->prepare_seek_segment)
1341 result = bclass->prepare_seek_segment (src, event, seeksegment);
1346 static GstFlowReturn
1347 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1348 guint size, GstBuffer ** buffer)
1351 GstBaseSrcPrivate *priv = src->priv;
1354 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1355 } else if (size != -1) {
1356 *buffer = gst_buffer_new_allocate (priv->allocator, size, priv->alignment);
1357 if (G_UNLIKELY (*buffer == NULL))
1362 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1371 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1372 return GST_FLOW_ERROR;
1376 static GstFlowReturn
1377 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1378 guint size, GstBuffer ** buffer)
1380 GstBaseSrcClass *bclass;
1383 bclass = GST_BASE_SRC_GET_CLASS (src);
1385 if (G_UNLIKELY (!bclass->alloc))
1387 if (G_UNLIKELY (!bclass->fill))
1390 ret = bclass->alloc (src, offset, size, buffer);
1391 if (G_UNLIKELY (ret != GST_FLOW_OK))
1394 if (G_LIKELY (size > 0)) {
1395 /* only call fill when there is a size */
1396 ret = bclass->fill (src, offset, size, *buffer);
1397 if (G_UNLIKELY (ret != GST_FLOW_OK))
1406 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1407 return GST_FLOW_NOT_SUPPORTED;
1411 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1416 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1417 gst_flow_get_name (ret));
1418 gst_buffer_unref (*buffer);
1423 /* this code implements the seeking. It is a good example
1424 * handling all cases.
1426 * A seek updates the currently configured segment.start
1427 * and segment.stop values based on the SEEK_TYPE. If the
1428 * segment.start value is updated, a seek to this new position
1429 * should be performed.
1431 * The seek can only be executed when we are not currently
1432 * streaming any data, to make sure that this is the case, we
1433 * acquire the STREAM_LOCK which is taken when we are in the
1434 * _loop() function or when a getrange() is called. Normally
1435 * we will not receive a seek if we are operating in pull mode
1436 * though. When we operate as a live source we might block on the live
1437 * cond, which does not release the STREAM_LOCK. Therefore we will try
1438 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1439 * safe to perform the seek.
1441 * When we are in the loop() function, we might be in the middle
1442 * of pushing a buffer, which might block in a sink. To make sure
1443 * that the push gets unblocked we push out a FLUSH_START event.
1444 * Our loop function will get a WRONG_STATE return value from
1445 * the push and will pause, effectively releasing the STREAM_LOCK.
1447 * For a non-flushing seek, we pause the task, which might eventually
1448 * release the STREAM_LOCK. We say eventually because when the sink
1449 * blocks on the sample we might wait a very long time until the sink
1450 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1451 * can continue the seek. A non-flushing seek is normally done in a
1452 * running pipeline to perform seamless playback, this means that the sink is
1453 * PLAYING and will return from its chain function.
1454 * In the case of a non-flushing seek we need to make sure that the
1455 * data we output after the seek is continuous with the previous data,
1456 * this is because a non-flushing seek does not reset the running-time
1457 * to 0. We do this by closing the currently running segment, ie. sending
1458 * a new_segment event with the stop position set to the last processed
1461 * After updating the segment.start/stop values, we prepare for
1462 * streaming again. We push out a FLUSH_STOP to make the peer pad
1463 * accept data again and we start our task again.
1465 * A segment seek posts a message on the bus saying that the playback
1466 * of the segment started. We store the segment flag internally because
1467 * when we reach the segment.stop we have to post a segment.done
1468 * instead of EOS when doing a segment seek.
1470 /* FIXME (0.11), we have the unlock gboolean here because most current
1471 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1472 * the streaming thread isn't running, resulting in bogus unlocks later when it
1473 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1474 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1478 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1480 gboolean res = TRUE, tres;
1482 GstFormat seek_format, dest_format;
1484 GstSeekType cur_type, stop_type;
1486 gboolean flush, playing;
1488 gboolean relative_seek = FALSE;
1489 gboolean seekseg_configured = FALSE;
1490 GstSegment seeksegment;
1494 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1496 GST_OBJECT_LOCK (src);
1497 dest_format = src->segment.format;
1498 GST_OBJECT_UNLOCK (src);
1501 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1502 &cur_type, &cur, &stop_type, &stop);
1504 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1505 SEEK_TYPE_IS_RELATIVE (stop_type);
1507 if (dest_format != seek_format && !relative_seek) {
1508 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1509 * here before taking the stream lock, otherwise we must convert it later,
1510 * once we have the stream lock and can read the last configures segment
1511 * start and stop positions */
1512 gst_segment_init (&seeksegment, dest_format);
1514 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1515 goto prepare_failed;
1517 seekseg_configured = TRUE;
1520 flush = flags & GST_SEEK_FLAG_FLUSH;
1521 seqnum = gst_event_get_seqnum (event);
1524 /* get next seqnum */
1525 seqnum = gst_util_seqnum_next ();
1528 /* send flush start */
1530 tevent = gst_event_new_flush_start ();
1531 gst_event_set_seqnum (tevent, seqnum);
1532 gst_pad_push_event (src->srcpad, tevent);
1534 gst_pad_pause_task (src->srcpad);
1536 /* unblock streaming thread. */
1537 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1539 /* grab streaming lock, this should eventually be possible, either
1540 * because the task is paused, our streaming thread stopped
1541 * or because our peer is flushing. */
1542 GST_PAD_STREAM_LOCK (src->srcpad);
1543 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1544 /* we have seen this event before, issue a warning for now */
1545 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1548 src->priv->seqnum = seqnum;
1549 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1552 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1554 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1555 * copy the current segment info into the temp segment that we can actually
1556 * attempt the seek with. We only update the real segment if the seek succeeds. */
1557 if (!seekseg_configured) {
1558 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1560 /* now configure the final seek segment */
1562 if (seeksegment.format != seek_format) {
1563 /* OK, here's where we give the subclass a chance to convert the relative
1564 * seek into an absolute one in the processing format. We set up any
1565 * absolute seek above, before taking the stream lock. */
1566 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1567 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1572 /* The seek format matches our processing format, no need to ask the
1573 * the subclass to configure the segment. */
1574 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1575 cur_type, cur, stop_type, stop, &update);
1578 /* Else, no seek event passed, so we're just (re)starting the
1583 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1584 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1585 seeksegment.start, seeksegment.stop, seeksegment.position);
1587 /* do the seek, segment.position contains the new position. */
1588 res = gst_base_src_do_seek (src, &seeksegment);
1591 /* and prepare to continue streaming */
1593 tevent = gst_event_new_flush_stop (TRUE);
1594 gst_event_set_seqnum (tevent, seqnum);
1595 /* send flush stop, peer will accept data and events again. We
1596 * are not yet providing data as we still have the STREAM_LOCK. */
1597 gst_pad_push_event (src->srcpad, tevent);
1600 /* The subclass must have converted the segment to the processing format
1602 if (res && seeksegment.format != dest_format) {
1603 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1604 "in the correct format. Aborting seek.");
1608 /* if the seek was successful, we update our real segment and push
1609 * out the new segment. */
1611 GST_OBJECT_LOCK (src);
1612 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1613 GST_OBJECT_UNLOCK (src);
1615 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1616 GstMessage *message;
1618 message = gst_message_new_segment_start (GST_OBJECT (src),
1619 seeksegment.format, seeksegment.position);
1620 gst_message_set_seqnum (message, seqnum);
1622 gst_element_post_message (GST_ELEMENT (src), message);
1625 /* for deriving a stop position for the playback segment from the seek
1626 * segment, we must take the duration when the stop is not set */
1627 if ((stop = seeksegment.stop) == -1)
1628 stop = seeksegment.duration;
1630 src->priv->segment_pending = TRUE;
1633 src->priv->discont = TRUE;
1634 src->running = TRUE;
1635 /* and restart the task in case it got paused explicitly or by
1636 * the FLUSH_START event we pushed out. */
1637 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1642 /* and release the lock again so we can continue streaming */
1643 GST_PAD_STREAM_UNLOCK (src->srcpad);
1649 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1654 /* all events send to this element directly. This is mainly done from the
1658 gst_base_src_send_event (GstElement * element, GstEvent * event)
1661 gboolean result = FALSE;
1663 src = GST_BASE_SRC (element);
1665 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1667 switch (GST_EVENT_TYPE (event)) {
1668 /* bidirectional events */
1669 case GST_EVENT_FLUSH_START:
1670 case GST_EVENT_FLUSH_STOP:
1671 /* sending random flushes downstream can break stuff,
1672 * especially sync since all segment info will get flushed */
1675 /* downstream serialized events */
1678 GstBaseSrcClass *bclass;
1680 bclass = GST_BASE_SRC_GET_CLASS (src);
1682 /* queue EOS and make sure the task or pull function performs the EOS
1685 * We have two possibilities:
1687 * - Before we are to enter the _create function, we check the pending_eos
1688 * first and do EOS instead of entering it.
1689 * - If we are in the _create function or we did not manage to set the
1690 * flag fast enough and we are about to enter the _create function,
1691 * we unlock it so that we exit with WRONG_STATE immediately. We then
1692 * check the EOS flag and do the EOS logic.
1694 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1695 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1698 /* unlock the _create function so that we can check the pending_eos flag
1699 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1700 * that we can grab it and stop the unlock again. We don't take the stream
1701 * lock so that this operation is guaranteed to never block. */
1702 gst_base_src_activate_pool (src, FALSE);
1704 bclass->unlock (src);
1706 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1708 GST_LIVE_LOCK (src);
1709 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1710 /* now stop the unlock of the streaming thread again. Grabbing the live
1711 * lock is enough because that protects the create function. */
1712 if (bclass->unlock_stop)
1713 bclass->unlock_stop (src);
1714 gst_base_src_activate_pool (src, TRUE);
1715 GST_LIVE_UNLOCK (src);
1720 case GST_EVENT_SEGMENT:
1721 /* sending random SEGMENT downstream can break sync. */
1724 case GST_EVENT_CUSTOM_DOWNSTREAM:
1725 case GST_EVENT_CUSTOM_BOTH:
1726 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1727 GST_OBJECT_LOCK (src);
1728 src->priv->pending_events =
1729 g_list_append (src->priv->pending_events, event);
1730 g_atomic_int_set (&src->priv->have_events, TRUE);
1731 GST_OBJECT_UNLOCK (src);
1735 case GST_EVENT_BUFFERSIZE:
1736 /* does not seem to make much sense currently */
1739 /* upstream events */
1741 /* elements should override send_event and do something */
1743 case GST_EVENT_SEEK:
1747 GST_OBJECT_LOCK (src->srcpad);
1748 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1750 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1751 GST_OBJECT_UNLOCK (src->srcpad);
1754 GST_DEBUG_OBJECT (src, "performing seek");
1755 /* when we are running in push mode, we can execute the
1756 * seek right now, we need to unlock. */
1757 result = gst_base_src_perform_seek (src, event, TRUE);
1761 /* else we store the event and execute the seek when we
1763 GST_OBJECT_LOCK (src);
1764 GST_DEBUG_OBJECT (src, "queueing seek");
1765 event_p = &src->pending_seek;
1766 gst_event_replace ((GstEvent **) event_p, event);
1767 GST_OBJECT_UNLOCK (src);
1768 /* assume the seek will work */
1773 case GST_EVENT_NAVIGATION:
1774 /* could make sense for elements that do something with navigation events
1775 * but then they would need to override the send_event function */
1777 case GST_EVENT_LATENCY:
1778 /* does not seem to make sense currently */
1782 case GST_EVENT_CUSTOM_UPSTREAM:
1783 /* override send_event if you want this */
1785 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1786 case GST_EVENT_CUSTOM_BOTH_OOB:
1787 /* insert a random custom event into the pipeline */
1788 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1789 result = gst_pad_push_event (src->srcpad, event);
1790 /* we gave away the ref to the event in the push */
1797 /* if we still have a ref to the event, unref it now */
1799 gst_event_unref (event);
1806 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1807 GST_OBJECT_UNLOCK (src->srcpad);
1814 gst_base_src_seekable (GstBaseSrc * src)
1816 GstBaseSrcClass *bclass;
1817 bclass = GST_BASE_SRC_GET_CLASS (src);
1818 if (bclass->is_seekable)
1819 return bclass->is_seekable (src);
1825 gst_base_src_update_qos (GstBaseSrc * src,
1826 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1828 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1829 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1830 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1832 GST_OBJECT_LOCK (src);
1833 src->priv->proportion = proportion;
1834 src->priv->earliest_time = timestamp + diff;
1835 GST_OBJECT_UNLOCK (src);
1840 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1844 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1846 switch (GST_EVENT_TYPE (event)) {
1847 case GST_EVENT_SEEK:
1848 /* is normally called when in push mode */
1849 if (!gst_base_src_seekable (src))
1852 result = gst_base_src_perform_seek (src, event, TRUE);
1854 case GST_EVENT_FLUSH_START:
1855 /* cancel any blocking getrange, is normally called
1856 * when in pull mode. */
1857 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1859 case GST_EVENT_FLUSH_STOP:
1860 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1865 GstClockTimeDiff diff;
1866 GstClockTime timestamp;
1868 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1869 gst_base_src_update_qos (src, proportion, diff, timestamp);
1873 case GST_EVENT_RECONFIGURE:
1876 case GST_EVENT_LATENCY:
1888 GST_DEBUG_OBJECT (src, "is not seekable");
1894 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1897 GstBaseSrcClass *bclass;
1898 gboolean result = FALSE;
1900 src = GST_BASE_SRC (parent);
1901 bclass = GST_BASE_SRC_GET_CLASS (src);
1903 if (bclass->event) {
1904 if (!(result = bclass->event (src, event)))
1905 goto subclass_failed;
1909 gst_event_unref (event);
1916 GST_DEBUG_OBJECT (src, "subclass refused event");
1922 gst_base_src_set_property (GObject * object, guint prop_id,
1923 const GValue * value, GParamSpec * pspec)
1927 src = GST_BASE_SRC (object);
1930 case PROP_BLOCKSIZE:
1931 gst_base_src_set_blocksize (src, g_value_get_uint (value));
1933 case PROP_NUM_BUFFERS:
1934 src->num_buffers = g_value_get_int (value);
1937 src->typefind = g_value_get_boolean (value);
1939 case PROP_DO_TIMESTAMP:
1940 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1943 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1949 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1954 src = GST_BASE_SRC (object);
1957 case PROP_BLOCKSIZE:
1958 g_value_set_uint (value, gst_base_src_get_blocksize (src));
1960 case PROP_NUM_BUFFERS:
1961 g_value_set_int (value, src->num_buffers);
1964 g_value_set_boolean (value, src->typefind);
1966 case PROP_DO_TIMESTAMP:
1967 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1970 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1975 /* with STREAM_LOCK and LOCK */
1976 static GstClockReturn
1977 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1982 id = gst_clock_new_single_shot_id (clock, time);
1984 basesrc->clock_id = id;
1985 /* release the live lock while waiting */
1986 GST_LIVE_UNLOCK (basesrc);
1988 ret = gst_clock_id_wait (id, NULL);
1990 GST_LIVE_LOCK (basesrc);
1991 gst_clock_id_unref (id);
1992 basesrc->clock_id = NULL;
1997 /* perform synchronisation on a buffer.
2000 static GstClockReturn
2001 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2003 GstClockReturn result;
2004 GstClockTime start, end;
2005 GstBaseSrcClass *bclass;
2006 GstClockTime base_time;
2008 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
2009 gboolean do_timestamp, first, pseudo_live;
2011 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2014 if (bclass->get_times)
2015 bclass->get_times (basesrc, buffer, &start, &end);
2017 /* get buffer timestamp */
2018 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2020 /* grab the lock to prepare for clocking and calculate the startup
2022 GST_OBJECT_LOCK (basesrc);
2024 /* if we are asked to sync against the clock we are a pseudo live element */
2025 pseudo_live = (start != -1 && basesrc->is_live);
2026 /* check for the first buffer */
2027 first = (basesrc->priv->latency == -1);
2029 if (timestamp != -1 && pseudo_live) {
2030 GstClockTime latency;
2032 /* we have a timestamp and a sync time, latency is the diff */
2033 if (timestamp <= start)
2034 latency = start - timestamp;
2039 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2040 GST_TIME_ARGS (latency));
2041 /* first time we calculate latency, just configure */
2042 basesrc->priv->latency = latency;
2044 if (basesrc->priv->latency != latency) {
2045 /* we have a new latency, FIXME post latency message */
2046 basesrc->priv->latency = latency;
2047 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2048 GST_TIME_ARGS (latency));
2052 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2053 basesrc->is_live, start != -1);
2054 basesrc->priv->latency = 0;
2057 /* get clock, if no clock, we can't sync or do timestamps */
2058 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2061 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2063 do_timestamp = basesrc->priv->do_timestamp;
2065 /* first buffer, calculate the timestamp offset */
2067 GstClockTime running_time;
2069 now = gst_clock_get_time (clock);
2070 running_time = now - base_time;
2072 GST_LOG_OBJECT (basesrc,
2073 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
2074 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
2075 GST_TIME_ARGS (running_time));
2077 if (pseudo_live && timestamp != -1) {
2078 /* live source and we need to sync, add startup latency to all timestamps
2079 * to get the real running_time. Live sources should always timestamp
2080 * according to the current running time. */
2081 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2083 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2084 GST_TIME_ARGS (basesrc->priv->ts_offset));
2086 basesrc->priv->ts_offset = 0;
2087 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2090 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
2092 timestamp = running_time;
2096 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
2098 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2099 GST_TIME_ARGS (timestamp));
2102 /* add the timestamp offset we need for sync */
2103 timestamp += basesrc->priv->ts_offset;
2105 /* not the first buffer, the timestamp is the diff between the clock and
2107 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
2108 now = gst_clock_get_time (clock);
2110 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
2112 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2113 GST_TIME_ARGS (now - base_time));
2117 /* if we don't have a buffer timestamp, we don't sync */
2118 if (!GST_CLOCK_TIME_IS_VALID (start))
2121 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
2122 /* for pseudo live sources, add our ts_offset to the timestamp */
2123 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
2124 start += basesrc->priv->ts_offset;
2127 GST_LOG_OBJECT (basesrc,
2128 "waiting for clock, base time %" GST_TIME_FORMAT
2129 ", stream_start %" GST_TIME_FORMAT,
2130 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2131 GST_OBJECT_UNLOCK (basesrc);
2133 result = gst_base_src_wait (basesrc, clock, start + base_time);
2135 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2142 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2143 GST_OBJECT_UNLOCK (basesrc);
2144 return GST_CLOCK_OK;
2148 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2149 GST_OBJECT_UNLOCK (basesrc);
2150 return GST_CLOCK_OK;
2154 /* Called with STREAM_LOCK and LIVE_LOCK */
2156 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
2158 guint64 size, maxsize;
2159 GstBaseSrcClass *bclass;
2164 bclass = GST_BASE_SRC_GET_CLASS (src);
2166 format = src->segment.format;
2167 stop = src->segment.stop;
2168 /* get total file size */
2169 size = src->segment.duration;
2171 /* only operate if we are working with bytes */
2172 if (format != GST_FORMAT_BYTES)
2175 /* the max amount of bytes to read is the total size or
2176 * up to the segment.stop if present. */
2178 maxsize = MIN (size, stop);
2182 GST_DEBUG_OBJECT (src,
2183 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2184 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2185 *length, size, stop, maxsize);
2187 dynamic = g_atomic_int_get (&src->priv->dynamic_size);
2188 GST_DEBUG_OBJECT (src, "dynamic size: %d", dynamic);
2190 /* check size if we have one */
2191 if (maxsize != -1) {
2192 /* if we run past the end, check if the file became bigger and
2194 if (G_UNLIKELY (offset + *length >= maxsize || dynamic)) {
2195 /* see if length of the file changed */
2196 if (bclass->get_size)
2197 if (!bclass->get_size (src, &size))
2200 /* make sure we don't exceed the configured segment stop
2203 maxsize = MIN (size, stop);
2207 /* if we are at or past the end, EOS */
2208 if (G_UNLIKELY (offset >= maxsize))
2209 goto unexpected_length;
2211 /* else we can clip to the end */
2212 if (G_UNLIKELY (offset + *length >= maxsize))
2213 *length = maxsize - offset;
2218 /* keep track of current duration.
2219 * segment is in bytes, we checked that above. */
2220 GST_OBJECT_LOCK (src);
2221 src->segment.duration = size;
2222 GST_OBJECT_UNLOCK (src);
2233 /* must be called with LIVE_LOCK */
2234 static GstFlowReturn
2235 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2239 GstBaseSrcClass *bclass;
2240 GstClockReturn status;
2242 bclass = GST_BASE_SRC_GET_CLASS (src);
2246 if (G_UNLIKELY (!src->live_running)) {
2247 ret = gst_base_src_wait_playing (src);
2248 if (ret != GST_FLOW_OK)
2253 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2254 && !GST_BASE_SRC_IS_STARTING (src)))
2257 if (G_UNLIKELY (!bclass->create))
2260 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2261 goto unexpected_length;
2263 /* track position */
2264 GST_OBJECT_LOCK (src);
2265 if (src->segment.format == GST_FORMAT_BYTES)
2266 src->segment.position = offset;
2267 GST_OBJECT_UNLOCK (src);
2269 /* normally we don't count buffers */
2270 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2271 if (src->num_buffers_left == 0)
2272 goto reached_num_buffers;
2274 src->num_buffers_left--;
2277 /* don't enter the create function if a pending EOS event was set. For the
2278 * logic of the pending_eos, check the event function of this class. */
2279 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2282 GST_DEBUG_OBJECT (src,
2283 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2284 G_GINT64_FORMAT, offset, length, src->segment.time);
2286 ret = bclass->create (src, offset, length, buf);
2288 /* The create function could be unlocked because we have a pending EOS. It's
2289 * possible that we have a valid buffer from create that we need to
2290 * discard when the create function returned _OK. */
2291 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2292 if (ret == GST_FLOW_OK) {
2293 gst_buffer_unref (*buf);
2299 if (G_UNLIKELY (ret != GST_FLOW_OK))
2302 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2303 if (offset == 0 && src->segment.time == 0
2304 && GST_BUFFER_TIMESTAMP (*buf) == -1 && !src->is_live) {
2305 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2306 *buf = gst_buffer_make_writable (*buf);
2307 GST_BUFFER_TIMESTAMP (*buf) = 0;
2310 /* now sync before pushing the buffer */
2311 status = gst_base_src_do_sync (src, *buf);
2313 /* waiting for the clock could have made us flushing */
2314 if (G_UNLIKELY (src->priv->flushing))
2318 case GST_CLOCK_EARLY:
2319 /* the buffer is too late. We currently don't drop the buffer. */
2320 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2323 /* buffer synchronised properly */
2324 GST_DEBUG_OBJECT (src, "buffer ok");
2326 case GST_CLOCK_UNSCHEDULED:
2327 /* this case is triggered when we were waiting for the clock and
2328 * it got unlocked because we did a state change. In any case, get rid of
2330 gst_buffer_unref (*buf);
2332 if (!src->live_running) {
2333 /* We return WRONG_STATE when we are not running to stop the dataflow also
2334 * get rid of the produced buffer. */
2335 GST_DEBUG_OBJECT (src,
2336 "clock was unscheduled (%d), returning WRONG_STATE", status);
2337 ret = GST_FLOW_FLUSHING;
2339 /* If we are running when this happens, we quickly switched between
2340 * pause and playing. We try to produce a new buffer */
2341 GST_DEBUG_OBJECT (src,
2342 "clock was unscheduled (%d), but we are running", status);
2347 /* all other result values are unexpected and errors */
2348 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2349 (_("Internal clock error.")),
2350 ("clock returned unexpected return value %d", status));
2351 gst_buffer_unref (*buf);
2353 ret = GST_FLOW_ERROR;
2361 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2362 gst_flow_get_name (ret));
2367 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2368 gst_flow_get_name (ret));
2373 GST_DEBUG_OBJECT (src, "getrange but not started");
2374 return GST_FLOW_FLUSHING;
2378 GST_DEBUG_OBJECT (src, "no create function");
2379 return GST_FLOW_NOT_SUPPORTED;
2383 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2384 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2385 return GST_FLOW_EOS;
2387 reached_num_buffers:
2389 GST_DEBUG_OBJECT (src, "sent all buffers");
2390 return GST_FLOW_EOS;
2394 GST_DEBUG_OBJECT (src, "we are flushing");
2395 gst_buffer_unref (*buf);
2397 return GST_FLOW_FLUSHING;
2401 GST_DEBUG_OBJECT (src, "we are EOS");
2402 return GST_FLOW_EOS;
2406 static GstFlowReturn
2407 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2408 guint length, GstBuffer ** buf)
2413 src = GST_BASE_SRC_CAST (parent);
2415 GST_LIVE_LOCK (src);
2416 if (G_UNLIKELY (src->priv->flushing))
2419 res = gst_base_src_get_range (src, offset, length, buf);
2422 GST_LIVE_UNLOCK (src);
2429 GST_DEBUG_OBJECT (src, "we are flushing");
2430 res = GST_FLOW_FLUSHING;
2436 gst_base_src_is_random_access (GstBaseSrc * src)
2438 /* we need to start the basesrc to check random access */
2439 if (!GST_BASE_SRC_IS_STARTED (src)) {
2440 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2441 if (G_LIKELY (gst_base_src_start (src))) {
2442 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2444 gst_base_src_stop (src);
2448 return src->random_access;
2453 GST_DEBUG_OBJECT (src, "failed to start");
2459 gst_base_src_loop (GstPad * pad)
2462 GstBuffer *buf = NULL;
2467 GList *pending_events = NULL, *tmp;
2471 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2473 gst_base_src_send_stream_start (src);
2475 /* check if we need to renegotiate */
2476 if (gst_pad_check_reconfigure (pad)) {
2477 if (!gst_base_src_negotiate (src))
2478 goto not_negotiated;
2481 GST_LIVE_LOCK (src);
2483 if (G_UNLIKELY (src->priv->flushing))
2486 blocksize = src->blocksize;
2488 /* if we operate in bytes, we can calculate an offset */
2489 if (src->segment.format == GST_FORMAT_BYTES) {
2490 position = src->segment.position;
2491 /* for negative rates, start with subtracting the blocksize */
2492 if (src->segment.rate < 0.0) {
2493 /* we cannot go below segment.start */
2494 if (position > src->segment.start + blocksize)
2495 position -= blocksize;
2497 /* last block, remainder up to segment.start */
2498 blocksize = position - src->segment.start;
2499 position = src->segment.start;
2505 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2506 GST_TIME_ARGS (position), blocksize);
2508 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2509 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2510 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2511 gst_flow_get_name (ret));
2512 GST_LIVE_UNLOCK (src);
2515 /* this should not happen */
2516 if (G_UNLIKELY (buf == NULL))
2519 /* push events to close/start our segment before we push the buffer. */
2520 if (G_UNLIKELY (src->priv->segment_pending)) {
2521 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2522 src->priv->segment_pending = FALSE;
2525 if (g_atomic_int_get (&src->priv->have_events)) {
2526 GST_OBJECT_LOCK (src);
2527 /* take the events */
2528 pending_events = src->priv->pending_events;
2529 src->priv->pending_events = NULL;
2530 g_atomic_int_set (&src->priv->have_events, FALSE);
2531 GST_OBJECT_UNLOCK (src);
2534 /* Push out pending events if any */
2535 if (G_UNLIKELY (pending_events != NULL)) {
2536 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2537 GstEvent *ev = (GstEvent *) tmp->data;
2538 gst_pad_push_event (pad, ev);
2540 g_list_free (pending_events);
2543 /* figure out the new position */
2544 switch (src->segment.format) {
2545 case GST_FORMAT_BYTES:
2547 guint bufsize = gst_buffer_get_size (buf);
2549 /* we subtracted above for negative rates */
2550 if (src->segment.rate >= 0.0)
2551 position += bufsize;
2554 case GST_FORMAT_TIME:
2556 GstClockTime start, duration;
2558 start = GST_BUFFER_TIMESTAMP (buf);
2559 duration = GST_BUFFER_DURATION (buf);
2561 if (GST_CLOCK_TIME_IS_VALID (start))
2564 position = src->segment.position;
2566 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2567 if (src->segment.rate >= 0.0)
2568 position += duration;
2569 else if (position > duration)
2570 position -= duration;
2576 case GST_FORMAT_DEFAULT:
2577 if (src->segment.rate >= 0.0)
2578 position = GST_BUFFER_OFFSET_END (buf);
2580 position = GST_BUFFER_OFFSET (buf);
2586 if (position != -1) {
2587 if (src->segment.rate >= 0.0) {
2588 /* positive rate, check if we reached the stop */
2589 if (src->segment.stop != -1) {
2590 if (position >= src->segment.stop) {
2592 position = src->segment.stop;
2596 /* negative rate, check if we reached the start. start is always set to
2597 * something different from -1 */
2598 if (position <= src->segment.start) {
2600 position = src->segment.start;
2602 /* when going reverse, all buffers are DISCONT */
2603 src->priv->discont = TRUE;
2605 GST_OBJECT_LOCK (src);
2606 src->segment.position = position;
2607 GST_OBJECT_UNLOCK (src);
2610 if (G_UNLIKELY (src->priv->discont)) {
2611 GST_INFO_OBJECT (src, "marking pending DISCONT");
2612 buf = gst_buffer_make_writable (buf);
2613 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2614 src->priv->discont = FALSE;
2616 GST_LIVE_UNLOCK (src);
2618 ret = gst_pad_push (pad, buf);
2619 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2620 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2621 gst_flow_get_name (ret));
2625 if (G_UNLIKELY (eos)) {
2626 GST_INFO_OBJECT (src, "pausing after end of segment");
2637 GST_DEBUG_OBJECT (src, "Failed to renegotiate");
2638 ret = GST_FLOW_NOT_NEGOTIATED;
2643 GST_DEBUG_OBJECT (src, "we are flushing");
2644 GST_LIVE_UNLOCK (src);
2645 ret = GST_FLOW_FLUSHING;
2650 const gchar *reason = gst_flow_get_name (ret);
2653 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2654 src->running = FALSE;
2655 gst_pad_pause_task (pad);
2656 if (ret == GST_FLOW_EOS) {
2657 gboolean flag_segment;
2661 /* perform EOS logic */
2662 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2663 format = src->segment.format;
2664 position = src->segment.position;
2667 GstMessage *message;
2669 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2671 gst_message_set_seqnum (message, src->priv->seqnum);
2672 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2674 event = gst_event_new_eos ();
2675 gst_event_set_seqnum (event, src->priv->seqnum);
2676 gst_pad_push_event (pad, event);
2678 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2679 event = gst_event_new_eos ();
2680 gst_event_set_seqnum (event, src->priv->seqnum);
2681 /* for fatal errors we post an error message, post the error
2682 * first so the app knows about the error first.
2683 * Also don't do this for WRONG_STATE because it happens
2684 * due to flushing and posting an error message because of
2685 * that is the wrong thing to do, e.g. when we're doing
2686 * a flushing seek. */
2687 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2688 (_("Internal data flow error.")),
2689 ("streaming task paused, reason %s (%d)", reason, ret));
2690 gst_pad_push_event (pad, event);
2696 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2697 (_("Internal data flow error.")), ("element returned NULL buffer"));
2698 GST_LIVE_UNLOCK (src);
2704 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2705 GstAllocator * allocator, guint prefix, guint alignment)
2707 GstAllocator *oldalloc;
2708 GstBufferPool *oldpool;
2709 GstBaseSrcPrivate *priv = basesrc->priv;
2712 GST_DEBUG_OBJECT (basesrc, "activate pool");
2713 if (!gst_buffer_pool_set_active (pool, TRUE))
2714 goto activate_failed;
2717 GST_OBJECT_LOCK (basesrc);
2718 oldpool = priv->pool;
2721 oldalloc = priv->allocator;
2722 priv->allocator = allocator;
2724 priv->prefix = prefix;
2725 priv->alignment = alignment;
2726 GST_OBJECT_UNLOCK (basesrc);
2729 /* only deactivate if the pool is not the one we're using */
2730 if (oldpool != pool) {
2731 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2732 gst_buffer_pool_set_active (oldpool, FALSE);
2734 gst_object_unref (oldpool);
2737 gst_allocator_unref (oldalloc);
2744 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2750 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2752 GstBaseSrcPrivate *priv = basesrc->priv;
2753 GstBufferPool *pool;
2754 gboolean res = TRUE;
2756 GST_OBJECT_LOCK (basesrc);
2757 if ((pool = priv->pool))
2758 pool = gst_object_ref (pool);
2759 GST_OBJECT_UNLOCK (basesrc);
2762 res = gst_buffer_pool_set_active (pool, active);
2763 gst_object_unref (pool);
2769 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
2771 GstBaseSrcClass *bclass;
2772 gboolean result = TRUE;
2774 GstBufferPool *pool = NULL;
2775 GstAllocator *allocator = NULL;
2776 guint size, min, max, prefix, alignment;
2778 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2780 /* make query and let peer pad answer, we don't really care if it worked or
2781 * not, if it failed, the allocation query would contain defaults and the
2782 * subclass would then set better values if needed */
2783 query = gst_query_new_allocation (caps, TRUE);
2784 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
2785 /* not a problem, just debug a little */
2786 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
2789 if (G_LIKELY (bclass->decide_allocation))
2790 result = bclass->decide_allocation (basesrc, query);
2792 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
2794 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
2798 /* no size, we have variable size buffers */
2799 if (gst_query_get_n_allocation_memories (query) > 0) {
2800 if ((allocator = gst_query_parse_nth_allocation_memory (query, 0)))
2801 gst_allocator_ref (allocator);
2803 GST_DEBUG_OBJECT (basesrc, "0 size, using allocator %p", allocator);
2804 } else if (pool == NULL) {
2805 /* fixed size, we can use a bufferpool */
2806 GstStructure *config;
2808 /* we did not get a pool, make one ourselves then */
2809 pool = gst_buffer_pool_new ();
2810 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
2812 config = gst_buffer_pool_get_config (pool);
2813 gst_buffer_pool_config_set (config, caps, size, min, max, prefix,
2815 gst_buffer_pool_set_config (pool, config);
2818 gst_query_unref (query);
2821 gst_base_src_set_allocation (basesrc, pool, allocator, prefix, alignment);
2827 /* default negotiation code.
2829 * Take intersection between src and sink pads, take first
2833 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2836 GstCaps *caps = NULL;
2837 GstCaps *peercaps = NULL;
2838 gboolean result = FALSE;
2840 /* first see what is possible on our source pad */
2841 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
2842 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2843 /* nothing or anything is allowed, we're done */
2844 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2845 goto no_nego_needed;
2847 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2850 /* get the peer caps */
2851 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
2852 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2854 /* The result is already a subset of our caps */
2856 gst_caps_unref (thiscaps);
2858 /* no peer, work with our own caps then */
2861 if (caps && !gst_caps_is_empty (caps)) {
2863 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
2864 if (gst_caps_is_any (caps)) {
2865 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
2866 /* hmm, still anything, so element can do anything and
2867 * nego is not needed */
2870 caps = gst_caps_make_writable (caps);
2871 gst_base_src_fixate (basesrc, caps);
2872 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2873 if (gst_caps_is_fixed (caps)) {
2874 /* yay, fixed caps, use those then, it's possible that the subclass does
2875 * not accept this caps after all and we have to fail. */
2876 result = gst_base_src_set_caps (basesrc, caps);
2879 gst_caps_unref (caps);
2882 gst_caps_unref (caps);
2883 GST_DEBUG_OBJECT (basesrc, "no common caps");
2889 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2891 gst_caps_unref (thiscaps);
2896 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2897 ("No supported formats found"),
2898 ("This element did not produce valid caps"));
2900 gst_caps_unref (thiscaps);
2906 gst_base_src_negotiate (GstBaseSrc * basesrc)
2908 GstBaseSrcClass *bclass;
2911 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2913 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
2915 if (G_LIKELY (bclass->negotiate))
2916 result = bclass->negotiate (basesrc);
2920 if (G_LIKELY (result)) {
2923 caps = gst_pad_get_current_caps (basesrc->srcpad);
2925 result = gst_base_src_prepare_allocation (basesrc, caps);
2928 gst_caps_unref (caps);
2934 gst_base_src_start (GstBaseSrc * basesrc)
2936 GstBaseSrcClass *bclass;
2939 GST_LIVE_LOCK (basesrc);
2940 if (GST_BASE_SRC_IS_STARTING (basesrc))
2942 if (GST_BASE_SRC_IS_STARTED (basesrc))
2945 basesrc->priv->start_result = GST_FLOW_FLUSHING;
2946 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
2947 basesrc->num_buffers_left = basesrc->num_buffers;
2948 basesrc->running = FALSE;
2949 basesrc->priv->segment_pending = FALSE;
2950 GST_OBJECT_LOCK (basesrc);
2951 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2952 GST_OBJECT_UNLOCK (basesrc);
2953 GST_LIVE_UNLOCK (basesrc);
2955 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2957 result = bclass->start (basesrc);
2962 goto could_not_start;
2964 if (!gst_base_src_is_async (basesrc))
2965 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
2972 GST_DEBUG_OBJECT (basesrc, "was starting");
2973 GST_LIVE_UNLOCK (basesrc);
2978 GST_DEBUG_OBJECT (basesrc, "was started");
2979 GST_LIVE_UNLOCK (basesrc);
2984 GST_DEBUG_OBJECT (basesrc, "could not start");
2985 /* subclass is supposed to post a message. We don't have to call _stop. */
2986 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
2992 * gst_base_src_start_complete:
2993 * @src: base source instance
2994 * @ret: a #GstFlowReturn
2996 * Complete an asynchronous start operation. When the subclass overrides the
2997 * start method, it should call gst_base_src_start_complete() when the start
2998 * operation completes either from the same thread or from an asynchronous
3002 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3011 if (ret != GST_FLOW_OK)
3014 GST_DEBUG_OBJECT (basesrc, "starting source");
3015 format = basesrc->segment.format;
3017 /* figure out the size */
3020 if (format == GST_FORMAT_BYTES) {
3021 GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3023 if (bclass->get_size) {
3024 if (!(have_size = bclass->get_size (basesrc, &size)))
3027 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3028 /* only update the size when operating in bytes, subclass is supposed
3029 * to set duration in the start method for other formats */
3030 GST_OBJECT_LOCK (basesrc);
3031 basesrc->segment.duration = size;
3032 GST_OBJECT_UNLOCK (basesrc);
3035 GST_DEBUG_OBJECT (basesrc,
3036 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3037 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3038 basesrc->segment.duration);
3040 seekable = gst_base_src_seekable (basesrc);
3041 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3043 /* update for random access flag */
3044 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3046 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3048 /* stop flushing now but for live sources, still block in the LIVE lock when
3049 * we are not yet PLAYING */
3050 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
3052 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3054 GST_OBJECT_LOCK (basesrc->srcpad);
3055 mode = GST_PAD_MODE (basesrc->srcpad);
3056 GST_OBJECT_UNLOCK (basesrc->srcpad);
3058 if (mode == GST_PAD_MODE_PUSH) {
3059 /* do initial seek, which will start the task */
3060 GST_OBJECT_LOCK (basesrc);
3061 event = basesrc->pending_seek;
3062 basesrc->pending_seek = NULL;
3063 GST_OBJECT_UNLOCK (basesrc);
3065 /* no need to unlock anything, the task is certainly
3066 * not running here. The perform seek code will start the task when
3068 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3072 gst_event_unref (event);
3074 /* if not random_access, we cannot operate in pull mode for now */
3075 if (G_UNLIKELY (!basesrc->random_access))
3079 GST_LIVE_LOCK (basesrc);
3080 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3081 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3082 basesrc->priv->start_result = ret;
3083 GST_LIVE_SIGNAL (basesrc);
3084 GST_LIVE_UNLOCK (basesrc);
3090 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3091 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3093 gst_event_unref (event);
3094 ret = GST_FLOW_ERROR;
3099 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3100 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3101 ret = GST_FLOW_ERROR;
3106 GST_LIVE_LOCK (basesrc);
3107 basesrc->priv->start_result = ret;
3108 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3109 GST_LIVE_SIGNAL (basesrc);
3110 GST_LIVE_UNLOCK (basesrc);
3116 * gst_base_src_start_wait:
3117 * @src: base source instance
3118 * @ret: a #GstFlowReturn
3120 * Wait until the start operation completes.
3122 * Returns: a #GstFlowReturn.
3125 gst_base_src_start_wait (GstBaseSrc * basesrc)
3127 GstFlowReturn result;
3129 GST_LIVE_LOCK (basesrc);
3130 if (G_UNLIKELY (basesrc->priv->flushing))
3133 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3134 GST_LIVE_WAIT (basesrc);
3135 if (G_UNLIKELY (basesrc->priv->flushing))
3138 result = basesrc->priv->start_result;
3139 GST_LIVE_UNLOCK (basesrc);
3146 GST_DEBUG_OBJECT (basesrc, "we are flushing");
3147 GST_LIVE_UNLOCK (basesrc);
3148 return GST_FLOW_FLUSHING;
3153 gst_base_src_stop (GstBaseSrc * basesrc)
3155 GstBaseSrcClass *bclass;
3156 gboolean result = TRUE;
3158 GST_DEBUG_OBJECT (basesrc, "stopping source");
3161 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3163 gst_pad_stop_task (basesrc->srcpad);
3165 GST_LIVE_LOCK (basesrc);
3166 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3169 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3170 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3171 basesrc->priv->start_result = GST_FLOW_FLUSHING;
3172 GST_LIVE_SIGNAL (basesrc);
3173 GST_LIVE_UNLOCK (basesrc);
3175 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3177 result = bclass->stop (basesrc);
3179 gst_base_src_set_allocation (basesrc, NULL, NULL, 0, 0);
3185 GST_DEBUG_OBJECT (basesrc, "was started");
3186 GST_LIVE_UNLOCK (basesrc);
3191 /* start or stop flushing dataprocessing
3194 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3195 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
3197 GstBaseSrcClass *bclass;
3199 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3201 if (flushing && unlock) {
3202 gst_base_src_activate_pool (basesrc, FALSE);
3203 /* unlock any subclasses, we need to do this before grabbing the
3204 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3205 * unlock to the params because of backwards compat (see seek handler)*/
3207 bclass->unlock (basesrc);
3210 /* the live lock is released when we are blocked, waiting for playing or
3211 * when we sync to the clock. */
3212 GST_LIVE_LOCK (basesrc);
3214 *playing = basesrc->live_running;
3215 basesrc->priv->flushing = flushing;
3217 /* if we are locked in the live lock, signal it to make it flush */
3218 basesrc->live_running = TRUE;
3220 /* clear pending EOS if any */
3221 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3223 /* step 1, now that we have the LIVE lock, clear our unlock request */
3224 if (bclass->unlock_stop)
3225 bclass->unlock_stop (basesrc);
3227 /* step 2, unblock clock sync (if any) or any other blocking thing */
3228 if (basesrc->clock_id)
3229 gst_clock_id_unschedule (basesrc->clock_id);
3231 /* signal the live source that it can start playing */
3232 basesrc->live_running = live_play;
3234 gst_base_src_activate_pool (basesrc, TRUE);
3236 /* When unlocking drop all delayed events */
3238 GST_OBJECT_LOCK (basesrc);
3239 if (basesrc->priv->pending_events) {
3240 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3242 g_list_free (basesrc->priv->pending_events);
3243 basesrc->priv->pending_events = NULL;
3244 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3246 GST_OBJECT_UNLOCK (basesrc);
3249 GST_LIVE_SIGNAL (basesrc);
3250 GST_LIVE_UNLOCK (basesrc);
3255 /* the purpose of this function is to make sure that a live source blocks in the
3256 * LIVE lock or leaves the LIVE lock and continues playing. */
3258 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3260 GstBaseSrcClass *bclass;
3262 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3264 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3266 GST_DEBUG_OBJECT (basesrc, "unlock");
3268 bclass->unlock (basesrc);
3271 /* we are now able to grab the LIVE lock, when we get it, we can be
3272 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3274 GST_LIVE_LOCK (basesrc);
3275 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3277 /* unblock clock sync (if any) */
3278 if (basesrc->clock_id)
3279 gst_clock_id_unschedule (basesrc->clock_id);
3281 /* configure what to do when we get to the LIVE lock. */
3282 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3283 basesrc->live_running = live_play;
3288 /* clear our unlock request when going to PLAYING */
3289 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3290 if (bclass->unlock_stop)
3291 bclass->unlock_stop (basesrc);
3293 /* for live sources we restart the timestamp correction */
3294 basesrc->priv->latency = -1;
3295 /* have to restart the task in case it stopped because of the unlock when
3296 * we went to PAUSED. Only do this if we operating in push mode. */
3297 GST_OBJECT_LOCK (basesrc->srcpad);
3298 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3299 GST_OBJECT_UNLOCK (basesrc->srcpad);
3301 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3303 GST_DEBUG_OBJECT (basesrc, "signal");
3304 GST_LIVE_SIGNAL (basesrc);
3306 GST_LIVE_UNLOCK (basesrc);
3312 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3314 GstBaseSrc *basesrc;
3316 basesrc = GST_BASE_SRC (parent);
3318 /* prepare subclass first */
3320 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3322 if (G_UNLIKELY (!basesrc->can_activate_push))
3323 goto no_push_activation;
3325 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3328 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3329 /* now we can stop the source */
3330 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3338 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3343 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3348 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3354 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3356 GstBaseSrc *basesrc;
3358 basesrc = GST_BASE_SRC (parent);
3360 /* prepare subclass first */
3362 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3363 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3366 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3367 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3375 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3380 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3386 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3387 GstPadMode mode, gboolean active)
3392 case GST_PAD_MODE_PULL:
3393 res = gst_base_src_activate_pull (pad, parent, active);
3395 case GST_PAD_MODE_PUSH:
3396 res = gst_base_src_activate_push (pad, parent, active);
3399 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3407 static GstStateChangeReturn
3408 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3410 GstBaseSrc *basesrc;
3411 GstStateChangeReturn result;
3412 gboolean no_preroll = FALSE;
3414 basesrc = GST_BASE_SRC (element);
3416 switch (transition) {
3417 case GST_STATE_CHANGE_NULL_TO_READY:
3419 case GST_STATE_CHANGE_READY_TO_PAUSED:
3420 basesrc->priv->stream_start_pending = TRUE;
3421 no_preroll = gst_base_src_is_live (basesrc);
3423 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3424 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3425 if (gst_base_src_is_live (basesrc)) {
3426 /* now we can start playback */
3427 gst_base_src_set_playing (basesrc, TRUE);
3435 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3436 transition)) == GST_STATE_CHANGE_FAILURE)
3439 switch (transition) {
3440 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3441 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3442 if (gst_base_src_is_live (basesrc)) {
3443 /* make sure we block in the live lock in PAUSED */
3444 gst_base_src_set_playing (basesrc, FALSE);
3448 case GST_STATE_CHANGE_PAUSED_TO_READY:
3450 /* we don't need to unblock anything here, the pad deactivation code
3451 * already did this */
3452 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3453 gst_event_replace (&basesrc->pending_seek, NULL);
3454 basesrc->priv->stream_start_pending = FALSE;
3457 case GST_STATE_CHANGE_READY_TO_NULL:
3463 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3464 result = GST_STATE_CHANGE_NO_PREROLL;
3471 GST_DEBUG_OBJECT (basesrc, "parent failed state change");