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 segment should be sent */
216 gboolean segment_pending;
218 /* if EOS is pending (atomic) */
221 /* startup latency is the time it takes between going to PLAYING and producing
222 * the first BUFFER with running_time 0. This value is included in the latency
224 GstClockTime latency;
225 /* timestamp offset, this is the offset add to the values of gst_times for
226 * pseudo live sources */
227 GstClockTimeDiff ts_offset;
229 gboolean do_timestamp;
230 volatile gint dynamic_size;
232 /* stream sequence number */
235 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
236 * pushed in the data stream */
237 GList *pending_events;
238 volatile gint have_events;
240 /* QoS *//* with LOCK */
241 gboolean qos_enabled;
243 GstClockTime earliest_time;
246 const GstAllocator *allocator;
251 static GstElementClass *parent_class = NULL;
253 static void gst_base_src_class_init (GstBaseSrcClass * klass);
254 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
255 static void gst_base_src_finalize (GObject * object);
259 gst_base_src_get_type (void)
261 static volatile gsize base_src_type = 0;
263 if (g_once_init_enter (&base_src_type)) {
265 static const GTypeInfo base_src_info = {
266 sizeof (GstBaseSrcClass),
269 (GClassInitFunc) gst_base_src_class_init,
274 (GInstanceInitFunc) gst_base_src_init,
277 _type = g_type_register_static (GST_TYPE_ELEMENT,
278 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
279 g_once_init_leave (&base_src_type, _type);
281 return base_src_type;
284 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
286 static void gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
287 static void gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
289 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
290 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
291 GstPadMode mode, gboolean active);
292 static void gst_base_src_set_property (GObject * object, guint prop_id,
293 const GValue * value, GParamSpec * pspec);
294 static void gst_base_src_get_property (GObject * object, guint prop_id,
295 GValue * value, GParamSpec * pspec);
296 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
298 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
299 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
301 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
304 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
306 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
307 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
308 GstSegment * segment);
309 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
310 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
311 GstEvent * event, GstSegment * segment);
312 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
313 guint64 offset, guint size, GstBuffer ** buf);
314 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
315 guint64 offset, guint size, GstBuffer ** buf);
317 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
318 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
320 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
321 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
323 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
324 GstStateChange transition);
326 static void gst_base_src_loop (GstPad * pad);
327 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
328 guint64 offset, guint length, GstBuffer ** buf);
329 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
330 guint length, GstBuffer ** buf);
331 static gboolean gst_base_src_seekable (GstBaseSrc * src);
332 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
333 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
337 gst_base_src_class_init (GstBaseSrcClass * klass)
339 GObjectClass *gobject_class;
340 GstElementClass *gstelement_class;
342 gobject_class = G_OBJECT_CLASS (klass);
343 gstelement_class = GST_ELEMENT_CLASS (klass);
345 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
347 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
349 parent_class = g_type_class_peek_parent (klass);
351 gobject_class->finalize = gst_base_src_finalize;
352 gobject_class->set_property = gst_base_src_set_property;
353 gobject_class->get_property = gst_base_src_get_property;
355 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
356 g_param_spec_uint ("blocksize", "Block size",
357 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
358 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
360 g_param_spec_int ("num-buffers", "num-buffers",
361 "Number of buffers to output before sending EOS (-1 = unlimited)",
362 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
363 G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
365 g_param_spec_boolean ("typefind", "Typefind",
366 "Run typefind before negotiating", DEFAULT_TYPEFIND,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
369 g_param_spec_boolean ("do-timestamp", "Do timestamp",
370 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 gstelement_class->change_state =
374 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
375 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
377 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
378 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
379 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
380 klass->prepare_seek_segment =
381 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
382 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
383 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
384 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
385 klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
386 klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
388 /* Registering debug symbols for function pointers */
389 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
390 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
391 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
392 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
393 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
397 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
400 GstPadTemplate *pad_template;
402 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
404 basesrc->is_live = FALSE;
405 g_mutex_init (&basesrc->live_lock);
406 g_cond_init (&basesrc->live_cond);
407 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
408 basesrc->num_buffers_left = -1;
410 basesrc->can_activate_push = TRUE;
413 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
414 g_return_if_fail (pad_template != NULL);
416 GST_DEBUG_OBJECT (basesrc, "creating src pad");
417 pad = gst_pad_new_from_template (pad_template, "src");
419 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
420 gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
421 gst_pad_set_event_function (pad, gst_base_src_event);
422 gst_pad_set_query_function (pad, gst_base_src_query);
423 gst_pad_set_getrange_function (pad, gst_base_src_getrange);
425 /* hold pointer to pad */
426 basesrc->srcpad = pad;
427 GST_DEBUG_OBJECT (basesrc, "adding src pad");
428 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
430 basesrc->blocksize = DEFAULT_BLOCKSIZE;
431 basesrc->clock_id = NULL;
432 /* we operate in BYTES by default */
433 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
434 basesrc->typefind = DEFAULT_TYPEFIND;
435 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
436 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
438 basesrc->priv->start_result = GST_FLOW_WRONG_STATE;
439 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
440 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
441 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
443 GST_DEBUG_OBJECT (basesrc, "init done");
447 gst_base_src_finalize (GObject * object)
452 basesrc = GST_BASE_SRC (object);
454 g_mutex_clear (&basesrc->live_lock);
455 g_cond_clear (&basesrc->live_cond);
457 event_p = &basesrc->pending_seek;
458 gst_event_replace (event_p, NULL);
460 if (basesrc->priv->pending_events) {
461 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
463 g_list_free (basesrc->priv->pending_events);
466 G_OBJECT_CLASS (parent_class)->finalize (object);
470 * gst_base_src_wait_playing:
473 * If the #GstBaseSrcClass.create() method performs its own synchronisation
474 * against the clock it must unblock when going from PLAYING to the PAUSED state
475 * and call this method before continuing to produce the remaining data.
477 * This function will block until a state change to PLAYING happens (in which
478 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
479 * to a state change to READY or a FLUSH event (in which case this function
480 * returns #GST_FLOW_WRONG_STATE).
484 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
485 * continue. Any other return value should be returned from the create vmethod.
488 gst_base_src_wait_playing (GstBaseSrc * src)
490 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
493 /* block until the state changes, or we get a flush, or something */
494 GST_DEBUG_OBJECT (src, "live source waiting for running state");
496 GST_DEBUG_OBJECT (src, "live source unlocked");
497 if (src->priv->flushing)
499 } while (G_UNLIKELY (!src->live_running));
506 GST_DEBUG_OBJECT (src, "we are flushing");
507 return GST_FLOW_WRONG_STATE;
512 * gst_base_src_set_live:
513 * @src: base source instance
514 * @live: new live-mode
516 * If the element listens to a live source, @live should
519 * A live source will not produce data in the PAUSED state and
520 * will therefore not be able to participate in the PREROLL phase
521 * of a pipeline. To signal this fact to the application and the
522 * pipeline, the state change return value of the live source will
523 * be GST_STATE_CHANGE_NO_PREROLL.
526 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
528 g_return_if_fail (GST_IS_BASE_SRC (src));
530 GST_OBJECT_LOCK (src);
532 GST_OBJECT_UNLOCK (src);
536 * gst_base_src_is_live:
537 * @src: base source instance
539 * Check if an element is in live mode.
541 * Returns: %TRUE if element is in live mode.
544 gst_base_src_is_live (GstBaseSrc * src)
548 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
550 GST_OBJECT_LOCK (src);
551 result = src->is_live;
552 GST_OBJECT_UNLOCK (src);
558 * gst_base_src_set_format:
559 * @src: base source instance
560 * @format: the format to use
562 * Sets the default format of the source. This will be the format used
563 * for sending NEW_SEGMENT events and for performing seeks.
565 * If a format of GST_FORMAT_BYTES is set, the element will be able to
566 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
568 * This function must only be called in states < %GST_STATE_PAUSED.
573 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
575 g_return_if_fail (GST_IS_BASE_SRC (src));
576 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
578 GST_OBJECT_LOCK (src);
579 gst_segment_init (&src->segment, format);
580 GST_OBJECT_UNLOCK (src);
584 * gst_base_src_set_dynamic_size:
585 * @src: base source instance
586 * @dynamic: new dynamic size mode
588 * If not @dynamic, size is only updated when needed, such as when trying to
589 * read past current tracked size. Otherwise, size is checked for upon each
595 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
597 g_return_if_fail (GST_IS_BASE_SRC (src));
599 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
603 * gst_base_src_set_async:
604 * @src: base source instance
605 * @async: new async mode
607 * Configure async behaviour in @src, no state change will block. The open,
608 * close, start, stop, play and pause virtual methods will be executed in a
609 * different thread and are thus allowed to perform blocking operations. Any
610 * blocking operation should be unblocked with the unlock vmethod.
613 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
615 g_return_if_fail (GST_IS_BASE_SRC (src));
617 GST_OBJECT_LOCK (src);
618 src->priv->async = async;
619 GST_OBJECT_UNLOCK (src);
623 * gst_base_src_is_async:
624 * @src: base source instance
626 * Get the current async behaviour of @src. See also gst_base_src_set_async().
628 * Returns: %TRUE if @src is operating in async mode.
631 gst_base_src_is_async (GstBaseSrc * src)
635 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
637 GST_OBJECT_LOCK (src);
638 res = src->priv->async;
639 GST_OBJECT_UNLOCK (src);
646 * gst_base_src_query_latency:
648 * @live: (out) (allow-none): if the source is live
649 * @min_latency: (out) (allow-none): the min latency of the source
650 * @max_latency: (out) (allow-none): the max latency of the source
652 * Query the source for the latency parameters. @live will be TRUE when @src is
653 * configured as a live source. @min_latency will be set to the difference
654 * between the running time and the timestamp of the first buffer.
655 * @max_latency is always the undefined value of -1.
657 * This function is mostly used by subclasses.
659 * Returns: TRUE if the query succeeded.
664 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
665 GstClockTime * min_latency, GstClockTime * max_latency)
669 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
671 GST_OBJECT_LOCK (src);
673 *live = src->is_live;
675 /* if we have a startup latency, report this one, else report 0. Subclasses
676 * are supposed to override the query function if they want something
678 if (src->priv->latency != -1)
679 min = src->priv->latency;
688 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
689 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
691 GST_OBJECT_UNLOCK (src);
697 * gst_base_src_set_blocksize:
699 * @blocksize: the new blocksize in bytes
701 * Set the number of bytes that @src will push out with each buffer. When
702 * @blocksize is set to -1, a default length will be used.
707 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
709 g_return_if_fail (GST_IS_BASE_SRC (src));
711 GST_OBJECT_LOCK (src);
712 src->blocksize = blocksize;
713 GST_OBJECT_UNLOCK (src);
717 * gst_base_src_get_blocksize:
720 * Get the number of bytes that @src will push out with each buffer.
722 * Returns: the number of bytes pushed with each buffer.
727 gst_base_src_get_blocksize (GstBaseSrc * src)
731 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
733 GST_OBJECT_LOCK (src);
734 res = src->blocksize;
735 GST_OBJECT_UNLOCK (src);
742 * gst_base_src_set_do_timestamp:
744 * @timestamp: enable or disable timestamping
746 * Configure @src to automatically timestamp outgoing buffers based on the
747 * current running_time of the pipeline. This property is mostly useful for live
753 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
755 g_return_if_fail (GST_IS_BASE_SRC (src));
757 GST_OBJECT_LOCK (src);
758 src->priv->do_timestamp = timestamp;
759 GST_OBJECT_UNLOCK (src);
763 * gst_base_src_get_do_timestamp:
766 * Query if @src timestamps outgoing buffers based on the current running_time.
768 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
773 gst_base_src_get_do_timestamp (GstBaseSrc * src)
777 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
779 GST_OBJECT_LOCK (src);
780 res = src->priv->do_timestamp;
781 GST_OBJECT_UNLOCK (src);
787 * gst_base_src_new_seamless_segment:
789 * @start: The new start value for the segment
790 * @stop: Stop value for the new segment
791 * @position: The position value for the new segent
793 * Prepare a new seamless segment for emission downstream. This function must
794 * only be called by derived sub-classes, and only from the create() function,
795 * as the stream-lock needs to be held.
797 * The format for the new segment will be the current format of the source, as
798 * configured with gst_base_src_set_format()
800 * Returns: %TRUE if preparation of the seamless segment succeeded.
805 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
810 GST_DEBUG_OBJECT (src,
811 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
812 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
813 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
815 GST_OBJECT_LOCK (src);
817 src->segment.base = gst_segment_to_running_time (&src->segment,
818 src->segment.format, src->segment.position);
819 src->segment.start = start;
820 src->segment.stop = stop;
821 src->segment.position = position;
823 /* forward, we send data from position to stop */
824 src->priv->segment_pending = TRUE;
825 GST_OBJECT_UNLOCK (src);
827 src->priv->discont = TRUE;
834 * gst_base_src_set_caps:
835 * @src: a #GstBaseSrc
838 * Set new caps on the basesrc source pad.
840 * Returns: %TRUE if the caps could be set
843 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
845 GstBaseSrcClass *bclass;
848 bclass = GST_BASE_SRC_GET_CLASS (src);
850 gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
852 if (bclass->set_caps)
853 res = bclass->set_caps (src, caps);
859 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
861 GstCaps *caps = NULL;
862 GstPadTemplate *pad_template;
863 GstBaseSrcClass *bclass;
865 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
868 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
870 if (pad_template != NULL) {
871 caps = gst_pad_template_get_caps (pad_template);
874 GstCaps *intersection;
877 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
878 gst_caps_unref (caps);
886 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
888 GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
889 gst_caps_fixate (caps);
893 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
895 GstBaseSrcClass *bclass;
897 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
900 bclass->fixate (bsrc, caps);
904 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
908 switch (GST_QUERY_TYPE (query)) {
909 case GST_QUERY_POSITION:
913 gst_query_parse_position (query, &format, NULL);
915 GST_DEBUG_OBJECT (src, "position query in format %s",
916 gst_format_get_name (format));
919 case GST_FORMAT_PERCENT:
925 GST_OBJECT_LOCK (src);
926 position = src->segment.position;
927 duration = src->segment.duration;
928 GST_OBJECT_UNLOCK (src);
930 if (position != -1 && duration != -1) {
931 if (position < duration)
932 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
935 percent = GST_FORMAT_PERCENT_MAX;
939 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
946 GstFormat seg_format;
948 GST_OBJECT_LOCK (src);
950 gst_segment_to_stream_time (&src->segment, src->segment.format,
951 src->segment.position);
952 seg_format = src->segment.format;
953 GST_OBJECT_UNLOCK (src);
955 if (position != -1) {
956 /* convert to requested format */
958 gst_pad_query_convert (src->srcpad, seg_format,
959 position, format, &position);
963 gst_query_set_position (query, format, position);
969 case GST_QUERY_DURATION:
973 gst_query_parse_duration (query, &format, NULL);
975 GST_DEBUG_OBJECT (src, "duration query in format %s",
976 gst_format_get_name (format));
979 case GST_FORMAT_PERCENT:
980 gst_query_set_duration (query, GST_FORMAT_PERCENT,
981 GST_FORMAT_PERCENT_MAX);
987 GstFormat seg_format;
990 /* may have to refresh duration */
991 if (g_atomic_int_get (&src->priv->dynamic_size))
992 gst_base_src_update_length (src, 0, &length);
994 /* this is the duration as configured by the subclass. */
995 GST_OBJECT_LOCK (src);
996 duration = src->segment.duration;
997 seg_format = src->segment.format;
998 GST_OBJECT_UNLOCK (src);
1000 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1001 duration, gst_format_get_name (seg_format));
1003 if (duration != -1) {
1004 /* convert to requested format, if this fails, we have a duration
1005 * but we cannot answer the query, we must return FALSE. */
1007 gst_pad_query_convert (src->srcpad, seg_format,
1008 duration, format, &duration);
1010 /* The subclass did not configure a duration, we assume that the
1011 * media has an unknown duration then and we return TRUE to report
1012 * this. Note that this is not the same as returning FALSE, which
1013 * means that we cannot report the duration at all. */
1016 gst_query_set_duration (query, format, duration);
1023 case GST_QUERY_SEEKING:
1025 GstFormat format, seg_format;
1028 GST_OBJECT_LOCK (src);
1029 duration = src->segment.duration;
1030 seg_format = src->segment.format;
1031 GST_OBJECT_UNLOCK (src);
1033 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1034 if (format == seg_format) {
1035 gst_query_set_seeking (query, seg_format,
1036 gst_base_src_seekable (src), 0, duration);
1039 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1040 /* Don't reply to the query to make up for demuxers which don't
1041 * handle the SEEKING query yet. Players like Totem will fall back
1042 * to the duration when the SEEKING query isn't answered. */
1047 case GST_QUERY_SEGMENT:
1051 GST_OBJECT_LOCK (src);
1052 /* no end segment configured, current duration then */
1053 if ((stop = src->segment.stop) == -1)
1054 stop = src->segment.duration;
1055 start = src->segment.start;
1057 /* adjust to stream time */
1058 if (src->segment.time != -1) {
1059 start -= src->segment.time;
1061 stop -= src->segment.time;
1064 gst_query_set_segment (query, src->segment.rate, src->segment.format,
1066 GST_OBJECT_UNLOCK (src);
1071 case GST_QUERY_FORMATS:
1073 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1074 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1078 case GST_QUERY_CONVERT:
1080 GstFormat src_fmt, dest_fmt;
1081 gint64 src_val, dest_val;
1083 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1085 /* we can only convert between equal formats... */
1086 if (src_fmt == dest_fmt) {
1092 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1095 case GST_QUERY_LATENCY:
1097 GstClockTime min, max;
1100 /* Subclasses should override and implement something useful */
1101 res = gst_base_src_query_latency (src, &live, &min, &max);
1103 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1104 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1105 GST_TIME_ARGS (max));
1107 gst_query_set_latency (query, live, min, max);
1110 case GST_QUERY_JITTER:
1111 case GST_QUERY_RATE:
1114 case GST_QUERY_BUFFERING:
1116 GstFormat format, seg_format;
1117 gint64 start, stop, estimated;
1119 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1121 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1122 gst_format_get_name (format));
1124 GST_OBJECT_LOCK (src);
1125 if (src->random_access) {
1128 if (format == GST_FORMAT_PERCENT)
1129 stop = GST_FORMAT_PERCENT_MAX;
1131 stop = src->segment.duration;
1137 seg_format = src->segment.format;
1138 GST_OBJECT_UNLOCK (src);
1140 /* convert to required format. When the conversion fails, we can't answer
1141 * the query. When the value is unknown, we can don't perform conversion
1142 * but report TRUE. */
1143 if (format != GST_FORMAT_PERCENT && stop != -1) {
1144 res = gst_pad_query_convert (src->srcpad, seg_format,
1145 stop, format, &stop);
1149 if (res && format != GST_FORMAT_PERCENT && start != -1)
1150 res = gst_pad_query_convert (src->srcpad, seg_format,
1151 start, format, &start);
1153 gst_query_set_buffering_range (query, format, start, stop, estimated);
1156 case GST_QUERY_SCHEDULING:
1158 gboolean random_access;
1160 random_access = gst_base_src_is_random_access (src);
1162 /* we can operate in getrange mode if the native format is bytes
1163 * and we are seekable, this condition is set in the random_access
1164 * flag and is set in the _start() method. */
1165 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1167 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1168 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1173 case GST_QUERY_CAPS:
1175 GstBaseSrcClass *bclass;
1176 GstCaps *caps, *filter;
1178 bclass = GST_BASE_SRC_GET_CLASS (src);
1179 if (bclass->get_caps) {
1180 gst_query_parse_caps (query, &filter);
1181 if ((caps = bclass->get_caps (src, filter))) {
1182 gst_query_set_caps_result (query, caps);
1183 gst_caps_unref (caps);
1196 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1203 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1206 GstBaseSrcClass *bclass;
1207 gboolean result = FALSE;
1209 src = GST_BASE_SRC (parent);
1210 bclass = GST_BASE_SRC_GET_CLASS (src);
1213 result = bclass->query (src, query);
1219 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1221 gboolean res = TRUE;
1223 /* update our offset if the start/stop position was updated */
1224 if (segment->format == GST_FORMAT_BYTES) {
1225 segment->time = segment->start;
1226 } else if (segment->start == 0) {
1227 /* seek to start, we can implement a default for this. */
1231 GST_INFO_OBJECT (src, "Can't do a default seek");
1238 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1240 GstBaseSrcClass *bclass;
1241 gboolean result = FALSE;
1243 bclass = GST_BASE_SRC_GET_CLASS (src);
1245 if (bclass->do_seek)
1246 result = bclass->do_seek (src, segment);
1251 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1254 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1255 GstSegment * segment)
1257 /* By default, we try one of 2 things:
1258 * - For absolute seek positions, convert the requested position to our
1259 * configured processing format and place it in the output segment \
1260 * - For relative seek positions, convert our current (input) values to the
1261 * seek format, adjust by the relative seek offset and then convert back to
1262 * the processing format
1264 GstSeekType cur_type, stop_type;
1267 GstFormat seek_format, dest_format;
1270 gboolean res = TRUE;
1272 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1273 &cur_type, &cur, &stop_type, &stop);
1274 dest_format = segment->format;
1276 if (seek_format == dest_format) {
1277 gst_segment_do_seek (segment, rate, seek_format, flags,
1278 cur_type, cur, stop_type, stop, &update);
1282 if (cur_type != GST_SEEK_TYPE_NONE) {
1283 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1285 gst_pad_query_convert (src->srcpad, seek_format, cur, dest_format,
1287 cur_type = GST_SEEK_TYPE_SET;
1290 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1291 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1293 gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1295 stop_type = GST_SEEK_TYPE_SET;
1298 /* And finally, configure our output segment in the desired format */
1299 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
1300 stop_type, stop, &update);
1309 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1315 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1316 GstSegment * seeksegment)
1318 GstBaseSrcClass *bclass;
1319 gboolean result = FALSE;
1321 bclass = GST_BASE_SRC_GET_CLASS (src);
1323 if (bclass->prepare_seek_segment)
1324 result = bclass->prepare_seek_segment (src, event, seeksegment);
1329 static GstFlowReturn
1330 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1331 guint size, GstBuffer ** buffer)
1334 GstBaseSrcPrivate *priv = src->priv;
1337 ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1338 } else if (size != -1) {
1339 *buffer = gst_buffer_new_allocate (priv->allocator, size, priv->alignment);
1340 if (G_UNLIKELY (*buffer == NULL))
1345 GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1354 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1355 return GST_FLOW_ERROR;
1359 static GstFlowReturn
1360 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1361 guint size, GstBuffer ** buffer)
1363 GstBaseSrcClass *bclass;
1366 bclass = GST_BASE_SRC_GET_CLASS (src);
1368 if (G_UNLIKELY (!bclass->alloc))
1370 if (G_UNLIKELY (!bclass->fill))
1373 ret = bclass->alloc (src, offset, size, buffer);
1374 if (G_UNLIKELY (ret != GST_FLOW_OK))
1377 if (G_LIKELY (size > 0)) {
1378 /* only call fill when there is a size */
1379 ret = bclass->fill (src, offset, size, *buffer);
1380 if (G_UNLIKELY (ret != GST_FLOW_OK))
1389 GST_DEBUG_OBJECT (src, "no fill or alloc function");
1390 return GST_FLOW_NOT_SUPPORTED;
1394 GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1399 GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1400 gst_flow_get_name (ret));
1401 gst_buffer_unref (*buffer);
1406 /* this code implements the seeking. It is a good example
1407 * handling all cases.
1409 * A seek updates the currently configured segment.start
1410 * and segment.stop values based on the SEEK_TYPE. If the
1411 * segment.start value is updated, a seek to this new position
1412 * should be performed.
1414 * The seek can only be executed when we are not currently
1415 * streaming any data, to make sure that this is the case, we
1416 * acquire the STREAM_LOCK which is taken when we are in the
1417 * _loop() function or when a getrange() is called. Normally
1418 * we will not receive a seek if we are operating in pull mode
1419 * though. When we operate as a live source we might block on the live
1420 * cond, which does not release the STREAM_LOCK. Therefore we will try
1421 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1422 * safe to perform the seek.
1424 * When we are in the loop() function, we might be in the middle
1425 * of pushing a buffer, which might block in a sink. To make sure
1426 * that the push gets unblocked we push out a FLUSH_START event.
1427 * Our loop function will get a WRONG_STATE return value from
1428 * the push and will pause, effectively releasing the STREAM_LOCK.
1430 * For a non-flushing seek, we pause the task, which might eventually
1431 * release the STREAM_LOCK. We say eventually because when the sink
1432 * blocks on the sample we might wait a very long time until the sink
1433 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1434 * can continue the seek. A non-flushing seek is normally done in a
1435 * running pipeline to perform seamless playback, this means that the sink is
1436 * PLAYING and will return from its chain function.
1437 * In the case of a non-flushing seek we need to make sure that the
1438 * data we output after the seek is continuous with the previous data,
1439 * this is because a non-flushing seek does not reset the running-time
1440 * to 0. We do this by closing the currently running segment, ie. sending
1441 * a new_segment event with the stop position set to the last processed
1444 * After updating the segment.start/stop values, we prepare for
1445 * streaming again. We push out a FLUSH_STOP to make the peer pad
1446 * accept data again and we start our task again.
1448 * A segment seek posts a message on the bus saying that the playback
1449 * of the segment started. We store the segment flag internally because
1450 * when we reach the segment.stop we have to post a segment.done
1451 * instead of EOS when doing a segment seek.
1453 /* FIXME (0.11), we have the unlock gboolean here because most current
1454 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1455 * the streaming thread isn't running, resulting in bogus unlocks later when it
1456 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1457 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1461 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1463 gboolean res = TRUE, tres;
1465 GstFormat seek_format, dest_format;
1467 GstSeekType cur_type, stop_type;
1469 gboolean flush, playing;
1471 gboolean relative_seek = FALSE;
1472 gboolean seekseg_configured = FALSE;
1473 GstSegment seeksegment;
1477 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1479 GST_OBJECT_LOCK (src);
1480 dest_format = src->segment.format;
1481 GST_OBJECT_UNLOCK (src);
1484 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1485 &cur_type, &cur, &stop_type, &stop);
1487 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1488 SEEK_TYPE_IS_RELATIVE (stop_type);
1490 if (dest_format != seek_format && !relative_seek) {
1491 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1492 * here before taking the stream lock, otherwise we must convert it later,
1493 * once we have the stream lock and can read the last configures segment
1494 * start and stop positions */
1495 gst_segment_init (&seeksegment, dest_format);
1497 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1498 goto prepare_failed;
1500 seekseg_configured = TRUE;
1503 flush = flags & GST_SEEK_FLAG_FLUSH;
1504 seqnum = gst_event_get_seqnum (event);
1507 /* get next seqnum */
1508 seqnum = gst_util_seqnum_next ();
1511 /* send flush start */
1513 tevent = gst_event_new_flush_start ();
1514 gst_event_set_seqnum (tevent, seqnum);
1515 gst_pad_push_event (src->srcpad, tevent);
1517 gst_pad_pause_task (src->srcpad);
1519 /* unblock streaming thread. */
1520 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1522 /* grab streaming lock, this should eventually be possible, either
1523 * because the task is paused, our streaming thread stopped
1524 * or because our peer is flushing. */
1525 GST_PAD_STREAM_LOCK (src->srcpad);
1526 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1527 /* we have seen this event before, issue a warning for now */
1528 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1531 src->priv->seqnum = seqnum;
1532 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1535 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1537 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1538 * copy the current segment info into the temp segment that we can actually
1539 * attempt the seek with. We only update the real segment if the seek succeeds. */
1540 if (!seekseg_configured) {
1541 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1543 /* now configure the final seek segment */
1545 if (seeksegment.format != seek_format) {
1546 /* OK, here's where we give the subclass a chance to convert the relative
1547 * seek into an absolute one in the processing format. We set up any
1548 * absolute seek above, before taking the stream lock. */
1549 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1550 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1555 /* The seek format matches our processing format, no need to ask the
1556 * the subclass to configure the segment. */
1557 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1558 cur_type, cur, stop_type, stop, &update);
1561 /* Else, no seek event passed, so we're just (re)starting the
1566 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1567 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1568 seeksegment.start, seeksegment.stop, seeksegment.position);
1570 /* do the seek, segment.position contains the new position. */
1571 res = gst_base_src_do_seek (src, &seeksegment);
1574 /* and prepare to continue streaming */
1576 tevent = gst_event_new_flush_stop (TRUE);
1577 gst_event_set_seqnum (tevent, seqnum);
1578 /* send flush stop, peer will accept data and events again. We
1579 * are not yet providing data as we still have the STREAM_LOCK. */
1580 gst_pad_push_event (src->srcpad, tevent);
1583 /* The subclass must have converted the segment to the processing format
1585 if (res && seeksegment.format != dest_format) {
1586 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1587 "in the correct format. Aborting seek.");
1591 /* if the seek was successful, we update our real segment and push
1592 * out the new segment. */
1594 GST_OBJECT_LOCK (src);
1595 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1596 GST_OBJECT_UNLOCK (src);
1598 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1599 GstMessage *message;
1601 message = gst_message_new_segment_start (GST_OBJECT (src),
1602 seeksegment.format, seeksegment.position);
1603 gst_message_set_seqnum (message, seqnum);
1605 gst_element_post_message (GST_ELEMENT (src), message);
1608 /* for deriving a stop position for the playback segment from the seek
1609 * segment, we must take the duration when the stop is not set */
1610 if ((stop = seeksegment.stop) == -1)
1611 stop = seeksegment.duration;
1613 src->priv->segment_pending = TRUE;
1616 src->priv->discont = TRUE;
1617 src->running = TRUE;
1618 /* and restart the task in case it got paused explicitly or by
1619 * the FLUSH_START event we pushed out. */
1620 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1625 /* and release the lock again so we can continue streaming */
1626 GST_PAD_STREAM_UNLOCK (src->srcpad);
1632 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1637 /* all events send to this element directly. This is mainly done from the
1641 gst_base_src_send_event (GstElement * element, GstEvent * event)
1644 gboolean result = FALSE;
1646 src = GST_BASE_SRC (element);
1648 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1650 switch (GST_EVENT_TYPE (event)) {
1651 /* bidirectional events */
1652 case GST_EVENT_FLUSH_START:
1653 case GST_EVENT_FLUSH_STOP:
1654 /* sending random flushes downstream can break stuff,
1655 * especially sync since all segment info will get flushed */
1658 /* downstream serialized events */
1661 GstBaseSrcClass *bclass;
1663 bclass = GST_BASE_SRC_GET_CLASS (src);
1665 /* queue EOS and make sure the task or pull function performs the EOS
1668 * We have two possibilities:
1670 * - Before we are to enter the _create function, we check the pending_eos
1671 * first and do EOS instead of entering it.
1672 * - If we are in the _create function or we did not manage to set the
1673 * flag fast enough and we are about to enter the _create function,
1674 * we unlock it so that we exit with WRONG_STATE immediately. We then
1675 * check the EOS flag and do the EOS logic.
1677 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1678 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1681 /* unlock the _create function so that we can check the pending_eos flag
1682 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1683 * that we can grab it and stop the unlock again. We don't take the stream
1684 * lock so that this operation is guaranteed to never block. */
1685 gst_base_src_activate_pool (src, FALSE);
1687 bclass->unlock (src);
1689 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1691 GST_LIVE_LOCK (src);
1692 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1693 /* now stop the unlock of the streaming thread again. Grabbing the live
1694 * lock is enough because that protects the create function. */
1695 if (bclass->unlock_stop)
1696 bclass->unlock_stop (src);
1697 gst_base_src_activate_pool (src, TRUE);
1698 GST_LIVE_UNLOCK (src);
1703 case GST_EVENT_SEGMENT:
1704 /* sending random SEGMENT downstream can break sync. */
1707 case GST_EVENT_CUSTOM_DOWNSTREAM:
1708 case GST_EVENT_CUSTOM_BOTH:
1709 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1710 GST_OBJECT_LOCK (src);
1711 src->priv->pending_events =
1712 g_list_append (src->priv->pending_events, event);
1713 g_atomic_int_set (&src->priv->have_events, TRUE);
1714 GST_OBJECT_UNLOCK (src);
1718 case GST_EVENT_BUFFERSIZE:
1719 /* does not seem to make much sense currently */
1722 /* upstream events */
1724 /* elements should override send_event and do something */
1726 case GST_EVENT_SEEK:
1730 GST_OBJECT_LOCK (src->srcpad);
1731 if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1733 started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1734 GST_OBJECT_UNLOCK (src->srcpad);
1737 GST_DEBUG_OBJECT (src, "performing seek");
1738 /* when we are running in push mode, we can execute the
1739 * seek right now, we need to unlock. */
1740 result = gst_base_src_perform_seek (src, event, TRUE);
1744 /* else we store the event and execute the seek when we
1746 GST_OBJECT_LOCK (src);
1747 GST_DEBUG_OBJECT (src, "queueing seek");
1748 event_p = &src->pending_seek;
1749 gst_event_replace ((GstEvent **) event_p, event);
1750 GST_OBJECT_UNLOCK (src);
1751 /* assume the seek will work */
1756 case GST_EVENT_NAVIGATION:
1757 /* could make sense for elements that do something with navigation events
1758 * but then they would need to override the send_event function */
1760 case GST_EVENT_LATENCY:
1761 /* does not seem to make sense currently */
1765 case GST_EVENT_CUSTOM_UPSTREAM:
1766 /* override send_event if you want this */
1768 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1769 case GST_EVENT_CUSTOM_BOTH_OOB:
1770 /* insert a random custom event into the pipeline */
1771 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1772 result = gst_pad_push_event (src->srcpad, event);
1773 /* we gave away the ref to the event in the push */
1780 /* if we still have a ref to the event, unref it now */
1782 gst_event_unref (event);
1789 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1790 GST_OBJECT_UNLOCK (src->srcpad);
1797 gst_base_src_seekable (GstBaseSrc * src)
1799 GstBaseSrcClass *bclass;
1800 bclass = GST_BASE_SRC_GET_CLASS (src);
1801 if (bclass->is_seekable)
1802 return bclass->is_seekable (src);
1808 gst_base_src_update_qos (GstBaseSrc * src,
1809 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1811 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1812 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1813 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1815 GST_OBJECT_LOCK (src);
1816 src->priv->proportion = proportion;
1817 src->priv->earliest_time = timestamp + diff;
1818 GST_OBJECT_UNLOCK (src);
1823 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1827 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1829 switch (GST_EVENT_TYPE (event)) {
1830 case GST_EVENT_SEEK:
1831 /* is normally called when in push mode */
1832 if (!gst_base_src_seekable (src))
1835 result = gst_base_src_perform_seek (src, event, TRUE);
1837 case GST_EVENT_FLUSH_START:
1838 /* cancel any blocking getrange, is normally called
1839 * when in pull mode. */
1840 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1842 case GST_EVENT_FLUSH_STOP:
1843 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1848 GstClockTimeDiff diff;
1849 GstClockTime timestamp;
1851 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1852 gst_base_src_update_qos (src, proportion, diff, timestamp);
1856 case GST_EVENT_RECONFIGURE:
1859 case GST_EVENT_LATENCY:
1871 GST_DEBUG_OBJECT (src, "is not seekable");
1877 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1880 GstBaseSrcClass *bclass;
1881 gboolean result = FALSE;
1883 src = GST_BASE_SRC (parent);
1884 bclass = GST_BASE_SRC_GET_CLASS (src);
1886 if (bclass->event) {
1887 if (!(result = bclass->event (src, event)))
1888 goto subclass_failed;
1892 gst_event_unref (event);
1899 GST_DEBUG_OBJECT (src, "subclass refused event");
1905 gst_base_src_set_property (GObject * object, guint prop_id,
1906 const GValue * value, GParamSpec * pspec)
1910 src = GST_BASE_SRC (object);
1913 case PROP_BLOCKSIZE:
1914 gst_base_src_set_blocksize (src, g_value_get_uint (value));
1916 case PROP_NUM_BUFFERS:
1917 src->num_buffers = g_value_get_int (value);
1920 src->typefind = g_value_get_boolean (value);
1922 case PROP_DO_TIMESTAMP:
1923 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1926 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1932 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1937 src = GST_BASE_SRC (object);
1940 case PROP_BLOCKSIZE:
1941 g_value_set_uint (value, gst_base_src_get_blocksize (src));
1943 case PROP_NUM_BUFFERS:
1944 g_value_set_int (value, src->num_buffers);
1947 g_value_set_boolean (value, src->typefind);
1949 case PROP_DO_TIMESTAMP:
1950 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1953 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1958 /* with STREAM_LOCK and LOCK */
1959 static GstClockReturn
1960 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1965 id = gst_clock_new_single_shot_id (clock, time);
1967 basesrc->clock_id = id;
1968 /* release the live lock while waiting */
1969 GST_LIVE_UNLOCK (basesrc);
1971 ret = gst_clock_id_wait (id, NULL);
1973 GST_LIVE_LOCK (basesrc);
1974 gst_clock_id_unref (id);
1975 basesrc->clock_id = NULL;
1980 /* perform synchronisation on a buffer.
1983 static GstClockReturn
1984 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1986 GstClockReturn result;
1987 GstClockTime start, end;
1988 GstBaseSrcClass *bclass;
1989 GstClockTime base_time;
1991 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1992 gboolean do_timestamp, first, pseudo_live;
1994 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1997 if (bclass->get_times)
1998 bclass->get_times (basesrc, buffer, &start, &end);
2000 /* get buffer timestamp */
2001 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2003 /* grab the lock to prepare for clocking and calculate the startup
2005 GST_OBJECT_LOCK (basesrc);
2007 /* if we are asked to sync against the clock we are a pseudo live element */
2008 pseudo_live = (start != -1 && basesrc->is_live);
2009 /* check for the first buffer */
2010 first = (basesrc->priv->latency == -1);
2012 if (timestamp != -1 && pseudo_live) {
2013 GstClockTime latency;
2015 /* we have a timestamp and a sync time, latency is the diff */
2016 if (timestamp <= start)
2017 latency = start - timestamp;
2022 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2023 GST_TIME_ARGS (latency));
2024 /* first time we calculate latency, just configure */
2025 basesrc->priv->latency = latency;
2027 if (basesrc->priv->latency != latency) {
2028 /* we have a new latency, FIXME post latency message */
2029 basesrc->priv->latency = latency;
2030 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2031 GST_TIME_ARGS (latency));
2035 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2036 basesrc->is_live, start != -1);
2037 basesrc->priv->latency = 0;
2040 /* get clock, if no clock, we can't sync or do timestamps */
2041 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2044 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2046 do_timestamp = basesrc->priv->do_timestamp;
2048 /* first buffer, calculate the timestamp offset */
2050 GstClockTime running_time;
2052 now = gst_clock_get_time (clock);
2053 running_time = now - base_time;
2055 GST_LOG_OBJECT (basesrc,
2056 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
2057 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
2058 GST_TIME_ARGS (running_time));
2060 if (pseudo_live && timestamp != -1) {
2061 /* live source and we need to sync, add startup latency to all timestamps
2062 * to get the real running_time. Live sources should always timestamp
2063 * according to the current running time. */
2064 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2066 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2067 GST_TIME_ARGS (basesrc->priv->ts_offset));
2069 basesrc->priv->ts_offset = 0;
2070 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2073 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
2075 timestamp = running_time;
2079 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
2081 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2082 GST_TIME_ARGS (timestamp));
2085 /* add the timestamp offset we need for sync */
2086 timestamp += basesrc->priv->ts_offset;
2088 /* not the first buffer, the timestamp is the diff between the clock and
2090 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
2091 now = gst_clock_get_time (clock);
2093 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
2095 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2096 GST_TIME_ARGS (now - base_time));
2100 /* if we don't have a buffer timestamp, we don't sync */
2101 if (!GST_CLOCK_TIME_IS_VALID (start))
2104 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
2105 /* for pseudo live sources, add our ts_offset to the timestamp */
2106 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
2107 start += basesrc->priv->ts_offset;
2110 GST_LOG_OBJECT (basesrc,
2111 "waiting for clock, base time %" GST_TIME_FORMAT
2112 ", stream_start %" GST_TIME_FORMAT,
2113 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2114 GST_OBJECT_UNLOCK (basesrc);
2116 result = gst_base_src_wait (basesrc, clock, start + base_time);
2118 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2125 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2126 GST_OBJECT_UNLOCK (basesrc);
2127 return GST_CLOCK_OK;
2131 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2132 GST_OBJECT_UNLOCK (basesrc);
2133 return GST_CLOCK_OK;
2137 /* Called with STREAM_LOCK and LIVE_LOCK */
2139 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
2141 guint64 size, maxsize;
2142 GstBaseSrcClass *bclass;
2147 bclass = GST_BASE_SRC_GET_CLASS (src);
2149 format = src->segment.format;
2150 stop = src->segment.stop;
2151 /* get total file size */
2152 size = src->segment.duration;
2154 /* only operate if we are working with bytes */
2155 if (format != GST_FORMAT_BYTES)
2158 /* the max amount of bytes to read is the total size or
2159 * up to the segment.stop if present. */
2161 maxsize = MIN (size, stop);
2165 GST_DEBUG_OBJECT (src,
2166 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2167 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2168 *length, size, stop, maxsize);
2170 dynamic = g_atomic_int_get (&src->priv->dynamic_size);
2171 GST_DEBUG_OBJECT (src, "dynamic size: %d", dynamic);
2173 /* check size if we have one */
2174 if (maxsize != -1) {
2175 /* if we run past the end, check if the file became bigger and
2177 if (G_UNLIKELY (offset + *length >= maxsize || dynamic)) {
2178 /* see if length of the file changed */
2179 if (bclass->get_size)
2180 if (!bclass->get_size (src, &size))
2183 /* make sure we don't exceed the configured segment stop
2186 maxsize = MIN (size, stop);
2190 /* if we are at or past the end, EOS */
2191 if (G_UNLIKELY (offset >= maxsize))
2192 goto unexpected_length;
2194 /* else we can clip to the end */
2195 if (G_UNLIKELY (offset + *length >= maxsize))
2196 *length = maxsize - offset;
2201 /* keep track of current duration.
2202 * segment is in bytes, we checked that above. */
2203 GST_OBJECT_LOCK (src);
2204 src->segment.duration = size;
2205 GST_OBJECT_UNLOCK (src);
2216 /* must be called with LIVE_LOCK */
2217 static GstFlowReturn
2218 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2222 GstBaseSrcClass *bclass;
2223 GstClockReturn status;
2225 bclass = GST_BASE_SRC_GET_CLASS (src);
2229 if (G_UNLIKELY (!src->live_running)) {
2230 ret = gst_base_src_wait_playing (src);
2231 if (ret != GST_FLOW_OK)
2236 if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)))
2239 if (G_UNLIKELY (!bclass->create))
2242 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2243 goto unexpected_length;
2245 /* track position */
2246 GST_OBJECT_LOCK (src);
2247 if (src->segment.format == GST_FORMAT_BYTES)
2248 src->segment.position = offset;
2249 GST_OBJECT_UNLOCK (src);
2251 /* normally we don't count buffers */
2252 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2253 if (src->num_buffers_left == 0)
2254 goto reached_num_buffers;
2256 src->num_buffers_left--;
2259 /* don't enter the create function if a pending EOS event was set. For the
2260 * logic of the pending_eos, check the event function of this class. */
2261 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2264 GST_DEBUG_OBJECT (src,
2265 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2266 G_GINT64_FORMAT, offset, length, src->segment.time);
2268 ret = bclass->create (src, offset, length, buf);
2270 /* The create function could be unlocked because we have a pending EOS. It's
2271 * possible that we have a valid buffer from create that we need to
2272 * discard when the create function returned _OK. */
2273 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2274 if (ret == GST_FLOW_OK) {
2275 gst_buffer_unref (*buf);
2281 if (G_UNLIKELY (ret != GST_FLOW_OK))
2284 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2285 if (offset == 0 && src->segment.time == 0
2286 && GST_BUFFER_TIMESTAMP (*buf) == -1 && !src->is_live) {
2287 GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2288 *buf = gst_buffer_make_writable (*buf);
2289 GST_BUFFER_TIMESTAMP (*buf) = 0;
2292 /* now sync before pushing the buffer */
2293 status = gst_base_src_do_sync (src, *buf);
2295 /* waiting for the clock could have made us flushing */
2296 if (G_UNLIKELY (src->priv->flushing))
2300 case GST_CLOCK_EARLY:
2301 /* the buffer is too late. We currently don't drop the buffer. */
2302 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2305 /* buffer synchronised properly */
2306 GST_DEBUG_OBJECT (src, "buffer ok");
2308 case GST_CLOCK_UNSCHEDULED:
2309 /* this case is triggered when we were waiting for the clock and
2310 * it got unlocked because we did a state change. In any case, get rid of
2312 gst_buffer_unref (*buf);
2314 if (!src->live_running) {
2315 /* We return WRONG_STATE when we are not running to stop the dataflow also
2316 * get rid of the produced buffer. */
2317 GST_DEBUG_OBJECT (src,
2318 "clock was unscheduled (%d), returning WRONG_STATE", status);
2319 ret = GST_FLOW_WRONG_STATE;
2321 /* If we are running when this happens, we quickly switched between
2322 * pause and playing. We try to produce a new buffer */
2323 GST_DEBUG_OBJECT (src,
2324 "clock was unscheduled (%d), but we are running", status);
2329 /* all other result values are unexpected and errors */
2330 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2331 (_("Internal clock error.")),
2332 ("clock returned unexpected return value %d", status));
2333 gst_buffer_unref (*buf);
2335 ret = GST_FLOW_ERROR;
2343 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2344 gst_flow_get_name (ret));
2349 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2350 gst_flow_get_name (ret));
2355 GST_DEBUG_OBJECT (src, "getrange but not started");
2356 return GST_FLOW_WRONG_STATE;
2360 GST_DEBUG_OBJECT (src, "no create function");
2361 return GST_FLOW_NOT_SUPPORTED;
2365 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2366 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2367 return GST_FLOW_EOS;
2369 reached_num_buffers:
2371 GST_DEBUG_OBJECT (src, "sent all buffers");
2372 return GST_FLOW_EOS;
2376 GST_DEBUG_OBJECT (src, "we are flushing");
2377 gst_buffer_unref (*buf);
2379 return GST_FLOW_WRONG_STATE;
2383 GST_DEBUG_OBJECT (src, "we are EOS");
2384 return GST_FLOW_EOS;
2388 static GstFlowReturn
2389 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2390 guint length, GstBuffer ** buf)
2395 src = GST_BASE_SRC_CAST (parent);
2397 GST_LIVE_LOCK (src);
2398 if (G_UNLIKELY (src->priv->flushing))
2401 res = gst_base_src_get_range (src, offset, length, buf);
2404 GST_LIVE_UNLOCK (src);
2411 GST_DEBUG_OBJECT (src, "we are flushing");
2412 res = GST_FLOW_WRONG_STATE;
2418 gst_base_src_is_random_access (GstBaseSrc * src)
2420 /* we need to start the basesrc to check random access */
2421 if (!GST_BASE_SRC_IS_STARTED (src)) {
2422 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2423 if (G_LIKELY (gst_base_src_start (src))) {
2424 if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2426 gst_base_src_stop (src);
2430 return src->random_access;
2435 GST_DEBUG_OBJECT (src, "failed to start");
2441 gst_base_src_loop (GstPad * pad)
2444 GstBuffer *buf = NULL;
2449 GList *pending_events = NULL, *tmp;
2453 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2455 /* check if we need to renegotiate */
2456 if (gst_pad_check_reconfigure (pad)) {
2457 if (!gst_base_src_negotiate (src))
2458 goto not_negotiated;
2461 GST_LIVE_LOCK (src);
2463 if (G_UNLIKELY (src->priv->flushing))
2466 blocksize = src->blocksize;
2468 /* if we operate in bytes, we can calculate an offset */
2469 if (src->segment.format == GST_FORMAT_BYTES) {
2470 position = src->segment.position;
2471 /* for negative rates, start with subtracting the blocksize */
2472 if (src->segment.rate < 0.0) {
2473 /* we cannot go below segment.start */
2474 if (position > src->segment.start + blocksize)
2475 position -= blocksize;
2477 /* last block, remainder up to segment.start */
2478 blocksize = position - src->segment.start;
2479 position = src->segment.start;
2485 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2486 GST_TIME_ARGS (position), blocksize);
2488 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2489 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2490 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2491 gst_flow_get_name (ret));
2492 GST_LIVE_UNLOCK (src);
2495 /* this should not happen */
2496 if (G_UNLIKELY (buf == NULL))
2499 /* push events to close/start our segment before we push the buffer. */
2500 if (G_UNLIKELY (src->priv->segment_pending)) {
2501 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2502 src->priv->segment_pending = FALSE;
2505 if (g_atomic_int_get (&src->priv->have_events)) {
2506 GST_OBJECT_LOCK (src);
2507 /* take the events */
2508 pending_events = src->priv->pending_events;
2509 src->priv->pending_events = NULL;
2510 g_atomic_int_set (&src->priv->have_events, FALSE);
2511 GST_OBJECT_UNLOCK (src);
2514 /* Push out pending events if any */
2515 if (G_UNLIKELY (pending_events != NULL)) {
2516 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2517 GstEvent *ev = (GstEvent *) tmp->data;
2518 gst_pad_push_event (pad, ev);
2520 g_list_free (pending_events);
2523 /* figure out the new position */
2524 switch (src->segment.format) {
2525 case GST_FORMAT_BYTES:
2527 guint bufsize = gst_buffer_get_size (buf);
2529 /* we subtracted above for negative rates */
2530 if (src->segment.rate >= 0.0)
2531 position += bufsize;
2534 case GST_FORMAT_TIME:
2536 GstClockTime start, duration;
2538 start = GST_BUFFER_TIMESTAMP (buf);
2539 duration = GST_BUFFER_DURATION (buf);
2541 if (GST_CLOCK_TIME_IS_VALID (start))
2544 position = src->segment.position;
2546 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2547 if (src->segment.rate >= 0.0)
2548 position += duration;
2549 else if (position > duration)
2550 position -= duration;
2556 case GST_FORMAT_DEFAULT:
2557 if (src->segment.rate >= 0.0)
2558 position = GST_BUFFER_OFFSET_END (buf);
2560 position = GST_BUFFER_OFFSET (buf);
2566 if (position != -1) {
2567 if (src->segment.rate >= 0.0) {
2568 /* positive rate, check if we reached the stop */
2569 if (src->segment.stop != -1) {
2570 if (position >= src->segment.stop) {
2572 position = src->segment.stop;
2576 /* negative rate, check if we reached the start. start is always set to
2577 * something different from -1 */
2578 if (position <= src->segment.start) {
2580 position = src->segment.start;
2582 /* when going reverse, all buffers are DISCONT */
2583 src->priv->discont = TRUE;
2585 GST_OBJECT_LOCK (src);
2586 src->segment.position = position;
2587 GST_OBJECT_UNLOCK (src);
2590 if (G_UNLIKELY (src->priv->discont)) {
2591 GST_INFO_OBJECT (src, "marking pending DISCONT");
2592 buf = gst_buffer_make_writable (buf);
2593 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2594 src->priv->discont = FALSE;
2596 GST_LIVE_UNLOCK (src);
2598 ret = gst_pad_push (pad, buf);
2599 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2600 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2601 gst_flow_get_name (ret));
2605 if (G_UNLIKELY (eos)) {
2606 GST_INFO_OBJECT (src, "pausing after end of segment");
2617 GST_DEBUG_OBJECT (src, "Failed to renegotiate");
2618 ret = GST_FLOW_NOT_NEGOTIATED;
2623 GST_DEBUG_OBJECT (src, "we are flushing");
2624 GST_LIVE_UNLOCK (src);
2625 ret = GST_FLOW_WRONG_STATE;
2630 const gchar *reason = gst_flow_get_name (ret);
2633 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2634 src->running = FALSE;
2635 gst_pad_pause_task (pad);
2636 if (ret == GST_FLOW_EOS) {
2637 gboolean flag_segment;
2641 /* perform EOS logic */
2642 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2643 format = src->segment.format;
2644 position = src->segment.position;
2647 GstMessage *message;
2649 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2651 gst_message_set_seqnum (message, src->priv->seqnum);
2652 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2654 event = gst_event_new_eos ();
2655 gst_event_set_seqnum (event, src->priv->seqnum);
2656 gst_pad_push_event (pad, event);
2658 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2659 event = gst_event_new_eos ();
2660 gst_event_set_seqnum (event, src->priv->seqnum);
2661 /* for fatal errors we post an error message, post the error
2662 * first so the app knows about the error first.
2663 * Also don't do this for WRONG_STATE because it happens
2664 * due to flushing and posting an error message because of
2665 * that is the wrong thing to do, e.g. when we're doing
2666 * a flushing seek. */
2667 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2668 (_("Internal data flow error.")),
2669 ("streaming task paused, reason %s (%d)", reason, ret));
2670 gst_pad_push_event (pad, event);
2676 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2677 (_("Internal data flow error.")), ("element returned NULL buffer"));
2678 GST_LIVE_UNLOCK (src);
2684 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2685 const GstAllocator * allocator, guint prefix, guint alignment)
2687 GstBufferPool *oldpool;
2688 GstBaseSrcPrivate *priv = basesrc->priv;
2691 GST_DEBUG_OBJECT (basesrc, "activate pool");
2692 if (!gst_buffer_pool_set_active (pool, TRUE))
2693 goto activate_failed;
2696 GST_OBJECT_LOCK (basesrc);
2697 oldpool = priv->pool;
2700 priv->allocator = allocator;
2702 priv->prefix = prefix;
2703 priv->alignment = alignment;
2704 GST_OBJECT_UNLOCK (basesrc);
2707 /* only deactivate if the pool is not the one we're using */
2708 if (oldpool != pool) {
2709 GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2710 gst_buffer_pool_set_active (oldpool, FALSE);
2712 gst_object_unref (oldpool);
2719 GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2725 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
2727 GstBaseSrcPrivate *priv = basesrc->priv;
2728 GstBufferPool *pool;
2729 gboolean res = TRUE;
2731 GST_OBJECT_LOCK (basesrc);
2732 if ((pool = priv->pool))
2733 pool = gst_object_ref (pool);
2734 GST_OBJECT_UNLOCK (basesrc);
2737 res = gst_buffer_pool_set_active (pool, active);
2738 gst_object_unref (pool);
2744 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
2746 GstBaseSrcClass *bclass;
2747 gboolean result = TRUE;
2749 GstBufferPool *pool = NULL;
2750 const GstAllocator *allocator = NULL;
2751 guint size, min, max, prefix, alignment;
2753 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2755 /* make query and let peer pad answer, we don't really care if it worked or
2756 * not, if it failed, the allocation query would contain defaults and the
2757 * subclass would then set better values if needed */
2758 query = gst_query_new_allocation (caps, TRUE);
2759 if (!gst_pad_peer_query (basesrc->srcpad, query)) {
2760 /* not a problem, just debug a little */
2761 GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
2764 if (G_LIKELY (bclass->decide_allocation))
2765 result = bclass->decide_allocation (basesrc, query);
2767 GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
2769 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
2773 const gchar *mem = NULL;
2775 /* no size, we have variable size buffers */
2776 if (gst_query_get_n_allocation_memories (query) > 0) {
2777 mem = gst_query_parse_nth_allocation_memory (query, 0);
2779 GST_DEBUG_OBJECT (basesrc, "0 size, getting allocator %s",
2780 GST_STR_NULL (mem));
2781 allocator = gst_allocator_find (mem);
2782 } else if (pool == NULL) {
2783 /* fixed size, we can use a bufferpool */
2784 GstStructure *config;
2786 /* we did not get a pool, make one ourselves then */
2787 pool = gst_buffer_pool_new ();
2788 GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
2790 config = gst_buffer_pool_get_config (pool);
2791 gst_buffer_pool_config_set (config, caps, size, min, max, prefix,
2793 gst_buffer_pool_set_config (pool, config);
2796 gst_query_unref (query);
2799 gst_base_src_set_allocation (basesrc, pool, allocator, prefix, alignment);
2805 /* default negotiation code.
2807 * Take intersection between src and sink pads, take first
2811 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2814 GstCaps *caps = NULL;
2815 GstCaps *peercaps = NULL;
2816 gboolean result = FALSE;
2818 /* first see what is possible on our source pad */
2819 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
2820 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2821 /* nothing or anything is allowed, we're done */
2822 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2823 goto no_nego_needed;
2825 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2828 /* get the peer caps */
2829 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
2830 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2832 /* The result is already a subset of our caps */
2834 gst_caps_unref (thiscaps);
2836 /* no peer, work with our own caps then */
2839 if (caps && !gst_caps_is_empty (caps)) {
2841 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
2842 if (gst_caps_is_any (caps)) {
2843 GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
2844 /* hmm, still anything, so element can do anything and
2845 * nego is not needed */
2848 caps = gst_caps_make_writable (caps);
2849 gst_base_src_fixate (basesrc, caps);
2850 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2851 if (gst_caps_is_fixed (caps)) {
2852 /* yay, fixed caps, use those then, it's possible that the subclass does
2853 * not accept this caps after all and we have to fail. */
2854 result = gst_base_src_set_caps (basesrc, caps);
2857 gst_caps_unref (caps);
2860 gst_caps_unref (caps);
2861 GST_DEBUG_OBJECT (basesrc, "no common caps");
2867 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2869 gst_caps_unref (thiscaps);
2874 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2875 ("No supported formats found"),
2876 ("This element did not produce valid caps"));
2878 gst_caps_unref (thiscaps);
2884 gst_base_src_negotiate (GstBaseSrc * basesrc)
2886 GstBaseSrcClass *bclass;
2889 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2891 GST_DEBUG_OBJECT (basesrc, "starting negotiation");
2893 if (G_LIKELY (bclass->negotiate))
2894 result = bclass->negotiate (basesrc);
2898 if (G_LIKELY (result)) {
2901 caps = gst_pad_get_current_caps (basesrc->srcpad);
2903 result = gst_base_src_prepare_allocation (basesrc, caps);
2906 gst_caps_unref (caps);
2912 gst_base_src_start (GstBaseSrc * basesrc)
2914 GstBaseSrcClass *bclass;
2917 GST_LIVE_LOCK (basesrc);
2918 if (GST_BASE_SRC_IS_STARTING (basesrc))
2920 if (GST_BASE_SRC_IS_STARTED (basesrc))
2923 basesrc->priv->start_result = GST_FLOW_WRONG_STATE;
2924 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
2925 basesrc->num_buffers_left = basesrc->num_buffers;
2926 basesrc->running = FALSE;
2927 basesrc->priv->segment_pending = FALSE;
2928 GST_OBJECT_LOCK (basesrc);
2929 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2930 GST_OBJECT_UNLOCK (basesrc);
2931 GST_LIVE_UNLOCK (basesrc);
2933 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2935 result = bclass->start (basesrc);
2940 goto could_not_start;
2942 if (!gst_base_src_is_async (basesrc))
2943 gst_base_src_start_complete (basesrc, GST_FLOW_OK);
2950 GST_DEBUG_OBJECT (basesrc, "was starting");
2951 GST_LIVE_UNLOCK (basesrc);
2956 GST_DEBUG_OBJECT (basesrc, "was started");
2957 GST_LIVE_UNLOCK (basesrc);
2962 GST_DEBUG_OBJECT (basesrc, "could not start");
2963 /* subclass is supposed to post a message. We don't have to call _stop. */
2964 gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
2970 * gst_base_src_start_complete:
2971 * @src: base source instance
2972 * @ret: a #GstFlowReturn
2974 * Complete an asynchronous start operation. When the subclass overrides the
2975 * start method, it should call gst_base_src_start_complete() when the start
2976 * operation completes either from the same thread or from an asynchronous
2980 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
2988 GstBaseSrcClass *bclass;
2990 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2992 if (ret != GST_FLOW_OK)
2995 GST_DEBUG_OBJECT (basesrc, "starting source");
2996 format = basesrc->segment.format;
2998 /* figure out the size */
3001 if (format == GST_FORMAT_BYTES) {
3002 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3004 if (bclass->get_size) {
3005 if (!(have_size = bclass->get_size (basesrc, &size)))
3008 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3009 /* only update the size when operating in bytes, subclass is supposed
3010 * to set duration in the start method for other formats */
3011 GST_OBJECT_LOCK (basesrc);
3012 basesrc->segment.duration = size;
3013 GST_OBJECT_UNLOCK (basesrc);
3016 GST_DEBUG_OBJECT (basesrc,
3017 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3018 G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3019 basesrc->segment.duration);
3021 seekable = gst_base_src_seekable (basesrc);
3022 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3024 /* update for random access flag */
3025 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3027 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3029 /* stop flushing now but for live sources, still block in the LIVE lock when
3030 * we are not yet PLAYING */
3031 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
3033 GST_OBJECT_LOCK (basesrc->srcpad);
3034 mode = GST_PAD_MODE (basesrc->srcpad);
3035 GST_OBJECT_UNLOCK (basesrc->srcpad);
3037 if (mode == GST_PAD_MODE_PUSH) {
3038 /* do initial seek, which will start the task */
3039 GST_OBJECT_LOCK (basesrc);
3040 event = basesrc->pending_seek;
3041 basesrc->pending_seek = NULL;
3042 GST_OBJECT_UNLOCK (basesrc);
3044 /* no need to unlock anything, the task is certainly
3045 * not running here. The perform seek code will start the task when
3047 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3051 gst_event_unref (event);
3053 /* if not random_access, we cannot operate in pull mode for now */
3054 if (G_UNLIKELY (!basesrc->random_access))
3058 gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3060 GST_LIVE_LOCK (basesrc);
3061 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3062 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3063 basesrc->priv->start_result = ret;
3064 GST_LIVE_SIGNAL (basesrc);
3065 GST_LIVE_UNLOCK (basesrc);
3071 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3072 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3074 gst_event_unref (event);
3075 ret = GST_FLOW_ERROR;
3080 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3081 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3082 ret = GST_FLOW_ERROR;
3087 GST_LIVE_LOCK (basesrc);
3088 basesrc->priv->start_result = ret;
3089 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3090 GST_LIVE_SIGNAL (basesrc);
3091 GST_LIVE_UNLOCK (basesrc);
3097 * gst_base_src_start_complete:
3098 * @src: base source instance
3099 * @ret: a #GstFlowReturn
3101 * Wait until the start operation completes.
3103 * Returns: a #GstFlowReturn.
3106 gst_base_src_start_wait (GstBaseSrc * basesrc)
3108 GstFlowReturn result;
3110 GST_LIVE_LOCK (basesrc);
3111 if (G_UNLIKELY (basesrc->priv->flushing))
3114 while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3115 GST_LIVE_WAIT (basesrc);
3116 if (G_UNLIKELY (basesrc->priv->flushing))
3119 result = basesrc->priv->start_result;
3120 GST_LIVE_UNLOCK (basesrc);
3127 GST_DEBUG_OBJECT (basesrc, "we are flushing");
3128 GST_LIVE_UNLOCK (basesrc);
3129 return GST_FLOW_WRONG_STATE;
3134 gst_base_src_stop (GstBaseSrc * basesrc)
3136 GstBaseSrcClass *bclass;
3137 gboolean result = TRUE;
3139 GST_DEBUG_OBJECT (basesrc, "stopping source");
3142 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3144 gst_pad_stop_task (basesrc->srcpad);
3146 GST_LIVE_LOCK (basesrc);
3147 if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3150 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3151 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3152 basesrc->priv->start_result = GST_FLOW_WRONG_STATE;
3153 GST_LIVE_SIGNAL (basesrc);
3154 GST_LIVE_UNLOCK (basesrc);
3156 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3158 result = bclass->stop (basesrc);
3160 gst_base_src_set_allocation (basesrc, NULL, NULL, 0, 0);
3166 GST_DEBUG_OBJECT (basesrc, "was started");
3167 GST_LIVE_UNLOCK (basesrc);
3172 /* start or stop flushing dataprocessing
3175 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3176 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
3178 GstBaseSrcClass *bclass;
3180 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3182 if (flushing && unlock) {
3183 gst_base_src_activate_pool (basesrc, FALSE);
3184 /* unlock any subclasses, we need to do this before grabbing the
3185 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3186 * unlock to the params because of backwards compat (see seek handler)*/
3188 bclass->unlock (basesrc);
3191 /* the live lock is released when we are blocked, waiting for playing or
3192 * when we sync to the clock. */
3193 GST_LIVE_LOCK (basesrc);
3195 *playing = basesrc->live_running;
3196 basesrc->priv->flushing = flushing;
3198 /* if we are locked in the live lock, signal it to make it flush */
3199 basesrc->live_running = TRUE;
3201 /* clear pending EOS if any */
3202 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3204 /* step 1, now that we have the LIVE lock, clear our unlock request */
3205 if (bclass->unlock_stop)
3206 bclass->unlock_stop (basesrc);
3208 /* step 2, unblock clock sync (if any) or any other blocking thing */
3209 if (basesrc->clock_id)
3210 gst_clock_id_unschedule (basesrc->clock_id);
3212 /* signal the live source that it can start playing */
3213 basesrc->live_running = live_play;
3215 gst_base_src_activate_pool (basesrc, TRUE);
3217 /* When unlocking drop all delayed events */
3219 GST_OBJECT_LOCK (basesrc);
3220 if (basesrc->priv->pending_events) {
3221 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3223 g_list_free (basesrc->priv->pending_events);
3224 basesrc->priv->pending_events = NULL;
3225 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3227 GST_OBJECT_UNLOCK (basesrc);
3230 GST_LIVE_SIGNAL (basesrc);
3231 GST_LIVE_UNLOCK (basesrc);
3236 /* the purpose of this function is to make sure that a live source blocks in the
3237 * LIVE lock or leaves the LIVE lock and continues playing. */
3239 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3241 GstBaseSrcClass *bclass;
3243 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3245 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3247 GST_DEBUG_OBJECT (basesrc, "unlock");
3249 bclass->unlock (basesrc);
3252 /* we are now able to grab the LIVE lock, when we get it, we can be
3253 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3255 GST_LIVE_LOCK (basesrc);
3256 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3258 /* unblock clock sync (if any) */
3259 if (basesrc->clock_id)
3260 gst_clock_id_unschedule (basesrc->clock_id);
3262 /* configure what to do when we get to the LIVE lock. */
3263 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3264 basesrc->live_running = live_play;
3269 /* clear our unlock request when going to PLAYING */
3270 GST_DEBUG_OBJECT (basesrc, "unlock stop");
3271 if (bclass->unlock_stop)
3272 bclass->unlock_stop (basesrc);
3274 /* for live sources we restart the timestamp correction */
3275 basesrc->priv->latency = -1;
3276 /* have to restart the task in case it stopped because of the unlock when
3277 * we went to PAUSED. Only do this if we operating in push mode. */
3278 GST_OBJECT_LOCK (basesrc->srcpad);
3279 start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3280 GST_OBJECT_UNLOCK (basesrc->srcpad);
3282 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3284 GST_DEBUG_OBJECT (basesrc, "signal");
3285 GST_LIVE_SIGNAL (basesrc);
3287 GST_LIVE_UNLOCK (basesrc);
3293 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3295 GstBaseSrc *basesrc;
3297 basesrc = GST_BASE_SRC (parent);
3299 /* prepare subclass first */
3301 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3303 if (G_UNLIKELY (!basesrc->can_activate_push))
3304 goto no_push_activation;
3306 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3309 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3310 /* now we can stop the source */
3311 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3319 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3324 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3329 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3335 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3337 GstBaseSrc *basesrc;
3339 basesrc = GST_BASE_SRC (parent);
3341 /* prepare subclass first */
3343 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3344 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3347 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3348 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3356 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3361 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3367 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3368 GstPadMode mode, gboolean active)
3373 case GST_PAD_MODE_PULL:
3374 res = gst_base_src_activate_pull (pad, parent, active);
3376 case GST_PAD_MODE_PUSH:
3377 res = gst_base_src_activate_push (pad, parent, active);
3380 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3388 static GstStateChangeReturn
3389 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3391 GstBaseSrc *basesrc;
3392 GstStateChangeReturn result;
3393 gboolean no_preroll = FALSE;
3395 basesrc = GST_BASE_SRC (element);
3397 switch (transition) {
3398 case GST_STATE_CHANGE_NULL_TO_READY:
3400 case GST_STATE_CHANGE_READY_TO_PAUSED:
3401 no_preroll = gst_base_src_is_live (basesrc);
3403 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3404 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3405 if (gst_base_src_is_live (basesrc)) {
3406 /* now we can start playback */
3407 gst_base_src_set_playing (basesrc, TRUE);
3415 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3416 transition)) == GST_STATE_CHANGE_FAILURE)
3419 switch (transition) {
3420 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3421 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3422 if (gst_base_src_is_live (basesrc)) {
3423 /* make sure we block in the live lock in PAUSED */
3424 gst_base_src_set_playing (basesrc, FALSE);
3428 case GST_STATE_CHANGE_PAUSED_TO_READY:
3430 /* we don't need to unblock anything here, the pad deactivation code
3431 * already did this */
3432 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3433 gst_event_replace (&basesrc->pending_seek, NULL);
3436 case GST_STATE_CHANGE_READY_TO_NULL:
3442 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3443 result = GST_STATE_CHANGE_NO_PREROLL;
3450 GST_DEBUG_OBJECT (basesrc, "parent failed state change");