2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Base class for getrange based source elements
26 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
28 * This is a generice base class for source elements. The following
29 * types of sources are supported:
31 * <listitem><para>random access sources like files</para></listitem>
32 * <listitem><para>seekable sources</para></listitem>
33 * <listitem><para>live sources</para></listitem>
36 * The source can be configured to operate in any #GstFormat with the
37 * gst_base_src_set_format() method. The currently set format determines
38 * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT
39 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
41 * #GstBaseSrc always supports push mode scheduling. If the following
42 * conditions are met, it also supports pull mode scheduling:
44 * <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
46 * <listitem><para>#GstBaseSrcClass.is_seekable() returns %TRUE.</para>
50 * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any time
51 * by overriding #GstBaseSrcClass.check_get_range() so that it returns %TRUE.
53 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
54 * automatically seekable in push mode as well. The following conditions must
55 * be met to make the element seekable in push mode when the format is not
59 * #GstBaseSrcClass.is_seekable() returns %TRUE.
62 * #GstBaseSrcClass.query() can convert all supported seek formats to the
63 * internal format as set with gst_base_src_set_format().
66 * #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
71 * When the element does not meet the requirements to operate in pull mode, the
72 * offset and length in the #GstBaseSrcClass.create() method should be ignored.
73 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
74 * element can operate in pull mode but only with specific offsets and
75 * lengths, it is allowed to generate an error when the wrong values are passed
76 * to the #GstBaseSrcClass.create() function.
78 * #GstBaseSrc has support for live sources. Live sources are sources that when
79 * paused discard data, such as audio or video capture devices. A typical live
80 * source also produces data at a fixed rate and thus provides a clock to publish
82 * Use gst_base_src_set_live() to activate the live source mode.
84 * A live source does not produce data in the PAUSED state. This means that the
85 * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
86 * PLAYING. To signal the pipeline that the element will not produce data, the
87 * return value from the READY to PAUSED state will be
88 * #GST_STATE_CHANGE_NO_PREROLL.
90 * A typical live source will timestamp the buffers it creates with the
91 * current running time of the pipeline. This is one reason why a live source
92 * can only produce data in the PLAYING state, when the clock is actually
93 * distributed and running.
95 * Live sources that synchronize and block on the clock (an audio source, for
96 * example) can since 0.10.12 use gst_base_src_wait_playing() when the
97 * #GstBaseSrcClass.create() function was interrupted by a state change to
100 * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
101 * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
102 * function if the source is a live source. The #GstBaseSrcClass.get_times()
103 * function should return timestamps starting from 0, as if it were a non-live
104 * source. The base class will make sure that the timestamps are transformed
105 * into the current running_time. The base source will then wait for the
106 * calculated running_time before pushing out the buffer.
108 * For live sources, the base class will by default report a latency of 0.
109 * For pseudo live sources, the base class will by default measure the difference
110 * between the first buffer timestamp and the start time of get_times and will
111 * report this value as the latency.
112 * Subclasses should override the query function when this behaviour is not
115 * There is only support in #GstBaseSrc for exactly one source pad, which
116 * should be named "src". A source implementation (subclass of #GstBaseSrc)
117 * should install a pad template in its class_init function, like so:
120 * my_element_class_init (GstMyElementClass *klass)
122 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
123 * // srctemplate should be a #GstStaticPadTemplate with direction
124 * // #GST_PAD_SRC and name "src"
125 * gst_element_class_add_pad_template (gstelement_class,
126 * gst_static_pad_template_get (&srctemplate));
127 * // see #GstElementDetails
128 * gst_element_class_set_details (gstelement_class, &details);
133 * <title>Controlled shutdown of live sources in applications</title>
135 * Applications that record from a live source may want to stop recording
136 * in a controlled way, so that the recording is stopped, but the data
137 * already in the pipeline is processed to the end (remember that many live
138 * sources would go on recording forever otherwise). For that to happen the
139 * application needs to make the source stop recording and send an EOS
140 * event down the pipeline. The application would then wait for an
141 * EOS message posted on the pipeline's bus to know when all data has
142 * been processed and the pipeline can safely be stopped.
144 * Since GStreamer 0.10.16 an application may send an EOS event to a source
145 * element to make it perform the EOS logic (send EOS event downstream or post a
146 * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
147 * with the gst_element_send_event() function on the element or its parent bin.
149 * After the EOS has been sent to the element, the application should wait for
150 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
151 * received, it may safely shut down the entire pipeline.
153 * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
154 * is still available but deprecated as it is dangerous and less flexible.
156 * Last reviewed on 2007-12-19 (0.10.16)
168 #include <gst/gst_private.h>
169 #include <gst/glib-compat-private.h>
171 #include "gstbasesrc.h"
172 #include "gsttypefindhelper.h"
173 #include <gst/gstmarshal.h>
174 #include <gst/gst-i18n-lib.h>
176 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
177 #define GST_CAT_DEFAULT gst_base_src_debug
179 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
180 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
181 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
182 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
183 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
184 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
185 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
187 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
188 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
190 /* BaseSrc signals and args */
197 #define DEFAULT_BLOCKSIZE 4096
198 #define DEFAULT_NUM_BUFFERS -1
199 #define DEFAULT_TYPEFIND FALSE
200 #define DEFAULT_DO_TIMESTAMP FALSE
211 #define GST_BASE_SRC_GET_PRIVATE(obj) \
212 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
214 struct _GstBaseSrcPrivate
216 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
217 * to avoid the sending of two EOS in some cases) */
221 /* two segments to be sent in the streaming thread with STREAM_LOCK */
222 GstEvent *close_segment;
223 GstEvent *start_segment;
224 gboolean newsegment_pending;
226 /* if EOS is pending (atomic) */
229 /* startup latency is the time it takes between going to PLAYING and producing
230 * the first BUFFER with running_time 0. This value is included in the latency
232 GstClockTime latency;
233 /* timestamp offset, this is the offset add to the values of gst_times for
234 * pseudo live sources */
235 GstClockTimeDiff ts_offset;
237 gboolean do_timestamp;
238 volatile gint dynamic_size;
240 /* stream sequence number */
243 /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
244 * pushed in the data stream */
245 GList *pending_events;
246 volatile gint have_events;
248 /* QoS *//* with LOCK */
249 gboolean qos_enabled;
251 GstClockTime earliest_time;
254 static GstElementClass *parent_class = NULL;
256 static void gst_base_src_base_init (gpointer g_class);
257 static void gst_base_src_class_init (GstBaseSrcClass * klass);
258 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
259 static void gst_base_src_finalize (GObject * object);
263 gst_base_src_get_type (void)
265 static volatile gsize base_src_type = 0;
267 if (g_once_init_enter (&base_src_type)) {
269 static const GTypeInfo base_src_info = {
270 sizeof (GstBaseSrcClass),
271 (GBaseInitFunc) gst_base_src_base_init,
273 (GClassInitFunc) gst_base_src_class_init,
278 (GInstanceInitFunc) gst_base_src_init,
281 _type = g_type_register_static (GST_TYPE_ELEMENT,
282 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
283 g_once_init_leave (&base_src_type, _type);
285 return base_src_type;
288 static GstCaps *gst_base_src_getcaps (GstPad * pad);
289 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
290 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
292 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
293 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
294 static void gst_base_src_set_property (GObject * object, guint prop_id,
295 const GValue * value, GParamSpec * pspec);
296 static void gst_base_src_get_property (GObject * object, guint prop_id,
297 GValue * value, GParamSpec * pspec);
298 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
299 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
300 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
301 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
303 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
305 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
306 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
307 GstSegment * segment);
308 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
309 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
310 GstEvent * event, GstSegment * segment);
312 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
313 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
314 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
315 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
317 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
318 GstStateChange transition);
320 static void gst_base_src_loop (GstPad * pad);
321 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
322 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
323 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
324 guint length, GstBuffer ** buf);
325 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
326 guint length, GstBuffer ** buf);
327 static gboolean gst_base_src_seekable (GstBaseSrc * src);
328 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
332 gst_base_src_base_init (gpointer g_class)
334 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
338 gst_base_src_class_init (GstBaseSrcClass * klass)
340 GObjectClass *gobject_class;
341 GstElementClass *gstelement_class;
343 gobject_class = G_OBJECT_CLASS (klass);
344 gstelement_class = GST_ELEMENT_CLASS (klass);
346 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
348 parent_class = g_type_class_peek_parent (klass);
350 gobject_class->finalize = gst_base_src_finalize;
351 gobject_class->set_property = gst_base_src_set_property;
352 gobject_class->get_property = gst_base_src_get_property;
354 /* FIXME 0.11: blocksize property should be int, not ulong (min is >max here) */
355 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
356 g_param_spec_ulong ("blocksize", "Block size",
357 "Size in bytes to read per buffer (-1 = default)", 0, G_MAXULONG,
358 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
360 g_param_spec_int ("num-buffers", "num-buffers",
361 "Number of buffers to output before sending EOS (-1 = unlimited)",
362 -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
363 G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
365 g_param_spec_boolean ("typefind", "Typefind",
366 "Run typefind before negotiating", DEFAULT_TYPEFIND,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
369 g_param_spec_boolean ("do-timestamp", "Do timestamp",
370 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 gstelement_class->change_state =
374 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
375 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
376 gstelement_class->get_query_types =
377 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
379 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
380 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
381 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
382 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
383 klass->check_get_range =
384 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
385 klass->prepare_seek_segment =
386 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
388 /* Registering debug symbols for function pointers */
389 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_push);
390 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_pull);
391 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event_handler);
392 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
393 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_get_range);
394 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_check_get_range);
395 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getcaps);
396 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_setcaps);
397 GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
401 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
404 GstPadTemplate *pad_template;
406 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
408 basesrc->is_live = FALSE;
409 basesrc->live_lock = g_mutex_new ();
410 basesrc->live_cond = g_cond_new ();
411 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
412 basesrc->num_buffers_left = -1;
414 basesrc->can_activate_push = TRUE;
415 basesrc->pad_mode = GST_ACTIVATE_NONE;
418 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
419 g_return_if_fail (pad_template != NULL);
421 GST_DEBUG_OBJECT (basesrc, "creating src pad");
422 pad = gst_pad_new_from_template (pad_template, "src");
424 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
425 gst_pad_set_activatepush_function (pad, gst_base_src_activate_push);
426 gst_pad_set_activatepull_function (pad, gst_base_src_activate_pull);
427 gst_pad_set_event_function (pad, gst_base_src_event_handler);
428 gst_pad_set_query_function (pad, gst_base_src_query);
429 gst_pad_set_checkgetrange_function (pad, gst_base_src_pad_check_get_range);
430 gst_pad_set_getrange_function (pad, gst_base_src_pad_get_range);
431 gst_pad_set_getcaps_function (pad, gst_base_src_getcaps);
432 gst_pad_set_setcaps_function (pad, gst_base_src_setcaps);
433 gst_pad_set_fixatecaps_function (pad, gst_base_src_fixate);
435 /* hold pointer to pad */
436 basesrc->srcpad = pad;
437 GST_DEBUG_OBJECT (basesrc, "adding src pad");
438 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
440 basesrc->blocksize = DEFAULT_BLOCKSIZE;
441 basesrc->clock_id = NULL;
442 /* we operate in BYTES by default */
443 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
444 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
445 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
446 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
448 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
449 GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_IS_SOURCE);
451 GST_DEBUG_OBJECT (basesrc, "init done");
455 gst_base_src_finalize (GObject * object)
460 basesrc = GST_BASE_SRC (object);
462 g_mutex_free (basesrc->live_lock);
463 g_cond_free (basesrc->live_cond);
465 event_p = &basesrc->data.ABI.pending_seek;
466 gst_event_replace (event_p, NULL);
468 if (basesrc->priv->pending_events) {
469 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
471 g_list_free (basesrc->priv->pending_events);
474 G_OBJECT_CLASS (parent_class)->finalize (object);
478 * gst_base_src_wait_playing:
481 * If the #GstBaseSrcClass.create() method performs its own synchronisation
482 * against the clock it must unblock when going from PLAYING to the PAUSED state
483 * and call this method before continuing to produce the remaining data.
485 * This function will block until a state change to PLAYING happens (in which
486 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
487 * to a state change to READY or a FLUSH event (in which case this function
488 * returns #GST_FLOW_WRONG_STATE).
492 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
493 * continue. Any other return value should be returned from the create vmethod.
496 gst_base_src_wait_playing (GstBaseSrc * src)
498 g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
501 /* block until the state changes, or we get a flush, or something */
502 GST_DEBUG_OBJECT (src, "live source waiting for running state");
504 GST_DEBUG_OBJECT (src, "live source unlocked");
505 if (src->priv->flushing)
507 } while (G_UNLIKELY (!src->live_running));
514 GST_DEBUG_OBJECT (src, "we are flushing");
515 return GST_FLOW_WRONG_STATE;
520 * gst_base_src_set_live:
521 * @src: base source instance
522 * @live: new live-mode
524 * If the element listens to a live source, @live should
527 * A live source will not produce data in the PAUSED state and
528 * will therefore not be able to participate in the PREROLL phase
529 * of a pipeline. To signal this fact to the application and the
530 * pipeline, the state change return value of the live source will
531 * be GST_STATE_CHANGE_NO_PREROLL.
534 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
536 g_return_if_fail (GST_IS_BASE_SRC (src));
538 GST_OBJECT_LOCK (src);
540 GST_OBJECT_UNLOCK (src);
544 * gst_base_src_is_live:
545 * @src: base source instance
547 * Check if an element is in live mode.
549 * Returns: %TRUE if element is in live mode.
552 gst_base_src_is_live (GstBaseSrc * src)
556 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
558 GST_OBJECT_LOCK (src);
559 result = src->is_live;
560 GST_OBJECT_UNLOCK (src);
566 * gst_base_src_set_format:
567 * @src: base source instance
568 * @format: the format to use
570 * Sets the default format of the source. This will be the format used
571 * for sending NEW_SEGMENT events and for performing seeks.
573 * If a format of GST_FORMAT_BYTES is set, the element will be able to
574 * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
576 * This function must only be called in states < %GST_STATE_PAUSED.
581 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
583 g_return_if_fail (GST_IS_BASE_SRC (src));
584 g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
586 GST_OBJECT_LOCK (src);
587 gst_segment_init (&src->segment, format);
588 GST_OBJECT_UNLOCK (src);
592 * gst_base_src_set_dynamic_size:
593 * @src: base source instance
594 * @dynamic: new dynamic size mode
596 * If not @dynamic, size is only updated when needed, such as when trying to
597 * read past current tracked size. Otherwise, size is checked for upon each
603 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
605 g_return_if_fail (GST_IS_BASE_SRC (src));
607 g_atomic_int_set (&src->priv->dynamic_size, dynamic);
611 * gst_base_src_query_latency:
613 * @live: (out) (allow-none): if the source is live
614 * @min_latency: (out) (allow-none): the min latency of the source
615 * @max_latency: (out) (allow-none): the max latency of the source
617 * Query the source for the latency parameters. @live will be TRUE when @src is
618 * configured as a live source. @min_latency will be set to the difference
619 * between the running time and the timestamp of the first buffer.
620 * @max_latency is always the undefined value of -1.
622 * This function is mostly used by subclasses.
624 * Returns: TRUE if the query succeeded.
629 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
630 GstClockTime * min_latency, GstClockTime * max_latency)
634 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
636 GST_OBJECT_LOCK (src);
638 *live = src->is_live;
640 /* if we have a startup latency, report this one, else report 0. Subclasses
641 * are supposed to override the query function if they want something
643 if (src->priv->latency != -1)
644 min = src->priv->latency;
653 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
654 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
656 GST_OBJECT_UNLOCK (src);
662 * gst_base_src_set_blocksize:
664 * @blocksize: the new blocksize in bytes
666 * Set the number of bytes that @src will push out with each buffer. When
667 * @blocksize is set to -1, a default length will be used.
671 /* FIXME 0.11: blocksize property should be int, not ulong */
673 gst_base_src_set_blocksize (GstBaseSrc * src, gulong blocksize)
675 g_return_if_fail (GST_IS_BASE_SRC (src));
677 GST_OBJECT_LOCK (src);
678 src->blocksize = blocksize;
679 GST_OBJECT_UNLOCK (src);
683 * gst_base_src_get_blocksize:
686 * Get the number of bytes that @src will push out with each buffer.
688 * Returns: the number of bytes pushed with each buffer.
692 /* FIXME 0.11: blocksize property should be int, not ulong */
694 gst_base_src_get_blocksize (GstBaseSrc * src)
698 g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
700 GST_OBJECT_LOCK (src);
701 res = src->blocksize;
702 GST_OBJECT_UNLOCK (src);
709 * gst_base_src_set_do_timestamp:
711 * @timestamp: enable or disable timestamping
713 * Configure @src to automatically timestamp outgoing buffers based on the
714 * current running_time of the pipeline. This property is mostly useful for live
720 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
722 g_return_if_fail (GST_IS_BASE_SRC (src));
724 GST_OBJECT_LOCK (src);
725 src->priv->do_timestamp = timestamp;
726 GST_OBJECT_UNLOCK (src);
730 * gst_base_src_get_do_timestamp:
733 * Query if @src timestamps outgoing buffers based on the current running_time.
735 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
740 gst_base_src_get_do_timestamp (GstBaseSrc * src)
744 g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
746 GST_OBJECT_LOCK (src);
747 res = src->priv->do_timestamp;
748 GST_OBJECT_UNLOCK (src);
754 * gst_base_src_new_seamless_segment:
756 * @start: The new start value for the segment
757 * @stop: Stop value for the new segment
758 * @position: The position value for the new segent
760 * Prepare a new seamless segment for emission downstream. This function must
761 * only be called by derived sub-classes, and only from the create() function,
762 * as the stream-lock needs to be held.
764 * The format for the new segment will be the current format of the source, as
765 * configured with gst_base_src_set_format()
767 * Returns: %TRUE if preparation of the seamless segment succeeded.
772 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
777 GST_DEBUG_OBJECT (src,
778 "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
779 GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
780 GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
782 GST_OBJECT_LOCK (src);
783 if (src->data.ABI.running && !src->priv->newsegment_pending) {
784 if (src->priv->close_segment)
785 gst_event_unref (src->priv->close_segment);
786 src->priv->close_segment =
787 gst_event_new_new_segment_full (TRUE,
788 src->segment.rate, src->segment.applied_rate, src->segment.format,
789 src->segment.start, src->segment.last_stop, src->segment.time);
792 gst_segment_set_newsegment_full (&src->segment, FALSE, src->segment.rate,
793 src->segment.applied_rate, src->segment.format, start, stop, position);
795 if (src->priv->start_segment)
796 gst_event_unref (src->priv->start_segment);
797 if (src->segment.rate >= 0.0) {
798 /* forward, we send data from last_stop to stop */
799 src->priv->start_segment =
800 gst_event_new_new_segment_full (FALSE,
801 src->segment.rate, src->segment.applied_rate, src->segment.format,
802 src->segment.last_stop, stop, src->segment.time);
804 /* reverse, we send data from last_stop to start */
805 src->priv->start_segment =
806 gst_event_new_new_segment_full (FALSE,
807 src->segment.rate, src->segment.applied_rate, src->segment.format,
808 src->segment.start, src->segment.last_stop, src->segment.time);
810 GST_OBJECT_UNLOCK (src);
812 src->priv->discont = TRUE;
813 src->data.ABI.running = TRUE;
819 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
821 GstBaseSrcClass *bclass;
825 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
826 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
828 if (bclass->set_caps)
829 res = bclass->set_caps (bsrc, caps);
835 gst_base_src_getcaps (GstPad * pad)
837 GstBaseSrcClass *bclass;
839 GstCaps *caps = NULL;
841 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
842 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
843 if (bclass->get_caps)
844 caps = bclass->get_caps (bsrc);
847 GstPadTemplate *pad_template;
850 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
851 if (pad_template != NULL) {
852 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
859 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
861 GstBaseSrcClass *bclass;
864 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
865 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
868 bclass->fixate (bsrc, caps);
870 gst_object_unref (bsrc);
874 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
878 switch (GST_QUERY_TYPE (query)) {
879 case GST_QUERY_POSITION:
883 gst_query_parse_position (query, &format, NULL);
885 GST_DEBUG_OBJECT (src, "position query in format %s",
886 gst_format_get_name (format));
889 case GST_FORMAT_PERCENT:
895 GST_OBJECT_LOCK (src);
896 position = src->segment.last_stop;
897 duration = src->segment.duration;
898 GST_OBJECT_UNLOCK (src);
900 if (position != -1 && duration != -1) {
901 if (position < duration)
902 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
905 percent = GST_FORMAT_PERCENT_MAX;
909 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
916 GstFormat seg_format;
918 GST_OBJECT_LOCK (src);
920 gst_segment_to_stream_time (&src->segment, src->segment.format,
921 src->segment.last_stop);
922 seg_format = src->segment.format;
923 GST_OBJECT_UNLOCK (src);
925 if (position != -1) {
926 /* convert to requested format */
928 gst_pad_query_convert (src->srcpad, seg_format,
929 position, &format, &position);
933 gst_query_set_position (query, format, position);
939 case GST_QUERY_DURATION:
943 gst_query_parse_duration (query, &format, NULL);
945 GST_DEBUG_OBJECT (src, "duration query in format %s",
946 gst_format_get_name (format));
949 case GST_FORMAT_PERCENT:
950 gst_query_set_duration (query, GST_FORMAT_PERCENT,
951 GST_FORMAT_PERCENT_MAX);
957 GstFormat seg_format;
960 /* may have to refresh duration */
961 if (g_atomic_int_get (&src->priv->dynamic_size))
962 gst_base_src_update_length (src, 0, &length);
964 /* this is the duration as configured by the subclass. */
965 GST_OBJECT_LOCK (src);
966 duration = src->segment.duration;
967 seg_format = src->segment.format;
968 GST_OBJECT_UNLOCK (src);
970 GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
971 duration, gst_format_get_name (seg_format));
973 if (duration != -1) {
974 /* convert to requested format, if this fails, we have a duration
975 * but we cannot answer the query, we must return FALSE. */
977 gst_pad_query_convert (src->srcpad, seg_format,
978 duration, &format, &duration);
980 /* The subclass did not configure a duration, we assume that the
981 * media has an unknown duration then and we return TRUE to report
982 * this. Note that this is not the same as returning FALSE, which
983 * means that we cannot report the duration at all. */
986 gst_query_set_duration (query, format, duration);
993 case GST_QUERY_SEEKING:
995 GstFormat format, seg_format;
998 GST_OBJECT_LOCK (src);
999 duration = src->segment.duration;
1000 seg_format = src->segment.format;
1001 GST_OBJECT_UNLOCK (src);
1003 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1004 if (format == seg_format) {
1005 gst_query_set_seeking (query, seg_format,
1006 gst_base_src_seekable (src), 0, duration);
1009 /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1010 /* Don't reply to the query to make up for demuxers which don't
1011 * handle the SEEKING query yet. Players like Totem will fall back
1012 * to the duration when the SEEKING query isn't answered. */
1017 case GST_QUERY_SEGMENT:
1021 GST_OBJECT_LOCK (src);
1022 /* no end segment configured, current duration then */
1023 if ((stop = src->segment.stop) == -1)
1024 stop = src->segment.duration;
1025 start = src->segment.start;
1027 /* adjust to stream time */
1028 if (src->segment.time != -1) {
1029 start -= src->segment.time;
1031 stop -= src->segment.time;
1034 gst_query_set_segment (query, src->segment.rate, src->segment.format,
1036 GST_OBJECT_UNLOCK (src);
1041 case GST_QUERY_FORMATS:
1043 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1044 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1048 case GST_QUERY_CONVERT:
1050 GstFormat src_fmt, dest_fmt;
1051 gint64 src_val, dest_val;
1053 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1055 /* we can only convert between equal formats... */
1056 if (src_fmt == dest_fmt) {
1062 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1065 case GST_QUERY_LATENCY:
1067 GstClockTime min, max;
1070 /* Subclasses should override and implement something useful */
1071 res = gst_base_src_query_latency (src, &live, &min, &max);
1073 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1074 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1075 GST_TIME_ARGS (max));
1077 gst_query_set_latency (query, live, min, max);
1080 case GST_QUERY_JITTER:
1081 case GST_QUERY_RATE:
1084 case GST_QUERY_BUFFERING:
1086 GstFormat format, seg_format;
1087 gint64 start, stop, estimated;
1089 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1091 GST_DEBUG_OBJECT (src, "buffering query in format %s",
1092 gst_format_get_name (format));
1094 GST_OBJECT_LOCK (src);
1095 if (src->random_access) {
1098 if (format == GST_FORMAT_PERCENT)
1099 stop = GST_FORMAT_PERCENT_MAX;
1101 stop = src->segment.duration;
1107 seg_format = src->segment.format;
1108 GST_OBJECT_UNLOCK (src);
1110 /* convert to required format. When the conversion fails, we can't answer
1111 * the query. When the value is unknown, we can don't perform conversion
1112 * but report TRUE. */
1113 if (format != GST_FORMAT_PERCENT && stop != -1) {
1114 res = gst_pad_query_convert (src->srcpad, seg_format,
1115 stop, &format, &stop);
1119 if (res && format != GST_FORMAT_PERCENT && start != -1)
1120 res = gst_pad_query_convert (src->srcpad, seg_format,
1121 start, &format, &start);
1123 gst_query_set_buffering_range (query, format, start, stop, estimated);
1130 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1136 gst_base_src_query (GstPad * pad, GstQuery * query)
1139 GstBaseSrcClass *bclass;
1140 gboolean result = FALSE;
1142 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1143 if (G_UNLIKELY (src == NULL))
1146 bclass = GST_BASE_SRC_GET_CLASS (src);
1149 result = bclass->query (src, query);
1151 result = gst_pad_query_default (pad, query);
1153 gst_object_unref (src);
1159 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1161 gboolean res = TRUE;
1163 /* update our offset if the start/stop position was updated */
1164 if (segment->format == GST_FORMAT_BYTES) {
1165 segment->time = segment->start;
1166 } else if (segment->start == 0) {
1167 /* seek to start, we can implement a default for this. */
1171 GST_INFO_OBJECT (src, "Can't do a default seek");
1178 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1180 GstBaseSrcClass *bclass;
1181 gboolean result = FALSE;
1183 bclass = GST_BASE_SRC_GET_CLASS (src);
1185 if (bclass->do_seek)
1186 result = bclass->do_seek (src, segment);
1191 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1194 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1195 GstSegment * segment)
1197 /* By default, we try one of 2 things:
1198 * - For absolute seek positions, convert the requested position to our
1199 * configured processing format and place it in the output segment \
1200 * - For relative seek positions, convert our current (input) values to the
1201 * seek format, adjust by the relative seek offset and then convert back to
1202 * the processing format
1204 GstSeekType cur_type, stop_type;
1207 GstFormat seek_format, dest_format;
1210 gboolean res = TRUE;
1212 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1213 &cur_type, &cur, &stop_type, &stop);
1214 dest_format = segment->format;
1216 if (seek_format == dest_format) {
1217 gst_segment_set_seek (segment, rate, seek_format, flags,
1218 cur_type, cur, stop_type, stop, &update);
1222 if (cur_type != GST_SEEK_TYPE_NONE) {
1223 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1225 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
1227 cur_type = GST_SEEK_TYPE_SET;
1230 if (res && stop_type != GST_SEEK_TYPE_NONE) {
1231 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
1233 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
1235 stop_type = GST_SEEK_TYPE_SET;
1238 /* And finally, configure our output segment in the desired format */
1239 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
1240 stop_type, stop, &update);
1249 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1255 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1256 GstSegment * seeksegment)
1258 GstBaseSrcClass *bclass;
1259 gboolean result = FALSE;
1261 bclass = GST_BASE_SRC_GET_CLASS (src);
1263 if (bclass->prepare_seek_segment)
1264 result = bclass->prepare_seek_segment (src, event, seeksegment);
1269 /* this code implements the seeking. It is a good example
1270 * handling all cases.
1272 * A seek updates the currently configured segment.start
1273 * and segment.stop values based on the SEEK_TYPE. If the
1274 * segment.start value is updated, a seek to this new position
1275 * should be performed.
1277 * The seek can only be executed when we are not currently
1278 * streaming any data, to make sure that this is the case, we
1279 * acquire the STREAM_LOCK which is taken when we are in the
1280 * _loop() function or when a getrange() is called. Normally
1281 * we will not receive a seek if we are operating in pull mode
1282 * though. When we operate as a live source we might block on the live
1283 * cond, which does not release the STREAM_LOCK. Therefore we will try
1284 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1285 * safe to perform the seek.
1287 * When we are in the loop() function, we might be in the middle
1288 * of pushing a buffer, which might block in a sink. To make sure
1289 * that the push gets unblocked we push out a FLUSH_START event.
1290 * Our loop function will get a WRONG_STATE return value from
1291 * the push and will pause, effectively releasing the STREAM_LOCK.
1293 * For a non-flushing seek, we pause the task, which might eventually
1294 * release the STREAM_LOCK. We say eventually because when the sink
1295 * blocks on the sample we might wait a very long time until the sink
1296 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1297 * can continue the seek. A non-flushing seek is normally done in a
1298 * running pipeline to perform seamless playback, this means that the sink is
1299 * PLAYING and will return from its chain function.
1300 * In the case of a non-flushing seek we need to make sure that the
1301 * data we output after the seek is continuous with the previous data,
1302 * this is because a non-flushing seek does not reset the running-time
1303 * to 0. We do this by closing the currently running segment, ie. sending
1304 * a new_segment event with the stop position set to the last processed
1307 * After updating the segment.start/stop values, we prepare for
1308 * streaming again. We push out a FLUSH_STOP to make the peer pad
1309 * accept data again and we start our task again.
1311 * A segment seek posts a message on the bus saying that the playback
1312 * of the segment started. We store the segment flag internally because
1313 * when we reach the segment.stop we have to post a segment.done
1314 * instead of EOS when doing a segment seek.
1316 /* FIXME (0.11), we have the unlock gboolean here because most current
1317 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1318 * the streaming thread isn't running, resulting in bogus unlocks later when it
1319 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1320 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1324 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1326 gboolean res = TRUE, tres;
1328 GstFormat seek_format, dest_format;
1330 GstSeekType cur_type, stop_type;
1332 gboolean flush, playing;
1334 gboolean relative_seek = FALSE;
1335 gboolean seekseg_configured = FALSE;
1336 GstSegment seeksegment;
1340 GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1342 GST_OBJECT_LOCK (src);
1343 dest_format = src->segment.format;
1344 GST_OBJECT_UNLOCK (src);
1347 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1348 &cur_type, &cur, &stop_type, &stop);
1350 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1351 SEEK_TYPE_IS_RELATIVE (stop_type);
1353 if (dest_format != seek_format && !relative_seek) {
1354 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1355 * here before taking the stream lock, otherwise we must convert it later,
1356 * once we have the stream lock and can read the last configures segment
1357 * start and stop positions */
1358 gst_segment_init (&seeksegment, dest_format);
1360 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1361 goto prepare_failed;
1363 seekseg_configured = TRUE;
1366 flush = flags & GST_SEEK_FLAG_FLUSH;
1367 seqnum = gst_event_get_seqnum (event);
1370 /* get next seqnum */
1371 seqnum = gst_util_seqnum_next ();
1374 /* send flush start */
1376 tevent = gst_event_new_flush_start ();
1377 gst_event_set_seqnum (tevent, seqnum);
1378 gst_pad_push_event (src->srcpad, tevent);
1380 gst_pad_pause_task (src->srcpad);
1382 /* unblock streaming thread. */
1383 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1385 /* grab streaming lock, this should eventually be possible, either
1386 * because the task is paused, our streaming thread stopped
1387 * or because our peer is flushing. */
1388 GST_PAD_STREAM_LOCK (src->srcpad);
1389 if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1390 /* we have seen this event before, issue a warning for now */
1391 GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1394 src->priv->seqnum = seqnum;
1395 GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1398 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1400 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1401 * copy the current segment info into the temp segment that we can actually
1402 * attempt the seek with. We only update the real segment if the seek succeeds. */
1403 if (!seekseg_configured) {
1404 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1406 /* now configure the final seek segment */
1408 if (seeksegment.format != seek_format) {
1409 /* OK, here's where we give the subclass a chance to convert the relative
1410 * seek into an absolute one in the processing format. We set up any
1411 * absolute seek above, before taking the stream lock. */
1412 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1413 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1418 /* The seek format matches our processing format, no need to ask the
1419 * the subclass to configure the segment. */
1420 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1421 cur_type, cur, stop_type, stop, &update);
1424 /* Else, no seek event passed, so we're just (re)starting the
1429 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1430 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1431 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1433 /* do the seek, segment.last_stop contains the new position. */
1434 res = gst_base_src_do_seek (src, &seeksegment);
1437 /* and prepare to continue streaming */
1439 tevent = gst_event_new_flush_stop ();
1440 gst_event_set_seqnum (tevent, seqnum);
1441 /* send flush stop, peer will accept data and events again. We
1442 * are not yet providing data as we still have the STREAM_LOCK. */
1443 gst_pad_push_event (src->srcpad, tevent);
1444 } else if (res && src->data.ABI.running) {
1445 /* we are running the current segment and doing a non-flushing seek,
1446 * close the segment first based on the last_stop. */
1447 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1448 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1450 /* queue the segment for sending in the stream thread */
1451 if (src->priv->close_segment)
1452 gst_event_unref (src->priv->close_segment);
1453 src->priv->close_segment =
1454 gst_event_new_new_segment_full (TRUE,
1455 src->segment.rate, src->segment.applied_rate, src->segment.format,
1456 src->segment.start, src->segment.last_stop, src->segment.time);
1457 seeksegment.time = gst_segment_to_stream_time (&src->segment,
1458 src->segment.format, src->segment.last_stop);
1459 seeksegment.start = src->segment.last_stop;
1460 gst_event_set_seqnum (src->priv->close_segment, seqnum);
1463 /* The subclass must have converted the segment to the processing format
1465 if (res && seeksegment.format != dest_format) {
1466 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1467 "in the correct format. Aborting seek.");
1471 /* if the seek was successful, we update our real segment and push
1472 * out the new segment. */
1474 GST_OBJECT_LOCK (src);
1475 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1476 GST_OBJECT_UNLOCK (src);
1478 if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
1479 GstMessage *message;
1481 message = gst_message_new_segment_start (GST_OBJECT (src),
1482 seeksegment.format, seeksegment.last_stop);
1483 gst_message_set_seqnum (message, seqnum);
1485 gst_element_post_message (GST_ELEMENT (src), message);
1488 /* for deriving a stop position for the playback segment from the seek
1489 * segment, we must take the duration when the stop is not set */
1490 if ((stop = seeksegment.stop) == -1)
1491 stop = seeksegment.duration;
1493 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1494 " to %" G_GINT64_FORMAT, seeksegment.start, stop);
1496 /* now replace the old segment so that we send it in the stream thread the
1497 * next time it is scheduled. */
1498 if (src->priv->start_segment)
1499 gst_event_unref (src->priv->start_segment);
1500 if (seeksegment.rate >= 0.0) {
1501 /* forward, we send data from last_stop to stop */
1502 src->priv->start_segment =
1503 gst_event_new_new_segment_full (FALSE,
1504 seeksegment.rate, seeksegment.applied_rate, seeksegment.format,
1505 seeksegment.last_stop, stop, seeksegment.time);
1507 /* reverse, we send data from last_stop to start */
1508 src->priv->start_segment =
1509 gst_event_new_new_segment_full (FALSE,
1510 seeksegment.rate, seeksegment.applied_rate, seeksegment.format,
1511 seeksegment.start, seeksegment.last_stop, seeksegment.time);
1513 gst_event_set_seqnum (src->priv->start_segment, seqnum);
1514 src->priv->newsegment_pending = TRUE;
1517 src->priv->discont = TRUE;
1518 src->data.ABI.running = TRUE;
1519 /* and restart the task in case it got paused explicitly or by
1520 * the FLUSH_START event we pushed out. */
1521 tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1526 /* and release the lock again so we can continue streaming */
1527 GST_PAD_STREAM_UNLOCK (src->srcpad);
1533 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1538 static const GstQueryType *
1539 gst_base_src_get_query_types (GstElement * element)
1541 static const GstQueryType query_types[] = {
1557 /* all events send to this element directly. This is mainly done from the
1561 gst_base_src_send_event (GstElement * element, GstEvent * event)
1564 gboolean result = FALSE;
1566 src = GST_BASE_SRC (element);
1568 GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1570 switch (GST_EVENT_TYPE (event)) {
1571 /* bidirectional events */
1572 case GST_EVENT_FLUSH_START:
1573 case GST_EVENT_FLUSH_STOP:
1574 /* sending random flushes downstream can break stuff,
1575 * especially sync since all segment info will get flushed */
1578 /* downstream serialized events */
1581 GstBaseSrcClass *bclass;
1583 bclass = GST_BASE_SRC_GET_CLASS (src);
1585 /* queue EOS and make sure the task or pull function performs the EOS
1588 * We have two possibilities:
1590 * - Before we are to enter the _create function, we check the pending_eos
1591 * first and do EOS instead of entering it.
1592 * - If we are in the _create function or we did not manage to set the
1593 * flag fast enough and we are about to enter the _create function,
1594 * we unlock it so that we exit with WRONG_STATE immediately. We then
1595 * check the EOS flag and do the EOS logic.
1597 g_atomic_int_set (&src->priv->pending_eos, TRUE);
1598 GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1600 /* unlock the _create function so that we can check the pending_eos flag
1601 * and we can do EOS. This will eventually release the LIVE_LOCK again so
1602 * that we can grab it and stop the unlock again. We don't take the stream
1603 * lock so that this operation is guaranteed to never block. */
1605 bclass->unlock (src);
1607 GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1609 GST_LIVE_LOCK (src);
1610 GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1611 /* now stop the unlock of the streaming thread again. Grabbing the live
1612 * lock is enough because that protects the create function. */
1613 if (bclass->unlock_stop)
1614 bclass->unlock_stop (src);
1615 GST_LIVE_UNLOCK (src);
1620 case GST_EVENT_NEWSEGMENT:
1621 /* sending random NEWSEGMENT downstream can break sync. */
1624 case GST_EVENT_CUSTOM_DOWNSTREAM:
1625 case GST_EVENT_CUSTOM_BOTH:
1626 /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1627 GST_OBJECT_LOCK (src);
1628 src->priv->pending_events =
1629 g_list_append (src->priv->pending_events, event);
1630 g_atomic_int_set (&src->priv->have_events, TRUE);
1631 GST_OBJECT_UNLOCK (src);
1635 case GST_EVENT_BUFFERSIZE:
1636 /* does not seem to make much sense currently */
1639 /* upstream events */
1641 /* elements should override send_event and do something */
1643 case GST_EVENT_SEEK:
1647 GST_OBJECT_LOCK (src->srcpad);
1648 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1650 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1651 GST_OBJECT_UNLOCK (src->srcpad);
1654 GST_DEBUG_OBJECT (src, "performing seek");
1655 /* when we are running in push mode, we can execute the
1656 * seek right now, we need to unlock. */
1657 result = gst_base_src_perform_seek (src, event, TRUE);
1661 /* else we store the event and execute the seek when we
1663 GST_OBJECT_LOCK (src);
1664 GST_DEBUG_OBJECT (src, "queueing seek");
1665 event_p = &src->data.ABI.pending_seek;
1666 gst_event_replace ((GstEvent **) event_p, event);
1667 GST_OBJECT_UNLOCK (src);
1668 /* assume the seek will work */
1673 case GST_EVENT_NAVIGATION:
1674 /* could make sense for elements that do something with navigation events
1675 * but then they would need to override the send_event function */
1677 case GST_EVENT_LATENCY:
1678 /* does not seem to make sense currently */
1682 case GST_EVENT_CUSTOM_UPSTREAM:
1683 /* override send_event if you want this */
1685 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1686 case GST_EVENT_CUSTOM_BOTH_OOB:
1687 /* insert a random custom event into the pipeline */
1688 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1689 result = gst_pad_push_event (src->srcpad, event);
1690 /* we gave away the ref to the event in the push */
1697 /* if we still have a ref to the event, unref it now */
1699 gst_event_unref (event);
1706 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1707 GST_OBJECT_UNLOCK (src->srcpad);
1714 gst_base_src_seekable (GstBaseSrc * src)
1716 GstBaseSrcClass *bclass;
1717 bclass = GST_BASE_SRC_GET_CLASS (src);
1718 if (bclass->is_seekable)
1719 return bclass->is_seekable (src);
1725 gst_base_src_update_qos (GstBaseSrc * src,
1726 gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1728 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1729 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1730 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1732 GST_OBJECT_LOCK (src);
1733 src->priv->proportion = proportion;
1734 src->priv->earliest_time = timestamp + diff;
1735 GST_OBJECT_UNLOCK (src);
1740 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1744 GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1746 switch (GST_EVENT_TYPE (event)) {
1747 case GST_EVENT_SEEK:
1748 /* is normally called when in push mode */
1749 if (!gst_base_src_seekable (src))
1752 result = gst_base_src_perform_seek (src, event, TRUE);
1754 case GST_EVENT_FLUSH_START:
1755 /* cancel any blocking getrange, is normally called
1756 * when in pull mode. */
1757 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1759 case GST_EVENT_FLUSH_STOP:
1760 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1765 GstClockTimeDiff diff;
1766 GstClockTime timestamp;
1768 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1769 gst_base_src_update_qos (src, proportion, diff, timestamp);
1782 GST_DEBUG_OBJECT (src, "is not seekable");
1788 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1791 GstBaseSrcClass *bclass;
1792 gboolean result = FALSE;
1794 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1795 if (G_UNLIKELY (src == NULL)) {
1796 gst_event_unref (event);
1800 bclass = GST_BASE_SRC_GET_CLASS (src);
1802 if (bclass->event) {
1803 if (!(result = bclass->event (src, event)))
1804 goto subclass_failed;
1808 gst_event_unref (event);
1809 gst_object_unref (src);
1816 GST_DEBUG_OBJECT (src, "subclass refused event");
1822 gst_base_src_set_property (GObject * object, guint prop_id,
1823 const GValue * value, GParamSpec * pspec)
1827 src = GST_BASE_SRC (object);
1830 case PROP_BLOCKSIZE:
1831 gst_base_src_set_blocksize (src, g_value_get_ulong (value));
1833 case PROP_NUM_BUFFERS:
1834 src->num_buffers = g_value_get_int (value);
1837 src->data.ABI.typefind = g_value_get_boolean (value);
1839 case PROP_DO_TIMESTAMP:
1840 gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
1843 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1849 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1854 src = GST_BASE_SRC (object);
1857 case PROP_BLOCKSIZE:
1858 g_value_set_ulong (value, gst_base_src_get_blocksize (src));
1860 case PROP_NUM_BUFFERS:
1861 g_value_set_int (value, src->num_buffers);
1864 g_value_set_boolean (value, src->data.ABI.typefind);
1866 case PROP_DO_TIMESTAMP:
1867 g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
1870 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1875 /* with STREAM_LOCK and LOCK */
1876 static GstClockReturn
1877 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1882 id = gst_clock_new_single_shot_id (clock, time);
1884 basesrc->clock_id = id;
1885 /* release the live lock while waiting */
1886 GST_LIVE_UNLOCK (basesrc);
1888 ret = gst_clock_id_wait (id, NULL);
1890 GST_LIVE_LOCK (basesrc);
1891 gst_clock_id_unref (id);
1892 basesrc->clock_id = NULL;
1897 /* perform synchronisation on a buffer.
1900 static GstClockReturn
1901 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1903 GstClockReturn result;
1904 GstClockTime start, end;
1905 GstBaseSrcClass *bclass;
1906 GstClockTime base_time;
1908 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1909 gboolean do_timestamp, first, pseudo_live;
1911 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1914 if (bclass->get_times)
1915 bclass->get_times (basesrc, buffer, &start, &end);
1917 /* get buffer timestamp */
1918 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1920 /* grab the lock to prepare for clocking and calculate the startup
1922 GST_OBJECT_LOCK (basesrc);
1924 /* if we are asked to sync against the clock we are a pseudo live element */
1925 pseudo_live = (start != -1 && basesrc->is_live);
1926 /* check for the first buffer */
1927 first = (basesrc->priv->latency == -1);
1929 if (timestamp != -1 && pseudo_live) {
1930 GstClockTime latency;
1932 /* we have a timestamp and a sync time, latency is the diff */
1933 if (timestamp <= start)
1934 latency = start - timestamp;
1939 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1940 GST_TIME_ARGS (latency));
1941 /* first time we calculate latency, just configure */
1942 basesrc->priv->latency = latency;
1944 if (basesrc->priv->latency != latency) {
1945 /* we have a new latency, FIXME post latency message */
1946 basesrc->priv->latency = latency;
1947 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1948 GST_TIME_ARGS (latency));
1952 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1953 basesrc->is_live, start != -1);
1954 basesrc->priv->latency = 0;
1957 /* get clock, if no clock, we can't sync or do timestamps */
1958 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1961 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1963 do_timestamp = basesrc->priv->do_timestamp;
1965 /* first buffer, calculate the timestamp offset */
1967 GstClockTime running_time;
1969 now = gst_clock_get_time (clock);
1970 running_time = now - base_time;
1972 GST_LOG_OBJECT (basesrc,
1973 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1974 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1975 GST_TIME_ARGS (running_time));
1977 if (pseudo_live && timestamp != -1) {
1978 /* live source and we need to sync, add startup latency to all timestamps
1979 * to get the real running_time. Live sources should always timestamp
1980 * according to the current running time. */
1981 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1983 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1984 GST_TIME_ARGS (basesrc->priv->ts_offset));
1986 basesrc->priv->ts_offset = 0;
1987 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1990 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1992 timestamp = running_time;
1996 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1998 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1999 GST_TIME_ARGS (timestamp));
2002 /* add the timestamp offset we need for sync */
2003 timestamp += basesrc->priv->ts_offset;
2005 /* not the first buffer, the timestamp is the diff between the clock and
2007 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
2008 now = gst_clock_get_time (clock);
2010 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
2012 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
2013 GST_TIME_ARGS (now - base_time));
2017 /* if we don't have a buffer timestamp, we don't sync */
2018 if (!GST_CLOCK_TIME_IS_VALID (start))
2021 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
2022 /* for pseudo live sources, add our ts_offset to the timestamp */
2023 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
2024 start += basesrc->priv->ts_offset;
2027 GST_LOG_OBJECT (basesrc,
2028 "waiting for clock, base time %" GST_TIME_FORMAT
2029 ", stream_start %" GST_TIME_FORMAT,
2030 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2031 GST_OBJECT_UNLOCK (basesrc);
2033 result = gst_base_src_wait (basesrc, clock, start + base_time);
2035 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2042 GST_DEBUG_OBJECT (basesrc, "we have no clock");
2043 GST_OBJECT_UNLOCK (basesrc);
2044 return GST_CLOCK_OK;
2048 GST_DEBUG_OBJECT (basesrc, "no sync needed");
2049 GST_OBJECT_UNLOCK (basesrc);
2050 return GST_CLOCK_OK;
2054 /* Called with STREAM_LOCK and LIVE_LOCK */
2056 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
2058 guint64 size, maxsize;
2059 GstBaseSrcClass *bclass;
2064 bclass = GST_BASE_SRC_GET_CLASS (src);
2066 format = src->segment.format;
2067 stop = src->segment.stop;
2068 /* get total file size */
2069 size = (guint64) src->segment.duration;
2071 /* only operate if we are working with bytes */
2072 if (format != GST_FORMAT_BYTES)
2075 /* the max amount of bytes to read is the total size or
2076 * up to the segment.stop if present. */
2078 maxsize = MIN (size, stop);
2082 GST_DEBUG_OBJECT (src,
2083 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2084 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2085 *length, size, stop, maxsize);
2087 dynamic = g_atomic_int_get (&src->priv->dynamic_size);
2088 GST_DEBUG_OBJECT (src, "dynamic size: %d", dynamic);
2090 /* check size if we have one */
2091 if (maxsize != -1) {
2092 /* if we run past the end, check if the file became bigger and
2094 if (G_UNLIKELY (offset + *length >= maxsize || dynamic)) {
2095 /* see if length of the file changed */
2096 if (bclass->get_size)
2097 if (!bclass->get_size (src, &size))
2100 /* make sure we don't exceed the configured segment stop
2103 maxsize = MIN (size, stop);
2107 /* if we are at or past the end, EOS */
2108 if (G_UNLIKELY (offset >= maxsize))
2109 goto unexpected_length;
2111 /* else we can clip to the end */
2112 if (G_UNLIKELY (offset + *length >= maxsize))
2113 *length = maxsize - offset;
2118 /* keep track of current position and update duration.
2119 * segment is in bytes, we checked that above. */
2120 GST_OBJECT_LOCK (src);
2121 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
2122 GST_OBJECT_UNLOCK (src);
2133 /* must be called with LIVE_LOCK */
2134 static GstFlowReturn
2135 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2139 GstBaseSrcClass *bclass;
2140 GstClockReturn status;
2142 bclass = GST_BASE_SRC_GET_CLASS (src);
2146 if (G_UNLIKELY (!src->live_running)) {
2147 ret = gst_base_src_wait_playing (src);
2148 if (ret != GST_FLOW_OK)
2153 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
2156 if (G_UNLIKELY (!bclass->create))
2159 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
2160 goto unexpected_length;
2162 /* track position */
2163 GST_OBJECT_LOCK (src);
2164 if (src->segment.format == GST_FORMAT_BYTES)
2165 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
2166 GST_OBJECT_UNLOCK (src);
2168 /* normally we don't count buffers */
2169 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2170 if (src->num_buffers_left == 0)
2171 goto reached_num_buffers;
2173 src->num_buffers_left--;
2176 /* don't enter the create function if a pending EOS event was set. For the
2177 * logic of the pending_eos, check the event function of this class. */
2178 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
2181 GST_DEBUG_OBJECT (src,
2182 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2183 G_GINT64_FORMAT, offset, length, src->segment.time);
2185 ret = bclass->create (src, offset, length, buf);
2187 /* The create function could be unlocked because we have a pending EOS. It's
2188 * possible that we have a valid buffer from create that we need to
2189 * discard when the create function returned _OK. */
2190 if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
2191 if (ret == GST_FLOW_OK) {
2192 gst_buffer_unref (*buf);
2198 if (G_UNLIKELY (ret != GST_FLOW_OK))
2201 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2202 if (offset == 0 && src->segment.time == 0
2203 && GST_BUFFER_TIMESTAMP (*buf) == -1 && !src->is_live) {
2204 *buf = gst_buffer_make_metadata_writable (*buf);
2205 GST_BUFFER_TIMESTAMP (*buf) = 0;
2208 /* set pad caps on the buffer if the buffer had no caps */
2209 if (GST_BUFFER_CAPS (*buf) == NULL) {
2210 *buf = gst_buffer_make_metadata_writable (*buf);
2211 gst_buffer_set_caps (*buf, GST_PAD_CAPS (src->srcpad));
2214 /* now sync before pushing the buffer */
2215 status = gst_base_src_do_sync (src, *buf);
2217 /* waiting for the clock could have made us flushing */
2218 if (G_UNLIKELY (src->priv->flushing))
2222 case GST_CLOCK_EARLY:
2223 /* the buffer is too late. We currently don't drop the buffer. */
2224 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2227 /* buffer synchronised properly */
2228 GST_DEBUG_OBJECT (src, "buffer ok");
2230 case GST_CLOCK_UNSCHEDULED:
2231 /* this case is triggered when we were waiting for the clock and
2232 * it got unlocked because we did a state change. In any case, get rid of
2234 gst_buffer_unref (*buf);
2236 if (!src->live_running) {
2237 /* We return WRONG_STATE when we are not running to stop the dataflow also
2238 * get rid of the produced buffer. */
2239 GST_DEBUG_OBJECT (src,
2240 "clock was unscheduled (%d), returning WRONG_STATE", status);
2241 ret = GST_FLOW_WRONG_STATE;
2243 /* If we are running when this happens, we quickly switched between
2244 * pause and playing. We try to produce a new buffer */
2245 GST_DEBUG_OBJECT (src,
2246 "clock was unscheduled (%d), but we are running", status);
2251 /* all other result values are unexpected and errors */
2252 GST_ELEMENT_ERROR (src, CORE, CLOCK,
2253 (_("Internal clock error.")),
2254 ("clock returned unexpected return value %d", status));
2255 gst_buffer_unref (*buf);
2257 ret = GST_FLOW_ERROR;
2265 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2266 gst_flow_get_name (ret));
2271 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2272 gst_flow_get_name (ret));
2277 GST_DEBUG_OBJECT (src, "getrange but not started");
2278 return GST_FLOW_WRONG_STATE;
2282 GST_DEBUG_OBJECT (src, "no create function");
2283 return GST_FLOW_ERROR;
2287 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2288 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2289 return GST_FLOW_UNEXPECTED;
2291 reached_num_buffers:
2293 GST_DEBUG_OBJECT (src, "sent all buffers");
2294 return GST_FLOW_UNEXPECTED;
2298 GST_DEBUG_OBJECT (src, "we are flushing");
2299 gst_buffer_unref (*buf);
2301 return GST_FLOW_WRONG_STATE;
2305 GST_DEBUG_OBJECT (src, "we are EOS");
2306 return GST_FLOW_UNEXPECTED;
2310 static GstFlowReturn
2311 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
2317 src = GST_BASE_SRC_CAST (gst_object_ref (GST_OBJECT_PARENT (pad)));
2319 GST_LIVE_LOCK (src);
2320 if (G_UNLIKELY (src->priv->flushing))
2323 res = gst_base_src_get_range (src, offset, length, buf);
2326 GST_LIVE_UNLOCK (src);
2328 gst_object_unref (src);
2335 GST_DEBUG_OBJECT (src, "we are flushing");
2336 res = GST_FLOW_WRONG_STATE;
2342 gst_base_src_default_check_get_range (GstBaseSrc * src)
2346 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
2347 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2348 if (G_LIKELY (gst_base_src_start (src)))
2349 gst_base_src_stop (src);
2352 /* we can operate in getrange mode if the native format is bytes
2353 * and we are seekable, this condition is set in the random_access
2354 * flag and is set in the _start() method. */
2355 res = src->random_access;
2361 gst_base_src_check_get_range (GstBaseSrc * src)
2363 GstBaseSrcClass *bclass;
2366 bclass = GST_BASE_SRC_GET_CLASS (src);
2368 if (bclass->check_get_range == NULL)
2371 res = bclass->check_get_range (src);
2372 GST_LOG_OBJECT (src, "%s() returned %d",
2373 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
2380 GST_WARNING_OBJECT (src, "no check_get_range function set");
2386 gst_base_src_pad_check_get_range (GstPad * pad)
2391 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2393 res = gst_base_src_check_get_range (src);
2399 gst_base_src_loop (GstPad * pad)
2402 GstBuffer *buf = NULL;
2407 GList *pending_events = NULL, *tmp;
2411 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2413 GST_LIVE_LOCK (src);
2415 if (G_UNLIKELY (src->priv->flushing))
2418 src->priv->last_sent_eos = FALSE;
2420 blocksize = src->blocksize;
2422 /* if we operate in bytes, we can calculate an offset */
2423 if (src->segment.format == GST_FORMAT_BYTES) {
2424 position = src->segment.last_stop;
2425 /* for negative rates, start with subtracting the blocksize */
2426 if (src->segment.rate < 0.0) {
2427 /* we cannot go below segment.start */
2428 if (position > src->segment.start + blocksize)
2429 position -= blocksize;
2431 /* last block, remainder up to segment.start */
2432 blocksize = position - src->segment.start;
2433 position = src->segment.start;
2439 GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
2440 GST_TIME_ARGS (position), blocksize);
2442 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2443 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2444 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2445 gst_flow_get_name (ret));
2446 GST_LIVE_UNLOCK (src);
2449 /* this should not happen */
2450 if (G_UNLIKELY (buf == NULL))
2453 /* push events to close/start our segment before we push the buffer. */
2454 if (G_UNLIKELY (src->priv->close_segment)) {
2455 gst_pad_push_event (pad, src->priv->close_segment);
2456 src->priv->close_segment = NULL;
2458 if (G_UNLIKELY (src->priv->start_segment)) {
2459 gst_pad_push_event (pad, src->priv->start_segment);
2460 src->priv->start_segment = NULL;
2462 src->priv->newsegment_pending = FALSE;
2464 if (g_atomic_int_get (&src->priv->have_events)) {
2465 GST_OBJECT_LOCK (src);
2466 /* take the events */
2467 pending_events = src->priv->pending_events;
2468 src->priv->pending_events = NULL;
2469 g_atomic_int_set (&src->priv->have_events, FALSE);
2470 GST_OBJECT_UNLOCK (src);
2473 /* Push out pending events if any */
2474 if (G_UNLIKELY (pending_events != NULL)) {
2475 for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2476 GstEvent *ev = (GstEvent *) tmp->data;
2477 gst_pad_push_event (pad, ev);
2479 g_list_free (pending_events);
2482 /* figure out the new position */
2483 switch (src->segment.format) {
2484 case GST_FORMAT_BYTES:
2486 guint bufsize = GST_BUFFER_SIZE (buf);
2488 /* we subtracted above for negative rates */
2489 if (src->segment.rate >= 0.0)
2490 position += bufsize;
2493 case GST_FORMAT_TIME:
2495 GstClockTime start, duration;
2497 start = GST_BUFFER_TIMESTAMP (buf);
2498 duration = GST_BUFFER_DURATION (buf);
2500 if (GST_CLOCK_TIME_IS_VALID (start))
2503 position = src->segment.last_stop;
2505 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2506 if (src->segment.rate >= 0.0)
2507 position += duration;
2508 else if (position > duration)
2509 position -= duration;
2515 case GST_FORMAT_DEFAULT:
2516 if (src->segment.rate >= 0.0)
2517 position = GST_BUFFER_OFFSET_END (buf);
2519 position = GST_BUFFER_OFFSET (buf);
2525 if (position != -1) {
2526 if (src->segment.rate >= 0.0) {
2527 /* positive rate, check if we reached the stop */
2528 if (src->segment.stop != -1) {
2529 if (position >= src->segment.stop) {
2531 position = src->segment.stop;
2535 /* negative rate, check if we reached the start. start is always set to
2536 * something different from -1 */
2537 if (position <= src->segment.start) {
2539 position = src->segment.start;
2541 /* when going reverse, all buffers are DISCONT */
2542 src->priv->discont = TRUE;
2544 GST_OBJECT_LOCK (src);
2545 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
2546 GST_OBJECT_UNLOCK (src);
2549 if (G_UNLIKELY (src->priv->discont)) {
2550 buf = gst_buffer_make_metadata_writable (buf);
2551 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2552 src->priv->discont = FALSE;
2554 GST_LIVE_UNLOCK (src);
2556 ret = gst_pad_push (pad, buf);
2557 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2558 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2559 gst_flow_get_name (ret));
2563 if (G_UNLIKELY (eos)) {
2564 GST_INFO_OBJECT (src, "pausing after end of segment");
2565 ret = GST_FLOW_UNEXPECTED;
2575 GST_DEBUG_OBJECT (src, "we are flushing");
2576 GST_LIVE_UNLOCK (src);
2577 ret = GST_FLOW_WRONG_STATE;
2582 const gchar *reason = gst_flow_get_name (ret);
2585 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2586 src->data.ABI.running = FALSE;
2587 gst_pad_pause_task (pad);
2588 if (ret == GST_FLOW_UNEXPECTED) {
2589 gboolean flag_segment;
2593 /* perform EOS logic */
2594 flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
2595 format = src->segment.format;
2596 last_stop = src->segment.last_stop;
2599 GstMessage *message;
2601 message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2603 gst_message_set_seqnum (message, src->priv->seqnum);
2604 gst_element_post_message (GST_ELEMENT_CAST (src), message);
2606 event = gst_event_new_eos ();
2607 gst_event_set_seqnum (event, src->priv->seqnum);
2608 gst_pad_push_event (pad, event);
2609 src->priv->last_sent_eos = TRUE;
2611 } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_UNEXPECTED) {
2612 event = gst_event_new_eos ();
2613 gst_event_set_seqnum (event, src->priv->seqnum);
2614 /* for fatal errors we post an error message, post the error
2615 * first so the app knows about the error first.
2616 * Also don't do this for WRONG_STATE because it happens
2617 * due to flushing and posting an error message because of
2618 * that is the wrong thing to do, e.g. when we're doing
2619 * a flushing seek. */
2620 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2621 (_("Internal data flow error.")),
2622 ("streaming task paused, reason %s (%d)", reason, ret));
2623 gst_pad_push_event (pad, event);
2624 src->priv->last_sent_eos = TRUE;
2630 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2631 (_("Internal data flow error.")), ("element returned NULL buffer"));
2632 GST_LIVE_UNLOCK (src);
2637 /* default negotiation code.
2639 * Take intersection between src and sink pads, take first
2643 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2646 GstCaps *caps = NULL;
2647 GstCaps *peercaps = NULL;
2648 gboolean result = FALSE;
2650 /* first see what is possible on our source pad */
2651 thiscaps = gst_pad_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
2652 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2653 /* nothing or anything is allowed, we're done */
2654 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2655 goto no_nego_needed;
2657 if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
2660 /* get the peer caps */
2661 peercaps = gst_pad_peer_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
2662 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2664 /* get intersection */
2666 gst_caps_intersect_full (peercaps, thiscaps, GST_CAPS_INTERSECT_FIRST);
2667 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
2668 gst_caps_unref (peercaps);
2670 /* no peer, work with our own caps then */
2671 caps = gst_caps_copy (thiscaps);
2673 gst_caps_unref (thiscaps);
2675 /* take first (and best, since they are sorted) possibility */
2676 gst_caps_truncate (caps);
2679 if (!gst_caps_is_empty (caps)) {
2680 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2681 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2683 if (gst_caps_is_any (caps)) {
2684 /* hmm, still anything, so element can do anything and
2685 * nego is not needed */
2687 } else if (gst_caps_is_fixed (caps)) {
2688 /* yay, fixed caps, use those then, it's possible that the subclass does
2689 * not accept this caps after all and we have to fail. */
2690 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2693 gst_caps_unref (caps);
2695 GST_DEBUG_OBJECT (basesrc, "no common caps");
2701 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2703 gst_caps_unref (thiscaps);
2708 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2709 ("No supported formats found"),
2710 ("This element did not produce valid caps"));
2712 gst_caps_unref (thiscaps);
2718 gst_base_src_negotiate (GstBaseSrc * basesrc)
2720 GstBaseSrcClass *bclass;
2721 gboolean result = TRUE;
2723 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2725 if (bclass->negotiate)
2726 result = bclass->negotiate (basesrc);
2732 gst_base_src_start (GstBaseSrc * basesrc)
2734 GstBaseSrcClass *bclass;
2740 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2743 GST_DEBUG_OBJECT (basesrc, "starting source");
2745 basesrc->num_buffers_left = basesrc->num_buffers;
2747 GST_OBJECT_LOCK (basesrc);
2748 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2749 GST_OBJECT_UNLOCK (basesrc);
2751 basesrc->data.ABI.running = FALSE;
2752 basesrc->priv->newsegment_pending = FALSE;
2754 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2756 result = bclass->start (basesrc);
2761 goto could_not_start;
2763 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2765 format = basesrc->segment.format;
2767 /* figure out the size */
2768 if (format == GST_FORMAT_BYTES) {
2769 if (bclass->get_size) {
2770 if (!(result = bclass->get_size (basesrc, &size)))
2776 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2777 /* only update the size when operating in bytes, subclass is supposed
2778 * to set duration in the start method for other formats */
2779 GST_OBJECT_LOCK (basesrc);
2780 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2781 GST_OBJECT_UNLOCK (basesrc);
2786 GST_DEBUG_OBJECT (basesrc,
2787 "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2788 G_GINT64_FORMAT, gst_format_get_name (format), result, size,
2789 basesrc->segment.duration);
2791 seekable = gst_base_src_seekable (basesrc);
2792 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
2794 /* update for random access flag */
2795 basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
2797 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2799 /* run typefind if we are random_access and the typefinding is enabled. */
2800 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2803 if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
2804 goto typefind_failed;
2806 result = gst_pad_set_caps (basesrc->srcpad, caps);
2807 gst_caps_unref (caps);
2809 /* use class or default negotiate function */
2810 if (!(result = gst_base_src_negotiate (basesrc)))
2811 goto could_not_negotiate;
2819 GST_DEBUG_OBJECT (basesrc, "could not start");
2820 /* subclass is supposed to post a message. We don't have to call _stop. */
2823 could_not_negotiate:
2825 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2826 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2827 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2828 /* we must call stop */
2829 gst_base_src_stop (basesrc);
2834 GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
2835 GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
2836 /* we must call stop */
2837 gst_base_src_stop (basesrc);
2843 gst_base_src_stop (GstBaseSrc * basesrc)
2845 GstBaseSrcClass *bclass;
2846 gboolean result = TRUE;
2848 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2851 GST_DEBUG_OBJECT (basesrc, "stopping source");
2853 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2855 result = bclass->stop (basesrc);
2858 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2863 /* start or stop flushing dataprocessing
2866 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2867 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2869 GstBaseSrcClass *bclass;
2871 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2873 if (flushing && unlock) {
2874 /* unlock any subclasses, we need to do this before grabbing the
2875 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2876 * unlock to the params because of backwards compat (see seek handler)*/
2878 bclass->unlock (basesrc);
2881 /* the live lock is released when we are blocked, waiting for playing or
2882 * when we sync to the clock. */
2883 GST_LIVE_LOCK (basesrc);
2885 *playing = basesrc->live_running;
2886 basesrc->priv->flushing = flushing;
2888 /* if we are locked in the live lock, signal it to make it flush */
2889 basesrc->live_running = TRUE;
2891 /* clear pending EOS if any */
2892 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
2894 /* step 1, now that we have the LIVE lock, clear our unlock request */
2895 if (bclass->unlock_stop)
2896 bclass->unlock_stop (basesrc);
2898 /* step 2, unblock clock sync (if any) or any other blocking thing */
2899 if (basesrc->clock_id)
2900 gst_clock_id_unschedule (basesrc->clock_id);
2902 /* signal the live source that it can start playing */
2903 basesrc->live_running = live_play;
2905 /* When unlocking drop all delayed events */
2907 GST_OBJECT_LOCK (basesrc);
2908 if (basesrc->priv->pending_events) {
2909 g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
2911 g_list_free (basesrc->priv->pending_events);
2912 basesrc->priv->pending_events = NULL;
2913 g_atomic_int_set (&basesrc->priv->have_events, FALSE);
2915 GST_OBJECT_UNLOCK (basesrc);
2918 GST_LIVE_SIGNAL (basesrc);
2919 GST_LIVE_UNLOCK (basesrc);
2924 /* the purpose of this function is to make sure that a live source blocks in the
2925 * LIVE lock or leaves the LIVE lock and continues playing. */
2927 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2929 GstBaseSrcClass *bclass;
2931 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2933 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2935 GST_DEBUG_OBJECT (basesrc, "unlock");
2937 bclass->unlock (basesrc);
2940 /* we are now able to grab the LIVE lock, when we get it, we can be
2941 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2943 GST_LIVE_LOCK (basesrc);
2944 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2946 /* unblock clock sync (if any) */
2947 if (basesrc->clock_id)
2948 gst_clock_id_unschedule (basesrc->clock_id);
2950 /* configure what to do when we get to the LIVE lock. */
2951 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2952 basesrc->live_running = live_play;
2957 /* clear our unlock request when going to PLAYING */
2958 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2959 if (bclass->unlock_stop)
2960 bclass->unlock_stop (basesrc);
2962 /* for live sources we restart the timestamp correction */
2963 basesrc->priv->latency = -1;
2964 /* have to restart the task in case it stopped because of the unlock when
2965 * we went to PAUSED. Only do this if we operating in push mode. */
2966 GST_OBJECT_LOCK (basesrc->srcpad);
2967 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2968 GST_OBJECT_UNLOCK (basesrc->srcpad);
2970 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2972 GST_DEBUG_OBJECT (basesrc, "signal");
2973 GST_LIVE_SIGNAL (basesrc);
2975 GST_LIVE_UNLOCK (basesrc);
2981 gst_base_src_activate_push (GstPad * pad, gboolean active)
2983 GstBaseSrc *basesrc;
2986 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2988 /* prepare subclass first */
2990 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2992 if (G_UNLIKELY (!basesrc->can_activate_push))
2993 goto no_push_activation;
2995 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2998 basesrc->priv->last_sent_eos = FALSE;
2999 basesrc->priv->discont = TRUE;
3000 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
3002 /* do initial seek, which will start the task */
3003 GST_OBJECT_LOCK (basesrc);
3004 event = basesrc->data.ABI.pending_seek;
3005 basesrc->data.ABI.pending_seek = NULL;
3006 GST_OBJECT_UNLOCK (basesrc);
3008 /* no need to unlock anything, the task is certainly
3009 * not running here. The perform seek code will start the task when
3011 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3015 gst_event_unref (event);
3017 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3019 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3021 gst_pad_stop_task (pad);
3022 /* now we can stop the source */
3023 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3031 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3036 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3041 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3043 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3045 gst_pad_stop_task (pad);
3046 /* Stop the basesrc */
3047 gst_base_src_stop (basesrc);
3049 gst_event_unref (event);
3054 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3060 gst_base_src_activate_pull (GstPad * pad, gboolean active)
3062 GstBaseSrc *basesrc;
3064 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
3066 /* prepare subclass first */
3068 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3069 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3072 /* if not random_access, we cannot operate in pull mode for now */
3073 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
3076 /* stop flushing now but for live sources, still block in the LIVE lock when
3077 * we are not yet PLAYING */
3078 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
3080 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3081 /* flush all, there is no task to stop */
3082 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
3084 /* don't send EOS when going from PAUSED => READY when in pull mode */
3085 basesrc->priv->last_sent_eos = TRUE;
3087 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3095 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3100 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3101 gst_base_src_stop (basesrc);
3106 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3111 static GstStateChangeReturn
3112 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3114 GstBaseSrc *basesrc;
3115 GstStateChangeReturn result;
3116 gboolean no_preroll = FALSE;
3118 basesrc = GST_BASE_SRC (element);
3120 switch (transition) {
3121 case GST_STATE_CHANGE_NULL_TO_READY:
3123 case GST_STATE_CHANGE_READY_TO_PAUSED:
3124 no_preroll = gst_base_src_is_live (basesrc);
3126 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3127 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3128 if (gst_base_src_is_live (basesrc)) {
3129 /* now we can start playback */
3130 gst_base_src_set_playing (basesrc, TRUE);
3138 GST_ELEMENT_CLASS (parent_class)->change_state (element,
3139 transition)) == GST_STATE_CHANGE_FAILURE)
3142 switch (transition) {
3143 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3144 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3145 if (gst_base_src_is_live (basesrc)) {
3146 /* make sure we block in the live lock in PAUSED */
3147 gst_base_src_set_playing (basesrc, FALSE);
3151 case GST_STATE_CHANGE_PAUSED_TO_READY:
3153 GstEvent **event_p, *event;
3155 /* we don't need to unblock anything here, the pad deactivation code
3156 * already did this */
3158 /* FIXME, deprecate this behaviour, it is very dangerous.
3159 * the preferred way of sending EOS downstream is by sending
3160 * the EOS event to the element */
3161 if (!basesrc->priv->last_sent_eos) {
3162 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
3163 event = gst_event_new_eos ();
3164 gst_event_set_seqnum (event, basesrc->priv->seqnum);
3165 gst_pad_push_event (basesrc->srcpad, event);
3166 basesrc->priv->last_sent_eos = TRUE;
3168 g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
3169 event_p = &basesrc->data.ABI.pending_seek;
3170 gst_event_replace (event_p, NULL);
3171 event_p = &basesrc->priv->close_segment;
3172 gst_event_replace (event_p, NULL);
3173 event_p = &basesrc->priv->start_segment;
3174 gst_event_replace (event_p, NULL);
3177 case GST_STATE_CHANGE_READY_TO_NULL:
3183 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3184 result = GST_STATE_CHANGE_NO_PREROLL;
3191 GST_DEBUG_OBJECT (basesrc, "parent failed state change");