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 * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any time
51 * by overriding #GstBaseSrcClass.check_get_range() so that it returns %TRUE.
53 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
54 * automatically seekable in push mode as well. The following conditions must
55 * be met to make the element seekable in push mode when the format is not
59 * #GstBaseSrcClass.is_seekable() returns %TRUE.
62 * #GstBaseSrcClass.query() can convert all supported seek formats to the
63 * internal format as set with gst_base_src_set_format().
66 * #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
71 * When the element does not meet the requirements to operate in pull mode, the
72 * offset and length in the #GstBaseSrcClass.create() method should be ignored.
73 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
74 * element can operate in pull mode but only with specific offsets and
75 * lengths, it is allowed to generate an error when the wrong values are passed
76 * to the #GstBaseSrcClass.create() function.
78 * #GstBaseSrc has support for live sources. Live sources are sources that when
79 * paused discard data, such as audio or video capture devices. A typical live
80 * source also produces data at a fixed rate and thus provides a clock to publish
82 * Use gst_base_src_set_live() to activate the live source mode.
84 * A live source does not produce data in the PAUSED state. This means that the
85 * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
86 * PLAYING. To signal the pipeline that the element will not produce data, the
87 * return value from the READY to PAUSED state will be
88 * #GST_STATE_CHANGE_NO_PREROLL.
90 * A typical live source will timestamp the buffers it creates with the
91 * current running time of the pipeline. This is one reason why a live source
92 * can only produce data in the PLAYING state, when the clock is actually
93 * distributed and running.
95 * Live sources that synchronize and block on the clock (an audio source, for
96 * example) can since 0.10.12 use gst_base_src_wait_playing() when the
97 * #GstBaseSrcClass.create() function was interrupted by a state change to
100 * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
101 * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
102 * function if the source is a live source. The #GstBaseSrcClass.get_times()
103 * function should return timestamps starting from 0, as if it were a non-live
104 * source. The base class will make sure that the timestamps are transformed
105 * into the current running_time. The base source will then wait for the
106 * calculated running_time before pushing out the buffer.
108 * For live sources, the base class will by default report a latency of 0.
109 * For pseudo live sources, the base class will by default measure the difference
110 * between the first buffer timestamp and the start time of get_times and will
111 * report this value as the latency.
112 * Subclasses should override the query function when this behaviour is not
115 * There is only support in #GstBaseSrc for exactly one source pad, which
116 * should be named "src". A source implementation (subclass of #GstBaseSrc)
117 * should install a pad template in its class_init function, like so:
120 * my_element_class_init (GstMyElementClass *klass)
122 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
123 * // srctemplate should be a #GstStaticPadTemplate with direction
124 * // #GST_PAD_SRC and name "src"
125 * gst_element_class_add_pad_template (gstelement_class,
126 * gst_static_pad_template_get (&srctemplate));
127 * // see #GstElementDetails
128 * gst_element_class_set_details (gstelement_class, &details);
133 * <title>Controlled shutdown of live sources in applications</title>
135 * Applications that record from a live source may want to stop recording
136 * in a controlled way, so that the recording is stopped, but the data
137 * already in the pipeline is processed to the end (remember that many live
138 * sources would go on recording forever otherwise). For that to happen the
139 * application needs to make the source stop recording and send an EOS
140 * event down the pipeline. The application would then wait for an
141 * EOS message posted on the pipeline's bus to know when all data has
142 * been processed and the pipeline can safely be stopped.
144 * Since GStreamer 0.10.16 an application may send an EOS event to a source
145 * element to make it perform the EOS logic (send EOS event downstream or post a
146 * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
147 * with the gst_element_send_event() function on the element or its parent bin.
149 * After the EOS has been sent to the element, the application should wait for
150 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
151 * received, it may safely shut down the entire pipeline.
153 * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
154 * is still available but deprecated as it is dangerous and less flexible.
156 * Last reviewed on 2007-12-19 (0.10.16)
168 #include <gst/gst_private.h>
170 #include "gstbasesrc.h"
171 #include "gsttypefindhelper.h"
172 #include <gst/gstmarshal.h>
173 #include <gst/gst-i18n-lib.h>
175 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
176 #define GST_CAT_DEFAULT gst_base_src_debug
178 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
179 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
180 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
181 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
182 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
183 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
184 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
186 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
187 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
189 /* BaseSrc signals and args */
196 #define DEFAULT_BLOCKSIZE 4096
197 #define DEFAULT_NUM_BUFFERS -1
198 #define DEFAULT_TYPEFIND FALSE
199 #define DEFAULT_DO_TIMESTAMP FALSE
210 #define GST_BASE_SRC_GET_PRIVATE(obj) \
211 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
213 struct _GstBaseSrcPrivate
215 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
216 * to avoid the sending of two EOS in some cases) */
220 /* if segment should be sent */
221 gboolean segment_pending;
223 /* if EOS is pending (atomic) */
226 /* startup latency is the time it takes between going to PLAYING and producing
227 * the first BUFFER with running_time 0. This value is included in the latency
229 GstClockTime latency;
230 /* timestamp offset, this is the offset add to the values of gst_times for
231 * pseudo live sources */
232 GstClockTimeDiff ts_offset;
234 gboolean do_timestamp;
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 static GstElementClass *parent_class = NULL;
252 static void gst_base_src_class_init (GstBaseSrcClass * klass);
253 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
254 static void gst_base_src_finalize (GObject * object);
258 gst_base_src_get_type (void)
260 static volatile gsize base_src_type = 0;
262 if (g_once_init_enter (&base_src_type)) {
264 static const GTypeInfo base_src_info = {
265 sizeof (GstBaseSrcClass),
268 (GClassInitFunc) gst_base_src_class_init,
273 (GInstanceInitFunc) gst_base_src_init,
276 _type = g_type_register_static (GST_TYPE_ELEMENT,
277 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
278 g_once_init_leave (&base_src_type, _type);
280 return base_src_type;
283 static GstCaps *gst_base_src_getcaps (GstPad * pad, GstCaps * filter);
284 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
285 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
287 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
288 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
289 static void gst_base_src_set_property (GObject * object, guint prop_id,
290 const GValue * value, GParamSpec * pspec);
291 static void gst_base_src_get_property (GObject * object, guint prop_id,
292 GValue * value, GParamSpec * pspec);
293 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
294 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
295 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
296 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
298 static gboolean gst_base_src_query (GstPad * pad, GstQuery ** query);
300 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
301 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
302 GstSegment * segment);
303 static gboolean gst_base_src_default_query (GstBaseSrc * src,
305 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
306 GstEvent * event, GstSegment * segment);
308 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
309 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
310 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
311 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
313 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
314 GstStateChange transition);
316 static void gst_base_src_loop (GstPad * pad);
317 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
318 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
319 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
320 guint length, GstBuffer ** buf);
321 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
322 guint length, GstBuffer ** buf);
323 static gboolean gst_base_src_seekable (GstBaseSrc * src);
324 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
327 gst_base_src_class_init (GstBaseSrcClass * klass)
329 GObjectClass *gobject_class;
330 GstElementClass *gstelement_class;
332 gobject_class = G_OBJECT_CLASS (klass);
333 gstelement_class = GST_ELEMENT_CLASS (klass);
335 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
337 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
339 parent_class = g_type_class_peek_parent (klass);
341 gobject_class->finalize = gst_base_src_finalize;
342 gobject_class->set_property = gst_base_src_set_property;
343 gobject_class->get_property = gst_base_src_get_property;
345 /* FIXME 0.11: blocksize property should be int, not ulong (min is >max here) */
346 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
347 g_param_spec_ulong ("blocksize", "Block size",
348 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXULONG,
349 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
351 g_param_spec_int ("num-buffers", "num-buffers",
352 "Number of buffers to output before sending EOS (-1 = unlimited)",
353 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
354 G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
356 g_param_spec_boolean ("typefind", "Typefind",
357 "Run typefind before negotiating", DEFAULT_TYPEFIND,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
360 g_param_spec_boolean ("do-timestamp", "Do timestamp",
361 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
362 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 gstelement_class->change_state =
365 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
366 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
367 gstelement_class->get_query_types =
368 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
370 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
371 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
372 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
373 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
374 klass->check_get_range =
375 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
376 klass->prepare_seek_segment =
377 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
379 /* Registering debug symbols for function pointers */
380 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_push);
381 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_pull);
382 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event_handler);
383 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
384 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_get_range);
385 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_check_get_range);
386 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getcaps);
387 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_setcaps);
388 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
392 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
395 GstPadTemplate *pad_template;
397 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
399 basesrc->is_live = FALSE;
400 basesrc->live_lock = g_mutex_new ();
401 basesrc->live_cond = g_cond_new ();
402 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
403 basesrc->num_buffers_left = -1;
405 basesrc->can_activate_push = TRUE;
406 basesrc->pad_mode = GST_ACTIVATE_NONE;
409 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
410 g_return_if_fail (pad_template != NULL);
412 GST_DEBUG_OBJECT (basesrc, "creating src pad");
413 pad = gst_pad_new_from_template (pad_template, "src");
415 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
416 gst_pad_set_activatepush_function (pad, gst_base_src_activate_push);
417 gst_pad_set_activatepull_function (pad, gst_base_src_activate_pull);
418 gst_pad_set_event_function (pad, gst_base_src_event_handler);
419 gst_pad_set_query_function (pad, gst_base_src_query);
420 gst_pad_set_checkgetrange_function (pad, gst_base_src_pad_check_get_range);
421 gst_pad_set_getrange_function (pad, gst_base_src_pad_get_range);
422 gst_pad_set_getcaps_function (pad, gst_base_src_getcaps);
423 gst_pad_set_setcaps_function (pad, gst_base_src_setcaps);
424 gst_pad_set_fixatecaps_function (pad, gst_base_src_fixate);
426 /* hold pointer to pad */
427 basesrc->srcpad = pad;
428 GST_DEBUG_OBJECT (basesrc, "adding src pad");
429 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
431 basesrc->blocksize = DEFAULT_BLOCKSIZE;
432 basesrc->clock_id = NULL;
433 /* we operate in BYTES by default */
434 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
435 basesrc->typefind = DEFAULT_TYPEFIND;
436 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
437 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
439 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
440 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_IS_SOURCE);
442 GST_DEBUG_OBJECT (basesrc, "init done");
446 gst_base_src_finalize (GObject * object)
451 basesrc = GST_BASE_SRC (object);
453 g_mutex_free (basesrc->live_lock);
454 g_cond_free (basesrc->live_cond);
456 event_p = &basesrc->pending_seek;
457 gst_event_replace (event_p, NULL);
459 if (basesrc->priv->pending_events) {
460 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
462 g_list_free (basesrc->priv->pending_events);
465 G_OBJECT_CLASS (parent_class)->finalize (object);
469 * gst_base_src_wait_playing:
472 * If the #GstBaseSrcClass.create() method performs its own synchronisation
473 * against the clock it must unblock when going from PLAYING to the PAUSED state
474 * and call this method before continuing to produce the remaining data.
476 * This function will block until a state change to PLAYING happens (in which
477 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
478 * to a state change to READY or a FLUSH event (in which case this function
479 * returns #GST_FLOW_WRONG_STATE).
483 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
484 * continue. Any other return value should be returned from the create vmethod.
487 gst_base_src_wait_playing (GstBaseSrc * src)
489 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
492 /* block until the state changes, or we get a flush, or something */
493 GST_DEBUG_OBJECT (src, "live source waiting for running state");
495 GST_DEBUG_OBJECT (src, "live source unlocked");
496 if (src->priv->flushing)
498 } while (G_UNLIKELY (!src->live_running));
505 GST_DEBUG_OBJECT (src, "we are flushing");
506 return GST_FLOW_WRONG_STATE;
511 * gst_base_src_set_live:
512 * @src: base source instance
513 * @live: new live-mode
515 * If the element listens to a live source, @live should
518 * A live source will not produce data in the PAUSED state and
519 * will therefore not be able to participate in the PREROLL phase
520 * of a pipeline. To signal this fact to the application and the
521 * pipeline, the state change return value of the live source will
522 * be GST_STATE_CHANGE_NO_PREROLL.
525 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
527 g_return_if_fail (GST_IS_BASE_SRC (src));
529 GST_OBJECT_LOCK (src);
531 GST_OBJECT_UNLOCK (src);
535 * gst_base_src_is_live:
536 * @src: base source instance
538 * Check if an element is in live mode.
540 * Returns: %TRUE if element is in live mode.
543 gst_base_src_is_live (GstBaseSrc * src)
547 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
549 GST_OBJECT_LOCK (src);
550 result = src->is_live;
551 GST_OBJECT_UNLOCK (src);
557 * gst_base_src_set_format:
558 * @src: base source instance
559 * @format: the format to use
561 * Sets the default format of the source. This will be the format used
562 * for sending NEW_SEGMENT events and for performing seeks.
564 * If a format of GST_FORMAT_BYTES is set, the element will be able to
565 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
567 * This function must only be called in states < %GST_STATE_PAUSED.
572 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
574 g_return_if_fail (GST_IS_BASE_SRC (src));
575 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
577 GST_OBJECT_LOCK (src);
578 gst_segment_init (&src->segment, format);
579 GST_OBJECT_UNLOCK (src);
583 * gst_base_src_query_latency:
585 * @live: (out) (allow-none): if the source is live
586 * @min_latency: (out) (allow-none): the min latency of the source
587 * @max_latency: (out) (allow-none): the max latency of the source
589 * Query the source for the latency parameters. @live will be TRUE when @src is
590 * configured as a live source. @min_latency will be set to the difference
591 * between the running time and the timestamp of the first buffer.
592 * @max_latency is always the undefined value of -1.
594 * This function is mostly used by subclasses.
596 * Returns: TRUE if the query succeeded.
601 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
602 GstClockTime * min_latency, GstClockTime * max_latency)
606 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
608 GST_OBJECT_LOCK (src);
610 *live = src->is_live;
612 /* if we have a startup latency, report this one, else report 0. Subclasses
613 * are supposed to override the query function if they want something
615 if (src->priv->latency != -1)
616 min = src->priv->latency;
625 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
626 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
628 GST_OBJECT_UNLOCK (src);
634 * gst_base_src_set_blocksize:
636 * @blocksize: the new blocksize in bytes
638 * Set the number of bytes that @src will push out with each buffer. When
639 * @blocksize is set to -1, a default length will be used.
643 /* FIXME 0.11: blocksize property should be int, not ulong */
645 gst_base_src_set_blocksize (GstBaseSrc * src, gulong blocksize)
647 g_return_if_fail (GST_IS_BASE_SRC (src));
649 GST_OBJECT_LOCK (src);
650 src->blocksize = blocksize;
651 GST_OBJECT_UNLOCK (src);
655 * gst_base_src_get_blocksize:
658 * Get the number of bytes that @src will push out with each buffer.
660 * Returns: the number of bytes pushed with each buffer.
664 /* FIXME 0.11: blocksize property should be int, not ulong */
666 gst_base_src_get_blocksize (GstBaseSrc * src)
670 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
672 GST_OBJECT_LOCK (src);
673 res = src->blocksize;
674 GST_OBJECT_UNLOCK (src);
681 * gst_base_src_set_do_timestamp:
683 * @timestamp: enable or disable timestamping
685 * Configure @src to automatically timestamp outgoing buffers based on the
686 * current running_time of the pipeline. This property is mostly useful for live
692 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
694 g_return_if_fail (GST_IS_BASE_SRC (src));
696 GST_OBJECT_LOCK (src);
697 src->priv->do_timestamp = timestamp;
698 GST_OBJECT_UNLOCK (src);
702 * gst_base_src_get_do_timestamp:
705 * Query if @src timestamps outgoing buffers based on the current running_time.
707 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
712 gst_base_src_get_do_timestamp (GstBaseSrc * src)
716 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
718 GST_OBJECT_LOCK (src);
719 res = src->priv->do_timestamp;
720 GST_OBJECT_UNLOCK (src);
726 * gst_base_src_new_seamless_segment:
728 * @start: The new start value for the segment
729 * @stop: Stop value for the new segment
730 * @position: The position value for the new segent
732 * Prepare a new seamless segment for emission downstream. This function must
733 * only be called by derived sub-classes, and only from the create() function,
734 * as the stream-lock needs to be held.
736 * The format for the new segment will be the current format of the source, as
737 * configured with gst_base_src_set_format()
739 * Returns: %TRUE if preparation of the seamless segment succeeded.
744 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
749 GST_DEBUG_OBJECT (src,
750 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
751 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
752 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
754 GST_OBJECT_LOCK (src);
756 src->segment.base = gst_segment_to_running_time (&src->segment,
757 src->segment.format, src->segment.position);
758 src->segment.start = start;
759 src->segment.stop = stop;
760 src->segment.position = position;
762 /* forward, we send data from position to stop */
763 src->priv->segment_pending = TRUE;
764 GST_OBJECT_UNLOCK (src);
766 src->priv->discont = TRUE;
773 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
775 GstBaseSrcClass *bclass;
779 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
780 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
782 if (bclass->set_caps)
783 res = bclass->set_caps (bsrc, caps);
789 gst_base_src_getcaps (GstPad * pad, GstCaps * filter)
791 GstBaseSrcClass *bclass;
793 GstCaps *caps = NULL;
795 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
796 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
797 if (bclass->get_caps)
798 caps = bclass->get_caps (bsrc, filter);
801 GstPadTemplate *pad_template;
804 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
805 if (pad_template != NULL) {
806 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
809 GstCaps *intersection;
812 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
813 gst_caps_unref (caps);
822 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
824 GstBaseSrcClass *bclass;
827 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
828 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
831 bclass->fixate (bsrc, caps);
833 gst_object_unref (bsrc);
837 gst_base_src_default_query (GstBaseSrc * src, GstQuery ** query)
841 switch (GST_QUERY_TYPE (*query)) {
842 case GST_QUERY_POSITION:
846 gst_query_parse_position (*query, &format, NULL);
848 GST_DEBUG_OBJECT (src, "position query in format %s",
849 gst_format_get_name (format));
852 case GST_FORMAT_PERCENT:
858 GST_OBJECT_LOCK (src);
859 position = src->segment.position;
860 duration = src->segment.duration;
861 GST_OBJECT_UNLOCK (src);
863 if (position != -1 && duration != -1) {
864 if (position < duration)
865 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
868 percent = GST_FORMAT_PERCENT_MAX;
872 gst_query_set_position (*query, GST_FORMAT_PERCENT, percent);
879 GstFormat seg_format;
881 GST_OBJECT_LOCK (src);
883 gst_segment_to_stream_time (&src->segment, src->segment.format,
884 src->segment.position);
885 seg_format = src->segment.format;
886 GST_OBJECT_UNLOCK (src);
888 if (position != -1) {
889 /* convert to requested format */
891 gst_pad_query_convert (src->srcpad, seg_format,
892 position, &format, &position);
896 gst_query_set_position (*query, format, position);
902 case GST_QUERY_DURATION:
906 gst_query_parse_duration (*query, &format, NULL);
908 GST_DEBUG_OBJECT (src, "duration query in format %s",
909 gst_format_get_name (format));
912 case GST_FORMAT_PERCENT:
913 gst_query_set_duration (*query, GST_FORMAT_PERCENT,
914 GST_FORMAT_PERCENT_MAX);
920 GstFormat seg_format;
922 GST_OBJECT_LOCK (src);
923 /* this is the duration as configured by the subclass. */
924 duration = src->segment.duration;
925 seg_format = src->segment.format;
926 GST_OBJECT_UNLOCK (src);
928 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
929 duration, gst_format_get_name (seg_format));
931 if (duration != -1) {
932 /* convert to requested format, if this fails, we have a duration
933 * but we cannot answer the query, we must return FALSE. */
935 gst_pad_query_convert (src->srcpad, seg_format,
936 duration, &format, &duration);
938 /* The subclass did not configure a duration, we assume that the
939 * media has an unknown duration then and we return TRUE to report
940 * this. Note that this is not the same as returning FALSE, which
941 * means that we cannot report the duration at all. */
944 gst_query_set_duration (*query, format, duration);
951 case GST_QUERY_SEEKING:
953 GstFormat format, seg_format;
956 GST_OBJECT_LOCK (src);
957 duration = src->segment.duration;
958 seg_format = src->segment.format;
959 GST_OBJECT_UNLOCK (src);
961 gst_query_parse_seeking (*query, &format, NULL, NULL, NULL);
962 if (format == seg_format) {
963 gst_query_set_seeking (*query, seg_format,
964 gst_base_src_seekable (src), 0, duration);
967 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
968 /* Don't reply to the query to make up for demuxers which don't
969 * handle the SEEKING query yet. Players like Totem will fall back
970 * to the duration when the SEEKING query isn't answered. */
975 case GST_QUERY_SEGMENT:
979 GST_OBJECT_LOCK (src);
980 /* no end segment configured, current duration then */
981 if ((stop = src->segment.stop) == -1)
982 stop = src->segment.duration;
983 start = src->segment.start;
985 /* adjust to stream time */
986 if (src->segment.time != -1) {
987 start -= src->segment.time;
989 stop -= src->segment.time;
992 gst_query_set_segment (*query, src->segment.rate, src->segment.format,
994 GST_OBJECT_UNLOCK (src);
999 case GST_QUERY_FORMATS:
1001 gst_query_set_formats (*query, 3, GST_FORMAT_DEFAULT,
1002 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1006 case GST_QUERY_CONVERT:
1008 GstFormat src_fmt, dest_fmt;
1009 gint64 src_val, dest_val;
1011 gst_query_parse_convert (*query, &src_fmt, &src_val, &dest_fmt,
1014 /* we can only convert between equal formats... */
1015 if (src_fmt == dest_fmt) {
1021 gst_query_set_convert (*query, src_fmt, src_val, dest_fmt, dest_val);
1024 case GST_QUERY_LATENCY:
1026 GstClockTime min, max;
1029 /* Subclasses should override and implement something usefull */
1030 res = gst_base_src_query_latency (src, &live, &min, &max);
1032 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1033 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1034 GST_TIME_ARGS (max));
1036 gst_query_set_latency (*query, live, min, max);
1039 case GST_QUERY_JITTER:
1040 case GST_QUERY_RATE:
1043 case GST_QUERY_BUFFERING:
1045 GstFormat format, seg_format;
1046 gint64 start, stop, estimated;
1048 gst_query_parse_buffering_range (*query, &format, NULL, NULL, NULL);
1050 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1051 gst_format_get_name (format));
1053 GST_OBJECT_LOCK (src);
1054 if (src->random_access) {
1057 if (format == GST_FORMAT_PERCENT)
1058 stop = GST_FORMAT_PERCENT_MAX;
1060 stop = src->segment.duration;
1066 seg_format = src->segment.format;
1067 GST_OBJECT_UNLOCK (src);
1069 /* convert to required format. When the conversion fails, we can't answer
1070 * the query. When the value is unknown, we can don't perform conversion
1071 * but report TRUE. */
1072 if (format != GST_FORMAT_PERCENT && stop != -1) {
1073 res = gst_pad_query_convert (src->srcpad, seg_format,
1074 stop, &format, &stop);
1078 if (res && format != GST_FORMAT_PERCENT && start != -1)
1079 res = gst_pad_query_convert (src->srcpad, seg_format,
1080 start, &format, &start);
1082 gst_query_set_buffering_range (*query, format, start, stop, estimated);
1089 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (*query),
1095 gst_base_src_query (GstPad * pad, GstQuery ** query)
1098 GstBaseSrcClass *bclass;
1099 gboolean result = FALSE;
1101 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1102 if (G_UNLIKELY (src == NULL))
1105 bclass = GST_BASE_SRC_GET_CLASS (src);
1108 result = bclass->query (src, query);
1110 result = gst_pad_query_default (pad, query);
1112 gst_object_unref (src);
1118 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1120 gboolean res = TRUE;
1122 /* update our offset if the start/stop position was updated */
1123 if (segment->format == GST_FORMAT_BYTES) {
1124 segment->time = segment->start;
1125 } else if (segment->start == 0) {
1126 /* seek to start, we can implement a default for this. */
1130 GST_INFO_OBJECT (src, "Can't do a default seek");
1137 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1139 GstBaseSrcClass *bclass;
1140 gboolean result = FALSE;
1142 bclass = GST_BASE_SRC_GET_CLASS (src);
1144 if (bclass->do_seek)
1145 result = bclass->do_seek (src, segment);
1150 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1153 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1154 GstSegment * segment)
1156 /* By default, we try one of 2 things:
1157 * - For absolute seek positions, convert the requested position to our
1158 * configured processing format and place it in the output segment \
1159 * - For relative seek positions, convert our current (input) values to the
1160 * seek format, adjust by the relative seek offset and then convert back to
1161 * the processing format
1163 GstSeekType cur_type, stop_type;
1166 GstFormat seek_format, dest_format;
1169 gboolean res = TRUE;
1171 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1172 &cur_type, &cur, &stop_type, &stop);
1173 dest_format = segment->format;
1175 if (seek_format == dest_format) {
1176 gst_segment_do_seek (segment, rate, seek_format, flags,
1177 cur_type, cur, stop_type, stop, &update);
1181 if (cur_type != GST_SEEK_TYPE_NONE) {
1182 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1184 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
1186 cur_type = GST_SEEK_TYPE_SET;
1189 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1190 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1192 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
1194 stop_type = GST_SEEK_TYPE_SET;
1197 /* And finally, configure our output segment in the desired format */
1198 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
1199 stop_type, stop, &update);
1208 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1214 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1215 GstSegment * seeksegment)
1217 GstBaseSrcClass *bclass;
1218 gboolean result = FALSE;
1220 bclass = GST_BASE_SRC_GET_CLASS (src);
1222 if (bclass->prepare_seek_segment)
1223 result = bclass->prepare_seek_segment (src, event, seeksegment);
1228 /* this code implements the seeking. It is a good example
1229 * handling all cases.
1231 * A seek updates the currently configured segment.start
1232 * and segment.stop values based on the SEEK_TYPE. If the
1233 * segment.start value is updated, a seek to this new position
1234 * should be performed.
1236 * The seek can only be executed when we are not currently
1237 * streaming any data, to make sure that this is the case, we
1238 * acquire the STREAM_LOCK which is taken when we are in the
1239 * _loop() function or when a getrange() is called. Normally
1240 * we will not receive a seek if we are operating in pull mode
1241 * though. When we operate as a live source we might block on the live
1242 * cond, which does not release the STREAM_LOCK. Therefore we will try
1243 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1244 * safe to perform the seek.
1246 * When we are in the loop() function, we might be in the middle
1247 * of pushing a buffer, which might block in a sink. To make sure
1248 * that the push gets unblocked we push out a FLUSH_START event.
1249 * Our loop function will get a WRONG_STATE return value from
1250 * the push and will pause, effectively releasing the STREAM_LOCK.
1252 * For a non-flushing seek, we pause the task, which might eventually
1253 * release the STREAM_LOCK. We say eventually because when the sink
1254 * blocks on the sample we might wait a very long time until the sink
1255 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1256 * can continue the seek. A non-flushing seek is normally done in a
1257 * running pipeline to perform seamless playback, this means that the sink is
1258 * PLAYING and will return from its chain function.
1259 * In the case of a non-flushing seek we need to make sure that the
1260 * data we output after the seek is continuous with the previous data,
1261 * this is because a non-flushing seek does not reset the running-time
1262 * to 0. We do this by closing the currently running segment, ie. sending
1263 * a new_segment event with the stop position set to the last processed
1266 * After updating the segment.start/stop values, we prepare for
1267 * streaming again. We push out a FLUSH_STOP to make the peer pad
1268 * accept data again and we start our task again.
1270 * A segment seek posts a message on the bus saying that the playback
1271 * of the segment started. We store the segment flag internally because
1272 * when we reach the segment.stop we have to post a segment.done
1273 * instead of EOS when doing a segment seek.
1275 /* FIXME (0.11), we have the unlock gboolean here because most current
1276 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1277 * the streaming thread isn't running, resulting in bogus unlocks later when it
1278 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1279 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1283 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1285 gboolean res = TRUE, tres;
1287 GstFormat seek_format, dest_format;
1289 GstSeekType cur_type, stop_type;
1291 gboolean flush, playing;
1293 gboolean relative_seek = FALSE;
1294 gboolean seekseg_configured = FALSE;
1295 GstSegment seeksegment;
1299 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1301 GST_OBJECT_LOCK (src);
1302 dest_format = src->segment.format;
1303 GST_OBJECT_UNLOCK (src);
1306 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1307 &cur_type, &cur, &stop_type, &stop);
1309 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1310 SEEK_TYPE_IS_RELATIVE (stop_type);
1312 if (dest_format != seek_format && !relative_seek) {
1313 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1314 * here before taking the stream lock, otherwise we must convert it later,
1315 * once we have the stream lock and can read the last configures segment
1316 * start and stop positions */
1317 gst_segment_init (&seeksegment, dest_format);
1319 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1320 goto prepare_failed;
1322 seekseg_configured = TRUE;
1325 flush = flags & GST_SEEK_FLAG_FLUSH;
1326 seqnum = gst_event_get_seqnum (event);
1329 /* get next seqnum */
1330 seqnum = gst_util_seqnum_next ();
1333 /* send flush start */
1335 tevent = gst_event_new_flush_start ();
1336 gst_event_set_seqnum (tevent, seqnum);
1337 gst_pad_push_event (src->srcpad, tevent);
1339 gst_pad_pause_task (src->srcpad);
1341 /* unblock streaming thread. */
1342 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1344 /* grab streaming lock, this should eventually be possible, either
1345 * because the task is paused, our streaming thread stopped
1346 * or because our peer is flushing. */
1347 GST_PAD_STREAM_LOCK (src->srcpad);
1348 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1349 /* we have seen this event before, issue a warning for now */
1350 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1353 src->priv->seqnum = seqnum;
1354 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1357 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1359 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1360 * copy the current segment info into the temp segment that we can actually
1361 * attempt the seek with. We only update the real segment if the seek suceeds. */
1362 if (!seekseg_configured) {
1363 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1365 /* now configure the final seek segment */
1367 if (seeksegment.format != seek_format) {
1368 /* OK, here's where we give the subclass a chance to convert the relative
1369 * seek into an absolute one in the processing format. We set up any
1370 * absolute seek above, before taking the stream lock. */
1371 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1372 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1377 /* The seek format matches our processing format, no need to ask the
1378 * the subclass to configure the segment. */
1379 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1380 cur_type, cur, stop_type, stop, &update);
1383 /* Else, no seek event passed, so we're just (re)starting the
1388 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1389 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1390 seeksegment.start, seeksegment.stop, seeksegment.position);
1392 /* do the seek, segment.position contains the new position. */
1393 res = gst_base_src_do_seek (src, &seeksegment);
1396 /* and prepare to continue streaming */
1398 tevent = gst_event_new_flush_stop ();
1399 gst_event_set_seqnum (tevent, seqnum);
1400 /* send flush stop, peer will accept data and events again. We
1401 * are not yet providing data as we still have the STREAM_LOCK. */
1402 gst_pad_push_event (src->srcpad, tevent);
1405 /* The subclass must have converted the segment to the processing format
1407 if (res && seeksegment.format != dest_format) {
1408 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1409 "in the correct format. Aborting seek.");
1413 /* if the seek was successful, we update our real segment and push
1414 * out the new segment. */
1416 GST_OBJECT_LOCK (src);
1417 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1418 GST_OBJECT_UNLOCK (src);
1420 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1421 GstMessage *message;
1423 message = gst_message_new_segment_start (GST_OBJECT (src),
1424 seeksegment.format, seeksegment.position);
1425 gst_message_set_seqnum (message, seqnum);
1427 gst_element_post_message (GST_ELEMENT (src), message);
1430 /* for deriving a stop position for the playback segment from the seek
1431 * segment, we must take the duration when the stop is not set */
1432 if ((stop = seeksegment.stop) == -1)
1433 stop = seeksegment.duration;
1435 src->priv->segment_pending = TRUE;
1438 src->priv->discont = TRUE;
1439 src->running = TRUE;
1440 /* and restart the task in case it got paused explicitly or by
1441 * the FLUSH_START event we pushed out. */
1442 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1447 /* and release the lock again so we can continue streaming */
1448 GST_PAD_STREAM_UNLOCK (src->srcpad);
1454 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1459 static const GstQueryType *
1460 gst_base_src_get_query_types (GstElement * element)
1462 static const GstQueryType query_types[] = {
1478 /* all events send to this element directly. This is mainly done from the
1482 gst_base_src_send_event (GstElement * element, GstEvent * event)
1485 gboolean result = FALSE;
1487 src = GST_BASE_SRC (element);
1489 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1491 switch (GST_EVENT_TYPE (event)) {
1492 /* bidirectional events */
1493 case GST_EVENT_FLUSH_START:
1494 case GST_EVENT_FLUSH_STOP:
1495 /* sending random flushes downstream can break stuff,
1496 * especially sync since all segment info will get flushed */
1499 /* downstream serialized events */
1502 GstBaseSrcClass *bclass;
1504 bclass = GST_BASE_SRC_GET_CLASS (src);
1506 /* queue EOS and make sure the task or pull function performs the EOS
1509 * We have two possibilities:
1511 * - Before we are to enter the _create function, we check the pending_eos
1512 * first and do EOS instead of entering it.
1513 * - If we are in the _create function or we did not manage to set the
1514 * flag fast enough and we are about to enter the _create function,
1515 * we unlock it so that we exit with WRONG_STATE immediatly. We then
1516 * check the EOS flag and do the EOS logic.
1518 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1519 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1521 /* unlock the _create function so that we can check the pending_eos flag
1522 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1523 * that we can grab it and stop the unlock again. We don't take the stream
1524 * lock so that this operation is guaranteed to never block. */
1526 bclass->unlock (src);
1528 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1530 GST_LIVE_LOCK (src);
1531 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1532 /* now stop the unlock of the streaming thread again. Grabbing the live
1533 * lock is enough because that protects the create function. */
1534 if (bclass->unlock_stop)
1535 bclass->unlock_stop (src);
1536 GST_LIVE_UNLOCK (src);
1541 case GST_EVENT_SEGMENT:
1542 /* sending random SEGMENT downstream can break sync. */
1545 case GST_EVENT_CUSTOM_DOWNSTREAM:
1546 case GST_EVENT_CUSTOM_BOTH:
1547 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1548 GST_OBJECT_LOCK (src);
1549 src->priv->pending_events =
1550 g_list_append (src->priv->pending_events, event);
1551 g_atomic_int_set (&src->priv->have_events, TRUE);
1552 GST_OBJECT_UNLOCK (src);
1556 case GST_EVENT_BUFFERSIZE:
1557 /* does not seem to make much sense currently */
1560 /* upstream events */
1562 /* elements should override send_event and do something */
1564 case GST_EVENT_SEEK:
1568 GST_OBJECT_LOCK (src->srcpad);
1569 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1571 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1572 GST_OBJECT_UNLOCK (src->srcpad);
1575 GST_DEBUG_OBJECT (src, "performing seek");
1576 /* when we are running in push mode, we can execute the
1577 * seek right now, we need to unlock. */
1578 result = gst_base_src_perform_seek (src, event, TRUE);
1582 /* else we store the event and execute the seek when we
1584 GST_OBJECT_LOCK (src);
1585 GST_DEBUG_OBJECT (src, "queueing seek");
1586 event_p = &src->pending_seek;
1587 gst_event_replace ((GstEvent **) event_p, event);
1588 GST_OBJECT_UNLOCK (src);
1589 /* assume the seek will work */
1594 case GST_EVENT_NAVIGATION:
1595 /* could make sense for elements that do something with navigation events
1596 * but then they would need to override the send_event function */
1598 case GST_EVENT_LATENCY:
1599 /* does not seem to make sense currently */
1603 case GST_EVENT_CUSTOM_UPSTREAM:
1604 /* override send_event if you want this */
1606 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1607 case GST_EVENT_CUSTOM_BOTH_OOB:
1608 /* insert a random custom event into the pipeline */
1609 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1610 result = gst_pad_push_event (src->srcpad, event);
1611 /* we gave away the ref to the event in the push */
1618 /* if we still have a ref to the event, unref it now */
1620 gst_event_unref (event);
1627 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1628 GST_OBJECT_UNLOCK (src->srcpad);
1635 gst_base_src_seekable (GstBaseSrc * src)
1637 GstBaseSrcClass *bclass;
1638 bclass = GST_BASE_SRC_GET_CLASS (src);
1639 if (bclass->is_seekable)
1640 return bclass->is_seekable (src);
1646 gst_base_src_update_qos (GstBaseSrc * src,
1647 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1649 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1650 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1651 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1653 GST_OBJECT_LOCK (src);
1654 src->priv->proportion = proportion;
1655 src->priv->earliest_time = timestamp + diff;
1656 GST_OBJECT_UNLOCK (src);
1661 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1665 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1667 switch (GST_EVENT_TYPE (event)) {
1668 case GST_EVENT_SEEK:
1669 /* is normally called when in push mode */
1670 if (!gst_base_src_seekable (src))
1673 result = gst_base_src_perform_seek (src, event, TRUE);
1675 case GST_EVENT_FLUSH_START:
1676 /* cancel any blocking getrange, is normally called
1677 * when in pull mode. */
1678 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1680 case GST_EVENT_FLUSH_STOP:
1681 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1686 GstClockTimeDiff diff;
1687 GstClockTime timestamp;
1689 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1690 gst_base_src_update_qos (src, proportion, diff, timestamp);
1703 GST_DEBUG_OBJECT (src, "is not seekable");
1709 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1712 GstBaseSrcClass *bclass;
1713 gboolean result = FALSE;
1715 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1716 if (G_UNLIKELY (src == NULL)) {
1717 gst_event_unref (event);
1721 bclass = GST_BASE_SRC_GET_CLASS (src);
1723 if (bclass->event) {
1724 if (!(result = bclass->event (src, event)))
1725 goto subclass_failed;
1729 gst_event_unref (event);
1730 gst_object_unref (src);
1737 GST_DEBUG_OBJECT (src, "subclass refused event");
1743 gst_base_src_set_property (GObject * object, guint prop_id,
1744 const GValue * value, GParamSpec * pspec)
1748 src = GST_BASE_SRC (object);
1751 case PROP_BLOCKSIZE:
1752 gst_base_src_set_blocksize (src, g_value_get_ulong (value));
1754 case PROP_NUM_BUFFERS:
1755 src->num_buffers = g_value_get_int (value);
1758 src->typefind = g_value_get_boolean (value);
1760 case PROP_DO_TIMESTAMP:
1761 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1764 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1770 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1775 src = GST_BASE_SRC (object);
1778 case PROP_BLOCKSIZE:
1779 g_value_set_ulong (value, gst_base_src_get_blocksize (src));
1781 case PROP_NUM_BUFFERS:
1782 g_value_set_int (value, src->num_buffers);
1785 g_value_set_boolean (value, src->typefind);
1787 case PROP_DO_TIMESTAMP:
1788 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1791 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1796 /* with STREAM_LOCK and LOCK */
1797 static GstClockReturn
1798 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1803 id = gst_clock_new_single_shot_id (clock, time);
1805 basesrc->clock_id = id;
1806 /* release the live lock while waiting */
1807 GST_LIVE_UNLOCK (basesrc);
1809 ret = gst_clock_id_wait (id, NULL);
1811 GST_LIVE_LOCK (basesrc);
1812 gst_clock_id_unref (id);
1813 basesrc->clock_id = NULL;
1818 /* perform synchronisation on a buffer.
1821 static GstClockReturn
1822 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1824 GstClockReturn result;
1825 GstClockTime start, end;
1826 GstBaseSrcClass *bclass;
1827 GstClockTime base_time;
1829 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1830 gboolean do_timestamp, first, pseudo_live;
1832 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1835 if (bclass->get_times)
1836 bclass->get_times (basesrc, buffer, &start, &end);
1838 /* get buffer timestamp */
1839 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1841 /* grab the lock to prepare for clocking and calculate the startup
1843 GST_OBJECT_LOCK (basesrc);
1845 /* if we are asked to sync against the clock we are a pseudo live element */
1846 pseudo_live = (start != -1 && basesrc->is_live);
1847 /* check for the first buffer */
1848 first = (basesrc->priv->latency == -1);
1850 if (timestamp != -1 && pseudo_live) {
1851 GstClockTime latency;
1853 /* we have a timestamp and a sync time, latency is the diff */
1854 if (timestamp <= start)
1855 latency = start - timestamp;
1860 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1861 GST_TIME_ARGS (latency));
1862 /* first time we calculate latency, just configure */
1863 basesrc->priv->latency = latency;
1865 if (basesrc->priv->latency != latency) {
1866 /* we have a new latency, FIXME post latency message */
1867 basesrc->priv->latency = latency;
1868 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1869 GST_TIME_ARGS (latency));
1873 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1874 basesrc->is_live, start != -1);
1875 basesrc->priv->latency = 0;
1878 /* get clock, if no clock, we can't sync or do timestamps */
1879 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1882 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1884 do_timestamp = basesrc->priv->do_timestamp;
1886 /* first buffer, calculate the timestamp offset */
1888 GstClockTime running_time;
1890 now = gst_clock_get_time (clock);
1891 running_time = now - base_time;
1893 GST_LOG_OBJECT (basesrc,
1894 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1895 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1896 GST_TIME_ARGS (running_time));
1898 if (pseudo_live && timestamp != -1) {
1899 /* live source and we need to sync, add startup latency to all timestamps
1900 * to get the real running_time. Live sources should always timestamp
1901 * according to the current running time. */
1902 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1904 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1905 GST_TIME_ARGS (basesrc->priv->ts_offset));
1907 basesrc->priv->ts_offset = 0;
1908 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1911 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1913 timestamp = running_time;
1917 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1919 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1920 GST_TIME_ARGS (timestamp));
1923 /* add the timestamp offset we need for sync */
1924 timestamp += basesrc->priv->ts_offset;
1926 /* not the first buffer, the timestamp is the diff between the clock and
1928 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1929 now = gst_clock_get_time (clock);
1931 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1933 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1934 GST_TIME_ARGS (now - base_time));
1938 /* if we don't have a buffer timestamp, we don't sync */
1939 if (!GST_CLOCK_TIME_IS_VALID (start))
1942 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1943 /* for pseudo live sources, add our ts_offset to the timestamp */
1944 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1945 start += basesrc->priv->ts_offset;
1948 GST_LOG_OBJECT (basesrc,
1949 "waiting for clock, base time %" GST_TIME_FORMAT
1950 ", stream_start %" GST_TIME_FORMAT,
1951 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1952 GST_OBJECT_UNLOCK (basesrc);
1954 result = gst_base_src_wait (basesrc, clock, start + base_time);
1956 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1963 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1964 GST_OBJECT_UNLOCK (basesrc);
1965 return GST_CLOCK_OK;
1969 GST_DEBUG_OBJECT (basesrc, "no sync needed");
1970 GST_OBJECT_UNLOCK (basesrc);
1971 return GST_CLOCK_OK;
1975 /* Called with STREAM_LOCK and LIVE_LOCK */
1977 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1979 guint64 size, maxsize;
1980 GstBaseSrcClass *bclass;
1984 bclass = GST_BASE_SRC_GET_CLASS (src);
1986 format = src->segment.format;
1987 stop = src->segment.stop;
1988 /* get total file size */
1989 size = (guint64) src->segment.duration;
1991 /* only operate if we are working with bytes */
1992 if (format != GST_FORMAT_BYTES)
1995 /* the max amount of bytes to read is the total size or
1996 * up to the segment.stop if present. */
1998 maxsize = MIN (size, stop);
2002 GST_DEBUG_OBJECT (src,
2003 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2004 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2005 *length, size, stop, maxsize);
2007 /* check size if we have one */
2008 if (maxsize != -1) {
2009 /* if we run past the end, check if the file became bigger and
2011 if (G_UNLIKELY (offset + *length >= maxsize)) {
2012 /* see if length of the file changed */
2013 if (bclass->get_size)
2014 if (!bclass->get_size (src, &size))
2017 /* make sure we don't exceed the configured segment stop
2020 maxsize = MIN (size, stop);
2024 /* if we are at or past the end, EOS */
2025 if (G_UNLIKELY (offset >= maxsize))
2026 goto unexpected_length;
2028 /* else we can clip to the end */
2029 if (G_UNLIKELY (offset + *length >= maxsize))
2030 *length = maxsize - offset;
2035 /* keep track of current position and update duration.
2036 * segment is in bytes, we checked that above. */
2037 GST_OBJECT_LOCK (src);
2038 src->segment.duration = size;
2039 src->segment.position = offset;
2040 GST_OBJECT_UNLOCK (src);
2051 /* must be called with LIVE_LOCK */
2052 static GstFlowReturn
2053 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2057 GstBaseSrcClass *bclass;
2058 GstClockReturn status;
2060 bclass = GST_BASE_SRC_GET_CLASS (src);
2064 if (G_UNLIKELY (!src->live_running)) {
2065 ret = gst_base_src_wait_playing (src);
2066 if (ret != GST_FLOW_OK)
2071 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
2074 if (G_UNLIKELY (!bclass->create))
2077 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2078 goto unexpected_length;
2080 /* normally we don't count buffers */
2081 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2082 if (src->num_buffers_left == 0)
2083 goto reached_num_buffers;
2085 src->num_buffers_left--;
2088 /* don't enter the create function if a pending EOS event was set. For the
2089 * logic of the pending_eos, check the event function of this class. */
2090 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2093 GST_DEBUG_OBJECT (src,
2094 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2095 G_GINT64_FORMAT, offset, length, src->segment.time);
2097 ret = bclass->create (src, offset, length, buf);
2099 /* The create function could be unlocked because we have a pending EOS. It's
2100 * possible that we have a valid buffer from create that we need to
2101 * discard when the create function returned _OK. */
2102 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2103 if (ret == GST_FLOW_OK) {
2104 gst_buffer_unref (*buf);
2110 if (G_UNLIKELY (ret != GST_FLOW_OK))
2113 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2114 if (offset == 0 && src->segment.time == 0
2115 && GST_BUFFER_TIMESTAMP (*buf) == -1 && !src->is_live) {
2116 *buf = gst_buffer_make_writable (*buf);
2117 GST_BUFFER_TIMESTAMP (*buf) = 0;
2120 /* now sync before pushing the buffer */
2121 status = gst_base_src_do_sync (src, *buf);
2123 /* waiting for the clock could have made us flushing */
2124 if (G_UNLIKELY (src->priv->flushing))
2128 case GST_CLOCK_EARLY:
2129 /* the buffer is too late. We currently don't drop the buffer. */
2130 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2133 /* buffer synchronised properly */
2134 GST_DEBUG_OBJECT (src, "buffer ok");
2136 case GST_CLOCK_UNSCHEDULED:
2137 /* this case is triggered when we were waiting for the clock and
2138 * it got unlocked because we did a state change. In any case, get rid of
2140 gst_buffer_unref (*buf);
2142 if (!src->live_running) {
2143 /* We return WRONG_STATE when we are not running to stop the dataflow also
2144 * get rid of the produced buffer. */
2145 GST_DEBUG_OBJECT (src,
2146 "clock was unscheduled (%d), returning WRONG_STATE", status);
2147 ret = GST_FLOW_WRONG_STATE;
2149 /* If we are running when this happens, we quickly switched between
2150 * pause and playing. We try to produce a new buffer */
2151 GST_DEBUG_OBJECT (src,
2152 "clock was unscheduled (%d), but we are running", status);
2157 /* all other result values are unexpected and errors */
2158 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2159 (_("Internal clock error.")),
2160 ("clock returned unexpected return value %d", status));
2161 gst_buffer_unref (*buf);
2163 ret = GST_FLOW_ERROR;
2171 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2172 gst_flow_get_name (ret));
2177 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2178 gst_flow_get_name (ret));
2183 GST_DEBUG_OBJECT (src, "getrange but not started");
2184 return GST_FLOW_WRONG_STATE;
2188 GST_DEBUG_OBJECT (src, "no create function");
2189 return GST_FLOW_ERROR;
2193 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2194 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2195 return GST_FLOW_UNEXPECTED;
2197 reached_num_buffers:
2199 GST_DEBUG_OBJECT (src, "sent all buffers");
2200 return GST_FLOW_UNEXPECTED;
2204 GST_DEBUG_OBJECT (src, "we are flushing");
2205 gst_buffer_unref (*buf);
2207 return GST_FLOW_WRONG_STATE;
2211 GST_DEBUG_OBJECT (src, "we are EOS");
2212 return GST_FLOW_UNEXPECTED;
2216 static GstFlowReturn
2217 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
2223 src = GST_BASE_SRC_CAST (gst_object_ref (GST_OBJECT_PARENT (pad)));
2225 GST_LIVE_LOCK (src);
2226 if (G_UNLIKELY (src->priv->flushing))
2229 res = gst_base_src_get_range (src, offset, length, buf);
2232 GST_LIVE_UNLOCK (src);
2234 gst_object_unref (src);
2241 GST_DEBUG_OBJECT (src, "we are flushing");
2242 res = GST_FLOW_WRONG_STATE;
2248 gst_base_src_default_check_get_range (GstBaseSrc * src)
2252 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
2253 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2254 if (G_LIKELY (gst_base_src_start (src)))
2255 gst_base_src_stop (src);
2258 /* we can operate in getrange mode if the native format is bytes
2259 * and we are seekable, this condition is set in the random_access
2260 * flag and is set in the _start() method. */
2261 res = src->random_access;
2267 gst_base_src_check_get_range (GstBaseSrc * src)
2269 GstBaseSrcClass *bclass;
2272 bclass = GST_BASE_SRC_GET_CLASS (src);
2274 if (bclass->check_get_range == NULL)
2277 res = bclass->check_get_range (src);
2278 GST_LOG_OBJECT (src, "%s() returned %d",
2279 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
2286 GST_WARNING_OBJECT (src, "no check_get_range function set");
2292 gst_base_src_pad_check_get_range (GstPad * pad)
2297 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2299 res = gst_base_src_check_get_range (src);
2305 gst_base_src_loop (GstPad * pad)
2308 GstBuffer *buf = NULL;
2313 GList *pending_events = NULL, *tmp;
2314 gboolean reconfigure;
2318 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2320 GST_OBJECT_LOCK (pad);
2321 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
2322 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
2323 GST_OBJECT_UNLOCK (pad);
2324 /* check if we need to renegotiate */
2326 if (!gst_base_src_negotiate (src))
2327 GST_DEBUG_OBJECT (src, "Failed to renegotiate");
2330 GST_LIVE_LOCK (src);
2332 if (G_UNLIKELY (src->priv->flushing))
2335 src->priv->last_sent_eos = FALSE;
2337 blocksize = src->blocksize;
2339 /* if we operate in bytes, we can calculate an offset */
2340 if (src->segment.format == GST_FORMAT_BYTES) {
2341 position = src->segment.position;
2342 /* for negative rates, start with subtracting the blocksize */
2343 if (src->segment.rate < 0.0) {
2344 /* we cannot go below segment.start */
2345 if (position > src->segment.start + blocksize)
2346 position -= blocksize;
2348 /* last block, remainder up to segment.start */
2349 blocksize = position - src->segment.start;
2350 position = src->segment.start;
2356 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
2357 GST_TIME_ARGS (position), blocksize);
2359 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2360 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2361 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2362 gst_flow_get_name (ret));
2363 GST_LIVE_UNLOCK (src);
2366 /* this should not happen */
2367 if (G_UNLIKELY (buf == NULL))
2370 /* push events to close/start our segment before we push the buffer. */
2371 if (G_UNLIKELY (src->priv->segment_pending)) {
2372 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2373 src->priv->segment_pending = FALSE;
2376 if (g_atomic_int_get (&src->priv->have_events)) {
2377 GST_OBJECT_LOCK (src);
2378 /* take the events */
2379 pending_events = src->priv->pending_events;
2380 src->priv->pending_events = NULL;
2381 g_atomic_int_set (&src->priv->have_events, FALSE);
2382 GST_OBJECT_UNLOCK (src);
2385 /* Push out pending events if any */
2386 if (G_UNLIKELY (pending_events != NULL)) {
2387 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2388 GstEvent *ev = (GstEvent *) tmp->data;
2389 gst_pad_push_event (pad, ev);
2391 g_list_free (pending_events);
2394 /* figure out the new position */
2395 switch (src->segment.format) {
2396 case GST_FORMAT_BYTES:
2398 guint bufsize = gst_buffer_get_size (buf);
2400 /* we subtracted above for negative rates */
2401 if (src->segment.rate >= 0.0)
2402 position += bufsize;
2405 case GST_FORMAT_TIME:
2407 GstClockTime start, duration;
2409 start = GST_BUFFER_TIMESTAMP (buf);
2410 duration = GST_BUFFER_DURATION (buf);
2412 if (GST_CLOCK_TIME_IS_VALID (start))
2415 position = src->segment.position;
2417 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2418 if (src->segment.rate >= 0.0)
2419 position += duration;
2420 else if (position > duration)
2421 position -= duration;
2427 case GST_FORMAT_DEFAULT:
2428 if (src->segment.rate >= 0.0)
2429 position = GST_BUFFER_OFFSET_END (buf);
2431 position = GST_BUFFER_OFFSET (buf);
2437 if (position != -1) {
2438 if (src->segment.rate >= 0.0) {
2439 /* positive rate, check if we reached the stop */
2440 if (src->segment.stop != -1) {
2441 if (position >= src->segment.stop) {
2443 position = src->segment.stop;
2447 /* negative rate, check if we reached the start. start is always set to
2448 * something different from -1 */
2449 if (position <= src->segment.start) {
2451 position = src->segment.start;
2453 /* when going reverse, all buffers are DISCONT */
2454 src->priv->discont = TRUE;
2456 GST_OBJECT_LOCK (src);
2457 src->segment.position = position;
2458 GST_OBJECT_UNLOCK (src);
2461 if (G_UNLIKELY (src->priv->discont)) {
2462 buf = gst_buffer_make_writable (buf);
2463 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2464 src->priv->discont = FALSE;
2466 GST_LIVE_UNLOCK (src);
2468 ret = gst_pad_push (pad, buf);
2469 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2470 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2471 gst_flow_get_name (ret));
2475 if (G_UNLIKELY (eos)) {
2476 GST_INFO_OBJECT (src, "pausing after end of segment");
2477 ret = GST_FLOW_UNEXPECTED;
2487 GST_DEBUG_OBJECT (src, "we are flushing");
2488 GST_LIVE_UNLOCK (src);
2489 ret = GST_FLOW_WRONG_STATE;
2494 const gchar *reason = gst_flow_get_name (ret);
2497 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2498 src->running = FALSE;
2499 gst_pad_pause_task (pad);
2500 if (ret == GST_FLOW_UNEXPECTED) {
2501 gboolean flag_segment;
2505 /* perform EOS logic */
2506 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2507 format = src->segment.format;
2508 position = src->segment.position;
2511 GstMessage *message;
2513 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2515 gst_message_set_seqnum (message, src->priv->seqnum);
2516 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2518 event = gst_event_new_eos ();
2519 gst_event_set_seqnum (event, src->priv->seqnum);
2520 gst_pad_push_event (pad, event);
2521 src->priv->last_sent_eos = TRUE;
2523 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_UNEXPECTED) {
2524 event = gst_event_new_eos ();
2525 gst_event_set_seqnum (event, src->priv->seqnum);
2526 /* for fatal errors we post an error message, post the error
2527 * first so the app knows about the error first.
2528 * Also don't do this for WRONG_STATE because it happens
2529 * due to flushing and posting an error message because of
2530 * that is the wrong thing to do, e.g. when we're doing
2531 * a flushing seek. */
2532 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2533 (_("Internal data flow error.")),
2534 ("streaming task paused, reason %s (%d)", reason, ret));
2535 gst_pad_push_event (pad, event);
2536 src->priv->last_sent_eos = TRUE;
2542 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2543 (_("Internal data flow error.")), ("element returned NULL buffer"));
2544 GST_LIVE_UNLOCK (src);
2549 /* default negotiation code.
2551 * Take intersection between src and sink pads, take first
2555 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2558 GstCaps *caps = NULL;
2559 GstCaps *peercaps = NULL;
2560 gboolean result = FALSE;
2562 /* first see what is possible on our source pad */
2563 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
2564 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2565 /* nothing or anything is allowed, we're done */
2566 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2567 goto no_nego_needed;
2569 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2572 /* get the peer caps */
2573 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
2574 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2576 /* The result is already a subset of our caps */
2578 gst_caps_unref (thiscaps);
2580 /* no peer, work with our own caps then */
2583 if (caps && !gst_caps_is_empty (caps)) {
2584 caps = gst_caps_make_writable (caps);
2586 /* take first (and best, since they are sorted) possibility */
2587 gst_caps_truncate (caps);
2590 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
2591 if (gst_caps_is_any (caps)) {
2592 /* hmm, still anything, so element can do anything and
2593 * nego is not needed */
2596 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2597 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2598 if (gst_caps_is_fixed (caps)) {
2599 /* yay, fixed caps, use those then, it's possible that the subclass does
2600 * not accept this caps after all and we have to fail. */
2601 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2604 gst_caps_unref (caps);
2606 GST_DEBUG_OBJECT (basesrc, "no common caps");
2612 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2614 gst_caps_unref (thiscaps);
2619 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2620 ("No supported formats found"),
2621 ("This element did not produce valid caps"));
2623 gst_caps_unref (thiscaps);
2629 gst_base_src_negotiate (GstBaseSrc * basesrc)
2631 GstBaseSrcClass *bclass;
2632 gboolean result = TRUE;
2634 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2636 if (bclass->negotiate)
2637 result = bclass->negotiate (basesrc);
2643 gst_base_src_start (GstBaseSrc * basesrc)
2645 GstBaseSrcClass *bclass;
2651 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2654 GST_DEBUG_OBJECT (basesrc, "starting source");
2656 basesrc->num_buffers_left = basesrc->num_buffers;
2658 GST_OBJECT_LOCK (basesrc);
2659 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2660 GST_OBJECT_UNLOCK (basesrc);
2662 basesrc->running = FALSE;
2663 basesrc->priv->segment_pending = FALSE;
2665 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2667 result = bclass->start (basesrc);
2672 goto could_not_start;
2674 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2676 format = basesrc->segment.format;
2678 /* figure out the size */
2679 if (format == GST_FORMAT_BYTES) {
2680 if (bclass->get_size) {
2681 if (!(result = bclass->get_size (basesrc, &size)))
2687 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2688 /* only update the size when operating in bytes, subclass is supposed
2689 * to set duration in the start method for other formats */
2690 GST_OBJECT_LOCK (basesrc);
2691 basesrc->segment.duration = size;
2692 GST_OBJECT_UNLOCK (basesrc);
2697 GST_DEBUG_OBJECT (basesrc,
2698 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2699 G_GINT64_FORMAT, gst_format_get_name (format), result, size,
2700 basesrc->segment.duration);
2702 seekable = gst_base_src_seekable (basesrc);
2703 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
2705 /* update for random access flag */
2706 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
2708 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2710 /* run typefind if we are random_access and the typefinding is enabled. */
2711 if (basesrc->random_access && basesrc->typefind && size != -1) {
2714 if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
2715 goto typefind_failed;
2717 result = gst_pad_set_caps (basesrc->srcpad, caps);
2718 gst_caps_unref (caps);
2720 /* use class or default negotiate function */
2721 if (!(result = gst_base_src_negotiate (basesrc)))
2722 goto could_not_negotiate;
2730 GST_DEBUG_OBJECT (basesrc, "could not start");
2731 /* subclass is supposed to post a message. We don't have to call _stop. */
2734 could_not_negotiate:
2736 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2737 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2738 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2739 /* we must call stop */
2740 gst_base_src_stop (basesrc);
2745 GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
2746 GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
2747 /* we must call stop */
2748 gst_base_src_stop (basesrc);
2754 gst_base_src_stop (GstBaseSrc * basesrc)
2756 GstBaseSrcClass *bclass;
2757 gboolean result = TRUE;
2759 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2762 GST_DEBUG_OBJECT (basesrc, "stopping source");
2764 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2766 result = bclass->stop (basesrc);
2769 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2774 /* start or stop flushing dataprocessing
2777 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2778 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2780 GstBaseSrcClass *bclass;
2782 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2784 if (flushing && unlock) {
2785 /* unlock any subclasses, we need to do this before grabbing the
2786 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2787 * unlock to the params because of backwards compat (see seek handler)*/
2789 bclass->unlock (basesrc);
2792 /* the live lock is released when we are blocked, waiting for playing or
2793 * when we sync to the clock. */
2794 GST_LIVE_LOCK (basesrc);
2796 *playing = basesrc->live_running;
2797 basesrc->priv->flushing = flushing;
2799 /* if we are locked in the live lock, signal it to make it flush */
2800 basesrc->live_running = TRUE;
2802 /* clear pending EOS if any */
2803 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
2805 /* step 1, now that we have the LIVE lock, clear our unlock request */
2806 if (bclass->unlock_stop)
2807 bclass->unlock_stop (basesrc);
2809 /* step 2, unblock clock sync (if any) or any other blocking thing */
2810 if (basesrc->clock_id)
2811 gst_clock_id_unschedule (basesrc->clock_id);
2813 /* signal the live source that it can start playing */
2814 basesrc->live_running = live_play;
2816 /* When unlocking drop all delayed events */
2818 GST_OBJECT_LOCK (basesrc);
2819 if (basesrc->priv->pending_events) {
2820 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
2822 g_list_free (basesrc->priv->pending_events);
2823 basesrc->priv->pending_events = NULL;
2824 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
2826 GST_OBJECT_UNLOCK (basesrc);
2829 GST_LIVE_SIGNAL (basesrc);
2830 GST_LIVE_UNLOCK (basesrc);
2835 /* the purpose of this function is to make sure that a live source blocks in the
2836 * LIVE lock or leaves the LIVE lock and continues playing. */
2838 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2840 GstBaseSrcClass *bclass;
2842 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2844 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2846 GST_DEBUG_OBJECT (basesrc, "unlock");
2848 bclass->unlock (basesrc);
2851 /* we are now able to grab the LIVE lock, when we get it, we can be
2852 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2854 GST_LIVE_LOCK (basesrc);
2855 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2857 /* unblock clock sync (if any) */
2858 if (basesrc->clock_id)
2859 gst_clock_id_unschedule (basesrc->clock_id);
2861 /* configure what to do when we get to the LIVE lock. */
2862 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2863 basesrc->live_running = live_play;
2868 /* clear our unlock request when going to PLAYING */
2869 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2870 if (bclass->unlock_stop)
2871 bclass->unlock_stop (basesrc);
2873 /* for live sources we restart the timestamp correction */
2874 basesrc->priv->latency = -1;
2875 /* have to restart the task in case it stopped because of the unlock when
2876 * we went to PAUSED. Only do this if we operating in push mode. */
2877 GST_OBJECT_LOCK (basesrc->srcpad);
2878 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2879 GST_OBJECT_UNLOCK (basesrc->srcpad);
2881 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2883 GST_DEBUG_OBJECT (basesrc, "signal");
2884 GST_LIVE_SIGNAL (basesrc);
2886 GST_LIVE_UNLOCK (basesrc);
2892 gst_base_src_activate_push (GstPad * pad, gboolean active)
2894 GstBaseSrc *basesrc;
2897 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2899 /* prepare subclass first */
2901 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2903 if (G_UNLIKELY (!basesrc->can_activate_push))
2904 goto no_push_activation;
2906 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2909 basesrc->priv->last_sent_eos = FALSE;
2910 basesrc->priv->discont = TRUE;
2911 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2913 /* do initial seek, which will start the task */
2914 GST_OBJECT_LOCK (basesrc);
2915 event = basesrc->pending_seek;
2916 basesrc->pending_seek = NULL;
2917 GST_OBJECT_UNLOCK (basesrc);
2919 /* no need to unlock anything, the task is certainly
2920 * not running here. The perform seek code will start the task when
2922 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2926 gst_event_unref (event);
2928 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2930 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2932 gst_pad_stop_task (pad);
2933 /* now we can stop the source */
2934 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2942 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2947 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2952 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2954 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2956 gst_pad_stop_task (pad);
2957 /* Stop the basesrc */
2958 gst_base_src_stop (basesrc);
2960 gst_event_unref (event);
2965 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2971 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2973 GstBaseSrc *basesrc;
2975 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2977 /* prepare subclass first */
2979 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2980 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2983 /* if not random_access, we cannot operate in pull mode for now */
2984 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2987 /* stop flushing now but for live sources, still block in the LIVE lock when
2988 * we are not yet PLAYING */
2989 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2991 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2992 /* flush all, there is no task to stop */
2993 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2995 /* don't send EOS when going from PAUSED => READY when in pull mode */
2996 basesrc->priv->last_sent_eos = TRUE;
2998 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3006 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3011 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3012 gst_base_src_stop (basesrc);
3017 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3022 static GstStateChangeReturn
3023 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3025 GstBaseSrc *basesrc;
3026 GstStateChangeReturn result;
3027 gboolean no_preroll = FALSE;
3029 basesrc = GST_BASE_SRC (element);
3031 switch (transition) {
3032 case GST_STATE_CHANGE_NULL_TO_READY:
3034 case GST_STATE_CHANGE_READY_TO_PAUSED:
3035 no_preroll = gst_base_src_is_live (basesrc);
3037 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3038 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3039 if (gst_base_src_is_live (basesrc)) {
3040 /* now we can start playback */
3041 gst_base_src_set_playing (basesrc, TRUE);
3049 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3050 transition)) == GST_STATE_CHANGE_FAILURE)
3053 switch (transition) {
3054 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3055 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3056 if (gst_base_src_is_live (basesrc)) {
3057 /* make sure we block in the live lock in PAUSED */
3058 gst_base_src_set_playing (basesrc, FALSE);
3062 case GST_STATE_CHANGE_PAUSED_TO_READY:
3064 GstEvent **event_p, *event;
3066 /* we don't need to unblock anything here, the pad deactivation code
3067 * already did this */
3069 /* FIXME, deprecate this behaviour, it is very dangerous.
3070 * the prefered way of sending EOS downstream is by sending
3071 * the EOS event to the element */
3072 if (!basesrc->priv->last_sent_eos) {
3073 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
3074 event = gst_event_new_eos ();
3075 gst_event_set_seqnum (event, basesrc->priv->seqnum);
3076 gst_pad_push_event (basesrc->srcpad, event);
3077 basesrc->priv->last_sent_eos = TRUE;
3079 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3080 event_p = &basesrc->pending_seek;
3081 gst_event_replace (event_p, NULL);
3084 case GST_STATE_CHANGE_READY_TO_NULL:
3090 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3091 result = GST_STATE_CHANGE_NO_PREROLL;
3098 GST_DEBUG_OBJECT (basesrc, "parent failed state change");