docs/random/moving-plugins: Make it clear that the "compiled-in descriptions" really...
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstbasesrc.c:
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gstbasesrc
25  * @short_description: Base class for getrange based source elements
26  * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
27  *
28  * This is a generice base class for source elements. The following
29  * types of sources are supported:
30  * <itemizedlist>
31  *   <listitem><para>random access sources like files</para></listitem>
32  *   <listitem><para>seekable sources</para></listitem>
33  *   <listitem><para>live sources</para></listitem>
34  * </itemizedlist>
35  *
36  * <refsect2>
37  * <para>
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.
42  * </para>
43  * <para>
44  * #GstBaseSrc always supports push mode scheduling. If the following
45  * conditions are met, it also supports pull mode scheduling:
46  * <itemizedlist>
47  *   <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
48  *   </listitem>
49  *   <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
50  *   </listitem>
51  * </itemizedlist>
52  * </para>
53  * <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. 
56  * </para>
57  * <para>
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
61  * #GST_FORMAT_BYTES:
62  * <itemizedlist>
63  *   <listitem><para>
64  *     #GstBaseSrc::is_seekable returns %TRUE.
65  *   </para></listitem>
66  *   <listitem><para>
67  *     #GstBaseSrc::query can convert all supported seek formats to the
68  *     internal format as set with gst_base_src_set_format().
69  *   </para></listitem>
70  *   <listitem><para>
71  *     #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
72  *   </para></listitem>
73  * </itemizedlist>
74  * </para>
75  * <para>
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.
82  * </para>
83  * <para>
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 
87  * they produce data.
88  * Use gst_base_src_set_live() to activate the live source mode.
89  * </para>
90  * <para>
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.
95  * </para>
96  * <para>
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.
101  * </para>
102  * <para>
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.
106  * </para>
107  * <para>
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 
112  * a live source.
113  * </para>
114  * <para>
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:
118  * </para>
119  * <para>
120  * <programlisting>
121  * static void
122  * my_element_base_init (gpointer g_class)
123  * {
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 (&amp;srctemplate));
129  *   // see #GstElementDetails
130  *   gst_element_class_set_details (gstelement_class, &amp;details);
131  * }
132  * </programlisting>
133  * </para>
134  * <title>Controlled shutdown of live sources in applications</title>
135  * <para>
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.
144  * </para>
145  * <para>
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:
151  * <programlisting>
152  * ...
153  * // stop recording
154  * gst_element_set_state (audio_source, #GST_STATE_NULL);
155  * gst_element_set_locked_state (audio_source, %TRUE);
156  * ...
157  * </programlisting>
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:
161  * <programlisting>
162  * ...
163  * // everything done - shut down pipeline
164  * gst_element_set_state (pipeline, #GST_STATE_NULL);
165  * gst_element_set_locked_state (audio_source, %FALSE);
166  * ...
167  * </programlisting>
168  * </para>
169  * <para>
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.
173  * </para>
174  * <para>
175  * Last reviewed on 2006-09-27 (0.10.11)
176  * </para>
177  * </refsect2>
178  */
179
180 #ifdef HAVE_CONFIG_H
181 #  include "config.h"
182 #endif
183
184 #include <stdlib.h>
185 #include <string.h>
186
187 #include "gstbasesrc.h"
188 #include "gsttypefindhelper.h"
189 #include <gst/gstmarshal.h>
190 #include <gst/gst-i18n-lib.h>
191
192 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
193 #define GST_CAT_DEFAULT gst_base_src_debug
194
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),\
202                                                                                 timeval)
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));
205
206 /* BaseSrc signals and args */
207 enum
208 {
209   /* FILL ME */
210   LAST_SIGNAL
211 };
212
213 #define DEFAULT_BLOCKSIZE       4096
214 #define DEFAULT_NUM_BUFFERS     -1
215 #define DEFAULT_TYPEFIND        FALSE
216
217 enum
218 {
219   PROP_0,
220   PROP_BLOCKSIZE,
221   PROP_NUM_BUFFERS,
222   PROP_TYPEFIND,
223 };
224
225 #define GST_BASE_SRC_GET_PRIVATE(obj)  \
226    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
227
228 struct _GstBaseSrcPrivate
229 {
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) */
232   gboolean discont;
233
234   /* two segments to be sent in the streaming thread with STREAM_LOCK */
235   GstEvent *close_segment;
236   GstEvent *start_segment;
237 };
238
239 static GstElementClass *parent_class = NULL;
240
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);
245
246
247 GType
248 gst_base_src_get_type (void)
249 {
250   static GType base_src_type = 0;
251
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,
256       NULL,
257       (GClassInitFunc) gst_base_src_class_init,
258       NULL,
259       NULL,
260       sizeof (GstBaseSrc),
261       0,
262       (GInstanceInitFunc) gst_base_src_init,
263     };
264
265     base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
266         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
267   }
268   return base_src_type;
269 }
270 static GstCaps *gst_base_src_getcaps (GstPad * pad);
271 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
272
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);
283
284 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
285
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);
290
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);
294
295 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
296     GstStateChange transition);
297
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);
305
306 static void
307 gst_base_src_base_init (gpointer g_class)
308 {
309   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
310 }
311
312 static void
313 gst_base_src_class_init (GstBaseSrcClass * klass)
314 {
315   GObjectClass *gobject_class;
316   GstElementClass *gstelement_class;
317
318   gobject_class = G_OBJECT_CLASS (klass);
319   gstelement_class = GST_ELEMENT_CLASS (klass);
320
321   g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
322
323   parent_class = g_type_class_peek_parent (klass);
324
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);
328
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,
340           G_PARAM_READWRITE));
341
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);
347
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);
354 }
355
356 static void
357 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
358 {
359   GstPad *pad;
360   GstPadTemplate *pad_template;
361
362   basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
363
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;
369
370   basesrc->can_activate_push = TRUE;
371   basesrc->pad_mode = GST_ACTIVATE_NONE;
372
373   pad_template =
374       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
375   g_return_if_fail (pad_template != NULL);
376
377   GST_DEBUG_OBJECT (basesrc, "creating src pad");
378   pad = gst_pad_new_from_template (pad_template, "src");
379
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));
394
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);
399
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;
405
406   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
407
408   GST_DEBUG_OBJECT (basesrc, "init done");
409 }
410
411 static void
412 gst_base_src_finalize (GObject * object)
413 {
414   GstBaseSrc *basesrc;
415   GstEvent **event_p;
416
417   basesrc = GST_BASE_SRC (object);
418
419   g_mutex_free (basesrc->live_lock);
420   g_cond_free (basesrc->live_cond);
421
422   event_p = &basesrc->data.ABI.pending_seek;
423   gst_event_replace ((GstEvent **) event_p, NULL);
424
425   G_OBJECT_CLASS (parent_class)->finalize (object);
426 }
427
428 /**
429  * gst_base_src_wait_playing:
430  * @src: the src
431  *
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.
435  *
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).
440  *
441  * Since: 0.10.12
442  *
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.
445  */
446 GstFlowReturn
447 gst_base_src_wait_playing (GstBaseSrc * src)
448 {
449   /* block until the state changes, or we get a flush, or something */
450   GST_LIVE_LOCK (src);
451   if (src->is_live) {
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");
456       GST_LIVE_WAIT (src);
457       GST_DEBUG ("live source unlocked");
458     }
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)))
463       goto flushing;
464     GST_OBJECT_UNLOCK (src->srcpad);
465   }
466   GST_LIVE_UNLOCK (src);
467
468   return GST_FLOW_OK;
469
470   /* ERRORS */
471 flushing:
472   {
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;
477   }
478 }
479
480 /**
481  * gst_base_src_set_live:
482  * @src: base source instance
483  * @live: new live-mode
484  *
485  * If the element listens to a live source, @live should
486  * be set to %TRUE. 
487  *
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.
493  */
494 void
495 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
496 {
497   GST_LIVE_LOCK (src);
498   src->is_live = live;
499   GST_LIVE_UNLOCK (src);
500 }
501
502 /**
503  * gst_base_src_is_live:
504  * @src: base source instance
505  *
506  * Check if an element is in live mode.
507  *
508  * Returns: %TRUE if element is in live mode.
509  */
510 gboolean
511 gst_base_src_is_live (GstBaseSrc * src)
512 {
513   gboolean result;
514
515   GST_LIVE_LOCK (src);
516   result = src->is_live;
517   GST_LIVE_UNLOCK (src);
518
519   return result;
520 }
521
522 /**
523  * gst_base_src_set_format:
524  * @src: base source instance
525  * @format: the format to use
526  *
527  * Sets the default format of the source. This will be the format used
528  * for sending NEW_SEGMENT events and for performing seeks.
529  *
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.
532  *
533  * @Since: 0.10.1
534  */
535 void
536 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
537 {
538   gst_segment_init (&src->segment, format);
539 }
540
541 static gboolean
542 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
543 {
544   GstBaseSrcClass *bclass;
545   GstBaseSrc *bsrc;
546   gboolean res = TRUE;
547
548   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
549   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
550
551   if (bclass->set_caps)
552     res = bclass->set_caps (bsrc, caps);
553
554   return res;
555 }
556
557 static GstCaps *
558 gst_base_src_getcaps (GstPad * pad)
559 {
560   GstBaseSrcClass *bclass;
561   GstBaseSrc *bsrc;
562   GstCaps *caps = NULL;
563
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);
568
569   if (caps == NULL) {
570     GstPadTemplate *pad_template;
571
572     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));
576     }
577   }
578   return caps;
579 }
580
581 static gboolean
582 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
583 {
584   gboolean res;
585
586   switch (GST_QUERY_TYPE (query)) {
587     case GST_QUERY_POSITION:
588     {
589       GstFormat format;
590
591       gst_query_parse_position (query, &format, NULL);
592       switch (format) {
593         case GST_FORMAT_PERCENT:
594         {
595           gint64 percent;
596           gint64 position;
597           gint64 duration;
598
599           position = src->segment.last_stop;
600           duration = src->segment.duration;
601
602           if (position != -1 && duration != -1) {
603             if (position < duration)
604               percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
605                   duration);
606             else
607               percent = GST_FORMAT_PERCENT_MAX;
608           } else
609             percent = -1;
610
611           gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
612           res = TRUE;
613           break;
614         }
615         default:
616         {
617           gint64 position;
618
619           position = src->segment.last_stop;
620
621           if (position != -1) {
622             /* convert to requested format */
623             res =
624                 gst_pad_query_convert (src->srcpad, src->segment.format,
625                 position, &format, &position);
626           } else
627             res = TRUE;
628
629           gst_query_set_position (query, format, position);
630           break;
631         }
632       }
633       break;
634     }
635     case GST_QUERY_DURATION:
636     {
637       GstFormat format;
638
639       gst_query_parse_duration (query, &format, NULL);
640       switch (format) {
641         case GST_FORMAT_PERCENT:
642           gst_query_set_duration (query, GST_FORMAT_PERCENT,
643               GST_FORMAT_PERCENT_MAX);
644           res = TRUE;
645           break;
646         default:
647         {
648           gint64 duration;
649
650           duration = src->segment.duration;
651
652           if (duration != -1) {
653             /* convert to requested format */
654             res =
655                 gst_pad_query_convert (src->srcpad, src->segment.format,
656                 duration, &format, &duration);
657           } else {
658             res = TRUE;
659           }
660           gst_query_set_duration (query, format, duration);
661           break;
662         }
663       }
664       break;
665     }
666
667     case GST_QUERY_SEEKING:
668     {
669       gst_query_set_seeking (query, src->segment.format,
670           src->seekable, 0, src->segment.duration);
671       res = TRUE;
672       break;
673     }
674     case GST_QUERY_SEGMENT:
675     {
676       gint64 start, stop;
677
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;
682
683       /* adjust to stream time */
684       if (src->segment.time != -1) {
685         start -= src->segment.time;
686         if (stop != -1)
687           stop -= src->segment.time;
688       }
689       gst_query_set_segment (query, src->segment.rate, src->segment.format,
690           start, stop);
691       res = TRUE;
692       break;
693     }
694
695     case GST_QUERY_FORMATS:
696     {
697       gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
698           GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
699       res = TRUE;
700       break;
701     }
702     case GST_QUERY_CONVERT:
703     {
704       GstFormat src_fmt, dest_fmt;
705       gint64 src_val, dest_val;
706
707       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
708
709       /* we can only convert between equal formats... */
710       if (src_fmt == dest_fmt) {
711         dest_val = src_val;
712         res = TRUE;
713       } else
714         res = FALSE;
715
716       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
717       break;
718     }
719     case GST_QUERY_LATENCY:
720     case GST_QUERY_JITTER:
721     case GST_QUERY_RATE:
722     default:
723       res = FALSE;
724       break;
725   }
726   return res;
727 }
728
729 static gboolean
730 gst_base_src_query (GstPad * pad, GstQuery * query)
731 {
732   GstBaseSrc *src;
733   GstBaseSrcClass *bclass;
734   gboolean result = FALSE;
735
736   src = GST_BASE_SRC (gst_pad_get_parent (pad));
737
738   bclass = GST_BASE_SRC_GET_CLASS (src);
739
740   if (bclass->query)
741     result = bclass->query (src, query);
742   else
743     result = gst_pad_query_default (pad, query);
744
745   gst_object_unref (src);
746
747   return result;
748 }
749
750 static gboolean
751 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
752 {
753   gboolean res = TRUE;
754
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;
762     segment->time = 0;
763     res = TRUE;
764   } else
765     res = FALSE;
766
767   return res;
768 }
769
770 static gboolean
771 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
772 {
773   GstBaseSrcClass *bclass;
774   gboolean result = FALSE;
775
776   bclass = GST_BASE_SRC_GET_CLASS (src);
777
778   if (bclass->do_seek)
779     result = bclass->do_seek (src, segment);
780
781   return result;
782 }
783
784 /* this code implements the seeking. It is a good example
785  * handling all cases.
786  *
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.
791  *
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
797  * though.
798  *
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.
804  *
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 
816  * position.
817  *
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.
821  *
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.
826  */
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
829  * unlock
830  */
831 static gboolean
832 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
833 {
834   gboolean res;
835   gdouble rate;
836   GstFormat format;
837   GstSeekFlags flags;
838   GstSeekType cur_type, stop_type;
839   gint64 cur, stop;
840   gboolean flush;
841   gboolean update;
842   GstSegment seeksegment;
843
844   GST_DEBUG_OBJECT (src, "doing seek");
845
846   if (event) {
847     gst_event_parse_seek (event, &rate, &format, &flags,
848         &cur_type, &cur, &stop_type, &stop);
849
850     /* we have to have a format as the segment format. Try to convert
851      * if not. */
852     if (src->segment.format != format) {
853       GstFormat fmt;
854
855       fmt = src->segment.format;
856       res = TRUE;
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);
861       if (!res)
862         goto no_format;
863
864       format = fmt;
865     }
866   } else {
867     flags = 0;
868   }
869
870   flush = flags & GST_SEEK_FLAG_FLUSH;
871
872   /* send flush start */
873   if (flush)
874     gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
875   else
876     gst_pad_pause_task (src->srcpad);
877
878   /* unblock streaming thread */
879   if (unlock)
880     gst_base_src_unlock (src);
881
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);
886
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));
890
891   /* now configure the seek segment */
892   if (event) {
893     gst_segment_set_seek (&seeksegment, rate, format, flags,
894         cur_type, cur, stop_type, stop, &update);
895   }
896
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);
900
901   /* do the seek, segment.last_stop contains new position. */
902   res = gst_base_src_do_seek (src, &seeksegment);
903
904   /* and prepare to continue streaming */
905   if (flush) {
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);
914
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);
922   }
923
924   /* if successfull seek, we update our real segment and push
925    * out the new segment. */
926   if (res) {
927     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
928
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));
933     }
934
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;
939
940     GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
941         " to %" G_GINT64_FORMAT, src->segment.start, stop);
942
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);
951   }
952
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,
958       src->srcpad);
959
960   /* and release the lock again so we can continue streaming */
961   GST_PAD_STREAM_UNLOCK (src->srcpad);
962
963   return res;
964
965   /* ERROR */
966 no_format:
967   {
968     GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
969     return FALSE;
970   }
971 }
972
973 static const GstQueryType *
974 gst_base_src_get_query_types (GstElement * element)
975 {
976   static const GstQueryType query_types[] = {
977     GST_QUERY_DURATION,
978     GST_QUERY_POSITION,
979     GST_QUERY_SEEKING,
980     GST_QUERY_SEGMENT,
981     GST_QUERY_FORMATS,
982     GST_QUERY_LATENCY,
983     GST_QUERY_JITTER,
984     GST_QUERY_RATE,
985     GST_QUERY_CONVERT,
986     0
987   };
988
989   return query_types;
990 }
991
992 /* all events send to this element directly
993  */
994 static gboolean
995 gst_base_src_send_event (GstElement * element, GstEvent * event)
996 {
997   GstBaseSrc *src;
998   gboolean result = FALSE;
999
1000   src = GST_BASE_SRC (element);
1001
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 */
1007       break;
1008     case GST_EVENT_EOS:
1009       /* FIXME, queue EOS and make sure the task or pull function 
1010        * perform the EOS actions. */
1011       break;
1012     case GST_EVENT_NEWSEGMENT:
1013       /* sending random NEWSEGMENT downstream can break sync. */
1014       break;
1015     case GST_EVENT_TAG:
1016     case GST_EVENT_BUFFERSIZE:
1017       break;
1018     case GST_EVENT_QOS:
1019       break;
1020     case GST_EVENT_SEEK:
1021     {
1022       gboolean started;
1023
1024       GST_OBJECT_LOCK (src->srcpad);
1025       if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1026         goto wrong_mode;
1027       started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1028       GST_OBJECT_UNLOCK (src->srcpad);
1029
1030       if (started) {
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);
1034       } else {
1035         GstEvent **event_p;
1036
1037         /* else we store the event and execute the seek when we
1038          * get activated */
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 */
1044         result = TRUE;
1045       }
1046       break;
1047     }
1048     case GST_EVENT_NAVIGATION:
1049       break;
1050     default:
1051       break;
1052   }
1053 done:
1054   gst_event_unref (event);
1055
1056   return result;
1057
1058   /* ERRORS */
1059 wrong_mode:
1060   {
1061     GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1062     GST_OBJECT_UNLOCK (src->srcpad);
1063     result = FALSE;
1064     goto done;
1065   }
1066 }
1067
1068 static gboolean
1069 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1070 {
1071   gboolean result;
1072
1073   switch (GST_EVENT_TYPE (event)) {
1074     case GST_EVENT_SEEK:
1075       /* is normally called when in push mode */
1076       if (!src->seekable)
1077         goto not_seekable;
1078
1079       result = gst_base_src_perform_seek (src, event, TRUE);
1080       break;
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);
1085       break;
1086     case GST_EVENT_FLUSH_STOP:
1087     default:
1088       result = TRUE;
1089       break;
1090   }
1091   return result;
1092
1093   /* ERRORS */
1094 not_seekable:
1095   {
1096     GST_DEBUG_OBJECT (src, "is not seekable");
1097     return FALSE;
1098   }
1099 }
1100
1101 static gboolean
1102 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1103 {
1104   GstBaseSrc *src;
1105   GstBaseSrcClass *bclass;
1106   gboolean result = FALSE;
1107
1108   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1109   bclass = GST_BASE_SRC_GET_CLASS (src);
1110
1111   if (bclass->event) {
1112     if (!(result = bclass->event (src, event)))
1113       goto subclass_failed;
1114   }
1115
1116 done:
1117   gst_event_unref (event);
1118   gst_object_unref (src);
1119
1120   return result;
1121
1122   /* ERRORS */
1123 subclass_failed:
1124   {
1125     GST_DEBUG_OBJECT (src, "subclass refused event");
1126     goto done;
1127   }
1128 }
1129
1130 static void
1131 gst_base_src_set_property (GObject * object, guint prop_id,
1132     const GValue * value, GParamSpec * pspec)
1133 {
1134   GstBaseSrc *src;
1135
1136   src = GST_BASE_SRC (object);
1137
1138   switch (prop_id) {
1139     case PROP_BLOCKSIZE:
1140       src->blocksize = g_value_get_ulong (value);
1141       break;
1142     case PROP_NUM_BUFFERS:
1143       src->num_buffers = g_value_get_int (value);
1144       break;
1145     case PROP_TYPEFIND:
1146       src->data.ABI.typefind = g_value_get_boolean (value);
1147       break;
1148     default:
1149       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1150       break;
1151   }
1152 }
1153
1154 static void
1155 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1156     GParamSpec * pspec)
1157 {
1158   GstBaseSrc *src;
1159
1160   src = GST_BASE_SRC (object);
1161
1162   switch (prop_id) {
1163     case PROP_BLOCKSIZE:
1164       g_value_set_ulong (value, src->blocksize);
1165       break;
1166     case PROP_NUM_BUFFERS:
1167       g_value_set_int (value, src->num_buffers);
1168       break;
1169     case PROP_TYPEFIND:
1170       g_value_set_boolean (value, src->data.ABI.typefind);
1171       break;
1172     default:
1173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1174       break;
1175   }
1176 }
1177
1178 /* with STREAM_LOCK and LOCK */
1179 static GstClockReturn
1180 gst_base_src_wait (GstBaseSrc * basesrc, GstClockTime time)
1181 {
1182   GstClockReturn ret;
1183   GstClockID id;
1184   GstClock *clock;
1185
1186   /* get clock, if no clock, we don't sync */
1187   if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1188     return GST_CLOCK_OK;
1189
1190   id = gst_clock_new_single_shot_id (clock, time);
1191
1192   basesrc->clock_id = id;
1193   /* release the object lock while waiting */
1194   GST_OBJECT_UNLOCK (basesrc);
1195
1196   ret = gst_clock_id_wait (id, NULL);
1197
1198   GST_OBJECT_LOCK (basesrc);
1199   gst_clock_id_unref (id);
1200   basesrc->clock_id = NULL;
1201
1202   return ret;
1203 }
1204
1205 /* perform synchronisation on a buffer. 
1206  * with STREAM_LOCK.
1207  */
1208 static GstClockReturn
1209 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1210 {
1211   GstClockReturn result;
1212   GstClockTime start, end;
1213   GstBaseSrcClass *bclass;
1214   GstClockTime base_time;
1215
1216   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1217
1218   start = end = -1;
1219   if (bclass->get_times)
1220     bclass->get_times (basesrc, buffer, &start, &end);
1221
1222   /* if we don't have a timestamp, we don't sync */
1223   if (!GST_CLOCK_TIME_IS_VALID (start))
1224     goto invalid_start;
1225
1226   /* now do clocking */
1227   GST_OBJECT_LOCK (basesrc);
1228   base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1229
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));
1234
1235   result = gst_base_src_wait (basesrc, start + base_time);
1236   GST_OBJECT_UNLOCK (basesrc);
1237
1238   GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1239
1240   return result;
1241
1242   /* special cases */
1243 invalid_start:
1244   {
1245     GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1246     return GST_CLOCK_OK;
1247   }
1248 }
1249
1250 static gboolean
1251 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1252 {
1253   guint64 size, maxsize;
1254   GstBaseSrcClass *bclass;
1255
1256   bclass = GST_BASE_SRC_GET_CLASS (src);
1257
1258   /* only operate if we are working with bytes */
1259   if (src->segment.format != GST_FORMAT_BYTES)
1260     return TRUE;
1261
1262   /* get total file size */
1263   size = (guint64) src->segment.duration;
1264
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);
1269   else
1270     maxsize = size;
1271
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);
1276
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 
1280      * retry. */
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);
1285
1286       /* make sure we don't exceed the configured segment stop
1287        * if it was set */
1288       if (src->segment.stop != -1)
1289         maxsize = MIN (size, src->segment.stop);
1290       else
1291         maxsize = size;
1292
1293       /* if we are at or past the end, EOS */
1294       if (G_UNLIKELY (offset >= maxsize))
1295         goto unexpected_length;
1296
1297       /* else we can clip to the end */
1298       if (G_UNLIKELY (offset + *length >= maxsize))
1299         *length = maxsize - offset;
1300     }
1301   }
1302
1303   /* keep track of current position. segment is in bytes, we checked 
1304    * that above. */
1305   gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1306
1307   return TRUE;
1308
1309   /* ERRORS */
1310 unexpected_length:
1311   {
1312     return FALSE;
1313   }
1314 }
1315
1316 static GstFlowReturn
1317 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1318     GstBuffer ** buf)
1319 {
1320   GstFlowReturn ret;
1321   GstBaseSrcClass *bclass;
1322   GstClockReturn status;
1323
1324   bclass = GST_BASE_SRC_GET_CLASS (src);
1325
1326   ret = gst_base_src_wait_playing (src);
1327   if (ret != GST_FLOW_OK)
1328     goto stopped;
1329
1330   if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1331     goto not_started;
1332
1333   if (G_UNLIKELY (!bclass->create))
1334     goto no_function;
1335
1336   if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1337     goto unexpected_length;
1338
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;
1343     else
1344       src->num_buffers_left--;
1345   }
1346
1347   GST_DEBUG_OBJECT (src,
1348       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1349       G_GINT64_FORMAT, offset, length, src->segment.time);
1350
1351   ret = bclass->create (src, offset, length, buf);
1352   if (G_UNLIKELY (ret != GST_FLOW_OK))
1353     goto done;
1354
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;
1359
1360   /* now sync before pushing the buffer */
1361   status = gst_base_src_do_sync (src, *buf);
1362   switch (status) {
1363     case GST_CLOCK_EARLY:
1364       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1365       break;
1366     case GST_CLOCK_OK:
1367       GST_DEBUG_OBJECT (src, "buffer ok");
1368       break;
1369     default:
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);
1376       *buf = NULL;
1377       ret = GST_FLOW_WRONG_STATE;
1378       break;
1379   }
1380 done:
1381   return ret;
1382
1383   /* ERROR */
1384 stopped:
1385   {
1386     GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
1387     return ret;
1388   }
1389 not_started:
1390   {
1391     GST_DEBUG_OBJECT (src, "getrange but not started");
1392     return GST_FLOW_WRONG_STATE;
1393   }
1394 no_function:
1395   {
1396     GST_DEBUG_OBJECT (src, "no create function");
1397     return GST_FLOW_ERROR;
1398   }
1399 unexpected_length:
1400   {
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;
1404   }
1405 reached_num_buffers:
1406   {
1407     GST_DEBUG_OBJECT (src, "sent all buffers");
1408     return GST_FLOW_UNEXPECTED;
1409   }
1410 }
1411
1412 static GstFlowReturn
1413 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1414     GstBuffer ** buf)
1415 {
1416   GstBaseSrc *src;
1417   GstFlowReturn res;
1418
1419   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1420
1421   res = gst_base_src_get_range (src, offset, length, buf);
1422
1423   gst_object_unref (src);
1424
1425   return res;
1426 }
1427
1428 static gboolean
1429 gst_base_src_default_check_get_range (GstBaseSrc * src)
1430 {
1431   gboolean res;
1432
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);
1437   }
1438
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;
1443
1444   return res;
1445 }
1446
1447 static gboolean
1448 gst_base_src_check_get_range (GstBaseSrc * src)
1449 {
1450   GstBaseSrcClass *bclass;
1451   gboolean res;
1452
1453   bclass = GST_BASE_SRC_GET_CLASS (src);
1454
1455   if (bclass->check_get_range == NULL)
1456     goto no_function;
1457
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);
1461
1462   return res;
1463
1464   /* ERRORS */
1465 no_function:
1466   {
1467     GST_WARNING_OBJECT (src, "no check_get_range function set");
1468     return FALSE;
1469   }
1470 }
1471
1472 static gboolean
1473 gst_base_src_pad_check_get_range (GstPad * pad)
1474 {
1475   GstBaseSrc *src;
1476   gboolean res;
1477
1478   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1479
1480   res = gst_base_src_check_get_range (src);
1481
1482   gst_object_unref (src);
1483
1484   return res;
1485 }
1486
1487 static void
1488 gst_base_src_loop (GstPad * pad)
1489 {
1490   GstBaseSrc *src;
1491   GstBuffer *buf = NULL;
1492   GstFlowReturn ret;
1493   gint64 position;
1494   gboolean eos;
1495
1496   eos = FALSE;
1497
1498   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1499
1500   src->priv->last_sent_eos = FALSE;
1501
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;
1506   }
1507   if (src->priv->start_segment) {
1508     gst_pad_push_event (pad, src->priv->start_segment);
1509     src->priv->start_segment = NULL;
1510   }
1511
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;
1515   else
1516     position = -1;
1517
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);
1521     goto pause;
1522   }
1523   if (G_UNLIKELY (buf == NULL))
1524     goto null_buffer;
1525
1526   /* figure out the new position */
1527   switch (src->segment.format) {
1528     case GST_FORMAT_BYTES:
1529       position += GST_BUFFER_SIZE (buf);
1530       break;
1531     case GST_FORMAT_TIME:
1532     {
1533       GstClockTime start, duration;
1534
1535       start = GST_BUFFER_TIMESTAMP (buf);
1536       duration = GST_BUFFER_DURATION (buf);
1537
1538       if (GST_CLOCK_TIME_IS_VALID (start))
1539         position = start;
1540       else
1541         position = src->segment.last_stop;
1542
1543       if (GST_CLOCK_TIME_IS_VALID (duration))
1544         position += duration;
1545       break;
1546     }
1547     case GST_FORMAT_DEFAULT:
1548       position = GST_BUFFER_OFFSET_END (buf);
1549       break;
1550     default:
1551       position = -1;
1552       break;
1553   }
1554   if (position != -1) {
1555     if (src->segment.stop != -1) {
1556       if (position >= src->segment.stop) {
1557         eos = TRUE;
1558         position = src->segment.stop;
1559       }
1560     }
1561     gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1562   }
1563
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;
1568   }
1569
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);
1573     goto pause;
1574   }
1575
1576   if (eos) {
1577     GST_INFO_OBJECT (src, "pausing after EOS");
1578     ret = GST_FLOW_UNEXPECTED;
1579     goto pause;
1580   }
1581
1582 done:
1583   gst_object_unref (src);
1584   return;
1585
1586   /* special cases */
1587 pause:
1588   {
1589     const gchar *reason = gst_flow_get_name (ret);
1590
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));
1601         } else {
1602           gst_pad_push_event (pad, gst_event_new_eos ());
1603           src->priv->last_sent_eos = TRUE;
1604         }
1605       } else {
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;
1613       }
1614     }
1615     goto done;
1616   }
1617 null_buffer:
1618   {
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;
1626     goto done;
1627   }
1628 }
1629
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. */
1633 static gboolean
1634 gst_base_src_unlock (GstBaseSrc * basesrc)
1635 {
1636   GstBaseSrcClass *bclass;
1637   gboolean result = TRUE;
1638
1639   GST_DEBUG ("unlock");
1640   /* unblock whatever the subclass is doing */
1641   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1642   if (bclass->unlock)
1643     result = bclass->unlock (basesrc);
1644
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);
1650   }
1651   GST_OBJECT_UNLOCK (basesrc);
1652
1653   GST_DEBUG ("unlock done");
1654
1655   return result;
1656 }
1657
1658 /* default negotiation code. 
1659  *
1660  * Take intersection between src and sink pads, take first
1661  * caps and fixate. 
1662  */
1663 static gboolean
1664 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1665 {
1666   GstCaps *thiscaps;
1667   GstCaps *caps = NULL;
1668   GstCaps *peercaps = NULL;
1669   gboolean result = FALSE;
1670
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;
1677
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);
1681   if (peercaps) {
1682     GstCaps *icaps;
1683
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);
1689     if (icaps) {
1690       /* take first (and best, since they are sorted) possibility */
1691       caps = gst_caps_copy_nth (icaps, 0);
1692       gst_caps_unref (icaps);
1693     }
1694   } else {
1695     /* no peer, work with our own caps then */
1696     caps = thiscaps;
1697   }
1698   if (caps) {
1699     caps = gst_caps_make_writable (caps);
1700     gst_caps_truncate (caps);
1701
1702     /* now fixate */
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);
1706
1707       if (gst_caps_is_any (caps)) {
1708         /* hmm, still anything, so element can do anything and
1709          * nego is not needed */
1710         result = TRUE;
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);
1714         result = TRUE;
1715       }
1716     }
1717     gst_caps_unref (caps);
1718   }
1719   return result;
1720
1721 no_nego_needed:
1722   {
1723     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1724     if (thiscaps)
1725       gst_caps_unref (thiscaps);
1726     return TRUE;
1727   }
1728 }
1729
1730 static gboolean
1731 gst_base_src_negotiate (GstBaseSrc * basesrc)
1732 {
1733   GstBaseSrcClass *bclass;
1734   gboolean result = TRUE;
1735
1736   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1737
1738   if (bclass->negotiate)
1739     result = bclass->negotiate (basesrc);
1740
1741   return result;
1742 }
1743
1744 static gboolean
1745 gst_base_src_start (GstBaseSrc * basesrc)
1746 {
1747   GstBaseSrcClass *bclass;
1748   gboolean result;
1749   guint64 size;
1750
1751   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1752     return TRUE;
1753
1754   GST_DEBUG_OBJECT (basesrc, "starting source");
1755
1756   basesrc->num_buffers_left = basesrc->num_buffers;
1757
1758   gst_segment_init (&basesrc->segment, basesrc->segment.format);
1759   basesrc->data.ABI.running = FALSE;
1760
1761   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1762   if (bclass->start)
1763     result = bclass->start (basesrc);
1764   else
1765     result = TRUE;
1766
1767   if (!result)
1768     goto could_not_start;
1769
1770   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
1771
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)))
1776         size = -1;
1777     } else {
1778       result = FALSE;
1779       size = -1;
1780     }
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);
1784   }
1785
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);
1790
1791   /* check if we can seek */
1792   if (bclass->is_seekable)
1793     basesrc->seekable = bclass->is_seekable (basesrc);
1794   else
1795     basesrc->seekable = FALSE;
1796
1797   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
1798
1799   /* update for random access flag */
1800   basesrc->random_access = basesrc->seekable &&
1801       basesrc->segment.format == GST_FORMAT_BYTES;
1802
1803   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
1804
1805   /* run typefind if we are random_access and the typefinding is enabled. */
1806   if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
1807     GstCaps *caps;
1808
1809     caps = gst_type_find_helper (basesrc->srcpad, size);
1810     gst_pad_set_caps (basesrc->srcpad, caps);
1811     gst_caps_unref (caps);
1812   } else {
1813     /* use class or default negotiate function */
1814     if (!gst_base_src_negotiate (basesrc))
1815       goto could_not_negotiate;
1816   }
1817
1818   return TRUE;
1819
1820   /* ERROR */
1821 could_not_start:
1822   {
1823     GST_DEBUG_OBJECT (basesrc, "could not start");
1824     /* subclass is supposed to post a message. We don't have to call _stop. */
1825     return FALSE;
1826   }
1827 could_not_negotiate:
1828   {
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);
1834     return FALSE;
1835   }
1836 }
1837
1838 static gboolean
1839 gst_base_src_stop (GstBaseSrc * basesrc)
1840 {
1841   GstBaseSrcClass *bclass;
1842   gboolean result = TRUE;
1843
1844   if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1845     return TRUE;
1846
1847   GST_DEBUG_OBJECT (basesrc, "stopping source");
1848
1849   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1850   if (bclass->stop)
1851     result = bclass->stop (basesrc);
1852
1853   if (result)
1854     GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
1855
1856   return result;
1857 }
1858
1859 static gboolean
1860 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
1861 {
1862   gboolean result;
1863
1864   GST_LIVE_LOCK (basesrc);
1865   basesrc->live_running = TRUE;
1866   GST_LIVE_SIGNAL (basesrc);
1867   GST_LIVE_UNLOCK (basesrc);
1868
1869   /* step 1, unblock clock sync (if any) */
1870   result = gst_base_src_unlock (basesrc);
1871
1872   /* step 2, make sure streaming finishes */
1873   result &= gst_pad_stop_task (pad);
1874
1875   return result;
1876 }
1877
1878 static gboolean
1879 gst_base_src_activate_push (GstPad * pad, gboolean active)
1880 {
1881   GstBaseSrc *basesrc;
1882   GstEvent *event;
1883
1884   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1885
1886   /* prepare subclass first */
1887   if (active) {
1888     GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1889
1890     if (G_UNLIKELY (!basesrc->can_activate_push))
1891       goto no_push_activation;
1892
1893     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1894       goto error_start;
1895
1896     basesrc->priv->last_sent_eos = FALSE;
1897
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);
1903
1904     /* no need to unlock anything, the task is certainly
1905      * not running here. The perform seek code will start the task when
1906      * finished. */
1907     if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
1908       goto seek_failed;
1909
1910     if (event)
1911       gst_event_unref (event);
1912   } else {
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;
1917
1918     /* now we can stop the source */
1919     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
1920       goto error_stop;
1921   }
1922   return TRUE;
1923
1924   /* ERRORS */
1925 no_push_activation:
1926   {
1927     GST_ERROR_OBJECT (basesrc, "Subclass disabled push-mode activation");
1928     return FALSE;
1929   }
1930 error_start:
1931   {
1932     GST_ERROR_OBJECT (basesrc, "Failed to start in push mode");
1933     return FALSE;
1934   }
1935 seek_failed:
1936   {
1937     GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
1938     gst_base_src_stop (basesrc);
1939     if (event)
1940       gst_event_unref (event);
1941     return FALSE;
1942   }
1943 deactivate_failed:
1944   {
1945     GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
1946     return FALSE;
1947   }
1948 error_stop:
1949   {
1950     GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
1951     return FALSE;
1952   }
1953 }
1954
1955 static gboolean
1956 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1957 {
1958   GstBaseSrc *basesrc;
1959
1960   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1961
1962   /* prepare subclass first */
1963   if (active) {
1964     GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1965     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1966       goto error_start;
1967
1968     /* if not random_access, we cannot operate in pull mode for now */
1969     if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
1970       goto no_get_range;
1971   } else {
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;
1976
1977     /* don't send EOS when going from PAUSED => READY when in pull mode */
1978     basesrc->priv->last_sent_eos = TRUE;
1979
1980     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
1981       goto error_stop;
1982   }
1983   return TRUE;
1984
1985   /* ERRORS */
1986 error_start:
1987   {
1988     GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
1989     return FALSE;
1990   }
1991 no_get_range:
1992   {
1993     GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
1994     gst_base_src_stop (basesrc);
1995     return FALSE;
1996   }
1997 deactivate_failed:
1998   {
1999     GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
2000     return FALSE;
2001   }
2002 error_stop:
2003   {
2004     GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2005     return FALSE;
2006   }
2007 }
2008
2009 static GstStateChangeReturn
2010 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2011 {
2012   GstBaseSrc *basesrc;
2013   GstStateChangeReturn result;
2014   gboolean no_preroll = FALSE;
2015
2016   basesrc = GST_BASE_SRC (element);
2017
2018   switch (transition) {
2019     case GST_STATE_CHANGE_NULL_TO_READY:
2020       break;
2021     case GST_STATE_CHANGE_READY_TO_PAUSED:
2022       GST_LIVE_LOCK (element);
2023       if (basesrc->is_live) {
2024         no_preroll = TRUE;
2025         basesrc->live_running = FALSE;
2026       }
2027       basesrc->priv->last_sent_eos = FALSE;
2028       basesrc->priv->discont = TRUE;
2029       GST_LIVE_UNLOCK (element);
2030       break;
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);
2036       }
2037       GST_LIVE_UNLOCK (element);
2038       break;
2039     default:
2040       break;
2041   }
2042
2043   if ((result =
2044           GST_ELEMENT_CLASS (parent_class)->change_state (element,
2045               transition)) == GST_STATE_CHANGE_FAILURE)
2046     goto failure;
2047
2048   switch (transition) {
2049     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2050       GST_LIVE_LOCK (element);
2051       if (basesrc->is_live) {
2052         no_preroll = TRUE;
2053         basesrc->live_running = FALSE;
2054       }
2055       GST_LIVE_UNLOCK (element);
2056       break;
2057     case GST_STATE_CHANGE_PAUSED_TO_READY:
2058     {
2059       GstEvent **event_p;
2060
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;
2068       }
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);
2075       break;
2076     }
2077     case GST_STATE_CHANGE_READY_TO_NULL:
2078       break;
2079     default:
2080       break;
2081   }
2082
2083   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2084     result = GST_STATE_CHANGE_NO_PREROLL;
2085
2086   return result;
2087
2088   /* ERRORS */
2089 failure:
2090   {
2091     GST_DEBUG_OBJECT (basesrc, "parent failed state change");
2092     return result;
2093   }
2094 }