2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Base class for getrange based source elements
26 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
28 * This is a generice base class for source elements. The following
29 * types of sources are supported:
31 * <listitem><para>random access sources like files</para></listitem>
32 * <listitem><para>seekable sources</para></listitem>
33 * <listitem><para>live sources</para></listitem>
38 * The source can be configured to operate in any #GstFormat with the
39 * gst_base_src_set_format() method. The currently set format determines
40 * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT
41 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
44 * #GstBaseSrc always supports push mode scheduling. If the following
45 * conditions are met, it also supports pull mode scheduling:
47 * <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
49 * <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
54 * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any
55 * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE.
58 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
59 * automatically seekable in push mode as well. The following conditions must
60 * be met to make the element seekable in push mode when the format is not
64 * #GstBaseSrc::is_seekable returns %TRUE.
67 * #GstBaseSrc::query can convert all supported seek formats to the
68 * internal format as set with gst_base_src_set_format().
71 * #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
76 * When the element does not meet the requirements to operate in pull mode,
77 * the offset and length in the #GstBaseSrc::create method should be ignored.
78 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
79 * element can operate in pull mode but only with specific offsets and
80 * lengths, it is allowed to generate an error when the wrong values are passed
81 * to the #GstBaseSrc::create function.
84 * #GstBaseSrc has support for live sources. Live sources are sources that
85 * produce data at a fixed rate, such as audio or video capture devices. A
86 * typical live source also provides a clock to publish the rate at which
88 * Use gst_base_src_set_live() to activate the live source mode.
91 * A live source does not produce data in the PAUSED state. This means that the
92 * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
93 * To signal the pipeline that the element will not produce data, the return
94 * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
97 * A typical live source will timestamp the buffers it creates with the
98 * current stream time of the pipeline. This is one reason why a live source
99 * can only produce data in the PLAYING state, when the clock is actually
100 * distributed and running.
103 * Live sources that synchronize and block on the clock (and audio source, for
104 * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
105 * function was interrupted by a state change to PAUSED.
108 * The #GstBaseSrc::get_times method can be used to implement pseudo-live
109 * sources. The base source will wait for the specified stream time returned in
110 * #GstBaseSrc::get_times before pushing out the buffer.
111 * It only makes sense to implement the ::get_times function if the source is
115 * There is only support in #GstBaseSrc for exactly one source pad, which
116 * should be named "src". A source implementation (subclass of #GstBaseSrc)
117 * should install a pad template in its base_init function, like so:
122 * my_element_base_init (gpointer g_class)
124 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
125 * // srctemplate should be a #GstStaticPadTemplate with direction
126 * // #GST_PAD_SRC and name "src"
127 * gst_element_class_add_pad_template (gstelement_class,
128 * gst_static_pad_template_get (&srctemplate));
129 * // see #GstElementDetails
130 * gst_element_class_set_details (gstelement_class, &details);
134 * <title>Controlled shutdown of live sources in applications</title>
136 * Applications that record from a live source may want to stop recording
137 * in a controlled way, so that the recording is stopped, but the data
138 * already in the pipeline is processed to the end (remember that many live
139 * sources would go on recording forever otherwise). For that to happen the
140 * application needs to make the source stop recording and send an EOS
141 * event down the pipeline. The application would then wait for an
142 * EOS message posted on the pipeline's bus to know when all data has
143 * been processed and the pipeline can safely be stopped.
146 * Since GStreamer 0.10.3 an application may simply set the source
147 * element to NULL or READY state to make it send an EOS event downstream.
148 * The application should lock the state of the source afterwards, so that
149 * shutting down the pipeline from PLAYING doesn't temporarily start up the
150 * source element for a second time:
154 * gst_element_set_state (audio_source, #GST_STATE_NULL);
155 * gst_element_set_locked_state (audio_source, %TRUE);
158 * Now the application should wait for an EOS message
159 * to be posted on the pipeline's bus. Once it has received
160 * an EOS message, it may safely shut down the entire pipeline:
163 * // everything done - shut down pipeline
164 * gst_element_set_state (pipeline, #GST_STATE_NULL);
165 * gst_element_set_locked_state (audio_source, %FALSE);
170 * Note that setting the source element to NULL or READY when the
171 * pipeline is in the PAUSED state may cause a deadlock since the streaming
172 * thread might be blocked in PREROLL.
175 * Last reviewed on 2006-09-27 (0.10.11)
187 #include "gstbasesrc.h"
188 #include "gsttypefindhelper.h"
189 #include <gst/gstmarshal.h>
190 #include <gst/gst-i18n-lib.h>
192 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
193 #define GST_CAT_DEFAULT gst_base_src_debug
195 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
196 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
197 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
198 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
199 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
200 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
201 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
203 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
204 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
206 /* BaseSrc signals and args */
213 #define DEFAULT_BLOCKSIZE 4096
214 #define DEFAULT_NUM_BUFFERS -1
215 #define DEFAULT_TYPEFIND FALSE
225 #define GST_BASE_SRC_GET_PRIVATE(obj) \
226 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
228 struct _GstBaseSrcPrivate
230 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
231 * to avoid the sending of two EOS in some cases) */
234 /* two segments to be sent in the streaming thread with STREAM_LOCK */
235 GstEvent *close_segment;
236 GstEvent *start_segment;
239 static GstElementClass *parent_class = NULL;
241 static void gst_base_src_base_init (gpointer g_class);
242 static void gst_base_src_class_init (GstBaseSrcClass * klass);
243 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
244 static void gst_base_src_finalize (GObject * object);
248 gst_base_src_get_type (void)
250 static GType base_src_type = 0;
252 if (G_UNLIKELY (base_src_type == 0)) {
253 static const GTypeInfo base_src_info = {
254 sizeof (GstBaseSrcClass),
255 (GBaseInitFunc) gst_base_src_base_init,
257 (GClassInitFunc) gst_base_src_class_init,
262 (GInstanceInitFunc) gst_base_src_init,
265 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
266 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
268 return base_src_type;
270 static GstCaps *gst_base_src_getcaps (GstPad * pad);
271 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
273 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
274 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
275 static void gst_base_src_set_property (GObject * object, guint prop_id,
276 const GValue * value, GParamSpec * pspec);
277 static void gst_base_src_get_property (GObject * object, guint prop_id,
278 GValue * value, GParamSpec * pspec);
279 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
280 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
281 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
282 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
284 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
286 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
287 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
288 GstSegment * segment);
289 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
291 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
292 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
293 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
295 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
296 GstStateChange transition);
298 static void gst_base_src_loop (GstPad * pad);
299 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
300 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
301 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
302 guint length, GstBuffer ** buf);
303 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
304 guint length, GstBuffer ** buf);
307 gst_base_src_base_init (gpointer g_class)
309 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
313 gst_base_src_class_init (GstBaseSrcClass * klass)
315 GObjectClass *gobject_class;
316 GstElementClass *gstelement_class;
318 gobject_class = G_OBJECT_CLASS (klass);
319 gstelement_class = GST_ELEMENT_CLASS (klass);
321 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
323 parent_class = g_type_class_peek_parent (klass);
325 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
326 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
327 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
329 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
330 g_param_spec_ulong ("blocksize", "Block size",
331 "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
332 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
333 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
334 g_param_spec_int ("num-buffers", "num-buffers",
335 "Number of buffers to output before sending EOS", -1, G_MAXINT,
336 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
337 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
338 g_param_spec_boolean ("typefind", "Typefind",
339 "Run typefind before negotiating", DEFAULT_TYPEFIND,
342 gstelement_class->change_state =
343 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
344 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
345 gstelement_class->get_query_types =
346 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
348 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
349 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
350 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
351 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
352 klass->check_get_range =
353 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
357 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
360 GstPadTemplate *pad_template;
362 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
364 basesrc->is_live = FALSE;
365 basesrc->live_lock = g_mutex_new ();
366 basesrc->live_cond = g_cond_new ();
367 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
368 basesrc->num_buffers_left = -1;
370 basesrc->can_activate_push = TRUE;
371 basesrc->pad_mode = GST_ACTIVATE_NONE;
374 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
375 g_return_if_fail (pad_template != NULL);
377 GST_DEBUG_OBJECT (basesrc, "creating src pad");
378 pad = gst_pad_new_from_template (pad_template, "src");
380 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
381 gst_pad_set_activatepush_function (pad,
382 GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
383 gst_pad_set_activatepull_function (pad,
384 GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
385 gst_pad_set_event_function (pad,
386 GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
387 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
388 gst_pad_set_checkgetrange_function (pad,
389 GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
390 gst_pad_set_getrange_function (pad,
391 GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
392 gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
393 gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
395 /* hold pointer to pad */
396 basesrc->srcpad = pad;
397 GST_DEBUG_OBJECT (basesrc, "adding src pad");
398 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
400 basesrc->blocksize = DEFAULT_BLOCKSIZE;
401 basesrc->clock_id = NULL;
402 /* we operate in BYTES by default */
403 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
404 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
406 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
408 GST_DEBUG_OBJECT (basesrc, "init done");
412 gst_base_src_finalize (GObject * object)
417 basesrc = GST_BASE_SRC (object);
419 g_mutex_free (basesrc->live_lock);
420 g_cond_free (basesrc->live_cond);
422 event_p = &basesrc->data.ABI.pending_seek;
423 gst_event_replace ((GstEvent **) event_p, NULL);
425 G_OBJECT_CLASS (parent_class)->finalize (object);
429 * gst_base_src_wait_playing:
432 * If the #GstBaseSrcClass::create method performs its own synchronisation against
433 * the clock it must unblock when going from PLAYING to the PAUSED state and call
434 * this method before continuing to produce the remaining data.
436 * This function will block until a state change to PLAYING happens (in which
437 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
438 * to a state change to READY or a FLUSH event (in which case this function
439 * returns #GST_FLOW_WRONG_STATE).
443 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
444 * continue. Any other return value should be returned from the create vmethod.
447 gst_base_src_wait_playing (GstBaseSrc * src)
449 /* block until the state changes, or we get a flush, or something */
452 while (G_UNLIKELY (!src->live_running)) {
453 GST_DEBUG ("live source signal waiting");
454 GST_LIVE_SIGNAL (src);
455 GST_DEBUG ("live source waiting for running state");
457 GST_DEBUG ("live source unlocked");
459 /* FIXME, use another variable to signal stopping so that we don't
460 * have to grab another lock. */
461 GST_OBJECT_LOCK (src->srcpad);
462 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
464 GST_OBJECT_UNLOCK (src->srcpad);
466 GST_LIVE_UNLOCK (src);
473 GST_DEBUG_OBJECT (src, "pad is flushing");
474 GST_OBJECT_UNLOCK (src->srcpad);
475 GST_LIVE_UNLOCK (src);
476 return GST_FLOW_WRONG_STATE;
481 * gst_base_src_set_live:
482 * @src: base source instance
483 * @live: new live-mode
485 * If the element listens to a live source, @live should
488 * A live source will not produce data in the PAUSED state and
489 * will therefore not be able to participate in the PREROLL phase
490 * of a pipeline. To signal this fact to the application and the
491 * pipeline, the state change return value of the live source will
492 * be GST_STATE_CHANGE_NO_PREROLL.
495 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
499 GST_LIVE_UNLOCK (src);
503 * gst_base_src_is_live:
504 * @src: base source instance
506 * Check if an element is in live mode.
508 * Returns: %TRUE if element is in live mode.
511 gst_base_src_is_live (GstBaseSrc * src)
516 result = src->is_live;
517 GST_LIVE_UNLOCK (src);
523 * gst_base_src_set_format:
524 * @src: base source instance
525 * @format: the format to use
527 * Sets the default format of the source. This will be the format used
528 * for sending NEW_SEGMENT events and for performing seeks.
530 * If a format of GST_FORMAT_BYTES is set, the element will be able to
531 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
536 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
538 gst_segment_init (&src->segment, format);
542 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
544 GstBaseSrcClass *bclass;
548 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
549 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
551 if (bclass->set_caps)
552 res = bclass->set_caps (bsrc, caps);
558 gst_base_src_getcaps (GstPad * pad)
560 GstBaseSrcClass *bclass;
562 GstCaps *caps = NULL;
564 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
565 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
566 if (bclass->get_caps)
567 caps = bclass->get_caps (bsrc);
570 GstPadTemplate *pad_template;
573 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
574 if (pad_template != NULL) {
575 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
582 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
586 switch (GST_QUERY_TYPE (query)) {
587 case GST_QUERY_POSITION:
591 gst_query_parse_position (query, &format, NULL);
593 case GST_FORMAT_PERCENT:
599 position = src->segment.last_stop;
600 duration = src->segment.duration;
602 if (position != -1 && duration != -1) {
603 if (position < duration)
604 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
607 percent = GST_FORMAT_PERCENT_MAX;
611 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
619 position = src->segment.last_stop;
621 if (position != -1) {
622 /* convert to requested format */
624 gst_pad_query_convert (src->srcpad, src->segment.format,
625 position, &format, &position);
629 gst_query_set_position (query, format, position);
635 case GST_QUERY_DURATION:
639 gst_query_parse_duration (query, &format, NULL);
641 case GST_FORMAT_PERCENT:
642 gst_query_set_duration (query, GST_FORMAT_PERCENT,
643 GST_FORMAT_PERCENT_MAX);
650 duration = src->segment.duration;
652 if (duration != -1) {
653 /* convert to requested format */
655 gst_pad_query_convert (src->srcpad, src->segment.format,
656 duration, &format, &duration);
660 gst_query_set_duration (query, format, duration);
667 case GST_QUERY_SEEKING:
669 gst_query_set_seeking (query, src->segment.format,
670 src->seekable, 0, src->segment.duration);
674 case GST_QUERY_SEGMENT:
678 /* no end segment configured, current duration then */
679 if ((stop = src->segment.stop) == -1)
680 stop = src->segment.duration;
681 start = src->segment.start;
683 /* adjust to stream time */
684 if (src->segment.time != -1) {
685 start -= src->segment.time;
687 stop -= src->segment.time;
689 gst_query_set_segment (query, src->segment.rate, src->segment.format,
695 case GST_QUERY_FORMATS:
697 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
698 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
702 case GST_QUERY_CONVERT:
704 GstFormat src_fmt, dest_fmt;
705 gint64 src_val, dest_val;
707 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
709 /* we can only convert between equal formats... */
710 if (src_fmt == dest_fmt) {
716 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
719 case GST_QUERY_LATENCY:
720 case GST_QUERY_JITTER:
730 gst_base_src_query (GstPad * pad, GstQuery * query)
733 GstBaseSrcClass *bclass;
734 gboolean result = FALSE;
736 src = GST_BASE_SRC (gst_pad_get_parent (pad));
738 bclass = GST_BASE_SRC_GET_CLASS (src);
741 result = bclass->query (src, query);
743 result = gst_pad_query_default (pad, query);
745 gst_object_unref (src);
751 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
755 /* update our offset if the start/stop position was updated */
756 if (segment->format == GST_FORMAT_BYTES) {
757 segment->last_stop = segment->start;
758 segment->time = segment->start;
759 } else if (segment->start == 0) {
760 /* seek to start, we can implement a default for this. */
761 segment->last_stop = 0;
771 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
773 GstBaseSrcClass *bclass;
774 gboolean result = FALSE;
776 bclass = GST_BASE_SRC_GET_CLASS (src);
779 result = bclass->do_seek (src, segment);
784 /* this code implements the seeking. It is a good example
785 * handling all cases.
787 * A seek updates the currently configured segment.start
788 * and segment.stop values based on the SEEK_TYPE. If the
789 * segment.start value is updated, a seek to this new position
790 * should be performed.
792 * The seek can only be executed when we are not currently
793 * streaming any data, to make sure that this is the case, we
794 * acquire the STREAM_LOCK which is taken when we are in the
795 * _loop() function or when a getrange() is called. Normally
796 * we will not receive a seek if we are operating in pull mode
799 * When we are in the loop() function, we might be in the middle
800 * of pushing a buffer, which might block in a sink. To make sure
801 * that the push gets unblocked we push out a FLUSH_START event.
802 * Our loop function will get a WRONG_STATE return value from
803 * the push and will pause, effectively releasing the STREAM_LOCK.
805 * For a non-flushing seek, we pause the task, which might eventually
806 * release the STREAM_LOCK. We say eventually because when the sink
807 * blocks on the sample we might wait a very long time until the sink
808 * unblocks the sample. In any case we acquire the STREAM_LOCK and
809 * can continue the seek. A non-flushing seek is normally done in a
810 * running pipeline to perform seamless playback.
811 * In the case of a non-flushing seek we need to make sure that the
812 * data we output after the seek is continuous with the previous data,
813 * this is because a non-flushing seek does not reset the stream-time
814 * to 0. We do this by closing the currently running segment, ie. sending
815 * a new_segment event with the stop position set to the last processed
818 * After updating the segment.start/stop values, we prepare for
819 * streaming again. We push out a FLUSH_STOP to make the peer pad
820 * accept data again and we start our task again.
822 * A segment seek posts a message on the bus saying that the playback
823 * of the segment started. We store the segment flag internally because
824 * when we reach the segment.stop we have to post a segment.done
825 * instead of EOS when doing a segment seek.
827 /* FIXME, we have the unlock gboolean here because most current implementations
828 * (fdsrc, -base/gst/tcp/, ...) not only unlock when there is something to
832 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
838 GstSeekType cur_type, stop_type;
842 GstSegment seeksegment;
844 GST_DEBUG_OBJECT (src, "doing seek");
847 gst_event_parse_seek (event, &rate, &format, &flags,
848 &cur_type, &cur, &stop_type, &stop);
850 /* we have to have a format as the segment format. Try to convert
852 if (src->segment.format != format) {
855 fmt = src->segment.format;
857 if (cur_type != GST_SEEK_TYPE_NONE)
858 res = gst_pad_query_convert (src->srcpad, format, cur, &fmt, &cur);
859 if (res && stop_type != GST_SEEK_TYPE_NONE)
860 res = gst_pad_query_convert (src->srcpad, format, stop, &fmt, &stop);
870 flush = flags & GST_SEEK_FLAG_FLUSH;
872 /* send flush start */
874 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
876 gst_pad_pause_task (src->srcpad);
878 /* unblock streaming thread */
880 gst_base_src_unlock (src);
882 /* grab streaming lock, this should eventually be possible, either
883 * because the task is paused or our streaming thread stopped
884 * because our peer is flushing. */
885 GST_PAD_STREAM_LOCK (src->srcpad);
887 /* make copy into temp structure, we can only update the main one
888 * when the subclass actually could do the seek. */
889 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
891 /* now configure the seek segment */
893 gst_segment_set_seek (&seeksegment, rate, format, flags,
894 cur_type, cur, stop_type, stop, &update);
897 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
898 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
899 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
901 /* do the seek, segment.last_stop contains new position. */
902 res = gst_base_src_do_seek (src, &seeksegment);
904 /* and prepare to continue streaming */
906 /* send flush stop, peer will accept data and events again. We
907 * are not yet providing data as we still have the STREAM_LOCK. */
908 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
909 } else if (res && src->data.ABI.running) {
910 /* we are running the current segment and doing a non-flushing seek,
911 * close the segment first based on the last_stop. */
912 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
913 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
915 /* queue the segment for sending in the stream thread */
916 if (src->priv->close_segment)
917 gst_event_unref (src->priv->close_segment);
918 src->priv->close_segment =
919 gst_event_new_new_segment_full (TRUE,
920 src->segment.rate, src->segment.applied_rate, src->segment.format,
921 src->segment.start, src->segment.last_stop, src->segment.time);
924 /* if successfull seek, we update our real segment and push
925 * out the new segment. */
927 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
929 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
930 gst_element_post_message (GST_ELEMENT (src),
931 gst_message_new_segment_start (GST_OBJECT (src),
932 src->segment.format, src->segment.last_stop));
935 /* for deriving a stop position for the playback segment form the seek
936 * segment, we must take the duration when the stop is not set */
937 if ((stop = src->segment.stop) == -1)
938 stop = src->segment.duration;
940 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
941 " to %" G_GINT64_FORMAT, src->segment.start, stop);
943 /* now replace the old segment so that we send it in the stream thread the
944 * next time it is scheduled. */
945 if (src->priv->start_segment)
946 gst_event_unref (src->priv->start_segment);
947 src->priv->start_segment =
948 gst_event_new_new_segment_full (FALSE,
949 src->segment.rate, src->segment.applied_rate, src->segment.format,
950 src->segment.last_stop, stop, src->segment.time);
953 src->priv->discont = TRUE;
954 src->data.ABI.running = TRUE;
955 /* and restart the task in case it got paused explicitely or by
956 * the FLUSH_START event we pushed out. */
957 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
960 /* and release the lock again so we can continue streaming */
961 GST_PAD_STREAM_UNLOCK (src->srcpad);
968 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
973 static const GstQueryType *
974 gst_base_src_get_query_types (GstElement * element)
976 static const GstQueryType query_types[] = {
992 /* all events send to this element directly
995 gst_base_src_send_event (GstElement * element, GstEvent * event)
998 gboolean result = FALSE;
1000 src = GST_BASE_SRC (element);
1002 switch (GST_EVENT_TYPE (event)) {
1003 case GST_EVENT_FLUSH_START:
1004 case GST_EVENT_FLUSH_STOP:
1005 /* sending random flushes downstream can break stuff,
1006 * especially sync since all segment info will get flushed */
1009 /* FIXME, queue EOS and make sure the task or pull function
1010 * perform the EOS actions. */
1012 case GST_EVENT_NEWSEGMENT:
1013 /* sending random NEWSEGMENT downstream can break sync. */
1016 case GST_EVENT_BUFFERSIZE:
1020 case GST_EVENT_SEEK:
1024 GST_OBJECT_LOCK (src->srcpad);
1025 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1027 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1028 GST_OBJECT_UNLOCK (src->srcpad);
1031 /* when we are running in push mode, we can execute the
1032 * seek right now, we need to unlock. */
1033 result = gst_base_src_perform_seek (src, event, TRUE);
1037 /* else we store the event and execute the seek when we
1039 GST_OBJECT_LOCK (src);
1040 event_p = &src->data.ABI.pending_seek;
1041 gst_event_replace ((GstEvent **) event_p, event);
1042 GST_OBJECT_UNLOCK (src);
1043 /* assume the seek will work */
1048 case GST_EVENT_NAVIGATION:
1054 gst_event_unref (event);
1061 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1062 GST_OBJECT_UNLOCK (src->srcpad);
1069 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1073 switch (GST_EVENT_TYPE (event)) {
1074 case GST_EVENT_SEEK:
1075 /* is normally called when in push mode */
1079 result = gst_base_src_perform_seek (src, event, TRUE);
1081 case GST_EVENT_FLUSH_START:
1082 /* cancel any blocking getrange, is normally called
1083 * when in pull mode. */
1084 result = gst_base_src_unlock (src);
1086 case GST_EVENT_FLUSH_STOP:
1096 GST_DEBUG_OBJECT (src, "is not seekable");
1102 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1105 GstBaseSrcClass *bclass;
1106 gboolean result = FALSE;
1108 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1109 bclass = GST_BASE_SRC_GET_CLASS (src);
1111 if (bclass->event) {
1112 if (!(result = bclass->event (src, event)))
1113 goto subclass_failed;
1117 gst_event_unref (event);
1118 gst_object_unref (src);
1125 GST_DEBUG_OBJECT (src, "subclass refused event");
1131 gst_base_src_set_property (GObject * object, guint prop_id,
1132 const GValue * value, GParamSpec * pspec)
1136 src = GST_BASE_SRC (object);
1139 case PROP_BLOCKSIZE:
1140 src->blocksize = g_value_get_ulong (value);
1142 case PROP_NUM_BUFFERS:
1143 src->num_buffers = g_value_get_int (value);
1146 src->data.ABI.typefind = g_value_get_boolean (value);
1149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1155 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1160 src = GST_BASE_SRC (object);
1163 case PROP_BLOCKSIZE:
1164 g_value_set_ulong (value, src->blocksize);
1166 case PROP_NUM_BUFFERS:
1167 g_value_set_int (value, src->num_buffers);
1170 g_value_set_boolean (value, src->data.ABI.typefind);
1173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1178 /* with STREAM_LOCK and LOCK */
1179 static GstClockReturn
1180 gst_base_src_wait (GstBaseSrc * basesrc, GstClockTime time)
1186 /* get clock, if no clock, we don't sync */
1187 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1188 return GST_CLOCK_OK;
1190 id = gst_clock_new_single_shot_id (clock, time);
1192 basesrc->clock_id = id;
1193 /* release the object lock while waiting */
1194 GST_OBJECT_UNLOCK (basesrc);
1196 ret = gst_clock_id_wait (id, NULL);
1198 GST_OBJECT_LOCK (basesrc);
1199 gst_clock_id_unref (id);
1200 basesrc->clock_id = NULL;
1205 /* perform synchronisation on a buffer.
1208 static GstClockReturn
1209 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1211 GstClockReturn result;
1212 GstClockTime start, end;
1213 GstBaseSrcClass *bclass;
1214 GstClockTime base_time;
1216 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1219 if (bclass->get_times)
1220 bclass->get_times (basesrc, buffer, &start, &end);
1222 /* if we don't have a timestamp, we don't sync */
1223 if (!GST_CLOCK_TIME_IS_VALID (start))
1226 /* now do clocking */
1227 GST_OBJECT_LOCK (basesrc);
1228 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1230 GST_LOG_OBJECT (basesrc,
1231 "waiting for clock, base time %" GST_TIME_FORMAT
1232 ", stream_start %" GST_TIME_FORMAT,
1233 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1235 result = gst_base_src_wait (basesrc, start + base_time);
1236 GST_OBJECT_UNLOCK (basesrc);
1238 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1245 GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1246 return GST_CLOCK_OK;
1251 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1253 guint64 size, maxsize;
1254 GstBaseSrcClass *bclass;
1256 bclass = GST_BASE_SRC_GET_CLASS (src);
1258 /* only operate if we are working with bytes */
1259 if (src->segment.format != GST_FORMAT_BYTES)
1262 /* get total file size */
1263 size = (guint64) src->segment.duration;
1265 /* the max amount of bytes to read is the total size or
1266 * up to the segment.stop if present. */
1267 if (src->segment.stop != -1)
1268 maxsize = MIN (size, src->segment.stop);
1272 GST_DEBUG_OBJECT (src,
1273 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1274 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1275 *length, size, src->segment.stop, maxsize);
1277 /* check size if we have one */
1278 if (maxsize != -1) {
1279 /* if we run past the end, check if the file became bigger and
1281 if (G_UNLIKELY (offset + *length >= maxsize)) {
1282 /* see if length of the file changed */
1283 if (bclass->get_size)
1284 bclass->get_size (src, &size);
1286 /* make sure we don't exceed the configured segment stop
1288 if (src->segment.stop != -1)
1289 maxsize = MIN (size, src->segment.stop);
1293 /* if we are at or past the end, EOS */
1294 if (G_UNLIKELY (offset >= maxsize))
1295 goto unexpected_length;
1297 /* else we can clip to the end */
1298 if (G_UNLIKELY (offset + *length >= maxsize))
1299 *length = maxsize - offset;
1303 /* keep track of current position. segment is in bytes, we checked
1305 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1316 static GstFlowReturn
1317 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1321 GstBaseSrcClass *bclass;
1322 GstClockReturn status;
1324 bclass = GST_BASE_SRC_GET_CLASS (src);
1326 ret = gst_base_src_wait_playing (src);
1327 if (ret != GST_FLOW_OK)
1330 if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1333 if (G_UNLIKELY (!bclass->create))
1336 if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1337 goto unexpected_length;
1339 /* normally we don't count buffers */
1340 if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1341 if (src->num_buffers_left == 0)
1342 goto reached_num_buffers;
1344 src->num_buffers_left--;
1347 GST_DEBUG_OBJECT (src,
1348 "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1349 G_GINT64_FORMAT, offset, length, src->segment.time);
1351 ret = bclass->create (src, offset, length, buf);
1352 if (G_UNLIKELY (ret != GST_FLOW_OK))
1355 /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1356 if (offset == 0 && src->segment.time == 0
1357 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1358 GST_BUFFER_TIMESTAMP (*buf) = 0;
1360 /* now sync before pushing the buffer */
1361 status = gst_base_src_do_sync (src, *buf);
1363 case GST_CLOCK_EARLY:
1364 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1367 GST_DEBUG_OBJECT (src, "buffer ok");
1370 /* this case is triggered when we were waiting for the clock and
1371 * it got unlocked because we did a state change. We return
1372 * WRONG_STATE in this case to stop the dataflow also get rid of the
1373 * produced buffer. */
1374 GST_DEBUG_OBJECT (src, "clock returned %d, not returning", status);
1375 gst_buffer_unref (*buf);
1377 ret = GST_FLOW_WRONG_STATE;
1386 GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
1391 GST_DEBUG_OBJECT (src, "getrange but not started");
1392 return GST_FLOW_WRONG_STATE;
1396 GST_DEBUG_OBJECT (src, "no create function");
1397 return GST_FLOW_ERROR;
1401 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1402 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1403 return GST_FLOW_UNEXPECTED;
1405 reached_num_buffers:
1407 GST_DEBUG_OBJECT (src, "sent all buffers");
1408 return GST_FLOW_UNEXPECTED;
1412 static GstFlowReturn
1413 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1419 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1421 res = gst_base_src_get_range (src, offset, length, buf);
1423 gst_object_unref (src);
1429 gst_base_src_default_check_get_range (GstBaseSrc * src)
1433 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1434 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1435 if (G_LIKELY (gst_base_src_start (src)))
1436 gst_base_src_stop (src);
1439 /* we can operate in getrange mode if the native format is bytes
1440 * and we are seekable, this condition is set in the random_access
1441 * flag and is set in the _start() method. */
1442 res = src->random_access;
1448 gst_base_src_check_get_range (GstBaseSrc * src)
1450 GstBaseSrcClass *bclass;
1453 bclass = GST_BASE_SRC_GET_CLASS (src);
1455 if (bclass->check_get_range == NULL)
1458 res = bclass->check_get_range (src);
1459 GST_LOG_OBJECT (src, "%s() returned %d",
1460 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1467 GST_WARNING_OBJECT (src, "no check_get_range function set");
1473 gst_base_src_pad_check_get_range (GstPad * pad)
1478 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1480 res = gst_base_src_check_get_range (src);
1482 gst_object_unref (src);
1488 gst_base_src_loop (GstPad * pad)
1491 GstBuffer *buf = NULL;
1498 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1500 src->priv->last_sent_eos = FALSE;
1502 /* push events to close/start our segment before we do the real work. */
1503 if (src->priv->close_segment) {
1504 gst_pad_push_event (pad, src->priv->close_segment);
1505 src->priv->close_segment = NULL;
1507 if (src->priv->start_segment) {
1508 gst_pad_push_event (pad, src->priv->start_segment);
1509 src->priv->start_segment = NULL;
1512 /* if we operate in bytes, we can calculate an offset */
1513 if (src->segment.format == GST_FORMAT_BYTES)
1514 position = src->segment.last_stop;
1518 ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1519 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1520 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %d", ret);
1523 if (G_UNLIKELY (buf == NULL))
1526 /* figure out the new position */
1527 switch (src->segment.format) {
1528 case GST_FORMAT_BYTES:
1529 position += GST_BUFFER_SIZE (buf);
1531 case GST_FORMAT_TIME:
1533 GstClockTime start, duration;
1535 start = GST_BUFFER_TIMESTAMP (buf);
1536 duration = GST_BUFFER_DURATION (buf);
1538 if (GST_CLOCK_TIME_IS_VALID (start))
1541 position = src->segment.last_stop;
1543 if (GST_CLOCK_TIME_IS_VALID (duration))
1544 position += duration;
1547 case GST_FORMAT_DEFAULT:
1548 position = GST_BUFFER_OFFSET_END (buf);
1554 if (position != -1) {
1555 if (src->segment.stop != -1) {
1556 if (position >= src->segment.stop) {
1558 position = src->segment.stop;
1561 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1564 if (G_UNLIKELY (src->priv->discont)) {
1565 buf = gst_buffer_make_metadata_writable (buf);
1566 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1567 src->priv->discont = FALSE;
1570 ret = gst_pad_push (pad, buf);
1571 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1572 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %d", ret);
1577 GST_INFO_OBJECT (src, "pausing after EOS");
1578 ret = GST_FLOW_UNEXPECTED;
1583 gst_object_unref (src);
1589 const gchar *reason = gst_flow_get_name (ret);
1591 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1592 src->data.ABI.running = FALSE;
1593 gst_pad_pause_task (pad);
1594 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1595 if (ret == GST_FLOW_UNEXPECTED) {
1596 /* perform EOS logic */
1597 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1598 gst_element_post_message (GST_ELEMENT_CAST (src),
1599 gst_message_new_segment_done (GST_OBJECT_CAST (src),
1600 src->segment.format, src->segment.last_stop));
1602 gst_pad_push_event (pad, gst_event_new_eos ());
1603 src->priv->last_sent_eos = TRUE;
1606 /* for fatal errors we post an error message, post the error
1607 * first so the app knows about the error first. */
1608 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1609 (_("Internal data flow error.")),
1610 ("streaming task paused, reason %s (%d)", reason, ret));
1611 gst_pad_push_event (pad, gst_event_new_eos ());
1612 src->priv->last_sent_eos = TRUE;
1619 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1620 (_("Internal data flow error.")), ("element returned NULL buffer"));
1621 /* we finished the segment on error */
1622 src->data.ABI.running = FALSE;
1623 gst_pad_pause_task (pad);
1624 gst_pad_push_event (pad, gst_event_new_eos ());
1625 src->priv->last_sent_eos = TRUE;
1630 /* this will always be called between start() and stop(). So you can rely on
1631 * resources allocated by start() and freed from stop(). This needs to be added
1632 * to the docs at some point. */
1634 gst_base_src_unlock (GstBaseSrc * basesrc)
1636 GstBaseSrcClass *bclass;
1637 gboolean result = TRUE;
1639 GST_DEBUG ("unlock");
1640 /* unblock whatever the subclass is doing */
1641 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1643 result = bclass->unlock (basesrc);
1645 GST_DEBUG ("unschedule clock");
1646 /* and unblock the clock as well, if any */
1647 GST_OBJECT_LOCK (basesrc);
1648 if (basesrc->clock_id) {
1649 gst_clock_id_unschedule (basesrc->clock_id);
1651 GST_OBJECT_UNLOCK (basesrc);
1653 GST_DEBUG ("unlock done");
1658 /* default negotiation code.
1660 * Take intersection between src and sink pads, take first
1664 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1667 GstCaps *caps = NULL;
1668 GstCaps *peercaps = NULL;
1669 gboolean result = FALSE;
1671 /* first see what is possible on our source pad */
1672 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
1673 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1674 /* nothing or anything is allowed, we're done */
1675 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1676 goto no_nego_needed;
1678 /* get the peer caps */
1679 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
1680 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1684 /* get intersection */
1685 icaps = gst_caps_intersect (thiscaps, peercaps);
1686 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
1687 gst_caps_unref (thiscaps);
1688 gst_caps_unref (peercaps);
1690 /* take first (and best, since they are sorted) possibility */
1691 caps = gst_caps_copy_nth (icaps, 0);
1692 gst_caps_unref (icaps);
1695 /* no peer, work with our own caps then */
1699 caps = gst_caps_make_writable (caps);
1700 gst_caps_truncate (caps);
1703 if (!gst_caps_is_empty (caps)) {
1704 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
1705 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1707 if (gst_caps_is_any (caps)) {
1708 /* hmm, still anything, so element can do anything and
1709 * nego is not needed */
1711 } else if (gst_caps_is_fixed (caps)) {
1712 /* yay, fixed caps, use those then */
1713 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1717 gst_caps_unref (caps);
1723 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1725 gst_caps_unref (thiscaps);
1731 gst_base_src_negotiate (GstBaseSrc * basesrc)
1733 GstBaseSrcClass *bclass;
1734 gboolean result = TRUE;
1736 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1738 if (bclass->negotiate)
1739 result = bclass->negotiate (basesrc);
1745 gst_base_src_start (GstBaseSrc * basesrc)
1747 GstBaseSrcClass *bclass;
1751 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1754 GST_DEBUG_OBJECT (basesrc, "starting source");
1756 basesrc->num_buffers_left = basesrc->num_buffers;
1758 gst_segment_init (&basesrc->segment, basesrc->segment.format);
1759 basesrc->data.ABI.running = FALSE;
1761 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1763 result = bclass->start (basesrc);
1768 goto could_not_start;
1770 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
1772 /* figure out the size */
1773 if (basesrc->segment.format == GST_FORMAT_BYTES) {
1774 if (bclass->get_size) {
1775 if (!(result = bclass->get_size (basesrc, &size)))
1781 /* only update the size when operating in bytes, subclass is supposed
1782 * to set duration in the start method for other formats */
1783 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
1786 GST_DEBUG_OBJECT (basesrc,
1787 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
1788 G_GINT64_FORMAT, basesrc->segment.format, result, size,
1789 basesrc->segment.duration);
1791 /* check if we can seek */
1792 if (bclass->is_seekable)
1793 basesrc->seekable = bclass->is_seekable (basesrc);
1795 basesrc->seekable = FALSE;
1797 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
1799 /* update for random access flag */
1800 basesrc->random_access = basesrc->seekable &&
1801 basesrc->segment.format == GST_FORMAT_BYTES;
1803 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
1805 /* run typefind if we are random_access and the typefinding is enabled. */
1806 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
1809 caps = gst_type_find_helper (basesrc->srcpad, size);
1810 gst_pad_set_caps (basesrc->srcpad, caps);
1811 gst_caps_unref (caps);
1813 /* use class or default negotiate function */
1814 if (!gst_base_src_negotiate (basesrc))
1815 goto could_not_negotiate;
1823 GST_DEBUG_OBJECT (basesrc, "could not start");
1824 /* subclass is supposed to post a message. We don't have to call _stop. */
1827 could_not_negotiate:
1829 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
1830 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
1831 ("Could not negotiate format"), ("Check your filtered caps, if any"));
1832 /* we must call stop */
1833 gst_base_src_stop (basesrc);
1839 gst_base_src_stop (GstBaseSrc * basesrc)
1841 GstBaseSrcClass *bclass;
1842 gboolean result = TRUE;
1844 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1847 GST_DEBUG_OBJECT (basesrc, "stopping source");
1849 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1851 result = bclass->stop (basesrc);
1854 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
1860 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
1864 GST_LIVE_LOCK (basesrc);
1865 basesrc->live_running = TRUE;
1866 GST_LIVE_SIGNAL (basesrc);
1867 GST_LIVE_UNLOCK (basesrc);
1869 /* step 1, unblock clock sync (if any) */
1870 result = gst_base_src_unlock (basesrc);
1872 /* step 2, make sure streaming finishes */
1873 result &= gst_pad_stop_task (pad);
1879 gst_base_src_activate_push (GstPad * pad, gboolean active)
1881 GstBaseSrc *basesrc;
1884 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1886 /* prepare subclass first */
1888 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1890 if (G_UNLIKELY (!basesrc->can_activate_push))
1891 goto no_push_activation;
1893 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1896 basesrc->priv->last_sent_eos = FALSE;
1898 /* do initial seek, which will start the task */
1899 GST_OBJECT_LOCK (basesrc);
1900 event = basesrc->data.ABI.pending_seek;
1901 basesrc->data.ABI.pending_seek = NULL;
1902 GST_OBJECT_UNLOCK (basesrc);
1904 /* no need to unlock anything, the task is certainly
1905 * not running here. The perform seek code will start the task when
1907 if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
1911 gst_event_unref (event);
1913 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1914 /* call the unlock function and stop the task */
1915 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
1916 goto deactivate_failed;
1918 /* now we can stop the source */
1919 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
1927 GST_ERROR_OBJECT (basesrc, "Subclass disabled push-mode activation");
1932 GST_ERROR_OBJECT (basesrc, "Failed to start in push mode");
1937 GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
1938 gst_base_src_stop (basesrc);
1940 gst_event_unref (event);
1945 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
1950 GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
1956 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1958 GstBaseSrc *basesrc;
1960 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1962 /* prepare subclass first */
1964 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1965 if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1968 /* if not random_access, we cannot operate in pull mode for now */
1969 if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
1972 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
1973 /* call the unlock function. We have no task to stop. */
1974 if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
1975 goto deactivate_failed;
1977 /* don't send EOS when going from PAUSED => READY when in pull mode */
1978 basesrc->priv->last_sent_eos = TRUE;
1980 if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
1988 GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
1993 GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
1994 gst_base_src_stop (basesrc);
1999 GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
2004 GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2009 static GstStateChangeReturn
2010 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2012 GstBaseSrc *basesrc;
2013 GstStateChangeReturn result;
2014 gboolean no_preroll = FALSE;
2016 basesrc = GST_BASE_SRC (element);
2018 switch (transition) {
2019 case GST_STATE_CHANGE_NULL_TO_READY:
2021 case GST_STATE_CHANGE_READY_TO_PAUSED:
2022 GST_LIVE_LOCK (element);
2023 if (basesrc->is_live) {
2025 basesrc->live_running = FALSE;
2027 basesrc->priv->last_sent_eos = FALSE;
2028 basesrc->priv->discont = TRUE;
2029 GST_LIVE_UNLOCK (element);
2031 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2032 GST_LIVE_LOCK (element);
2033 if (basesrc->is_live) {
2034 basesrc->live_running = TRUE;
2035 GST_LIVE_SIGNAL (element);
2037 GST_LIVE_UNLOCK (element);
2044 GST_ELEMENT_CLASS (parent_class)->change_state (element,
2045 transition)) == GST_STATE_CHANGE_FAILURE)
2048 switch (transition) {
2049 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2050 GST_LIVE_LOCK (element);
2051 if (basesrc->is_live) {
2053 basesrc->live_running = FALSE;
2055 GST_LIVE_UNLOCK (element);
2057 case GST_STATE_CHANGE_PAUSED_TO_READY:
2061 /* FIXME, deprecate this behaviour, it is very dangerous.
2062 * the prefered way of sending EOS downstream is by sending
2063 * the EOS event to the element */
2064 if (!basesrc->priv->last_sent_eos) {
2065 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2066 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2067 basesrc->priv->last_sent_eos = TRUE;
2069 event_p = &basesrc->data.ABI.pending_seek;
2070 gst_event_replace (event_p, NULL);
2071 event_p = &basesrc->priv->close_segment;
2072 gst_event_replace (event_p, NULL);
2073 event_p = &basesrc->priv->start_segment;
2074 gst_event_replace (event_p, NULL);
2077 case GST_STATE_CHANGE_READY_TO_NULL:
2083 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2084 result = GST_STATE_CHANGE_NO_PREROLL;
2091 GST_DEBUG_OBJECT (basesrc, "parent failed state change");