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 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
163 * with newer GLib versions (>= 2.31.0) */
164 #define GLIB_DISABLE_DEPRECATION_WARNINGS
165 #include <gst/gst_private.h>
166 #include <gst/glib-compat-private.h>
168 #include "gstbasesrc.h"
169 #include "gsttypefindhelper.h"
170 #include <gst/gstmarshal.h>
171 #include <gst/gst-i18n-lib.h>
173 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
174 #define GST_CAT_DEFAULT gst_base_src_debug
176 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
177 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
178 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
179 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
180 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
181 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
182 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
184 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
185 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
187 /* BaseSrc signals and args */
194 #define DEFAULT_BLOCKSIZE 4096
195 #define DEFAULT_NUM_BUFFERS -1
196 #define DEFAULT_TYPEFIND FALSE
197 #define DEFAULT_DO_TIMESTAMP FALSE
208 #define GST_BASE_SRC_GET_PRIVATE(obj) \
209 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
211 struct _GstBaseSrcPrivate
216 GstFlowReturn start_result;
219 /* if segment should be sent */
220 gboolean segment_pending;
222 /* if EOS is pending (atomic) */
225 /* startup latency is the time it takes between going to PLAYING and producing
226 * the first BUFFER with running_time 0. This value is included in the latency
228 GstClockTime latency;
229 /* timestamp offset, this is the offset add to the values of gst_times for
230 * pseudo live sources */
231 GstClockTimeDiff ts_offset;
233 gboolean do_timestamp;
234 volatile gint dynamic_size;
236 /* stream sequence number */
239 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
240 * pushed in the data stream */
241 GList *pending_events;
242 volatile gint have_events;
244 /* QoS *//* with LOCK */
245 gboolean qos_enabled;
247 GstClockTime earliest_time;
250 const GstAllocator *allocator;
255 static GstElementClass *parent_class = NULL;
257 static void gst_base_src_class_init (GstBaseSrcClass * klass);
258 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
259 static void gst_base_src_finalize (GObject * object);
263 gst_base_src_get_type (void)
265 static volatile gsize base_src_type = 0;
267 if (g_once_init_enter (&base_src_type)) {
269 static const GTypeInfo base_src_info = {
270 sizeof (GstBaseSrcClass),
273 (GClassInitFunc) gst_base_src_class_init,
278 (GInstanceInitFunc) gst_base_src_init,
281 _type = g_type_register_static (GST_TYPE_ELEMENT,
282 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
283 g_once_init_leave (&base_src_type, _type);
285 return base_src_type;
288 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
290 static void gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
291 static void gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
293 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
294 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
295 GstPadMode mode, gboolean active);
296 static void gst_base_src_set_property (GObject * object, guint prop_id,
297 const GValue * value, GParamSpec * pspec);
298 static void gst_base_src_get_property (GObject * object, guint prop_id,
299 GValue * value, GParamSpec * pspec);
300 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
302 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
303 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
305 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
308 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
310 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
311 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
312 GstSegment * segment);
313 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
314 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
315 GstEvent * event, GstSegment * segment);
316 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
317 guint64 offset, guint size, GstBuffer ** buf);
318 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
319 guint64 offset, guint size, GstBuffer ** buf);
321 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
322 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
324 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
325 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
327 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
328 GstStateChange transition);
330 static void gst_base_src_loop (GstPad * pad);
331 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
332 guint64 offset, guint length, GstBuffer ** buf);
333 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
334 guint length, GstBuffer ** buf);
335 static gboolean gst_base_src_seekable (GstBaseSrc * src);
336 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
337 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
341 gst_base_src_class_init (GstBaseSrcClass * klass)
343 GObjectClass *gobject_class;
344 GstElementClass *gstelement_class;
346 gobject_class = G_OBJECT_CLASS (klass);
347 gstelement_class = GST_ELEMENT_CLASS (klass);
349 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
351 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
353 parent_class = g_type_class_peek_parent (klass);
355 gobject_class->finalize = gst_base_src_finalize;
356 gobject_class->set_property = gst_base_src_set_property;
357 gobject_class->get_property = gst_base_src_get_property;
359 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
360 g_param_spec_uint ("blocksize", "Block size",
361 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
362 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
364 g_param_spec_int ("num-buffers", "num-buffers",
365 "Number of buffers to output before sending EOS (-1 = unlimited)",
366 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
367 G_PARAM_STATIC_STRINGS));
368 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
369 g_param_spec_boolean ("typefind", "Typefind",
370 "Run typefind before negotiating", DEFAULT_TYPEFIND,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
373 g_param_spec_boolean ("do-timestamp", "Do timestamp",
374 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
375 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 gstelement_class->change_state =
378 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
379 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
381 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
382 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
383 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
384 klass->prepare_seek_segment =
385 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
386 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
387 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
388 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
389 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
390 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
392 /* Registering debug symbols for function pointers */
393 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
394 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
395 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
396 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
397 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
401 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
404 GstPadTemplate *pad_template;
406 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
408 basesrc->is_live = FALSE;
409 basesrc->live_lock = g_mutex_new ();
410 basesrc->live_cond = g_cond_new ();
411 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
412 basesrc->num_buffers_left = -1;
414 basesrc->can_activate_push = TRUE;
417 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
418 g_return_if_fail (pad_template != NULL);
420 GST_DEBUG_OBJECT (basesrc, "creating src pad");
421 pad = gst_pad_new_from_template (pad_template, "src");
423 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
424 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
425 gst_pad_set_event_function (pad, gst_base_src_event);
426 gst_pad_set_query_function (pad, gst_base_src_query);
427 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
429 /* hold pointer to pad */
430 basesrc->srcpad = pad;
431 GST_DEBUG_OBJECT (basesrc, "adding src pad");
432 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
434 basesrc->blocksize = DEFAULT_BLOCKSIZE;
435 basesrc->clock_id = NULL;
436 /* we operate in BYTES by default */
437 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
438 basesrc->typefind = DEFAULT_TYPEFIND;
439 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
440 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
442 basesrc->priv->start_result = GST_FLOW_WRONG_STATE;
443 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
444 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
445 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
447 GST_DEBUG_OBJECT (basesrc, "init done");
451 gst_base_src_finalize (GObject * object)
456 basesrc = GST_BASE_SRC (object);
458 g_mutex_free (basesrc->live_lock);
459 g_cond_free (basesrc->live_cond);
461 event_p = &basesrc->pending_seek;
462 gst_event_replace (event_p, NULL);
464 if (basesrc->priv->pending_events) {
465 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
467 g_list_free (basesrc->priv->pending_events);
470 G_OBJECT_CLASS (parent_class)->finalize (object);
474 * gst_base_src_wait_playing:
477 * If the #GstBaseSrcClass.create() method performs its own synchronisation
478 * against the clock it must unblock when going from PLAYING to the PAUSED state
479 * and call this method before continuing to produce the remaining data.
481 * This function will block until a state change to PLAYING happens (in which
482 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
483 * to a state change to READY or a FLUSH event (in which case this function
484 * returns #GST_FLOW_WRONG_STATE).
488 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
489 * continue. Any other return value should be returned from the create vmethod.
492 gst_base_src_wait_playing (GstBaseSrc * src)
494 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
497 /* block until the state changes, or we get a flush, or something */
498 GST_DEBUG_OBJECT (src, "live source waiting for running state");
500 GST_DEBUG_OBJECT (src, "live source unlocked");
501 if (src->priv->flushing)
503 } while (G_UNLIKELY (!src->live_running));
510 GST_DEBUG_OBJECT (src, "we are flushing");
511 return GST_FLOW_WRONG_STATE;
516 * gst_base_src_set_live:
517 * @src: base source instance
518 * @live: new live-mode
520 * If the element listens to a live source, @live should
523 * A live source will not produce data in the PAUSED state and
524 * will therefore not be able to participate in the PREROLL phase
525 * of a pipeline. To signal this fact to the application and the
526 * pipeline, the state change return value of the live source will
527 * be GST_STATE_CHANGE_NO_PREROLL.
530 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
532 g_return_if_fail (GST_IS_BASE_SRC (src));
534 GST_OBJECT_LOCK (src);
536 GST_OBJECT_UNLOCK (src);
540 * gst_base_src_is_live:
541 * @src: base source instance
543 * Check if an element is in live mode.
545 * Returns: %TRUE if element is in live mode.
548 gst_base_src_is_live (GstBaseSrc * src)
552 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
554 GST_OBJECT_LOCK (src);
555 result = src->is_live;
556 GST_OBJECT_UNLOCK (src);
562 * gst_base_src_set_format:
563 * @src: base source instance
564 * @format: the format to use
566 * Sets the default format of the source. This will be the format used
567 * for sending NEW_SEGMENT events and for performing seeks.
569 * If a format of GST_FORMAT_BYTES is set, the element will be able to
570 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
572 * This function must only be called in states < %GST_STATE_PAUSED.
577 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
579 g_return_if_fail (GST_IS_BASE_SRC (src));
580 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
582 GST_OBJECT_LOCK (src);
583 gst_segment_init (&src->segment, format);
584 GST_OBJECT_UNLOCK (src);
588 * gst_base_src_set_dynamic_size:
589 * @src: base source instance
590 * @dynamic: new dynamic size mode
592 * If not @dynamic, size is only updated when needed, such as when trying to
593 * read past current tracked size. Otherwise, size is checked for upon each
599 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
601 g_return_if_fail (GST_IS_BASE_SRC (src));
603 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
607 * gst_base_src_set_async:
608 * @src: base source instance
609 * @async: new async mode
611 * Configure async behaviour in @src, no state change will block. The open,
612 * close, start, stop, play and pause virtual methods will be executed in a
613 * different thread and are thus allowed to perform blocking operations. Any
614 * blocking operation should be unblocked with the unlock vmethod.
617 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
619 g_return_if_fail (GST_IS_BASE_SRC (src));
621 GST_OBJECT_LOCK (src);
622 src->priv->async = async;
623 GST_OBJECT_UNLOCK (src);
627 * gst_base_src_is_async:
628 * @src: base source instance
630 * Get the current async behaviour of @src. See also gst_base_src_set_async().
632 * Returns: %TRUE if @src is operating in async mode.
635 gst_base_src_is_async (GstBaseSrc * src)
639 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
641 GST_OBJECT_LOCK (src);
642 res = src->priv->async;
643 GST_OBJECT_UNLOCK (src);
650 * gst_base_src_query_latency:
652 * @live: (out) (allow-none): if the source is live
653 * @min_latency: (out) (allow-none): the min latency of the source
654 * @max_latency: (out) (allow-none): the max latency of the source
656 * Query the source for the latency parameters. @live will be TRUE when @src is
657 * configured as a live source. @min_latency will be set to the difference
658 * between the running time and the timestamp of the first buffer.
659 * @max_latency is always the undefined value of -1.
661 * This function is mostly used by subclasses.
663 * Returns: TRUE if the query succeeded.
668 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
669 GstClockTime * min_latency, GstClockTime * max_latency)
673 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
675 GST_OBJECT_LOCK (src);
677 *live = src->is_live;
679 /* if we have a startup latency, report this one, else report 0. Subclasses
680 * are supposed to override the query function if they want something
682 if (src->priv->latency != -1)
683 min = src->priv->latency;
692 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
693 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
695 GST_OBJECT_UNLOCK (src);
701 * gst_base_src_set_blocksize:
703 * @blocksize: the new blocksize in bytes
705 * Set the number of bytes that @src will push out with each buffer. When
706 * @blocksize is set to -1, a default length will be used.
711 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
713 g_return_if_fail (GST_IS_BASE_SRC (src));
715 GST_OBJECT_LOCK (src);
716 src->blocksize = blocksize;
717 GST_OBJECT_UNLOCK (src);
721 * gst_base_src_get_blocksize:
724 * Get the number of bytes that @src will push out with each buffer.
726 * Returns: the number of bytes pushed with each buffer.
731 gst_base_src_get_blocksize (GstBaseSrc * src)
735 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
737 GST_OBJECT_LOCK (src);
738 res = src->blocksize;
739 GST_OBJECT_UNLOCK (src);
746 * gst_base_src_set_do_timestamp:
748 * @timestamp: enable or disable timestamping
750 * Configure @src to automatically timestamp outgoing buffers based on the
751 * current running_time of the pipeline. This property is mostly useful for live
757 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
759 g_return_if_fail (GST_IS_BASE_SRC (src));
761 GST_OBJECT_LOCK (src);
762 src->priv->do_timestamp = timestamp;
763 GST_OBJECT_UNLOCK (src);
767 * gst_base_src_get_do_timestamp:
770 * Query if @src timestamps outgoing buffers based on the current running_time.
772 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
777 gst_base_src_get_do_timestamp (GstBaseSrc * src)
781 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
783 GST_OBJECT_LOCK (src);
784 res = src->priv->do_timestamp;
785 GST_OBJECT_UNLOCK (src);
791 * gst_base_src_new_seamless_segment:
793 * @start: The new start value for the segment
794 * @stop: Stop value for the new segment
795 * @position: The position value for the new segent
797 * Prepare a new seamless segment for emission downstream. This function must
798 * only be called by derived sub-classes, and only from the create() function,
799 * as the stream-lock needs to be held.
801 * The format for the new segment will be the current format of the source, as
802 * configured with gst_base_src_set_format()
804 * Returns: %TRUE if preparation of the seamless segment succeeded.
809 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
814 GST_DEBUG_OBJECT (src,
815 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
816 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
817 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
819 GST_OBJECT_LOCK (src);
821 src->segment.base = gst_segment_to_running_time (&src->segment,
822 src->segment.format, src->segment.position);
823 src->segment.start = start;
824 src->segment.stop = stop;
825 src->segment.position = position;
827 /* forward, we send data from position to stop */
828 src->priv->segment_pending = TRUE;
829 GST_OBJECT_UNLOCK (src);
831 src->priv->discont = TRUE;
838 * gst_base_src_set_caps:
839 * @src: a #GstBaseSrc
842 * Set new caps on the basesrc source pad.
844 * Returns: %TRUE if the caps could be set
847 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
849 GstBaseSrcClass *bclass;
852 bclass = GST_BASE_SRC_GET_CLASS (src);
854 gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
856 if (bclass->set_caps)
857 res = bclass->set_caps (src, caps);
863 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
865 GstCaps *caps = NULL;
866 GstPadTemplate *pad_template;
867 GstBaseSrcClass *bclass;
869 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
872 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
874 if (pad_template != NULL) {
875 caps = gst_pad_template_get_caps (pad_template);
878 GstCaps *intersection;
881 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
882 gst_caps_unref (caps);
890 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
892 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
893 gst_caps_fixate (caps);
897 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
899 GstBaseSrcClass *bclass;
901 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
904 bclass->fixate (bsrc, caps);
908 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
912 switch (GST_QUERY_TYPE (query)) {
913 case GST_QUERY_POSITION:
917 gst_query_parse_position (query, &format, NULL);
919 GST_DEBUG_OBJECT (src, "position query in format %s",
920 gst_format_get_name (format));
923 case GST_FORMAT_PERCENT:
929 GST_OBJECT_LOCK (src);
930 position = src->segment.position;
931 duration = src->segment.duration;
932 GST_OBJECT_UNLOCK (src);
934 if (position != -1 && duration != -1) {
935 if (position < duration)
936 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
939 percent = GST_FORMAT_PERCENT_MAX;
943 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
950 GstFormat seg_format;
952 GST_OBJECT_LOCK (src);
954 gst_segment_to_stream_time (&src->segment, src->segment.format,
955 src->segment.position);
956 seg_format = src->segment.format;
957 GST_OBJECT_UNLOCK (src);
959 if (position != -1) {
960 /* convert to requested format */
962 gst_pad_query_convert (src->srcpad, seg_format,
963 position, format, &position);
967 gst_query_set_position (query, format, position);
973 case GST_QUERY_DURATION:
977 gst_query_parse_duration (query, &format, NULL);
979 GST_DEBUG_OBJECT (src, "duration query in format %s",
980 gst_format_get_name (format));
983 case GST_FORMAT_PERCENT:
984 gst_query_set_duration (query, GST_FORMAT_PERCENT,
985 GST_FORMAT_PERCENT_MAX);
991 GstFormat seg_format;
994 /* may have to refresh duration */
995 if (g_atomic_int_get (&src->priv->dynamic_size))
996 gst_base_src_update_length (src, 0, &length);
998 /* this is the duration as configured by the subclass. */
999 GST_OBJECT_LOCK (src);
1000 duration = src->segment.duration;
1001 seg_format = src->segment.format;
1002 GST_OBJECT_UNLOCK (src);
1004 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1005 duration, gst_format_get_name (seg_format));
1007 if (duration != -1) {
1008 /* convert to requested format, if this fails, we have a duration
1009 * but we cannot answer the query, we must return FALSE. */
1011 gst_pad_query_convert (src->srcpad, seg_format,
1012 duration, format, &duration);
1014 /* The subclass did not configure a duration, we assume that the
1015 * media has an unknown duration then and we return TRUE to report
1016 * this. Note that this is not the same as returning FALSE, which
1017 * means that we cannot report the duration at all. */
1020 gst_query_set_duration (query, format, duration);
1027 case GST_QUERY_SEEKING:
1029 GstFormat format, seg_format;
1032 GST_OBJECT_LOCK (src);
1033 duration = src->segment.duration;
1034 seg_format = src->segment.format;
1035 GST_OBJECT_UNLOCK (src);
1037 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1038 if (format == seg_format) {
1039 gst_query_set_seeking (query, seg_format,
1040 gst_base_src_seekable (src), 0, duration);
1043 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1044 /* Don't reply to the query to make up for demuxers which don't
1045 * handle the SEEKING query yet. Players like Totem will fall back
1046 * to the duration when the SEEKING query isn't answered. */
1051 case GST_QUERY_SEGMENT:
1055 GST_OBJECT_LOCK (src);
1056 /* no end segment configured, current duration then */
1057 if ((stop = src->segment.stop) == -1)
1058 stop = src->segment.duration;
1059 start = src->segment.start;
1061 /* adjust to stream time */
1062 if (src->segment.time != -1) {
1063 start -= src->segment.time;
1065 stop -= src->segment.time;
1068 gst_query_set_segment (query, src->segment.rate, src->segment.format,
1070 GST_OBJECT_UNLOCK (src);
1075 case GST_QUERY_FORMATS:
1077 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1078 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1082 case GST_QUERY_CONVERT:
1084 GstFormat src_fmt, dest_fmt;
1085 gint64 src_val, dest_val;
1087 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1089 /* we can only convert between equal formats... */
1090 if (src_fmt == dest_fmt) {
1096 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1099 case GST_QUERY_LATENCY:
1101 GstClockTime min, max;
1104 /* Subclasses should override and implement something useful */
1105 res = gst_base_src_query_latency (src, &live, &min, &max);
1107 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1108 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1109 GST_TIME_ARGS (max));
1111 gst_query_set_latency (query, live, min, max);
1114 case GST_QUERY_JITTER:
1115 case GST_QUERY_RATE:
1118 case GST_QUERY_BUFFERING:
1120 GstFormat format, seg_format;
1121 gint64 start, stop, estimated;
1123 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1125 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1126 gst_format_get_name (format));
1128 GST_OBJECT_LOCK (src);
1129 if (src->random_access) {
1132 if (format == GST_FORMAT_PERCENT)
1133 stop = GST_FORMAT_PERCENT_MAX;
1135 stop = src->segment.duration;
1141 seg_format = src->segment.format;
1142 GST_OBJECT_UNLOCK (src);
1144 /* convert to required format. When the conversion fails, we can't answer
1145 * the query. When the value is unknown, we can don't perform conversion
1146 * but report TRUE. */
1147 if (format != GST_FORMAT_PERCENT && stop != -1) {
1148 res = gst_pad_query_convert (src->srcpad, seg_format,
1149 stop, format, &stop);
1153 if (res && format != GST_FORMAT_PERCENT && start != -1)
1154 res = gst_pad_query_convert (src->srcpad, seg_format,
1155 start, format, &start);
1157 gst_query_set_buffering_range (query, format, start, stop, estimated);
1160 case GST_QUERY_SCHEDULING:
1162 gboolean random_access;
1164 random_access = gst_base_src_is_random_access (src);
1166 /* we can operate in getrange mode if the native format is bytes
1167 * and we are seekable, this condition is set in the random_access
1168 * flag and is set in the _start() method. */
1169 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1171 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1172 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1177 case GST_QUERY_CAPS:
1179 GstBaseSrcClass *bclass;
1180 GstCaps *caps, *filter;
1182 bclass = GST_BASE_SRC_GET_CLASS (src);
1183 if (bclass->get_caps) {
1184 gst_query_parse_caps (query, &filter);
1185 caps = bclass->get_caps (src, filter);
1186 gst_query_set_caps_result (query, caps);
1187 gst_caps_unref (caps);
1197 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1204 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1207 GstBaseSrcClass *bclass;
1208 gboolean result = FALSE;
1210 src = GST_BASE_SRC (parent);
1211 bclass = GST_BASE_SRC_GET_CLASS (src);
1214 result = bclass->query (src, query);
1220 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1222 gboolean res = TRUE;
1224 /* update our offset if the start/stop position was updated */
1225 if (segment->format == GST_FORMAT_BYTES) {
1226 segment->time = segment->start;
1227 } else if (segment->start == 0) {
1228 /* seek to start, we can implement a default for this. */
1232 GST_INFO_OBJECT (src, "Can't do a default seek");
1239 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1241 GstBaseSrcClass *bclass;
1242 gboolean result = FALSE;
1244 bclass = GST_BASE_SRC_GET_CLASS (src);
1246 if (bclass->do_seek)
1247 result = bclass->do_seek (src, segment);
1252 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1255 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1256 GstSegment * segment)
1258 /* By default, we try one of 2 things:
1259 * - For absolute seek positions, convert the requested position to our
1260 * configured processing format and place it in the output segment \
1261 * - For relative seek positions, convert our current (input) values to the
1262 * seek format, adjust by the relative seek offset and then convert back to
1263 * the processing format
1265 GstSeekType cur_type, stop_type;
1268 GstFormat seek_format, dest_format;
1271 gboolean res = TRUE;
1273 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1274 &cur_type, &cur, &stop_type, &stop);
1275 dest_format = segment->format;
1277 if (seek_format == dest_format) {
1278 gst_segment_do_seek (segment, rate, seek_format, flags,
1279 cur_type, cur, stop_type, stop, &update);
1283 if (cur_type != GST_SEEK_TYPE_NONE) {
1284 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1286 gst_pad_query_convert (src->srcpad, seek_format, cur, dest_format,
1288 cur_type = GST_SEEK_TYPE_SET;
1291 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1292 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1294 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1296 stop_type = GST_SEEK_TYPE_SET;
1299 /* And finally, configure our output segment in the desired format */
1300 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
1301 stop_type, stop, &update);
1310 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1316 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1317 GstSegment * seeksegment)
1319 GstBaseSrcClass *bclass;
1320 gboolean result = FALSE;
1322 bclass = GST_BASE_SRC_GET_CLASS (src);
1324 if (bclass->prepare_seek_segment)
1325 result = bclass->prepare_seek_segment (src, event, seeksegment);
1330 static GstFlowReturn
1331 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1332 guint size, GstBuffer ** buffer)
1335 GstBaseSrcPrivate *priv = src->priv;
1338 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1339 } else if (size != -1) {
1340 *buffer = gst_buffer_new_allocate (priv->allocator, size, priv->alignment);
1341 if (G_UNLIKELY (*buffer == NULL))
1346 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1355 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1356 return GST_FLOW_ERROR;
1360 static GstFlowReturn
1361 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1362 guint size, GstBuffer ** buffer)
1364 GstBaseSrcClass *bclass;
1367 bclass = GST_BASE_SRC_GET_CLASS (src);
1369 if (G_UNLIKELY (!bclass->alloc))
1371 if (G_UNLIKELY (!bclass->fill))
1374 ret = bclass->alloc (src, offset, size, buffer);
1375 if (G_UNLIKELY (ret != GST_FLOW_OK))
1378 if (G_LIKELY (size > 0)) {
1379 /* only call fill when there is a size */
1380 ret = bclass->fill (src, offset, size, *buffer);
1381 if (G_UNLIKELY (ret != GST_FLOW_OK))
1390 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1391 return GST_FLOW_NOT_SUPPORTED;
1395 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1400 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1401 gst_flow_get_name (ret));
1402 gst_buffer_unref (*buffer);
1407 /* this code implements the seeking. It is a good example
1408 * handling all cases.
1410 * A seek updates the currently configured segment.start
1411 * and segment.stop values based on the SEEK_TYPE. If the
1412 * segment.start value is updated, a seek to this new position
1413 * should be performed.
1415 * The seek can only be executed when we are not currently
1416 * streaming any data, to make sure that this is the case, we
1417 * acquire the STREAM_LOCK which is taken when we are in the
1418 * _loop() function or when a getrange() is called. Normally
1419 * we will not receive a seek if we are operating in pull mode
1420 * though. When we operate as a live source we might block on the live
1421 * cond, which does not release the STREAM_LOCK. Therefore we will try
1422 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1423 * safe to perform the seek.
1425 * When we are in the loop() function, we might be in the middle
1426 * of pushing a buffer, which might block in a sink. To make sure
1427 * that the push gets unblocked we push out a FLUSH_START event.
1428 * Our loop function will get a WRONG_STATE return value from
1429 * the push and will pause, effectively releasing the STREAM_LOCK.
1431 * For a non-flushing seek, we pause the task, which might eventually
1432 * release the STREAM_LOCK. We say eventually because when the sink
1433 * blocks on the sample we might wait a very long time until the sink
1434 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1435 * can continue the seek. A non-flushing seek is normally done in a
1436 * running pipeline to perform seamless playback, this means that the sink is
1437 * PLAYING and will return from its chain function.
1438 * In the case of a non-flushing seek we need to make sure that the
1439 * data we output after the seek is continuous with the previous data,
1440 * this is because a non-flushing seek does not reset the running-time
1441 * to 0. We do this by closing the currently running segment, ie. sending
1442 * a new_segment event with the stop position set to the last processed
1445 * After updating the segment.start/stop values, we prepare for
1446 * streaming again. We push out a FLUSH_STOP to make the peer pad
1447 * accept data again and we start our task again.
1449 * A segment seek posts a message on the bus saying that the playback
1450 * of the segment started. We store the segment flag internally because
1451 * when we reach the segment.stop we have to post a segment.done
1452 * instead of EOS when doing a segment seek.
1454 /* FIXME (0.11), we have the unlock gboolean here because most current
1455 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1456 * the streaming thread isn't running, resulting in bogus unlocks later when it
1457 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1458 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1462 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1464 gboolean res = TRUE, tres;
1466 GstFormat seek_format, dest_format;
1468 GstSeekType cur_type, stop_type;
1470 gboolean flush, playing;
1472 gboolean relative_seek = FALSE;
1473 gboolean seekseg_configured = FALSE;
1474 GstSegment seeksegment;
1478 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1480 GST_OBJECT_LOCK (src);
1481 dest_format = src->segment.format;
1482 GST_OBJECT_UNLOCK (src);
1485 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1486 &cur_type, &cur, &stop_type, &stop);
1488 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1489 SEEK_TYPE_IS_RELATIVE (stop_type);
1491 if (dest_format != seek_format && !relative_seek) {
1492 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1493 * here before taking the stream lock, otherwise we must convert it later,
1494 * once we have the stream lock and can read the last configures segment
1495 * start and stop positions */
1496 gst_segment_init (&seeksegment, dest_format);
1498 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1499 goto prepare_failed;
1501 seekseg_configured = TRUE;
1504 flush = flags & GST_SEEK_FLAG_FLUSH;
1505 seqnum = gst_event_get_seqnum (event);
1508 /* get next seqnum */
1509 seqnum = gst_util_seqnum_next ();
1512 /* send flush start */
1514 tevent = gst_event_new_flush_start ();
1515 gst_event_set_seqnum (tevent, seqnum);
1516 gst_pad_push_event (src->srcpad, tevent);
1518 gst_pad_pause_task (src->srcpad);
1520 /* unblock streaming thread. */
1521 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1523 /* grab streaming lock, this should eventually be possible, either
1524 * because the task is paused, our streaming thread stopped
1525 * or because our peer is flushing. */
1526 GST_PAD_STREAM_LOCK (src->srcpad);
1527 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1528 /* we have seen this event before, issue a warning for now */
1529 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1532 src->priv->seqnum = seqnum;
1533 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1536 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1538 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1539 * copy the current segment info into the temp segment that we can actually
1540 * attempt the seek with. We only update the real segment if the seek succeeds. */
1541 if (!seekseg_configured) {
1542 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1544 /* now configure the final seek segment */
1546 if (seeksegment.format != seek_format) {
1547 /* OK, here's where we give the subclass a chance to convert the relative
1548 * seek into an absolute one in the processing format. We set up any
1549 * absolute seek above, before taking the stream lock. */
1550 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1551 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1556 /* The seek format matches our processing format, no need to ask the
1557 * the subclass to configure the segment. */
1558 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1559 cur_type, cur, stop_type, stop, &update);
1562 /* Else, no seek event passed, so we're just (re)starting the
1567 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1568 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1569 seeksegment.start, seeksegment.stop, seeksegment.position);
1571 /* do the seek, segment.position contains the new position. */
1572 res = gst_base_src_do_seek (src, &seeksegment);
1575 /* and prepare to continue streaming */
1577 tevent = gst_event_new_flush_stop (TRUE);
1578 gst_event_set_seqnum (tevent, seqnum);
1579 /* send flush stop, peer will accept data and events again. We
1580 * are not yet providing data as we still have the STREAM_LOCK. */
1581 gst_pad_push_event (src->srcpad, tevent);
1584 /* The subclass must have converted the segment to the processing format
1586 if (res && seeksegment.format != dest_format) {
1587 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1588 "in the correct format. Aborting seek.");
1592 /* if the seek was successful, we update our real segment and push
1593 * out the new segment. */
1595 GST_OBJECT_LOCK (src);
1596 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1597 GST_OBJECT_UNLOCK (src);
1599 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1600 GstMessage *message;
1602 message = gst_message_new_segment_start (GST_OBJECT (src),
1603 seeksegment.format, seeksegment.position);
1604 gst_message_set_seqnum (message, seqnum);
1606 gst_element_post_message (GST_ELEMENT (src), message);
1609 /* for deriving a stop position for the playback segment from the seek
1610 * segment, we must take the duration when the stop is not set */
1611 if ((stop = seeksegment.stop) == -1)
1612 stop = seeksegment.duration;
1614 src->priv->segment_pending = TRUE;
1617 src->priv->discont = TRUE;
1618 src->running = TRUE;
1619 /* and restart the task in case it got paused explicitly or by
1620 * the FLUSH_START event we pushed out. */
1621 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1626 /* and release the lock again so we can continue streaming */
1627 GST_PAD_STREAM_UNLOCK (src->srcpad);
1633 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1638 /* all events send to this element directly. This is mainly done from the
1642 gst_base_src_send_event (GstElement * element, GstEvent * event)
1645 gboolean result = FALSE;
1647 src = GST_BASE_SRC (element);
1649 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1651 switch (GST_EVENT_TYPE (event)) {
1652 /* bidirectional events */
1653 case GST_EVENT_FLUSH_START:
1654 case GST_EVENT_FLUSH_STOP:
1655 /* sending random flushes downstream can break stuff,
1656 * especially sync since all segment info will get flushed */
1659 /* downstream serialized events */
1662 GstBaseSrcClass *bclass;
1664 bclass = GST_BASE_SRC_GET_CLASS (src);
1666 /* queue EOS and make sure the task or pull function performs the EOS
1669 * We have two possibilities:
1671 * - Before we are to enter the _create function, we check the pending_eos
1672 * first and do EOS instead of entering it.
1673 * - If we are in the _create function or we did not manage to set the
1674 * flag fast enough and we are about to enter the _create function,
1675 * we unlock it so that we exit with WRONG_STATE immediately. We then
1676 * check the EOS flag and do the EOS logic.
1678 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1679 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1682 /* unlock the _create function so that we can check the pending_eos flag
1683 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1684 * that we can grab it and stop the unlock again. We don't take the stream
1685 * lock so that this operation is guaranteed to never block. */
1686 gst_base_src_activate_pool (src, FALSE);
1688 bclass->unlock (src);
1690 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1692 GST_LIVE_LOCK (src);
1693 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1694 /* now stop the unlock of the streaming thread again. Grabbing the live
1695 * lock is enough because that protects the create function. */
1696 if (bclass->unlock_stop)
1697 bclass->unlock_stop (src);
1698 gst_base_src_activate_pool (src, TRUE);
1699 GST_LIVE_UNLOCK (src);
1704 case GST_EVENT_SEGMENT:
1705 /* sending random SEGMENT downstream can break sync. */
1708 case GST_EVENT_CUSTOM_DOWNSTREAM:
1709 case GST_EVENT_CUSTOM_BOTH:
1710 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1711 GST_OBJECT_LOCK (src);
1712 src->priv->pending_events =
1713 g_list_append (src->priv->pending_events, event);
1714 g_atomic_int_set (&src->priv->have_events, TRUE);
1715 GST_OBJECT_UNLOCK (src);
1719 case GST_EVENT_BUFFERSIZE:
1720 /* does not seem to make much sense currently */
1723 /* upstream events */
1725 /* elements should override send_event and do something */
1727 case GST_EVENT_SEEK:
1731 GST_OBJECT_LOCK (src->srcpad);
1732 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1734 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1735 GST_OBJECT_UNLOCK (src->srcpad);
1738 GST_DEBUG_OBJECT (src, "performing seek");
1739 /* when we are running in push mode, we can execute the
1740 * seek right now, we need to unlock. */
1741 result = gst_base_src_perform_seek (src, event, TRUE);
1745 /* else we store the event and execute the seek when we
1747 GST_OBJECT_LOCK (src);
1748 GST_DEBUG_OBJECT (src, "queueing seek");
1749 event_p = &src->pending_seek;
1750 gst_event_replace ((GstEvent **) event_p, event);
1751 GST_OBJECT_UNLOCK (src);
1752 /* assume the seek will work */
1757 case GST_EVENT_NAVIGATION:
1758 /* could make sense for elements that do something with navigation events
1759 * but then they would need to override the send_event function */
1761 case GST_EVENT_LATENCY:
1762 /* does not seem to make sense currently */
1766 case GST_EVENT_CUSTOM_UPSTREAM:
1767 /* override send_event if you want this */
1769 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1770 case GST_EVENT_CUSTOM_BOTH_OOB:
1771 /* insert a random custom event into the pipeline */
1772 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1773 result = gst_pad_push_event (src->srcpad, event);
1774 /* we gave away the ref to the event in the push */
1781 /* if we still have a ref to the event, unref it now */
1783 gst_event_unref (event);
1790 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1791 GST_OBJECT_UNLOCK (src->srcpad);
1798 gst_base_src_seekable (GstBaseSrc * src)
1800 GstBaseSrcClass *bclass;
1801 bclass = GST_BASE_SRC_GET_CLASS (src);
1802 if (bclass->is_seekable)
1803 return bclass->is_seekable (src);
1809 gst_base_src_update_qos (GstBaseSrc * src,
1810 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1812 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1813 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1814 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1816 GST_OBJECT_LOCK (src);
1817 src->priv->proportion = proportion;
1818 src->priv->earliest_time = timestamp + diff;
1819 GST_OBJECT_UNLOCK (src);
1824 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1828 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1830 switch (GST_EVENT_TYPE (event)) {
1831 case GST_EVENT_SEEK:
1832 /* is normally called when in push mode */
1833 if (!gst_base_src_seekable (src))
1836 result = gst_base_src_perform_seek (src, event, TRUE);
1838 case GST_EVENT_FLUSH_START:
1839 /* cancel any blocking getrange, is normally called
1840 * when in pull mode. */
1841 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1843 case GST_EVENT_FLUSH_STOP:
1844 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1849 GstClockTimeDiff diff;
1850 GstClockTime timestamp;
1852 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1853 gst_base_src_update_qos (src, proportion, diff, timestamp);
1857 case GST_EVENT_RECONFIGURE:
1869 GST_DEBUG_OBJECT (src, "is not seekable");
1875 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1878 GstBaseSrcClass *bclass;
1879 gboolean result = FALSE;
1881 src = GST_BASE_SRC (parent);
1882 bclass = GST_BASE_SRC_GET_CLASS (src);
1884 if (bclass->event) {
1885 if (!(result = bclass->event (src, event)))
1886 goto subclass_failed;
1890 gst_event_unref (event);
1897 GST_DEBUG_OBJECT (src, "subclass refused event");
1903 gst_base_src_set_property (GObject * object, guint prop_id,
1904 const GValue * value, GParamSpec * pspec)
1908 src = GST_BASE_SRC (object);
1911 case PROP_BLOCKSIZE:
1912 gst_base_src_set_blocksize (src, g_value_get_uint (value));
1914 case PROP_NUM_BUFFERS:
1915 src->num_buffers = g_value_get_int (value);
1918 src->typefind = g_value_get_boolean (value);
1920 case PROP_DO_TIMESTAMP:
1921 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1924 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1930 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1935 src = GST_BASE_SRC (object);
1938 case PROP_BLOCKSIZE:
1939 g_value_set_uint (value, gst_base_src_get_blocksize (src));
1941 case PROP_NUM_BUFFERS:
1942 g_value_set_int (value, src->num_buffers);
1945 g_value_set_boolean (value, src->typefind);
1947 case PROP_DO_TIMESTAMP:
1948 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1951 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1956 /* with STREAM_LOCK and LOCK */
1957 static GstClockReturn
1958 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1963 id = gst_clock_new_single_shot_id (clock, time);
1965 basesrc->clock_id = id;
1966 /* release the live lock while waiting */
1967 GST_LIVE_UNLOCK (basesrc);
1969 ret = gst_clock_id_wait (id, NULL);
1971 GST_LIVE_LOCK (basesrc);
1972 gst_clock_id_unref (id);
1973 basesrc->clock_id = NULL;
1978 /* perform synchronisation on a buffer.
1981 static GstClockReturn
1982 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1984 GstClockReturn result;
1985 GstClockTime start, end;
1986 GstBaseSrcClass *bclass;
1987 GstClockTime base_time;
1989 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1990 gboolean do_timestamp, first, pseudo_live;
1992 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1995 if (bclass->get_times)
1996 bclass->get_times (basesrc, buffer, &start, &end);
1998 /* get buffer timestamp */
1999 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2001 /* grab the lock to prepare for clocking and calculate the startup
2003 GST_OBJECT_LOCK (basesrc);
2005 /* if we are asked to sync against the clock we are a pseudo live element */
2006 pseudo_live = (start != -1 && basesrc->is_live);
2007 /* check for the first buffer */
2008 first = (basesrc->priv->latency == -1);
2010 if (timestamp != -1 && pseudo_live) {
2011 GstClockTime latency;
2013 /* we have a timestamp and a sync time, latency is the diff */
2014 if (timestamp <= start)
2015 latency = start - timestamp;
2020 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2021 GST_TIME_ARGS (latency));
2022 /* first time we calculate latency, just configure */
2023 basesrc->priv->latency = latency;
2025 if (basesrc->priv->latency != latency) {
2026 /* we have a new latency, FIXME post latency message */
2027 basesrc->priv->latency = latency;
2028 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2029 GST_TIME_ARGS (latency));
2033 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2034 basesrc->is_live, start != -1);
2035 basesrc->priv->latency = 0;
2038 /* get clock, if no clock, we can't sync or do timestamps */
2039 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2042 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2044 do_timestamp = basesrc->priv->do_timestamp;
2046 /* first buffer, calculate the timestamp offset */
2048 GstClockTime running_time;
2050 now = gst_clock_get_time (clock);
2051 running_time = now - base_time;
2053 GST_LOG_OBJECT (basesrc,
2054 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
2055 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
2056 GST_TIME_ARGS (running_time));
2058 if (pseudo_live && timestamp != -1) {
2059 /* live source and we need to sync, add startup latency to all timestamps
2060 * to get the real running_time. Live sources should always timestamp
2061 * according to the current running time. */
2062 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2064 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2065 GST_TIME_ARGS (basesrc->priv->ts_offset));
2067 basesrc->priv->ts_offset = 0;
2068 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2071 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
2073 timestamp = running_time;
2077 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
2079 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2080 GST_TIME_ARGS (timestamp));
2083 /* add the timestamp offset we need for sync */
2084 timestamp += basesrc->priv->ts_offset;
2086 /* not the first buffer, the timestamp is the diff between the clock and
2088 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
2089 now = gst_clock_get_time (clock);
2091 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
2093 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2094 GST_TIME_ARGS (now - base_time));
2098 /* if we don't have a buffer timestamp, we don't sync */
2099 if (!GST_CLOCK_TIME_IS_VALID (start))
2102 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
2103 /* for pseudo live sources, add our ts_offset to the timestamp */
2104 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
2105 start += basesrc->priv->ts_offset;
2108 GST_LOG_OBJECT (basesrc,
2109 "waiting for clock, base time %" GST_TIME_FORMAT
2110 ", stream_start %" GST_TIME_FORMAT,
2111 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2112 GST_OBJECT_UNLOCK (basesrc);
2114 result = gst_base_src_wait (basesrc, clock, start + base_time);
2116 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2123 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2124 GST_OBJECT_UNLOCK (basesrc);
2125 return GST_CLOCK_OK;
2129 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2130 GST_OBJECT_UNLOCK (basesrc);
2131 return GST_CLOCK_OK;
2135 /* Called with STREAM_LOCK and LIVE_LOCK */
2137 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
2139 guint64 size, maxsize;
2140 GstBaseSrcClass *bclass;
2145 bclass = GST_BASE_SRC_GET_CLASS (src);
2147 format = src->segment.format;
2148 stop = src->segment.stop;
2149 /* get total file size */
2150 size = src->segment.duration;
2152 /* only operate if we are working with bytes */
2153 if (format != GST_FORMAT_BYTES)
2156 /* the max amount of bytes to read is the total size or
2157 * up to the segment.stop if present. */
2159 maxsize = MIN (size, stop);
2163 GST_DEBUG_OBJECT (src,
2164 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2165 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2166 *length, size, stop, maxsize);
2168 dynamic = g_atomic_int_get (&src->priv->dynamic_size);
2169 GST_DEBUG_OBJECT (src, "dynamic size: %d", dynamic);
2171 /* check size if we have one */
2172 if (maxsize != -1) {
2173 /* if we run past the end, check if the file became bigger and
2175 if (G_UNLIKELY (offset + *length >= maxsize || dynamic)) {
2176 /* see if length of the file changed */
2177 if (bclass->get_size)
2178 if (!bclass->get_size (src, &size))
2181 /* make sure we don't exceed the configured segment stop
2184 maxsize = MIN (size, stop);
2188 /* if we are at or past the end, EOS */
2189 if (G_UNLIKELY (offset >= maxsize))
2190 goto unexpected_length;
2192 /* else we can clip to the end */
2193 if (G_UNLIKELY (offset + *length >= maxsize))
2194 *length = maxsize - offset;
2199 /* keep track of current duration.
2200 * segment is in bytes, we checked that above. */
2201 GST_OBJECT_LOCK (src);
2202 src->segment.duration = size;
2203 GST_OBJECT_UNLOCK (src);
2214 /* must be called with LIVE_LOCK */
2215 static GstFlowReturn
2216 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2220 GstBaseSrcClass *bclass;
2221 GstClockReturn status;
2223 bclass = GST_BASE_SRC_GET_CLASS (src);
2227 if (G_UNLIKELY (!src->live_running)) {
2228 ret = gst_base_src_wait_playing (src);
2229 if (ret != GST_FLOW_OK)
2234 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)))
2237 if (G_UNLIKELY (!bclass->create))
2240 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2241 goto unexpected_length;
2243 /* track position */
2244 GST_OBJECT_LOCK (src);
2245 if (src->segment.format == GST_FORMAT_BYTES)
2246 src->segment.position = offset;
2247 GST_OBJECT_UNLOCK (src);
2249 /* normally we don't count buffers */
2250 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2251 if (src->num_buffers_left == 0)
2252 goto reached_num_buffers;
2254 src->num_buffers_left--;
2257 /* don't enter the create function if a pending EOS event was set. For the
2258 * logic of the pending_eos, check the event function of this class. */
2259 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2262 GST_DEBUG_OBJECT (src,
2263 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2264 G_GINT64_FORMAT, offset, length, src->segment.time);
2266 ret = bclass->create (src, offset, length, buf);
2268 /* The create function could be unlocked because we have a pending EOS. It's
2269 * possible that we have a valid buffer from create that we need to
2270 * discard when the create function returned _OK. */
2271 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2272 if (ret == GST_FLOW_OK) {
2273 gst_buffer_unref (*buf);
2279 if (G_UNLIKELY (ret != GST_FLOW_OK))
2282 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2283 if (offset == 0 && src->segment.time == 0
2284 && GST_BUFFER_TIMESTAMP (*buf) == -1 && !src->is_live) {
2285 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2286 *buf = gst_buffer_make_writable (*buf);
2287 GST_BUFFER_TIMESTAMP (*buf) = 0;
2290 /* now sync before pushing the buffer */
2291 status = gst_base_src_do_sync (src, *buf);
2293 /* waiting for the clock could have made us flushing */
2294 if (G_UNLIKELY (src->priv->flushing))
2298 case GST_CLOCK_EARLY:
2299 /* the buffer is too late. We currently don't drop the buffer. */
2300 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2303 /* buffer synchronised properly */
2304 GST_DEBUG_OBJECT (src, "buffer ok");
2306 case GST_CLOCK_UNSCHEDULED:
2307 /* this case is triggered when we were waiting for the clock and
2308 * it got unlocked because we did a state change. In any case, get rid of
2310 gst_buffer_unref (*buf);
2312 if (!src->live_running) {
2313 /* We return WRONG_STATE when we are not running to stop the dataflow also
2314 * get rid of the produced buffer. */
2315 GST_DEBUG_OBJECT (src,
2316 "clock was unscheduled (%d), returning WRONG_STATE", status);
2317 ret = GST_FLOW_WRONG_STATE;
2319 /* If we are running when this happens, we quickly switched between
2320 * pause and playing. We try to produce a new buffer */
2321 GST_DEBUG_OBJECT (src,
2322 "clock was unscheduled (%d), but we are running", status);
2327 /* all other result values are unexpected and errors */
2328 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2329 (_("Internal clock error.")),
2330 ("clock returned unexpected return value %d", status));
2331 gst_buffer_unref (*buf);
2333 ret = GST_FLOW_ERROR;
2341 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2342 gst_flow_get_name (ret));
2347 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2348 gst_flow_get_name (ret));
2353 GST_DEBUG_OBJECT (src, "getrange but not started");
2354 return GST_FLOW_WRONG_STATE;
2358 GST_DEBUG_OBJECT (src, "no create function");
2359 return GST_FLOW_NOT_SUPPORTED;
2363 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2364 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2365 return GST_FLOW_EOS;
2367 reached_num_buffers:
2369 GST_DEBUG_OBJECT (src, "sent all buffers");
2370 return GST_FLOW_EOS;
2374 GST_DEBUG_OBJECT (src, "we are flushing");
2375 gst_buffer_unref (*buf);
2377 return GST_FLOW_WRONG_STATE;
2381 GST_DEBUG_OBJECT (src, "we are EOS");
2382 return GST_FLOW_EOS;
2386 static GstFlowReturn
2387 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2388 guint length, GstBuffer ** buf)
2393 src = GST_BASE_SRC_CAST (parent);
2395 GST_LIVE_LOCK (src);
2396 if (G_UNLIKELY (src->priv->flushing))
2399 res = gst_base_src_get_range (src, offset, length, buf);
2402 GST_LIVE_UNLOCK (src);
2409 GST_DEBUG_OBJECT (src, "we are flushing");
2410 res = GST_FLOW_WRONG_STATE;
2416 gst_base_src_is_random_access (GstBaseSrc * src)
2418 /* we need to start the basesrc to check random access */
2419 if (!GST_BASE_SRC_IS_STARTED (src)) {
2420 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2421 if (G_LIKELY (gst_base_src_start (src))) {
2422 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2424 gst_base_src_stop (src);
2428 return src->random_access;
2433 GST_DEBUG_OBJECT (src, "failed to start");
2439 gst_base_src_loop (GstPad * pad)
2442 GstBuffer *buf = NULL;
2447 GList *pending_events = NULL, *tmp;
2451 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2453 /* check if we need to renegotiate */
2454 if (gst_pad_check_reconfigure (pad)) {
2455 if (!gst_base_src_negotiate (src))
2456 goto not_negotiated;
2459 GST_LIVE_LOCK (src);
2461 if (G_UNLIKELY (src->priv->flushing))
2464 blocksize = src->blocksize;
2466 /* if we operate in bytes, we can calculate an offset */
2467 if (src->segment.format == GST_FORMAT_BYTES) {
2468 position = src->segment.position;
2469 /* for negative rates, start with subtracting the blocksize */
2470 if (src->segment.rate < 0.0) {
2471 /* we cannot go below segment.start */
2472 if (position > src->segment.start + blocksize)
2473 position -= blocksize;
2475 /* last block, remainder up to segment.start */
2476 blocksize = position - src->segment.start;
2477 position = src->segment.start;
2483 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2484 GST_TIME_ARGS (position), blocksize);
2486 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2487 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2488 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2489 gst_flow_get_name (ret));
2490 GST_LIVE_UNLOCK (src);
2493 /* this should not happen */
2494 if (G_UNLIKELY (buf == NULL))
2497 /* push events to close/start our segment before we push the buffer. */
2498 if (G_UNLIKELY (src->priv->segment_pending)) {
2499 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2500 src->priv->segment_pending = FALSE;
2503 if (g_atomic_int_get (&src->priv->have_events)) {
2504 GST_OBJECT_LOCK (src);
2505 /* take the events */
2506 pending_events = src->priv->pending_events;
2507 src->priv->pending_events = NULL;
2508 g_atomic_int_set (&src->priv->have_events, FALSE);
2509 GST_OBJECT_UNLOCK (src);
2512 /* Push out pending events if any */
2513 if (G_UNLIKELY (pending_events != NULL)) {
2514 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2515 GstEvent *ev = (GstEvent *) tmp->data;
2516 gst_pad_push_event (pad, ev);
2518 g_list_free (pending_events);
2521 /* figure out the new position */
2522 switch (src->segment.format) {
2523 case GST_FORMAT_BYTES:
2525 guint bufsize = gst_buffer_get_size (buf);
2527 /* we subtracted above for negative rates */
2528 if (src->segment.rate >= 0.0)
2529 position += bufsize;
2532 case GST_FORMAT_TIME:
2534 GstClockTime start, duration;
2536 start = GST_BUFFER_TIMESTAMP (buf);
2537 duration = GST_BUFFER_DURATION (buf);
2539 if (GST_CLOCK_TIME_IS_VALID (start))
2542 position = src->segment.position;
2544 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2545 if (src->segment.rate >= 0.0)
2546 position += duration;
2547 else if (position > duration)
2548 position -= duration;
2554 case GST_FORMAT_DEFAULT:
2555 if (src->segment.rate >= 0.0)
2556 position = GST_BUFFER_OFFSET_END (buf);
2558 position = GST_BUFFER_OFFSET (buf);
2564 if (position != -1) {
2565 if (src->segment.rate >= 0.0) {
2566 /* positive rate, check if we reached the stop */
2567 if (src->segment.stop != -1) {
2568 if (position >= src->segment.stop) {
2570 position = src->segment.stop;
2574 /* negative rate, check if we reached the start. start is always set to
2575 * something different from -1 */
2576 if (position <= src->segment.start) {
2578 position = src->segment.start;
2580 /* when going reverse, all buffers are DISCONT */
2581 src->priv->discont = TRUE;
2583 GST_OBJECT_LOCK (src);
2584 src->segment.position = position;
2585 GST_OBJECT_UNLOCK (src);
2588 if (G_UNLIKELY (src->priv->discont)) {
2589 GST_INFO_OBJECT (src, "marking pending DISCONT");
2590 buf = gst_buffer_make_writable (buf);
2591 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2592 src->priv->discont = FALSE;
2594 GST_LIVE_UNLOCK (src);
2596 ret = gst_pad_push (pad, buf);
2597 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2598 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2599 gst_flow_get_name (ret));
2603 if (G_UNLIKELY (eos)) {
2604 GST_INFO_OBJECT (src, "pausing after end of segment");
2615 GST_DEBUG_OBJECT (src, "Failed to renegotiate");
2616 ret = GST_FLOW_NOT_NEGOTIATED;
2621 GST_DEBUG_OBJECT (src, "we are flushing");
2622 GST_LIVE_UNLOCK (src);
2623 ret = GST_FLOW_WRONG_STATE;
2628 const gchar *reason = gst_flow_get_name (ret);
2631 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2632 src->running = FALSE;
2633 gst_pad_pause_task (pad);
2634 if (ret == GST_FLOW_EOS) {
2635 gboolean flag_segment;
2639 /* perform EOS logic */
2640 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2641 format = src->segment.format;
2642 position = src->segment.position;
2645 GstMessage *message;
2647 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2649 gst_message_set_seqnum (message, src->priv->seqnum);
2650 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2652 event = gst_event_new_eos ();
2653 gst_event_set_seqnum (event, src->priv->seqnum);
2654 gst_pad_push_event (pad, event);
2656 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2657 event = gst_event_new_eos ();
2658 gst_event_set_seqnum (event, src->priv->seqnum);
2659 /* for fatal errors we post an error message, post the error
2660 * first so the app knows about the error first.
2661 * Also don't do this for WRONG_STATE because it happens
2662 * due to flushing and posting an error message because of
2663 * that is the wrong thing to do, e.g. when we're doing
2664 * a flushing seek. */
2665 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2666 (_("Internal data flow error.")),
2667 ("streaming task paused, reason %s (%d)", reason, ret));
2668 gst_pad_push_event (pad, event);
2674 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2675 (_("Internal data flow error.")), ("element returned NULL buffer"));
2676 GST_LIVE_UNLOCK (src);
2682 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2683 const GstAllocator * allocator, guint prefix, guint alignment)
2685 GstBufferPool *oldpool;
2686 GstBaseSrcPrivate *priv = basesrc->priv;
2689 GST_DEBUG_OBJECT (basesrc, "activate pool");
2690 if (!gst_buffer_pool_set_active (pool, TRUE))
2691 goto activate_failed;
2694 GST_OBJECT_LOCK (basesrc);
2695 oldpool = priv->pool;
2698 priv->allocator = allocator;
2700 priv->prefix = prefix;
2701 priv->alignment = alignment;
2702 GST_OBJECT_UNLOCK (basesrc);
2705 /* only deactivate if the pool is not the one we're using */
2706 if (oldpool != pool) {
2707 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2708 gst_buffer_pool_set_active (oldpool, FALSE);
2710 gst_object_unref (oldpool);
2717 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2723 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2725 GstBaseSrcPrivate *priv = basesrc->priv;
2726 GstBufferPool *pool;
2727 gboolean res = TRUE;
2729 GST_OBJECT_LOCK (basesrc);
2730 if ((pool = priv->pool))
2731 pool = gst_object_ref (pool);
2732 GST_OBJECT_UNLOCK (basesrc);
2735 res = gst_buffer_pool_set_active (pool, active);
2736 gst_object_unref (pool);
2742 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
2744 GstBaseSrcClass *bclass;
2745 gboolean result = TRUE;
2747 GstBufferPool *pool = NULL;
2748 const GstAllocator *allocator = NULL;
2749 guint size, min, max, prefix, alignment;
2751 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2753 /* make query and let peer pad answer, we don't really care if it worked or
2754 * not, if it failed, the allocation query would contain defaults and the
2755 * subclass would then set better values if needed */
2756 query = gst_query_new_allocation (caps, TRUE);
2757 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
2758 /* not a problem, just debug a little */
2759 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
2762 if (G_LIKELY (bclass->decide_allocation))
2763 result = bclass->decide_allocation (basesrc, query);
2765 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
2767 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
2771 const gchar *mem = NULL;
2773 /* no size, we have variable size buffers */
2774 if (gst_query_get_n_allocation_memories (query) > 0) {
2775 mem = gst_query_parse_nth_allocation_memory (query, 0);
2777 GST_DEBUG_OBJECT (basesrc, "0 size, getting allocator %s",
2778 GST_STR_NULL (mem));
2779 allocator = gst_allocator_find (mem);
2780 } else if (pool == NULL) {
2781 /* fixed size, we can use a bufferpool */
2782 GstStructure *config;
2784 /* we did not get a pool, make one ourselves then */
2785 pool = gst_buffer_pool_new ();
2786 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
2788 config = gst_buffer_pool_get_config (pool);
2789 gst_buffer_pool_config_set (config, caps, size, min, max, prefix,
2791 gst_buffer_pool_set_config (pool, config);
2794 gst_query_unref (query);
2797 gst_base_src_set_allocation (basesrc, pool, allocator, prefix, alignment);
2803 /* default negotiation code.
2805 * Take intersection between src and sink pads, take first
2809 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2812 GstCaps *caps = NULL;
2813 GstCaps *peercaps = NULL;
2814 gboolean result = FALSE;
2816 /* first see what is possible on our source pad */
2817 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
2818 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2819 /* nothing or anything is allowed, we're done */
2820 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2821 goto no_nego_needed;
2823 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2826 /* get the peer caps */
2827 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
2828 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2830 /* The result is already a subset of our caps */
2832 gst_caps_unref (thiscaps);
2834 /* no peer, work with our own caps then */
2837 if (caps && !gst_caps_is_empty (caps)) {
2839 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
2840 if (gst_caps_is_any (caps)) {
2841 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
2842 /* hmm, still anything, so element can do anything and
2843 * nego is not needed */
2846 caps = gst_caps_make_writable (caps);
2847 gst_base_src_fixate (basesrc, caps);
2848 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2849 if (gst_caps_is_fixed (caps)) {
2850 /* yay, fixed caps, use those then, it's possible that the subclass does
2851 * not accept this caps after all and we have to fail. */
2852 result = gst_base_src_set_caps (basesrc, caps);
2855 gst_caps_unref (caps);
2857 GST_DEBUG_OBJECT (basesrc, "no common caps");
2863 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2865 gst_caps_unref (thiscaps);
2870 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2871 ("No supported formats found"),
2872 ("This element did not produce valid caps"));
2874 gst_caps_unref (thiscaps);
2880 gst_base_src_negotiate (GstBaseSrc * basesrc)
2882 GstBaseSrcClass *bclass;
2885 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2887 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
2889 if (G_LIKELY (bclass->negotiate))
2890 result = bclass->negotiate (basesrc);
2894 if (G_LIKELY (result)) {
2897 caps = gst_pad_get_current_caps (basesrc->srcpad);
2899 result = gst_base_src_prepare_allocation (basesrc, caps);
2902 gst_caps_unref (caps);
2908 gst_base_src_start (GstBaseSrc * basesrc)
2910 GstBaseSrcClass *bclass;
2913 GST_LIVE_LOCK (basesrc);
2914 if (GST_BASE_SRC_IS_STARTING (basesrc))
2916 if (GST_BASE_SRC_IS_STARTED (basesrc))
2919 basesrc->priv->start_result = GST_FLOW_WRONG_STATE;
2920 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
2921 basesrc->num_buffers_left = basesrc->num_buffers;
2922 basesrc->running = FALSE;
2923 basesrc->priv->segment_pending = FALSE;
2924 GST_OBJECT_LOCK (basesrc);
2925 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2926 GST_OBJECT_UNLOCK (basesrc);
2927 GST_LIVE_UNLOCK (basesrc);
2929 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2931 result = bclass->start (basesrc);
2936 goto could_not_start;
2938 if (!gst_base_src_is_async (basesrc))
2939 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
2946 GST_DEBUG_OBJECT (basesrc, "was starting");
2947 GST_LIVE_UNLOCK (basesrc);
2952 GST_DEBUG_OBJECT (basesrc, "was started");
2953 GST_LIVE_UNLOCK (basesrc);
2958 GST_DEBUG_OBJECT (basesrc, "could not start");
2959 /* subclass is supposed to post a message. We don't have to call _stop. */
2960 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
2966 * gst_base_src_start_complete:
2967 * @src: base source instance
2968 * @ret: a #GstFlowReturn
2970 * Complete an asynchronous start operation. When the subclass overrides the
2971 * start method, it should call gst_base_src_start_complete() when the start
2972 * operation completes either from the same thread or from an asynchronous
2976 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
2984 GstBaseSrcClass *bclass;
2986 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2988 if (ret != GST_FLOW_OK)
2991 GST_DEBUG_OBJECT (basesrc, "starting source");
2992 format = basesrc->segment.format;
2994 /* figure out the size */
2997 if (format == GST_FORMAT_BYTES) {
2998 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3000 if (bclass->get_size) {
3001 if (!(have_size = bclass->get_size (basesrc, &size)))
3004 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3005 /* only update the size when operating in bytes, subclass is supposed
3006 * to set duration in the start method for other formats */
3007 GST_OBJECT_LOCK (basesrc);
3008 basesrc->segment.duration = size;
3009 GST_OBJECT_UNLOCK (basesrc);
3012 GST_DEBUG_OBJECT (basesrc,
3013 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3014 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3015 basesrc->segment.duration);
3017 seekable = gst_base_src_seekable (basesrc);
3018 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3020 /* update for random access flag */
3021 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3023 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3025 /* stop flushing now but for live sources, still block in the LIVE lock when
3026 * we are not yet PLAYING */
3027 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
3029 GST_OBJECT_LOCK (basesrc->srcpad);
3030 mode = GST_PAD_MODE (basesrc->srcpad);
3031 GST_OBJECT_UNLOCK (basesrc->srcpad);
3033 if (mode == GST_PAD_MODE_PUSH) {
3034 /* do initial seek, which will start the task */
3035 GST_OBJECT_LOCK (basesrc);
3036 event = basesrc->pending_seek;
3037 basesrc->pending_seek = NULL;
3038 GST_OBJECT_UNLOCK (basesrc);
3040 /* no need to unlock anything, the task is certainly
3041 * not running here. The perform seek code will start the task when
3043 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3047 gst_event_unref (event);
3049 /* if not random_access, we cannot operate in pull mode for now */
3050 if (G_UNLIKELY (!basesrc->random_access))
3054 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3056 GST_LIVE_LOCK (basesrc);
3057 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3058 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3059 basesrc->priv->start_result = ret;
3060 GST_LIVE_SIGNAL (basesrc);
3061 GST_LIVE_UNLOCK (basesrc);
3067 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3068 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3070 gst_event_unref (event);
3071 ret = GST_FLOW_ERROR;
3076 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3077 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3078 ret = GST_FLOW_ERROR;
3083 GST_LIVE_LOCK (basesrc);
3084 basesrc->priv->start_result = ret;
3085 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3086 GST_LIVE_SIGNAL (basesrc);
3087 GST_LIVE_UNLOCK (basesrc);
3093 * gst_base_src_start_complete:
3094 * @src: base source instance
3095 * @ret: a #GstFlowReturn
3097 * Wait until the start operation completes.
3099 * Returns: a #GstFlowReturn.
3102 gst_base_src_start_wait (GstBaseSrc * basesrc)
3104 GstFlowReturn result;
3106 GST_LIVE_LOCK (basesrc);
3107 if (G_UNLIKELY (basesrc->priv->flushing))
3110 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3111 GST_LIVE_WAIT (basesrc);
3112 if (G_UNLIKELY (basesrc->priv->flushing))
3115 result = basesrc->priv->start_result;
3116 GST_LIVE_UNLOCK (basesrc);
3123 GST_DEBUG_OBJECT (basesrc, "we are flushing");
3124 GST_LIVE_UNLOCK (basesrc);
3125 return GST_FLOW_WRONG_STATE;
3130 gst_base_src_stop (GstBaseSrc * basesrc)
3132 GstBaseSrcClass *bclass;
3133 gboolean result = TRUE;
3135 GST_DEBUG_OBJECT (basesrc, "stopping source");
3138 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3140 gst_pad_stop_task (basesrc->srcpad);
3142 GST_LIVE_LOCK (basesrc);
3143 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3146 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3147 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3148 basesrc->priv->start_result = GST_FLOW_WRONG_STATE;
3149 GST_LIVE_SIGNAL (basesrc);
3150 GST_LIVE_UNLOCK (basesrc);
3152 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3154 result = bclass->stop (basesrc);
3156 gst_base_src_set_allocation (basesrc, NULL, NULL, 0, 0);
3162 GST_DEBUG_OBJECT (basesrc, "was started");
3163 GST_LIVE_UNLOCK (basesrc);
3168 /* start or stop flushing dataprocessing
3171 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3172 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
3174 GstBaseSrcClass *bclass;
3176 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3178 if (flushing && unlock) {
3179 gst_base_src_activate_pool (basesrc, FALSE);
3180 /* unlock any subclasses, we need to do this before grabbing the
3181 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3182 * unlock to the params because of backwards compat (see seek handler)*/
3184 bclass->unlock (basesrc);
3187 /* the live lock is released when we are blocked, waiting for playing or
3188 * when we sync to the clock. */
3189 GST_LIVE_LOCK (basesrc);
3191 *playing = basesrc->live_running;
3192 basesrc->priv->flushing = flushing;
3194 /* if we are locked in the live lock, signal it to make it flush */
3195 basesrc->live_running = TRUE;
3197 /* clear pending EOS if any */
3198 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3200 /* step 1, now that we have the LIVE lock, clear our unlock request */
3201 if (bclass->unlock_stop)
3202 bclass->unlock_stop (basesrc);
3204 /* step 2, unblock clock sync (if any) or any other blocking thing */
3205 if (basesrc->clock_id)
3206 gst_clock_id_unschedule (basesrc->clock_id);
3208 /* signal the live source that it can start playing */
3209 basesrc->live_running = live_play;
3211 gst_base_src_activate_pool (basesrc, TRUE);
3213 /* When unlocking drop all delayed events */
3215 GST_OBJECT_LOCK (basesrc);
3216 if (basesrc->priv->pending_events) {
3217 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3219 g_list_free (basesrc->priv->pending_events);
3220 basesrc->priv->pending_events = NULL;
3221 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3223 GST_OBJECT_UNLOCK (basesrc);
3226 GST_LIVE_SIGNAL (basesrc);
3227 GST_LIVE_UNLOCK (basesrc);
3232 /* the purpose of this function is to make sure that a live source blocks in the
3233 * LIVE lock or leaves the LIVE lock and continues playing. */
3235 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3237 GstBaseSrcClass *bclass;
3239 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3241 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3243 GST_DEBUG_OBJECT (basesrc, "unlock");
3245 bclass->unlock (basesrc);
3248 /* we are now able to grab the LIVE lock, when we get it, we can be
3249 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3251 GST_LIVE_LOCK (basesrc);
3252 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3254 /* unblock clock sync (if any) */
3255 if (basesrc->clock_id)
3256 gst_clock_id_unschedule (basesrc->clock_id);
3258 /* configure what to do when we get to the LIVE lock. */
3259 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3260 basesrc->live_running = live_play;
3265 /* clear our unlock request when going to PLAYING */
3266 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3267 if (bclass->unlock_stop)
3268 bclass->unlock_stop (basesrc);
3270 /* for live sources we restart the timestamp correction */
3271 basesrc->priv->latency = -1;
3272 /* have to restart the task in case it stopped because of the unlock when
3273 * we went to PAUSED. Only do this if we operating in push mode. */
3274 GST_OBJECT_LOCK (basesrc->srcpad);
3275 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3276 GST_OBJECT_UNLOCK (basesrc->srcpad);
3278 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3280 GST_DEBUG_OBJECT (basesrc, "signal");
3281 GST_LIVE_SIGNAL (basesrc);
3283 GST_LIVE_UNLOCK (basesrc);
3289 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3291 GstBaseSrc *basesrc;
3293 basesrc = GST_BASE_SRC (parent);
3295 /* prepare subclass first */
3297 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3299 if (G_UNLIKELY (!basesrc->can_activate_push))
3300 goto no_push_activation;
3302 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3305 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3306 /* now we can stop the source */
3307 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3315 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3320 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3325 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3331 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3333 GstBaseSrc *basesrc;
3335 basesrc = GST_BASE_SRC (parent);
3337 /* prepare subclass first */
3339 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3340 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3343 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3344 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3352 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3357 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3363 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3364 GstPadMode mode, gboolean active)
3369 case GST_PAD_MODE_PULL:
3370 res = gst_base_src_activate_pull (pad, parent, active);
3372 case GST_PAD_MODE_PUSH:
3373 res = gst_base_src_activate_push (pad, parent, active);
3376 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3384 static GstStateChangeReturn
3385 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3387 GstBaseSrc *basesrc;
3388 GstStateChangeReturn result;
3389 gboolean no_preroll = FALSE;
3391 basesrc = GST_BASE_SRC (element);
3393 switch (transition) {
3394 case GST_STATE_CHANGE_NULL_TO_READY:
3396 case GST_STATE_CHANGE_READY_TO_PAUSED:
3397 no_preroll = gst_base_src_is_live (basesrc);
3399 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3400 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3401 if (gst_base_src_is_live (basesrc)) {
3402 /* now we can start playback */
3403 gst_base_src_set_playing (basesrc, TRUE);
3411 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3412 transition)) == GST_STATE_CHANGE_FAILURE)
3415 switch (transition) {
3416 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3417 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3418 if (gst_base_src_is_live (basesrc)) {
3419 /* make sure we block in the live lock in PAUSED */
3420 gst_base_src_set_playing (basesrc, FALSE);
3424 case GST_STATE_CHANGE_PAUSED_TO_READY:
3426 /* we don't need to unblock anything here, the pad deactivation code
3427 * already did this */
3428 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3429 gst_event_replace (&basesrc->pending_seek, NULL);
3432 case GST_STATE_CHANGE_READY_TO_NULL:
3438 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3439 result = GST_STATE_CHANGE_NO_PREROLL;
3446 GST_DEBUG_OBJECT (basesrc, "parent failed state change");