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 "gstbasesrc.h"
169 #include "gsttypefindhelper.h"
170 #include <gst/gstmarshal.h>
171 #include <gst/gst-i18n-lib.h>
173 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
174 #define GST_CAT_DEFAULT gst_base_src_debug
176 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
177 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
178 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
179 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
180 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
181 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
182 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
184 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
185 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
187 /* BaseSrc signals and args */
194 #define DEFAULT_BLOCKSIZE 4096
195 #define DEFAULT_NUM_BUFFERS -1
196 #define DEFAULT_TYPEFIND FALSE
197 #define DEFAULT_DO_TIMESTAMP FALSE
208 #define GST_BASE_SRC_GET_PRIVATE(obj) \
209 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
211 struct _GstBaseSrcPrivate
213 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
214 * to avoid the sending of two EOS in some cases) */
218 /* two segments to be sent in the streaming thread with STREAM_LOCK */
219 GstEvent *close_segment;
220 GstEvent *start_segment;
222 /* if EOS is pending (atomic) */
225 /* startup latency is the time it takes between going to PLAYING and producing
226 * the first BUFFER with running_time 0. This value is included in the latency
228 GstClockTime latency;
229 /* timestamp offset, this is the offset add to the values of gst_times for
230 * pseudo live sources */
231 GstClockTimeDiff ts_offset;
233 gboolean do_timestamp;
235 /* stream sequence number */
238 /* pending tags to be pushed in the data stream */
242 static GstElementClass *parent_class = NULL;
244 static void gst_base_src_base_init (gpointer g_class);
245 static void gst_base_src_class_init (GstBaseSrcClass * klass);
246 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
247 static void gst_base_src_finalize (GObject * object);
251 gst_base_src_get_type (void)
253 static volatile gsize base_src_type = 0;
255 if (g_once_init_enter (&base_src_type)) {
257 static const GTypeInfo base_src_info = {
258 sizeof (GstBaseSrcClass),
259 (GBaseInitFunc) gst_base_src_base_init,
261 (GClassInitFunc) gst_base_src_class_init,
266 (GInstanceInitFunc) gst_base_src_init,
269 _type = g_type_register_static (GST_TYPE_ELEMENT,
270 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
271 g_once_init_leave (&base_src_type, _type);
273 return base_src_type;
276 static GstCaps *gst_base_src_getcaps (GstPad * pad);
277 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
278 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
280 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
281 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
282 static void gst_base_src_set_property (GObject * object, guint prop_id,
283 const GValue * value, GParamSpec * pspec);
284 static void gst_base_src_get_property (GObject * object, guint prop_id,
285 GValue * value, GParamSpec * pspec);
286 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
287 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
288 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
289 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
291 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
293 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
294 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
295 GstSegment * segment);
296 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
297 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
298 GstEvent * event, GstSegment * segment);
300 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
301 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
302 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
303 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
305 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
306 GstStateChange transition);
308 static void gst_base_src_loop (GstPad * pad);
309 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
310 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
311 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
312 guint length, GstBuffer ** buf);
313 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
314 guint length, GstBuffer ** buf);
315 static gboolean gst_base_src_seekable (GstBaseSrc * src);
318 gst_base_src_base_init (gpointer g_class)
320 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
324 gst_base_src_class_init (GstBaseSrcClass * klass)
326 GObjectClass *gobject_class;
327 GstElementClass *gstelement_class;
329 gobject_class = G_OBJECT_CLASS (klass);
330 gstelement_class = GST_ELEMENT_CLASS (klass);
332 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
334 parent_class = g_type_class_peek_parent (klass);
336 gobject_class->finalize = gst_base_src_finalize;
337 gobject_class->set_property = gst_base_src_set_property;
338 gobject_class->get_property = gst_base_src_get_property;
340 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
341 g_param_spec_ulong ("blocksize", "Block size",
342 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXULONG,
343 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
345 g_param_spec_int ("num-buffers", "num-buffers",
346 "Number of buffers to output before sending EOS (-1 = unlimited)",
347 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
348 G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
350 g_param_spec_boolean ("typefind", "Typefind",
351 "Run typefind before negotiating", DEFAULT_TYPEFIND,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
354 g_param_spec_boolean ("do-timestamp", "Do timestamp",
355 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 gstelement_class->change_state =
359 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
360 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
361 gstelement_class->get_query_types =
362 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
364 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
365 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
366 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
367 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
368 klass->check_get_range =
369 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
370 klass->prepare_seek_segment =
371 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
373 /* Registering debug symbols for function pointers */
374 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_push);
375 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_pull);
376 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event_handler);
377 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
378 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_get_range);
379 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_check_get_range);
380 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getcaps);
381 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_setcaps);
382 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
386 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
389 GstPadTemplate *pad_template;
391 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
393 basesrc->is_live = FALSE;
394 basesrc->live_lock = g_mutex_new ();
395 basesrc->live_cond = g_cond_new ();
396 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
397 basesrc->num_buffers_left = -1;
399 basesrc->can_activate_push = TRUE;
400 basesrc->pad_mode = GST_ACTIVATE_NONE;
403 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
404 g_return_if_fail (pad_template != NULL);
406 GST_DEBUG_OBJECT (basesrc, "creating src pad");
407 pad = gst_pad_new_from_template (pad_template, "src");
409 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
410 gst_pad_set_activatepush_function (pad, gst_base_src_activate_push);
411 gst_pad_set_activatepull_function (pad, gst_base_src_activate_pull);
412 gst_pad_set_event_function (pad, gst_base_src_event_handler);
413 gst_pad_set_query_function (pad, gst_base_src_query);
414 gst_pad_set_checkgetrange_function (pad, gst_base_src_pad_check_get_range);
415 gst_pad_set_getrange_function (pad, gst_base_src_pad_get_range);
416 gst_pad_set_getcaps_function (pad, gst_base_src_getcaps);
417 gst_pad_set_setcaps_function (pad, gst_base_src_setcaps);
418 gst_pad_set_fixatecaps_function (pad, gst_base_src_fixate);
420 /* hold pointer to pad */
421 basesrc->srcpad = pad;
422 GST_DEBUG_OBJECT (basesrc, "adding src pad");
423 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
425 basesrc->blocksize = DEFAULT_BLOCKSIZE;
426 basesrc->clock_id = NULL;
427 /* we operate in BYTES by default */
428 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
429 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
430 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
432 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
434 GST_DEBUG_OBJECT (basesrc, "init done");
438 gst_base_src_finalize (GObject * object)
443 basesrc = GST_BASE_SRC (object);
445 g_mutex_free (basesrc->live_lock);
446 g_cond_free (basesrc->live_cond);
448 event_p = &basesrc->data.ABI.pending_seek;
449 gst_event_replace (event_p, NULL);
451 if (basesrc->priv->pending_tags) {
452 g_list_foreach (basesrc->priv->pending_tags, (GFunc) gst_event_unref, NULL);
453 g_list_free (basesrc->priv->pending_tags);
456 G_OBJECT_CLASS (parent_class)->finalize (object);
460 * gst_base_src_wait_playing:
463 * If the #GstBaseSrcClass.create() method performs its own synchronisation
464 * against the clock it must unblock when going from PLAYING to the PAUSED state
465 * and call this method before continuing to produce the remaining data.
467 * This function will block until a state change to PLAYING happens (in which
468 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
469 * to a state change to READY or a FLUSH event (in which case this function
470 * returns #GST_FLOW_WRONG_STATE).
474 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
475 * continue. Any other return value should be returned from the create vmethod.
478 gst_base_src_wait_playing (GstBaseSrc * src)
480 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
482 /* block until the state changes, or we get a flush, or something */
483 GST_DEBUG_OBJECT (src, "live source waiting for running state");
485 if (src->priv->flushing)
487 GST_DEBUG_OBJECT (src, "live source unlocked");
494 GST_DEBUG_OBJECT (src, "we are flushing");
495 return GST_FLOW_WRONG_STATE;
500 * gst_base_src_set_live:
501 * @src: base source instance
502 * @live: new live-mode
504 * If the element listens to a live source, @live should
507 * A live source will not produce data in the PAUSED state and
508 * will therefore not be able to participate in the PREROLL phase
509 * of a pipeline. To signal this fact to the application and the
510 * pipeline, the state change return value of the live source will
511 * be GST_STATE_CHANGE_NO_PREROLL.
514 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
516 g_return_if_fail (GST_IS_BASE_SRC (src));
518 GST_OBJECT_LOCK (src);
520 GST_OBJECT_UNLOCK (src);
524 * gst_base_src_is_live:
525 * @src: base source instance
527 * Check if an element is in live mode.
529 * Returns: %TRUE if element is in live mode.
532 gst_base_src_is_live (GstBaseSrc * src)
536 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
538 GST_OBJECT_LOCK (src);
539 result = src->is_live;
540 GST_OBJECT_UNLOCK (src);
546 * gst_base_src_set_format:
547 * @src: base source instance
548 * @format: the format to use
550 * Sets the default format of the source. This will be the format used
551 * for sending NEW_SEGMENT events and for performing seeks.
553 * If a format of GST_FORMAT_BYTES is set, the element will be able to
554 * operate in pull mode if the #GstBaseSrc.is_seekable() returns TRUE.
559 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
561 g_return_if_fail (GST_IS_BASE_SRC (src));
563 gst_segment_init (&src->segment, format);
567 * gst_base_src_query_latency:
569 * @live: if the source is live
570 * @min_latency: the min latency of the source
571 * @max_latency: the max latency of the source
573 * Query the source for the latency parameters. @live will be TRUE when @src is
574 * configured as a live source. @min_latency will be set to the difference
575 * between the running time and the timestamp of the first buffer.
576 * @max_latency is always the undefined value of -1.
578 * This function is mostly used by subclasses.
580 * Returns: TRUE if the query succeeded.
585 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
586 GstClockTime * min_latency, GstClockTime * max_latency)
590 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
592 GST_OBJECT_LOCK (src);
594 *live = src->is_live;
596 /* if we have a startup latency, report this one, else report 0. Subclasses
597 * are supposed to override the query function if they want something
599 if (src->priv->latency != -1)
600 min = src->priv->latency;
609 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
610 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
612 GST_OBJECT_UNLOCK (src);
618 * gst_base_src_set_blocksize:
620 * @blocksize: the new blocksize in bytes
622 * Set the number of bytes that @src will push out with each buffer. When
623 * @blocksize is set to -1, a default length will be used.
628 gst_base_src_set_blocksize (GstBaseSrc * src, gulong blocksize)
630 g_return_if_fail (GST_IS_BASE_SRC (src));
632 GST_OBJECT_LOCK (src);
633 src->blocksize = blocksize;
634 GST_OBJECT_UNLOCK (src);
638 * gst_base_src_get_blocksize:
641 * Get the number of bytes that @src will push out with each buffer.
643 * Returns: the number of bytes pushed with each buffer.
648 gst_base_src_get_blocksize (GstBaseSrc * src)
652 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
654 GST_OBJECT_LOCK (src);
655 res = src->blocksize;
656 GST_OBJECT_UNLOCK (src);
663 * gst_base_src_set_do_timestamp:
665 * @timestamp: enable or disable timestamping
667 * Configure @src to automatically timestamp outgoing buffers based on the
668 * current running_time of the pipeline. This property is mostly useful for live
674 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
676 g_return_if_fail (GST_IS_BASE_SRC (src));
678 GST_OBJECT_LOCK (src);
679 src->priv->do_timestamp = timestamp;
680 GST_OBJECT_UNLOCK (src);
684 * gst_base_src_get_do_timestamp:
687 * Query if @src timestamps outgoing buffers based on the current running_time.
689 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
694 gst_base_src_get_do_timestamp (GstBaseSrc * src)
698 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
700 GST_OBJECT_LOCK (src);
701 res = src->priv->do_timestamp;
702 GST_OBJECT_UNLOCK (src);
708 * gst_base_src_new_seamless_segment:
710 * @start: The new start value for the segment
711 * @stop: Stop value for the new segment
712 * @position: The position value for the new segent
714 * Prepare a new seamless segment for emission downstream. This function must
715 * only be called by derived sub-classes, and only from the create() function,
716 * as the stream-lock needs to be held.
718 * The format for the new segment will be the current format of the source, as
719 * configured with gst_base_src_set_format()
721 * Returns: %TRUE if preparation of the seamless segment succeeded.
726 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
731 GST_DEBUG_OBJECT (src,
732 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
733 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
734 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
736 if (src->data.ABI.running) {
737 if (src->priv->close_segment)
738 gst_event_unref (src->priv->close_segment);
739 src->priv->close_segment =
740 gst_event_new_new_segment_full (TRUE,
741 src->segment.rate, src->segment.applied_rate, src->segment.format,
742 src->segment.start, src->segment.last_stop, src->segment.time);
745 gst_segment_set_newsegment_full (&src->segment, FALSE, src->segment.rate,
746 src->segment.applied_rate, src->segment.format, start, stop, position);
748 if (src->priv->start_segment)
749 gst_event_unref (src->priv->start_segment);
750 if (src->segment.rate >= 0.0) {
751 /* forward, we send data from last_stop to stop */
752 src->priv->start_segment =
753 gst_event_new_new_segment_full (FALSE,
754 src->segment.rate, src->segment.applied_rate, src->segment.format,
755 src->segment.last_stop, stop, src->segment.time);
757 /* reverse, we send data from last_stop to start */
758 src->priv->start_segment =
759 gst_event_new_new_segment_full (FALSE,
760 src->segment.rate, src->segment.applied_rate, src->segment.format,
761 src->segment.start, src->segment.last_stop, src->segment.time);
764 src->priv->discont = TRUE;
765 src->data.ABI.running = TRUE;
771 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
773 GstBaseSrcClass *bclass;
777 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
778 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
780 if (bclass->set_caps)
781 res = bclass->set_caps (bsrc, caps);
787 gst_base_src_getcaps (GstPad * pad)
789 GstBaseSrcClass *bclass;
791 GstCaps *caps = NULL;
793 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
794 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
795 if (bclass->get_caps)
796 caps = bclass->get_caps (bsrc);
799 GstPadTemplate *pad_template;
802 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
803 if (pad_template != NULL) {
804 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
811 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
813 GstBaseSrcClass *bclass;
816 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
817 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
820 bclass->fixate (bsrc, caps);
822 gst_object_unref (bsrc);
826 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
830 switch (GST_QUERY_TYPE (query)) {
831 case GST_QUERY_POSITION:
835 gst_query_parse_position (query, &format, NULL);
837 case GST_FORMAT_PERCENT:
843 position = src->segment.last_stop;
844 duration = src->segment.duration;
846 if (position != -1 && duration != -1) {
847 if (position < duration)
848 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
851 percent = GST_FORMAT_PERCENT_MAX;
855 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
863 position = src->segment.last_stop;
865 if (position != -1) {
866 /* convert to requested format */
868 gst_pad_query_convert (src->srcpad, src->segment.format,
869 position, &format, &position);
873 gst_query_set_position (query, format, position);
879 case GST_QUERY_DURATION:
883 gst_query_parse_duration (query, &format, NULL);
885 GST_DEBUG_OBJECT (src, "duration query in format %s",
886 gst_format_get_name (format));
889 case GST_FORMAT_PERCENT:
890 gst_query_set_duration (query, GST_FORMAT_PERCENT,
891 GST_FORMAT_PERCENT_MAX);
898 /* this is the duration as configured by the subclass. */
899 duration = src->segment.duration;
901 if (duration != -1) {
902 /* convert to requested format, if this fails, we have a duration
903 * but we cannot answer the query, we must return FALSE. */
905 gst_pad_query_convert (src->srcpad, src->segment.format,
906 duration, &format, &duration);
908 /* The subclass did not configure a duration, we assume that the
909 * media has an unknown duration then and we return TRUE to report
910 * this. Note that this is not the same as returning FALSE, which
911 * means that we cannot report the duration at all. */
914 gst_query_set_duration (query, format, duration);
921 case GST_QUERY_SEEKING:
925 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
926 if (format == src->segment.format) {
927 gst_query_set_seeking (query, src->segment.format,
928 gst_base_src_seekable (src), 0, src->segment.duration);
931 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
932 /* Don't reply to the query to make up for demuxers which don't
933 * handle the SEEKING query yet. Players like Totem will fall back
934 * to the duration when the SEEKING query isn't answered. */
939 case GST_QUERY_SEGMENT:
943 /* no end segment configured, current duration then */
944 if ((stop = src->segment.stop) == -1)
945 stop = src->segment.duration;
946 start = src->segment.start;
948 /* adjust to stream time */
949 if (src->segment.time != -1) {
950 start -= src->segment.time;
952 stop -= src->segment.time;
954 gst_query_set_segment (query, src->segment.rate, src->segment.format,
960 case GST_QUERY_FORMATS:
962 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
963 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
967 case GST_QUERY_CONVERT:
969 GstFormat src_fmt, dest_fmt;
970 gint64 src_val, dest_val;
972 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
974 /* we can only convert between equal formats... */
975 if (src_fmt == dest_fmt) {
981 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
984 case GST_QUERY_LATENCY:
986 GstClockTime min, max;
989 /* Subclasses should override and implement something usefull */
990 res = gst_base_src_query_latency (src, &live, &min, &max);
992 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
993 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
994 GST_TIME_ARGS (max));
996 gst_query_set_latency (query, live, min, max);
999 case GST_QUERY_JITTER:
1000 case GST_QUERY_RATE:
1003 case GST_QUERY_BUFFERING:
1006 gint64 start, stop, estimated;
1008 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1010 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1011 gst_format_get_name (format));
1013 if (src->random_access) {
1016 if (format == GST_FORMAT_PERCENT)
1017 stop = GST_FORMAT_PERCENT_MAX;
1019 stop = src->segment.duration;
1025 /* convert to required format. When the conversion fails, we can't answer
1026 * the query. When the value is unknown, we can don't perform conversion
1027 * but report TRUE. */
1028 if (format != GST_FORMAT_PERCENT && stop != -1) {
1029 res = gst_pad_query_convert (src->srcpad, src->segment.format,
1030 stop, &format, &stop);
1034 if (res && format != GST_FORMAT_PERCENT && start != -1)
1035 res = gst_pad_query_convert (src->srcpad, src->segment.format,
1036 start, &format, &start);
1038 gst_query_set_buffering_range (query, format, start, stop, estimated);
1045 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1051 gst_base_src_query (GstPad * pad, GstQuery * query)
1054 GstBaseSrcClass *bclass;
1055 gboolean result = FALSE;
1057 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1059 bclass = GST_BASE_SRC_GET_CLASS (src);
1062 result = bclass->query (src, query);
1064 result = gst_pad_query_default (pad, query);
1066 gst_object_unref (src);
1072 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1074 gboolean res = TRUE;
1076 /* update our offset if the start/stop position was updated */
1077 if (segment->format == GST_FORMAT_BYTES) {
1078 segment->time = segment->start;
1079 } else if (segment->start == 0) {
1080 /* seek to start, we can implement a default for this. */
1084 GST_INFO_OBJECT (src, "Can't do a default seek");
1091 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1093 GstBaseSrcClass *bclass;
1094 gboolean result = FALSE;
1096 bclass = GST_BASE_SRC_GET_CLASS (src);
1098 if (bclass->do_seek)
1099 result = bclass->do_seek (src, segment);
1104 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1107 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1108 GstSegment * segment)
1110 /* By default, we try one of 2 things:
1111 * - For absolute seek positions, convert the requested position to our
1112 * configured processing format and place it in the output segment \
1113 * - For relative seek positions, convert our current (input) values to the
1114 * seek format, adjust by the relative seek offset and then convert back to
1115 * the processing format
1117 GstSeekType cur_type, stop_type;
1120 GstFormat seek_format, dest_format;
1123 gboolean res = TRUE;
1125 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1126 &cur_type, &cur, &stop_type, &stop);
1127 dest_format = segment->format;
1129 if (seek_format == dest_format) {
1130 gst_segment_set_seek (segment, rate, seek_format, flags,
1131 cur_type, cur, stop_type, stop, &update);
1135 if (cur_type != GST_SEEK_TYPE_NONE) {
1136 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1138 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
1140 cur_type = GST_SEEK_TYPE_SET;
1143 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1144 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1146 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
1148 stop_type = GST_SEEK_TYPE_SET;
1151 /* And finally, configure our output segment in the desired format */
1152 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
1153 stop_type, stop, &update);
1162 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1168 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1169 GstSegment * seeksegment)
1171 GstBaseSrcClass *bclass;
1172 gboolean result = FALSE;
1174 bclass = GST_BASE_SRC_GET_CLASS (src);
1176 if (bclass->prepare_seek_segment)
1177 result = bclass->prepare_seek_segment (src, event, seeksegment);
1182 /* this code implements the seeking. It is a good example
1183 * handling all cases.
1185 * A seek updates the currently configured segment.start
1186 * and segment.stop values based on the SEEK_TYPE. If the
1187 * segment.start value is updated, a seek to this new position
1188 * should be performed.
1190 * The seek can only be executed when we are not currently
1191 * streaming any data, to make sure that this is the case, we
1192 * acquire the STREAM_LOCK which is taken when we are in the
1193 * _loop() function or when a getrange() is called. Normally
1194 * we will not receive a seek if we are operating in pull mode
1195 * though. When we operate as a live source we might block on the live
1196 * cond, which does not release the STREAM_LOCK. Therefore we will try
1197 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1198 * safe to perform the seek.
1200 * When we are in the loop() function, we might be in the middle
1201 * of pushing a buffer, which might block in a sink. To make sure
1202 * that the push gets unblocked we push out a FLUSH_START event.
1203 * Our loop function will get a WRONG_STATE return value from
1204 * the push and will pause, effectively releasing the STREAM_LOCK.
1206 * For a non-flushing seek, we pause the task, which might eventually
1207 * release the STREAM_LOCK. We say eventually because when the sink
1208 * blocks on the sample we might wait a very long time until the sink
1209 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1210 * can continue the seek. A non-flushing seek is normally done in a
1211 * running pipeline to perform seamless playback, this means that the sink is
1212 * PLAYING and will return from its chain function.
1213 * In the case of a non-flushing seek we need to make sure that the
1214 * data we output after the seek is continuous with the previous data,
1215 * this is because a non-flushing seek does not reset the running-time
1216 * to 0. We do this by closing the currently running segment, ie. sending
1217 * a new_segment event with the stop position set to the last processed
1220 * After updating the segment.start/stop values, we prepare for
1221 * streaming again. We push out a FLUSH_STOP to make the peer pad
1222 * accept data again and we start our task again.
1224 * A segment seek posts a message on the bus saying that the playback
1225 * of the segment started. We store the segment flag internally because
1226 * when we reach the segment.stop we have to post a segment.done
1227 * instead of EOS when doing a segment seek.
1229 /* FIXME (0.11), we have the unlock gboolean here because most current
1230 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1231 * the streaming thread isn't running, resulting in bogus unlocks later when it
1232 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1233 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1237 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1239 gboolean res = TRUE, tres;
1241 GstFormat seek_format, dest_format;
1243 GstSeekType cur_type, stop_type;
1245 gboolean flush, playing;
1247 gboolean relative_seek = FALSE;
1248 gboolean seekseg_configured = FALSE;
1249 GstSegment seeksegment;
1253 GST_DEBUG_OBJECT (src, "doing seek");
1255 dest_format = src->segment.format;
1258 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1259 &cur_type, &cur, &stop_type, &stop);
1261 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1262 SEEK_TYPE_IS_RELATIVE (stop_type);
1264 if (dest_format != seek_format && !relative_seek) {
1265 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1266 * here before taking the stream lock, otherwise we must convert it later,
1267 * once we have the stream lock and can read the last configures segment
1268 * start and stop positions */
1269 gst_segment_init (&seeksegment, dest_format);
1271 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1272 goto prepare_failed;
1274 seekseg_configured = TRUE;
1277 flush = flags & GST_SEEK_FLAG_FLUSH;
1278 seqnum = gst_event_get_seqnum (event);
1281 /* get next seqnum */
1282 seqnum = gst_util_seqnum_next ();
1285 /* send flush start */
1287 tevent = gst_event_new_flush_start ();
1288 gst_event_set_seqnum (tevent, seqnum);
1289 gst_pad_push_event (src->srcpad, tevent);
1291 gst_pad_pause_task (src->srcpad);
1293 /* unblock streaming thread. */
1294 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1296 /* grab streaming lock, this should eventually be possible, either
1297 * because the task is paused, our streaming thread stopped
1298 * or because our peer is flushing. */
1299 GST_PAD_STREAM_LOCK (src->srcpad);
1300 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1301 /* we have seen this event before, issue a warning for now */
1302 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1305 src->priv->seqnum = seqnum;
1306 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1309 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1311 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1312 * copy the current segment info into the temp segment that we can actually
1313 * attempt the seek with. We only update the real segment if the seek suceeds. */
1314 if (!seekseg_configured) {
1315 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1317 /* now configure the final seek segment */
1319 if (src->segment.format != seek_format) {
1320 /* OK, here's where we give the subclass a chance to convert the relative
1321 * seek into an absolute one in the processing format. We set up any
1322 * absolute seek above, before taking the stream lock. */
1323 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1324 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1329 /* The seek format matches our processing format, no need to ask the
1330 * the subclass to configure the segment. */
1331 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1332 cur_type, cur, stop_type, stop, &update);
1335 /* Else, no seek event passed, so we're just (re)starting the
1340 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1341 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1342 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1344 /* do the seek, segment.last_stop contains the new position. */
1345 res = gst_base_src_do_seek (src, &seeksegment);
1348 /* and prepare to continue streaming */
1350 tevent = gst_event_new_flush_stop ();
1351 gst_event_set_seqnum (tevent, seqnum);
1352 /* send flush stop, peer will accept data and events again. We
1353 * are not yet providing data as we still have the STREAM_LOCK. */
1354 gst_pad_push_event (src->srcpad, tevent);
1355 } else if (res && src->data.ABI.running) {
1356 /* we are running the current segment and doing a non-flushing seek,
1357 * close the segment first based on the last_stop. */
1358 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1359 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1361 /* queue the segment for sending in the stream thread */
1362 if (src->priv->close_segment)
1363 gst_event_unref (src->priv->close_segment);
1364 src->priv->close_segment =
1365 gst_event_new_new_segment_full (TRUE,
1366 src->segment.rate, src->segment.applied_rate, src->segment.format,
1367 src->segment.start, src->segment.last_stop, src->segment.time);
1368 gst_event_set_seqnum (src->priv->close_segment, seqnum);
1371 /* The subclass must have converted the segment to the processing format
1373 if (res && seeksegment.format != dest_format) {
1374 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1375 "in the correct format. Aborting seek.");
1379 /* if the seek was successful, we update our real segment and push
1380 * out the new segment. */
1382 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1384 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1385 GstMessage *message;
1387 message = gst_message_new_segment_start (GST_OBJECT (src),
1388 src->segment.format, src->segment.last_stop);
1389 gst_message_set_seqnum (message, seqnum);
1391 gst_element_post_message (GST_ELEMENT (src), message);
1394 /* for deriving a stop position for the playback segment from the seek
1395 * segment, we must take the duration when the stop is not set */
1396 if ((stop = src->segment.stop) == -1)
1397 stop = src->segment.duration;
1399 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1400 " to %" G_GINT64_FORMAT, src->segment.start, stop);
1402 /* now replace the old segment so that we send it in the stream thread the
1403 * next time it is scheduled. */
1404 if (src->priv->start_segment)
1405 gst_event_unref (src->priv->start_segment);
1406 if (src->segment.rate >= 0.0) {
1407 /* forward, we send data from last_stop to stop */
1408 src->priv->start_segment =
1409 gst_event_new_new_segment_full (FALSE,
1410 src->segment.rate, src->segment.applied_rate, src->segment.format,
1411 src->segment.last_stop, stop, src->segment.time);
1413 /* reverse, we send data from last_stop to start */
1414 src->priv->start_segment =
1415 gst_event_new_new_segment_full (FALSE,
1416 src->segment.rate, src->segment.applied_rate, src->segment.format,
1417 src->segment.start, src->segment.last_stop, src->segment.time);
1419 gst_event_set_seqnum (src->priv->start_segment, seqnum);
1422 src->priv->discont = TRUE;
1423 src->data.ABI.running = TRUE;
1424 /* and restart the task in case it got paused explicitly or by
1425 * the FLUSH_START event we pushed out. */
1426 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1431 /* and release the lock again so we can continue streaming */
1432 GST_PAD_STREAM_UNLOCK (src->srcpad);
1438 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1443 static const GstQueryType *
1444 gst_base_src_get_query_types (GstElement * element)
1446 static const GstQueryType query_types[] = {
1462 /* all events send to this element directly. This is mainly done from the
1466 gst_base_src_send_event (GstElement * element, GstEvent * event)
1469 gboolean result = FALSE;
1471 src = GST_BASE_SRC (element);
1473 GST_DEBUG_OBJECT (src, "reveived %s event", GST_EVENT_TYPE_NAME (event));
1475 switch (GST_EVENT_TYPE (event)) {
1476 /* bidirectional events */
1477 case GST_EVENT_FLUSH_START:
1478 case GST_EVENT_FLUSH_STOP:
1479 /* sending random flushes downstream can break stuff,
1480 * especially sync since all segment info will get flushed */
1483 /* downstream serialized events */
1486 GstBaseSrcClass *bclass;
1488 bclass = GST_BASE_SRC_GET_CLASS (src);
1490 /* queue EOS and make sure the task or pull function performs the EOS
1493 * We have two possibilities:
1495 * - Before we are to enter the _create function, we check the pending_eos
1496 * first and do EOS instead of entering it.
1497 * - If we are in the _create function or we did not manage to set the
1498 * flag fast enough and we are about to enter the _create function,
1499 * we unlock it so that we exit with WRONG_STATE immediatly. We then
1500 * check the EOS flag and do the EOS logic.
1502 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1503 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1505 /* unlock the _create function so that we can check the pending_eos flag
1506 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1507 * that we can grab it and stop the unlock again. We don't take the stream
1508 * lock so that this operation is guaranteed to never block. */
1510 bclass->unlock (src);
1512 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1514 GST_LIVE_LOCK (src);
1515 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1516 /* now stop the unlock of the streaming thread again. Grabbing the live
1517 * lock is enough because that protects the create function. */
1518 if (bclass->unlock_stop)
1519 bclass->unlock_stop (src);
1520 GST_LIVE_UNLOCK (src);
1525 case GST_EVENT_NEWSEGMENT:
1526 /* sending random NEWSEGMENT downstream can break sync. */
1529 /* Insert tag in the dataflow */
1530 GST_OBJECT_LOCK (src);
1531 src->priv->pending_tags = g_list_append (src->priv->pending_tags, event);
1532 GST_OBJECT_UNLOCK (src);
1536 case GST_EVENT_BUFFERSIZE:
1537 /* does not seem to make much sense currently */
1540 /* upstream events */
1542 /* elements should override send_event and do something */
1544 case GST_EVENT_SEEK:
1548 GST_OBJECT_LOCK (src->srcpad);
1549 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1551 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1552 GST_OBJECT_UNLOCK (src->srcpad);
1555 GST_DEBUG_OBJECT (src, "performing seek");
1556 /* when we are running in push mode, we can execute the
1557 * seek right now, we need to unlock. */
1558 result = gst_base_src_perform_seek (src, event, TRUE);
1562 /* else we store the event and execute the seek when we
1564 GST_OBJECT_LOCK (src);
1565 GST_DEBUG_OBJECT (src, "queueing seek");
1566 event_p = &src->data.ABI.pending_seek;
1567 gst_event_replace ((GstEvent **) event_p, event);
1568 GST_OBJECT_UNLOCK (src);
1569 /* assume the seek will work */
1574 case GST_EVENT_NAVIGATION:
1575 /* could make sense for elements that do something with navigation events
1576 * but then they would need to override the send_event function */
1578 case GST_EVENT_LATENCY:
1579 /* does not seem to make sense currently */
1583 case GST_EVENT_CUSTOM_UPSTREAM:
1584 /* override send_event if you want this */
1586 case GST_EVENT_CUSTOM_DOWNSTREAM:
1587 case GST_EVENT_CUSTOM_BOTH:
1588 /* FIXME, insert event in the dataflow */
1590 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1591 case GST_EVENT_CUSTOM_BOTH_OOB:
1592 /* insert a random custom event into the pipeline */
1593 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1594 result = gst_pad_push_event (src->srcpad, event);
1595 /* we gave away the ref to the event in the push */
1602 /* if we still have a ref to the event, unref it now */
1604 gst_event_unref (event);
1611 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1612 GST_OBJECT_UNLOCK (src->srcpad);
1619 gst_base_src_seekable (GstBaseSrc * src)
1621 GstBaseSrcClass *bclass;
1622 bclass = GST_BASE_SRC_GET_CLASS (src);
1623 if (bclass->is_seekable)
1624 return bclass->is_seekable (src);
1630 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1634 switch (GST_EVENT_TYPE (event)) {
1635 case GST_EVENT_SEEK:
1636 /* is normally called when in push mode */
1637 if (!gst_base_src_seekable (src))
1640 result = gst_base_src_perform_seek (src, event, TRUE);
1642 case GST_EVENT_FLUSH_START:
1643 /* cancel any blocking getrange, is normally called
1644 * when in pull mode. */
1645 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1647 case GST_EVENT_FLUSH_STOP:
1648 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1659 GST_DEBUG_OBJECT (src, "is not seekable");
1665 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1668 GstBaseSrcClass *bclass;
1669 gboolean result = FALSE;
1671 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1672 bclass = GST_BASE_SRC_GET_CLASS (src);
1674 if (bclass->event) {
1675 if (!(result = bclass->event (src, event)))
1676 goto subclass_failed;
1680 gst_event_unref (event);
1681 gst_object_unref (src);
1688 GST_DEBUG_OBJECT (src, "subclass refused event");
1694 gst_base_src_set_property (GObject * object, guint prop_id,
1695 const GValue * value, GParamSpec * pspec)
1699 src = GST_BASE_SRC (object);
1702 case PROP_BLOCKSIZE:
1703 gst_base_src_set_blocksize (src, g_value_get_ulong (value));
1705 case PROP_NUM_BUFFERS:
1706 src->num_buffers = g_value_get_int (value);
1709 src->data.ABI.typefind = g_value_get_boolean (value);
1711 case PROP_DO_TIMESTAMP:
1712 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1715 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1721 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1726 src = GST_BASE_SRC (object);
1729 case PROP_BLOCKSIZE:
1730 g_value_set_ulong (value, gst_base_src_get_blocksize (src));
1732 case PROP_NUM_BUFFERS:
1733 g_value_set_int (value, src->num_buffers);
1736 g_value_set_boolean (value, src->data.ABI.typefind);
1738 case PROP_DO_TIMESTAMP:
1739 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1742 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1747 /* with STREAM_LOCK and LOCK */
1748 static GstClockReturn
1749 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1754 id = gst_clock_new_single_shot_id (clock, time);
1756 basesrc->clock_id = id;
1757 /* release the live lock while waiting */
1758 GST_LIVE_UNLOCK (basesrc);
1760 ret = gst_clock_id_wait (id, NULL);
1762 GST_LIVE_LOCK (basesrc);
1763 gst_clock_id_unref (id);
1764 basesrc->clock_id = NULL;
1769 /* perform synchronisation on a buffer.
1772 static GstClockReturn
1773 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1775 GstClockReturn result;
1776 GstClockTime start, end;
1777 GstBaseSrcClass *bclass;
1778 GstClockTime base_time;
1780 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1781 gboolean do_timestamp, first, pseudo_live;
1783 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1786 if (bclass->get_times)
1787 bclass->get_times (basesrc, buffer, &start, &end);
1789 /* get buffer timestamp */
1790 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1792 /* grab the lock to prepare for clocking and calculate the startup
1794 GST_OBJECT_LOCK (basesrc);
1796 /* if we are asked to sync against the clock we are a pseudo live element */
1797 pseudo_live = (start != -1 && basesrc->is_live);
1798 /* check for the first buffer */
1799 first = (basesrc->priv->latency == -1);
1801 if (timestamp != -1 && pseudo_live) {
1802 GstClockTime latency;
1804 /* we have a timestamp and a sync time, latency is the diff */
1805 if (timestamp <= start)
1806 latency = start - timestamp;
1811 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1812 GST_TIME_ARGS (latency));
1813 /* first time we calculate latency, just configure */
1814 basesrc->priv->latency = latency;
1816 if (basesrc->priv->latency != latency) {
1817 /* we have a new latency, FIXME post latency message */
1818 basesrc->priv->latency = latency;
1819 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1820 GST_TIME_ARGS (latency));
1824 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1825 basesrc->is_live, start != -1);
1826 basesrc->priv->latency = 0;
1829 /* get clock, if no clock, we can't sync or do timestamps */
1830 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1833 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1835 do_timestamp = basesrc->priv->do_timestamp;
1837 /* first buffer, calculate the timestamp offset */
1839 GstClockTime running_time;
1841 now = gst_clock_get_time (clock);
1842 running_time = now - base_time;
1844 GST_LOG_OBJECT (basesrc,
1845 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1846 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1847 GST_TIME_ARGS (running_time));
1849 if (pseudo_live && timestamp != -1) {
1850 /* live source and we need to sync, add startup latency to all timestamps
1851 * to get the real running_time. Live sources should always timestamp
1852 * according to the current running time. */
1853 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1855 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1856 GST_TIME_ARGS (basesrc->priv->ts_offset));
1858 basesrc->priv->ts_offset = 0;
1859 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1862 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1864 timestamp = running_time;
1868 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1870 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1871 GST_TIME_ARGS (timestamp));
1874 /* add the timestamp offset we need for sync */
1875 timestamp += basesrc->priv->ts_offset;
1877 /* not the first buffer, the timestamp is the diff between the clock and
1879 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1880 now = gst_clock_get_time (clock);
1882 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1884 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1885 GST_TIME_ARGS (now - base_time));
1889 /* if we don't have a buffer timestamp, we don't sync */
1890 if (!GST_CLOCK_TIME_IS_VALID (start))
1893 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1894 /* for pseudo live sources, add our ts_offset to the timestamp */
1895 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1896 start += basesrc->priv->ts_offset;
1899 GST_LOG_OBJECT (basesrc,
1900 "waiting for clock, base time %" GST_TIME_FORMAT
1901 ", stream_start %" GST_TIME_FORMAT,
1902 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1903 GST_OBJECT_UNLOCK (basesrc);
1905 result = gst_base_src_wait (basesrc, clock, start + base_time);
1907 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1914 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1915 GST_OBJECT_UNLOCK (basesrc);
1916 return GST_CLOCK_OK;
1920 GST_DEBUG_OBJECT (basesrc, "no sync needed");
1921 GST_OBJECT_UNLOCK (basesrc);
1922 return GST_CLOCK_OK;
1927 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1929 guint64 size, maxsize;
1930 GstBaseSrcClass *bclass;
1932 bclass = GST_BASE_SRC_GET_CLASS (src);
1934 /* only operate if we are working with bytes */
1935 if (src->segment.format != GST_FORMAT_BYTES)
1938 /* get total file size */
1939 size = (guint64) src->segment.duration;
1941 /* the max amount of bytes to read is the total size or
1942 * up to the segment.stop if present. */
1943 if (src->segment.stop != -1)
1944 maxsize = MIN (size, src->segment.stop);
1948 GST_DEBUG_OBJECT (src,
1949 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1950 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1951 *length, size, src->segment.stop, maxsize);
1953 /* check size if we have one */
1954 if (maxsize != -1) {
1955 /* if we run past the end, check if the file became bigger and
1957 if (G_UNLIKELY (offset + *length >= maxsize)) {
1958 /* see if length of the file changed */
1959 if (bclass->get_size)
1960 if (!bclass->get_size (src, &size))
1963 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1965 /* make sure we don't exceed the configured segment stop
1967 if (src->segment.stop != -1)
1968 maxsize = MIN (size, src->segment.stop);
1972 /* if we are at or past the end, EOS */
1973 if (G_UNLIKELY (offset >= maxsize))
1974 goto unexpected_length;
1976 /* else we can clip to the end */
1977 if (G_UNLIKELY (offset + *length >= maxsize))
1978 *length = maxsize - offset;
1983 /* keep track of current position. segment is in bytes, we checked
1985 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1996 /* must be called with LIVE_LOCK */
1997 static GstFlowReturn
1998 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2002 GstBaseSrcClass *bclass;
2003 GstClockReturn status;
2005 bclass = GST_BASE_SRC_GET_CLASS (src);
2009 while (G_UNLIKELY (!src->live_running)) {
2010 ret = gst_base_src_wait_playing (src);
2011 if (ret != GST_FLOW_OK)
2016 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
2019 if (G_UNLIKELY (!bclass->create))
2022 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2023 goto unexpected_length;
2025 /* normally we don't count buffers */
2026 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2027 if (src->num_buffers_left == 0)
2028 goto reached_num_buffers;
2030 src->num_buffers_left--;
2033 /* don't enter the create function if a pending EOS event was set. For the
2034 * logic of the pending_eos, check the event function of this class. */
2035 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2038 GST_DEBUG_OBJECT (src,
2039 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2040 G_GINT64_FORMAT, offset, length, src->segment.time);
2042 ret = bclass->create (src, offset, length, buf);
2044 /* The create function could be unlocked because we have a pending EOS. It's
2045 * possible that we have a valid buffer from create that we need to
2046 * discard when the create function returned _OK. */
2047 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2048 if (ret == GST_FLOW_OK) {
2049 gst_buffer_unref (*buf);
2055 if (G_UNLIKELY (ret != GST_FLOW_OK))
2058 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2059 if (offset == 0 && src->segment.time == 0
2060 && GST_BUFFER_TIMESTAMP (*buf) == -1)
2061 GST_BUFFER_TIMESTAMP (*buf) = 0;
2063 /* set pad caps on the buffer if the buffer had no caps */
2064 if (GST_BUFFER_CAPS (*buf) == NULL)
2065 gst_buffer_set_caps (*buf, GST_PAD_CAPS (src->srcpad));
2067 /* now sync before pushing the buffer */
2068 status = gst_base_src_do_sync (src, *buf);
2070 /* waiting for the clock could have made us flushing */
2071 if (G_UNLIKELY (src->priv->flushing))
2075 case GST_CLOCK_EARLY:
2076 /* the buffer is too late. We currently don't drop the buffer. */
2077 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2080 /* buffer synchronised properly */
2081 GST_DEBUG_OBJECT (src, "buffer ok");
2083 case GST_CLOCK_UNSCHEDULED:
2084 /* this case is triggered when we were waiting for the clock and
2085 * it got unlocked because we did a state change. In any case, get rid of
2087 gst_buffer_unref (*buf);
2089 if (!src->live_running) {
2090 /* We return WRONG_STATE when we are not running to stop the dataflow also
2091 * get rid of the produced buffer. */
2092 GST_DEBUG_OBJECT (src,
2093 "clock was unscheduled (%d), returning WRONG_STATE", status);
2094 ret = GST_FLOW_WRONG_STATE;
2096 /* If we are running when this happens, we quickly switched between
2097 * pause and playing. We try to produce a new buffer */
2098 GST_DEBUG_OBJECT (src,
2099 "clock was unscheduled (%d), but we are running", status);
2104 /* all other result values are unexpected and errors */
2105 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2106 (_("Internal clock error.")),
2107 ("clock returned unexpected return value %d", status));
2108 gst_buffer_unref (*buf);
2110 ret = GST_FLOW_ERROR;
2118 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2119 gst_flow_get_name (ret));
2124 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2125 gst_flow_get_name (ret));
2130 GST_DEBUG_OBJECT (src, "getrange but not started");
2131 return GST_FLOW_WRONG_STATE;
2135 GST_DEBUG_OBJECT (src, "no create function");
2136 return GST_FLOW_ERROR;
2140 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2141 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2142 return GST_FLOW_UNEXPECTED;
2144 reached_num_buffers:
2146 GST_DEBUG_OBJECT (src, "sent all buffers");
2147 return GST_FLOW_UNEXPECTED;
2151 GST_DEBUG_OBJECT (src, "we are flushing");
2152 gst_buffer_unref (*buf);
2154 return GST_FLOW_WRONG_STATE;
2158 GST_DEBUG_OBJECT (src, "we are EOS");
2159 return GST_FLOW_UNEXPECTED;
2163 static GstFlowReturn
2164 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
2170 src = GST_BASE_SRC (gst_pad_get_parent (pad));
2172 GST_LIVE_LOCK (src);
2173 if (G_UNLIKELY (src->priv->flushing))
2176 res = gst_base_src_get_range (src, offset, length, buf);
2179 GST_LIVE_UNLOCK (src);
2181 gst_object_unref (src);
2188 GST_DEBUG_OBJECT (src, "we are flushing");
2189 res = GST_FLOW_WRONG_STATE;
2195 gst_base_src_default_check_get_range (GstBaseSrc * src)
2199 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
2200 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2201 if (G_LIKELY (gst_base_src_start (src)))
2202 gst_base_src_stop (src);
2205 /* we can operate in getrange mode if the native format is bytes
2206 * and we are seekable, this condition is set in the random_access
2207 * flag and is set in the _start() method. */
2208 res = src->random_access;
2214 gst_base_src_check_get_range (GstBaseSrc * src)
2216 GstBaseSrcClass *bclass;
2219 bclass = GST_BASE_SRC_GET_CLASS (src);
2221 if (bclass->check_get_range == NULL)
2224 res = bclass->check_get_range (src);
2225 GST_LOG_OBJECT (src, "%s() returned %d",
2226 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
2233 GST_WARNING_OBJECT (src, "no check_get_range function set");
2239 gst_base_src_pad_check_get_range (GstPad * pad)
2244 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2246 res = gst_base_src_check_get_range (src);
2252 gst_base_src_loop (GstPad * pad)
2255 GstBuffer *buf = NULL;
2264 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2266 GST_LIVE_LOCK (src);
2268 if (G_UNLIKELY (src->priv->flushing))
2271 src->priv->last_sent_eos = FALSE;
2273 blocksize = src->blocksize;
2275 /* if we operate in bytes, we can calculate an offset */
2276 if (src->segment.format == GST_FORMAT_BYTES) {
2277 position = src->segment.last_stop;
2278 /* for negative rates, start with subtracting the blocksize */
2279 if (src->segment.rate < 0.0) {
2280 /* we cannot go below segment.start */
2281 if (position > src->segment.start + blocksize)
2282 position -= blocksize;
2284 /* last block, remainder up to segment.start */
2285 blocksize = position - src->segment.start;
2286 position = src->segment.start;
2292 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
2293 GST_TIME_ARGS (position), blocksize);
2295 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2296 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2297 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2298 gst_flow_get_name (ret));
2299 GST_LIVE_UNLOCK (src);
2302 /* this should not happen */
2303 if (G_UNLIKELY (buf == NULL))
2306 /* push events to close/start our segment before we push the buffer. */
2307 if (G_UNLIKELY (src->priv->close_segment)) {
2308 gst_pad_push_event (pad, src->priv->close_segment);
2309 src->priv->close_segment = NULL;
2311 if (G_UNLIKELY (src->priv->start_segment)) {
2312 gst_pad_push_event (pad, src->priv->start_segment);
2313 src->priv->start_segment = NULL;
2316 GST_OBJECT_LOCK (src);
2318 tags = src->priv->pending_tags;
2319 src->priv->pending_tags = NULL;
2320 GST_OBJECT_UNLOCK (src);
2322 /* Push out pending tags if any */
2323 if (G_UNLIKELY (tags != NULL)) {
2324 for (tmp = tags; tmp; tmp = g_list_next (tmp)) {
2325 GstEvent *ev = (GstEvent *) tmp->data;
2326 gst_pad_push_event (pad, ev);
2331 /* figure out the new position */
2332 switch (src->segment.format) {
2333 case GST_FORMAT_BYTES:
2335 guint bufsize = GST_BUFFER_SIZE (buf);
2337 /* we subtracted above for negative rates */
2338 if (src->segment.rate >= 0.0)
2339 position += bufsize;
2342 case GST_FORMAT_TIME:
2344 GstClockTime start, duration;
2346 start = GST_BUFFER_TIMESTAMP (buf);
2347 duration = GST_BUFFER_DURATION (buf);
2349 if (GST_CLOCK_TIME_IS_VALID (start))
2352 position = src->segment.last_stop;
2354 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2355 if (src->segment.rate >= 0.0)
2356 position += duration;
2357 else if (position > duration)
2358 position -= duration;
2364 case GST_FORMAT_DEFAULT:
2365 if (src->segment.rate >= 0.0)
2366 position = GST_BUFFER_OFFSET_END (buf);
2368 position = GST_BUFFER_OFFSET (buf);
2374 if (position != -1) {
2375 if (src->segment.rate >= 0.0) {
2376 /* positive rate, check if we reached the stop */
2377 if (src->segment.stop != -1) {
2378 if (position >= src->segment.stop) {
2380 position = src->segment.stop;
2384 /* negative rate, check if we reached the start. start is always set to
2385 * something different from -1 */
2386 if (position <= src->segment.start) {
2388 position = src->segment.start;
2390 /* when going reverse, all buffers are DISCONT */
2391 src->priv->discont = TRUE;
2393 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
2396 if (G_UNLIKELY (src->priv->discont)) {
2397 buf = gst_buffer_make_metadata_writable (buf);
2398 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2399 src->priv->discont = FALSE;
2401 GST_LIVE_UNLOCK (src);
2403 ret = gst_pad_push (pad, buf);
2404 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2405 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2406 gst_flow_get_name (ret));
2410 if (G_UNLIKELY (eos)) {
2411 GST_INFO_OBJECT (src, "pausing after end of segment");
2412 ret = GST_FLOW_UNEXPECTED;
2422 GST_DEBUG_OBJECT (src, "we are flushing");
2423 GST_LIVE_UNLOCK (src);
2424 ret = GST_FLOW_WRONG_STATE;
2429 const gchar *reason = gst_flow_get_name (ret);
2432 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2433 src->data.ABI.running = FALSE;
2434 gst_pad_pause_task (pad);
2435 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2436 if (ret == GST_FLOW_UNEXPECTED) {
2437 /* perform EOS logic */
2438 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2439 GstMessage *message;
2441 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2442 src->segment.format, src->segment.last_stop);
2443 gst_message_set_seqnum (message, src->priv->seqnum);
2444 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2446 event = gst_event_new_eos ();
2447 gst_event_set_seqnum (event, src->priv->seqnum);
2448 gst_pad_push_event (pad, event);
2449 src->priv->last_sent_eos = TRUE;
2452 event = gst_event_new_eos ();
2453 gst_event_set_seqnum (event, src->priv->seqnum);
2454 /* for fatal errors we post an error message, post the error
2455 * first so the app knows about the error first. */
2456 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2457 (_("Internal data flow error.")),
2458 ("streaming task paused, reason %s (%d)", reason, ret));
2459 gst_pad_push_event (pad, event);
2460 src->priv->last_sent_eos = TRUE;
2467 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2468 (_("Internal data flow error.")), ("element returned NULL buffer"));
2469 GST_LIVE_UNLOCK (src);
2470 /* we finished the segment on error */
2471 ret = GST_FLOW_ERROR;
2476 /* default negotiation code.
2478 * Take intersection between src and sink pads, take first
2482 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2485 GstCaps *caps = NULL;
2486 GstCaps *peercaps = NULL;
2487 gboolean result = FALSE;
2489 /* first see what is possible on our source pad */
2490 thiscaps = gst_pad_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
2491 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2492 /* nothing or anything is allowed, we're done */
2493 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2494 goto no_nego_needed;
2496 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2499 /* get the peer caps */
2500 peercaps = gst_pad_peer_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
2501 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2505 /* get intersection */
2506 icaps = gst_caps_intersect (thiscaps, peercaps);
2507 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
2508 gst_caps_unref (thiscaps);
2509 gst_caps_unref (peercaps);
2511 /* take first (and best, since they are sorted) possibility */
2512 caps = gst_caps_copy_nth (icaps, 0);
2513 gst_caps_unref (icaps);
2516 /* no peer, work with our own caps then */
2520 caps = gst_caps_make_writable (caps);
2521 gst_caps_truncate (caps);
2524 if (!gst_caps_is_empty (caps)) {
2525 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2526 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2528 if (gst_caps_is_any (caps)) {
2529 /* hmm, still anything, so element can do anything and
2530 * nego is not needed */
2532 } else if (gst_caps_is_fixed (caps)) {
2533 /* yay, fixed caps, use those then, it's possible that the subclass does
2534 * not accept this caps after all and we have to fail. */
2535 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2538 gst_caps_unref (caps);
2540 GST_DEBUG_OBJECT (basesrc, "no common caps");
2546 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2548 gst_caps_unref (thiscaps);
2553 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2554 ("No supported formats found"),
2555 ("This element did not produce valid caps"));
2557 gst_caps_unref (thiscaps);
2563 gst_base_src_negotiate (GstBaseSrc * basesrc)
2565 GstBaseSrcClass *bclass;
2566 gboolean result = TRUE;
2568 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2570 if (bclass->negotiate)
2571 result = bclass->negotiate (basesrc);
2577 gst_base_src_start (GstBaseSrc * basesrc)
2579 GstBaseSrcClass *bclass;
2584 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2587 GST_DEBUG_OBJECT (basesrc, "starting source");
2589 basesrc->num_buffers_left = basesrc->num_buffers;
2591 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2592 basesrc->data.ABI.running = FALSE;
2594 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2596 result = bclass->start (basesrc);
2601 goto could_not_start;
2603 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2605 /* figure out the size */
2606 if (basesrc->segment.format == GST_FORMAT_BYTES) {
2607 if (bclass->get_size) {
2608 if (!(result = bclass->get_size (basesrc, &size)))
2614 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2615 /* only update the size when operating in bytes, subclass is supposed
2616 * to set duration in the start method for other formats */
2617 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2622 GST_DEBUG_OBJECT (basesrc,
2623 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2624 G_GINT64_FORMAT, basesrc->segment.format, result, size,
2625 basesrc->segment.duration);
2627 seekable = gst_base_src_seekable (basesrc);
2628 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
2630 /* update for random access flag */
2631 basesrc->random_access = seekable &&
2632 basesrc->segment.format == GST_FORMAT_BYTES;
2634 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2636 /* run typefind if we are random_access and the typefinding is enabled. */
2637 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2640 if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
2641 goto typefind_failed;
2643 result = gst_pad_set_caps (basesrc->srcpad, caps);
2644 gst_caps_unref (caps);
2646 /* use class or default negotiate function */
2647 if (!(result = gst_base_src_negotiate (basesrc)))
2648 goto could_not_negotiate;
2656 GST_DEBUG_OBJECT (basesrc, "could not start");
2657 /* subclass is supposed to post a message. We don't have to call _stop. */
2660 could_not_negotiate:
2662 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2663 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2664 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2665 /* we must call stop */
2666 gst_base_src_stop (basesrc);
2671 GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
2672 GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
2673 /* we must call stop */
2674 gst_base_src_stop (basesrc);
2680 gst_base_src_stop (GstBaseSrc * basesrc)
2682 GstBaseSrcClass *bclass;
2683 gboolean result = TRUE;
2685 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2688 GST_DEBUG_OBJECT (basesrc, "stopping source");
2690 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2692 result = bclass->stop (basesrc);
2695 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2700 /* start or stop flushing dataprocessing
2703 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2704 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2706 GstBaseSrcClass *bclass;
2708 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2710 if (flushing && unlock) {
2711 /* unlock any subclasses, we need to do this before grabbing the
2712 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2713 * unlock to the params because of backwards compat (see seek handler)*/
2715 bclass->unlock (basesrc);
2718 /* the live lock is released when we are blocked, waiting for playing or
2719 * when we sync to the clock. */
2720 GST_LIVE_LOCK (basesrc);
2722 *playing = basesrc->live_running;
2723 basesrc->priv->flushing = flushing;
2725 /* if we are locked in the live lock, signal it to make it flush */
2726 basesrc->live_running = TRUE;
2728 /* clear pending EOS if any */
2729 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
2731 /* step 1, now that we have the LIVE lock, clear our unlock request */
2732 if (bclass->unlock_stop)
2733 bclass->unlock_stop (basesrc);
2735 /* step 2, unblock clock sync (if any) or any other blocking thing */
2736 if (basesrc->clock_id)
2737 gst_clock_id_unschedule (basesrc->clock_id);
2739 /* signal the live source that it can start playing */
2740 basesrc->live_running = live_play;
2742 GST_LIVE_SIGNAL (basesrc);
2743 GST_LIVE_UNLOCK (basesrc);
2748 /* the purpose of this function is to make sure that a live source blocks in the
2749 * LIVE lock or leaves the LIVE lock and continues playing. */
2751 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2753 GstBaseSrcClass *bclass;
2755 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2757 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2759 GST_DEBUG_OBJECT (basesrc, "unlock");
2761 bclass->unlock (basesrc);
2764 /* we are now able to grab the LIVE lock, when we get it, we can be
2765 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2767 GST_LIVE_LOCK (basesrc);
2768 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2770 /* unblock clock sync (if any) */
2771 if (basesrc->clock_id)
2772 gst_clock_id_unschedule (basesrc->clock_id);
2774 /* configure what to do when we get to the LIVE lock. */
2775 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2776 basesrc->live_running = live_play;
2781 /* clear our unlock request when going to PLAYING */
2782 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2783 if (bclass->unlock_stop)
2784 bclass->unlock_stop (basesrc);
2786 /* for live sources we restart the timestamp correction */
2787 basesrc->priv->latency = -1;
2788 /* have to restart the task in case it stopped because of the unlock when
2789 * we went to PAUSED. Only do this if we operating in push mode. */
2790 GST_OBJECT_LOCK (basesrc->srcpad);
2791 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2792 GST_OBJECT_UNLOCK (basesrc->srcpad);
2794 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2796 GST_DEBUG_OBJECT (basesrc, "signal");
2797 GST_LIVE_SIGNAL (basesrc);
2799 GST_LIVE_UNLOCK (basesrc);
2805 gst_base_src_activate_push (GstPad * pad, gboolean active)
2807 GstBaseSrc *basesrc;
2810 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2812 /* prepare subclass first */
2814 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2816 if (G_UNLIKELY (!basesrc->can_activate_push))
2817 goto no_push_activation;
2819 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2822 basesrc->priv->last_sent_eos = FALSE;
2823 basesrc->priv->discont = TRUE;
2824 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2826 /* do initial seek, which will start the task */
2827 GST_OBJECT_LOCK (basesrc);
2828 event = basesrc->data.ABI.pending_seek;
2829 basesrc->data.ABI.pending_seek = NULL;
2830 GST_OBJECT_UNLOCK (basesrc);
2832 /* no need to unlock anything, the task is certainly
2833 * not running here. The perform seek code will start the task when
2835 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2839 gst_event_unref (event);
2841 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2843 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2845 gst_pad_stop_task (pad);
2846 /* now we can stop the source */
2847 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2855 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2860 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2865 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2867 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2869 gst_pad_stop_task (pad);
2870 /* Stop the basesrc */
2871 gst_base_src_stop (basesrc);
2873 gst_event_unref (event);
2878 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2884 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2886 GstBaseSrc *basesrc;
2888 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2890 /* prepare subclass first */
2892 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2893 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2896 /* if not random_access, we cannot operate in pull mode for now */
2897 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2900 /* stop flushing now but for live sources, still block in the LIVE lock when
2901 * we are not yet PLAYING */
2902 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2904 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2905 /* flush all, there is no task to stop */
2906 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2908 /* don't send EOS when going from PAUSED => READY when in pull mode */
2909 basesrc->priv->last_sent_eos = TRUE;
2911 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2919 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2924 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2925 gst_base_src_stop (basesrc);
2930 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2935 static GstStateChangeReturn
2936 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2938 GstBaseSrc *basesrc;
2939 GstStateChangeReturn result;
2940 gboolean no_preroll = FALSE;
2942 basesrc = GST_BASE_SRC (element);
2944 switch (transition) {
2945 case GST_STATE_CHANGE_NULL_TO_READY:
2947 case GST_STATE_CHANGE_READY_TO_PAUSED:
2948 no_preroll = gst_base_src_is_live (basesrc);
2950 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2951 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
2952 if (gst_base_src_is_live (basesrc)) {
2953 /* now we can start playback */
2954 gst_base_src_set_playing (basesrc, TRUE);
2962 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2963 transition)) == GST_STATE_CHANGE_FAILURE)
2966 switch (transition) {
2967 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2968 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
2969 if (gst_base_src_is_live (basesrc)) {
2970 /* make sure we block in the live lock in PAUSED */
2971 gst_base_src_set_playing (basesrc, FALSE);
2975 case GST_STATE_CHANGE_PAUSED_TO_READY:
2977 GstEvent **event_p, *event;
2979 /* we don't need to unblock anything here, the pad deactivation code
2980 * already did this */
2982 /* FIXME, deprecate this behaviour, it is very dangerous.
2983 * the prefered way of sending EOS downstream is by sending
2984 * the EOS event to the element */
2985 if (!basesrc->priv->last_sent_eos) {
2986 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2987 event = gst_event_new_eos ();
2988 gst_event_set_seqnum (event, basesrc->priv->seqnum);
2989 gst_pad_push_event (basesrc->srcpad, event);
2990 basesrc->priv->last_sent_eos = TRUE;
2992 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
2993 event_p = &basesrc->data.ABI.pending_seek;
2994 gst_event_replace (event_p, NULL);
2995 event_p = &basesrc->priv->close_segment;
2996 gst_event_replace (event_p, NULL);
2997 event_p = &basesrc->priv->start_segment;
2998 gst_event_replace (event_p, NULL);
3001 case GST_STATE_CHANGE_READY_TO_NULL:
3007 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3008 result = GST_STATE_CHANGE_NO_PREROLL;
3015 GST_DEBUG_OBJECT (basesrc, "parent failed state change");