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>
38 * The source can be configured to operate in any #GstFormat with the
39 * gst_base_src_set_format() method. The currently set format determines
40 * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT
41 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
44 * #GstBaseSrc always supports push mode scheduling. If the following
45 * conditions are met, it also supports pull mode scheduling:
47 * <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
49 * <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
54 * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any
55 * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE.
58 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
59 * automatically seekable in push mode as well. The following conditions must
60 * be met to make the element seekable in push mode when the format is not
64 * #GstBaseSrc::is_seekable returns %TRUE.
67 * #GstBaseSrc::query can convert all supported seek formats to the
68 * internal format as set with gst_base_src_set_format().
71 * #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
76 * When the element does not meet the requirements to operate in pull mode,
77 * the offset and length in the #GstBaseSrc::create method should be ignored.
78 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
79 * element can operate in pull mode but only with specific offsets and
80 * lengths, it is allowed to generate an error when the wrong values are passed
81 * to the #GstBaseSrc::create function.
84 * #GstBaseSrc has support for live sources. Live sources are sources that
85 * produce data at a fixed rate, such as audio or video capture devices. A
86 * typical live source also provides a clock to publish the rate at which
88 * Use gst_base_src_set_live() to activate the live source mode.
91 * A live source does not produce data in the PAUSED state. This means that the
92 * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
93 * To signal the pipeline that the element will not produce data, the return
94 * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
97 * A typical live source will timestamp the buffers it creates with the
98 * current stream time of the pipeline. This is one reason why a live source
99 * can only produce data in the PLAYING state, when the clock is actually
100 * distributed and running.
103 * Live sources that synchronize and block on the clock (an audio source, for
104 * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
105 * function was interrupted by a state change to PAUSED.
108 * The #GstBaseSrc::get_times method can be used to implement pseudo-live
109 * sources. The base source will wait for the specified stream time returned in
110 * #GstBaseSrc::get_times before pushing out the buffer.
111 * It only makes sense to implement the ::get_times function if the source is
115 * For live sources, the base class will by default measure the time it takes to
116 * create the first buffer in the PLAYING state and will report this value as
117 * the latency. Subclasses should override the query function when this
118 * behaviour is not acceptable.
121 * There is only support in #GstBaseSrc for exactly one source pad, which
122 * should be named "src". A source implementation (subclass of #GstBaseSrc)
123 * should install a pad template in its base_init function, like so:
128 * my_element_base_init (gpointer g_class)
130 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
131 * // srctemplate should be a #GstStaticPadTemplate with direction
132 * // #GST_PAD_SRC and name "src"
133 * gst_element_class_add_pad_template (gstelement_class,
134 * gst_static_pad_template_get (&srctemplate));
135 * // see #GstElementDetails
136 * gst_element_class_set_details (gstelement_class, &details);
140 * <title>Controlled shutdown of live sources in applications</title>
142 * Applications that record from a live source may want to stop recording
143 * in a controlled way, so that the recording is stopped, but the data
144 * already in the pipeline is processed to the end (remember that many live
145 * sources would go on recording forever otherwise). For that to happen the
146 * application needs to make the source stop recording and send an EOS
147 * event down the pipeline. The application would then wait for an
148 * EOS message posted on the pipeline's bus to know when all data has
149 * been processed and the pipeline can safely be stopped.
152 * Since GStreamer 0.10.3 an application may simply set the source
153 * element to NULL or READY state to make it send an EOS event downstream.
154 * The application should lock the state of the source afterwards, so that
155 * shutting down the pipeline from PLAYING doesn't temporarily start up the
156 * source element for a second time:
160 * gst_element_set_state (audio_source, #GST_STATE_NULL);
161 * gst_element_set_locked_state (audio_source, %TRUE);
164 * Now the application should wait for an EOS message
165 * to be posted on the pipeline's bus. Once it has received
166 * an EOS message, it may safely shut down the entire pipeline:
169 * // everything done - shut down pipeline
170 * gst_element_set_state (pipeline, #GST_STATE_NULL);
171 * gst_element_set_locked_state (audio_source, %FALSE);
176 * Note that setting the source element to NULL or READY when the
177 * pipeline is in the PAUSED state may cause a deadlock since the streaming
178 * thread might be blocked in PREROLL.
181 * Last reviewed on 2006-09-27 (0.10.11)
193 #include "gstbasesrc.h"
194 #include "gsttypefindhelper.h"
195 #include <gst/gstmarshal.h>
196 #include <gst/gst-i18n-lib.h>
198 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
199 #define GST_CAT_DEFAULT gst_base_src_debug
201 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
202 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
203 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
204 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
205 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
206 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
207 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
209 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
210 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
212 /* BaseSrc signals and args */
219 #define DEFAULT_BLOCKSIZE 4096
220 #define DEFAULT_NUM_BUFFERS -1
221 #define DEFAULT_TYPEFIND FALSE
231 #define GST_BASE_SRC_GET_PRIVATE(obj) \
232 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
234 struct _GstBaseSrcPrivate
236 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
237 * to avoid the sending of two EOS in some cases) */
240 /* two segments to be sent in the streaming thread with STREAM_LOCK */
241 GstEvent *close_segment;
242 GstEvent *start_segment;
244 /* startup latency is the time it takes between going to PLAYING and producing
245 * the first BUFFER with running_time 0. This value is included in the latency
247 GstClockTime startup_latency;
250 static GstElementClass *parent_class = NULL;
252 static void gst_base_src_base_init (gpointer g_class);
253 static void gst_base_src_class_init (GstBaseSrcClass * klass);
254 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
255 static void gst_base_src_finalize (GObject * object);
259 gst_base_src_get_type (void)
261 static GType base_src_type = 0;
263 if (G_UNLIKELY (base_src_type == 0)) {
264 static const GTypeInfo base_src_info = {
265 sizeof (GstBaseSrcClass),
266 (GBaseInitFunc) gst_base_src_base_init,
268 (GClassInitFunc) gst_base_src_class_init,
273 (GInstanceInitFunc) gst_base_src_init,
276 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
277 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
279 return base_src_type;
281 static GstCaps *gst_base_src_getcaps (GstPad * pad);
282 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
283 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
285 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
286 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
287 static void gst_base_src_set_property (GObject * object, guint prop_id,
288 const GValue * value, GParamSpec * pspec);
289 static void gst_base_src_get_property (GObject * object, guint prop_id,
290 GValue * value, GParamSpec * pspec);
291 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
292 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
293 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
294 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
296 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
298 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
299 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
300 GstSegment * segment);
301 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
302 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
303 GstEvent * event, GstSegment * segment);
305 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
306 static gboolean gst_base_src_unlock_stop (GstBaseSrc * basesrc);
307 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
308 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
310 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
311 GstStateChange transition);
313 static void gst_base_src_loop (GstPad * pad);
314 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
315 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
316 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
317 guint length, GstBuffer ** buf);
318 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
319 guint length, GstBuffer ** buf);
322 gst_base_src_base_init (gpointer g_class)
324 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
328 gst_base_src_class_init (GstBaseSrcClass * klass)
330 GObjectClass *gobject_class;
331 GstElementClass *gstelement_class;
333 gobject_class = G_OBJECT_CLASS (klass);
334 gstelement_class = GST_ELEMENT_CLASS (klass);
336 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
338 parent_class = g_type_class_peek_parent (klass);
340 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
341 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
342 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
344 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
345 g_param_spec_ulong ("blocksize", "Block size",
346 "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
347 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
348 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
349 g_param_spec_int ("num-buffers", "num-buffers",
350 "Number of buffers to output before sending EOS", -1, G_MAXINT,
351 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
352 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
353 g_param_spec_boolean ("typefind", "Typefind",
354 "Run typefind before negotiating", DEFAULT_TYPEFIND,
357 gstelement_class->change_state =
358 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
359 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
360 gstelement_class->get_query_types =
361 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
363 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
364 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
365 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
366 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
367 klass->check_get_range =
368 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
369 klass->prepare_seek_segment =
370 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
374 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
377 GstPadTemplate *pad_template;
379 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
381 basesrc->is_live = FALSE;
382 basesrc->live_lock = g_mutex_new ();
383 basesrc->live_cond = g_cond_new ();
384 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
385 basesrc->num_buffers_left = -1;
387 basesrc->can_activate_push = TRUE;
388 basesrc->pad_mode = GST_ACTIVATE_NONE;
391 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
392 g_return_if_fail (pad_template != NULL);
394 GST_DEBUG_OBJECT (basesrc, "creating src pad");
395 pad = gst_pad_new_from_template (pad_template, "src");
397 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
398 gst_pad_set_activatepush_function (pad,
399 GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
400 gst_pad_set_activatepull_function (pad,
401 GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
402 gst_pad_set_event_function (pad,
403 GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
404 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
405 gst_pad_set_checkgetrange_function (pad,
406 GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
407 gst_pad_set_getrange_function (pad,
408 GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
409 gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
410 gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
411 gst_pad_set_fixatecaps_function (pad,
412 GST_DEBUG_FUNCPTR (gst_base_src_fixate));
414 /* hold pointer to pad */
415 basesrc->srcpad = pad;
416 GST_DEBUG_OBJECT (basesrc, "adding src pad");
417 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
419 basesrc->blocksize = DEFAULT_BLOCKSIZE;
420 basesrc->clock_id = NULL;
421 /* we operate in BYTES by default */
422 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
423 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
425 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
427 GST_DEBUG_OBJECT (basesrc, "init done");
431 gst_base_src_finalize (GObject * object)
436 basesrc = GST_BASE_SRC (object);
438 g_mutex_free (basesrc->live_lock);
439 g_cond_free (basesrc->live_cond);
441 event_p = &basesrc->data.ABI.pending_seek;
442 gst_event_replace ((GstEvent **) event_p, NULL);
444 G_OBJECT_CLASS (parent_class)->finalize (object);
448 * gst_base_src_wait_playing:
451 * If the #GstBaseSrcClass::create method performs its own synchronisation against
452 * the clock it must unblock when going from PLAYING to the PAUSED state and call
453 * this method before continuing to produce the remaining data.
455 * This function will block until a state change to PLAYING happens (in which
456 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
457 * to a state change to READY or a FLUSH event (in which case this function
458 * returns #GST_FLOW_WRONG_STATE).
462 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
463 * continue. Any other return value should be returned from the create vmethod.
466 gst_base_src_wait_playing (GstBaseSrc * src)
468 /* block until the state changes, or we get a flush, or something */
471 while (G_UNLIKELY (!src->live_running)) {
472 GST_DEBUG ("live source signal waiting");
473 GST_LIVE_SIGNAL (src);
474 GST_DEBUG ("live source waiting for running state");
476 GST_DEBUG ("live source unlocked");
478 /* FIXME, use another variable to signal stopping so that we don't
479 * have to grab another lock. */
480 GST_OBJECT_LOCK (src->srcpad);
481 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
483 GST_OBJECT_UNLOCK (src->srcpad);
485 GST_LIVE_UNLOCK (src);
492 GST_DEBUG_OBJECT (src, "pad is flushing");
493 GST_OBJECT_UNLOCK (src->srcpad);
494 GST_LIVE_UNLOCK (src);
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)
518 GST_LIVE_UNLOCK (src);
522 * gst_base_src_is_live:
523 * @src: base source instance
525 * Check if an element is in live mode.
527 * Returns: %TRUE if element is in live mode.
530 gst_base_src_is_live (GstBaseSrc * src)
535 result = src->is_live;
536 GST_LIVE_UNLOCK (src);
542 * gst_base_src_set_format:
543 * @src: base source instance
544 * @format: the format to use
546 * Sets the default format of the source. This will be the format used
547 * for sending NEW_SEGMENT events and for performing seeks.
549 * If a format of GST_FORMAT_BYTES is set, the element will be able to
550 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
555 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
557 gst_segment_init (&src->segment, format);
561 * gst_base_src_query_latency:
563 * @live: if the source is live
564 * @min_latency: the min latency of the source
565 * @max_latency: the max latency of the source
567 * Query the source for the latency parameters. @live will be TRUE when @src is
568 * configured as a live source. @min_latency will be set to the difference
569 * between the running time and the timestamp of the first buffer.
570 * @max_latency is always the undefined value of -1.
572 * This function is mostly used by subclasses.
574 * Returns: TRUE if the query succeeded.
579 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
580 GstClockTime * min_latency, GstClockTime * max_latency)
586 *live = src->is_live;
588 /* if we have a startup latency, report this one, else report 0. Subclasses
589 * are supposed to override the query function if they want something
591 if (src->priv->startup_latency != -1)
592 min = src->priv->startup_latency;
601 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
602 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
604 GST_LIVE_UNLOCK (src);
610 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
612 GstBaseSrcClass *bclass;
616 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
617 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
619 if (bclass->set_caps)
620 res = bclass->set_caps (bsrc, caps);
626 gst_base_src_getcaps (GstPad * pad)
628 GstBaseSrcClass *bclass;
630 GstCaps *caps = NULL;
632 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
633 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
634 if (bclass->get_caps)
635 caps = bclass->get_caps (bsrc);
638 GstPadTemplate *pad_template;
641 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
642 if (pad_template != NULL) {
643 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
650 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
652 GstBaseSrcClass *bclass;
655 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
656 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
659 bclass->fixate (bsrc, caps);
661 gst_object_unref (bsrc);
665 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
669 switch (GST_QUERY_TYPE (query)) {
670 case GST_QUERY_POSITION:
674 gst_query_parse_position (query, &format, NULL);
676 case GST_FORMAT_PERCENT:
682 position = src->segment.last_stop;
683 duration = src->segment.duration;
685 if (position != -1 && duration != -1) {
686 if (position < duration)
687 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
690 percent = GST_FORMAT_PERCENT_MAX;
694 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
702 position = src->segment.last_stop;
704 if (position != -1) {
705 /* convert to requested format */
707 gst_pad_query_convert (src->srcpad, src->segment.format,
708 position, &format, &position);
712 gst_query_set_position (query, format, position);
718 case GST_QUERY_DURATION:
722 gst_query_parse_duration (query, &format, NULL);
724 GST_DEBUG_OBJECT (src, "duration query in format %s",
725 gst_format_get_name (format));
727 case GST_FORMAT_PERCENT:
728 gst_query_set_duration (query, GST_FORMAT_PERCENT,
729 GST_FORMAT_PERCENT_MAX);
736 duration = src->segment.duration;
738 if (duration != -1) {
739 /* convert to requested format */
741 gst_pad_query_convert (src->srcpad, src->segment.format,
742 duration, &format, &duration);
746 gst_query_set_duration (query, format, duration);
753 case GST_QUERY_SEEKING:
755 gst_query_set_seeking (query, src->segment.format,
756 src->seekable, 0, src->segment.duration);
760 case GST_QUERY_SEGMENT:
764 /* no end segment configured, current duration then */
765 if ((stop = src->segment.stop) == -1)
766 stop = src->segment.duration;
767 start = src->segment.start;
769 /* adjust to stream time */
770 if (src->segment.time != -1) {
771 start -= src->segment.time;
773 stop -= src->segment.time;
775 gst_query_set_segment (query, src->segment.rate, src->segment.format,
781 case GST_QUERY_FORMATS:
783 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
784 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
788 case GST_QUERY_CONVERT:
790 GstFormat src_fmt, dest_fmt;
791 gint64 src_val, dest_val;
793 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
795 /* we can only convert between equal formats... */
796 if (src_fmt == dest_fmt) {
802 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
805 case GST_QUERY_LATENCY:
807 GstClockTime min, max;
810 /* Subclasses should override and implement something usefull */
811 res = gst_base_src_query_latency (src, &live, &min, &max);
813 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
814 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
815 GST_TIME_ARGS (max));
817 gst_query_set_latency (query, live, min, max);
820 case GST_QUERY_JITTER:
826 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
832 gst_base_src_query (GstPad * pad, GstQuery * query)
835 GstBaseSrcClass *bclass;
836 gboolean result = FALSE;
838 src = GST_BASE_SRC (gst_pad_get_parent (pad));
840 bclass = GST_BASE_SRC_GET_CLASS (src);
843 result = bclass->query (src, query);
845 result = gst_pad_query_default (pad, query);
847 gst_object_unref (src);
853 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
857 /* update our offset if the start/stop position was updated */
858 if (segment->format == GST_FORMAT_BYTES) {
859 segment->last_stop = segment->start;
860 segment->time = segment->start;
861 } else if (segment->start == 0) {
862 /* seek to start, we can implement a default for this. */
863 segment->last_stop = 0;
873 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
875 GstBaseSrcClass *bclass;
876 gboolean result = FALSE;
878 bclass = GST_BASE_SRC_GET_CLASS (src);
881 result = bclass->do_seek (src, segment);
886 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
889 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
890 GstSegment * segment)
892 /* By default, we try one of 2 things:
893 * - For absolute seek positions, convert the requested position to our
894 * configured processing format and place it in the output segment \
895 * - For relative seek positions, convert our current (input) values to the
896 * seek format, adjust by the relative seek offset and then convert back to
897 * the processing format
899 GstSeekType cur_type, stop_type;
902 GstFormat seek_format, dest_format;
907 gst_event_parse_seek (event, &rate, &seek_format, &flags,
908 &cur_type, &cur, &stop_type, &stop);
909 dest_format = segment->format;
911 if (seek_format == dest_format) {
912 gst_segment_set_seek (segment, rate, seek_format, flags,
913 cur_type, cur, stop_type, stop, &update);
917 if (cur_type != GST_SEEK_TYPE_NONE) {
918 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
920 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
922 cur_type = GST_SEEK_TYPE_SET;
925 if (res && stop_type != GST_SEEK_TYPE_NONE) {
926 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
928 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
930 stop_type = GST_SEEK_TYPE_SET;
933 /* And finally, configure our output segment in the desired format */
934 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
935 stop_type, stop, &update);
944 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
950 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
951 GstSegment * seeksegment)
953 GstBaseSrcClass *bclass;
954 gboolean result = FALSE;
956 bclass = GST_BASE_SRC_GET_CLASS (src);
958 if (bclass->prepare_seek_segment)
959 result = bclass->prepare_seek_segment (src, event, seeksegment);
964 /* this code implements the seeking. It is a good example
965 * handling all cases.
967 * A seek updates the currently configured segment.start
968 * and segment.stop values based on the SEEK_TYPE. If the
969 * segment.start value is updated, a seek to this new position
970 * should be performed.
972 * The seek can only be executed when we are not currently
973 * streaming any data, to make sure that this is the case, we
974 * acquire the STREAM_LOCK which is taken when we are in the
975 * _loop() function or when a getrange() is called. Normally
976 * we will not receive a seek if we are operating in pull mode
979 * When we are in the loop() function, we might be in the middle
980 * of pushing a buffer, which might block in a sink. To make sure
981 * that the push gets unblocked we push out a FLUSH_START event.
982 * Our loop function will get a WRONG_STATE return value from
983 * the push and will pause, effectively releasing the STREAM_LOCK.
985 * For a non-flushing seek, we pause the task, which might eventually
986 * release the STREAM_LOCK. We say eventually because when the sink
987 * blocks on the sample we might wait a very long time until the sink
988 * unblocks the sample. In any case we acquire the STREAM_LOCK and
989 * can continue the seek. A non-flushing seek is normally done in a
990 * running pipeline to perform seamless playback.
991 * In the case of a non-flushing seek we need to make sure that the
992 * data we output after the seek is continuous with the previous data,
993 * this is because a non-flushing seek does not reset the stream-time
994 * to 0. We do this by closing the currently running segment, ie. sending
995 * a new_segment event with the stop position set to the last processed
998 * After updating the segment.start/stop values, we prepare for
999 * streaming again. We push out a FLUSH_STOP to make the peer pad
1000 * accept data again and we start our task again.
1002 * A segment seek posts a message on the bus saying that the playback
1003 * of the segment started. We store the segment flag internally because
1004 * when we reach the segment.stop we have to post a segment.done
1005 * instead of EOS when doing a segment seek.
1007 /* FIXME (0.11), we have the unlock gboolean here because most current
1008 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1009 * the streaming thread isn't running, resulting in bogus unlocks later when it
1010 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1011 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1015 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1017 gboolean res = TRUE;
1019 GstFormat seek_format, dest_format;
1021 GstSeekType cur_type, stop_type;
1025 gboolean relative_seek = FALSE;
1026 gboolean seekseg_configured = FALSE;
1027 GstSegment seeksegment;
1029 GST_DEBUG_OBJECT (src, "doing seek");
1031 dest_format = src->segment.format;
1034 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1035 &cur_type, &cur, &stop_type, &stop);
1037 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1038 SEEK_TYPE_IS_RELATIVE (stop_type);
1040 if (dest_format != seek_format && !relative_seek) {
1041 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1042 * here before taking the stream lock, otherwise we must convert it later,
1043 * once we have the stream lock and can read the current position */
1044 gst_segment_init (&seeksegment, dest_format);
1046 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1047 goto prepare_failed;
1049 seekseg_configured = TRUE;
1052 flush = flags & GST_SEEK_FLAG_FLUSH;
1057 /* send flush start */
1059 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
1061 gst_pad_pause_task (src->srcpad);
1063 /* unblock streaming thread */
1065 gst_base_src_unlock (src);
1067 /* grab streaming lock, this should eventually be possible, either
1068 * because the task is paused or our streaming thread stopped
1069 * because our peer is flushing. */
1070 GST_PAD_STREAM_LOCK (src->srcpad);
1073 gst_base_src_unlock_stop (src);
1075 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1076 * copy the current segment info into the temp segment that we can actually
1077 * attempt the seek with. We only update the real segment if the seek suceeds. */
1078 if (!seekseg_configured) {
1079 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1081 /* now configure the final seek segment */
1083 if (src->segment.format != seek_format) {
1084 /* OK, here's where we give the subclass a chance to convert the relative
1085 * seek into an absolute one in the processing format. We set up any
1086 * absolute seek above, before taking the stream lock. */
1087 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1088 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1093 /* The seek format matches our processing format, no need to ask the
1094 * the subclass to configure the segment. */
1095 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1096 cur_type, cur, stop_type, stop, &update);
1099 /* Else, no seek event passed, so we're just (re)starting the
1104 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1105 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1106 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1108 /* do the seek, segment.last_stop contains the new position. */
1109 res = gst_base_src_do_seek (src, &seeksegment);
1112 /* and prepare to continue streaming */
1114 /* send flush stop, peer will accept data and events again. We
1115 * are not yet providing data as we still have the STREAM_LOCK. */
1116 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
1117 } else if (res && src->data.ABI.running) {
1118 /* we are running the current segment and doing a non-flushing seek,
1119 * close the segment first based on the last_stop. */
1120 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1121 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1123 /* queue the segment for sending in the stream thread */
1124 if (src->priv->close_segment)
1125 gst_event_unref (src->priv->close_segment);
1126 src->priv->close_segment =
1127 gst_event_new_new_segment_full (TRUE,
1128 src->segment.rate, src->segment.applied_rate, src->segment.format,
1129 src->segment.start, src->segment.last_stop, src->segment.time);
1132 /* The subclass must have converted the segment to the processing format
1134 if (res && seeksegment.format != dest_format) {
1135 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1136 "in the correct format. Aborting seek.");
1140 /* if successfull seek, we update our real segment and push
1141 * out the new segment. */
1143 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1145 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1146 gst_element_post_message (GST_ELEMENT (src),
1147 gst_message_new_segment_start (GST_OBJECT (src),
1148 src->segment.format, src->segment.last_stop));
1151 /* for deriving a stop position for the playback segment form the seek
1152 * segment, we must take the duration when the stop is not set */
1153 if ((stop = src->segment.stop) == -1)
1154 stop = src->segment.duration;
1156 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1157 " to %" G_GINT64_FORMAT, src->segment.start, stop);
1159 /* now replace the old segment so that we send it in the stream thread the
1160 * next time it is scheduled. */
1161 if (src->priv->start_segment)
1162 gst_event_unref (src->priv->start_segment);
1163 src->priv->start_segment =
1164 gst_event_new_new_segment_full (FALSE,
1165 src->segment.rate, src->segment.applied_rate, src->segment.format,
1166 src->segment.last_stop, stop, src->segment.time);
1169 src->priv->discont = TRUE;
1170 src->data.ABI.running = TRUE;
1171 /* and restart the task in case it got paused explicitely or by
1172 * the FLUSH_START event we pushed out. */
1173 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1176 /* and release the lock again so we can continue streaming */
1177 GST_PAD_STREAM_UNLOCK (src->srcpad);
1183 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1188 static const GstQueryType *
1189 gst_base_src_get_query_types (GstElement * element)
1191 static const GstQueryType query_types[] = {
1207 /* all events send to this element directly
1210 gst_base_src_send_event (GstElement * element, GstEvent * event)
1213 gboolean result = FALSE;
1215 src = GST_BASE_SRC (element);
1217 switch (GST_EVENT_TYPE (event)) {
1218 case GST_EVENT_FLUSH_START:
1219 case GST_EVENT_FLUSH_STOP:
1220 /* sending random flushes downstream can break stuff,
1221 * especially sync since all segment info will get flushed */
1224 /* FIXME, queue EOS and make sure the task or pull function
1225 * perform the EOS actions. */
1227 case GST_EVENT_NEWSEGMENT:
1228 /* sending random NEWSEGMENT downstream can break sync. */
1231 case GST_EVENT_BUFFERSIZE:
1235 case GST_EVENT_SEEK:
1239 GST_OBJECT_LOCK (src->srcpad);
1240 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1242 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1243 GST_OBJECT_UNLOCK (src->srcpad);
1246 /* when we are running in push mode, we can execute the
1247 * seek right now, we need to unlock. */
1248 result = gst_base_src_perform_seek (src, event, TRUE);
1252 /* else we store the event and execute the seek when we
1254 GST_OBJECT_LOCK (src);
1255 event_p = &src->data.ABI.pending_seek;
1256 gst_event_replace ((GstEvent **) event_p, event);
1257 GST_OBJECT_UNLOCK (src);
1258 /* assume the seek will work */
1263 case GST_EVENT_NAVIGATION:
1269 gst_event_unref (event);
1276 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1277 GST_OBJECT_UNLOCK (src->srcpad);
1284 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1288 switch (GST_EVENT_TYPE (event)) {
1289 case GST_EVENT_SEEK:
1290 /* is normally called when in push mode */
1294 result = gst_base_src_perform_seek (src, event, TRUE);
1296 case GST_EVENT_FLUSH_START:
1297 /* cancel any blocking getrange, is normally called
1298 * when in pull mode. */
1299 result = gst_base_src_unlock (src);
1301 case GST_EVENT_FLUSH_STOP:
1302 result = gst_base_src_unlock_stop (src);
1313 GST_DEBUG_OBJECT (src, "is not seekable");
1319 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1322 GstBaseSrcClass *bclass;
1323 gboolean result = FALSE;
1325 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1326 bclass = GST_BASE_SRC_GET_CLASS (src);
1328 if (bclass->event) {
1329 if (!(result = bclass->event (src, event)))
1330 goto subclass_failed;
1334 gst_event_unref (event);
1335 gst_object_unref (src);
1342 GST_DEBUG_OBJECT (src, "subclass refused event");
1348 gst_base_src_set_property (GObject * object, guint prop_id,
1349 const GValue * value, GParamSpec * pspec)
1353 src = GST_BASE_SRC (object);
1356 case PROP_BLOCKSIZE:
1357 src->blocksize = g_value_get_ulong (value);
1359 case PROP_NUM_BUFFERS:
1360 src->num_buffers = g_value_get_int (value);
1363 src->data.ABI.typefind = g_value_get_boolean (value);
1366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1372 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1377 src = GST_BASE_SRC (object);
1380 case PROP_BLOCKSIZE:
1381 g_value_set_ulong (value, src->blocksize);
1383 case PROP_NUM_BUFFERS:
1384 g_value_set_int (value, src->num_buffers);
1387 g_value_set_boolean (value, src->data.ABI.typefind);
1390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1395 /* with STREAM_LOCK and LOCK */
1396 static GstClockReturn
1397 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1402 id = gst_clock_new_single_shot_id (clock, time);
1404 basesrc->clock_id = id;
1405 /* release the object lock while waiting */
1406 GST_OBJECT_UNLOCK (basesrc);
1408 ret = gst_clock_id_wait (id, NULL);
1410 GST_OBJECT_LOCK (basesrc);
1411 gst_clock_id_unref (id);
1412 basesrc->clock_id = NULL;
1417 /* perform synchronisation on a buffer.
1420 static GstClockReturn
1421 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1423 GstClockReturn result;
1424 GstClockTime start, end;
1425 GstBaseSrcClass *bclass;
1426 GstClockTime base_time;
1429 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1432 if (bclass->get_times)
1433 bclass->get_times (basesrc, buffer, &start, &end);
1435 /* grab the lock to prepare for clocking and calculate the startup
1437 GST_OBJECT_LOCK (basesrc);
1439 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1441 /* get clock, if no clock, we can't sync or get the latency */
1442 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1445 if (basesrc->priv->startup_latency == -1) {
1446 GstClockTime now = gst_clock_get_time (clock);
1448 /* startup latency is the diff between when we went to PLAYING (base_time)
1449 * and the current clock time */
1450 if (now > base_time)
1451 basesrc->priv->startup_latency = now - base_time;
1453 basesrc->priv->startup_latency = 0;
1455 GST_LOG_OBJECT (basesrc, "startup latency: %" GST_TIME_FORMAT,
1456 GST_TIME_ARGS (basesrc->priv->startup_latency));
1459 /* if we don't have a buffer timestamp, we don't sync */
1460 if (!GST_CLOCK_TIME_IS_VALID (start))
1463 /* we have a timestamp, we can subtract it from the startup_latency when it is
1464 * smaller. If the timestamp is bigger, there is no startup latency. */
1465 if (start < basesrc->priv->startup_latency)
1466 basesrc->priv->startup_latency -= start;
1468 basesrc->priv->startup_latency = 0;
1470 GST_LOG_OBJECT (basesrc,
1471 "waiting for clock, base time %" GST_TIME_FORMAT
1472 ", stream_start %" GST_TIME_FORMAT,
1473 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1475 result = gst_base_src_wait (basesrc, clock, start + base_time);
1476 GST_OBJECT_UNLOCK (basesrc);
1478 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1485 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1486 GST_OBJECT_UNLOCK (basesrc);
1487 return GST_CLOCK_OK;
1491 GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1492 GST_OBJECT_UNLOCK (basesrc);
1493 return GST_CLOCK_OK;
1498 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1500 guint64 size, maxsize;
1501 GstBaseSrcClass *bclass;
1503 bclass = GST_BASE_SRC_GET_CLASS (src);
1505 /* only operate if we are working with bytes */
1506 if (src->segment.format != GST_FORMAT_BYTES)
1509 /* get total file size */
1510 size = (guint64) src->segment.duration;
1512 /* the max amount of bytes to read is the total size or
1513 * up to the segment.stop if present. */
1514 if (src->segment.stop != -1)
1515 maxsize = MIN (size, src->segment.stop);
1519 GST_DEBUG_OBJECT (src,
1520 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1521 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1522 *length, size, src->segment.stop, maxsize);
1524 /* check size if we have one */
1525 if (maxsize != -1) {
1526 /* if we run past the end, check if the file became bigger and
1528 if (G_UNLIKELY (offset + *length >= maxsize)) {
1529 /* see if length of the file changed */
1530 if (bclass->get_size)
1531 if (!bclass->get_size (src, &size))
1534 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1536 /* make sure we don't exceed the configured segment stop
1538 if (src->segment.stop != -1)
1539 maxsize = MIN (size, src->segment.stop);
1543 /* if we are at or past the end, EOS */
1544 if (G_UNLIKELY (offset >= maxsize))
1545 goto unexpected_length;
1547 /* else we can clip to the end */
1548 if (G_UNLIKELY (offset + *length >= maxsize))
1549 *length = maxsize - offset;
1554 /* keep track of current position. segment is in bytes, we checked
1556 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1567 static GstFlowReturn
1568 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1572 GstBaseSrcClass *bclass;
1573 GstClockReturn status;
1575 bclass = GST_BASE_SRC_GET_CLASS (src);
1577 ret = gst_base_src_wait_playing (src);
1578 if (ret != GST_FLOW_OK)
1581 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1584 if (G_UNLIKELY (!bclass->create))
1587 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1588 goto unexpected_length;
1590 /* normally we don't count buffers */
1591 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1592 if (src->num_buffers_left == 0)
1593 goto reached_num_buffers;
1595 src->num_buffers_left--;
1598 GST_DEBUG_OBJECT (src,
1599 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1600 G_GINT64_FORMAT, offset, length, src->segment.time);
1602 ret = bclass->create (src, offset, length, buf);
1603 if (G_UNLIKELY (ret != GST_FLOW_OK))
1606 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1607 if (offset == 0 && src->segment.time == 0
1608 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1609 GST_BUFFER_TIMESTAMP (*buf) = 0;
1611 /* now sync before pushing the buffer */
1612 status = gst_base_src_do_sync (src, *buf);
1614 case GST_CLOCK_EARLY:
1615 /* the buffer is too late. We currently don't drop the buffer. */
1616 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1619 /* buffer synchronised properly */
1620 GST_DEBUG_OBJECT (src, "buffer ok");
1622 case GST_CLOCK_UNSCHEDULED:
1623 /* this case is triggered when we were waiting for the clock and
1624 * it got unlocked because we did a state change. We return
1625 * WRONG_STATE in this case to stop the dataflow also get rid of the
1626 * produced buffer. */
1627 GST_DEBUG_OBJECT (src,
1628 "clock was unscheduled (%d), returning WRONG_STATE", status);
1629 gst_buffer_unref (*buf);
1631 ret = GST_FLOW_WRONG_STATE;
1634 /* all other result values are unexpected and errors */
1635 GST_ELEMENT_ERROR (src, CORE, CLOCK,
1636 (_("Internal clock error.")),
1637 ("clock returned unexpected return value %d", status));
1638 gst_buffer_unref (*buf);
1640 ret = GST_FLOW_ERROR;
1649 GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
1654 GST_DEBUG_OBJECT (src, "getrange but not started");
1655 return GST_FLOW_WRONG_STATE;
1659 GST_DEBUG_OBJECT (src, "no create function");
1660 return GST_FLOW_ERROR;
1664 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1665 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1666 return GST_FLOW_UNEXPECTED;
1668 reached_num_buffers:
1670 GST_DEBUG_OBJECT (src, "sent all buffers");
1671 return GST_FLOW_UNEXPECTED;
1675 static GstFlowReturn
1676 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1682 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1684 res = gst_base_src_get_range (src, offset, length, buf);
1686 gst_object_unref (src);
1692 gst_base_src_default_check_get_range (GstBaseSrc * src)
1696 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1697 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1698 if (G_LIKELY (gst_base_src_start (src)))
1699 gst_base_src_stop (src);
1702 /* we can operate in getrange mode if the native format is bytes
1703 * and we are seekable, this condition is set in the random_access
1704 * flag and is set in the _start() method. */
1705 res = src->random_access;
1711 gst_base_src_check_get_range (GstBaseSrc * src)
1713 GstBaseSrcClass *bclass;
1716 bclass = GST_BASE_SRC_GET_CLASS (src);
1718 if (bclass->check_get_range == NULL)
1721 res = bclass->check_get_range (src);
1722 GST_LOG_OBJECT (src, "%s() returned %d",
1723 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1730 GST_WARNING_OBJECT (src, "no check_get_range function set");
1736 gst_base_src_pad_check_get_range (GstPad * pad)
1741 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1743 res = gst_base_src_check_get_range (src);
1745 gst_object_unref (src);
1751 gst_base_src_loop (GstPad * pad)
1754 GstBuffer *buf = NULL;
1761 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1763 src->priv->last_sent_eos = FALSE;
1765 /* if we operate in bytes, we can calculate an offset */
1766 if (src->segment.format == GST_FORMAT_BYTES)
1767 position = src->segment.last_stop;
1771 ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1772 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1773 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
1774 gst_flow_get_name (ret));
1777 /* this should not happen */
1778 if (G_UNLIKELY (buf == NULL))
1781 /* push events to close/start our segment before we push the buffer. */
1782 if (src->priv->close_segment) {
1783 gst_pad_push_event (pad, src->priv->close_segment);
1784 src->priv->close_segment = NULL;
1786 if (src->priv->start_segment) {
1787 gst_pad_push_event (pad, src->priv->start_segment);
1788 src->priv->start_segment = NULL;
1791 /* figure out the new position */
1792 switch (src->segment.format) {
1793 case GST_FORMAT_BYTES:
1794 position += GST_BUFFER_SIZE (buf);
1796 case GST_FORMAT_TIME:
1798 GstClockTime start, duration;
1800 start = GST_BUFFER_TIMESTAMP (buf);
1801 duration = GST_BUFFER_DURATION (buf);
1803 if (GST_CLOCK_TIME_IS_VALID (start))
1806 position = src->segment.last_stop;
1808 if (GST_CLOCK_TIME_IS_VALID (duration))
1809 position += duration;
1812 case GST_FORMAT_DEFAULT:
1813 position = GST_BUFFER_OFFSET_END (buf);
1819 if (position != -1) {
1820 if (src->segment.stop != -1) {
1821 if (position >= src->segment.stop) {
1823 position = src->segment.stop;
1826 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1829 if (G_UNLIKELY (src->priv->discont)) {
1830 buf = gst_buffer_make_metadata_writable (buf);
1831 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1832 src->priv->discont = FALSE;
1835 ret = gst_pad_push (pad, buf);
1836 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1837 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
1838 gst_flow_get_name (ret));
1843 GST_INFO_OBJECT (src, "pausing after EOS");
1844 ret = GST_FLOW_UNEXPECTED;
1849 gst_object_unref (src);
1855 const gchar *reason = gst_flow_get_name (ret);
1857 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1858 src->data.ABI.running = FALSE;
1859 gst_pad_pause_task (pad);
1860 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1861 if (ret == GST_FLOW_UNEXPECTED) {
1862 /* perform EOS logic */
1863 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1864 gst_element_post_message (GST_ELEMENT_CAST (src),
1865 gst_message_new_segment_done (GST_OBJECT_CAST (src),
1866 src->segment.format, src->segment.last_stop));
1868 gst_pad_push_event (pad, gst_event_new_eos ());
1869 src->priv->last_sent_eos = TRUE;
1872 /* for fatal errors we post an error message, post the error
1873 * first so the app knows about the error first. */
1874 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1875 (_("Internal data flow error.")),
1876 ("streaming task paused, reason %s (%d)", reason, ret));
1877 gst_pad_push_event (pad, gst_event_new_eos ());
1878 src->priv->last_sent_eos = TRUE;
1885 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1886 (_("Internal data flow error.")), ("element returned NULL buffer"));
1887 /* we finished the segment on error */
1888 src->data.ABI.running = FALSE;
1889 gst_pad_pause_task (pad);
1890 gst_pad_push_event (pad, gst_event_new_eos ());
1891 src->priv->last_sent_eos = TRUE;
1896 /* this will always be called between start() and stop(). So you can rely on
1897 * resources allocated by start() and freed from stop(). This needs to be added
1898 * to the docs at some point. */
1900 gst_base_src_unlock (GstBaseSrc * basesrc)
1902 GstBaseSrcClass *bclass;
1903 gboolean result = TRUE;
1905 GST_DEBUG ("unlock");
1906 /* unblock whatever the subclass is doing */
1907 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1909 result = bclass->unlock (basesrc);
1911 GST_DEBUG ("unschedule clock");
1912 /* and unblock the clock as well, if any */
1913 GST_OBJECT_LOCK (basesrc);
1914 if (basesrc->clock_id) {
1915 gst_clock_id_unschedule (basesrc->clock_id);
1917 GST_OBJECT_UNLOCK (basesrc);
1919 GST_DEBUG ("unlock done");
1924 /* this will always be called between start() and stop(). So you can rely on
1925 * resources allocated by start() and freed from stop(). This needs to be added
1926 * to the docs at some point. */
1928 gst_base_src_unlock_stop (GstBaseSrc * basesrc)
1930 GstBaseSrcClass *bclass;
1931 gboolean result = TRUE;
1933 GST_DEBUG_OBJECT (basesrc, "unlock stop");
1935 /* Finish a previous unblock request, allowing subclasses to flush command
1936 * queues or whatever they need to do */
1937 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1938 if (bclass->unlock_stop)
1939 result = bclass->unlock_stop (basesrc);
1941 GST_DEBUG_OBJECT (basesrc, "unlock stop done");
1946 /* default negotiation code.
1948 * Take intersection between src and sink pads, take first
1952 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1955 GstCaps *caps = NULL;
1956 GstCaps *peercaps = NULL;
1957 gboolean result = FALSE;
1959 /* first see what is possible on our source pad */
1960 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
1961 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1962 /* nothing or anything is allowed, we're done */
1963 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1964 goto no_nego_needed;
1966 /* get the peer caps */
1967 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
1968 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1972 /* get intersection */
1973 icaps = gst_caps_intersect (thiscaps, peercaps);
1974 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
1975 gst_caps_unref (thiscaps);
1976 gst_caps_unref (peercaps);
1978 /* take first (and best, since they are sorted) possibility */
1979 caps = gst_caps_copy_nth (icaps, 0);
1980 gst_caps_unref (icaps);
1983 /* no peer, work with our own caps then */
1987 caps = gst_caps_make_writable (caps);
1988 gst_caps_truncate (caps);
1991 if (!gst_caps_is_empty (caps)) {
1992 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
1993 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1995 if (gst_caps_is_any (caps)) {
1996 /* hmm, still anything, so element can do anything and
1997 * nego is not needed */
1999 } else if (gst_caps_is_fixed (caps)) {
2000 /* yay, fixed caps, use those then */
2001 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2005 gst_caps_unref (caps);
2011 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2013 gst_caps_unref (thiscaps);
2019 gst_base_src_negotiate (GstBaseSrc * basesrc)
2021 GstBaseSrcClass *bclass;
2022 gboolean result = TRUE;
2024 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2026 if (bclass->negotiate)
2027 result = bclass->negotiate (basesrc);
2033 gst_base_src_start (GstBaseSrc * basesrc)
2035 GstBaseSrcClass *bclass;
2039 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2042 GST_DEBUG_OBJECT (basesrc, "starting source");
2044 basesrc->num_buffers_left = basesrc->num_buffers;
2046 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2047 basesrc->data.ABI.running = FALSE;
2049 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2051 result = bclass->start (basesrc);
2056 goto could_not_start;
2058 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2060 /* figure out the size */
2061 if (basesrc->segment.format == GST_FORMAT_BYTES) {
2062 if (bclass->get_size) {
2063 if (!(result = bclass->get_size (basesrc, &size)))
2069 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2070 /* only update the size when operating in bytes, subclass is supposed
2071 * to set duration in the start method for other formats */
2072 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2077 GST_DEBUG_OBJECT (basesrc,
2078 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2079 G_GINT64_FORMAT, basesrc->segment.format, result, size,
2080 basesrc->segment.duration);
2082 /* check if we can seek */
2083 if (bclass->is_seekable)
2084 basesrc->seekable = bclass->is_seekable (basesrc);
2086 basesrc->seekable = FALSE;
2088 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
2090 /* update for random access flag */
2091 basesrc->random_access = basesrc->seekable &&
2092 basesrc->segment.format == GST_FORMAT_BYTES;
2094 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2096 /* run typefind if we are random_access and the typefinding is enabled. */
2097 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2100 caps = gst_type_find_helper (basesrc->srcpad, size);
2101 gst_pad_set_caps (basesrc->srcpad, caps);
2102 gst_caps_unref (caps);
2104 /* use class or default negotiate function */
2105 if (!gst_base_src_negotiate (basesrc))
2106 goto could_not_negotiate;
2114 GST_DEBUG_OBJECT (basesrc, "could not start");
2115 /* subclass is supposed to post a message. We don't have to call _stop. */
2118 could_not_negotiate:
2120 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2121 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2122 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2123 /* we must call stop */
2124 gst_base_src_stop (basesrc);
2130 gst_base_src_stop (GstBaseSrc * basesrc)
2132 GstBaseSrcClass *bclass;
2133 gboolean result = TRUE;
2135 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2138 GST_DEBUG_OBJECT (basesrc, "stopping source");
2140 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2142 result = bclass->stop (basesrc);
2145 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2151 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
2155 GST_LIVE_LOCK (basesrc);
2156 basesrc->live_running = TRUE;
2157 GST_LIVE_SIGNAL (basesrc);
2158 GST_LIVE_UNLOCK (basesrc);
2160 /* step 1, unblock clock sync (if any) */
2161 result = gst_base_src_unlock (basesrc);
2163 /* step 2, make sure streaming finishes */
2164 result &= gst_pad_stop_task (pad);
2166 /* step 3, clear the unblock condition */
2167 result &= gst_base_src_unlock_stop (basesrc);
2173 gst_base_src_activate_push (GstPad * pad, gboolean active)
2175 GstBaseSrc *basesrc;
2178 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2180 /* prepare subclass first */
2182 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2184 if (G_UNLIKELY (!basesrc->can_activate_push))
2185 goto no_push_activation;
2187 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2190 basesrc->priv->last_sent_eos = FALSE;
2192 /* do initial seek, which will start the task */
2193 GST_OBJECT_LOCK (basesrc);
2194 event = basesrc->data.ABI.pending_seek;
2195 basesrc->data.ABI.pending_seek = NULL;
2196 GST_OBJECT_UNLOCK (basesrc);
2198 /* no need to unlock anything, the task is certainly
2199 * not running here. The perform seek code will start the task when
2201 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2205 gst_event_unref (event);
2207 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2208 /* call the unlock function and stop the task */
2209 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
2210 goto deactivate_failed;
2212 /* now we can stop the source */
2213 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2221 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2226 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2231 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2232 gst_base_src_stop (basesrc);
2234 gst_event_unref (event);
2239 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
2244 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2250 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2252 GstBaseSrc *basesrc;
2254 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2256 /* prepare subclass first */
2258 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2259 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2262 /* if not random_access, we cannot operate in pull mode for now */
2263 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2266 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2267 /* call the unlock function. We have no task to stop. */
2268 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
2269 goto deactivate_failed;
2271 /* don't send EOS when going from PAUSED => READY when in pull mode */
2272 basesrc->priv->last_sent_eos = TRUE;
2274 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2282 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2287 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2288 gst_base_src_stop (basesrc);
2293 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
2298 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2303 static GstStateChangeReturn
2304 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2306 GstBaseSrc *basesrc;
2307 GstStateChangeReturn result;
2308 gboolean no_preroll = FALSE;
2310 basesrc = GST_BASE_SRC (element);
2312 switch (transition) {
2313 case GST_STATE_CHANGE_NULL_TO_READY:
2315 case GST_STATE_CHANGE_READY_TO_PAUSED:
2316 GST_LIVE_LOCK (element);
2317 if (basesrc->is_live) {
2319 basesrc->live_running = FALSE;
2321 basesrc->priv->last_sent_eos = FALSE;
2322 basesrc->priv->discont = TRUE;
2323 basesrc->priv->startup_latency = -1;
2324 GST_LIVE_UNLOCK (element);
2326 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2327 GST_LIVE_LOCK (element);
2328 if (basesrc->is_live) {
2329 basesrc->live_running = TRUE;
2330 GST_LIVE_SIGNAL (element);
2332 GST_LIVE_UNLOCK (element);
2339 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2340 transition)) == GST_STATE_CHANGE_FAILURE)
2343 switch (transition) {
2344 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2345 GST_LIVE_LOCK (element);
2346 if (basesrc->is_live) {
2348 basesrc->live_running = FALSE;
2350 GST_LIVE_UNLOCK (element);
2352 case GST_STATE_CHANGE_PAUSED_TO_READY:
2356 /* FIXME, deprecate this behaviour, it is very dangerous.
2357 * the prefered way of sending EOS downstream is by sending
2358 * the EOS event to the element */
2359 if (!basesrc->priv->last_sent_eos) {
2360 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2361 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2362 basesrc->priv->last_sent_eos = TRUE;
2364 event_p = &basesrc->data.ABI.pending_seek;
2365 gst_event_replace (event_p, NULL);
2366 event_p = &basesrc->priv->close_segment;
2367 gst_event_replace (event_p, NULL);
2368 event_p = &basesrc->priv->start_segment;
2369 gst_event_replace (event_p, NULL);
2372 case GST_STATE_CHANGE_READY_TO_NULL:
2378 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2379 result = GST_STATE_CHANGE_NO_PREROLL;
2386 GST_DEBUG_OBJECT (basesrc, "parent failed state change");