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 when
85 * paused discard data, such as audio or video capture devices. A typical live
86 * source also produces data at a fixed rate and thus provides a clock to publish
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 running 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
110 * It only makes sense to implement the ::get_times function if the source is
111 * a live source. The ::get_times function should return timestamps starting
112 * from 0, as if it were a non-live source. The base class will make sure that
113 * the timestamps are transformed into the current running_time.
114 * The base source will then wait for the calculated running_time before pushing
118 * For live sources, the base class will by default report a latency of 0.
119 * For pseudo live sources, the base class will by default measure the difference
120 * between the first buffer timestamp and the start time of get_times and will
121 * report this value as the latency.
122 * Subclasses should override the query function when this behaviour is not
126 * There is only support in #GstBaseSrc for exactly one source pad, which
127 * should be named "src". A source implementation (subclass of #GstBaseSrc)
128 * should install a pad template in its base_init function, like so:
133 * my_element_base_init (gpointer g_class)
135 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
136 * // srctemplate should be a #GstStaticPadTemplate with direction
137 * // #GST_PAD_SRC and name "src"
138 * gst_element_class_add_pad_template (gstelement_class,
139 * gst_static_pad_template_get (&srctemplate));
140 * // see #GstElementDetails
141 * gst_element_class_set_details (gstelement_class, &details);
145 * <title>Controlled shutdown of live sources in applications</title>
147 * Applications that record from a live source may want to stop recording
148 * in a controlled way, so that the recording is stopped, but the data
149 * already in the pipeline is processed to the end (remember that many live
150 * sources would go on recording forever otherwise). For that to happen the
151 * application needs to make the source stop recording and send an EOS
152 * event down the pipeline. The application would then wait for an
153 * EOS message posted on the pipeline's bus to know when all data has
154 * been processed and the pipeline can safely be stopped.
157 * Since GStreamer 0.10.16 an application may send an EOS event to a source
158 * element to make it send an EOS event downstream. This can typically be done
159 * with the gst_element_send_event() function on the element or its parent bin.
162 * After the EOS has been sent to the element, the application should wait for
163 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
164 * received, it may safely shut down the entire pipeline.
167 * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
168 * is still available but deprecated as it is dangerous and less flexible.
171 * Last reviewed on 2007-12-19 (0.10.16)
183 #include "gstbasesrc.h"
184 #include "gsttypefindhelper.h"
185 #include <gst/gstmarshal.h>
186 #include <gst/gst-i18n-lib.h>
188 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
189 #define GST_CAT_DEFAULT gst_base_src_debug
191 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
192 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
193 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
194 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
195 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
196 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
197 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
199 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
200 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
202 /* BaseSrc signals and args */
209 #define DEFAULT_BLOCKSIZE 4096
210 #define DEFAULT_NUM_BUFFERS -1
211 #define DEFAULT_TYPEFIND FALSE
212 #define DEFAULT_DO_TIMESTAMP FALSE
223 #define GST_BASE_SRC_GET_PRIVATE(obj) \
224 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
226 struct _GstBaseSrcPrivate
228 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
229 * to avoid the sending of two EOS in some cases) */
233 /* two segments to be sent in the streaming thread with STREAM_LOCK */
234 GstEvent *close_segment;
235 GstEvent *start_segment;
237 /* if EOS is pending */
238 gboolean pending_eos;
240 /* startup latency is the time it takes between going to PLAYING and producing
241 * the first BUFFER with running_time 0. This value is included in the latency
243 GstClockTime latency;
244 /* timestamp offset, this is the offset add to the values of gst_times for
245 * pseudo live sources */
246 GstClockTimeDiff ts_offset;
248 gboolean do_timestamp;
251 static GstElementClass *parent_class = NULL;
253 static void gst_base_src_base_init (gpointer g_class);
254 static void gst_base_src_class_init (GstBaseSrcClass * klass);
255 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
256 static void gst_base_src_finalize (GObject * object);
260 gst_base_src_get_type (void)
262 static GType base_src_type = 0;
264 if (G_UNLIKELY (base_src_type == 0)) {
265 static const GTypeInfo base_src_info = {
266 sizeof (GstBaseSrcClass),
267 (GBaseInitFunc) gst_base_src_base_init,
269 (GClassInitFunc) gst_base_src_class_init,
274 (GInstanceInitFunc) gst_base_src_init,
277 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
278 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
280 return base_src_type;
282 static GstCaps *gst_base_src_getcaps (GstPad * pad);
283 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
284 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
286 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
287 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
288 static void gst_base_src_set_property (GObject * object, guint prop_id,
289 const GValue * value, GParamSpec * pspec);
290 static void gst_base_src_get_property (GObject * object, guint prop_id,
291 GValue * value, GParamSpec * pspec);
292 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
293 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
294 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
295 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
297 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
299 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
300 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
301 GstSegment * segment);
302 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
303 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
304 GstEvent * event, GstSegment * segment);
306 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
307 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
308 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
309 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
311 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
312 GstStateChange transition);
314 static void gst_base_src_loop (GstPad * pad);
315 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
316 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
317 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
318 guint length, GstBuffer ** buf);
319 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
320 guint length, GstBuffer ** buf);
323 gst_base_src_base_init (gpointer g_class)
325 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
329 gst_base_src_class_init (GstBaseSrcClass * klass)
331 GObjectClass *gobject_class;
332 GstElementClass *gstelement_class;
334 gobject_class = G_OBJECT_CLASS (klass);
335 gstelement_class = GST_ELEMENT_CLASS (klass);
337 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
339 parent_class = g_type_class_peek_parent (klass);
341 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
342 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
343 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
345 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
346 g_param_spec_ulong ("blocksize", "Block size",
347 "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
348 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
349 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
350 g_param_spec_int ("num-buffers", "num-buffers",
351 "Number of buffers to output before sending EOS", -1, G_MAXINT,
352 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
353 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
354 g_param_spec_boolean ("typefind", "Typefind",
355 "Run typefind before negotiating", DEFAULT_TYPEFIND,
357 g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
358 g_param_spec_boolean ("do-timestamp", "Do timestamp",
359 "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
362 gstelement_class->change_state =
363 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
364 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
365 gstelement_class->get_query_types =
366 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
368 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
369 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
370 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
371 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
372 klass->check_get_range =
373 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
374 klass->prepare_seek_segment =
375 GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
379 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
382 GstPadTemplate *pad_template;
384 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
386 basesrc->is_live = FALSE;
387 basesrc->live_lock = g_mutex_new ();
388 basesrc->live_cond = g_cond_new ();
389 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
390 basesrc->num_buffers_left = -1;
392 basesrc->can_activate_push = TRUE;
393 basesrc->pad_mode = GST_ACTIVATE_NONE;
396 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
397 g_return_if_fail (pad_template != NULL);
399 GST_DEBUG_OBJECT (basesrc, "creating src pad");
400 pad = gst_pad_new_from_template (pad_template, "src");
402 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
403 gst_pad_set_activatepush_function (pad,
404 GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
405 gst_pad_set_activatepull_function (pad,
406 GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
407 gst_pad_set_event_function (pad,
408 GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
409 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
410 gst_pad_set_checkgetrange_function (pad,
411 GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
412 gst_pad_set_getrange_function (pad,
413 GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
414 gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
415 gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
416 gst_pad_set_fixatecaps_function (pad,
417 GST_DEBUG_FUNCPTR (gst_base_src_fixate));
419 /* hold pointer to pad */
420 basesrc->srcpad = pad;
421 GST_DEBUG_OBJECT (basesrc, "adding src pad");
422 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
424 basesrc->blocksize = DEFAULT_BLOCKSIZE;
425 basesrc->clock_id = NULL;
426 /* we operate in BYTES by default */
427 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
428 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
429 basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
431 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
433 GST_DEBUG_OBJECT (basesrc, "init done");
437 gst_base_src_finalize (GObject * object)
442 basesrc = GST_BASE_SRC (object);
444 g_mutex_free (basesrc->live_lock);
445 g_cond_free (basesrc->live_cond);
447 event_p = &basesrc->data.ABI.pending_seek;
448 gst_event_replace (event_p, NULL);
450 G_OBJECT_CLASS (parent_class)->finalize (object);
454 * gst_base_src_wait_playing:
457 * If the #GstBaseSrcClass::create method performs its own synchronisation against
458 * the clock it must unblock when going from PLAYING to the PAUSED state and call
459 * this method before continuing to produce the remaining data.
461 * This function will block until a state change to PLAYING happens (in which
462 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
463 * to a state change to READY or a FLUSH event (in which case this function
464 * returns #GST_FLOW_WRONG_STATE).
468 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
469 * continue. Any other return value should be returned from the create vmethod.
472 gst_base_src_wait_playing (GstBaseSrc * src)
474 /* block until the state changes, or we get a flush, or something */
475 GST_DEBUG_OBJECT (src, "live source waiting for running state");
477 if (src->priv->flushing)
479 GST_DEBUG_OBJECT (src, "live source unlocked");
486 GST_DEBUG_OBJECT (src, "we are flushing");
487 return GST_FLOW_WRONG_STATE;
492 * gst_base_src_set_live:
493 * @src: base source instance
494 * @live: new live-mode
496 * If the element listens to a live source, @live should
499 * A live source will not produce data in the PAUSED state and
500 * will therefore not be able to participate in the PREROLL phase
501 * of a pipeline. To signal this fact to the application and the
502 * pipeline, the state change return value of the live source will
503 * be GST_STATE_CHANGE_NO_PREROLL.
506 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
508 GST_OBJECT_LOCK (src);
510 GST_OBJECT_UNLOCK (src);
514 * gst_base_src_is_live:
515 * @src: base source instance
517 * Check if an element is in live mode.
519 * Returns: %TRUE if element is in live mode.
522 gst_base_src_is_live (GstBaseSrc * src)
526 GST_OBJECT_LOCK (src);
527 result = src->is_live;
528 GST_OBJECT_UNLOCK (src);
534 * gst_base_src_set_format:
535 * @src: base source instance
536 * @format: the format to use
538 * Sets the default format of the source. This will be the format used
539 * for sending NEW_SEGMENT events and for performing seeks.
541 * If a format of GST_FORMAT_BYTES is set, the element will be able to
542 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
547 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
549 gst_segment_init (&src->segment, format);
553 * gst_base_src_query_latency:
555 * @live: if the source is live
556 * @min_latency: the min latency of the source
557 * @max_latency: the max latency of the source
559 * Query the source for the latency parameters. @live will be TRUE when @src is
560 * configured as a live source. @min_latency will be set to the difference
561 * between the running time and the timestamp of the first buffer.
562 * @max_latency is always the undefined value of -1.
564 * This function is mostly used by subclasses.
566 * Returns: TRUE if the query succeeded.
571 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
572 GstClockTime * min_latency, GstClockTime * max_latency)
576 GST_OBJECT_LOCK (src);
578 *live = src->is_live;
580 /* if we have a startup latency, report this one, else report 0. Subclasses
581 * are supposed to override the query function if they want something
583 if (src->priv->latency != -1)
584 min = src->priv->latency;
593 GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
594 ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
596 GST_OBJECT_UNLOCK (src);
602 * gst_base_src_set_do_timestamp:
604 * @timestamp: enable or disable timestamping
606 * Configure @src to automatically timestamp outgoing buffers based on the
607 * current running_time of the pipeline. This property is mostly useful for live
613 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
615 GST_OBJECT_LOCK (src);
616 src->priv->do_timestamp = timestamp;
617 GST_OBJECT_UNLOCK (src);
621 * gst_base_src_get_do_timestamp:
624 * Query if @src timestamps outgoing buffers based on the current running_time.
626 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
631 gst_base_src_get_do_timestamp (GstBaseSrc * src)
635 GST_OBJECT_LOCK (src);
636 res = src->priv->do_timestamp;
637 GST_OBJECT_UNLOCK (src);
643 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
645 GstBaseSrcClass *bclass;
649 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
650 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
652 if (bclass->set_caps)
653 res = bclass->set_caps (bsrc, caps);
659 gst_base_src_getcaps (GstPad * pad)
661 GstBaseSrcClass *bclass;
663 GstCaps *caps = NULL;
665 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
666 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
667 if (bclass->get_caps)
668 caps = bclass->get_caps (bsrc);
671 GstPadTemplate *pad_template;
674 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
675 if (pad_template != NULL) {
676 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
683 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
685 GstBaseSrcClass *bclass;
688 bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
689 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
692 bclass->fixate (bsrc, caps);
694 gst_object_unref (bsrc);
698 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
702 switch (GST_QUERY_TYPE (query)) {
703 case GST_QUERY_POSITION:
707 gst_query_parse_position (query, &format, NULL);
709 case GST_FORMAT_PERCENT:
715 position = src->segment.last_stop;
716 duration = src->segment.duration;
718 if (position != -1 && duration != -1) {
719 if (position < duration)
720 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
723 percent = GST_FORMAT_PERCENT_MAX;
727 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
735 position = src->segment.last_stop;
737 if (position != -1) {
738 /* convert to requested format */
740 gst_pad_query_convert (src->srcpad, src->segment.format,
741 position, &format, &position);
745 gst_query_set_position (query, format, position);
751 case GST_QUERY_DURATION:
755 gst_query_parse_duration (query, &format, NULL);
757 GST_DEBUG_OBJECT (src, "duration query in format %s",
758 gst_format_get_name (format));
760 case GST_FORMAT_PERCENT:
761 gst_query_set_duration (query, GST_FORMAT_PERCENT,
762 GST_FORMAT_PERCENT_MAX);
769 /* this is the duration as configured by the subclass. */
770 duration = src->segment.duration;
772 if (duration != -1) {
773 /* convert to requested format, if this fails, we have a duration
774 * but we cannot answer the query, we must return FALSE. */
776 gst_pad_query_convert (src->srcpad, src->segment.format,
777 duration, &format, &duration);
779 /* The subclass did not configure a duration, we assume that the
780 * media has an unknown duration then and we return TRUE to report
781 * this. Note that this is not the same as returning FALSE, which
782 * means that we cannot report the duration at all. */
785 gst_query_set_duration (query, format, duration);
792 case GST_QUERY_SEEKING:
794 gst_query_set_seeking (query, src->segment.format,
795 src->seekable, 0, src->segment.duration);
799 case GST_QUERY_SEGMENT:
803 /* no end segment configured, current duration then */
804 if ((stop = src->segment.stop) == -1)
805 stop = src->segment.duration;
806 start = src->segment.start;
808 /* adjust to stream time */
809 if (src->segment.time != -1) {
810 start -= src->segment.time;
812 stop -= src->segment.time;
814 gst_query_set_segment (query, src->segment.rate, src->segment.format,
820 case GST_QUERY_FORMATS:
822 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
823 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
827 case GST_QUERY_CONVERT:
829 GstFormat src_fmt, dest_fmt;
830 gint64 src_val, dest_val;
832 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
834 /* we can only convert between equal formats... */
835 if (src_fmt == dest_fmt) {
841 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
844 case GST_QUERY_LATENCY:
846 GstClockTime min, max;
849 /* Subclasses should override and implement something usefull */
850 res = gst_base_src_query_latency (src, &live, &min, &max);
852 GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
853 ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
854 GST_TIME_ARGS (max));
856 gst_query_set_latency (query, live, min, max);
859 case GST_QUERY_JITTER:
865 GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
871 gst_base_src_query (GstPad * pad, GstQuery * query)
874 GstBaseSrcClass *bclass;
875 gboolean result = FALSE;
877 src = GST_BASE_SRC (gst_pad_get_parent (pad));
879 bclass = GST_BASE_SRC_GET_CLASS (src);
882 result = bclass->query (src, query);
884 result = gst_pad_query_default (pad, query);
886 gst_object_unref (src);
892 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
896 /* update our offset if the start/stop position was updated */
897 if (segment->format == GST_FORMAT_BYTES) {
898 segment->last_stop = segment->start;
899 segment->time = segment->start;
900 } else if (segment->start == 0) {
901 /* seek to start, we can implement a default for this. */
902 segment->last_stop = 0;
912 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
914 GstBaseSrcClass *bclass;
915 gboolean result = FALSE;
917 bclass = GST_BASE_SRC_GET_CLASS (src);
920 result = bclass->do_seek (src, segment);
925 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
928 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
929 GstSegment * segment)
931 /* By default, we try one of 2 things:
932 * - For absolute seek positions, convert the requested position to our
933 * configured processing format and place it in the output segment \
934 * - For relative seek positions, convert our current (input) values to the
935 * seek format, adjust by the relative seek offset and then convert back to
936 * the processing format
938 GstSeekType cur_type, stop_type;
941 GstFormat seek_format, dest_format;
946 gst_event_parse_seek (event, &rate, &seek_format, &flags,
947 &cur_type, &cur, &stop_type, &stop);
948 dest_format = segment->format;
950 if (seek_format == dest_format) {
951 gst_segment_set_seek (segment, rate, seek_format, flags,
952 cur_type, cur, stop_type, stop, &update);
956 if (cur_type != GST_SEEK_TYPE_NONE) {
957 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
959 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
961 cur_type = GST_SEEK_TYPE_SET;
964 if (res && stop_type != GST_SEEK_TYPE_NONE) {
965 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
967 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
969 stop_type = GST_SEEK_TYPE_SET;
972 /* And finally, configure our output segment in the desired format */
973 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
974 stop_type, stop, &update);
983 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
989 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
990 GstSegment * seeksegment)
992 GstBaseSrcClass *bclass;
993 gboolean result = FALSE;
995 bclass = GST_BASE_SRC_GET_CLASS (src);
997 if (bclass->prepare_seek_segment)
998 result = bclass->prepare_seek_segment (src, event, seeksegment);
1003 /* this code implements the seeking. It is a good example
1004 * handling all cases.
1006 * A seek updates the currently configured segment.start
1007 * and segment.stop values based on the SEEK_TYPE. If the
1008 * segment.start value is updated, a seek to this new position
1009 * should be performed.
1011 * The seek can only be executed when we are not currently
1012 * streaming any data, to make sure that this is the case, we
1013 * acquire the STREAM_LOCK which is taken when we are in the
1014 * _loop() function or when a getrange() is called. Normally
1015 * we will not receive a seek if we are operating in pull mode
1016 * though. When we operate as a live source we might block on the live
1017 * cond, which does not release the STREAM_LOCK. Therefore we will try
1018 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1019 * safe to perform the seek.
1021 * When we are in the loop() function, we might be in the middle
1022 * of pushing a buffer, which might block in a sink. To make sure
1023 * that the push gets unblocked we push out a FLUSH_START event.
1024 * Our loop function will get a WRONG_STATE return value from
1025 * the push and will pause, effectively releasing the STREAM_LOCK.
1027 * For a non-flushing seek, we pause the task, which might eventually
1028 * release the STREAM_LOCK. We say eventually because when the sink
1029 * blocks on the sample we might wait a very long time until the sink
1030 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1031 * can continue the seek. A non-flushing seek is normally done in a
1032 * running pipeline to perform seamless playback, this means that the sink is
1033 * PLAYING and will return from its chain function.
1034 * In the case of a non-flushing seek we need to make sure that the
1035 * data we output after the seek is continuous with the previous data,
1036 * this is because a non-flushing seek does not reset the running-time
1037 * to 0. We do this by closing the currently running segment, ie. sending
1038 * a new_segment event with the stop position set to the last processed
1041 * After updating the segment.start/stop values, we prepare for
1042 * streaming again. We push out a FLUSH_STOP to make the peer pad
1043 * accept data again and we start our task again.
1045 * A segment seek posts a message on the bus saying that the playback
1046 * of the segment started. We store the segment flag internally because
1047 * when we reach the segment.stop we have to post a segment.done
1048 * instead of EOS when doing a segment seek.
1050 /* FIXME (0.11), we have the unlock gboolean here because most current
1051 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1052 * the streaming thread isn't running, resulting in bogus unlocks later when it
1053 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1054 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1058 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1060 gboolean res = TRUE;
1062 GstFormat seek_format, dest_format;
1064 GstSeekType cur_type, stop_type;
1066 gboolean flush, playing;
1068 gboolean relative_seek = FALSE;
1069 gboolean seekseg_configured = FALSE;
1070 GstSegment seeksegment;
1072 GST_DEBUG_OBJECT (src, "doing seek");
1074 dest_format = src->segment.format;
1077 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1078 &cur_type, &cur, &stop_type, &stop);
1080 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1081 SEEK_TYPE_IS_RELATIVE (stop_type);
1083 if (dest_format != seek_format && !relative_seek) {
1084 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1085 * here before taking the stream lock, otherwise we must convert it later,
1086 * once we have the stream lock and can read the current position */
1087 gst_segment_init (&seeksegment, dest_format);
1089 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1090 goto prepare_failed;
1092 seekseg_configured = TRUE;
1095 flush = flags & GST_SEEK_FLAG_FLUSH;
1100 /* send flush start */
1102 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
1104 gst_pad_pause_task (src->srcpad);
1106 /* unblock streaming thread. */
1107 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1109 /* grab streaming lock, this should eventually be possible, either
1110 * because the task is paused, our streaming thread stopped
1111 * or because our peer is flushing. */
1112 GST_PAD_STREAM_LOCK (src->srcpad);
1114 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1116 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1117 * copy the current segment info into the temp segment that we can actually
1118 * attempt the seek with. We only update the real segment if the seek suceeds. */
1119 if (!seekseg_configured) {
1120 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1122 /* now configure the final seek segment */
1124 if (src->segment.format != seek_format) {
1125 /* OK, here's where we give the subclass a chance to convert the relative
1126 * seek into an absolute one in the processing format. We set up any
1127 * absolute seek above, before taking the stream lock. */
1128 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1129 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1134 /* The seek format matches our processing format, no need to ask the
1135 * the subclass to configure the segment. */
1136 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1137 cur_type, cur, stop_type, stop, &update);
1140 /* Else, no seek event passed, so we're just (re)starting the
1145 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1146 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1147 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1149 /* do the seek, segment.last_stop contains the new position. */
1150 res = gst_base_src_do_seek (src, &seeksegment);
1153 /* and prepare to continue streaming */
1155 /* send flush stop, peer will accept data and events again. We
1156 * are not yet providing data as we still have the STREAM_LOCK. */
1157 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
1158 } else if (res && src->data.ABI.running) {
1159 /* we are running the current segment and doing a non-flushing seek,
1160 * close the segment first based on the last_stop. */
1161 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1162 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1164 /* queue the segment for sending in the stream thread */
1165 if (src->priv->close_segment)
1166 gst_event_unref (src->priv->close_segment);
1167 src->priv->close_segment =
1168 gst_event_new_new_segment_full (TRUE,
1169 src->segment.rate, src->segment.applied_rate, src->segment.format,
1170 src->segment.start, src->segment.last_stop, src->segment.time);
1173 /* The subclass must have converted the segment to the processing format
1175 if (res && seeksegment.format != dest_format) {
1176 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1177 "in the correct format. Aborting seek.");
1181 /* if successfull seek, we update our real segment and push
1182 * out the new segment. */
1184 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1186 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1187 gst_element_post_message (GST_ELEMENT (src),
1188 gst_message_new_segment_start (GST_OBJECT (src),
1189 src->segment.format, src->segment.last_stop));
1192 /* for deriving a stop position for the playback segment from the seek
1193 * segment, we must take the duration when the stop is not set */
1194 if ((stop = src->segment.stop) == -1)
1195 stop = src->segment.duration;
1197 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1198 " to %" G_GINT64_FORMAT, src->segment.start, stop);
1200 /* now replace the old segment so that we send it in the stream thread the
1201 * next time it is scheduled. */
1202 if (src->priv->start_segment)
1203 gst_event_unref (src->priv->start_segment);
1204 src->priv->start_segment =
1205 gst_event_new_new_segment_full (FALSE,
1206 src->segment.rate, src->segment.applied_rate, src->segment.format,
1207 src->segment.last_stop, stop, src->segment.time);
1210 src->priv->discont = TRUE;
1211 src->data.ABI.running = TRUE;
1212 /* and restart the task in case it got paused explicitely or by
1213 * the FLUSH_START event we pushed out. */
1214 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1217 /* and release the lock again so we can continue streaming */
1218 GST_PAD_STREAM_UNLOCK (src->srcpad);
1224 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1229 static const GstQueryType *
1230 gst_base_src_get_query_types (GstElement * element)
1232 static const GstQueryType query_types[] = {
1248 /* all events send to this element directly. This is mainly done from the
1252 gst_base_src_send_event (GstElement * element, GstEvent * event)
1255 gboolean result = FALSE;
1257 src = GST_BASE_SRC (element);
1259 switch (GST_EVENT_TYPE (event)) {
1260 /* bidirectional events */
1261 case GST_EVENT_FLUSH_START:
1262 case GST_EVENT_FLUSH_STOP:
1263 /* sending random flushes downstream can break stuff,
1264 * especially sync since all segment info will get flushed */
1267 /* downstream serialized events */
1269 /* queue EOS and make sure the task or pull function
1270 * performs the EOS actions. */
1271 GST_LIVE_LOCK (src);
1272 src->priv->pending_eos = TRUE;
1273 GST_LIVE_UNLOCK (src);
1276 case GST_EVENT_NEWSEGMENT:
1277 /* sending random NEWSEGMENT downstream can break sync. */
1280 /* sending tags could be useful, FIXME insert in dataflow */
1282 case GST_EVENT_BUFFERSIZE:
1283 /* does not seem to make much sense currently */
1286 /* upstream events */
1288 /* elements should override send_event and do something */
1290 case GST_EVENT_SEEK:
1294 GST_OBJECT_LOCK (src->srcpad);
1295 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1297 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1298 GST_OBJECT_UNLOCK (src->srcpad);
1301 /* when we are running in push mode, we can execute the
1302 * seek right now, we need to unlock. */
1303 result = gst_base_src_perform_seek (src, event, TRUE);
1307 /* else we store the event and execute the seek when we
1309 GST_OBJECT_LOCK (src);
1310 event_p = &src->data.ABI.pending_seek;
1311 gst_event_replace ((GstEvent **) event_p, event);
1312 GST_OBJECT_UNLOCK (src);
1313 /* assume the seek will work */
1318 case GST_EVENT_NAVIGATION:
1319 /* could make sense for elements that do something with navigation events
1320 * but then they would need to override the send_event function */
1322 case GST_EVENT_LATENCY:
1323 /* does not seem to make sense currently */
1327 case GST_EVENT_CUSTOM_UPSTREAM:
1328 /* override send_event if you want this */
1330 case GST_EVENT_CUSTOM_DOWNSTREAM:
1331 case GST_EVENT_CUSTOM_BOTH:
1332 /* FIXME, insert event in the dataflow */
1334 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1335 case GST_EVENT_CUSTOM_BOTH_OOB:
1336 /* insert a random custom event into the pipeline */
1337 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1338 result = gst_pad_push_event (src->srcpad, event);
1339 /* we gave away the ref to the event in the push */
1346 /* if we still have a ref to the event, unref it now */
1348 gst_event_unref (event);
1355 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1356 GST_OBJECT_UNLOCK (src->srcpad);
1363 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1367 switch (GST_EVENT_TYPE (event)) {
1368 case GST_EVENT_SEEK:
1369 /* is normally called when in push mode */
1373 result = gst_base_src_perform_seek (src, event, TRUE);
1375 case GST_EVENT_FLUSH_START:
1376 /* cancel any blocking getrange, is normally called
1377 * when in pull mode. */
1378 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1380 case GST_EVENT_FLUSH_STOP:
1381 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1392 GST_DEBUG_OBJECT (src, "is not seekable");
1398 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1401 GstBaseSrcClass *bclass;
1402 gboolean result = FALSE;
1404 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1405 bclass = GST_BASE_SRC_GET_CLASS (src);
1407 if (bclass->event) {
1408 if (!(result = bclass->event (src, event)))
1409 goto subclass_failed;
1413 gst_event_unref (event);
1414 gst_object_unref (src);
1421 GST_DEBUG_OBJECT (src, "subclass refused event");
1427 gst_base_src_set_property (GObject * object, guint prop_id,
1428 const GValue * value, GParamSpec * pspec)
1432 src = GST_BASE_SRC (object);
1435 case PROP_BLOCKSIZE:
1436 src->blocksize = g_value_get_ulong (value);
1438 case PROP_NUM_BUFFERS:
1439 src->num_buffers = g_value_get_int (value);
1442 src->data.ABI.typefind = g_value_get_boolean (value);
1444 case PROP_DO_TIMESTAMP:
1445 src->priv->do_timestamp = g_value_get_boolean (value);
1448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1454 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1459 src = GST_BASE_SRC (object);
1462 case PROP_BLOCKSIZE:
1463 g_value_set_ulong (value, src->blocksize);
1465 case PROP_NUM_BUFFERS:
1466 g_value_set_int (value, src->num_buffers);
1469 g_value_set_boolean (value, src->data.ABI.typefind);
1471 case PROP_DO_TIMESTAMP:
1472 g_value_set_boolean (value, src->priv->do_timestamp);
1475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1480 /* with STREAM_LOCK and LOCK */
1481 static GstClockReturn
1482 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1487 id = gst_clock_new_single_shot_id (clock, time);
1489 basesrc->clock_id = id;
1490 /* release the live lock while waiting */
1491 GST_LIVE_UNLOCK (basesrc);
1493 ret = gst_clock_id_wait (id, NULL);
1495 GST_LIVE_LOCK (basesrc);
1496 gst_clock_id_unref (id);
1497 basesrc->clock_id = NULL;
1502 /* perform synchronisation on a buffer.
1505 static GstClockReturn
1506 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1508 GstClockReturn result;
1509 GstClockTime start, end;
1510 GstBaseSrcClass *bclass;
1511 GstClockTime base_time;
1513 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1514 gboolean do_timestamp, first, pseudo_live;
1516 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1519 if (bclass->get_times)
1520 bclass->get_times (basesrc, buffer, &start, &end);
1522 /* get buffer timestamp */
1523 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1525 /* grab the lock to prepare for clocking and calculate the startup
1527 GST_OBJECT_LOCK (basesrc);
1529 /* if we are asked to sync against the clock we are a pseudo live element */
1530 pseudo_live = (start != -1 && basesrc->is_live);
1531 /* check for the first buffer */
1532 first = (basesrc->priv->latency == -1);
1534 if (timestamp != -1 && pseudo_live) {
1535 GstClockTime latency;
1537 /* we have a timestamp and a sync time, latency is the diff */
1538 if (timestamp <= start)
1539 latency = start - timestamp;
1544 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1545 GST_TIME_ARGS (latency));
1546 /* first time we calculate latency, just configure */
1547 basesrc->priv->latency = latency;
1549 if (basesrc->priv->latency != latency) {
1550 /* we have a new latency, FIXME post latency message */
1551 basesrc->priv->latency = latency;
1552 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1553 GST_TIME_ARGS (latency));
1557 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1558 basesrc->is_live, start != -1);
1559 basesrc->priv->latency = 0;
1562 /* get clock, if no clock, we can't sync or do timestamps */
1563 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1566 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1568 do_timestamp = basesrc->priv->do_timestamp;
1570 /* first buffer, calculate the timestamp offset */
1572 GstClockTime running_time;
1574 now = gst_clock_get_time (clock);
1575 running_time = now - base_time;
1577 GST_LOG_OBJECT (basesrc,
1578 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1579 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1580 GST_TIME_ARGS (running_time));
1582 if (pseudo_live && timestamp != -1) {
1583 /* live source and we need to sync, add startup latency to all timestamps
1584 * to get the real running_time. Live sources should always timestamp
1585 * according to the current running time. */
1586 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1588 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1589 GST_TIME_ARGS (basesrc->priv->ts_offset));
1591 basesrc->priv->ts_offset = 0;
1592 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1595 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1597 timestamp = running_time;
1601 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1603 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1604 GST_TIME_ARGS (timestamp));
1607 /* add the timestamp offset we need for sync */
1608 timestamp += basesrc->priv->ts_offset;
1610 /* not the first buffer, the timestamp is the diff between the clock and
1612 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1613 now = gst_clock_get_time (clock);
1615 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1617 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1618 GST_TIME_ARGS (now - base_time));
1622 /* if we don't have a buffer timestamp, we don't sync */
1623 if (!GST_CLOCK_TIME_IS_VALID (start))
1626 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1627 /* for pseudo live sources, add our ts_offset to the timestamp */
1628 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1629 start += basesrc->priv->ts_offset;
1632 GST_LOG_OBJECT (basesrc,
1633 "waiting for clock, base time %" GST_TIME_FORMAT
1634 ", stream_start %" GST_TIME_FORMAT,
1635 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1636 GST_OBJECT_UNLOCK (basesrc);
1638 result = gst_base_src_wait (basesrc, clock, start + base_time);
1640 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1647 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1648 GST_OBJECT_UNLOCK (basesrc);
1649 return GST_CLOCK_OK;
1653 GST_DEBUG_OBJECT (basesrc, "no sync needed");
1654 GST_OBJECT_UNLOCK (basesrc);
1655 return GST_CLOCK_OK;
1660 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1662 guint64 size, maxsize;
1663 GstBaseSrcClass *bclass;
1665 bclass = GST_BASE_SRC_GET_CLASS (src);
1667 /* only operate if we are working with bytes */
1668 if (src->segment.format != GST_FORMAT_BYTES)
1671 /* get total file size */
1672 size = (guint64) src->segment.duration;
1674 /* the max amount of bytes to read is the total size or
1675 * up to the segment.stop if present. */
1676 if (src->segment.stop != -1)
1677 maxsize = MIN (size, src->segment.stop);
1681 GST_DEBUG_OBJECT (src,
1682 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1683 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1684 *length, size, src->segment.stop, maxsize);
1686 /* check size if we have one */
1687 if (maxsize != -1) {
1688 /* if we run past the end, check if the file became bigger and
1690 if (G_UNLIKELY (offset + *length >= maxsize)) {
1691 /* see if length of the file changed */
1692 if (bclass->get_size)
1693 if (!bclass->get_size (src, &size))
1696 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1698 /* make sure we don't exceed the configured segment stop
1700 if (src->segment.stop != -1)
1701 maxsize = MIN (size, src->segment.stop);
1705 /* if we are at or past the end, EOS */
1706 if (G_UNLIKELY (offset >= maxsize))
1707 goto unexpected_length;
1709 /* else we can clip to the end */
1710 if (G_UNLIKELY (offset + *length >= maxsize))
1711 *length = maxsize - offset;
1716 /* keep track of current position. segment is in bytes, we checked
1718 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1729 /* must be called with LIVE_LOCK */
1730 static GstFlowReturn
1731 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1735 GstBaseSrcClass *bclass;
1736 GstClockReturn status;
1738 bclass = GST_BASE_SRC_GET_CLASS (src);
1741 while (G_UNLIKELY (!src->live_running)) {
1742 ret = gst_base_src_wait_playing (src);
1743 if (ret != GST_FLOW_OK)
1748 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1751 if (G_UNLIKELY (!bclass->create))
1754 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1755 goto unexpected_length;
1757 /* normally we don't count buffers */
1758 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1759 if (src->num_buffers_left == 0)
1760 goto reached_num_buffers;
1762 src->num_buffers_left--;
1765 GST_DEBUG_OBJECT (src,
1766 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1767 G_GINT64_FORMAT, offset, length, src->segment.time);
1769 ret = bclass->create (src, offset, length, buf);
1770 if (G_UNLIKELY (ret != GST_FLOW_OK))
1773 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1774 if (offset == 0 && src->segment.time == 0
1775 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1776 GST_BUFFER_TIMESTAMP (*buf) = 0;
1778 /* now sync before pushing the buffer */
1779 status = gst_base_src_do_sync (src, *buf);
1781 /* waiting for the clock could have made us flushing */
1782 if (G_UNLIKELY (src->priv->flushing))
1785 if (G_UNLIKELY (src->priv->pending_eos))
1789 case GST_CLOCK_EARLY:
1790 /* the buffer is too late. We currently don't drop the buffer. */
1791 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1794 /* buffer synchronised properly */
1795 GST_DEBUG_OBJECT (src, "buffer ok");
1797 case GST_CLOCK_UNSCHEDULED:
1798 /* this case is triggered when we were waiting for the clock and
1799 * it got unlocked because we did a state change. We return
1800 * WRONG_STATE in this case to stop the dataflow also get rid of the
1801 * produced buffer. */
1802 GST_DEBUG_OBJECT (src,
1803 "clock was unscheduled (%d), returning WRONG_STATE", status);
1804 gst_buffer_unref (*buf);
1806 ret = GST_FLOW_WRONG_STATE;
1809 /* all other result values are unexpected and errors */
1810 GST_ELEMENT_ERROR (src, CORE, CLOCK,
1811 (_("Internal clock error.")),
1812 ("clock returned unexpected return value %d", status));
1813 gst_buffer_unref (*buf);
1815 ret = GST_FLOW_ERROR;
1823 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
1824 gst_flow_get_name (ret));
1829 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
1830 gst_flow_get_name (ret));
1835 GST_DEBUG_OBJECT (src, "getrange but not started");
1836 return GST_FLOW_WRONG_STATE;
1840 GST_DEBUG_OBJECT (src, "no create function");
1841 return GST_FLOW_ERROR;
1845 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1846 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1847 return GST_FLOW_UNEXPECTED;
1849 reached_num_buffers:
1851 GST_DEBUG_OBJECT (src, "sent all buffers");
1852 return GST_FLOW_UNEXPECTED;
1856 GST_DEBUG_OBJECT (src, "we are flushing");
1857 gst_buffer_unref (*buf);
1859 return GST_FLOW_WRONG_STATE;
1863 GST_DEBUG_OBJECT (src, "we are EOS");
1864 gst_buffer_unref (*buf);
1866 return GST_FLOW_UNEXPECTED;
1870 static GstFlowReturn
1871 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1877 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1879 GST_LIVE_LOCK (src);
1880 if (G_UNLIKELY (src->priv->flushing))
1883 /* if we're EOS, return right away */
1884 if (G_UNLIKELY (src->priv->pending_eos))
1887 res = gst_base_src_get_range (src, offset, length, buf);
1890 GST_LIVE_UNLOCK (src);
1892 gst_object_unref (src);
1899 GST_DEBUG_OBJECT (src, "we are flushing");
1900 res = GST_FLOW_WRONG_STATE;
1905 GST_DEBUG_OBJECT (src, "we are EOS");
1906 res = GST_FLOW_UNEXPECTED;
1912 gst_base_src_default_check_get_range (GstBaseSrc * src)
1916 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1917 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1918 if (G_LIKELY (gst_base_src_start (src)))
1919 gst_base_src_stop (src);
1922 /* we can operate in getrange mode if the native format is bytes
1923 * and we are seekable, this condition is set in the random_access
1924 * flag and is set in the _start() method. */
1925 res = src->random_access;
1931 gst_base_src_check_get_range (GstBaseSrc * src)
1933 GstBaseSrcClass *bclass;
1936 bclass = GST_BASE_SRC_GET_CLASS (src);
1938 if (bclass->check_get_range == NULL)
1941 res = bclass->check_get_range (src);
1942 GST_LOG_OBJECT (src, "%s() returned %d",
1943 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1950 GST_WARNING_OBJECT (src, "no check_get_range function set");
1956 gst_base_src_pad_check_get_range (GstPad * pad)
1961 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1963 res = gst_base_src_check_get_range (src);
1969 gst_base_src_loop (GstPad * pad)
1972 GstBuffer *buf = NULL;
1979 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1981 GST_LIVE_LOCK (src);
1982 if (G_UNLIKELY (src->priv->flushing))
1985 /* if we're EOS, return right away */
1986 if (G_UNLIKELY (src->priv->pending_eos))
1989 src->priv->last_sent_eos = FALSE;
1991 /* if we operate in bytes, we can calculate an offset */
1992 if (src->segment.format == GST_FORMAT_BYTES)
1993 position = src->segment.last_stop;
1997 ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1998 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1999 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2000 gst_flow_get_name (ret));
2001 GST_LIVE_UNLOCK (src);
2004 /* this should not happen */
2005 if (G_UNLIKELY (buf == NULL))
2008 /* push events to close/start our segment before we push the buffer. */
2009 if (G_UNLIKELY (src->priv->close_segment)) {
2010 gst_pad_push_event (pad, src->priv->close_segment);
2011 src->priv->close_segment = NULL;
2013 if (G_UNLIKELY (src->priv->start_segment)) {
2014 gst_pad_push_event (pad, src->priv->start_segment);
2015 src->priv->start_segment = NULL;
2018 /* figure out the new position */
2019 switch (src->segment.format) {
2020 case GST_FORMAT_BYTES:
2021 position += GST_BUFFER_SIZE (buf);
2023 case GST_FORMAT_TIME:
2025 GstClockTime start, duration;
2027 start = GST_BUFFER_TIMESTAMP (buf);
2028 duration = GST_BUFFER_DURATION (buf);
2030 if (GST_CLOCK_TIME_IS_VALID (start))
2033 position = src->segment.last_stop;
2035 if (GST_CLOCK_TIME_IS_VALID (duration))
2036 position += duration;
2039 case GST_FORMAT_DEFAULT:
2040 position = GST_BUFFER_OFFSET_END (buf);
2046 if (position != -1) {
2047 if (src->segment.stop != -1) {
2048 if (position >= src->segment.stop) {
2050 position = src->segment.stop;
2053 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
2056 if (G_UNLIKELY (src->priv->discont)) {
2057 buf = gst_buffer_make_metadata_writable (buf);
2058 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2059 src->priv->discont = FALSE;
2061 GST_LIVE_UNLOCK (src);
2063 ret = gst_pad_push (pad, buf);
2064 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2065 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2066 gst_flow_get_name (ret));
2070 if (G_UNLIKELY (eos)) {
2071 GST_INFO_OBJECT (src, "pausing after end of segment");
2072 ret = GST_FLOW_UNEXPECTED;
2082 GST_DEBUG_OBJECT (src, "we are flushing");
2083 GST_LIVE_UNLOCK (src);
2084 ret = GST_FLOW_WRONG_STATE;
2089 GST_DEBUG_OBJECT (src, "we are EOS");
2090 GST_LIVE_UNLOCK (src);
2091 ret = GST_FLOW_UNEXPECTED;
2096 const gchar *reason = gst_flow_get_name (ret);
2098 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2099 src->data.ABI.running = FALSE;
2100 gst_pad_pause_task (pad);
2101 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2102 if (ret == GST_FLOW_UNEXPECTED) {
2103 /* perform EOS logic */
2104 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2105 gst_element_post_message (GST_ELEMENT_CAST (src),
2106 gst_message_new_segment_done (GST_OBJECT_CAST (src),
2107 src->segment.format, src->segment.last_stop));
2109 gst_pad_push_event (pad, gst_event_new_eos ());
2110 src->priv->last_sent_eos = TRUE;
2113 /* for fatal errors we post an error message, post the error
2114 * first so the app knows about the error first. */
2115 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2116 (_("Internal data flow error.")),
2117 ("streaming task paused, reason %s (%d)", reason, ret));
2118 gst_pad_push_event (pad, gst_event_new_eos ());
2119 src->priv->last_sent_eos = TRUE;
2126 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2127 (_("Internal data flow error.")), ("element returned NULL buffer"));
2128 GST_LIVE_UNLOCK (src);
2129 /* we finished the segment on error */
2130 ret = GST_FLOW_ERROR;
2135 /* default negotiation code.
2137 * Take intersection between src and sink pads, take first
2141 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2144 GstCaps *caps = NULL;
2145 GstCaps *peercaps = NULL;
2146 gboolean result = FALSE;
2148 /* first see what is possible on our source pad */
2149 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
2150 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2151 /* nothing or anything is allowed, we're done */
2152 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2153 goto no_nego_needed;
2155 /* get the peer caps */
2156 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
2157 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2161 /* get intersection */
2162 icaps = gst_caps_intersect (thiscaps, peercaps);
2163 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
2164 gst_caps_unref (thiscaps);
2165 gst_caps_unref (peercaps);
2167 /* take first (and best, since they are sorted) possibility */
2168 caps = gst_caps_copy_nth (icaps, 0);
2169 gst_caps_unref (icaps);
2172 /* no peer, work with our own caps then */
2176 caps = gst_caps_make_writable (caps);
2177 gst_caps_truncate (caps);
2180 if (!gst_caps_is_empty (caps)) {
2181 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2182 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2184 if (gst_caps_is_any (caps)) {
2185 /* hmm, still anything, so element can do anything and
2186 * nego is not needed */
2188 } else if (gst_caps_is_fixed (caps)) {
2189 /* yay, fixed caps, use those then */
2190 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2194 gst_caps_unref (caps);
2200 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2202 gst_caps_unref (thiscaps);
2208 gst_base_src_negotiate (GstBaseSrc * basesrc)
2210 GstBaseSrcClass *bclass;
2211 gboolean result = TRUE;
2213 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2215 if (bclass->negotiate)
2216 result = bclass->negotiate (basesrc);
2222 gst_base_src_start (GstBaseSrc * basesrc)
2224 GstBaseSrcClass *bclass;
2228 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2231 GST_DEBUG_OBJECT (basesrc, "starting source");
2233 basesrc->num_buffers_left = basesrc->num_buffers;
2235 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2236 basesrc->data.ABI.running = FALSE;
2238 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2240 result = bclass->start (basesrc);
2245 goto could_not_start;
2247 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2249 /* figure out the size */
2250 if (basesrc->segment.format == GST_FORMAT_BYTES) {
2251 if (bclass->get_size) {
2252 if (!(result = bclass->get_size (basesrc, &size)))
2258 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2259 /* only update the size when operating in bytes, subclass is supposed
2260 * to set duration in the start method for other formats */
2261 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2266 GST_DEBUG_OBJECT (basesrc,
2267 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2268 G_GINT64_FORMAT, basesrc->segment.format, result, size,
2269 basesrc->segment.duration);
2271 /* check if we can seek */
2272 if (bclass->is_seekable)
2273 basesrc->seekable = bclass->is_seekable (basesrc);
2275 basesrc->seekable = FALSE;
2277 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
2279 /* update for random access flag */
2280 basesrc->random_access = basesrc->seekable &&
2281 basesrc->segment.format == GST_FORMAT_BYTES;
2283 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2285 /* run typefind if we are random_access and the typefinding is enabled. */
2286 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2289 caps = gst_type_find_helper (basesrc->srcpad, size);
2290 gst_pad_set_caps (basesrc->srcpad, caps);
2291 gst_caps_unref (caps);
2293 /* use class or default negotiate function */
2294 if (!gst_base_src_negotiate (basesrc))
2295 goto could_not_negotiate;
2303 GST_DEBUG_OBJECT (basesrc, "could not start");
2304 /* subclass is supposed to post a message. We don't have to call _stop. */
2307 could_not_negotiate:
2309 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2310 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2311 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2312 /* we must call stop */
2313 gst_base_src_stop (basesrc);
2319 gst_base_src_stop (GstBaseSrc * basesrc)
2321 GstBaseSrcClass *bclass;
2322 gboolean result = TRUE;
2324 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2327 GST_DEBUG_OBJECT (basesrc, "stopping source");
2329 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2331 result = bclass->stop (basesrc);
2334 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2339 /* start or stop flushing dataprocessing
2342 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2343 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2345 GstBaseSrcClass *bclass;
2347 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2349 if (flushing && unlock) {
2350 /* unlock any subclasses, we need to do this before grabbing the
2351 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2352 * unlock to the params because of backwards compat (see seek handler)*/
2354 bclass->unlock (basesrc);
2357 /* the live lock is released when we are blocked, waiting for playing or
2358 * when we sync to the clock. */
2359 GST_LIVE_LOCK (basesrc);
2361 *playing = basesrc->live_running;
2362 basesrc->priv->flushing = flushing;
2364 /* if we are locked in the live lock, signal it to make it flush */
2365 basesrc->live_running = TRUE;
2366 /* clear pending EOS if any */
2367 basesrc->priv->pending_eos = FALSE;
2369 /* step 1, now that we have the LIVE lock, clear our unlock request */
2370 if (bclass->unlock_stop)
2371 bclass->unlock_stop (basesrc);
2373 /* step 2, unblock clock sync (if any) or any other blocking thing */
2374 if (basesrc->clock_id)
2375 gst_clock_id_unschedule (basesrc->clock_id);
2377 /* signal the live source that it can start playing */
2378 basesrc->live_running = live_play;
2380 GST_LIVE_SIGNAL (basesrc);
2381 GST_LIVE_UNLOCK (basesrc);
2386 /* the purpose of this function is to make sure that a live source blocks in the
2387 * LIVE lock or leaves the LIVE lock and continues playing. */
2389 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2391 GstBaseSrcClass *bclass;
2393 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2395 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2397 GST_DEBUG_OBJECT (basesrc, "unlock");
2399 bclass->unlock (basesrc);
2402 /* we are now able to grab the LIVE lock, when we get it, we can be
2403 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2405 GST_LIVE_LOCK (basesrc);
2406 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2408 /* unblock clock sync (if any) */
2409 if (basesrc->clock_id)
2410 gst_clock_id_unschedule (basesrc->clock_id);
2412 /* configure what to do when we get to the LIVE lock. */
2413 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2414 basesrc->live_running = live_play;
2419 /* clear our unlock request when going to PLAYING */
2420 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2421 if (bclass->unlock_stop)
2422 bclass->unlock_stop (basesrc);
2424 /* for live sources we restart the timestamp correction */
2425 basesrc->priv->latency = -1;
2426 /* have to restart the task in case it stopped because of the unlock when
2427 * we went to PAUSED. Only do this if we operating in push mode. */
2428 GST_OBJECT_LOCK (basesrc->srcpad);
2429 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2430 GST_OBJECT_UNLOCK (basesrc->srcpad);
2432 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2434 GST_DEBUG_OBJECT (basesrc, "signal");
2435 GST_LIVE_SIGNAL (basesrc);
2437 GST_LIVE_UNLOCK (basesrc);
2443 gst_base_src_activate_push (GstPad * pad, gboolean active)
2445 GstBaseSrc *basesrc;
2448 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2450 /* prepare subclass first */
2452 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2454 if (G_UNLIKELY (!basesrc->can_activate_push))
2455 goto no_push_activation;
2457 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2460 basesrc->priv->last_sent_eos = FALSE;
2461 basesrc->priv->discont = TRUE;
2462 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2464 /* do initial seek, which will start the task */
2465 GST_OBJECT_LOCK (basesrc);
2466 event = basesrc->data.ABI.pending_seek;
2467 basesrc->data.ABI.pending_seek = NULL;
2468 GST_OBJECT_UNLOCK (basesrc);
2470 /* no need to unlock anything, the task is certainly
2471 * not running here. The perform seek code will start the task when
2473 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2477 gst_event_unref (event);
2479 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2481 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2483 gst_pad_stop_task (pad);
2484 /* now we can stop the source */
2485 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2493 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2498 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2503 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2504 gst_base_src_stop (basesrc);
2506 gst_event_unref (event);
2511 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2517 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2519 GstBaseSrc *basesrc;
2521 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2523 /* prepare subclass first */
2525 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2526 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2529 /* if not random_access, we cannot operate in pull mode for now */
2530 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2533 /* stop flushing now but for live sources, still block in the LIVE lock when
2534 * we are not yet PLAYING */
2535 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2537 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2538 /* flush all, there is no task to stop */
2539 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2541 /* don't send EOS when going from PAUSED => READY when in pull mode */
2542 basesrc->priv->last_sent_eos = TRUE;
2544 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2552 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2557 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2558 gst_base_src_stop (basesrc);
2563 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2568 static GstStateChangeReturn
2569 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2571 GstBaseSrc *basesrc;
2572 GstStateChangeReturn result;
2573 gboolean no_preroll = FALSE;
2575 basesrc = GST_BASE_SRC (element);
2577 switch (transition) {
2578 case GST_STATE_CHANGE_NULL_TO_READY:
2580 case GST_STATE_CHANGE_READY_TO_PAUSED:
2581 no_preroll = gst_base_src_is_live (basesrc);
2583 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2584 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
2585 if (gst_base_src_is_live (basesrc)) {
2586 /* now we can start playback */
2587 gst_base_src_set_playing (basesrc, TRUE);
2595 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2596 transition)) == GST_STATE_CHANGE_FAILURE)
2599 switch (transition) {
2600 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2601 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
2602 if (gst_base_src_is_live (basesrc)) {
2603 /* make sure we block in the live lock in PAUSED */
2604 gst_base_src_set_playing (basesrc, FALSE);
2608 case GST_STATE_CHANGE_PAUSED_TO_READY:
2612 /* we don't need to unblock anything here, the pad deactivation code
2613 * already did this */
2615 /* FIXME, deprecate this behaviour, it is very dangerous.
2616 * the prefered way of sending EOS downstream is by sending
2617 * the EOS event to the element */
2618 if (!basesrc->priv->last_sent_eos) {
2619 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2620 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2621 basesrc->priv->last_sent_eos = TRUE;
2623 basesrc->priv->pending_eos = FALSE;
2624 event_p = &basesrc->data.ABI.pending_seek;
2625 gst_event_replace (event_p, NULL);
2626 event_p = &basesrc->priv->close_segment;
2627 gst_event_replace (event_p, NULL);
2628 event_p = &basesrc->priv->start_segment;
2629 gst_event_replace (event_p, NULL);
2632 case GST_STATE_CHANGE_READY_TO_NULL:
2638 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2639 result = GST_STATE_CHANGE_NO_PREROLL;
2646 GST_DEBUG_OBJECT (basesrc, "parent failed state change");