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_NEW_SEGMENT
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 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
55 * automatically seekable in push mode as well. The following conditions must
56 * be met to make the element seekable in push mode when the format is not
60 * #GstBaseSrc::is_seekable returns TRUE.
63 * #GstBaseSrc::query can convert all supported seek formats to the
64 * internal format as set with gst_base_src_set_format().
67 * #GstBaseSrc::do_seek is implemented, performs the seek and returns TRUE.
72 * When the default format is not GST_FORMAT_BYTES, the subclass should ignore
73 * the offset and length in the #GstBaseSrc::create method. It is recommended
74 * to subclass #GstPushSrc instead in this situation.
77 * #GstBaseSrc has support for live sources. Live sources are sources that
78 * produce data at a fixed rate, such as audio or video capture devices. A
79 * typical live source also provides a clock to publish the rate at which
81 * Use gst_base_src_set_live() to activate the live source mode.
84 * A live source does not produce data in the PAUSED state. This means that the
85 * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
86 * To signal the pipeline that the element will not produce data, the return
87 * value from the READY to PAUSED state will be GST_STATE_CHANGE_NO_PREROLL.
90 * A typical live source will timestamp the buffers it creates with the
91 * current stream time of the pipeline. This is one reason why a live source
92 * can only produce data in the PLAYING state, when the clock is actually
93 * distributed and running.
96 * The #GstBaseSrc::get_times method can be used to implement pseudo-live
97 * sources. The base source will wait for the specified stream time returned in
98 * #GstBaseSrc::get_times before pushing out the buffer.
99 * It only makes sense to implement the ::get_times function if the source is
103 * There is only support in GstBaseSrc for exactly one source pad, which
104 * should be named "src". A source implementation (subclass of GstBaseSrc)
105 * should install a pad template in its base_init function, like so:
110 * my_element_base_init (gpointer g_class)
112 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
113 * // srctemplate should be a #GstStaticPadTemplate with direction
114 * // #GST_PAD_SRC and name "src"
115 * gst_element_class_add_pad_template (gstelement_class,
116 * gst_static_pad_template_get (&srctemplate));
117 * // see #GstElementDetails
118 * gst_element_class_set_details (gstelement_class, &details);
122 * <title>Controlled shutdown of live sources in applications</title>
124 * Applications that record from a live source may want to stop recording
125 * in a controlled way, so that the recording is stopped, but the data
126 * already in the pipeline is processed to the end (remember that many live
127 * sources would go on recording forever otherwise). For that to happen the
128 * application needs to make the source stop recording and send an EOS
129 * event down the pipeline. The application would then wait for an
130 * EOS message posted on the pipeline's bus to know when all data has
131 * been processed and the pipeline can safely be stopped.
134 * Since GStreamer 0.10.3 an application may simply set the source
135 * element to NULL or READY state to make it send an EOS event downstream.
136 * The application should lock the state of the source afterwards, so that
137 * shutting down the pipeline from PLAYING doesn't temporarily start up the
138 * source element for a second time:
142 * gst_element_set_state (audio_source, GST_STATE_NULL);
143 * gst_element_set_locked_state (audio_source, TRUE);
146 * Now the application should wait for an EOS message
147 * to be posted on the pipeline's bus. Once it has received
148 * an EOS message, it may safely shut down the entire pipeline:
151 * // everything done - shut down pipeline
152 * gst_element_set_state (pipeline, GST_STATE_NULL);
153 * gst_element_set_locked_state (audio_source, FALSE);
158 * Note that setting the source element to NULL or READY when the
159 * pipeline is in the PAUSED state may cause a deadlock since the streaming
160 * thread might be blocked in PREROLL.
163 * Last reviewed on 2006-04-14 (0.10.6)
175 #include "gstbasesrc.h"
176 #include "gsttypefindhelper.h"
177 #include <gst/gstmarshal.h>
178 #include <gst/gst-i18n-lib.h>
180 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
181 #define GST_CAT_DEFAULT gst_base_src_debug
183 #define GST_LIVE_GET_LOCK(elem) (GST_BASE_SRC_CAST(elem)->live_lock)
184 #define GST_LIVE_LOCK(elem) g_mutex_lock(GST_LIVE_GET_LOCK(elem))
185 #define GST_LIVE_TRYLOCK(elem) g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
186 #define GST_LIVE_UNLOCK(elem) g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
187 #define GST_LIVE_GET_COND(elem) (GST_BASE_SRC_CAST(elem)->live_cond)
188 #define GST_LIVE_WAIT(elem) g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
189 #define GST_LIVE_TIMED_WAIT(elem, timeval) g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
191 #define GST_LIVE_SIGNAL(elem) g_cond_signal (GST_LIVE_GET_COND (elem));
192 #define GST_LIVE_BROADCAST(elem) g_cond_broadcast (GST_LIVE_GET_COND (elem));
194 /* BaseSrc signals and args */
201 #define DEFAULT_BLOCKSIZE 4096
202 #define DEFAULT_NUM_BUFFERS -1
203 #define DEFAULT_TYPEFIND FALSE
213 #define GST_BASE_SRC_GET_PRIVATE(obj) \
214 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
216 struct _GstBaseSrcPrivate
218 gboolean last_sent_eos; /* last thing we did was send an EOS (we set this
219 * to avoid the sending of two EOS in some cases) */
222 /* two segments to be sent in the streaming thread with STREAM_LOCK */
223 GstEvent *close_segment;
224 GstEvent *start_segment;
227 static GstElementClass *parent_class = NULL;
229 static void gst_base_src_base_init (gpointer g_class);
230 static void gst_base_src_class_init (GstBaseSrcClass * klass);
231 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
232 static void gst_base_src_finalize (GObject * object);
236 gst_base_src_get_type (void)
238 static GType base_src_type = 0;
240 if (G_UNLIKELY (base_src_type == 0)) {
241 static const GTypeInfo base_src_info = {
242 sizeof (GstBaseSrcClass),
243 (GBaseInitFunc) gst_base_src_base_init,
245 (GClassInitFunc) gst_base_src_class_init,
250 (GInstanceInitFunc) gst_base_src_init,
253 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
254 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
256 return base_src_type;
258 static GstCaps *gst_base_src_getcaps (GstPad * pad);
259 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
261 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
262 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
263 static void gst_base_src_set_property (GObject * object, guint prop_id,
264 const GValue * value, GParamSpec * pspec);
265 static void gst_base_src_get_property (GObject * object, guint prop_id,
266 GValue * value, GParamSpec * pspec);
267 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
268 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
269 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
270 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
272 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
274 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
275 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
276 GstSegment * segment);
277 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
279 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
280 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
281 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
283 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
284 GstStateChange transition);
286 static void gst_base_src_loop (GstPad * pad);
287 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
288 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
289 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
290 guint length, GstBuffer ** buf);
291 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
292 guint length, GstBuffer ** buf);
295 gst_base_src_base_init (gpointer g_class)
297 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
301 gst_base_src_class_init (GstBaseSrcClass * klass)
303 GObjectClass *gobject_class;
304 GstElementClass *gstelement_class;
306 gobject_class = G_OBJECT_CLASS (klass);
307 gstelement_class = GST_ELEMENT_CLASS (klass);
309 g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
311 parent_class = g_type_class_peek_parent (klass);
313 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
314 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
315 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
317 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
318 g_param_spec_ulong ("blocksize", "Block size",
319 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
321 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
322 g_param_spec_int ("num-buffers", "num-buffers",
323 "Number of buffers to output before sending EOS", -1, G_MAXINT,
324 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
325 g_object_class_install_property (gobject_class, PROP_TYPEFIND,
326 g_param_spec_boolean ("typefind", "Typefind",
327 "Run typefind before negotiating", DEFAULT_TYPEFIND,
330 gstelement_class->change_state =
331 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
332 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
333 gstelement_class->get_query_types =
334 GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
336 klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
337 klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
338 klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
339 klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
340 klass->check_get_range =
341 GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
345 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
348 GstPadTemplate *pad_template;
350 basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
352 basesrc->is_live = FALSE;
353 basesrc->live_lock = g_mutex_new ();
354 basesrc->live_cond = g_cond_new ();
355 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
356 basesrc->num_buffers_left = -1;
358 basesrc->can_activate_push = TRUE;
359 basesrc->pad_mode = GST_ACTIVATE_NONE;
362 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
363 g_return_if_fail (pad_template != NULL);
365 GST_DEBUG_OBJECT (basesrc, "creating src pad");
366 pad = gst_pad_new_from_template (pad_template, "src");
368 GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
369 gst_pad_set_activatepush_function (pad,
370 GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
371 gst_pad_set_activatepull_function (pad,
372 GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
373 gst_pad_set_event_function (pad,
374 GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
375 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
376 gst_pad_set_checkgetrange_function (pad,
377 GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
378 gst_pad_set_getrange_function (pad,
379 GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
380 gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
381 gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
383 /* hold pointer to pad */
384 basesrc->srcpad = pad;
385 GST_DEBUG_OBJECT (basesrc, "adding src pad");
386 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
388 basesrc->blocksize = DEFAULT_BLOCKSIZE;
389 basesrc->clock_id = NULL;
390 /* we operate in BYTES by default */
391 gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
392 basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
394 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
396 GST_DEBUG_OBJECT (basesrc, "init done");
400 gst_base_src_finalize (GObject * object)
405 basesrc = GST_BASE_SRC (object);
407 g_mutex_free (basesrc->live_lock);
408 g_cond_free (basesrc->live_cond);
410 event_p = &basesrc->data.ABI.pending_seek;
411 gst_event_replace ((GstEvent **) event_p, NULL);
413 G_OBJECT_CLASS (parent_class)->finalize (object);
417 * gst_base_src_set_live:
418 * @src: base source instance
419 * @live: new live-mode
421 * If the element listens to a live source, @live should
424 * A live source will not produce data in the PAUSED state and
425 * will therefore not be able to participate in the PREROLL phase
426 * of a pipeline. To signal this fact to the application and the
427 * pipeline, the state change return value of the live source will
428 * be GST_STATE_CHANGE_NO_PREROLL.
431 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
435 GST_LIVE_UNLOCK (src);
439 * gst_base_src_is_live:
440 * @src: base source instance
442 * Check if an element is in live mode.
444 * Returns: %TRUE if element is in live mode.
447 gst_base_src_is_live (GstBaseSrc * src)
452 result = src->is_live;
453 GST_LIVE_UNLOCK (src);
459 * gst_base_src_set_format:
460 * @src: base source instance
461 * @format: the format to use
463 * Sets the default format of the source. This will be the format used
464 * for sending NEW_SEGMENT events and for performing seeks.
466 * If a format of GST_FORMAT_BYTES is set, the element will be able to
467 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
472 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
474 gst_segment_init (&src->segment, format);
478 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
480 GstBaseSrcClass *bclass;
484 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
485 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
487 if (bclass->set_caps)
488 res = bclass->set_caps (bsrc, caps);
494 gst_base_src_getcaps (GstPad * pad)
496 GstBaseSrcClass *bclass;
498 GstCaps *caps = NULL;
500 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
501 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
502 if (bclass->get_caps)
503 caps = bclass->get_caps (bsrc);
506 GstPadTemplate *pad_template;
509 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
510 if (pad_template != NULL) {
511 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
518 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
522 switch (GST_QUERY_TYPE (query)) {
523 case GST_QUERY_POSITION:
527 gst_query_parse_position (query, &format, NULL);
529 case GST_FORMAT_PERCENT:
535 position = src->segment.last_stop;
536 duration = src->segment.duration;
538 if (position != -1 && duration != -1) {
539 if (position < duration)
540 percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
543 percent = GST_FORMAT_PERCENT_MAX;
547 gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
555 position = src->segment.last_stop;
557 if (position != -1) {
558 /* convert to requested format */
560 gst_pad_query_convert (src->srcpad, src->segment.format,
561 position, &format, &position);
565 gst_query_set_position (query, format, position);
571 case GST_QUERY_DURATION:
575 gst_query_parse_duration (query, &format, NULL);
577 case GST_FORMAT_PERCENT:
578 gst_query_set_duration (query, GST_FORMAT_PERCENT,
579 GST_FORMAT_PERCENT_MAX);
586 duration = src->segment.duration;
588 if (duration != -1) {
589 /* convert to requested format */
591 gst_pad_query_convert (src->srcpad, src->segment.format,
592 duration, &format, &duration);
596 gst_query_set_duration (query, format, duration);
603 case GST_QUERY_SEEKING:
605 gst_query_set_seeking (query, src->segment.format,
606 src->seekable, 0, src->segment.duration);
610 case GST_QUERY_SEGMENT:
614 /* no end segment configured, current duration then */
615 if ((stop = src->segment.stop) == -1)
616 stop = src->segment.duration;
617 start = src->segment.start;
619 /* adjust to stream time */
620 if (src->segment.time != -1) {
621 start -= src->segment.time;
623 stop -= src->segment.time;
625 gst_query_set_segment (query, src->segment.rate, src->segment.format,
631 case GST_QUERY_FORMATS:
633 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
634 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
638 case GST_QUERY_LATENCY:
639 case GST_QUERY_JITTER:
641 case GST_QUERY_CONVERT:
643 GstFormat src_fmt, dest_fmt;
644 gint64 src_val, dest_val;
646 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
648 /* we can only convert between equal formats... */
649 if (src_fmt == dest_fmt) {
655 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
666 gst_base_src_query (GstPad * pad, GstQuery * query)
669 GstBaseSrcClass *bclass;
670 gboolean result = FALSE;
672 src = GST_BASE_SRC (gst_pad_get_parent (pad));
674 bclass = GST_BASE_SRC_GET_CLASS (src);
677 result = bclass->query (src, query);
679 result = gst_pad_query_default (pad, query);
681 gst_object_unref (src);
687 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
691 /* update our offset if the start/stop position was updated */
692 if (segment->format == GST_FORMAT_BYTES) {
693 segment->last_stop = segment->start;
694 segment->time = segment->start;
695 } else if (segment->start == 0) {
696 /* seek to start, we can implement a default for this. */
697 segment->last_stop = 0;
707 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
709 GstBaseSrcClass *bclass;
710 gboolean result = FALSE;
712 bclass = GST_BASE_SRC_GET_CLASS (src);
715 result = bclass->do_seek (src, segment);
720 /* this code implements the seeking. It is a good example
721 * handling all cases.
723 * A seek updates the currently configured segment.start
724 * and segment.stop values based on the SEEK_TYPE. If the
725 * segment.start value is updated, a seek to this new position
726 * should be performed.
728 * The seek can only be executed when we are not currently
729 * streaming any data, to make sure that this is the case, we
730 * acquire the STREAM_LOCK which is taken when we are in the
731 * _loop() function or when a getrange() is called. Normally
732 * we will not receive a seek if we are operating in pull mode
735 * When we are in the loop() function, we might be in the middle
736 * of pushing a buffer, which might block in a sink. To make sure
737 * that the push gets unblocked we push out a FLUSH_START event.
738 * Our loop function will get a WRONG_STATE return value from
739 * the push and will pause, effectively releasing the STREAM_LOCK.
741 * For a non-flushing seek, we pause the task, which might eventually
742 * release the STREAM_LOCK. We say eventually because when the sink
743 * blocks on the sample we might wait a very long time until the sink
744 * unblocks the sample. In any case we acquire the STREAM_LOCK and
745 * can continue the seek. A non-flushing seek is normally done in a
746 * running pipeline to perform seamless playback.
747 * In the case of a non-flushing seek we need to make sure that the
748 * data we output after the seek is continuous with the previous data,
749 * this is because a non-flushing seek does not reset the stream-time
750 * to 0. We do this by closing the currently running segment, ie. sending
751 * a new_segment event with the stop position set to the last processed
754 * After updating the segment.start/stop values, we prepare for
755 * streaming again. We push out a FLUSH_STOP to make the peer pad
756 * accept data again and we start our task again.
758 * A segment seek posts a message on the bus saying that the playback
759 * of the segment started. We store the segment flag internally because
760 * when we reach the segment.stop we have to post a segment.done
761 * instead of EOS when doing a segment seek.
763 /* FIXME, we have the unlock gboolean here because most current implementations
764 * (fdsrc, -base/gst/tcp/, ...) not only unlock when there is something to
768 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
774 GstSeekType cur_type, stop_type;
778 GstSegment seeksegment;
780 GST_DEBUG_OBJECT (src, "doing seek");
783 gst_event_parse_seek (event, &rate, &format, &flags,
784 &cur_type, &cur, &stop_type, &stop);
786 /* we have to have a format as the segment format. Try to convert
788 if (src->segment.format != format) {
791 fmt = src->segment.format;
793 if (cur_type != GST_SEEK_TYPE_NONE)
794 res = gst_pad_query_convert (src->srcpad, format, cur, &fmt, &cur);
795 if (res && stop_type != GST_SEEK_TYPE_NONE)
796 res = gst_pad_query_convert (src->srcpad, format, stop, &fmt, &stop);
806 flush = flags & GST_SEEK_FLAG_FLUSH;
808 /* send flush start */
810 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
812 gst_pad_pause_task (src->srcpad);
814 /* unblock streaming thread */
816 gst_base_src_unlock (src);
818 /* grab streaming lock, this should eventually be possible, either
819 * because the task is paused or our streaming thread stopped
820 * because our peer is flushing. */
821 GST_PAD_STREAM_LOCK (src->srcpad);
823 /* make copy into temp structure, we can only update the main one
824 * when the subclass actually could do the seek. */
825 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
827 /* now configure the seek segment */
829 gst_segment_set_seek (&seeksegment, rate, format, flags,
830 cur_type, cur, stop_type, stop, &update);
833 GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
834 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
835 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
837 /* do the seek, segment.last_stop contains new position. */
838 res = gst_base_src_do_seek (src, &seeksegment);
840 /* and prepare to continue streaming */
842 /* send flush stop, peer will accept data and events again. We
843 * are not yet providing data as we still have the STREAM_LOCK. */
844 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
845 } else if (res && src->data.ABI.running) {
846 /* we are running the current segment and doing a non-flushing seek,
847 * close the segment first based on the last_stop. */
848 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
849 " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
851 if (src->priv->close_segment)
852 gst_event_unref (src->priv->close_segment);
853 src->priv->close_segment =
854 gst_event_new_new_segment_full (TRUE,
855 src->segment.rate, src->segment.applied_rate, src->segment.format,
856 src->segment.start, src->segment.last_stop, src->segment.time);
859 /* if successfull seek, we update our real segment and push
860 * out the new segment. */
862 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
864 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
865 gst_element_post_message (GST_ELEMENT (src),
866 gst_message_new_segment_start (GST_OBJECT (src),
867 src->segment.format, src->segment.last_stop));
870 if ((stop = src->segment.stop) == -1)
871 stop = src->segment.duration;
873 /* now send the newsegment */
874 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
875 " to %" G_GINT64_FORMAT, src->segment.start, stop);
877 if (src->priv->start_segment)
878 gst_event_unref (src->priv->start_segment);
879 src->priv->start_segment =
880 gst_event_new_new_segment_full (FALSE,
881 src->segment.rate, src->segment.applied_rate, src->segment.format,
882 src->segment.last_stop, stop, src->segment.time);
885 src->priv->discont = TRUE;
886 src->data.ABI.running = TRUE;
887 /* and restart the task in case it got paused explicitely or by
888 * the FLUSH_START event we pushed out. */
889 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
892 /* and release the lock again so we can continue streaming */
893 GST_PAD_STREAM_UNLOCK (src->srcpad);
900 GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
905 static const GstQueryType *
906 gst_base_src_get_query_types (GstElement * element)
908 static const GstQueryType query_types[] = {
924 /* all events send to this element directly
927 gst_base_src_send_event (GstElement * element, GstEvent * event)
930 gboolean result = FALSE;
932 src = GST_BASE_SRC (element);
934 switch (GST_EVENT_TYPE (event)) {
935 case GST_EVENT_FLUSH_START:
936 case GST_EVENT_FLUSH_STOP:
937 /* sending random flushes downstream can break stuff,
938 * especially sync since all segment info will get flushed */
941 /* FIXME, queue EOS and make sure the task or pull function
942 * perform the EOS actions. */
944 case GST_EVENT_NEWSEGMENT:
945 /* sending random NEWSEGMENT downstream can break sync. */
948 case GST_EVENT_BUFFERSIZE:
956 GST_OBJECT_LOCK (src->srcpad);
957 if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
959 started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
960 GST_OBJECT_UNLOCK (src->srcpad);
963 /* when we are running in push mode, we can execute the
964 * seek right now, we need to unlock. */
965 result = gst_base_src_perform_seek (src, event, TRUE);
969 /* else we store the event and execute the seek when we
971 GST_OBJECT_LOCK (src);
972 event_p = &src->data.ABI.pending_seek;
973 gst_event_replace ((GstEvent **) event_p, event);
974 GST_OBJECT_UNLOCK (src);
975 /* assume the seek will work */
980 case GST_EVENT_NAVIGATION:
986 gst_event_unref (event);
993 GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
994 GST_OBJECT_UNLOCK (src->srcpad);
1001 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1005 switch (GST_EVENT_TYPE (event)) {
1006 case GST_EVENT_SEEK:
1007 /* is normally called when in push mode */
1011 result = gst_base_src_perform_seek (src, event, TRUE);
1013 case GST_EVENT_FLUSH_START:
1014 /* cancel any blocking getrange, is normally called
1015 * when in pull mode. */
1016 result = gst_base_src_unlock (src);
1018 case GST_EVENT_FLUSH_STOP:
1028 GST_DEBUG_OBJECT (src, "is not seekable");
1034 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1037 GstBaseSrcClass *bclass;
1038 gboolean result = FALSE;
1040 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1041 bclass = GST_BASE_SRC_GET_CLASS (src);
1043 if (bclass->event) {
1044 if (!(result = bclass->event (src, event)))
1045 goto subclass_failed;
1049 gst_event_unref (event);
1050 gst_object_unref (src);
1057 GST_DEBUG_OBJECT (src, "subclass refused event");
1063 gst_base_src_set_property (GObject * object, guint prop_id,
1064 const GValue * value, GParamSpec * pspec)
1068 src = GST_BASE_SRC (object);
1071 case PROP_BLOCKSIZE:
1072 src->blocksize = g_value_get_ulong (value);
1074 case PROP_NUM_BUFFERS:
1075 src->num_buffers = g_value_get_int (value);
1078 src->data.ABI.typefind = g_value_get_boolean (value);
1081 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1087 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1092 src = GST_BASE_SRC (object);
1095 case PROP_BLOCKSIZE:
1096 g_value_set_ulong (value, src->blocksize);
1098 case PROP_NUM_BUFFERS:
1099 g_value_set_int (value, src->num_buffers);
1102 g_value_set_boolean (value, src->data.ABI.typefind);
1105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1110 /* with STREAM_LOCK and LOCK*/
1111 static GstClockReturn
1112 gst_base_src_wait (GstBaseSrc * basesrc, GstClockTime time)
1118 /* get clock, if no clock, we don't sync */
1119 if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1120 return GST_CLOCK_OK;
1122 /* clock_id should be NULL outside of this function */
1123 g_assert (basesrc->clock_id == NULL);
1124 g_assert (GST_CLOCK_TIME_IS_VALID (time));
1126 id = gst_clock_new_single_shot_id (clock, time);
1128 basesrc->clock_id = id;
1129 /* release the object lock while waiting */
1130 GST_OBJECT_UNLOCK (basesrc);
1132 ret = gst_clock_id_wait (id, NULL);
1134 GST_OBJECT_LOCK (basesrc);
1135 gst_clock_id_unref (id);
1136 basesrc->clock_id = NULL;
1141 /* perform synchronisation on a buffer.
1144 static GstClockReturn
1145 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1147 GstClockReturn result;
1148 GstClockTime start, end;
1149 GstBaseSrcClass *bclass;
1150 GstClockTime base_time;
1152 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1155 if (bclass->get_times)
1156 bclass->get_times (basesrc, buffer, &start, &end);
1158 /* if we don't have a timestamp, we don't sync */
1159 if (!GST_CLOCK_TIME_IS_VALID (start))
1162 /* now do clocking */
1163 GST_OBJECT_LOCK (basesrc);
1164 base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1166 GST_LOG_OBJECT (basesrc,
1167 "waiting for clock, base time %" GST_TIME_FORMAT
1168 ", stream_start %" GST_TIME_FORMAT,
1169 GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1171 result = gst_base_src_wait (basesrc, start + base_time);
1172 GST_OBJECT_UNLOCK (basesrc);
1174 GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1181 GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1182 return GST_CLOCK_OK;
1187 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1189 guint64 size, maxsize;
1190 GstBaseSrcClass *bclass;
1192 bclass = GST_BASE_SRC_GET_CLASS (src);
1194 /* only operate if we are working with bytes */
1195 if (src->segment.format != GST_FORMAT_BYTES)
1198 size = (guint64) src->segment.duration;
1200 /* the max amount of bytes to read is the total size or
1201 * up to the segment.stop if present. */
1202 if (src->segment.stop != -1)
1203 maxsize = MIN (size, src->segment.stop);
1207 GST_DEBUG_OBJECT (src,
1208 "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1209 ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1210 *length, size, src->segment.stop, maxsize);
1213 if (maxsize != -1) {
1214 if (offset > maxsize)
1215 goto unexpected_length;
1217 if (offset + *length > maxsize) {
1218 /* see if length of the file changed */
1219 if (bclass->get_size)
1220 bclass->get_size (src, &size);
1222 if (src->segment.stop != -1)
1223 maxsize = MIN (size, src->segment.stop);
1227 if (offset + *length > maxsize) {
1228 *length = maxsize - offset;
1233 goto unexpected_length;
1235 /* keep track f current position. segment is in bytes, we checked
1237 gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1248 static GstFlowReturn
1249 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1253 GstBaseSrcClass *bclass;
1254 GstClockReturn status;
1256 bclass = GST_BASE_SRC_GET_CLASS (src);
1258 GST_LIVE_LOCK (src);
1260 while (!src->live_running) {
1261 GST_DEBUG ("live source signal waiting");
1262 GST_LIVE_SIGNAL (src);
1263 GST_DEBUG ("live source waiting for running state");
1264 GST_LIVE_WAIT (src);
1265 GST_DEBUG ("live source unlocked");
1267 /* FIXME, use another variable to signal stopping */
1268 GST_OBJECT_LOCK (src->srcpad);
1269 if (GST_PAD_IS_FLUSHING (src->srcpad))
1271 GST_OBJECT_UNLOCK (src->srcpad);
1273 GST_LIVE_UNLOCK (src);
1275 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED))
1278 if (G_UNLIKELY (!bclass->create))
1281 if (!gst_base_src_update_length (src, offset, &length))
1282 goto unexpected_length;
1284 if (src->num_buffers_left == 0) {
1285 goto reached_num_buffers;
1287 if (src->num_buffers_left > 0)
1288 src->num_buffers_left--;
1291 GST_DEBUG_OBJECT (src,
1292 "calling create offset %" G_GUINT64_FORMAT " %" G_GINT64_FORMAT, offset,
1295 ret = bclass->create (src, offset, length, buf);
1296 if (ret != GST_FLOW_OK)
1299 /* no timestamp set and we are at offset 0 */
1300 if (offset == 0 && src->segment.time == 0
1301 && GST_BUFFER_TIMESTAMP (*buf) == -1)
1302 GST_BUFFER_TIMESTAMP (*buf) = 0;
1304 /* now sync before pushing the buffer */
1305 status = gst_base_src_do_sync (src, *buf);
1307 case GST_CLOCK_EARLY:
1308 GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1311 GST_DEBUG_OBJECT (src, "buffer ok");
1314 GST_DEBUG_OBJECT (src, "clock returned %d, not returning", status);
1315 gst_buffer_unref (*buf);
1317 ret = GST_FLOW_WRONG_STATE;
1326 GST_DEBUG_OBJECT (src, "pad is flushing");
1327 GST_OBJECT_UNLOCK (src->srcpad);
1328 GST_LIVE_UNLOCK (src);
1329 return GST_FLOW_WRONG_STATE;
1333 GST_DEBUG_OBJECT (src, "getrange but not started");
1334 return GST_FLOW_WRONG_STATE;
1338 GST_DEBUG_OBJECT (src, "no create function");
1339 return GST_FLOW_ERROR;
1343 GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1344 ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1345 return GST_FLOW_UNEXPECTED;
1347 reached_num_buffers:
1349 GST_DEBUG_OBJECT (src, "sent all buffers");
1350 return GST_FLOW_UNEXPECTED;
1354 static GstFlowReturn
1355 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1361 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1363 res = gst_base_src_get_range (src, offset, length, buf);
1365 gst_object_unref (src);
1371 gst_base_src_pad_check_get_range (GstPad * pad)
1373 GstBaseSrcClass *bclass;
1377 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1379 bclass = GST_BASE_SRC_GET_CLASS (src);
1381 if (bclass->check_get_range == NULL) {
1382 GST_WARNING_OBJECT (src, "no check_get_range function set");
1386 res = bclass->check_get_range (src);
1387 GST_LOG_OBJECT (src, "%s() returned %d",
1388 GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1390 gst_object_unref (src);
1396 gst_base_src_default_check_get_range (GstBaseSrc * src)
1400 if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1401 GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1402 gst_base_src_start (src);
1403 gst_base_src_stop (src);
1406 /* we can operate in getrange mode if the native format is bytes
1407 * and we are seekable, this condition is set in the random_access
1408 * flag and is set in the _start() method. */
1409 res = src->random_access;
1415 gst_base_src_loop (GstPad * pad)
1418 GstBuffer *buf = NULL;
1425 src = GST_BASE_SRC (gst_pad_get_parent (pad));
1427 src->priv->last_sent_eos = FALSE;
1429 /* push events to close/start our segment before we do the real work. */
1430 if (src->priv->close_segment) {
1431 gst_pad_push_event (pad, src->priv->close_segment);
1432 src->priv->close_segment = NULL;
1434 if (src->priv->start_segment) {
1435 gst_pad_push_event (pad, src->priv->start_segment);
1436 src->priv->start_segment = NULL;
1439 /* if we operate in bytes, we can calculate an offset */
1440 if (src->segment.format == GST_FORMAT_BYTES)
1441 position = src->segment.last_stop;
1445 ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1446 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1447 GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %d", ret);
1450 if (G_UNLIKELY (buf == NULL))
1453 /* figure out the new position */
1454 switch (src->segment.format) {
1455 case GST_FORMAT_BYTES:
1456 position += GST_BUFFER_SIZE (buf);
1458 case GST_FORMAT_TIME:
1460 GstClockTime start, duration;
1462 start = GST_BUFFER_TIMESTAMP (buf);
1463 duration = GST_BUFFER_DURATION (buf);
1465 if (GST_CLOCK_TIME_IS_VALID (start))
1468 position = src->segment.last_stop;
1470 if (GST_CLOCK_TIME_IS_VALID (duration))
1471 position += duration;
1474 case GST_FORMAT_DEFAULT:
1475 position = GST_BUFFER_OFFSET_END (buf);
1481 if (position != -1) {
1482 if (src->segment.stop != -1) {
1483 if (position >= src->segment.stop) {
1485 position = src->segment.stop;
1488 gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1491 if (G_UNLIKELY (src->priv->discont)) {
1492 buf = gst_buffer_make_metadata_writable (buf);
1493 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1494 src->priv->discont = FALSE;
1497 ret = gst_pad_push (pad, buf);
1498 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1499 GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %d", ret);
1504 GST_INFO_OBJECT (src, "pausing after EOS");
1505 ret = GST_FLOW_UNEXPECTED;
1510 gst_object_unref (src);
1516 const gchar *reason = gst_flow_get_name (ret);
1518 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1519 src->data.ABI.running = FALSE;
1520 gst_pad_pause_task (pad);
1521 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1522 if (ret == GST_FLOW_UNEXPECTED) {
1523 /* perform EOS logic */
1524 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1525 gst_element_post_message (GST_ELEMENT (src),
1526 gst_message_new_segment_done (GST_OBJECT (src),
1527 src->segment.format, src->segment.last_stop));
1529 gst_pad_push_event (pad, gst_event_new_eos ());
1530 src->priv->last_sent_eos = TRUE;
1533 /* for fatal errors we post an error message */
1534 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1535 (_("Internal data flow error.")),
1536 ("streaming task paused, reason %s", reason));
1537 gst_pad_push_event (pad, gst_event_new_eos ());
1538 src->priv->last_sent_eos = TRUE;
1545 GST_ELEMENT_ERROR (src, STREAM, FAILED,
1546 (_("Internal data flow error.")), ("element returned NULL buffer"));
1547 /* we finished the segment on error */
1548 src->data.ABI.running = FALSE;
1549 gst_pad_pause_task (pad);
1550 gst_pad_push_event (pad, gst_event_new_eos ());
1551 src->priv->last_sent_eos = TRUE;
1556 /* this will always be called between start() and stop(). So you can rely on
1557 * resources allocated by start() and freed from stop(). This needs to be added
1558 * to the docs at some point. */
1560 gst_base_src_unlock (GstBaseSrc * basesrc)
1562 GstBaseSrcClass *bclass;
1563 gboolean result = TRUE;
1565 GST_DEBUG ("unlock");
1566 /* unblock whatever the subclass is doing */
1567 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1569 result = bclass->unlock (basesrc);
1571 GST_DEBUG ("unschedule clock");
1572 /* and unblock the clock as well, if any */
1573 GST_OBJECT_LOCK (basesrc);
1574 if (basesrc->clock_id) {
1575 gst_clock_id_unschedule (basesrc->clock_id);
1577 GST_OBJECT_UNLOCK (basesrc);
1579 GST_DEBUG ("unlock done");
1584 /* default negotiation code.
1586 * Take intersection between src and sink pads, take first
1590 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1593 GstCaps *caps = NULL;
1594 GstCaps *peercaps = NULL;
1595 gboolean result = FALSE;
1597 /* first see what is possible on our source pad */
1598 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
1599 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1600 /* nothing or anything is allowed, we're done */
1601 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1602 goto no_nego_needed;
1604 /* get the peer caps */
1605 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
1606 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1610 /* get intersection */
1611 icaps = gst_caps_intersect (thiscaps, peercaps);
1612 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
1613 gst_caps_unref (thiscaps);
1614 gst_caps_unref (peercaps);
1616 /* take first (and best, since they are sorted) possibility */
1617 caps = gst_caps_copy_nth (icaps, 0);
1618 gst_caps_unref (icaps);
1621 /* no peer, work with our own caps then */
1625 caps = gst_caps_make_writable (caps);
1626 gst_caps_truncate (caps);
1629 if (!gst_caps_is_empty (caps)) {
1630 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
1631 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1633 if (gst_caps_is_any (caps)) {
1634 /* hmm, still anything, so element can do anything and
1635 * nego is not needed */
1637 } else if (gst_caps_is_fixed (caps)) {
1638 /* yay, fixed caps, use those then */
1639 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1643 gst_caps_unref (caps);
1649 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1651 gst_caps_unref (thiscaps);
1657 gst_base_src_negotiate (GstBaseSrc * basesrc)
1659 GstBaseSrcClass *bclass;
1660 gboolean result = TRUE;
1662 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1664 if (bclass->negotiate)
1665 result = bclass->negotiate (basesrc);
1671 gst_base_src_start (GstBaseSrc * basesrc)
1673 GstBaseSrcClass *bclass;
1677 if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1680 GST_DEBUG_OBJECT (basesrc, "starting source");
1682 basesrc->num_buffers_left = basesrc->num_buffers;
1684 gst_segment_init (&basesrc->segment, basesrc->segment.format);
1685 basesrc->data.ABI.running = FALSE;
1687 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1689 result = bclass->start (basesrc);
1694 goto could_not_start;
1696 GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
1698 /* figure out the size */
1699 if (basesrc->segment.format == GST_FORMAT_BYTES) {
1700 if (bclass->get_size) {
1701 if (!(result = bclass->get_size (basesrc, &size)))
1707 /* only update the size when operating in bytes, subclass is supposed
1708 * to set duration in the start method for other formats */
1709 gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
1712 GST_DEBUG_OBJECT (basesrc,
1713 "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
1714 G_GINT64_FORMAT, basesrc->segment.format, result, size,
1715 basesrc->segment.duration);
1717 /* check if we can seek */
1718 if (bclass->is_seekable)
1719 basesrc->seekable = bclass->is_seekable (basesrc);
1721 basesrc->seekable = FALSE;
1723 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
1725 /* update for random access flag */
1726 basesrc->random_access = basesrc->seekable &&
1727 basesrc->segment.format == GST_FORMAT_BYTES;
1729 GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
1731 /* run typefind if we are random_access and the typefinding is enabled. */
1732 if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
1735 caps = gst_type_find_helper (basesrc->srcpad, size);
1736 gst_pad_set_caps (basesrc->srcpad, caps);
1737 gst_caps_unref (caps);
1739 /* use class or default negotiate function */
1740 if (!gst_base_src_negotiate (basesrc))
1741 goto could_not_negotiate;
1749 GST_DEBUG_OBJECT (basesrc, "could not start");
1752 could_not_negotiate:
1754 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
1755 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
1756 ("Could not negotiate format"), ("Check your filtered caps, if any"));
1757 gst_base_src_stop (basesrc);
1763 gst_base_src_stop (GstBaseSrc * basesrc)
1765 GstBaseSrcClass *bclass;
1766 gboolean result = TRUE;
1768 if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1771 GST_DEBUG_OBJECT (basesrc, "stopping source");
1773 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1775 result = bclass->stop (basesrc);
1778 GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
1784 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
1788 GST_LIVE_LOCK (basesrc);
1789 basesrc->live_running = TRUE;
1790 GST_LIVE_SIGNAL (basesrc);
1791 GST_LIVE_UNLOCK (basesrc);
1793 /* step 1, unblock clock sync (if any) */
1794 result = gst_base_src_unlock (basesrc);
1796 /* step 2, make sure streaming finishes */
1797 result &= gst_pad_stop_task (pad);
1803 gst_base_src_activate_push (GstPad * pad, gboolean active)
1805 GstBaseSrc *basesrc;
1808 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1810 /* prepare subclass first */
1814 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1816 if (!basesrc->can_activate_push)
1817 goto no_push_activation;
1819 if (!gst_base_src_start (basesrc))
1822 basesrc->priv->last_sent_eos = FALSE;
1824 /* do initial seek, which will start the task */
1825 GST_OBJECT_LOCK (basesrc);
1826 event = basesrc->data.ABI.pending_seek;
1827 basesrc->data.ABI.pending_seek = NULL;
1828 GST_OBJECT_UNLOCK (basesrc);
1830 /* no need to unlock anything, the task is certainly
1831 * not running here. */
1832 res = gst_base_src_perform_seek (basesrc, event, FALSE);
1835 gst_event_unref (event);
1837 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1838 res = gst_base_src_deactivate (basesrc, pad);
1845 GST_DEBUG_OBJECT (basesrc, "Subclass disabled push-mode activation");
1850 gst_base_src_stop (basesrc);
1851 GST_DEBUG_OBJECT (basesrc, "Failed to start in push mode");
1857 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1859 GstBaseSrc *basesrc;
1861 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1863 /* prepare subclass first */
1865 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1866 if (!gst_base_src_start (basesrc))
1869 /* if not random_access, we cannot operate in pull mode for now */
1870 if (!basesrc->random_access) {
1871 gst_base_src_stop (basesrc);
1876 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
1878 /* don't send EOS when going from PAUSED => READY when in pull mode */
1879 basesrc->priv->last_sent_eos = TRUE;
1881 if (!gst_base_src_stop (basesrc))
1884 return gst_base_src_deactivate (basesrc, pad);
1889 gst_base_src_stop (basesrc);
1890 GST_DEBUG_OBJECT (basesrc, "Failed to start in pull mode");
1895 GST_DEBUG_OBJECT (basesrc, "Failed to stop in pull mode");
1900 static GstStateChangeReturn
1901 gst_base_src_change_state (GstElement * element, GstStateChange transition)
1903 GstBaseSrc *basesrc;
1904 GstStateChangeReturn result;
1905 gboolean no_preroll = FALSE;
1907 basesrc = GST_BASE_SRC (element);
1909 switch (transition) {
1910 case GST_STATE_CHANGE_NULL_TO_READY:
1912 case GST_STATE_CHANGE_READY_TO_PAUSED:
1913 GST_LIVE_LOCK (element);
1914 if (basesrc->is_live) {
1916 basesrc->live_running = FALSE;
1918 basesrc->priv->last_sent_eos = FALSE;
1919 basesrc->priv->discont = TRUE;
1920 GST_LIVE_UNLOCK (element);
1922 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1923 GST_LIVE_LOCK (element);
1924 if (basesrc->is_live) {
1925 basesrc->live_running = TRUE;
1926 GST_LIVE_SIGNAL (element);
1928 GST_LIVE_UNLOCK (element);
1935 GST_ELEMENT_CLASS (parent_class)->change_state (element,
1936 transition)) == GST_STATE_CHANGE_FAILURE)
1939 switch (transition) {
1940 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1941 GST_LIVE_LOCK (element);
1942 if (basesrc->is_live) {
1944 basesrc->live_running = FALSE;
1946 GST_LIVE_UNLOCK (element);
1948 case GST_STATE_CHANGE_PAUSED_TO_READY:
1952 if (!gst_base_src_stop (basesrc))
1955 /* FIXME, deprecate this behaviour, it is very dangerous.
1956 * the prefered way of sending EOS downstream is by sending
1957 * the EOS event to the element */
1958 if (!basesrc->priv->last_sent_eos) {
1959 GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
1960 gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
1961 basesrc->priv->last_sent_eos = TRUE;
1963 event_p = &basesrc->data.ABI.pending_seek;
1964 gst_event_replace (event_p, NULL);
1965 event_p = &basesrc->priv->close_segment;
1966 gst_event_replace (event_p, NULL);
1967 event_p = &basesrc->priv->start_segment;
1968 gst_event_replace (event_p, NULL);
1971 case GST_STATE_CHANGE_READY_TO_NULL:
1977 if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
1978 result = GST_STATE_CHANGE_NO_PREROLL;
1985 GST_DEBUG_OBJECT (basesrc, "parent failed state change");
1986 gst_base_src_stop (basesrc);
1991 GST_DEBUG_OBJECT (basesrc, "Failed to stop");
1992 return GST_STATE_CHANGE_FAILURE;