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->time = segment->start;
899 } else if (segment->start == 0) {
900 /* seek to start, we can implement a default for this. */
910 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
912 GstBaseSrcClass *bclass;
913 gboolean result = FALSE;
915 bclass = GST_BASE_SRC_GET_CLASS (src);
918 result = bclass->do_seek (src, segment);
923 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
926 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
927 GstSegment * segment)
929 /* By default, we try one of 2 things:
930 * - For absolute seek positions, convert the requested position to our
931 * configured processing format and place it in the output segment \
932 * - For relative seek positions, convert our current (input) values to the
933 * seek format, adjust by the relative seek offset and then convert back to
934 * the processing format
936 GstSeekType cur_type, stop_type;
939 GstFormat seek_format, dest_format;
944 gst_event_parse_seek (event, &rate, &seek_format, &flags,
945 &cur_type, &cur, &stop_type, &stop);
946 dest_format = segment->format;
948 if (seek_format == dest_format) {
949 gst_segment_set_seek (segment, rate, seek_format, flags,
950 cur_type, cur, stop_type, stop, &update);
954 if (cur_type != GST_SEEK_TYPE_NONE) {
955 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
957 gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
959 cur_type = GST_SEEK_TYPE_SET;
962 if (res && stop_type != GST_SEEK_TYPE_NONE) {
963 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
965 gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
967 stop_type = GST_SEEK_TYPE_SET;
970 /* And finally, configure our output segment in the desired format */
971 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
972 stop_type, stop, &update);
981 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
987 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
988 GstSegment * seeksegment)
990 GstBaseSrcClass *bclass;
991 gboolean result = FALSE;
993 bclass = GST_BASE_SRC_GET_CLASS (src);
995 if (bclass->prepare_seek_segment)
996 result = bclass->prepare_seek_segment (src, event, seeksegment);
1001 /* this code implements the seeking. It is a good example
1002 * handling all cases.
1004 * A seek updates the currently configured segment.start
1005 * and segment.stop values based on the SEEK_TYPE. If the
1006 * segment.start value is updated, a seek to this new position
1007 * should be performed.
1009 * The seek can only be executed when we are not currently
1010 * streaming any data, to make sure that this is the case, we
1011 * acquire the STREAM_LOCK which is taken when we are in the
1012 * _loop() function or when a getrange() is called. Normally
1013 * we will not receive a seek if we are operating in pull mode
1014 * though. When we operate as a live source we might block on the live
1015 * cond, which does not release the STREAM_LOCK. Therefore we will try
1016 * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1017 * safe to perform the seek.
1019 * When we are in the loop() function, we might be in the middle
1020 * of pushing a buffer, which might block in a sink. To make sure
1021 * that the push gets unblocked we push out a FLUSH_START event.
1022 * Our loop function will get a WRONG_STATE return value from
1023 * the push and will pause, effectively releasing the STREAM_LOCK.
1025 * For a non-flushing seek, we pause the task, which might eventually
1026 * release the STREAM_LOCK. We say eventually because when the sink
1027 * blocks on the sample we might wait a very long time until the sink
1028 * unblocks the sample. In any case we acquire the STREAM_LOCK and
1029 * can continue the seek. A non-flushing seek is normally done in a
1030 * running pipeline to perform seamless playback, this means that the sink is
1031 * PLAYING and will return from its chain function.
1032 * In the case of a non-flushing seek we need to make sure that the
1033 * data we output after the seek is continuous with the previous data,
1034 * this is because a non-flushing seek does not reset the running-time
1035 * to 0. We do this by closing the currently running segment, ie. sending
1036 * a new_segment event with the stop position set to the last processed
1039 * After updating the segment.start/stop values, we prepare for
1040 * streaming again. We push out a FLUSH_STOP to make the peer pad
1041 * accept data again and we start our task again.
1043 * A segment seek posts a message on the bus saying that the playback
1044 * of the segment started. We store the segment flag internally because
1045 * when we reach the segment.stop we have to post a segment.done
1046 * instead of EOS when doing a segment seek.
1048 /* FIXME (0.11), we have the unlock gboolean here because most current
1049 * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1050 * the streaming thread isn't running, resulting in bogus unlocks later when it
1051 * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1052 * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1056 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1058 gboolean res = TRUE;
1060 GstFormat seek_format, dest_format;
1062 GstSeekType cur_type, stop_type;
1064 gboolean flush, playing;
1066 gboolean relative_seek = FALSE;
1067 gboolean seekseg_configured = FALSE;
1068 GstSegment seeksegment;
1070 GST_DEBUG_OBJECT (src, "doing seek");
1072 dest_format = src->segment.format;
1075 gst_event_parse_seek (event, &rate, &seek_format, &flags,
1076 &cur_type, &cur, &stop_type, &stop);
1078 relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1079 SEEK_TYPE_IS_RELATIVE (stop_type);
1081 if (dest_format != seek_format && !relative_seek) {
1082 /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1083 * here before taking the stream lock, otherwise we must convert it later,
1084 * once we have the stream lock and can read the current position */
1085 gst_segment_init (&seeksegment, dest_format);
1087 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1088 goto prepare_failed;
1090 seekseg_configured = TRUE;
1093 flush = flags & GST_SEEK_FLAG_FLUSH;
1098 /* send flush start */
1100 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
1102 gst_pad_pause_task (src->srcpad);
1104 /* unblock streaming thread. */
1105 gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1107 /* grab streaming lock, this should eventually be possible, either
1108 * because the task is paused, our streaming thread stopped
1109 * or because our peer is flushing. */
1110 GST_PAD_STREAM_LOCK (src->srcpad);
1112 gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1114 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1115 * copy the current segment info into the temp segment that we can actually
1116 * attempt the seek with. We only update the real segment if the seek suceeds. */
1117 if (!seekseg_configured) {
1118 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1120 /* now configure the final seek segment */
1122 if (src->segment.format != seek_format) {
1123 /* OK, here's where we give the subclass a chance to convert the relative
1124 * seek into an absolute one in the processing format. We set up any
1125 * absolute seek above, before taking the stream lock. */
1126 if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1127 GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1132 /* The seek format matches our processing format, no need to ask the
1133 * the subclass to configure the segment. */
1134 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1135 cur_type, cur, stop_type, stop, &update);
1138 /* Else, no seek event passed, so we're just (re)starting the
1143 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1144 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1145 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1147 /* do the seek, segment.last_stop contains the new position. */
1148 res = gst_base_src_do_seek (src, &seeksegment);
1151 /* and prepare to continue streaming */
1153 /* send flush stop, peer will accept data and events again. We
1154 * are not yet providing data as we still have the STREAM_LOCK. */
1155 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
1156 } else if (res && src->data.ABI.running) {
1157 /* we are running the current segment and doing a non-flushing seek,
1158 * close the segment first based on the last_stop. */
1159 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1160 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1162 /* queue the segment for sending in the stream thread */
1163 if (src->priv->close_segment)
1164 gst_event_unref (src->priv->close_segment);
1165 src->priv->close_segment =
1166 gst_event_new_new_segment_full (TRUE,
1167 src->segment.rate, src->segment.applied_rate, src->segment.format,
1168 src->segment.start, src->segment.last_stop, src->segment.time);
1171 /* The subclass must have converted the segment to the processing format
1173 if (res && seeksegment.format != dest_format) {
1174 GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1175 "in the correct format. Aborting seek.");
1179 /* if successfull seek, we update our real segment and push
1180 * out the new segment. */
1182 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1184 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1185 gst_element_post_message (GST_ELEMENT (src),
1186 gst_message_new_segment_start (GST_OBJECT (src),
1187 src->segment.format, src->segment.last_stop));
1190 /* for deriving a stop position for the playback segment from the seek
1191 * segment, we must take the duration when the stop is not set */
1192 if ((stop = src->segment.stop) == -1)
1193 stop = src->segment.duration;
1195 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1196 " to %" G_GINT64_FORMAT, src->segment.start, stop);
1198 /* now replace the old segment so that we send it in the stream thread the
1199 * next time it is scheduled. */
1200 if (src->priv->start_segment)
1201 gst_event_unref (src->priv->start_segment);
1202 src->priv->start_segment =
1203 gst_event_new_new_segment_full (FALSE,
1204 src->segment.rate, src->segment.applied_rate, src->segment.format,
1205 src->segment.last_stop, stop, src->segment.time);
1208 src->priv->discont = TRUE;
1209 src->data.ABI.running = TRUE;
1210 /* and restart the task in case it got paused explicitely or by
1211 * the FLUSH_START event we pushed out. */
1212 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1215 /* and release the lock again so we can continue streaming */
1216 GST_PAD_STREAM_UNLOCK (src->srcpad);
1222 GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1227 static const GstQueryType *
1228 gst_base_src_get_query_types (GstElement * element)
1230 static const GstQueryType query_types[] = {
1246 /* all events send to this element directly. This is mainly done from the
1250 gst_base_src_send_event (GstElement * element, GstEvent * event)
1253 gboolean result = FALSE;
1255 src = GST_BASE_SRC (element);
1257 switch (GST_EVENT_TYPE (event)) {
1258 /* bidirectional events */
1259 case GST_EVENT_FLUSH_START:
1260 case GST_EVENT_FLUSH_STOP:
1261 /* sending random flushes downstream can break stuff,
1262 * especially sync since all segment info will get flushed */
1265 /* downstream serialized events */
1267 /* queue EOS and make sure the task or pull function
1268 * performs the EOS actions. */
1269 GST_LIVE_LOCK (src);
1270 src->priv->pending_eos = TRUE;
1271 GST_LIVE_UNLOCK (src);
1274 case GST_EVENT_NEWSEGMENT:
1275 /* sending random NEWSEGMENT downstream can break sync. */
1278 /* sending tags could be useful, FIXME insert in dataflow */
1280 case GST_EVENT_BUFFERSIZE:
1281 /* does not seem to make much sense currently */
1284 /* upstream events */
1286 /* elements should override send_event and do something */
1288 case GST_EVENT_SEEK:
1292 GST_OBJECT_LOCK (src->srcpad);
1293 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1295 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1296 GST_OBJECT_UNLOCK (src->srcpad);
1299 /* when we are running in push mode, we can execute the
1300 * seek right now, we need to unlock. */
1301 result = gst_base_src_perform_seek (src, event, TRUE);
1305 /* else we store the event and execute the seek when we
1307 GST_OBJECT_LOCK (src);
1308 event_p = &src->data.ABI.pending_seek;
1309 gst_event_replace ((GstEvent **) event_p, event);
1310 GST_OBJECT_UNLOCK (src);
1311 /* assume the seek will work */
1316 case GST_EVENT_NAVIGATION:
1317 /* could make sense for elements that do something with navigation events
1318 * but then they would need to override the send_event function */
1320 case GST_EVENT_LATENCY:
1321 /* does not seem to make sense currently */
1325 case GST_EVENT_CUSTOM_UPSTREAM:
1326 /* override send_event if you want this */
1328 case GST_EVENT_CUSTOM_DOWNSTREAM:
1329 case GST_EVENT_CUSTOM_BOTH:
1330 /* FIXME, insert event in the dataflow */
1332 case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1333 case GST_EVENT_CUSTOM_BOTH_OOB:
1334 /* insert a random custom event into the pipeline */
1335 GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1336 result = gst_pad_push_event (src->srcpad, event);
1337 /* we gave away the ref to the event in the push */
1344 /* if we still have a ref to the event, unref it now */
1346 gst_event_unref (event);
1353 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1354 GST_OBJECT_UNLOCK (src->srcpad);
1361 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1365 switch (GST_EVENT_TYPE (event)) {
1366 case GST_EVENT_SEEK:
1367 /* is normally called when in push mode */
1371 result = gst_base_src_perform_seek (src, event, TRUE);
1373 case GST_EVENT_FLUSH_START:
1374 /* cancel any blocking getrange, is normally called
1375 * when in pull mode. */
1376 result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1378 case GST_EVENT_FLUSH_STOP:
1379 result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1390 GST_DEBUG_OBJECT (src, "is not seekable");
1396 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1399 GstBaseSrcClass *bclass;
1400 gboolean result = FALSE;
1402 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1403 bclass = GST_BASE_SRC_GET_CLASS (src);
1405 if (bclass->event) {
1406 if (!(result = bclass->event (src, event)))
1407 goto subclass_failed;
1411 gst_event_unref (event);
1412 gst_object_unref (src);
1419 GST_DEBUG_OBJECT (src, "subclass refused event");
1425 gst_base_src_set_property (GObject * object, guint prop_id,
1426 const GValue * value, GParamSpec * pspec)
1430 src = GST_BASE_SRC (object);
1433 case PROP_BLOCKSIZE:
1434 src->blocksize = g_value_get_ulong (value);
1436 case PROP_NUM_BUFFERS:
1437 src->num_buffers = g_value_get_int (value);
1440 src->data.ABI.typefind = g_value_get_boolean (value);
1442 case PROP_DO_TIMESTAMP:
1443 src->priv->do_timestamp = g_value_get_boolean (value);
1446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1452 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1457 src = GST_BASE_SRC (object);
1460 case PROP_BLOCKSIZE:
1461 g_value_set_ulong (value, src->blocksize);
1463 case PROP_NUM_BUFFERS:
1464 g_value_set_int (value, src->num_buffers);
1467 g_value_set_boolean (value, src->data.ABI.typefind);
1469 case PROP_DO_TIMESTAMP:
1470 g_value_set_boolean (value, src->priv->do_timestamp);
1473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1478 /* with STREAM_LOCK and LOCK */
1479 static GstClockReturn
1480 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1485 id = gst_clock_new_single_shot_id (clock, time);
1487 basesrc->clock_id = id;
1488 /* release the live lock while waiting */
1489 GST_LIVE_UNLOCK (basesrc);
1491 ret = gst_clock_id_wait (id, NULL);
1493 GST_LIVE_LOCK (basesrc);
1494 gst_clock_id_unref (id);
1495 basesrc->clock_id = NULL;
1500 /* perform synchronisation on a buffer.
1503 static GstClockReturn
1504 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1506 GstClockReturn result;
1507 GstClockTime start, end;
1508 GstBaseSrcClass *bclass;
1509 GstClockTime base_time;
1511 GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1512 gboolean do_timestamp, first, pseudo_live;
1514 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1517 if (bclass->get_times)
1518 bclass->get_times (basesrc, buffer, &start, &end);
1520 /* get buffer timestamp */
1521 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1523 /* grab the lock to prepare for clocking and calculate the startup
1525 GST_OBJECT_LOCK (basesrc);
1527 /* if we are asked to sync against the clock we are a pseudo live element */
1528 pseudo_live = (start != -1 && basesrc->is_live);
1529 /* check for the first buffer */
1530 first = (basesrc->priv->latency == -1);
1532 if (timestamp != -1 && pseudo_live) {
1533 GstClockTime latency;
1535 /* we have a timestamp and a sync time, latency is the diff */
1536 if (timestamp <= start)
1537 latency = start - timestamp;
1542 GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1543 GST_TIME_ARGS (latency));
1544 /* first time we calculate latency, just configure */
1545 basesrc->priv->latency = latency;
1547 if (basesrc->priv->latency != latency) {
1548 /* we have a new latency, FIXME post latency message */
1549 basesrc->priv->latency = latency;
1550 GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1551 GST_TIME_ARGS (latency));
1555 GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1556 basesrc->is_live, start != -1);
1557 basesrc->priv->latency = 0;
1560 /* get clock, if no clock, we can't sync or do timestamps */
1561 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1564 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1566 do_timestamp = basesrc->priv->do_timestamp;
1568 /* first buffer, calculate the timestamp offset */
1570 GstClockTime running_time;
1572 now = gst_clock_get_time (clock);
1573 running_time = now - base_time;
1575 GST_LOG_OBJECT (basesrc,
1576 "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1577 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1578 GST_TIME_ARGS (running_time));
1580 if (pseudo_live && timestamp != -1) {
1581 /* live source and we need to sync, add startup latency to all timestamps
1582 * to get the real running_time. Live sources should always timestamp
1583 * according to the current running time. */
1584 basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1586 GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1587 GST_TIME_ARGS (basesrc->priv->ts_offset));
1589 basesrc->priv->ts_offset = 0;
1590 GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1593 if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1595 timestamp = running_time;
1599 GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1601 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1602 GST_TIME_ARGS (timestamp));
1605 /* add the timestamp offset we need for sync */
1606 timestamp += basesrc->priv->ts_offset;
1608 /* not the first buffer, the timestamp is the diff between the clock and
1610 if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1611 now = gst_clock_get_time (clock);
1613 GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1615 GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1616 GST_TIME_ARGS (now - base_time));
1620 /* if we don't have a buffer timestamp, we don't sync */
1621 if (!GST_CLOCK_TIME_IS_VALID (start))
1624 if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1625 /* for pseudo live sources, add our ts_offset to the timestamp */
1626 GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1627 start += basesrc->priv->ts_offset;
1630 GST_LOG_OBJECT (basesrc,
1631 "waiting for clock, base time %" GST_TIME_FORMAT
1632 ", stream_start %" GST_TIME_FORMAT,
1633 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1634 GST_OBJECT_UNLOCK (basesrc);
1636 result = gst_base_src_wait (basesrc, clock, start + base_time);
1638 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1645 GST_DEBUG_OBJECT (basesrc, "we have no clock");
1646 GST_OBJECT_UNLOCK (basesrc);
1647 return GST_CLOCK_OK;
1651 GST_DEBUG_OBJECT (basesrc, "no sync needed");
1652 GST_OBJECT_UNLOCK (basesrc);
1653 return GST_CLOCK_OK;
1658 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1660 guint64 size, maxsize;
1661 GstBaseSrcClass *bclass;
1663 bclass = GST_BASE_SRC_GET_CLASS (src);
1665 /* only operate if we are working with bytes */
1666 if (src->segment.format != GST_FORMAT_BYTES)
1669 /* get total file size */
1670 size = (guint64) src->segment.duration;
1672 /* the max amount of bytes to read is the total size or
1673 * up to the segment.stop if present. */
1674 if (src->segment.stop != -1)
1675 maxsize = MIN (size, src->segment.stop);
1679 GST_DEBUG_OBJECT (src,
1680 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1681 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1682 *length, size, src->segment.stop, maxsize);
1684 /* check size if we have one */
1685 if (maxsize != -1) {
1686 /* if we run past the end, check if the file became bigger and
1688 if (G_UNLIKELY (offset + *length >= maxsize)) {
1689 /* see if length of the file changed */
1690 if (bclass->get_size)
1691 if (!bclass->get_size (src, &size))
1694 gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1696 /* make sure we don't exceed the configured segment stop
1698 if (src->segment.stop != -1)
1699 maxsize = MIN (size, src->segment.stop);
1703 /* if we are at or past the end, EOS */
1704 if (G_UNLIKELY (offset >= maxsize))
1705 goto unexpected_length;
1707 /* else we can clip to the end */
1708 if (G_UNLIKELY (offset + *length >= maxsize))
1709 *length = maxsize - offset;
1714 /* keep track of current position. segment is in bytes, we checked
1716 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1727 /* must be called with LIVE_LOCK */
1728 static GstFlowReturn
1729 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1733 GstBaseSrcClass *bclass;
1734 GstClockReturn status;
1736 bclass = GST_BASE_SRC_GET_CLASS (src);
1739 while (G_UNLIKELY (!src->live_running)) {
1740 ret = gst_base_src_wait_playing (src);
1741 if (ret != GST_FLOW_OK)
1746 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1749 if (G_UNLIKELY (!bclass->create))
1752 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1753 goto unexpected_length;
1755 /* normally we don't count buffers */
1756 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1757 if (src->num_buffers_left == 0)
1758 goto reached_num_buffers;
1760 src->num_buffers_left--;
1763 GST_DEBUG_OBJECT (src,
1764 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1765 G_GINT64_FORMAT, offset, length, src->segment.time);
1767 ret = bclass->create (src, offset, length, buf);
1768 if (G_UNLIKELY (ret != GST_FLOW_OK))
1771 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1772 if (offset == 0 && src->segment.time == 0
1773 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1774 GST_BUFFER_TIMESTAMP (*buf) = 0;
1776 /* now sync before pushing the buffer */
1777 status = gst_base_src_do_sync (src, *buf);
1779 /* waiting for the clock could have made us flushing */
1780 if (G_UNLIKELY (src->priv->flushing))
1783 if (G_UNLIKELY (src->priv->pending_eos))
1787 case GST_CLOCK_EARLY:
1788 /* the buffer is too late. We currently don't drop the buffer. */
1789 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1792 /* buffer synchronised properly */
1793 GST_DEBUG_OBJECT (src, "buffer ok");
1795 case GST_CLOCK_UNSCHEDULED:
1796 /* this case is triggered when we were waiting for the clock and
1797 * it got unlocked because we did a state change. We return
1798 * WRONG_STATE in this case to stop the dataflow also get rid of the
1799 * produced buffer. */
1800 GST_DEBUG_OBJECT (src,
1801 "clock was unscheduled (%d), returning WRONG_STATE", status);
1802 gst_buffer_unref (*buf);
1804 ret = GST_FLOW_WRONG_STATE;
1807 /* all other result values are unexpected and errors */
1808 GST_ELEMENT_ERROR (src, CORE, CLOCK,
1809 (_("Internal clock error.")),
1810 ("clock returned unexpected return value %d", status));
1811 gst_buffer_unref (*buf);
1813 ret = GST_FLOW_ERROR;
1821 GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
1822 gst_flow_get_name (ret));
1827 GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
1828 gst_flow_get_name (ret));
1833 GST_DEBUG_OBJECT (src, "getrange but not started");
1834 return GST_FLOW_WRONG_STATE;
1838 GST_DEBUG_OBJECT (src, "no create function");
1839 return GST_FLOW_ERROR;
1843 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1844 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1845 return GST_FLOW_UNEXPECTED;
1847 reached_num_buffers:
1849 GST_DEBUG_OBJECT (src, "sent all buffers");
1850 return GST_FLOW_UNEXPECTED;
1854 GST_DEBUG_OBJECT (src, "we are flushing");
1855 gst_buffer_unref (*buf);
1857 return GST_FLOW_WRONG_STATE;
1861 GST_DEBUG_OBJECT (src, "we are EOS");
1862 gst_buffer_unref (*buf);
1864 return GST_FLOW_UNEXPECTED;
1868 static GstFlowReturn
1869 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1875 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1877 GST_LIVE_LOCK (src);
1878 if (G_UNLIKELY (src->priv->flushing))
1881 /* if we're EOS, return right away */
1882 if (G_UNLIKELY (src->priv->pending_eos))
1885 res = gst_base_src_get_range (src, offset, length, buf);
1888 GST_LIVE_UNLOCK (src);
1890 gst_object_unref (src);
1897 GST_DEBUG_OBJECT (src, "we are flushing");
1898 res = GST_FLOW_WRONG_STATE;
1903 GST_DEBUG_OBJECT (src, "we are EOS");
1904 res = GST_FLOW_UNEXPECTED;
1910 gst_base_src_default_check_get_range (GstBaseSrc * src)
1914 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1915 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1916 if (G_LIKELY (gst_base_src_start (src)))
1917 gst_base_src_stop (src);
1920 /* we can operate in getrange mode if the native format is bytes
1921 * and we are seekable, this condition is set in the random_access
1922 * flag and is set in the _start() method. */
1923 res = src->random_access;
1929 gst_base_src_check_get_range (GstBaseSrc * src)
1931 GstBaseSrcClass *bclass;
1934 bclass = GST_BASE_SRC_GET_CLASS (src);
1936 if (bclass->check_get_range == NULL)
1939 res = bclass->check_get_range (src);
1940 GST_LOG_OBJECT (src, "%s() returned %d",
1941 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1948 GST_WARNING_OBJECT (src, "no check_get_range function set");
1954 gst_base_src_pad_check_get_range (GstPad * pad)
1959 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1961 res = gst_base_src_check_get_range (src);
1967 gst_base_src_loop (GstPad * pad)
1970 GstBuffer *buf = NULL;
1978 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1980 GST_LIVE_LOCK (src);
1981 if (G_UNLIKELY (src->priv->flushing))
1984 /* if we're EOS, return right away */
1985 if (G_UNLIKELY (src->priv->pending_eos))
1988 src->priv->last_sent_eos = FALSE;
1990 blocksize = src->blocksize;
1992 /* if we operate in bytes, we can calculate an offset */
1993 if (src->segment.format == GST_FORMAT_BYTES) {
1994 position = src->segment.last_stop;
1995 /* for negative rates, start with subtracting the blocksize */
1996 if (src->segment.rate < 0.0) {
1997 /* we cannot go below segment.start */
1998 if (position > src->segment.start + blocksize)
1999 position -= blocksize;
2001 /* last block, remainder up to segment.start */
2002 blocksize = position - src->segment.start;
2003 position = src->segment.start;
2009 ret = gst_base_src_get_range (src, position, blocksize, &buf);
2010 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2011 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2012 gst_flow_get_name (ret));
2013 GST_LIVE_UNLOCK (src);
2016 /* this should not happen */
2017 if (G_UNLIKELY (buf == NULL))
2020 /* push events to close/start our segment before we push the buffer. */
2021 if (G_UNLIKELY (src->priv->close_segment)) {
2022 gst_pad_push_event (pad, src->priv->close_segment);
2023 src->priv->close_segment = NULL;
2025 if (G_UNLIKELY (src->priv->start_segment)) {
2026 gst_pad_push_event (pad, src->priv->start_segment);
2027 src->priv->start_segment = NULL;
2030 /* figure out the new position */
2031 switch (src->segment.format) {
2032 case GST_FORMAT_BYTES:
2034 guint bufsize = GST_BUFFER_SIZE (buf);
2036 /* we subtracted above for negative rates */
2037 if (src->segment.rate >= 0.0)
2038 position += bufsize;
2041 case GST_FORMAT_TIME:
2043 GstClockTime start, duration;
2045 start = GST_BUFFER_TIMESTAMP (buf);
2046 duration = GST_BUFFER_DURATION (buf);
2048 if (GST_CLOCK_TIME_IS_VALID (start))
2051 position = src->segment.last_stop;
2053 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2054 if (src->segment.rate >= 0.0)
2055 position += duration;
2056 else if (position > duration)
2057 position -= duration;
2063 case GST_FORMAT_DEFAULT:
2064 if (src->segment.rate >= 0.0)
2065 position = GST_BUFFER_OFFSET_END (buf);
2067 position = GST_BUFFER_OFFSET (buf);
2073 if (position != -1) {
2074 if (src->segment.rate >= 0.0) {
2075 /* positive rate, check if we reached the stop */
2076 if (src->segment.stop != -1) {
2077 if (position >= src->segment.stop) {
2079 position = src->segment.stop;
2083 /* negative rate, check if we reached the start. start is always set to
2084 * something different from -1 */
2085 if (position <= src->segment.start) {
2087 position = src->segment.start;
2089 /* when going reverse, all buffers are DISCONT */
2090 src->priv->discont = TRUE;
2092 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
2095 if (G_UNLIKELY (src->priv->discont)) {
2096 buf = gst_buffer_make_metadata_writable (buf);
2097 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2098 src->priv->discont = FALSE;
2100 GST_LIVE_UNLOCK (src);
2102 ret = gst_pad_push (pad, buf);
2103 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2104 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2105 gst_flow_get_name (ret));
2109 if (G_UNLIKELY (eos)) {
2110 GST_INFO_OBJECT (src, "pausing after end of segment");
2111 ret = GST_FLOW_UNEXPECTED;
2121 GST_DEBUG_OBJECT (src, "we are flushing");
2122 GST_LIVE_UNLOCK (src);
2123 ret = GST_FLOW_WRONG_STATE;
2128 GST_DEBUG_OBJECT (src, "we are EOS");
2129 GST_LIVE_UNLOCK (src);
2130 ret = GST_FLOW_UNEXPECTED;
2135 const gchar *reason = gst_flow_get_name (ret);
2137 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2138 src->data.ABI.running = FALSE;
2139 gst_pad_pause_task (pad);
2140 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2141 if (ret == GST_FLOW_UNEXPECTED) {
2142 /* perform EOS logic */
2143 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2144 gst_element_post_message (GST_ELEMENT_CAST (src),
2145 gst_message_new_segment_done (GST_OBJECT_CAST (src),
2146 src->segment.format, src->segment.last_stop));
2148 gst_pad_push_event (pad, gst_event_new_eos ());
2149 src->priv->last_sent_eos = TRUE;
2152 /* for fatal errors we post an error message, post the error
2153 * first so the app knows about the error first. */
2154 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2155 (_("Internal data flow error.")),
2156 ("streaming task paused, reason %s (%d)", reason, ret));
2157 gst_pad_push_event (pad, gst_event_new_eos ());
2158 src->priv->last_sent_eos = TRUE;
2165 GST_ELEMENT_ERROR (src, STREAM, FAILED,
2166 (_("Internal data flow error.")), ("element returned NULL buffer"));
2167 GST_LIVE_UNLOCK (src);
2168 /* we finished the segment on error */
2169 ret = GST_FLOW_ERROR;
2174 /* default negotiation code.
2176 * Take intersection between src and sink pads, take first
2180 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2183 GstCaps *caps = NULL;
2184 GstCaps *peercaps = NULL;
2185 gboolean result = FALSE;
2187 /* first see what is possible on our source pad */
2188 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
2189 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2190 /* nothing or anything is allowed, we're done */
2191 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2192 goto no_nego_needed;
2194 /* get the peer caps */
2195 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
2196 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2200 /* get intersection */
2201 icaps = gst_caps_intersect (thiscaps, peercaps);
2202 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
2203 gst_caps_unref (thiscaps);
2204 gst_caps_unref (peercaps);
2206 /* take first (and best, since they are sorted) possibility */
2207 caps = gst_caps_copy_nth (icaps, 0);
2208 gst_caps_unref (icaps);
2211 /* no peer, work with our own caps then */
2215 caps = gst_caps_make_writable (caps);
2216 gst_caps_truncate (caps);
2219 if (!gst_caps_is_empty (caps)) {
2220 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2221 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2223 if (gst_caps_is_any (caps)) {
2224 /* hmm, still anything, so element can do anything and
2225 * nego is not needed */
2227 } else if (gst_caps_is_fixed (caps)) {
2228 /* yay, fixed caps, use those then */
2229 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2233 gst_caps_unref (caps);
2239 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2241 gst_caps_unref (thiscaps);
2247 gst_base_src_negotiate (GstBaseSrc * basesrc)
2249 GstBaseSrcClass *bclass;
2250 gboolean result = TRUE;
2252 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2254 if (bclass->negotiate)
2255 result = bclass->negotiate (basesrc);
2261 gst_base_src_start (GstBaseSrc * basesrc)
2263 GstBaseSrcClass *bclass;
2267 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2270 GST_DEBUG_OBJECT (basesrc, "starting source");
2272 basesrc->num_buffers_left = basesrc->num_buffers;
2274 gst_segment_init (&basesrc->segment, basesrc->segment.format);
2275 basesrc->data.ABI.running = FALSE;
2277 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2279 result = bclass->start (basesrc);
2284 goto could_not_start;
2286 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2288 /* figure out the size */
2289 if (basesrc->segment.format == GST_FORMAT_BYTES) {
2290 if (bclass->get_size) {
2291 if (!(result = bclass->get_size (basesrc, &size)))
2297 GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2298 /* only update the size when operating in bytes, subclass is supposed
2299 * to set duration in the start method for other formats */
2300 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2305 GST_DEBUG_OBJECT (basesrc,
2306 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2307 G_GINT64_FORMAT, basesrc->segment.format, result, size,
2308 basesrc->segment.duration);
2310 /* check if we can seek */
2311 if (bclass->is_seekable)
2312 basesrc->seekable = bclass->is_seekable (basesrc);
2314 basesrc->seekable = FALSE;
2316 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
2318 /* update for random access flag */
2319 basesrc->random_access = basesrc->seekable &&
2320 basesrc->segment.format == GST_FORMAT_BYTES;
2322 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2324 /* run typefind if we are random_access and the typefinding is enabled. */
2325 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2328 caps = gst_type_find_helper (basesrc->srcpad, size);
2329 gst_pad_set_caps (basesrc->srcpad, caps);
2330 gst_caps_unref (caps);
2332 /* use class or default negotiate function */
2333 if (!gst_base_src_negotiate (basesrc))
2334 goto could_not_negotiate;
2342 GST_DEBUG_OBJECT (basesrc, "could not start");
2343 /* subclass is supposed to post a message. We don't have to call _stop. */
2346 could_not_negotiate:
2348 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2349 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2350 ("Could not negotiate format"), ("Check your filtered caps, if any"));
2351 /* we must call stop */
2352 gst_base_src_stop (basesrc);
2358 gst_base_src_stop (GstBaseSrc * basesrc)
2360 GstBaseSrcClass *bclass;
2361 gboolean result = TRUE;
2363 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2366 GST_DEBUG_OBJECT (basesrc, "stopping source");
2368 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2370 result = bclass->stop (basesrc);
2373 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2378 /* start or stop flushing dataprocessing
2381 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2382 gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2384 GstBaseSrcClass *bclass;
2386 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2388 if (flushing && unlock) {
2389 /* unlock any subclasses, we need to do this before grabbing the
2390 * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2391 * unlock to the params because of backwards compat (see seek handler)*/
2393 bclass->unlock (basesrc);
2396 /* the live lock is released when we are blocked, waiting for playing or
2397 * when we sync to the clock. */
2398 GST_LIVE_LOCK (basesrc);
2400 *playing = basesrc->live_running;
2401 basesrc->priv->flushing = flushing;
2403 /* if we are locked in the live lock, signal it to make it flush */
2404 basesrc->live_running = TRUE;
2405 /* clear pending EOS if any */
2406 basesrc->priv->pending_eos = FALSE;
2408 /* step 1, now that we have the LIVE lock, clear our unlock request */
2409 if (bclass->unlock_stop)
2410 bclass->unlock_stop (basesrc);
2412 /* step 2, unblock clock sync (if any) or any other blocking thing */
2413 if (basesrc->clock_id)
2414 gst_clock_id_unschedule (basesrc->clock_id);
2416 /* signal the live source that it can start playing */
2417 basesrc->live_running = live_play;
2419 GST_LIVE_SIGNAL (basesrc);
2420 GST_LIVE_UNLOCK (basesrc);
2425 /* the purpose of this function is to make sure that a live source blocks in the
2426 * LIVE lock or leaves the LIVE lock and continues playing. */
2428 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2430 GstBaseSrcClass *bclass;
2432 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2434 /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2436 GST_DEBUG_OBJECT (basesrc, "unlock");
2438 bclass->unlock (basesrc);
2441 /* we are now able to grab the LIVE lock, when we get it, we can be
2442 * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2444 GST_LIVE_LOCK (basesrc);
2445 GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2447 /* unblock clock sync (if any) */
2448 if (basesrc->clock_id)
2449 gst_clock_id_unschedule (basesrc->clock_id);
2451 /* configure what to do when we get to the LIVE lock. */
2452 GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2453 basesrc->live_running = live_play;
2458 /* clear our unlock request when going to PLAYING */
2459 GST_DEBUG_OBJECT (basesrc, "unlock stop");
2460 if (bclass->unlock_stop)
2461 bclass->unlock_stop (basesrc);
2463 /* for live sources we restart the timestamp correction */
2464 basesrc->priv->latency = -1;
2465 /* have to restart the task in case it stopped because of the unlock when
2466 * we went to PAUSED. Only do this if we operating in push mode. */
2467 GST_OBJECT_LOCK (basesrc->srcpad);
2468 start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2469 GST_OBJECT_UNLOCK (basesrc->srcpad);
2471 gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2473 GST_DEBUG_OBJECT (basesrc, "signal");
2474 GST_LIVE_SIGNAL (basesrc);
2476 GST_LIVE_UNLOCK (basesrc);
2482 gst_base_src_activate_push (GstPad * pad, gboolean active)
2484 GstBaseSrc *basesrc;
2487 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2489 /* prepare subclass first */
2491 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2493 if (G_UNLIKELY (!basesrc->can_activate_push))
2494 goto no_push_activation;
2496 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2499 basesrc->priv->last_sent_eos = FALSE;
2500 basesrc->priv->discont = TRUE;
2501 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2503 /* do initial seek, which will start the task */
2504 GST_OBJECT_LOCK (basesrc);
2505 event = basesrc->data.ABI.pending_seek;
2506 basesrc->data.ABI.pending_seek = NULL;
2507 GST_OBJECT_UNLOCK (basesrc);
2509 /* no need to unlock anything, the task is certainly
2510 * not running here. The perform seek code will start the task when
2512 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2516 gst_event_unref (event);
2518 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2520 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2522 gst_pad_stop_task (pad);
2523 /* now we can stop the source */
2524 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2532 GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2537 GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2542 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2543 gst_base_src_stop (basesrc);
2545 gst_event_unref (event);
2550 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2556 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2558 GstBaseSrc *basesrc;
2560 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2562 /* prepare subclass first */
2564 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2565 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2568 /* if not random_access, we cannot operate in pull mode for now */
2569 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2572 /* stop flushing now but for live sources, still block in the LIVE lock when
2573 * we are not yet PLAYING */
2574 gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2576 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2577 /* flush all, there is no task to stop */
2578 gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2580 /* don't send EOS when going from PAUSED => READY when in pull mode */
2581 basesrc->priv->last_sent_eos = TRUE;
2583 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2591 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2596 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2597 gst_base_src_stop (basesrc);
2602 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2607 static GstStateChangeReturn
2608 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2610 GstBaseSrc *basesrc;
2611 GstStateChangeReturn result;
2612 gboolean no_preroll = FALSE;
2614 basesrc = GST_BASE_SRC (element);
2616 switch (transition) {
2617 case GST_STATE_CHANGE_NULL_TO_READY:
2619 case GST_STATE_CHANGE_READY_TO_PAUSED:
2620 no_preroll = gst_base_src_is_live (basesrc);
2622 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2623 GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
2624 if (gst_base_src_is_live (basesrc)) {
2625 /* now we can start playback */
2626 gst_base_src_set_playing (basesrc, TRUE);
2634 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2635 transition)) == GST_STATE_CHANGE_FAILURE)
2638 switch (transition) {
2639 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2640 GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
2641 if (gst_base_src_is_live (basesrc)) {
2642 /* make sure we block in the live lock in PAUSED */
2643 gst_base_src_set_playing (basesrc, FALSE);
2647 case GST_STATE_CHANGE_PAUSED_TO_READY:
2651 /* we don't need to unblock anything here, the pad deactivation code
2652 * already did this */
2654 /* FIXME, deprecate this behaviour, it is very dangerous.
2655 * the prefered way of sending EOS downstream is by sending
2656 * the EOS event to the element */
2657 if (!basesrc->priv->last_sent_eos) {
2658 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2659 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2660 basesrc->priv->last_sent_eos = TRUE;
2662 basesrc->priv->pending_eos = FALSE;
2663 event_p = &basesrc->data.ABI.pending_seek;
2664 gst_event_replace (event_p, NULL);
2665 event_p = &basesrc->priv->close_segment;
2666 gst_event_replace (event_p, NULL);
2667 event_p = &basesrc->priv->start_segment;
2668 gst_event_replace (event_p, NULL);
2671 case GST_STATE_CHANGE_READY_TO_NULL:
2677 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2678 result = GST_STATE_CHANGE_NO_PREROLL;
2685 GST_DEBUG_OBJECT (basesrc, "parent failed state change");