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 * #GstBaseSrc:Class.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 /* two segments to be sent in the streaming thread with STREAM_LOCK */
221 GstEvent *close_segment;
222 GstEvent *start_segment;
223 gboolean newsegment_pending;
225 /* if EOS is pending (atomic) */
228 /* startup latency is the time it takes between going to PLAYING and producing
229 * the first BUFFER with running_time 0. This value is included in the latency
231 GstClockTime latency;
232 /* timestamp offset, this is the offset add to the values of gst_times for
233 * pseudo live sources */
234 GstClockTimeDiff ts_offset;
236 gboolean do_timestamp;
238 /* stream sequence number */
241 /* pending tags to be pushed in the data stream */
244 /* QoS *//* with LOCK */
245 gboolean qos_enabled;
247 GstClockTime earliest_time;
250 static GstElementClass *parent_class = NULL;
252 static void gst_base_src_base_init (gpointer g_class);
253 static void gst_base_src_class_init (GstBaseSrcClass * klass);
254 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
255 static void gst_base_src_finalize (GObject * object);
259 gst_base_src_get_type (void)
261 static volatile gsize base_src_type = 0;
263 if (g_once_init_enter (&base_src_type)) {
265 static const GTypeInfo base_src_info = {
266 sizeof (GstBaseSrcClass),
267 (GBaseInitFunc) gst_base_src_base_init,
269 (GClassInitFunc) gst_base_src_class_init,
274 (GInstanceInitFunc) gst_base_src_init,
277 _type = g_type_register_static (GST_TYPE_ELEMENT,
278 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
279 g_once_init_leave (&base_src_type, _type);
281 return base_src_type;
284 static GstCaps *gst_base_src_getcaps (GstPad * pad);
285 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
286 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
288 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
289 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
290 static void gst_base_src_set_property (GObject * object, guint prop_id,
291 const GValue * value, GParamSpec * pspec);
292 static void gst_base_src_get_property (GObject * object, guint prop_id,
293 GValue * value, GParamSpec * pspec);
294 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
295 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
296 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
297 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
299 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
301 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
302 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
303 GstSegment * segment);
304 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
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);
326 gst_base_src_base_init (gpointer g_class)
328 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
332 gst_base_src_class_init (GstBaseSrcClass * klass)
334 GObjectClass *gobject_class;
335 GstElementClass *gstelement_class;
337 gobject_class = G_OBJECT_CLASS (klass);
338 gstelement_class = GST_ELEMENT_CLASS (klass);
340 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
342 parent_class = g_type_class_peek_parent (klass);
344 gobject_class->finalize = gst_base_src_finalize;
345 gobject_class->set_property = gst_base_src_set_property;
346 gobject_class->get_property = gst_base_src_get_property;
348 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
349 g_param_spec_ulong ("blocksize", "Block size",
350 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXULONG,
351 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
353 g_param_spec_int ("num-buffers", "num-buffers",
354 "Number of buffers to output before sending EOS (-1 = unlimited)",
355 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
356 G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
358 g_param_spec_boolean ("typefind", "Typefind",
359 "Run typefind before negotiating", DEFAULT_TYPEFIND,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
362 g_param_spec_boolean ("do-timestamp", "Do timestamp",
363 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 gstelement_class->change_state =
367 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
368 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
369 gstelement_class->get_query_types =
370 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
372 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
373 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
374 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
375 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
376 klass->check_get_range =
377 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
378 klass->prepare_seek_segment =
379 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
381 /* Registering debug symbols for function pointers */
382 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_push);
383 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_pull);
384 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event_handler);
385 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
386 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_get_range);
387 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_check_get_range);
388 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getcaps);
389 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_setcaps);
390 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
394 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
397 GstPadTemplate *pad_template;
399 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
401 basesrc->is_live = FALSE;
402 basesrc->live_lock = g_mutex_new ();
403 basesrc->live_cond = g_cond_new ();
404 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
405 basesrc->num_buffers_left = -1;
407 basesrc->can_activate_push = TRUE;
408 basesrc->pad_mode = GST_ACTIVATE_NONE;
411 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
412 g_return_if_fail (pad_template != NULL);
414 GST_DEBUG_OBJECT (basesrc, "creating src pad");
415 pad = gst_pad_new_from_template (pad_template, "src");
417 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
418 gst_pad_set_activatepush_function (pad, gst_base_src_activate_push);
419 gst_pad_set_activatepull_function (pad, gst_base_src_activate_pull);
420 gst_pad_set_event_function (pad, gst_base_src_event_handler);
421 gst_pad_set_query_function (pad, gst_base_src_query);
422 gst_pad_set_checkgetrange_function (pad, gst_base_src_pad_check_get_range);
423 gst_pad_set_getrange_function (pad, gst_base_src_pad_get_range);
424 gst_pad_set_getcaps_function (pad, gst_base_src_getcaps);
425 gst_pad_set_setcaps_function (pad, gst_base_src_setcaps);
426 gst_pad_set_fixatecaps_function (pad, gst_base_src_fixate);
428 /* hold pointer to pad */
429 basesrc->srcpad = pad;
430 GST_DEBUG_OBJECT (basesrc, "adding src pad");
431 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
433 basesrc->blocksize = DEFAULT_BLOCKSIZE;
434 basesrc->clock_id = NULL;
435 /* we operate in BYTES by default */
436 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
437 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
438 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
440 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
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->data.ABI.pending_seek;
457 gst_event_replace (event_p, NULL);
459 if (basesrc->priv->pending_tags) {
460 g_list_foreach (basesrc->priv->pending_tags, (GFunc) gst_event_unref, NULL);
461 g_list_free (basesrc->priv->pending_tags);
464 G_OBJECT_CLASS (parent_class)->finalize (object);
468 * gst_base_src_wait_playing:
471 * If the #GstBaseSrcClass.create() method performs its own synchronisation
472 * against the clock it must unblock when going from PLAYING to the PAUSED state
473 * and call this method before continuing to produce the remaining data.
475 * This function will block until a state change to PLAYING happens (in which
476 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
477 * to a state change to READY or a FLUSH event (in which case this function
478 * returns #GST_FLOW_WRONG_STATE).
482 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
483 * continue. Any other return value should be returned from the create vmethod.
486 gst_base_src_wait_playing (GstBaseSrc * src)
488 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
490 /* block until the state changes, or we get a flush, or something */
491 GST_DEBUG_OBJECT (src, "live source waiting for running state");
493 if (src->priv->flushing)
495 GST_DEBUG_OBJECT (src, "live source unlocked");
502 GST_DEBUG_OBJECT (src, "we are flushing");
503 return GST_FLOW_WRONG_STATE;
508 * gst_base_src_set_live:
509 * @src: base source instance
510 * @live: new live-mode
512 * If the element listens to a live source, @live should
515 * A live source will not produce data in the PAUSED state and
516 * will therefore not be able to participate in the PREROLL phase
517 * of a pipeline. To signal this fact to the application and the
518 * pipeline, the state change return value of the live source will
519 * be GST_STATE_CHANGE_NO_PREROLL.
522 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
524 g_return_if_fail (GST_IS_BASE_SRC (src));
526 GST_OBJECT_LOCK (src);
528 GST_OBJECT_UNLOCK (src);
532 * gst_base_src_is_live:
533 * @src: base source instance
535 * Check if an element is in live mode.
537 * Returns: %TRUE if element is in live mode.
540 gst_base_src_is_live (GstBaseSrc * src)
544 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
546 GST_OBJECT_LOCK (src);
547 result = src->is_live;
548 GST_OBJECT_UNLOCK (src);
554 * gst_base_src_set_format:
555 * @src: base source instance
556 * @format: the format to use
558 * Sets the default format of the source. This will be the format used
559 * for sending NEW_SEGMENT events and for performing seeks.
561 * If a format of GST_FORMAT_BYTES is set, the element will be able to
562 * operate in pull mode if the #GstBaseSrc.is_seekable() returns TRUE.
564 * This function must only be called in states < %GST_STATE_PAUSED.
569 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
571 g_return_if_fail (GST_IS_BASE_SRC (src));
572 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
574 GST_OBJECT_LOCK (src);
575 gst_segment_init (&src->segment, format);
576 GST_OBJECT_UNLOCK (src);
580 * gst_base_src_query_latency:
582 * @live: if the source is live
583 * @min_latency: the min latency of the source
584 * @max_latency: the max latency of the source
586 * Query the source for the latency parameters. @live will be TRUE when @src is
587 * configured as a live source. @min_latency will be set to the difference
588 * between the running time and the timestamp of the first buffer.
589 * @max_latency is always the undefined value of -1.
591 * This function is mostly used by subclasses.
593 * Returns: TRUE if the query succeeded.
598 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
599 GstClockTime * min_latency, GstClockTime * max_latency)
603 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
605 GST_OBJECT_LOCK (src);
607 *live = src->is_live;
609 /* if we have a startup latency, report this one, else report 0. Subclasses
610 * are supposed to override the query function if they want something
612 if (src->priv->latency != -1)
613 min = src->priv->latency;
622 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
623 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
625 GST_OBJECT_UNLOCK (src);
631 * gst_base_src_set_blocksize:
633 * @blocksize: the new blocksize in bytes
635 * Set the number of bytes that @src will push out with each buffer. When
636 * @blocksize is set to -1, a default length will be used.
641 gst_base_src_set_blocksize (GstBaseSrc * src, gulong blocksize)
643 g_return_if_fail (GST_IS_BASE_SRC (src));
645 GST_OBJECT_LOCK (src);
646 src->blocksize = blocksize;
647 GST_OBJECT_UNLOCK (src);
651 * gst_base_src_get_blocksize:
654 * Get the number of bytes that @src will push out with each buffer.
656 * Returns: the number of bytes pushed with each buffer.
661 gst_base_src_get_blocksize (GstBaseSrc * src)
665 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
667 GST_OBJECT_LOCK (src);
668 res = src->blocksize;
669 GST_OBJECT_UNLOCK (src);
676 * gst_base_src_set_do_timestamp:
678 * @timestamp: enable or disable timestamping
680 * Configure @src to automatically timestamp outgoing buffers based on the
681 * current running_time of the pipeline. This property is mostly useful for live
687 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
689 g_return_if_fail (GST_IS_BASE_SRC (src));
691 GST_OBJECT_LOCK (src);
692 src->priv->do_timestamp = timestamp;
693 GST_OBJECT_UNLOCK (src);
697 * gst_base_src_get_do_timestamp:
700 * Query if @src timestamps outgoing buffers based on the current running_time.
702 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
707 gst_base_src_get_do_timestamp (GstBaseSrc * src)
711 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
713 GST_OBJECT_LOCK (src);
714 res = src->priv->do_timestamp;
715 GST_OBJECT_UNLOCK (src);
721 * gst_base_src_new_seamless_segment:
723 * @start: The new start value for the segment
724 * @stop: Stop value for the new segment
725 * @position: The position value for the new segent
727 * Prepare a new seamless segment for emission downstream. This function must
728 * only be called by derived sub-classes, and only from the create() function,
729 * as the stream-lock needs to be held.
731 * The format for the new segment will be the current format of the source, as
732 * configured with gst_base_src_set_format()
734 * Returns: %TRUE if preparation of the seamless segment succeeded.
739 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
744 GST_DEBUG_OBJECT (src,
745 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
746 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
747 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
749 GST_OBJECT_LOCK (src);
750 if (src->data.ABI.running && !src->priv->newsegment_pending) {
751 if (src->priv->close_segment)
752 gst_event_unref (src->priv->close_segment);
753 src->priv->close_segment =
754 gst_event_new_new_segment_full (TRUE,
755 src->segment.rate, src->segment.applied_rate, src->segment.format,
756 src->segment.start, src->segment.last_stop, src->segment.time);
759 gst_segment_set_newsegment_full (&src->segment, FALSE, src->segment.rate,
760 src->segment.applied_rate, src->segment.format, start, stop, position);
762 if (src->priv->start_segment)
763 gst_event_unref (src->priv->start_segment);
764 if (src->segment.rate >= 0.0) {
765 /* forward, we send data from last_stop to stop */
766 src->priv->start_segment =
767 gst_event_new_new_segment_full (FALSE,
768 src->segment.rate, src->segment.applied_rate, src->segment.format,
769 src->segment.last_stop, stop, src->segment.time);
771 /* reverse, we send data from last_stop to start */
772 src->priv->start_segment =
773 gst_event_new_new_segment_full (FALSE,
774 src->segment.rate, src->segment.applied_rate, src->segment.format,
775 src->segment.start, src->segment.last_stop, src->segment.time);
777 GST_OBJECT_UNLOCK (src);
779 src->priv->discont = TRUE;
780 src->data.ABI.running = TRUE;
786 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
788 GstBaseSrcClass *bclass;
792 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
793 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
795 if (bclass->set_caps)
796 res = bclass->set_caps (bsrc, caps);
802 gst_base_src_getcaps (GstPad * pad)
804 GstBaseSrcClass *bclass;
806 GstCaps *caps = NULL;
808 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
809 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
810 if (bclass->get_caps)
811 caps = bclass->get_caps (bsrc);
814 GstPadTemplate *pad_template;
817 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
818 if (pad_template != NULL) {
819 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
826 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
828 GstBaseSrcClass *bclass;
831 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
832 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
835 bclass->fixate (bsrc, caps);
837 gst_object_unref (bsrc);
841 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
845 switch (GST_QUERY_TYPE (query)) {
846 case GST_QUERY_POSITION:
850 gst_query_parse_position (query, &format, NULL);
852 GST_DEBUG_OBJECT (src, "position query in format %s",
853 gst_format_get_name (format));
856 case GST_FORMAT_PERCENT:
862 GST_OBJECT_LOCK (src);
863 position = src->segment.last_stop;
864 duration = src->segment.duration;
865 GST_OBJECT_UNLOCK (src);
867 if (position != -1 && duration != -1) {
868 if (position < duration)
869 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
872 percent = GST_FORMAT_PERCENT_MAX;
876 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
883 GstFormat seg_format;
885 GST_OBJECT_LOCK (src);
886 position = src->segment.last_stop;
887 seg_format = src->segment.format;
888 GST_OBJECT_UNLOCK (src);
890 if (position != -1) {
891 /* convert to requested format */
893 gst_pad_query_convert (src->srcpad, seg_format,
894 position, &format, &position);
898 gst_query_set_position (query, format, position);
904 case GST_QUERY_DURATION:
908 gst_query_parse_duration (query, &format, NULL);
910 GST_DEBUG_OBJECT (src, "duration query in format %s",
911 gst_format_get_name (format));
914 case GST_FORMAT_PERCENT:
915 gst_query_set_duration (query, GST_FORMAT_PERCENT,
916 GST_FORMAT_PERCENT_MAX);
922 GstFormat seg_format;
924 GST_OBJECT_LOCK (src);
925 /* this is the duration as configured by the subclass. */
926 duration = src->segment.duration;
927 seg_format = src->segment.format;
928 GST_OBJECT_UNLOCK (src);
930 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
931 duration, gst_format_get_name (seg_format));
933 if (duration != -1) {
934 /* convert to requested format, if this fails, we have a duration
935 * but we cannot answer the query, we must return FALSE. */
937 gst_pad_query_convert (src->srcpad, seg_format,
938 duration, &format, &duration);
940 /* The subclass did not configure a duration, we assume that the
941 * media has an unknown duration then and we return TRUE to report
942 * this. Note that this is not the same as returning FALSE, which
943 * means that we cannot report the duration at all. */
946 gst_query_set_duration (query, format, duration);
953 case GST_QUERY_SEEKING:
955 GstFormat format, seg_format;
958 GST_OBJECT_LOCK (src);
959 duration = src->segment.duration;
960 seg_format = src->segment.format;
961 GST_OBJECT_UNLOCK (src);
963 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
964 if (format == seg_format) {
965 gst_query_set_seeking (query, seg_format,
966 gst_base_src_seekable (src), 0, duration);
969 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
970 /* Don't reply to the query to make up for demuxers which don't
971 * handle the SEEKING query yet. Players like Totem will fall back
972 * to the duration when the SEEKING query isn't answered. */
977 case GST_QUERY_SEGMENT:
981 GST_OBJECT_LOCK (src);
982 /* no end segment configured, current duration then */
983 if ((stop = src->segment.stop) == -1)
984 stop = src->segment.duration;
985 start = src->segment.start;
987 /* adjust to stream time */
988 if (src->segment.time != -1) {
989 start -= src->segment.time;
991 stop -= src->segment.time;
993 gst_query_set_segment (query, src->segment.rate, src->segment.format,
995 GST_OBJECT_UNLOCK (src);
1000 case GST_QUERY_FORMATS:
1002 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1003 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1007 case GST_QUERY_CONVERT:
1009 GstFormat src_fmt, dest_fmt;
1010 gint64 src_val, dest_val;
1012 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
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));
1103 bclass = GST_BASE_SRC_GET_CLASS (src);
1106 result = bclass->query (src, query);
1108 result = gst_pad_query_default (pad, query);
1110 gst_object_unref (src);
1116 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1118 gboolean res = TRUE;
1120 /* update our offset if the start/stop position was updated */
1121 if (segment->format == GST_FORMAT_BYTES) {
1122 segment->time = segment->start;
1123 } else if (segment->start == 0) {
1124 /* seek to start, we can implement a default for this. */
1128 GST_INFO_OBJECT (src, "Can't do a default seek");
1135 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1137 GstBaseSrcClass *bclass;
1138 gboolean result = FALSE;
1140 bclass = GST_BASE_SRC_GET_CLASS (src);
1142 if (bclass->do_seek)
1143 result = bclass->do_seek (src, segment);
1148 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1151 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1152 GstSegment * segment)
1154 /* By default, we try one of 2 things:
1155 * - For absolute seek positions, convert the requested position to our
1156 * configured processing format and place it in the output segment \
1157 * - For relative seek positions, convert our current (input) values to the
1158 * seek format, adjust by the relative seek offset and then convert back to
1159 * the processing format
1161 GstSeekType cur_type, stop_type;
1164 GstFormat seek_format, dest_format;
1167 gboolean res = TRUE;
1169 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1170 &cur_type, &cur, &stop_type, &stop);
1171 dest_format = segment->format;
1173 if (seek_format == dest_format) {
1174 gst_segment_set_seek (segment, rate, seek_format, flags,
1175 cur_type, cur, stop_type, stop, &update);
1179 if (cur_type != GST_SEEK_TYPE_NONE) {
1180 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1182 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
1184 cur_type = GST_SEEK_TYPE_SET;
1187 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1188 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1190 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
1192 stop_type = GST_SEEK_TYPE_SET;
1195 /* And finally, configure our output segment in the desired format */
1196 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
1197 stop_type, stop, &update);
1206 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1212 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1213 GstSegment * seeksegment)
1215 GstBaseSrcClass *bclass;
1216 gboolean result = FALSE;
1218 bclass = GST_BASE_SRC_GET_CLASS (src);
1220 if (bclass->prepare_seek_segment)
1221 result = bclass->prepare_seek_segment (src, event, seeksegment);
1226 /* this code implements the seeking. It is a good example
1227 * handling all cases.
1229 * A seek updates the currently configured segment.start
1230 * and segment.stop values based on the SEEK_TYPE. If the
1231 * segment.start value is updated, a seek to this new position
1232 * should be performed.
1234 * The seek can only be executed when we are not currently
1235 * streaming any data, to make sure that this is the case, we
1236 * acquire the STREAM_LOCK which is taken when we are in the
1237 * _loop() function or when a getrange() is called. Normally
1238 * we will not receive a seek if we are operating in pull mode
1239 * though. When we operate as a live source we might block on the live
1240 * cond, which does not release the STREAM_LOCK. Therefore we will try
1241 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1242 * safe to perform the seek.
1244 * When we are in the loop() function, we might be in the middle
1245 * of pushing a buffer, which might block in a sink. To make sure
1246 * that the push gets unblocked we push out a FLUSH_START event.
1247 * Our loop function will get a WRONG_STATE return value from
1248 * the push and will pause, effectively releasing the STREAM_LOCK.
1250 * For a non-flushing seek, we pause the task, which might eventually
1251 * release the STREAM_LOCK. We say eventually because when the sink
1252 * blocks on the sample we might wait a very long time until the sink
1253 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1254 * can continue the seek. A non-flushing seek is normally done in a
1255 * running pipeline to perform seamless playback, this means that the sink is
1256 * PLAYING and will return from its chain function.
1257 * In the case of a non-flushing seek we need to make sure that the
1258 * data we output after the seek is continuous with the previous data,
1259 * this is because a non-flushing seek does not reset the running-time
1260 * to 0. We do this by closing the currently running segment, ie. sending
1261 * a new_segment event with the stop position set to the last processed
1264 * After updating the segment.start/stop values, we prepare for
1265 * streaming again. We push out a FLUSH_STOP to make the peer pad
1266 * accept data again and we start our task again.
1268 * A segment seek posts a message on the bus saying that the playback
1269 * of the segment started. We store the segment flag internally because
1270 * when we reach the segment.stop we have to post a segment.done
1271 * instead of EOS when doing a segment seek.
1273 /* FIXME (0.11), we have the unlock gboolean here because most current
1274 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1275 * the streaming thread isn't running, resulting in bogus unlocks later when it
1276 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1277 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1281 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1283 gboolean res = TRUE, tres;
1285 GstFormat seek_format, dest_format;
1287 GstSeekType cur_type, stop_type;
1289 gboolean flush, playing;
1291 gboolean relative_seek = FALSE;
1292 gboolean seekseg_configured = FALSE;
1293 GstSegment seeksegment;
1297 GST_DEBUG_OBJECT (src, "doing seek");
1299 GST_OBJECT_LOCK (src);
1300 dest_format = src->segment.format;
1301 GST_OBJECT_UNLOCK (src);
1304 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1305 &cur_type, &cur, &stop_type, &stop);
1307 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1308 SEEK_TYPE_IS_RELATIVE (stop_type);
1310 if (dest_format != seek_format && !relative_seek) {
1311 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1312 * here before taking the stream lock, otherwise we must convert it later,
1313 * once we have the stream lock and can read the last configures segment
1314 * start and stop positions */
1315 gst_segment_init (&seeksegment, dest_format);
1317 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1318 goto prepare_failed;
1320 seekseg_configured = TRUE;
1323 flush = flags & GST_SEEK_FLAG_FLUSH;
1324 seqnum = gst_event_get_seqnum (event);
1327 /* get next seqnum */
1328 seqnum = gst_util_seqnum_next ();
1331 /* send flush start */
1333 tevent = gst_event_new_flush_start ();
1334 gst_event_set_seqnum (tevent, seqnum);
1335 gst_pad_push_event (src->srcpad, tevent);
1337 gst_pad_pause_task (src->srcpad);
1339 /* unblock streaming thread. */
1340 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1342 /* grab streaming lock, this should eventually be possible, either
1343 * because the task is paused, our streaming thread stopped
1344 * or because our peer is flushing. */
1345 GST_PAD_STREAM_LOCK (src->srcpad);
1346 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1347 /* we have seen this event before, issue a warning for now */
1348 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1351 src->priv->seqnum = seqnum;
1352 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1355 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1357 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1358 * copy the current segment info into the temp segment that we can actually
1359 * attempt the seek with. We only update the real segment if the seek suceeds. */
1360 if (!seekseg_configured) {
1361 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1363 /* now configure the final seek segment */
1365 if (seeksegment.format != seek_format) {
1366 /* OK, here's where we give the subclass a chance to convert the relative
1367 * seek into an absolute one in the processing format. We set up any
1368 * absolute seek above, before taking the stream lock. */
1369 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1370 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1375 /* The seek format matches our processing format, no need to ask the
1376 * the subclass to configure the segment. */
1377 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1378 cur_type, cur, stop_type, stop, &update);
1381 /* Else, no seek event passed, so we're just (re)starting the
1386 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1387 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1388 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1390 /* do the seek, segment.last_stop contains the new position. */
1391 res = gst_base_src_do_seek (src, &seeksegment);
1394 /* and prepare to continue streaming */
1396 tevent = gst_event_new_flush_stop ();
1397 gst_event_set_seqnum (tevent, seqnum);
1398 /* send flush stop, peer will accept data and events again. We
1399 * are not yet providing data as we still have the STREAM_LOCK. */
1400 gst_pad_push_event (src->srcpad, tevent);
1401 } else if (res && src->data.ABI.running) {
1402 /* we are running the current segment and doing a non-flushing seek,
1403 * close the segment first based on the last_stop. */
1404 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1405 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1407 /* queue the segment for sending in the stream thread */
1408 if (src->priv->close_segment)
1409 gst_event_unref (src->priv->close_segment);
1410 src->priv->close_segment =
1411 gst_event_new_new_segment_full (TRUE,
1412 src->segment.rate, src->segment.applied_rate, src->segment.format,
1413 src->segment.start, src->segment.last_stop, src->segment.time);
1414 gst_event_set_seqnum (src->priv->close_segment, seqnum);
1417 /* The subclass must have converted the segment to the processing format
1419 if (res && seeksegment.format != dest_format) {
1420 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1421 "in the correct format. Aborting seek.");
1425 /* if the seek was successful, we update our real segment and push
1426 * out the new segment. */
1428 GST_OBJECT_LOCK (src);
1429 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1430 GST_OBJECT_UNLOCK (src);
1432 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1433 GstMessage *message;
1435 message = gst_message_new_segment_start (GST_OBJECT (src),
1436 seeksegment.format, seeksegment.last_stop);
1437 gst_message_set_seqnum (message, seqnum);
1439 gst_element_post_message (GST_ELEMENT (src), message);
1442 /* for deriving a stop position for the playback segment from the seek
1443 * segment, we must take the duration when the stop is not set */
1444 if ((stop = seeksegment.stop) == -1)
1445 stop = seeksegment.duration;
1447 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1448 " to %" G_GINT64_FORMAT, seeksegment.start, stop);
1450 /* now replace the old segment so that we send it in the stream thread the
1451 * next time it is scheduled. */
1452 if (src->priv->start_segment)
1453 gst_event_unref (src->priv->start_segment);
1454 if (seeksegment.rate >= 0.0) {
1455 /* forward, we send data from last_stop to stop */
1456 src->priv->start_segment =
1457 gst_event_new_new_segment_full (FALSE,
1458 seeksegment.rate, seeksegment.applied_rate, seeksegment.format,
1459 seeksegment.last_stop, stop, seeksegment.time);
1461 /* reverse, we send data from last_stop to start */
1462 src->priv->start_segment =
1463 gst_event_new_new_segment_full (FALSE,
1464 seeksegment.rate, seeksegment.applied_rate, seeksegment.format,
1465 seeksegment.start, seeksegment.last_stop, seeksegment.time);
1467 gst_event_set_seqnum (src->priv->start_segment, seqnum);
1468 src->priv->newsegment_pending = TRUE;
1471 src->priv->discont = TRUE;
1472 src->data.ABI.running = TRUE;
1473 /* and restart the task in case it got paused explicitly or by
1474 * the FLUSH_START event we pushed out. */
1475 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1480 /* and release the lock again so we can continue streaming */
1481 GST_PAD_STREAM_UNLOCK (src->srcpad);
1487 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1492 static const GstQueryType *
1493 gst_base_src_get_query_types (GstElement * element)
1495 static const GstQueryType query_types[] = {
1511 /* all events send to this element directly. This is mainly done from the
1515 gst_base_src_send_event (GstElement * element, GstEvent * event)
1518 gboolean result = FALSE;
1520 src = GST_BASE_SRC (element);
1522 GST_DEBUG_OBJECT (src, "reveived %s event", GST_EVENT_TYPE_NAME (event));
1524 switch (GST_EVENT_TYPE (event)) {
1525 /* bidirectional events */
1526 case GST_EVENT_FLUSH_START:
1527 case GST_EVENT_FLUSH_STOP:
1528 /* sending random flushes downstream can break stuff,
1529 * especially sync since all segment info will get flushed */
1532 /* downstream serialized events */
1535 GstBaseSrcClass *bclass;
1537 bclass = GST_BASE_SRC_GET_CLASS (src);
1539 /* queue EOS and make sure the task or pull function performs the EOS
1542 * We have two possibilities:
1544 * - Before we are to enter the _create function, we check the pending_eos
1545 * first and do EOS instead of entering it.
1546 * - If we are in the _create function or we did not manage to set the
1547 * flag fast enough and we are about to enter the _create function,
1548 * we unlock it so that we exit with WRONG_STATE immediatly. We then
1549 * check the EOS flag and do the EOS logic.
1551 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1552 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1554 /* unlock the _create function so that we can check the pending_eos flag
1555 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1556 * that we can grab it and stop the unlock again. We don't take the stream
1557 * lock so that this operation is guaranteed to never block. */
1559 bclass->unlock (src);
1561 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1563 GST_LIVE_LOCK (src);
1564 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1565 /* now stop the unlock of the streaming thread again. Grabbing the live
1566 * lock is enough because that protects the create function. */
1567 if (bclass->unlock_stop)
1568 bclass->unlock_stop (src);
1569 GST_LIVE_UNLOCK (src);
1574 case GST_EVENT_NEWSEGMENT:
1575 /* sending random NEWSEGMENT downstream can break sync. */
1578 /* Insert tag in the dataflow */
1579 GST_OBJECT_LOCK (src);
1580 src->priv->pending_tags = g_list_append (src->priv->pending_tags, event);
1581 GST_OBJECT_UNLOCK (src);
1585 case GST_EVENT_BUFFERSIZE:
1586 /* does not seem to make much sense currently */
1589 /* upstream events */
1591 /* elements should override send_event and do something */
1593 case GST_EVENT_SEEK:
1597 GST_OBJECT_LOCK (src->srcpad);
1598 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1600 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1601 GST_OBJECT_UNLOCK (src->srcpad);
1604 GST_DEBUG_OBJECT (src, "performing seek");
1605 /* when we are running in push mode, we can execute the
1606 * seek right now, we need to unlock. */
1607 result = gst_base_src_perform_seek (src, event, TRUE);
1611 /* else we store the event and execute the seek when we
1613 GST_OBJECT_LOCK (src);
1614 GST_DEBUG_OBJECT (src, "queueing seek");
1615 event_p = &src->data.ABI.pending_seek;
1616 gst_event_replace ((GstEvent **) event_p, event);
1617 GST_OBJECT_UNLOCK (src);
1618 /* assume the seek will work */
1623 case GST_EVENT_NAVIGATION:
1624 /* could make sense for elements that do something with navigation events
1625 * but then they would need to override the send_event function */
1627 case GST_EVENT_LATENCY:
1628 /* does not seem to make sense currently */
1632 case GST_EVENT_CUSTOM_UPSTREAM:
1633 /* override send_event if you want this */
1635 case GST_EVENT_CUSTOM_DOWNSTREAM:
1636 case GST_EVENT_CUSTOM_BOTH:
1637 /* FIXME, insert event in the dataflow */
1639 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1640 case GST_EVENT_CUSTOM_BOTH_OOB:
1641 /* insert a random custom event into the pipeline */
1642 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1643 result = gst_pad_push_event (src->srcpad, event);
1644 /* we gave away the ref to the event in the push */
1651 /* if we still have a ref to the event, unref it now */
1653 gst_event_unref (event);
1660 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1661 GST_OBJECT_UNLOCK (src->srcpad);
1668 gst_base_src_seekable (GstBaseSrc * src)
1670 GstBaseSrcClass *bclass;
1671 bclass = GST_BASE_SRC_GET_CLASS (src);
1672 if (bclass->is_seekable)
1673 return bclass->is_seekable (src);
1679 gst_base_src_update_qos (GstBaseSrc * src,
1680 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1682 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1683 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1684 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1686 GST_OBJECT_LOCK (src);
1687 src->priv->proportion = proportion;
1688 src->priv->earliest_time = timestamp + diff;
1689 GST_OBJECT_UNLOCK (src);
1694 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1698 switch (GST_EVENT_TYPE (event)) {
1699 case GST_EVENT_SEEK:
1700 /* is normally called when in push mode */
1701 if (!gst_base_src_seekable (src))
1704 result = gst_base_src_perform_seek (src, event, TRUE);
1706 case GST_EVENT_FLUSH_START:
1707 /* cancel any blocking getrange, is normally called
1708 * when in pull mode. */
1709 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1711 case GST_EVENT_FLUSH_STOP:
1712 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1717 GstClockTimeDiff diff;
1718 GstClockTime timestamp;
1720 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1721 gst_base_src_update_qos (src, proportion, diff, timestamp);
1734 GST_DEBUG_OBJECT (src, "is not seekable");
1740 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1743 GstBaseSrcClass *bclass;
1744 gboolean result = FALSE;
1746 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1747 bclass = GST_BASE_SRC_GET_CLASS (src);
1749 if (bclass->event) {
1750 if (!(result = bclass->event (src, event)))
1751 goto subclass_failed;
1755 gst_event_unref (event);
1756 gst_object_unref (src);
1763 GST_DEBUG_OBJECT (src, "subclass refused event");
1769 gst_base_src_set_property (GObject * object, guint prop_id,
1770 const GValue * value, GParamSpec * pspec)
1774 src = GST_BASE_SRC (object);
1777 case PROP_BLOCKSIZE:
1778 gst_base_src_set_blocksize (src, g_value_get_ulong (value));
1780 case PROP_NUM_BUFFERS:
1781 src->num_buffers = g_value_get_int (value);
1784 src->data.ABI.typefind = g_value_get_boolean (value);
1786 case PROP_DO_TIMESTAMP:
1787 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1790 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1796 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1801 src = GST_BASE_SRC (object);
1804 case PROP_BLOCKSIZE:
1805 g_value_set_ulong (value, gst_base_src_get_blocksize (src));
1807 case PROP_NUM_BUFFERS:
1808 g_value_set_int (value, src->num_buffers);
1811 g_value_set_boolean (value, src->data.ABI.typefind);
1813 case PROP_DO_TIMESTAMP:
1814 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1817 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1822 /* with STREAM_LOCK and LOCK */
1823 static GstClockReturn
1824 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1829 id = gst_clock_new_single_shot_id (clock, time);
1831 basesrc->clock_id = id;
1832 /* release the live lock while waiting */
1833 GST_LIVE_UNLOCK (basesrc);
1835 ret = gst_clock_id_wait (id, NULL);
1837 GST_LIVE_LOCK (basesrc);
1838 gst_clock_id_unref (id);
1839 basesrc->clock_id = NULL;
1844 /* perform synchronisation on a buffer.
1847 static GstClockReturn
1848 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1850 GstClockReturn result;
1851 GstClockTime start, end;
1852 GstBaseSrcClass *bclass;
1853 GstClockTime base_time;
1855 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1856 gboolean do_timestamp, first, pseudo_live;
1858 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1861 if (bclass->get_times)
1862 bclass->get_times (basesrc, buffer, &start, &end);
1864 /* get buffer timestamp */
1865 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1867 /* grab the lock to prepare for clocking and calculate the startup
1869 GST_OBJECT_LOCK (basesrc);
1871 /* if we are asked to sync against the clock we are a pseudo live element */
1872 pseudo_live = (start != -1 && basesrc->is_live);
1873 /* check for the first buffer */
1874 first = (basesrc->priv->latency == -1);
1876 if (timestamp != -1 && pseudo_live) {
1877 GstClockTime latency;
1879 /* we have a timestamp and a sync time, latency is the diff */
1880 if (timestamp <= start)
1881 latency = start - timestamp;
1886 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1887 GST_TIME_ARGS (latency));
1888 /* first time we calculate latency, just configure */
1889 basesrc->priv->latency = latency;
1891 if (basesrc->priv->latency != latency) {
1892 /* we have a new latency, FIXME post latency message */
1893 basesrc->priv->latency = latency;
1894 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1895 GST_TIME_ARGS (latency));
1899 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1900 basesrc->is_live, start != -1);
1901 basesrc->priv->latency = 0;
1904 /* get clock, if no clock, we can't sync or do timestamps */
1905 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1908 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1910 do_timestamp = basesrc->priv->do_timestamp;
1912 /* first buffer, calculate the timestamp offset */
1914 GstClockTime running_time;
1916 now = gst_clock_get_time (clock);
1917 running_time = now - base_time;
1919 GST_LOG_OBJECT (basesrc,
1920 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1921 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1922 GST_TIME_ARGS (running_time));
1924 if (pseudo_live && timestamp != -1) {
1925 /* live source and we need to sync, add startup latency to all timestamps
1926 * to get the real running_time. Live sources should always timestamp
1927 * according to the current running time. */
1928 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1930 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1931 GST_TIME_ARGS (basesrc->priv->ts_offset));
1933 basesrc->priv->ts_offset = 0;
1934 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1937 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1939 timestamp = running_time;
1943 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1945 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1946 GST_TIME_ARGS (timestamp));
1949 /* add the timestamp offset we need for sync */
1950 timestamp += basesrc->priv->ts_offset;
1952 /* not the first buffer, the timestamp is the diff between the clock and
1954 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1955 now = gst_clock_get_time (clock);
1957 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1959 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1960 GST_TIME_ARGS (now - base_time));
1964 /* if we don't have a buffer timestamp, we don't sync */
1965 if (!GST_CLOCK_TIME_IS_VALID (start))
1968 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1969 /* for pseudo live sources, add our ts_offset to the timestamp */
1970 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1971 start += basesrc->priv->ts_offset;
1974 GST_LOG_OBJECT (basesrc,
1975 "waiting for clock, base time %" GST_TIME_FORMAT
1976 ", stream_start %" GST_TIME_FORMAT,
1977 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1978 GST_OBJECT_UNLOCK (basesrc);
1980 result = gst_base_src_wait (basesrc, clock, start + base_time);
1982 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1989 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1990 GST_OBJECT_UNLOCK (basesrc);
1991 return GST_CLOCK_OK;
1995 GST_DEBUG_OBJECT (basesrc, "no sync needed");
1996 GST_OBJECT_UNLOCK (basesrc);
1997 return GST_CLOCK_OK;
2001 /* Called with STREAM_LOCK and LIVE_LOCK */
2003 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
2005 guint64 size, maxsize;
2006 GstBaseSrcClass *bclass;
2010 bclass = GST_BASE_SRC_GET_CLASS (src);
2012 format = src->segment.format;
2013 stop = src->segment.stop;
2014 /* get total file size */
2015 size = (guint64) src->segment.duration;
2017 /* only operate if we are working with bytes */
2018 if (format != GST_FORMAT_BYTES)
2021 /* the max amount of bytes to read is the total size or
2022 * up to the segment.stop if present. */
2024 maxsize = MIN (size, stop);
2028 GST_DEBUG_OBJECT (src,
2029 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2030 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2031 *length, size, stop, maxsize);
2033 /* check size if we have one */
2034 if (maxsize != -1) {
2035 /* if we run past the end, check if the file became bigger and
2037 if (G_UNLIKELY (offset + *length >= maxsize)) {
2038 /* see if length of the file changed */
2039 if (bclass->get_size)
2040 if (!bclass->get_size (src, &size))
2043 /* make sure we don't exceed the configured segment stop
2046 maxsize = MIN (size, stop);
2050 /* if we are at or past the end, EOS */
2051 if (G_UNLIKELY (offset >= maxsize))
2052 goto unexpected_length;
2054 /* else we can clip to the end */
2055 if (G_UNLIKELY (offset + *length >= maxsize))
2056 *length = maxsize - offset;
2061 /* keep track of current position and update duration.
2062 * segment is in bytes, we checked that above. */
2063 GST_OBJECT_LOCK (src);
2064 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
2065 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
2066 GST_OBJECT_UNLOCK (src);
2077 /* must be called with LIVE_LOCK */
2078 static GstFlowReturn
2079 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2083 GstBaseSrcClass *bclass;
2084 GstClockReturn status;
2086 bclass = GST_BASE_SRC_GET_CLASS (src);
2090 while (G_UNLIKELY (!src->live_running)) {
2091 ret = gst_base_src_wait_playing (src);
2092 if (ret != GST_FLOW_OK)
2097 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
2100 if (G_UNLIKELY (!bclass->create))
2103 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2104 goto unexpected_length;
2106 /* normally we don't count buffers */
2107 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2108 if (src->num_buffers_left == 0)
2109 goto reached_num_buffers;
2111 src->num_buffers_left--;
2114 /* don't enter the create function if a pending EOS event was set. For the
2115 * logic of the pending_eos, check the event function of this class. */
2116 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2119 GST_DEBUG_OBJECT (src,
2120 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2121 G_GINT64_FORMAT, offset, length, src->segment.time);
2123 ret = bclass->create (src, offset, length, buf);
2125 /* The create function could be unlocked because we have a pending EOS. It's
2126 * possible that we have a valid buffer from create that we need to
2127 * discard when the create function returned _OK. */
2128 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2129 if (ret == GST_FLOW_OK) {
2130 gst_buffer_unref (*buf);
2136 if (G_UNLIKELY (ret != GST_FLOW_OK))
2139 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2140 if (offset == 0 && src->segment.time == 0
2141 && GST_BUFFER_TIMESTAMP (*buf) == -1)
2142 GST_BUFFER_TIMESTAMP (*buf) = 0;
2144 /* set pad caps on the buffer if the buffer had no caps */
2145 if (GST_BUFFER_CAPS (*buf) == NULL)
2146 gst_buffer_set_caps (*buf, GST_PAD_CAPS (src->srcpad));
2148 /* now sync before pushing the buffer */
2149 status = gst_base_src_do_sync (src, *buf);
2151 /* waiting for the clock could have made us flushing */
2152 if (G_UNLIKELY (src->priv->flushing))
2156 case GST_CLOCK_EARLY:
2157 /* the buffer is too late. We currently don't drop the buffer. */
2158 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2161 /* buffer synchronised properly */
2162 GST_DEBUG_OBJECT (src, "buffer ok");
2164 case GST_CLOCK_UNSCHEDULED:
2165 /* this case is triggered when we were waiting for the clock and
2166 * it got unlocked because we did a state change. In any case, get rid of
2168 gst_buffer_unref (*buf);
2170 if (!src->live_running) {
2171 /* We return WRONG_STATE when we are not running to stop the dataflow also
2172 * get rid of the produced buffer. */
2173 GST_DEBUG_OBJECT (src,
2174 "clock was unscheduled (%d), returning WRONG_STATE", status);
2175 ret = GST_FLOW_WRONG_STATE;
2177 /* If we are running when this happens, we quickly switched between
2178 * pause and playing. We try to produce a new buffer */
2179 GST_DEBUG_OBJECT (src,
2180 "clock was unscheduled (%d), but we are running", status);
2185 /* all other result values are unexpected and errors */
2186 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2187 (_("Internal clock error.")),
2188 ("clock returned unexpected return value %d", status));
2189 gst_buffer_unref (*buf);
2191 ret = GST_FLOW_ERROR;
2199 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2200 gst_flow_get_name (ret));
2205 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2206 gst_flow_get_name (ret));
2211 GST_DEBUG_OBJECT (src, "getrange but not started");
2212 return GST_FLOW_WRONG_STATE;
2216 GST_DEBUG_OBJECT (src, "no create function");
2217 return GST_FLOW_ERROR;
2221 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2222 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2223 return GST_FLOW_UNEXPECTED;
2225 reached_num_buffers:
2227 GST_DEBUG_OBJECT (src, "sent all buffers");
2228 return GST_FLOW_UNEXPECTED;
2232 GST_DEBUG_OBJECT (src, "we are flushing");
2233 gst_buffer_unref (*buf);
2235 return GST_FLOW_WRONG_STATE;
2239 GST_DEBUG_OBJECT (src, "we are EOS");
2240 return GST_FLOW_UNEXPECTED;
2244 static GstFlowReturn
2245 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
2251 src = GST_BASE_SRC_CAST (gst_object_ref (GST_OBJECT_PARENT (pad)));
2253 GST_LIVE_LOCK (src);
2254 if (G_UNLIKELY (src->priv->flushing))
2257 res = gst_base_src_get_range (src, offset, length, buf);
2260 GST_LIVE_UNLOCK (src);
2262 gst_object_unref (src);
2269 GST_DEBUG_OBJECT (src, "we are flushing");
2270 res = GST_FLOW_WRONG_STATE;
2276 gst_base_src_default_check_get_range (GstBaseSrc * src)
2280 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
2281 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2282 if (G_LIKELY (gst_base_src_start (src)))
2283 gst_base_src_stop (src);
2286 /* we can operate in getrange mode if the native format is bytes
2287 * and we are seekable, this condition is set in the random_access
2288 * flag and is set in the _start() method. */
2289 res = src->random_access;
2295 gst_base_src_check_get_range (GstBaseSrc * src)
2297 GstBaseSrcClass *bclass;
2300 bclass = GST_BASE_SRC_GET_CLASS (src);
2302 if (bclass->check_get_range == NULL)
2305 res = bclass->check_get_range (src);
2306 GST_LOG_OBJECT (src, "%s() returned %d",
2307 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
2314 GST_WARNING_OBJECT (src, "no check_get_range function set");
2320 gst_base_src_pad_check_get_range (GstPad * pad)
2325 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2327 res = gst_base_src_check_get_range (src);
2333 gst_base_src_loop (GstPad * pad)
2336 GstBuffer *buf = NULL;
2345 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2347 GST_LIVE_LOCK (src);
2349 if (G_UNLIKELY (src->priv->flushing))
2352 src->priv->last_sent_eos = FALSE;
2354 blocksize = src->blocksize;
2356 /* if we operate in bytes, we can calculate an offset */
2357 if (src->segment.format == GST_FORMAT_BYTES) {
2358 position = src->segment.last_stop;
2359 /* for negative rates, start with subtracting the blocksize */
2360 if (src->segment.rate < 0.0) {
2361 /* we cannot go below segment.start */
2362 if (position > src->segment.start + blocksize)
2363 position -= blocksize;
2365 /* last block, remainder up to segment.start */
2366 blocksize = position - src->segment.start;
2367 position = src->segment.start;
2373 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
2374 GST_TIME_ARGS (position), blocksize);
2376 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2377 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2378 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2379 gst_flow_get_name (ret));
2380 GST_LIVE_UNLOCK (src);
2383 /* this should not happen */
2384 if (G_UNLIKELY (buf == NULL))
2387 /* push events to close/start our segment before we push the buffer. */
2388 if (G_UNLIKELY (src->priv->close_segment)) {
2389 gst_pad_push_event (pad, src->priv->close_segment);
2390 src->priv->close_segment = NULL;
2392 if (G_UNLIKELY (src->priv->start_segment)) {
2393 gst_pad_push_event (pad, src->priv->start_segment);
2394 src->priv->start_segment = NULL;
2396 src->priv->newsegment_pending = FALSE;
2398 GST_OBJECT_LOCK (src);
2400 tags = src->priv->pending_tags;
2401 src->priv->pending_tags = NULL;
2402 GST_OBJECT_UNLOCK (src);
2404 /* Push out pending tags if any */
2405 if (G_UNLIKELY (tags != NULL)) {
2406 for (tmp = tags; tmp; tmp = g_list_next (tmp)) {
2407 GstEvent *ev = (GstEvent *) tmp->data;
2408 gst_pad_push_event (pad, ev);
2413 /* figure out the new position */
2414 switch (src->segment.format) {
2415 case GST_FORMAT_BYTES:
2417 guint bufsize = GST_BUFFER_SIZE (buf);
2419 /* we subtracted above for negative rates */
2420 if (src->segment.rate >= 0.0)
2421 position += bufsize;
2424 case GST_FORMAT_TIME:
2426 GstClockTime start, duration;
2428 start = GST_BUFFER_TIMESTAMP (buf);
2429 duration = GST_BUFFER_DURATION (buf);
2431 if (GST_CLOCK_TIME_IS_VALID (start))
2434 position = src->segment.last_stop;
2436 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2437 if (src->segment.rate >= 0.0)
2438 position += duration;
2439 else if (position > duration)
2440 position -= duration;
2446 case GST_FORMAT_DEFAULT:
2447 if (src->segment.rate >= 0.0)
2448 position = GST_BUFFER_OFFSET_END (buf);
2450 position = GST_BUFFER_OFFSET (buf);
2456 if (position != -1) {
2457 if (src->segment.rate >= 0.0) {
2458 /* positive rate, check if we reached the stop */
2459 if (src->segment.stop != -1) {
2460 if (position >= src->segment.stop) {
2462 position = src->segment.stop;
2466 /* negative rate, check if we reached the start. start is always set to
2467 * something different from -1 */
2468 if (position <= src->segment.start) {
2470 position = src->segment.start;
2472 /* when going reverse, all buffers are DISCONT */
2473 src->priv->discont = TRUE;
2475 GST_OBJECT_LOCK (src);
2476 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
2477 GST_OBJECT_UNLOCK (src);
2480 if (G_UNLIKELY (src->priv->discont)) {
2481 buf = gst_buffer_make_metadata_writable (buf);
2482 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2483 src->priv->discont = FALSE;
2485 GST_LIVE_UNLOCK (src);
2487 ret = gst_pad_push (pad, buf);
2488 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2489 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2490 gst_flow_get_name (ret));
2494 if (G_UNLIKELY (eos)) {
2495 GST_INFO_OBJECT (src, "pausing after end of segment");
2496 ret = GST_FLOW_UNEXPECTED;
2506 GST_DEBUG_OBJECT (src, "we are flushing");
2507 GST_LIVE_UNLOCK (src);
2508 ret = GST_FLOW_WRONG_STATE;
2513 const gchar *reason = gst_flow_get_name (ret);
2516 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2517 src->data.ABI.running = FALSE;
2518 gst_pad_pause_task (pad);
2519 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2520 if (ret == GST_FLOW_UNEXPECTED) {
2521 gboolean flag_segment;
2525 /* perform EOS logic */
2526 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2527 format = src->segment.format;
2528 last_stop = src->segment.last_stop;
2531 GstMessage *message;
2533 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2535 gst_message_set_seqnum (message, src->priv->seqnum);
2536 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2538 event = gst_event_new_eos ();
2539 gst_event_set_seqnum (event, src->priv->seqnum);
2540 gst_pad_push_event (pad, event);
2541 src->priv->last_sent_eos = TRUE;
2544 event = gst_event_new_eos ();
2545 gst_event_set_seqnum (event, src->priv->seqnum);
2546 /* for fatal errors we post an error message, post the error
2547 * first so the app knows about the error first. */
2548 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2549 (_("Internal data flow error.")),
2550 ("streaming task paused, reason %s (%d)", reason, ret));
2551 gst_pad_push_event (pad, event);
2552 src->priv->last_sent_eos = TRUE;
2559 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2560 (_("Internal data flow error.")), ("element returned NULL buffer"));
2561 GST_LIVE_UNLOCK (src);
2566 /* default negotiation code.
2568 * Take intersection between src and sink pads, take first
2572 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2575 GstCaps *caps = NULL;
2576 GstCaps *peercaps = NULL;
2577 gboolean result = FALSE;
2579 /* first see what is possible on our source pad */
2580 thiscaps = gst_pad_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
2581 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2582 /* nothing or anything is allowed, we're done */
2583 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2584 goto no_nego_needed;
2586 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2589 /* get the peer caps */
2590 peercaps = gst_pad_peer_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
2591 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2593 /* get intersection */
2594 caps = gst_caps_intersect (thiscaps, peercaps);
2595 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
2596 gst_caps_unref (thiscaps);
2597 gst_caps_unref (peercaps);
2599 /* no peer, work with our own caps then */
2603 /* take first (and best, since they are sorted) possibility */
2604 caps = gst_caps_make_writable (caps);
2605 gst_caps_truncate (caps);
2608 if (!gst_caps_is_empty (caps)) {
2609 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2610 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2612 if (gst_caps_is_any (caps)) {
2613 /* hmm, still anything, so element can do anything and
2614 * nego is not needed */
2616 } else if (gst_caps_is_fixed (caps)) {
2617 /* yay, fixed caps, use those then, it's possible that the subclass does
2618 * not accept this caps after all and we have to fail. */
2619 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2622 gst_caps_unref (caps);
2624 GST_DEBUG_OBJECT (basesrc, "no common caps");
2630 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2632 gst_caps_unref (thiscaps);
2637 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2638 ("No supported formats found"),
2639 ("This element did not produce valid caps"));
2641 gst_caps_unref (thiscaps);
2647 gst_base_src_negotiate (GstBaseSrc * basesrc)
2649 GstBaseSrcClass *bclass;
2650 gboolean result = TRUE;
2652 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2654 if (bclass->negotiate)
2655 result = bclass->negotiate (basesrc);
2661 gst_base_src_start (GstBaseSrc * basesrc)
2663 GstBaseSrcClass *bclass;
2669 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2672 GST_DEBUG_OBJECT (basesrc, "starting source");
2674 basesrc->num_buffers_left = basesrc->num_buffers;
2676 GST_OBJECT_LOCK (basesrc);
2677 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2678 GST_OBJECT_UNLOCK (basesrc);
2680 basesrc->data.ABI.running = FALSE;
2681 basesrc->priv->newsegment_pending = FALSE;
2683 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2685 result = bclass->start (basesrc);
2690 goto could_not_start;
2692 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2694 format = basesrc->segment.format;
2696 /* figure out the size */
2697 if (format == GST_FORMAT_BYTES) {
2698 if (bclass->get_size) {
2699 if (!(result = bclass->get_size (basesrc, &size)))
2705 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2706 /* only update the size when operating in bytes, subclass is supposed
2707 * to set duration in the start method for other formats */
2708 GST_OBJECT_LOCK (basesrc);
2709 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2710 GST_OBJECT_UNLOCK (basesrc);
2715 GST_DEBUG_OBJECT (basesrc,
2716 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2717 G_GINT64_FORMAT, format, result, size, basesrc->segment.duration);
2719 seekable = gst_base_src_seekable (basesrc);
2720 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
2722 /* update for random access flag */
2723 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
2725 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2727 /* run typefind if we are random_access and the typefinding is enabled. */
2728 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2731 if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
2732 goto typefind_failed;
2734 result = gst_pad_set_caps (basesrc->srcpad, caps);
2735 gst_caps_unref (caps);
2737 /* use class or default negotiate function */
2738 if (!(result = gst_base_src_negotiate (basesrc)))
2739 goto could_not_negotiate;
2747 GST_DEBUG_OBJECT (basesrc, "could not start");
2748 /* subclass is supposed to post a message. We don't have to call _stop. */
2751 could_not_negotiate:
2753 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2754 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2755 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2756 /* we must call stop */
2757 gst_base_src_stop (basesrc);
2762 GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
2763 GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
2764 /* we must call stop */
2765 gst_base_src_stop (basesrc);
2771 gst_base_src_stop (GstBaseSrc * basesrc)
2773 GstBaseSrcClass *bclass;
2774 gboolean result = TRUE;
2776 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2779 GST_DEBUG_OBJECT (basesrc, "stopping source");
2781 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2783 result = bclass->stop (basesrc);
2786 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2791 /* start or stop flushing dataprocessing
2794 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2795 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2797 GstBaseSrcClass *bclass;
2799 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2801 if (flushing && unlock) {
2802 /* unlock any subclasses, we need to do this before grabbing the
2803 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2804 * unlock to the params because of backwards compat (see seek handler)*/
2806 bclass->unlock (basesrc);
2809 /* the live lock is released when we are blocked, waiting for playing or
2810 * when we sync to the clock. */
2811 GST_LIVE_LOCK (basesrc);
2813 *playing = basesrc->live_running;
2814 basesrc->priv->flushing = flushing;
2816 /* if we are locked in the live lock, signal it to make it flush */
2817 basesrc->live_running = TRUE;
2819 /* clear pending EOS if any */
2820 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
2822 /* step 1, now that we have the LIVE lock, clear our unlock request */
2823 if (bclass->unlock_stop)
2824 bclass->unlock_stop (basesrc);
2826 /* step 2, unblock clock sync (if any) or any other blocking thing */
2827 if (basesrc->clock_id)
2828 gst_clock_id_unschedule (basesrc->clock_id);
2830 /* signal the live source that it can start playing */
2831 basesrc->live_running = live_play;
2833 GST_LIVE_SIGNAL (basesrc);
2834 GST_LIVE_UNLOCK (basesrc);
2839 /* the purpose of this function is to make sure that a live source blocks in the
2840 * LIVE lock or leaves the LIVE lock and continues playing. */
2842 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2844 GstBaseSrcClass *bclass;
2846 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2848 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2850 GST_DEBUG_OBJECT (basesrc, "unlock");
2852 bclass->unlock (basesrc);
2855 /* we are now able to grab the LIVE lock, when we get it, we can be
2856 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2858 GST_LIVE_LOCK (basesrc);
2859 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2861 /* unblock clock sync (if any) */
2862 if (basesrc->clock_id)
2863 gst_clock_id_unschedule (basesrc->clock_id);
2865 /* configure what to do when we get to the LIVE lock. */
2866 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2867 basesrc->live_running = live_play;
2872 /* clear our unlock request when going to PLAYING */
2873 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2874 if (bclass->unlock_stop)
2875 bclass->unlock_stop (basesrc);
2877 /* for live sources we restart the timestamp correction */
2878 basesrc->priv->latency = -1;
2879 /* have to restart the task in case it stopped because of the unlock when
2880 * we went to PAUSED. Only do this if we operating in push mode. */
2881 GST_OBJECT_LOCK (basesrc->srcpad);
2882 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2883 GST_OBJECT_UNLOCK (basesrc->srcpad);
2885 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2887 GST_DEBUG_OBJECT (basesrc, "signal");
2888 GST_LIVE_SIGNAL (basesrc);
2890 GST_LIVE_UNLOCK (basesrc);
2896 gst_base_src_activate_push (GstPad * pad, gboolean active)
2898 GstBaseSrc *basesrc;
2901 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2903 /* prepare subclass first */
2905 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2907 if (G_UNLIKELY (!basesrc->can_activate_push))
2908 goto no_push_activation;
2910 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2913 basesrc->priv->last_sent_eos = FALSE;
2914 basesrc->priv->discont = TRUE;
2915 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2917 /* do initial seek, which will start the task */
2918 GST_OBJECT_LOCK (basesrc);
2919 event = basesrc->data.ABI.pending_seek;
2920 basesrc->data.ABI.pending_seek = NULL;
2921 GST_OBJECT_UNLOCK (basesrc);
2923 /* no need to unlock anything, the task is certainly
2924 * not running here. The perform seek code will start the task when
2926 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2930 gst_event_unref (event);
2932 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2934 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2936 gst_pad_stop_task (pad);
2937 /* now we can stop the source */
2938 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2946 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2951 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2956 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2958 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2960 gst_pad_stop_task (pad);
2961 /* Stop the basesrc */
2962 gst_base_src_stop (basesrc);
2964 gst_event_unref (event);
2969 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2975 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2977 GstBaseSrc *basesrc;
2979 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2981 /* prepare subclass first */
2983 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2984 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2987 /* if not random_access, we cannot operate in pull mode for now */
2988 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2991 /* stop flushing now but for live sources, still block in the LIVE lock when
2992 * we are not yet PLAYING */
2993 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2995 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2996 /* flush all, there is no task to stop */
2997 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2999 /* don't send EOS when going from PAUSED => READY when in pull mode */
3000 basesrc->priv->last_sent_eos = TRUE;
3002 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3010 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3015 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3016 gst_base_src_stop (basesrc);
3021 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3026 static GstStateChangeReturn
3027 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3029 GstBaseSrc *basesrc;
3030 GstStateChangeReturn result;
3031 gboolean no_preroll = FALSE;
3033 basesrc = GST_BASE_SRC (element);
3035 switch (transition) {
3036 case GST_STATE_CHANGE_NULL_TO_READY:
3038 case GST_STATE_CHANGE_READY_TO_PAUSED:
3039 no_preroll = gst_base_src_is_live (basesrc);
3041 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3042 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3043 if (gst_base_src_is_live (basesrc)) {
3044 /* now we can start playback */
3045 gst_base_src_set_playing (basesrc, TRUE);
3053 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3054 transition)) == GST_STATE_CHANGE_FAILURE)
3057 switch (transition) {
3058 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3059 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3060 if (gst_base_src_is_live (basesrc)) {
3061 /* make sure we block in the live lock in PAUSED */
3062 gst_base_src_set_playing (basesrc, FALSE);
3066 case GST_STATE_CHANGE_PAUSED_TO_READY:
3068 GstEvent **event_p, *event;
3070 /* we don't need to unblock anything here, the pad deactivation code
3071 * already did this */
3073 /* FIXME, deprecate this behaviour, it is very dangerous.
3074 * the prefered way of sending EOS downstream is by sending
3075 * the EOS event to the element */
3076 if (!basesrc->priv->last_sent_eos) {
3077 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
3078 event = gst_event_new_eos ();
3079 gst_event_set_seqnum (event, basesrc->priv->seqnum);
3080 gst_pad_push_event (basesrc->srcpad, event);
3081 basesrc->priv->last_sent_eos = TRUE;
3083 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3084 event_p = &basesrc->data.ABI.pending_seek;
3085 gst_event_replace (event_p, NULL);
3086 event_p = &basesrc->priv->close_segment;
3087 gst_event_replace (event_p, NULL);
3088 event_p = &basesrc->priv->start_segment;
3089 gst_event_replace (event_p, NULL);
3092 case GST_STATE_CHANGE_READY_TO_NULL:
3098 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3099 result = GST_STATE_CHANGE_NO_PREROLL;
3106 GST_DEBUG_OBJECT (basesrc, "parent failed state change");