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);
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)
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);
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));
813 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
815 GstBaseSrcClass *bclass;
818 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
819 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
822 bclass->fixate (bsrc, caps);
824 gst_object_unref (bsrc);
828 gst_base_src_default_query (GstBaseSrc * src, GstQuery ** query)
832 switch (GST_QUERY_TYPE (*query)) {
833 case GST_QUERY_POSITION:
837 gst_query_parse_position (*query, &format, NULL);
839 GST_DEBUG_OBJECT (src, "position query in format %s",
840 gst_format_get_name (format));
843 case GST_FORMAT_PERCENT:
849 GST_OBJECT_LOCK (src);
850 position = src->segment.position;
851 duration = src->segment.duration;
852 GST_OBJECT_UNLOCK (src);
854 if (position != -1 && duration != -1) {
855 if (position < duration)
856 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
859 percent = GST_FORMAT_PERCENT_MAX;
863 gst_query_set_position (*query, GST_FORMAT_PERCENT, percent);
870 GstFormat seg_format;
872 GST_OBJECT_LOCK (src);
874 gst_segment_to_stream_time (&src->segment, src->segment.format,
875 src->segment.position);
876 seg_format = src->segment.format;
877 GST_OBJECT_UNLOCK (src);
879 if (position != -1) {
880 /* convert to requested format */
882 gst_pad_query_convert (src->srcpad, seg_format,
883 position, &format, &position);
887 gst_query_set_position (*query, format, position);
893 case GST_QUERY_DURATION:
897 gst_query_parse_duration (*query, &format, NULL);
899 GST_DEBUG_OBJECT (src, "duration query in format %s",
900 gst_format_get_name (format));
903 case GST_FORMAT_PERCENT:
904 gst_query_set_duration (*query, GST_FORMAT_PERCENT,
905 GST_FORMAT_PERCENT_MAX);
911 GstFormat seg_format;
913 GST_OBJECT_LOCK (src);
914 /* this is the duration as configured by the subclass. */
915 duration = src->segment.duration;
916 seg_format = src->segment.format;
917 GST_OBJECT_UNLOCK (src);
919 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
920 duration, gst_format_get_name (seg_format));
922 if (duration != -1) {
923 /* convert to requested format, if this fails, we have a duration
924 * but we cannot answer the query, we must return FALSE. */
926 gst_pad_query_convert (src->srcpad, seg_format,
927 duration, &format, &duration);
929 /* The subclass did not configure a duration, we assume that the
930 * media has an unknown duration then and we return TRUE to report
931 * this. Note that this is not the same as returning FALSE, which
932 * means that we cannot report the duration at all. */
935 gst_query_set_duration (*query, format, duration);
942 case GST_QUERY_SEEKING:
944 GstFormat format, seg_format;
947 GST_OBJECT_LOCK (src);
948 duration = src->segment.duration;
949 seg_format = src->segment.format;
950 GST_OBJECT_UNLOCK (src);
952 gst_query_parse_seeking (*query, &format, NULL, NULL, NULL);
953 if (format == seg_format) {
954 gst_query_set_seeking (*query, seg_format,
955 gst_base_src_seekable (src), 0, duration);
958 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
959 /* Don't reply to the query to make up for demuxers which don't
960 * handle the SEEKING query yet. Players like Totem will fall back
961 * to the duration when the SEEKING query isn't answered. */
966 case GST_QUERY_SEGMENT:
970 GST_OBJECT_LOCK (src);
971 /* no end segment configured, current duration then */
972 if ((stop = src->segment.stop) == -1)
973 stop = src->segment.duration;
974 start = src->segment.start;
976 /* adjust to stream time */
977 if (src->segment.time != -1) {
978 start -= src->segment.time;
980 stop -= src->segment.time;
983 gst_query_set_segment (*query, src->segment.rate, src->segment.format,
985 GST_OBJECT_UNLOCK (src);
990 case GST_QUERY_FORMATS:
992 gst_query_set_formats (*query, 3, GST_FORMAT_DEFAULT,
993 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
997 case GST_QUERY_CONVERT:
999 GstFormat src_fmt, dest_fmt;
1000 gint64 src_val, dest_val;
1002 gst_query_parse_convert (*query, &src_fmt, &src_val, &dest_fmt,
1005 /* we can only convert between equal formats... */
1006 if (src_fmt == dest_fmt) {
1012 gst_query_set_convert (*query, src_fmt, src_val, dest_fmt, dest_val);
1015 case GST_QUERY_LATENCY:
1017 GstClockTime min, max;
1020 /* Subclasses should override and implement something usefull */
1021 res = gst_base_src_query_latency (src, &live, &min, &max);
1023 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1024 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1025 GST_TIME_ARGS (max));
1027 gst_query_set_latency (*query, live, min, max);
1030 case GST_QUERY_JITTER:
1031 case GST_QUERY_RATE:
1034 case GST_QUERY_BUFFERING:
1036 GstFormat format, seg_format;
1037 gint64 start, stop, estimated;
1039 gst_query_parse_buffering_range (*query, &format, NULL, NULL, NULL);
1041 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1042 gst_format_get_name (format));
1044 GST_OBJECT_LOCK (src);
1045 if (src->random_access) {
1048 if (format == GST_FORMAT_PERCENT)
1049 stop = GST_FORMAT_PERCENT_MAX;
1051 stop = src->segment.duration;
1057 seg_format = src->segment.format;
1058 GST_OBJECT_UNLOCK (src);
1060 /* convert to required format. When the conversion fails, we can't answer
1061 * the query. When the value is unknown, we can don't perform conversion
1062 * but report TRUE. */
1063 if (format != GST_FORMAT_PERCENT && stop != -1) {
1064 res = gst_pad_query_convert (src->srcpad, seg_format,
1065 stop, &format, &stop);
1069 if (res && format != GST_FORMAT_PERCENT && start != -1)
1070 res = gst_pad_query_convert (src->srcpad, seg_format,
1071 start, &format, &start);
1073 gst_query_set_buffering_range (*query, format, start, stop, estimated);
1080 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (*query),
1086 gst_base_src_query (GstPad * pad, GstQuery ** query)
1089 GstBaseSrcClass *bclass;
1090 gboolean result = FALSE;
1092 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1093 if (G_UNLIKELY (src == NULL))
1096 bclass = GST_BASE_SRC_GET_CLASS (src);
1099 result = bclass->query (src, query);
1101 result = gst_pad_query_default (pad, query);
1103 gst_object_unref (src);
1109 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1111 gboolean res = TRUE;
1113 /* update our offset if the start/stop position was updated */
1114 if (segment->format == GST_FORMAT_BYTES) {
1115 segment->time = segment->start;
1116 } else if (segment->start == 0) {
1117 /* seek to start, we can implement a default for this. */
1121 GST_INFO_OBJECT (src, "Can't do a default seek");
1128 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1130 GstBaseSrcClass *bclass;
1131 gboolean result = FALSE;
1133 bclass = GST_BASE_SRC_GET_CLASS (src);
1135 if (bclass->do_seek)
1136 result = bclass->do_seek (src, segment);
1141 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1144 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1145 GstSegment * segment)
1147 /* By default, we try one of 2 things:
1148 * - For absolute seek positions, convert the requested position to our
1149 * configured processing format and place it in the output segment \
1150 * - For relative seek positions, convert our current (input) values to the
1151 * seek format, adjust by the relative seek offset and then convert back to
1152 * the processing format
1154 GstSeekType cur_type, stop_type;
1157 GstFormat seek_format, dest_format;
1160 gboolean res = TRUE;
1162 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1163 &cur_type, &cur, &stop_type, &stop);
1164 dest_format = segment->format;
1166 if (seek_format == dest_format) {
1167 gst_segment_do_seek (segment, rate, seek_format, flags,
1168 cur_type, cur, stop_type, stop, &update);
1172 if (cur_type != GST_SEEK_TYPE_NONE) {
1173 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1175 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
1177 cur_type = GST_SEEK_TYPE_SET;
1180 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1181 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1183 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
1185 stop_type = GST_SEEK_TYPE_SET;
1188 /* And finally, configure our output segment in the desired format */
1189 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
1190 stop_type, stop, &update);
1199 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1205 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1206 GstSegment * seeksegment)
1208 GstBaseSrcClass *bclass;
1209 gboolean result = FALSE;
1211 bclass = GST_BASE_SRC_GET_CLASS (src);
1213 if (bclass->prepare_seek_segment)
1214 result = bclass->prepare_seek_segment (src, event, seeksegment);
1219 /* this code implements the seeking. It is a good example
1220 * handling all cases.
1222 * A seek updates the currently configured segment.start
1223 * and segment.stop values based on the SEEK_TYPE. If the
1224 * segment.start value is updated, a seek to this new position
1225 * should be performed.
1227 * The seek can only be executed when we are not currently
1228 * streaming any data, to make sure that this is the case, we
1229 * acquire the STREAM_LOCK which is taken when we are in the
1230 * _loop() function or when a getrange() is called. Normally
1231 * we will not receive a seek if we are operating in pull mode
1232 * though. When we operate as a live source we might block on the live
1233 * cond, which does not release the STREAM_LOCK. Therefore we will try
1234 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1235 * safe to perform the seek.
1237 * When we are in the loop() function, we might be in the middle
1238 * of pushing a buffer, which might block in a sink. To make sure
1239 * that the push gets unblocked we push out a FLUSH_START event.
1240 * Our loop function will get a WRONG_STATE return value from
1241 * the push and will pause, effectively releasing the STREAM_LOCK.
1243 * For a non-flushing seek, we pause the task, which might eventually
1244 * release the STREAM_LOCK. We say eventually because when the sink
1245 * blocks on the sample we might wait a very long time until the sink
1246 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1247 * can continue the seek. A non-flushing seek is normally done in a
1248 * running pipeline to perform seamless playback, this means that the sink is
1249 * PLAYING and will return from its chain function.
1250 * In the case of a non-flushing seek we need to make sure that the
1251 * data we output after the seek is continuous with the previous data,
1252 * this is because a non-flushing seek does not reset the running-time
1253 * to 0. We do this by closing the currently running segment, ie. sending
1254 * a new_segment event with the stop position set to the last processed
1257 * After updating the segment.start/stop values, we prepare for
1258 * streaming again. We push out a FLUSH_STOP to make the peer pad
1259 * accept data again and we start our task again.
1261 * A segment seek posts a message on the bus saying that the playback
1262 * of the segment started. We store the segment flag internally because
1263 * when we reach the segment.stop we have to post a segment.done
1264 * instead of EOS when doing a segment seek.
1266 /* FIXME (0.11), we have the unlock gboolean here because most current
1267 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1268 * the streaming thread isn't running, resulting in bogus unlocks later when it
1269 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1270 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1274 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1276 gboolean res = TRUE, tres;
1278 GstFormat seek_format, dest_format;
1280 GstSeekType cur_type, stop_type;
1282 gboolean flush, playing;
1284 gboolean relative_seek = FALSE;
1285 gboolean seekseg_configured = FALSE;
1286 GstSegment seeksegment;
1290 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1292 GST_OBJECT_LOCK (src);
1293 dest_format = src->segment.format;
1294 GST_OBJECT_UNLOCK (src);
1297 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1298 &cur_type, &cur, &stop_type, &stop);
1300 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1301 SEEK_TYPE_IS_RELATIVE (stop_type);
1303 if (dest_format != seek_format && !relative_seek) {
1304 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1305 * here before taking the stream lock, otherwise we must convert it later,
1306 * once we have the stream lock and can read the last configures segment
1307 * start and stop positions */
1308 gst_segment_init (&seeksegment, dest_format);
1310 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1311 goto prepare_failed;
1313 seekseg_configured = TRUE;
1316 flush = flags & GST_SEEK_FLAG_FLUSH;
1317 seqnum = gst_event_get_seqnum (event);
1320 /* get next seqnum */
1321 seqnum = gst_util_seqnum_next ();
1324 /* send flush start */
1326 tevent = gst_event_new_flush_start ();
1327 gst_event_set_seqnum (tevent, seqnum);
1328 gst_pad_push_event (src->srcpad, tevent);
1330 gst_pad_pause_task (src->srcpad);
1332 /* unblock streaming thread. */
1333 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1335 /* grab streaming lock, this should eventually be possible, either
1336 * because the task is paused, our streaming thread stopped
1337 * or because our peer is flushing. */
1338 GST_PAD_STREAM_LOCK (src->srcpad);
1339 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1340 /* we have seen this event before, issue a warning for now */
1341 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1344 src->priv->seqnum = seqnum;
1345 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1348 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1350 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1351 * copy the current segment info into the temp segment that we can actually
1352 * attempt the seek with. We only update the real segment if the seek suceeds. */
1353 if (!seekseg_configured) {
1354 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1356 /* now configure the final seek segment */
1358 if (seeksegment.format != seek_format) {
1359 /* OK, here's where we give the subclass a chance to convert the relative
1360 * seek into an absolute one in the processing format. We set up any
1361 * absolute seek above, before taking the stream lock. */
1362 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1363 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1368 /* The seek format matches our processing format, no need to ask the
1369 * the subclass to configure the segment. */
1370 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1371 cur_type, cur, stop_type, stop, &update);
1374 /* Else, no seek event passed, so we're just (re)starting the
1379 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1380 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1381 seeksegment.start, seeksegment.stop, seeksegment.position);
1383 /* do the seek, segment.position contains the new position. */
1384 res = gst_base_src_do_seek (src, &seeksegment);
1387 /* and prepare to continue streaming */
1389 tevent = gst_event_new_flush_stop ();
1390 gst_event_set_seqnum (tevent, seqnum);
1391 /* send flush stop, peer will accept data and events again. We
1392 * are not yet providing data as we still have the STREAM_LOCK. */
1393 gst_pad_push_event (src->srcpad, tevent);
1396 /* The subclass must have converted the segment to the processing format
1398 if (res && seeksegment.format != dest_format) {
1399 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1400 "in the correct format. Aborting seek.");
1404 /* if the seek was successful, we update our real segment and push
1405 * out the new segment. */
1407 GST_OBJECT_LOCK (src);
1408 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1409 GST_OBJECT_UNLOCK (src);
1411 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1412 GstMessage *message;
1414 message = gst_message_new_segment_start (GST_OBJECT (src),
1415 seeksegment.format, seeksegment.position);
1416 gst_message_set_seqnum (message, seqnum);
1418 gst_element_post_message (GST_ELEMENT (src), message);
1421 /* for deriving a stop position for the playback segment from the seek
1422 * segment, we must take the duration when the stop is not set */
1423 if ((stop = seeksegment.stop) == -1)
1424 stop = seeksegment.duration;
1426 src->priv->segment_pending = TRUE;
1429 src->priv->discont = TRUE;
1430 src->running = TRUE;
1431 /* and restart the task in case it got paused explicitly or by
1432 * the FLUSH_START event we pushed out. */
1433 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1438 /* and release the lock again so we can continue streaming */
1439 GST_PAD_STREAM_UNLOCK (src->srcpad);
1445 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1450 static const GstQueryType *
1451 gst_base_src_get_query_types (GstElement * element)
1453 static const GstQueryType query_types[] = {
1469 /* all events send to this element directly. This is mainly done from the
1473 gst_base_src_send_event (GstElement * element, GstEvent * event)
1476 gboolean result = FALSE;
1478 src = GST_BASE_SRC (element);
1480 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1482 switch (GST_EVENT_TYPE (event)) {
1483 /* bidirectional events */
1484 case GST_EVENT_FLUSH_START:
1485 case GST_EVENT_FLUSH_STOP:
1486 /* sending random flushes downstream can break stuff,
1487 * especially sync since all segment info will get flushed */
1490 /* downstream serialized events */
1493 GstBaseSrcClass *bclass;
1495 bclass = GST_BASE_SRC_GET_CLASS (src);
1497 /* queue EOS and make sure the task or pull function performs the EOS
1500 * We have two possibilities:
1502 * - Before we are to enter the _create function, we check the pending_eos
1503 * first and do EOS instead of entering it.
1504 * - If we are in the _create function or we did not manage to set the
1505 * flag fast enough and we are about to enter the _create function,
1506 * we unlock it so that we exit with WRONG_STATE immediatly. We then
1507 * check the EOS flag and do the EOS logic.
1509 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1510 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1512 /* unlock the _create function so that we can check the pending_eos flag
1513 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1514 * that we can grab it and stop the unlock again. We don't take the stream
1515 * lock so that this operation is guaranteed to never block. */
1517 bclass->unlock (src);
1519 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1521 GST_LIVE_LOCK (src);
1522 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1523 /* now stop the unlock of the streaming thread again. Grabbing the live
1524 * lock is enough because that protects the create function. */
1525 if (bclass->unlock_stop)
1526 bclass->unlock_stop (src);
1527 GST_LIVE_UNLOCK (src);
1532 case GST_EVENT_SEGMENT:
1533 /* sending random SEGMENT downstream can break sync. */
1536 case GST_EVENT_CUSTOM_DOWNSTREAM:
1537 case GST_EVENT_CUSTOM_BOTH:
1538 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1539 GST_OBJECT_LOCK (src);
1540 src->priv->pending_events =
1541 g_list_append (src->priv->pending_events, event);
1542 g_atomic_int_set (&src->priv->have_events, TRUE);
1543 GST_OBJECT_UNLOCK (src);
1547 case GST_EVENT_BUFFERSIZE:
1548 /* does not seem to make much sense currently */
1551 /* upstream events */
1553 /* elements should override send_event and do something */
1555 case GST_EVENT_SEEK:
1559 GST_OBJECT_LOCK (src->srcpad);
1560 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1562 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1563 GST_OBJECT_UNLOCK (src->srcpad);
1566 GST_DEBUG_OBJECT (src, "performing seek");
1567 /* when we are running in push mode, we can execute the
1568 * seek right now, we need to unlock. */
1569 result = gst_base_src_perform_seek (src, event, TRUE);
1573 /* else we store the event and execute the seek when we
1575 GST_OBJECT_LOCK (src);
1576 GST_DEBUG_OBJECT (src, "queueing seek");
1577 event_p = &src->pending_seek;
1578 gst_event_replace ((GstEvent **) event_p, event);
1579 GST_OBJECT_UNLOCK (src);
1580 /* assume the seek will work */
1585 case GST_EVENT_NAVIGATION:
1586 /* could make sense for elements that do something with navigation events
1587 * but then they would need to override the send_event function */
1589 case GST_EVENT_LATENCY:
1590 /* does not seem to make sense currently */
1594 case GST_EVENT_CUSTOM_UPSTREAM:
1595 /* override send_event if you want this */
1597 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1598 case GST_EVENT_CUSTOM_BOTH_OOB:
1599 /* insert a random custom event into the pipeline */
1600 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1601 result = gst_pad_push_event (src->srcpad, event);
1602 /* we gave away the ref to the event in the push */
1609 /* if we still have a ref to the event, unref it now */
1611 gst_event_unref (event);
1618 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1619 GST_OBJECT_UNLOCK (src->srcpad);
1626 gst_base_src_seekable (GstBaseSrc * src)
1628 GstBaseSrcClass *bclass;
1629 bclass = GST_BASE_SRC_GET_CLASS (src);
1630 if (bclass->is_seekable)
1631 return bclass->is_seekable (src);
1637 gst_base_src_update_qos (GstBaseSrc * src,
1638 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1640 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1641 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1642 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1644 GST_OBJECT_LOCK (src);
1645 src->priv->proportion = proportion;
1646 src->priv->earliest_time = timestamp + diff;
1647 GST_OBJECT_UNLOCK (src);
1652 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1656 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1658 switch (GST_EVENT_TYPE (event)) {
1659 case GST_EVENT_SEEK:
1660 /* is normally called when in push mode */
1661 if (!gst_base_src_seekable (src))
1664 result = gst_base_src_perform_seek (src, event, TRUE);
1666 case GST_EVENT_FLUSH_START:
1667 /* cancel any blocking getrange, is normally called
1668 * when in pull mode. */
1669 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1671 case GST_EVENT_FLUSH_STOP:
1672 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1677 GstClockTimeDiff diff;
1678 GstClockTime timestamp;
1680 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
1681 gst_base_src_update_qos (src, proportion, diff, timestamp);
1694 GST_DEBUG_OBJECT (src, "is not seekable");
1700 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1703 GstBaseSrcClass *bclass;
1704 gboolean result = FALSE;
1706 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1707 if (G_UNLIKELY (src == NULL)) {
1708 gst_event_unref (event);
1712 bclass = GST_BASE_SRC_GET_CLASS (src);
1714 if (bclass->event) {
1715 if (!(result = bclass->event (src, event)))
1716 goto subclass_failed;
1720 gst_event_unref (event);
1721 gst_object_unref (src);
1728 GST_DEBUG_OBJECT (src, "subclass refused event");
1734 gst_base_src_set_property (GObject * object, guint prop_id,
1735 const GValue * value, GParamSpec * pspec)
1739 src = GST_BASE_SRC (object);
1742 case PROP_BLOCKSIZE:
1743 gst_base_src_set_blocksize (src, g_value_get_ulong (value));
1745 case PROP_NUM_BUFFERS:
1746 src->num_buffers = g_value_get_int (value);
1749 src->typefind = g_value_get_boolean (value);
1751 case PROP_DO_TIMESTAMP:
1752 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1755 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1761 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1766 src = GST_BASE_SRC (object);
1769 case PROP_BLOCKSIZE:
1770 g_value_set_ulong (value, gst_base_src_get_blocksize (src));
1772 case PROP_NUM_BUFFERS:
1773 g_value_set_int (value, src->num_buffers);
1776 g_value_set_boolean (value, src->typefind);
1778 case PROP_DO_TIMESTAMP:
1779 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1782 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1787 /* with STREAM_LOCK and LOCK */
1788 static GstClockReturn
1789 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1794 id = gst_clock_new_single_shot_id (clock, time);
1796 basesrc->clock_id = id;
1797 /* release the live lock while waiting */
1798 GST_LIVE_UNLOCK (basesrc);
1800 ret = gst_clock_id_wait (id, NULL);
1802 GST_LIVE_LOCK (basesrc);
1803 gst_clock_id_unref (id);
1804 basesrc->clock_id = NULL;
1809 /* perform synchronisation on a buffer.
1812 static GstClockReturn
1813 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1815 GstClockReturn result;
1816 GstClockTime start, end;
1817 GstBaseSrcClass *bclass;
1818 GstClockTime base_time;
1820 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1821 gboolean do_timestamp, first, pseudo_live;
1823 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1826 if (bclass->get_times)
1827 bclass->get_times (basesrc, buffer, &start, &end);
1829 /* get buffer timestamp */
1830 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1832 /* grab the lock to prepare for clocking and calculate the startup
1834 GST_OBJECT_LOCK (basesrc);
1836 /* if we are asked to sync against the clock we are a pseudo live element */
1837 pseudo_live = (start != -1 && basesrc->is_live);
1838 /* check for the first buffer */
1839 first = (basesrc->priv->latency == -1);
1841 if (timestamp != -1 && pseudo_live) {
1842 GstClockTime latency;
1844 /* we have a timestamp and a sync time, latency is the diff */
1845 if (timestamp <= start)
1846 latency = start - timestamp;
1851 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1852 GST_TIME_ARGS (latency));
1853 /* first time we calculate latency, just configure */
1854 basesrc->priv->latency = latency;
1856 if (basesrc->priv->latency != latency) {
1857 /* we have a new latency, FIXME post latency message */
1858 basesrc->priv->latency = latency;
1859 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1860 GST_TIME_ARGS (latency));
1864 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1865 basesrc->is_live, start != -1);
1866 basesrc->priv->latency = 0;
1869 /* get clock, if no clock, we can't sync or do timestamps */
1870 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1873 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1875 do_timestamp = basesrc->priv->do_timestamp;
1877 /* first buffer, calculate the timestamp offset */
1879 GstClockTime running_time;
1881 now = gst_clock_get_time (clock);
1882 running_time = now - base_time;
1884 GST_LOG_OBJECT (basesrc,
1885 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1886 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1887 GST_TIME_ARGS (running_time));
1889 if (pseudo_live && timestamp != -1) {
1890 /* live source and we need to sync, add startup latency to all timestamps
1891 * to get the real running_time. Live sources should always timestamp
1892 * according to the current running time. */
1893 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1895 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1896 GST_TIME_ARGS (basesrc->priv->ts_offset));
1898 basesrc->priv->ts_offset = 0;
1899 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1902 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1904 timestamp = running_time;
1908 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1910 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1911 GST_TIME_ARGS (timestamp));
1914 /* add the timestamp offset we need for sync */
1915 timestamp += basesrc->priv->ts_offset;
1917 /* not the first buffer, the timestamp is the diff between the clock and
1919 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1920 now = gst_clock_get_time (clock);
1922 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1924 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1925 GST_TIME_ARGS (now - base_time));
1929 /* if we don't have a buffer timestamp, we don't sync */
1930 if (!GST_CLOCK_TIME_IS_VALID (start))
1933 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1934 /* for pseudo live sources, add our ts_offset to the timestamp */
1935 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1936 start += basesrc->priv->ts_offset;
1939 GST_LOG_OBJECT (basesrc,
1940 "waiting for clock, base time %" GST_TIME_FORMAT
1941 ", stream_start %" GST_TIME_FORMAT,
1942 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1943 GST_OBJECT_UNLOCK (basesrc);
1945 result = gst_base_src_wait (basesrc, clock, start + base_time);
1947 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1954 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1955 GST_OBJECT_UNLOCK (basesrc);
1956 return GST_CLOCK_OK;
1960 GST_DEBUG_OBJECT (basesrc, "no sync needed");
1961 GST_OBJECT_UNLOCK (basesrc);
1962 return GST_CLOCK_OK;
1966 /* Called with STREAM_LOCK and LIVE_LOCK */
1968 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1970 guint64 size, maxsize;
1971 GstBaseSrcClass *bclass;
1975 bclass = GST_BASE_SRC_GET_CLASS (src);
1977 format = src->segment.format;
1978 stop = src->segment.stop;
1979 /* get total file size */
1980 size = (guint64) src->segment.duration;
1982 /* only operate if we are working with bytes */
1983 if (format != GST_FORMAT_BYTES)
1986 /* the max amount of bytes to read is the total size or
1987 * up to the segment.stop if present. */
1989 maxsize = MIN (size, stop);
1993 GST_DEBUG_OBJECT (src,
1994 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1995 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1996 *length, size, stop, maxsize);
1998 /* check size if we have one */
1999 if (maxsize != -1) {
2000 /* if we run past the end, check if the file became bigger and
2002 if (G_UNLIKELY (offset + *length >= maxsize)) {
2003 /* see if length of the file changed */
2004 if (bclass->get_size)
2005 if (!bclass->get_size (src, &size))
2008 /* make sure we don't exceed the configured segment stop
2011 maxsize = MIN (size, stop);
2015 /* if we are at or past the end, EOS */
2016 if (G_UNLIKELY (offset >= maxsize))
2017 goto unexpected_length;
2019 /* else we can clip to the end */
2020 if (G_UNLIKELY (offset + *length >= maxsize))
2021 *length = maxsize - offset;
2026 /* keep track of current position and update duration.
2027 * segment is in bytes, we checked that above. */
2028 GST_OBJECT_LOCK (src);
2029 src->segment.duration = size;
2030 src->segment.position = offset;
2031 GST_OBJECT_UNLOCK (src);
2042 /* must be called with LIVE_LOCK */
2043 static GstFlowReturn
2044 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2048 GstBaseSrcClass *bclass;
2049 GstClockReturn status;
2051 bclass = GST_BASE_SRC_GET_CLASS (src);
2055 if (G_UNLIKELY (!src->live_running)) {
2056 ret = gst_base_src_wait_playing (src);
2057 if (ret != GST_FLOW_OK)
2062 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
2065 if (G_UNLIKELY (!bclass->create))
2068 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2069 goto unexpected_length;
2071 /* normally we don't count buffers */
2072 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2073 if (src->num_buffers_left == 0)
2074 goto reached_num_buffers;
2076 src->num_buffers_left--;
2079 /* don't enter the create function if a pending EOS event was set. For the
2080 * logic of the pending_eos, check the event function of this class. */
2081 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2084 GST_DEBUG_OBJECT (src,
2085 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2086 G_GINT64_FORMAT, offset, length, src->segment.time);
2088 ret = bclass->create (src, offset, length, buf);
2090 /* The create function could be unlocked because we have a pending EOS. It's
2091 * possible that we have a valid buffer from create that we need to
2092 * discard when the create function returned _OK. */
2093 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2094 if (ret == GST_FLOW_OK) {
2095 gst_buffer_unref (*buf);
2101 if (G_UNLIKELY (ret != GST_FLOW_OK))
2104 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2105 if (offset == 0 && src->segment.time == 0
2106 && GST_BUFFER_TIMESTAMP (*buf) == -1) {
2107 *buf = gst_buffer_make_writable (*buf);
2108 GST_BUFFER_TIMESTAMP (*buf) = 0;
2111 /* now sync before pushing the buffer */
2112 status = gst_base_src_do_sync (src, *buf);
2114 /* waiting for the clock could have made us flushing */
2115 if (G_UNLIKELY (src->priv->flushing))
2119 case GST_CLOCK_EARLY:
2120 /* the buffer is too late. We currently don't drop the buffer. */
2121 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2124 /* buffer synchronised properly */
2125 GST_DEBUG_OBJECT (src, "buffer ok");
2127 case GST_CLOCK_UNSCHEDULED:
2128 /* this case is triggered when we were waiting for the clock and
2129 * it got unlocked because we did a state change. In any case, get rid of
2131 gst_buffer_unref (*buf);
2133 if (!src->live_running) {
2134 /* We return WRONG_STATE when we are not running to stop the dataflow also
2135 * get rid of the produced buffer. */
2136 GST_DEBUG_OBJECT (src,
2137 "clock was unscheduled (%d), returning WRONG_STATE", status);
2138 ret = GST_FLOW_WRONG_STATE;
2140 /* If we are running when this happens, we quickly switched between
2141 * pause and playing. We try to produce a new buffer */
2142 GST_DEBUG_OBJECT (src,
2143 "clock was unscheduled (%d), but we are running", status);
2148 /* all other result values are unexpected and errors */
2149 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2150 (_("Internal clock error.")),
2151 ("clock returned unexpected return value %d", status));
2152 gst_buffer_unref (*buf);
2154 ret = GST_FLOW_ERROR;
2162 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2163 gst_flow_get_name (ret));
2168 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2169 gst_flow_get_name (ret));
2174 GST_DEBUG_OBJECT (src, "getrange but not started");
2175 return GST_FLOW_WRONG_STATE;
2179 GST_DEBUG_OBJECT (src, "no create function");
2180 return GST_FLOW_ERROR;
2184 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2185 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2186 return GST_FLOW_UNEXPECTED;
2188 reached_num_buffers:
2190 GST_DEBUG_OBJECT (src, "sent all buffers");
2191 return GST_FLOW_UNEXPECTED;
2195 GST_DEBUG_OBJECT (src, "we are flushing");
2196 gst_buffer_unref (*buf);
2198 return GST_FLOW_WRONG_STATE;
2202 GST_DEBUG_OBJECT (src, "we are EOS");
2203 return GST_FLOW_UNEXPECTED;
2207 static GstFlowReturn
2208 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
2214 src = GST_BASE_SRC_CAST (gst_object_ref (GST_OBJECT_PARENT (pad)));
2216 GST_LIVE_LOCK (src);
2217 if (G_UNLIKELY (src->priv->flushing))
2220 res = gst_base_src_get_range (src, offset, length, buf);
2223 GST_LIVE_UNLOCK (src);
2225 gst_object_unref (src);
2232 GST_DEBUG_OBJECT (src, "we are flushing");
2233 res = GST_FLOW_WRONG_STATE;
2239 gst_base_src_default_check_get_range (GstBaseSrc * src)
2243 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
2244 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2245 if (G_LIKELY (gst_base_src_start (src)))
2246 gst_base_src_stop (src);
2249 /* we can operate in getrange mode if the native format is bytes
2250 * and we are seekable, this condition is set in the random_access
2251 * flag and is set in the _start() method. */
2252 res = src->random_access;
2258 gst_base_src_check_get_range (GstBaseSrc * src)
2260 GstBaseSrcClass *bclass;
2263 bclass = GST_BASE_SRC_GET_CLASS (src);
2265 if (bclass->check_get_range == NULL)
2268 res = bclass->check_get_range (src);
2269 GST_LOG_OBJECT (src, "%s() returned %d",
2270 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
2277 GST_WARNING_OBJECT (src, "no check_get_range function set");
2283 gst_base_src_pad_check_get_range (GstPad * pad)
2288 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2290 res = gst_base_src_check_get_range (src);
2296 gst_base_src_loop (GstPad * pad)
2299 GstBuffer *buf = NULL;
2304 GList *pending_events = NULL, *tmp;
2305 gboolean reconfigure;
2309 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2311 GST_OBJECT_LOCK (pad);
2312 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
2313 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_NEED_RECONFIGURE);
2314 GST_OBJECT_UNLOCK (pad);
2315 /* check if we need to renegotiate */
2317 if (!gst_base_src_negotiate (src))
2318 GST_DEBUG_OBJECT (src, "Failed to renegotiate");
2321 GST_LIVE_LOCK (src);
2323 if (G_UNLIKELY (src->priv->flushing))
2326 src->priv->last_sent_eos = FALSE;
2328 blocksize = src->blocksize;
2330 /* if we operate in bytes, we can calculate an offset */
2331 if (src->segment.format == GST_FORMAT_BYTES) {
2332 position = src->segment.position;
2333 /* for negative rates, start with subtracting the blocksize */
2334 if (src->segment.rate < 0.0) {
2335 /* we cannot go below segment.start */
2336 if (position > src->segment.start + blocksize)
2337 position -= blocksize;
2339 /* last block, remainder up to segment.start */
2340 blocksize = position - src->segment.start;
2341 position = src->segment.start;
2347 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
2348 GST_TIME_ARGS (position), blocksize);
2350 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2351 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2352 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2353 gst_flow_get_name (ret));
2354 GST_LIVE_UNLOCK (src);
2357 /* this should not happen */
2358 if (G_UNLIKELY (buf == NULL))
2361 /* push events to close/start our segment before we push the buffer. */
2362 if (G_UNLIKELY (src->priv->segment_pending)) {
2363 gst_pad_push_event (pad, gst_event_new_segment (&src->segment));
2364 src->priv->segment_pending = FALSE;
2367 if (g_atomic_int_get (&src->priv->have_events)) {
2368 GST_OBJECT_LOCK (src);
2369 /* take the events */
2370 pending_events = src->priv->pending_events;
2371 src->priv->pending_events = NULL;
2372 g_atomic_int_set (&src->priv->have_events, FALSE);
2373 GST_OBJECT_UNLOCK (src);
2376 /* Push out pending events if any */
2377 if (G_UNLIKELY (pending_events != NULL)) {
2378 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2379 GstEvent *ev = (GstEvent *) tmp->data;
2380 gst_pad_push_event (pad, ev);
2382 g_list_free (pending_events);
2385 /* figure out the new position */
2386 switch (src->segment.format) {
2387 case GST_FORMAT_BYTES:
2389 guint bufsize = gst_buffer_get_size (buf);
2391 /* we subtracted above for negative rates */
2392 if (src->segment.rate >= 0.0)
2393 position += bufsize;
2396 case GST_FORMAT_TIME:
2398 GstClockTime start, duration;
2400 start = GST_BUFFER_TIMESTAMP (buf);
2401 duration = GST_BUFFER_DURATION (buf);
2403 if (GST_CLOCK_TIME_IS_VALID (start))
2406 position = src->segment.position;
2408 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2409 if (src->segment.rate >= 0.0)
2410 position += duration;
2411 else if (position > duration)
2412 position -= duration;
2418 case GST_FORMAT_DEFAULT:
2419 if (src->segment.rate >= 0.0)
2420 position = GST_BUFFER_OFFSET_END (buf);
2422 position = GST_BUFFER_OFFSET (buf);
2428 if (position != -1) {
2429 if (src->segment.rate >= 0.0) {
2430 /* positive rate, check if we reached the stop */
2431 if (src->segment.stop != -1) {
2432 if (position >= src->segment.stop) {
2434 position = src->segment.stop;
2438 /* negative rate, check if we reached the start. start is always set to
2439 * something different from -1 */
2440 if (position <= src->segment.start) {
2442 position = src->segment.start;
2444 /* when going reverse, all buffers are DISCONT */
2445 src->priv->discont = TRUE;
2447 GST_OBJECT_LOCK (src);
2448 src->segment.position = position;
2449 GST_OBJECT_UNLOCK (src);
2452 if (G_UNLIKELY (src->priv->discont)) {
2453 buf = gst_buffer_make_writable (buf);
2454 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2455 src->priv->discont = FALSE;
2457 GST_LIVE_UNLOCK (src);
2459 ret = gst_pad_push (pad, buf);
2460 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2461 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2462 gst_flow_get_name (ret));
2466 if (G_UNLIKELY (eos)) {
2467 GST_INFO_OBJECT (src, "pausing after end of segment");
2468 ret = GST_FLOW_UNEXPECTED;
2478 GST_DEBUG_OBJECT (src, "we are flushing");
2479 GST_LIVE_UNLOCK (src);
2480 ret = GST_FLOW_WRONG_STATE;
2485 const gchar *reason = gst_flow_get_name (ret);
2488 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2489 src->running = FALSE;
2490 gst_pad_pause_task (pad);
2491 if (ret == GST_FLOW_UNEXPECTED) {
2492 gboolean flag_segment;
2496 /* perform EOS logic */
2497 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2498 format = src->segment.format;
2499 position = src->segment.position;
2502 GstMessage *message;
2504 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2506 gst_message_set_seqnum (message, src->priv->seqnum);
2507 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2509 event = gst_event_new_eos ();
2510 gst_event_set_seqnum (event, src->priv->seqnum);
2511 gst_pad_push_event (pad, event);
2512 src->priv->last_sent_eos = TRUE;
2514 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_UNEXPECTED) {
2515 event = gst_event_new_eos ();
2516 gst_event_set_seqnum (event, src->priv->seqnum);
2517 /* for fatal errors we post an error message, post the error
2518 * first so the app knows about the error first.
2519 * Also don't do this for WRONG_STATE because it happens
2520 * due to flushing and posting an error message because of
2521 * that is the wrong thing to do, e.g. when we're doing
2522 * a flushing seek. */
2523 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2524 (_("Internal data flow error.")),
2525 ("streaming task paused, reason %s (%d)", reason, ret));
2526 gst_pad_push_event (pad, event);
2527 src->priv->last_sent_eos = TRUE;
2533 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2534 (_("Internal data flow error.")), ("element returned NULL buffer"));
2535 GST_LIVE_UNLOCK (src);
2540 /* default negotiation code.
2542 * Take intersection between src and sink pads, take first
2546 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2549 GstCaps *caps = NULL;
2550 GstCaps *peercaps = NULL;
2551 gboolean result = FALSE;
2553 /* first see what is possible on our source pad */
2554 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
2555 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2556 /* nothing or anything is allowed, we're done */
2557 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2558 goto no_nego_needed;
2560 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2563 /* get the peer caps */
2564 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
2565 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2567 /* get intersection */
2569 gst_caps_intersect_full (peercaps, thiscaps, GST_CAPS_INTERSECT_FIRST);
2570 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
2571 gst_caps_unref (peercaps);
2573 /* no peer, work with our own caps then */
2574 caps = gst_caps_copy (thiscaps);
2576 gst_caps_unref (thiscaps);
2578 /* take first (and best, since they are sorted) possibility */
2579 gst_caps_truncate (caps);
2582 if (!gst_caps_is_empty (caps)) {
2583 GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
2584 if (gst_caps_is_any (caps)) {
2585 /* hmm, still anything, so element can do anything and
2586 * nego is not needed */
2589 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2590 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2591 if (gst_caps_is_fixed (caps)) {
2592 /* yay, fixed caps, use those then, it's possible that the subclass does
2593 * not accept this caps after all and we have to fail. */
2594 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2598 gst_caps_unref (caps);
2600 GST_DEBUG_OBJECT (basesrc, "no common caps");
2606 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2608 gst_caps_unref (thiscaps);
2613 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2614 ("No supported formats found"),
2615 ("This element did not produce valid caps"));
2617 gst_caps_unref (thiscaps);
2623 gst_base_src_negotiate (GstBaseSrc * basesrc)
2625 GstBaseSrcClass *bclass;
2626 gboolean result = TRUE;
2628 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2630 if (bclass->negotiate)
2631 result = bclass->negotiate (basesrc);
2637 gst_base_src_start (GstBaseSrc * basesrc)
2639 GstBaseSrcClass *bclass;
2645 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2648 GST_DEBUG_OBJECT (basesrc, "starting source");
2650 basesrc->num_buffers_left = basesrc->num_buffers;
2652 GST_OBJECT_LOCK (basesrc);
2653 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2654 GST_OBJECT_UNLOCK (basesrc);
2656 basesrc->running = FALSE;
2657 basesrc->priv->segment_pending = FALSE;
2659 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2661 result = bclass->start (basesrc);
2666 goto could_not_start;
2668 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2670 format = basesrc->segment.format;
2672 /* figure out the size */
2673 if (format == GST_FORMAT_BYTES) {
2674 if (bclass->get_size) {
2675 if (!(result = bclass->get_size (basesrc, &size)))
2681 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2682 /* only update the size when operating in bytes, subclass is supposed
2683 * to set duration in the start method for other formats */
2684 GST_OBJECT_LOCK (basesrc);
2685 basesrc->segment.duration = size;
2686 GST_OBJECT_UNLOCK (basesrc);
2691 GST_DEBUG_OBJECT (basesrc,
2692 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2693 G_GINT64_FORMAT, gst_format_get_name (format), result, size,
2694 basesrc->segment.duration);
2696 seekable = gst_base_src_seekable (basesrc);
2697 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
2699 /* update for random access flag */
2700 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
2702 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2704 /* run typefind if we are random_access and the typefinding is enabled. */
2705 if (basesrc->random_access && basesrc->typefind && size != -1) {
2708 if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
2709 goto typefind_failed;
2711 result = gst_pad_set_caps (basesrc->srcpad, caps);
2712 gst_caps_unref (caps);
2714 /* use class or default negotiate function */
2715 if (!(result = gst_base_src_negotiate (basesrc)))
2716 goto could_not_negotiate;
2724 GST_DEBUG_OBJECT (basesrc, "could not start");
2725 /* subclass is supposed to post a message. We don't have to call _stop. */
2728 could_not_negotiate:
2730 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2731 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2732 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2733 /* we must call stop */
2734 gst_base_src_stop (basesrc);
2739 GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
2740 GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
2741 /* we must call stop */
2742 gst_base_src_stop (basesrc);
2748 gst_base_src_stop (GstBaseSrc * basesrc)
2750 GstBaseSrcClass *bclass;
2751 gboolean result = TRUE;
2753 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2756 GST_DEBUG_OBJECT (basesrc, "stopping source");
2758 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2760 result = bclass->stop (basesrc);
2763 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2768 /* start or stop flushing dataprocessing
2771 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2772 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2774 GstBaseSrcClass *bclass;
2776 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2778 if (flushing && unlock) {
2779 /* unlock any subclasses, we need to do this before grabbing the
2780 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2781 * unlock to the params because of backwards compat (see seek handler)*/
2783 bclass->unlock (basesrc);
2786 /* the live lock is released when we are blocked, waiting for playing or
2787 * when we sync to the clock. */
2788 GST_LIVE_LOCK (basesrc);
2790 *playing = basesrc->live_running;
2791 basesrc->priv->flushing = flushing;
2793 /* if we are locked in the live lock, signal it to make it flush */
2794 basesrc->live_running = TRUE;
2796 /* clear pending EOS if any */
2797 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
2799 /* step 1, now that we have the LIVE lock, clear our unlock request */
2800 if (bclass->unlock_stop)
2801 bclass->unlock_stop (basesrc);
2803 /* step 2, unblock clock sync (if any) or any other blocking thing */
2804 if (basesrc->clock_id)
2805 gst_clock_id_unschedule (basesrc->clock_id);
2807 /* signal the live source that it can start playing */
2808 basesrc->live_running = live_play;
2810 /* When unlocking drop all delayed events */
2812 GST_OBJECT_LOCK (basesrc);
2813 if (basesrc->priv->pending_events) {
2814 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
2816 g_list_free (basesrc->priv->pending_events);
2817 basesrc->priv->pending_events = NULL;
2818 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
2820 GST_OBJECT_UNLOCK (basesrc);
2823 GST_LIVE_SIGNAL (basesrc);
2824 GST_LIVE_UNLOCK (basesrc);
2829 /* the purpose of this function is to make sure that a live source blocks in the
2830 * LIVE lock or leaves the LIVE lock and continues playing. */
2832 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2834 GstBaseSrcClass *bclass;
2836 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2838 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2840 GST_DEBUG_OBJECT (basesrc, "unlock");
2842 bclass->unlock (basesrc);
2845 /* we are now able to grab the LIVE lock, when we get it, we can be
2846 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2848 GST_LIVE_LOCK (basesrc);
2849 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2851 /* unblock clock sync (if any) */
2852 if (basesrc->clock_id)
2853 gst_clock_id_unschedule (basesrc->clock_id);
2855 /* configure what to do when we get to the LIVE lock. */
2856 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2857 basesrc->live_running = live_play;
2862 /* clear our unlock request when going to PLAYING */
2863 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2864 if (bclass->unlock_stop)
2865 bclass->unlock_stop (basesrc);
2867 /* for live sources we restart the timestamp correction */
2868 basesrc->priv->latency = -1;
2869 /* have to restart the task in case it stopped because of the unlock when
2870 * we went to PAUSED. Only do this if we operating in push mode. */
2871 GST_OBJECT_LOCK (basesrc->srcpad);
2872 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2873 GST_OBJECT_UNLOCK (basesrc->srcpad);
2875 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2877 GST_DEBUG_OBJECT (basesrc, "signal");
2878 GST_LIVE_SIGNAL (basesrc);
2880 GST_LIVE_UNLOCK (basesrc);
2886 gst_base_src_activate_push (GstPad * pad, gboolean active)
2888 GstBaseSrc *basesrc;
2891 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2893 /* prepare subclass first */
2895 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2897 if (G_UNLIKELY (!basesrc->can_activate_push))
2898 goto no_push_activation;
2900 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2903 basesrc->priv->last_sent_eos = FALSE;
2904 basesrc->priv->discont = TRUE;
2905 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2907 /* do initial seek, which will start the task */
2908 GST_OBJECT_LOCK (basesrc);
2909 event = basesrc->pending_seek;
2910 basesrc->pending_seek = NULL;
2911 GST_OBJECT_UNLOCK (basesrc);
2913 /* no need to unlock anything, the task is certainly
2914 * not running here. The perform seek code will start the task when
2916 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2920 gst_event_unref (event);
2922 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2924 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2926 gst_pad_stop_task (pad);
2927 /* now we can stop the source */
2928 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2936 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2941 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2946 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2948 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2950 gst_pad_stop_task (pad);
2951 /* Stop the basesrc */
2952 gst_base_src_stop (basesrc);
2954 gst_event_unref (event);
2959 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2965 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2967 GstBaseSrc *basesrc;
2969 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2971 /* prepare subclass first */
2973 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2974 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2977 /* if not random_access, we cannot operate in pull mode for now */
2978 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2981 /* stop flushing now but for live sources, still block in the LIVE lock when
2982 * we are not yet PLAYING */
2983 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2985 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2986 /* flush all, there is no task to stop */
2987 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2989 /* don't send EOS when going from PAUSED => READY when in pull mode */
2990 basesrc->priv->last_sent_eos = TRUE;
2992 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3000 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3005 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3006 gst_base_src_stop (basesrc);
3011 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3016 static GstStateChangeReturn
3017 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3019 GstBaseSrc *basesrc;
3020 GstStateChangeReturn result;
3021 gboolean no_preroll = FALSE;
3023 basesrc = GST_BASE_SRC (element);
3025 switch (transition) {
3026 case GST_STATE_CHANGE_NULL_TO_READY:
3028 case GST_STATE_CHANGE_READY_TO_PAUSED:
3029 no_preroll = gst_base_src_is_live (basesrc);
3031 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3032 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3033 if (gst_base_src_is_live (basesrc)) {
3034 /* now we can start playback */
3035 gst_base_src_set_playing (basesrc, TRUE);
3043 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3044 transition)) == GST_STATE_CHANGE_FAILURE)
3047 switch (transition) {
3048 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3049 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3050 if (gst_base_src_is_live (basesrc)) {
3051 /* make sure we block in the live lock in PAUSED */
3052 gst_base_src_set_playing (basesrc, FALSE);
3056 case GST_STATE_CHANGE_PAUSED_TO_READY:
3058 GstEvent **event_p, *event;
3060 /* we don't need to unblock anything here, the pad deactivation code
3061 * already did this */
3063 /* FIXME, deprecate this behaviour, it is very dangerous.
3064 * the prefered way of sending EOS downstream is by sending
3065 * the EOS event to the element */
3066 if (!basesrc->priv->last_sent_eos) {
3067 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
3068 event = gst_event_new_eos ();
3069 gst_event_set_seqnum (event, basesrc->priv->seqnum);
3070 gst_pad_push_event (basesrc->srcpad, event);
3071 basesrc->priv->last_sent_eos = TRUE;
3073 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3074 event_p = &basesrc->pending_seek;
3075 gst_event_replace (event_p, NULL);
3078 case GST_STATE_CHANGE_READY_TO_NULL:
3084 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3085 result = GST_STATE_CHANGE_NO_PREROLL;
3092 GST_DEBUG_OBJECT (basesrc, "parent failed state change");