libs/gst/base/gstbasesrc.c: Allow sending EOS to the source to make it send out an...
[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 when
85  * paused discard data, such as audio or video capture devices. A typical live
86  * source also produces data at a fixed rate and thus provides a clock to publish
87  * this rate.
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 running time of the pipeline. This is one reason why a live source
99  * can only produce data in the PLAYING state, when the clock is actually 
100  * distributed and running. 
101  * </para>
102  * <para>
103  * Live sources that synchronize and block on the clock (an audio source, for
104  * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
105  * function was interrupted by a state change to PAUSED.
106  * </para>
107  * <para>
108  * The #GstBaseSrc::get_times method can be used to implement pseudo-live 
109  * sources.
110  * It only makes sense to implement the ::get_times function if the source is 
111  * a live source. The ::get_times function should return timestamps starting
112  * from 0, as if it were a non-live source. The base class will make sure that
113  * the timestamps are transformed into the current running_time.
114  * The base source will then wait for the calculated running_time before pushing
115  * out the buffer.
116  * </para>
117  * <para>
118  * For live sources, the base class will by default report a latency of 0.
119  * For pseudo live sources, the base class will by default measure the difference
120  * between the first buffer timestamp and the start time of get_times and will
121  * report this value as the latency. 
122  * Subclasses should override the query function when this behaviour is not
123  * acceptable.
124  * </para>
125  * <para>
126  * There is only support in #GstBaseSrc for exactly one source pad, which 
127  * should be named "src". A source implementation (subclass of #GstBaseSrc) 
128  * should install a pad template in its base_init function, like so:
129  * </para>
130  * <para>
131  * <programlisting>
132  * static void
133  * my_element_base_init (gpointer g_class)
134  * {
135  *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
136  *   // srctemplate should be a #GstStaticPadTemplate with direction
137  *   // #GST_PAD_SRC and name "src"
138  *   gst_element_class_add_pad_template (gstelement_class,
139  *       gst_static_pad_template_get (&amp;srctemplate));
140  *   // see #GstElementDetails
141  *   gst_element_class_set_details (gstelement_class, &amp;details);
142  * }
143  * </programlisting>
144  * </para>
145  * <title>Controlled shutdown of live sources in applications</title>
146  * <para>
147  * Applications that record from a live source may want to stop recording
148  * in a controlled way, so that the recording is stopped, but the data
149  * already in the pipeline is processed to the end (remember that many live
150  * sources would go on recording forever otherwise). For that to happen the
151  * application needs to make the source stop recording and send an EOS
152  * event down the pipeline. The application would then wait for an
153  * EOS message posted on the pipeline's bus to know when all data has
154  * been processed and the pipeline can safely be stopped.
155  * </para>
156  * <para>
157  * Since GStreamer 0.10.16 an application may send an EOS event to a source
158  * element to make it send an EOS event downstream. This can typically be done
159  * with the gst_element_send_event() function on the element or its parent bin.
160  * </para>
161  * <para>
162  * After the EOS has been sent to the element, the application should wait for
163  * an EOS message to be posted on the pipeline's bus. Once this EOS message is
164  * received, it may safely shut down the entire pipeline.
165  * </para>
166  * <para>
167  * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
168  * is still available but deprecated as it is dangerous and less flexible.
169  * </para>
170  * <para>
171  * Last reviewed on 2007-12-19 (0.10.16)
172  * </para>
173  * </refsect2>
174  */
175
176 #ifdef HAVE_CONFIG_H
177 #  include "config.h"
178 #endif
179
180 #include <stdlib.h>
181 #include <string.h>
182
183 #include "gstbasesrc.h"
184 #include "gsttypefindhelper.h"
185 #include <gst/gstmarshal.h>
186 #include <gst/gst-i18n-lib.h>
187
188 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
189 #define GST_CAT_DEFAULT gst_base_src_debug
190
191 #define GST_LIVE_GET_LOCK(elem)               (GST_BASE_SRC_CAST(elem)->live_lock)
192 #define GST_LIVE_LOCK(elem)                   g_mutex_lock(GST_LIVE_GET_LOCK(elem))
193 #define GST_LIVE_TRYLOCK(elem)                g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
194 #define GST_LIVE_UNLOCK(elem)                 g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
195 #define GST_LIVE_GET_COND(elem)               (GST_BASE_SRC_CAST(elem)->live_cond)
196 #define GST_LIVE_WAIT(elem)                   g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
197 #define GST_LIVE_TIMED_WAIT(elem, timeval)    g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
198                                                                                 timeval)
199 #define GST_LIVE_SIGNAL(elem)                 g_cond_signal (GST_LIVE_GET_COND (elem));
200 #define GST_LIVE_BROADCAST(elem)              g_cond_broadcast (GST_LIVE_GET_COND (elem));
201
202 /* BaseSrc signals and args */
203 enum
204 {
205   /* FILL ME */
206   LAST_SIGNAL
207 };
208
209 #define DEFAULT_BLOCKSIZE       4096
210 #define DEFAULT_NUM_BUFFERS     -1
211 #define DEFAULT_TYPEFIND        FALSE
212 #define DEFAULT_DO_TIMESTAMP    FALSE
213
214 enum
215 {
216   PROP_0,
217   PROP_BLOCKSIZE,
218   PROP_NUM_BUFFERS,
219   PROP_TYPEFIND,
220   PROP_DO_TIMESTAMP
221 };
222
223 #define GST_BASE_SRC_GET_PRIVATE(obj)  \
224    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
225
226 struct _GstBaseSrcPrivate
227 {
228   gboolean last_sent_eos;       /* last thing we did was send an EOS (we set this
229                                  * to avoid the sending of two EOS in some cases) */
230   gboolean discont;
231   gboolean flushing;
232
233   /* two segments to be sent in the streaming thread with STREAM_LOCK */
234   GstEvent *close_segment;
235   GstEvent *start_segment;
236
237   /* if EOS is pending */
238   gboolean pending_eos;
239
240   /* startup latency is the time it takes between going to PLAYING and producing
241    * the first BUFFER with running_time 0. This value is included in the latency
242    * reporting. */
243   GstClockTime latency;
244   /* timestamp offset, this is the offset add to the values of gst_times for
245    * pseudo live sources */
246   GstClockTimeDiff ts_offset;
247
248   gboolean do_timestamp;
249 };
250
251 static GstElementClass *parent_class = NULL;
252
253 static void gst_base_src_base_init (gpointer g_class);
254 static void gst_base_src_class_init (GstBaseSrcClass * klass);
255 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
256 static void gst_base_src_finalize (GObject * object);
257
258
259 GType
260 gst_base_src_get_type (void)
261 {
262   static GType base_src_type = 0;
263
264   if (G_UNLIKELY (base_src_type == 0)) {
265     static const GTypeInfo base_src_info = {
266       sizeof (GstBaseSrcClass),
267       (GBaseInitFunc) gst_base_src_base_init,
268       NULL,
269       (GClassInitFunc) gst_base_src_class_init,
270       NULL,
271       NULL,
272       sizeof (GstBaseSrc),
273       0,
274       (GInstanceInitFunc) gst_base_src_init,
275     };
276
277     base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
278         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
279   }
280   return base_src_type;
281 }
282 static GstCaps *gst_base_src_getcaps (GstPad * pad);
283 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
284 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
285
286 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
287 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
288 static void gst_base_src_set_property (GObject * object, guint prop_id,
289     const GValue * value, GParamSpec * pspec);
290 static void gst_base_src_get_property (GObject * object, guint prop_id,
291     GValue * value, GParamSpec * pspec);
292 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
293 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
294 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
295 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
296
297 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
298
299 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
300 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
301     GstSegment * segment);
302 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
303 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
304     GstEvent * event, GstSegment * segment);
305
306 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
307     gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
308 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
309 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
310
311 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
312     GstStateChange transition);
313
314 static void gst_base_src_loop (GstPad * pad);
315 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
316 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
317 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
318     guint length, GstBuffer ** buf);
319 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
320     guint length, GstBuffer ** buf);
321
322 static void
323 gst_base_src_base_init (gpointer g_class)
324 {
325   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
326 }
327
328 static void
329 gst_base_src_class_init (GstBaseSrcClass * klass)
330 {
331   GObjectClass *gobject_class;
332   GstElementClass *gstelement_class;
333
334   gobject_class = G_OBJECT_CLASS (klass);
335   gstelement_class = GST_ELEMENT_CLASS (klass);
336
337   g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
338
339   parent_class = g_type_class_peek_parent (klass);
340
341   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
342   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
343   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
344
345   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
346       g_param_spec_ulong ("blocksize", "Block size",
347           "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
348           DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
349   g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
350       g_param_spec_int ("num-buffers", "num-buffers",
351           "Number of buffers to output before sending EOS", -1, G_MAXINT,
352           DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
353   g_object_class_install_property (gobject_class, PROP_TYPEFIND,
354       g_param_spec_boolean ("typefind", "Typefind",
355           "Run typefind before negotiating", DEFAULT_TYPEFIND,
356           G_PARAM_READWRITE));
357   g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
358       g_param_spec_boolean ("do-timestamp", "Do timestamp",
359           "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
360           G_PARAM_READWRITE));
361
362   gstelement_class->change_state =
363       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
364   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
365   gstelement_class->get_query_types =
366       GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
367
368   klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
369   klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
370   klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
371   klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
372   klass->check_get_range =
373       GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
374   klass->prepare_seek_segment =
375       GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
376 }
377
378 static void
379 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
380 {
381   GstPad *pad;
382   GstPadTemplate *pad_template;
383
384   basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
385
386   basesrc->is_live = FALSE;
387   basesrc->live_lock = g_mutex_new ();
388   basesrc->live_cond = g_cond_new ();
389   basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
390   basesrc->num_buffers_left = -1;
391
392   basesrc->can_activate_push = TRUE;
393   basesrc->pad_mode = GST_ACTIVATE_NONE;
394
395   pad_template =
396       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
397   g_return_if_fail (pad_template != NULL);
398
399   GST_DEBUG_OBJECT (basesrc, "creating src pad");
400   pad = gst_pad_new_from_template (pad_template, "src");
401
402   GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
403   gst_pad_set_activatepush_function (pad,
404       GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
405   gst_pad_set_activatepull_function (pad,
406       GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
407   gst_pad_set_event_function (pad,
408       GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
409   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
410   gst_pad_set_checkgetrange_function (pad,
411       GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
412   gst_pad_set_getrange_function (pad,
413       GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
414   gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
415   gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
416   gst_pad_set_fixatecaps_function (pad,
417       GST_DEBUG_FUNCPTR (gst_base_src_fixate));
418
419   /* hold pointer to pad */
420   basesrc->srcpad = pad;
421   GST_DEBUG_OBJECT (basesrc, "adding src pad");
422   gst_element_add_pad (GST_ELEMENT (basesrc), pad);
423
424   basesrc->blocksize = DEFAULT_BLOCKSIZE;
425   basesrc->clock_id = NULL;
426   /* we operate in BYTES by default */
427   gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
428   basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
429   basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
430
431   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
432
433   GST_DEBUG_OBJECT (basesrc, "init done");
434 }
435
436 static void
437 gst_base_src_finalize (GObject * object)
438 {
439   GstBaseSrc *basesrc;
440   GstEvent **event_p;
441
442   basesrc = GST_BASE_SRC (object);
443
444   g_mutex_free (basesrc->live_lock);
445   g_cond_free (basesrc->live_cond);
446
447   event_p = &basesrc->data.ABI.pending_seek;
448   gst_event_replace (event_p, NULL);
449
450   G_OBJECT_CLASS (parent_class)->finalize (object);
451 }
452
453 /**
454  * gst_base_src_wait_playing:
455  * @src: the src
456  *
457  * If the #GstBaseSrcClass::create method performs its own synchronisation against
458  * the clock it must unblock when going from PLAYING to the PAUSED state and call
459  * this method before continuing to produce the remaining data.
460  *
461  * This function will block until a state change to PLAYING happens (in which
462  * case this function returns #GST_FLOW_OK) or the processing must be stopped due
463  * to a state change to READY or a FLUSH event (in which case this function
464  * returns #GST_FLOW_WRONG_STATE).
465  *
466  * Since: 0.10.12
467  *
468  * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
469  * continue. Any other return value should be returned from the create vmethod.
470  */
471 GstFlowReturn
472 gst_base_src_wait_playing (GstBaseSrc * src)
473 {
474   /* block until the state changes, or we get a flush, or something */
475   GST_DEBUG_OBJECT (src, "live source waiting for running state");
476   GST_LIVE_WAIT (src);
477   if (src->priv->flushing)
478     goto flushing;
479   GST_DEBUG_OBJECT (src, "live source unlocked");
480
481   return GST_FLOW_OK;
482
483   /* ERRORS */
484 flushing:
485   {
486     GST_DEBUG_OBJECT (src, "we are flushing");
487     return GST_FLOW_WRONG_STATE;
488   }
489 }
490
491 /**
492  * gst_base_src_set_live:
493  * @src: base source instance
494  * @live: new live-mode
495  *
496  * If the element listens to a live source, @live should
497  * be set to %TRUE. 
498  *
499  * A live source will not produce data in the PAUSED state and
500  * will therefore not be able to participate in the PREROLL phase
501  * of a pipeline. To signal this fact to the application and the 
502  * pipeline, the state change return value of the live source will
503  * be GST_STATE_CHANGE_NO_PREROLL.
504  */
505 void
506 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
507 {
508   GST_OBJECT_LOCK (src);
509   src->is_live = live;
510   GST_OBJECT_UNLOCK (src);
511 }
512
513 /**
514  * gst_base_src_is_live:
515  * @src: base source instance
516  *
517  * Check if an element is in live mode.
518  *
519  * Returns: %TRUE if element is in live mode.
520  */
521 gboolean
522 gst_base_src_is_live (GstBaseSrc * src)
523 {
524   gboolean result;
525
526   GST_OBJECT_LOCK (src);
527   result = src->is_live;
528   GST_OBJECT_UNLOCK (src);
529
530   return result;
531 }
532
533 /**
534  * gst_base_src_set_format:
535  * @src: base source instance
536  * @format: the format to use
537  *
538  * Sets the default format of the source. This will be the format used
539  * for sending NEW_SEGMENT events and for performing seeks.
540  *
541  * If a format of GST_FORMAT_BYTES is set, the element will be able to
542  * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
543  *
544  * Since: 0.10.1
545  */
546 void
547 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
548 {
549   gst_segment_init (&src->segment, format);
550 }
551
552 /**
553  * gst_base_src_query_latency:
554  * @src: the source
555  * @live: if the source is live
556  * @min_latency: the min latency of the source
557  * @max_latency: the max latency of the source
558  *
559  * Query the source for the latency parameters. @live will be TRUE when @src is
560  * configured as a live source. @min_latency will be set to the difference
561  * between the running time and the timestamp of the first buffer.
562  * @max_latency is always the undefined value of -1.
563  *
564  * This function is mostly used by subclasses. 
565  *
566  * Returns: TRUE if the query succeeded.
567  *
568  * Since: 0.10.13
569  */
570 gboolean
571 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
572     GstClockTime * min_latency, GstClockTime * max_latency)
573 {
574   GstClockTime min;
575
576   GST_OBJECT_LOCK (src);
577   if (live)
578     *live = src->is_live;
579
580   /* if we have a startup latency, report this one, else report 0. Subclasses
581    * are supposed to override the query function if they want something
582    * else. */
583   if (src->priv->latency != -1)
584     min = src->priv->latency;
585   else
586     min = 0;
587
588   if (min_latency)
589     *min_latency = min;
590   if (max_latency)
591     *max_latency = -1;
592
593   GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
594       ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
595       GST_TIME_ARGS (-1));
596   GST_OBJECT_UNLOCK (src);
597
598   return TRUE;
599 }
600
601 /**
602  * gst_base_src_set_do_timestamp:
603  * @src: the source
604  * @timestamp: enable or disable timestamping
605  *
606  * Configure @src to automatically timestamp outgoing buffers based on the
607  * current running_time of the pipeline. This property is mostly useful for live
608  * sources.
609  *
610  * Since: 0.10.15
611  */
612 void
613 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
614 {
615   GST_OBJECT_LOCK (src);
616   src->priv->do_timestamp = timestamp;
617   GST_OBJECT_UNLOCK (src);
618 }
619
620 /**
621  * gst_base_src_get_do_timestamp:
622  * @src: the source
623  *
624  * Query if @src timestamps outgoing buffers based on the current running_time.
625  *
626  * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
627  *
628  * Since: 0.10.15
629  */
630 gboolean
631 gst_base_src_get_do_timestamp (GstBaseSrc * src)
632 {
633   gboolean res;
634
635   GST_OBJECT_LOCK (src);
636   res = src->priv->do_timestamp;
637   GST_OBJECT_UNLOCK (src);
638
639   return res;
640 }
641
642 static gboolean
643 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
644 {
645   GstBaseSrcClass *bclass;
646   GstBaseSrc *bsrc;
647   gboolean res = TRUE;
648
649   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
650   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
651
652   if (bclass->set_caps)
653     res = bclass->set_caps (bsrc, caps);
654
655   return res;
656 }
657
658 static GstCaps *
659 gst_base_src_getcaps (GstPad * pad)
660 {
661   GstBaseSrcClass *bclass;
662   GstBaseSrc *bsrc;
663   GstCaps *caps = NULL;
664
665   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
666   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
667   if (bclass->get_caps)
668     caps = bclass->get_caps (bsrc);
669
670   if (caps == NULL) {
671     GstPadTemplate *pad_template;
672
673     pad_template =
674         gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
675     if (pad_template != NULL) {
676       caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
677     }
678   }
679   return caps;
680 }
681
682 static void
683 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
684 {
685   GstBaseSrcClass *bclass;
686   GstBaseSrc *bsrc;
687
688   bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
689   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
690
691   if (bclass->fixate)
692     bclass->fixate (bsrc, caps);
693
694   gst_object_unref (bsrc);
695 }
696
697 static gboolean
698 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
699 {
700   gboolean res;
701
702   switch (GST_QUERY_TYPE (query)) {
703     case GST_QUERY_POSITION:
704     {
705       GstFormat format;
706
707       gst_query_parse_position (query, &format, NULL);
708       switch (format) {
709         case GST_FORMAT_PERCENT:
710         {
711           gint64 percent;
712           gint64 position;
713           gint64 duration;
714
715           position = src->segment.last_stop;
716           duration = src->segment.duration;
717
718           if (position != -1 && duration != -1) {
719             if (position < duration)
720               percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
721                   duration);
722             else
723               percent = GST_FORMAT_PERCENT_MAX;
724           } else
725             percent = -1;
726
727           gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
728           res = TRUE;
729           break;
730         }
731         default:
732         {
733           gint64 position;
734
735           position = src->segment.last_stop;
736
737           if (position != -1) {
738             /* convert to requested format */
739             res =
740                 gst_pad_query_convert (src->srcpad, src->segment.format,
741                 position, &format, &position);
742           } else
743             res = TRUE;
744
745           gst_query_set_position (query, format, position);
746           break;
747         }
748       }
749       break;
750     }
751     case GST_QUERY_DURATION:
752     {
753       GstFormat format;
754
755       gst_query_parse_duration (query, &format, NULL);
756
757       GST_DEBUG_OBJECT (src, "duration query in format %s",
758           gst_format_get_name (format));
759       switch (format) {
760         case GST_FORMAT_PERCENT:
761           gst_query_set_duration (query, GST_FORMAT_PERCENT,
762               GST_FORMAT_PERCENT_MAX);
763           res = TRUE;
764           break;
765         default:
766         {
767           gint64 duration;
768
769           /* this is the duration as configured by the subclass. */
770           duration = src->segment.duration;
771
772           if (duration != -1) {
773             /* convert to requested format, if this fails, we have a duration
774              * but we cannot answer the query, we must return FALSE. */
775             res =
776                 gst_pad_query_convert (src->srcpad, src->segment.format,
777                 duration, &format, &duration);
778           } else {
779             /* The subclass did not configure a duration, we assume that the
780              * media has an unknown duration then and we return TRUE to report
781              * this. Note that this is not the same as returning FALSE, which
782              * means that we cannot report the duration at all. */
783             res = TRUE;
784           }
785           gst_query_set_duration (query, format, duration);
786           break;
787         }
788       }
789       break;
790     }
791
792     case GST_QUERY_SEEKING:
793     {
794       gst_query_set_seeking (query, src->segment.format,
795           src->seekable, 0, src->segment.duration);
796       res = TRUE;
797       break;
798     }
799     case GST_QUERY_SEGMENT:
800     {
801       gint64 start, stop;
802
803       /* no end segment configured, current duration then */
804       if ((stop = src->segment.stop) == -1)
805         stop = src->segment.duration;
806       start = src->segment.start;
807
808       /* adjust to stream time */
809       if (src->segment.time != -1) {
810         start -= src->segment.time;
811         if (stop != -1)
812           stop -= src->segment.time;
813       }
814       gst_query_set_segment (query, src->segment.rate, src->segment.format,
815           start, stop);
816       res = TRUE;
817       break;
818     }
819
820     case GST_QUERY_FORMATS:
821     {
822       gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
823           GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
824       res = TRUE;
825       break;
826     }
827     case GST_QUERY_CONVERT:
828     {
829       GstFormat src_fmt, dest_fmt;
830       gint64 src_val, dest_val;
831
832       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
833
834       /* we can only convert between equal formats... */
835       if (src_fmt == dest_fmt) {
836         dest_val = src_val;
837         res = TRUE;
838       } else
839         res = FALSE;
840
841       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
842       break;
843     }
844     case GST_QUERY_LATENCY:
845     {
846       GstClockTime min, max;
847       gboolean live;
848
849       /* Subclasses should override and implement something usefull */
850       res = gst_base_src_query_latency (src, &live, &min, &max);
851
852       GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
853           ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
854           GST_TIME_ARGS (max));
855
856       gst_query_set_latency (query, live, min, max);
857       break;
858     }
859     case GST_QUERY_JITTER:
860     case GST_QUERY_RATE:
861     default:
862       res = FALSE;
863       break;
864   }
865   GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
866       res);
867   return res;
868 }
869
870 static gboolean
871 gst_base_src_query (GstPad * pad, GstQuery * query)
872 {
873   GstBaseSrc *src;
874   GstBaseSrcClass *bclass;
875   gboolean result = FALSE;
876
877   src = GST_BASE_SRC (gst_pad_get_parent (pad));
878
879   bclass = GST_BASE_SRC_GET_CLASS (src);
880
881   if (bclass->query)
882     result = bclass->query (src, query);
883   else
884     result = gst_pad_query_default (pad, query);
885
886   gst_object_unref (src);
887
888   return result;
889 }
890
891 static gboolean
892 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
893 {
894   gboolean res = TRUE;
895
896   /* update our offset if the start/stop position was updated */
897   if (segment->format == GST_FORMAT_BYTES) {
898     segment->last_stop = segment->start;
899     segment->time = segment->start;
900   } else if (segment->start == 0) {
901     /* seek to start, we can implement a default for this. */
902     segment->last_stop = 0;
903     segment->time = 0;
904     res = TRUE;
905   } else
906     res = FALSE;
907
908   return res;
909 }
910
911 static gboolean
912 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
913 {
914   GstBaseSrcClass *bclass;
915   gboolean result = FALSE;
916
917   bclass = GST_BASE_SRC_GET_CLASS (src);
918
919   if (bclass->do_seek)
920     result = bclass->do_seek (src, segment);
921
922   return result;
923 }
924
925 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
926
927 static gboolean
928 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
929     GstSegment * segment)
930 {
931   /* By default, we try one of 2 things:
932    *   - For absolute seek positions, convert the requested position to our 
933    *     configured processing format and place it in the output segment \
934    *   - For relative seek positions, convert our current (input) values to the
935    *     seek format, adjust by the relative seek offset and then convert back to
936    *     the processing format
937    */
938   GstSeekType cur_type, stop_type;
939   gint64 cur, stop;
940   GstSeekFlags flags;
941   GstFormat seek_format, dest_format;
942   gdouble rate;
943   gboolean update;
944   gboolean res = TRUE;
945
946   gst_event_parse_seek (event, &rate, &seek_format, &flags,
947       &cur_type, &cur, &stop_type, &stop);
948   dest_format = segment->format;
949
950   if (seek_format == dest_format) {
951     gst_segment_set_seek (segment, rate, seek_format, flags,
952         cur_type, cur, stop_type, stop, &update);
953     return TRUE;
954   }
955
956   if (cur_type != GST_SEEK_TYPE_NONE) {
957     /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
958     res =
959         gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
960         &cur);
961     cur_type = GST_SEEK_TYPE_SET;
962   }
963
964   if (res && stop_type != GST_SEEK_TYPE_NONE) {
965     /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
966     res =
967         gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
968         &stop);
969     stop_type = GST_SEEK_TYPE_SET;
970   }
971
972   /* And finally, configure our output segment in the desired format */
973   gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
974       stop_type, stop, &update);
975
976   if (!res)
977     goto no_format;
978
979   return res;
980
981 no_format:
982   {
983     GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
984     return FALSE;
985   }
986 }
987
988 static gboolean
989 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
990     GstSegment * seeksegment)
991 {
992   GstBaseSrcClass *bclass;
993   gboolean result = FALSE;
994
995   bclass = GST_BASE_SRC_GET_CLASS (src);
996
997   if (bclass->prepare_seek_segment)
998     result = bclass->prepare_seek_segment (src, event, seeksegment);
999
1000   return result;
1001 }
1002
1003 /* this code implements the seeking. It is a good example
1004  * handling all cases.
1005  *
1006  * A seek updates the currently configured segment.start
1007  * and segment.stop values based on the SEEK_TYPE. If the
1008  * segment.start value is updated, a seek to this new position
1009  * should be performed.
1010  *
1011  * The seek can only be executed when we are not currently
1012  * streaming any data, to make sure that this is the case, we
1013  * acquire the STREAM_LOCK which is taken when we are in the
1014  * _loop() function or when a getrange() is called. Normally
1015  * we will not receive a seek if we are operating in pull mode
1016  * though. When we operate as a live source we might block on the live
1017  * cond, which does not release the STREAM_LOCK. Therefore we will try
1018  * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1019  * safe to perform the seek.
1020  *
1021  * When we are in the loop() function, we might be in the middle
1022  * of pushing a buffer, which might block in a sink. To make sure
1023  * that the push gets unblocked we push out a FLUSH_START event.
1024  * Our loop function will get a WRONG_STATE return value from
1025  * the push and will pause, effectively releasing the STREAM_LOCK.
1026  *
1027  * For a non-flushing seek, we pause the task, which might eventually
1028  * release the STREAM_LOCK. We say eventually because when the sink
1029  * blocks on the sample we might wait a very long time until the sink
1030  * unblocks the sample. In any case we acquire the STREAM_LOCK and
1031  * can continue the seek. A non-flushing seek is normally done in a 
1032  * running pipeline to perform seamless playback, this means that the sink is
1033  * PLAYING and will return from its chain function.
1034  * In the case of a non-flushing seek we need to make sure that the
1035  * data we output after the seek is continuous with the previous data,
1036  * this is because a non-flushing seek does not reset the running-time
1037  * to 0. We do this by closing the currently running segment, ie. sending
1038  * a new_segment event with the stop position set to the last processed 
1039  * position.
1040  *
1041  * After updating the segment.start/stop values, we prepare for
1042  * streaming again. We push out a FLUSH_STOP to make the peer pad
1043  * accept data again and we start our task again.
1044  *
1045  * A segment seek posts a message on the bus saying that the playback
1046  * of the segment started. We store the segment flag internally because
1047  * when we reach the segment.stop we have to post a segment.done
1048  * instead of EOS when doing a segment seek.
1049  */
1050 /* FIXME (0.11), we have the unlock gboolean here because most current 
1051  * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
1052  * the streaming thread isn't running, resulting in bogus unlocks later when it 
1053  * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
1054  * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
1055  * until 0.11
1056  */
1057 static gboolean
1058 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1059 {
1060   gboolean res = TRUE;
1061   gdouble rate;
1062   GstFormat seek_format, dest_format;
1063   GstSeekFlags flags;
1064   GstSeekType cur_type, stop_type;
1065   gint64 cur, stop;
1066   gboolean flush, playing;
1067   gboolean update;
1068   gboolean relative_seek = FALSE;
1069   gboolean seekseg_configured = FALSE;
1070   GstSegment seeksegment;
1071
1072   GST_DEBUG_OBJECT (src, "doing seek");
1073
1074   dest_format = src->segment.format;
1075
1076   if (event) {
1077     gst_event_parse_seek (event, &rate, &seek_format, &flags,
1078         &cur_type, &cur, &stop_type, &stop);
1079
1080     relative_seek = SEEK_TYPE_IS_RELATIVE (cur_type) ||
1081         SEEK_TYPE_IS_RELATIVE (stop_type);
1082
1083     if (dest_format != seek_format && !relative_seek) {
1084       /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1085        * here before taking the stream lock, otherwise we must convert it later,
1086        * once we have the stream lock and can read the current position */
1087       gst_segment_init (&seeksegment, dest_format);
1088
1089       if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1090         goto prepare_failed;
1091
1092       seekseg_configured = TRUE;
1093     }
1094
1095     flush = flags & GST_SEEK_FLAG_FLUSH;
1096   } else {
1097     flush = FALSE;
1098   }
1099
1100   /* send flush start */
1101   if (flush)
1102     gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
1103   else
1104     gst_pad_pause_task (src->srcpad);
1105
1106   /* unblock streaming thread. */
1107   gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
1108
1109   /* grab streaming lock, this should eventually be possible, either
1110    * because the task is paused, our streaming thread stopped 
1111    * or because our peer is flushing. */
1112   GST_PAD_STREAM_LOCK (src->srcpad);
1113
1114   gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
1115
1116   /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1117    * copy the current segment info into the temp segment that we can actually
1118    * attempt the seek with. We only update the real segment if the seek suceeds. */
1119   if (!seekseg_configured) {
1120     memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1121
1122     /* now configure the final seek segment */
1123     if (event) {
1124       if (src->segment.format != seek_format) {
1125         /* OK, here's where we give the subclass a chance to convert the relative
1126          * seek into an absolute one in the processing format. We set up any
1127          * absolute seek above, before taking the stream lock. */
1128         if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1129           GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1130               "Aborting seek");
1131           res = FALSE;
1132         }
1133       } else {
1134         /* The seek format matches our processing format, no need to ask the
1135          * the subclass to configure the segment. */
1136         gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
1137             cur_type, cur, stop_type, stop, &update);
1138       }
1139     }
1140     /* Else, no seek event passed, so we're just (re)starting the 
1141        current segment. */
1142   }
1143
1144   if (res) {
1145     GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1146         " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1147         seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1148
1149     /* do the seek, segment.last_stop contains the new position. */
1150     res = gst_base_src_do_seek (src, &seeksegment);
1151   }
1152
1153   /* and prepare to continue streaming */
1154   if (flush) {
1155     /* send flush stop, peer will accept data and events again. We
1156      * are not yet providing data as we still have the STREAM_LOCK. */
1157     gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
1158   } else if (res && src->data.ABI.running) {
1159     /* we are running the current segment and doing a non-flushing seek, 
1160      * close the segment first based on the last_stop. */
1161     GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1162         " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
1163
1164     /* queue the segment for sending in the stream thread */
1165     if (src->priv->close_segment)
1166       gst_event_unref (src->priv->close_segment);
1167     src->priv->close_segment =
1168         gst_event_new_new_segment_full (TRUE,
1169         src->segment.rate, src->segment.applied_rate, src->segment.format,
1170         src->segment.start, src->segment.last_stop, src->segment.time);
1171   }
1172
1173   /* The subclass must have converted the segment to the processing format 
1174    * by now */
1175   if (res && seeksegment.format != dest_format) {
1176     GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1177         "in the correct format. Aborting seek.");
1178     res = FALSE;
1179   }
1180
1181   /* if successfull seek, we update our real segment and push
1182    * out the new segment. */
1183   if (res) {
1184     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1185
1186     if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1187       gst_element_post_message (GST_ELEMENT (src),
1188           gst_message_new_segment_start (GST_OBJECT (src),
1189               src->segment.format, src->segment.last_stop));
1190     }
1191
1192     /* for deriving a stop position for the playback segment from the seek
1193      * segment, we must take the duration when the stop is not set */
1194     if ((stop = src->segment.stop) == -1)
1195       stop = src->segment.duration;
1196
1197     GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
1198         " to %" G_GINT64_FORMAT, src->segment.start, stop);
1199
1200     /* now replace the old segment so that we send it in the stream thread the
1201      * next time it is scheduled. */
1202     if (src->priv->start_segment)
1203       gst_event_unref (src->priv->start_segment);
1204     src->priv->start_segment =
1205         gst_event_new_new_segment_full (FALSE,
1206         src->segment.rate, src->segment.applied_rate, src->segment.format,
1207         src->segment.last_stop, stop, src->segment.time);
1208   }
1209
1210   src->priv->discont = TRUE;
1211   src->data.ABI.running = TRUE;
1212   /* and restart the task in case it got paused explicitely or by
1213    * the FLUSH_START event we pushed out. */
1214   gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1215       src->srcpad);
1216
1217   /* and release the lock again so we can continue streaming */
1218   GST_PAD_STREAM_UNLOCK (src->srcpad);
1219
1220   return res;
1221
1222   /* ERROR */
1223 prepare_failed:
1224   GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1225       "Aborting seek");
1226   return FALSE;
1227 }
1228
1229 static const GstQueryType *
1230 gst_base_src_get_query_types (GstElement * element)
1231 {
1232   static const GstQueryType query_types[] = {
1233     GST_QUERY_DURATION,
1234     GST_QUERY_POSITION,
1235     GST_QUERY_SEEKING,
1236     GST_QUERY_SEGMENT,
1237     GST_QUERY_FORMATS,
1238     GST_QUERY_LATENCY,
1239     GST_QUERY_JITTER,
1240     GST_QUERY_RATE,
1241     GST_QUERY_CONVERT,
1242     0
1243   };
1244
1245   return query_types;
1246 }
1247
1248 /* all events send to this element directly. This is mainly done from the
1249  * application.
1250  */
1251 static gboolean
1252 gst_base_src_send_event (GstElement * element, GstEvent * event)
1253 {
1254   GstBaseSrc *src;
1255   gboolean result = FALSE;
1256
1257   src = GST_BASE_SRC (element);
1258
1259   switch (GST_EVENT_TYPE (event)) {
1260       /* bidirectional events */
1261     case GST_EVENT_FLUSH_START:
1262     case GST_EVENT_FLUSH_STOP:
1263       /* sending random flushes downstream can break stuff,
1264        * especially sync since all segment info will get flushed */
1265       break;
1266
1267       /* downstream serialized events */
1268     case GST_EVENT_EOS:
1269       /* queue EOS and make sure the task or pull function 
1270        * performs the EOS actions. */
1271       GST_LIVE_LOCK (src);
1272       src->priv->pending_eos = TRUE;
1273       GST_LIVE_UNLOCK (src);
1274       result = TRUE;
1275       break;
1276     case GST_EVENT_NEWSEGMENT:
1277       /* sending random NEWSEGMENT downstream can break sync. */
1278       break;
1279     case GST_EVENT_TAG:
1280       /* sending tags could be useful, FIXME insert in dataflow */
1281       break;
1282     case GST_EVENT_BUFFERSIZE:
1283       /* does not seem to make much sense currently */
1284       break;
1285
1286       /* upstream events */
1287     case GST_EVENT_QOS:
1288       /* elements should override send_event and do something */
1289       break;
1290     case GST_EVENT_SEEK:
1291     {
1292       gboolean started;
1293
1294       GST_OBJECT_LOCK (src->srcpad);
1295       if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1296         goto wrong_mode;
1297       started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1298       GST_OBJECT_UNLOCK (src->srcpad);
1299
1300       if (started) {
1301         /* when we are running in push mode, we can execute the
1302          * seek right now, we need to unlock. */
1303         result = gst_base_src_perform_seek (src, event, TRUE);
1304       } else {
1305         GstEvent **event_p;
1306
1307         /* else we store the event and execute the seek when we
1308          * get activated */
1309         GST_OBJECT_LOCK (src);
1310         event_p = &src->data.ABI.pending_seek;
1311         gst_event_replace ((GstEvent **) event_p, event);
1312         GST_OBJECT_UNLOCK (src);
1313         /* assume the seek will work */
1314         result = TRUE;
1315       }
1316       break;
1317     }
1318     case GST_EVENT_NAVIGATION:
1319       /* could make sense for elements that do something with navigation events
1320        * but then they would need to override the send_event function */
1321       break;
1322     case GST_EVENT_LATENCY:
1323       /* does not seem to make sense currently */
1324       break;
1325
1326       /* custom events */
1327     case GST_EVENT_CUSTOM_UPSTREAM:
1328       /* override send_event if you want this */
1329       break;
1330     case GST_EVENT_CUSTOM_DOWNSTREAM:
1331     case GST_EVENT_CUSTOM_BOTH:
1332       /* FIXME, insert event in the dataflow */
1333       break;
1334     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1335     case GST_EVENT_CUSTOM_BOTH_OOB:
1336       /* insert a random custom event into the pipeline */
1337       GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1338       result = gst_pad_push_event (src->srcpad, event);
1339       /* we gave away the ref to the event in the push */
1340       event = NULL;
1341       break;
1342     default:
1343       break;
1344   }
1345 done:
1346   /* if we still have a ref to the event, unref it now */
1347   if (event)
1348     gst_event_unref (event);
1349
1350   return result;
1351
1352   /* ERRORS */
1353 wrong_mode:
1354   {
1355     GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1356     GST_OBJECT_UNLOCK (src->srcpad);
1357     result = FALSE;
1358     goto done;
1359   }
1360 }
1361
1362 static gboolean
1363 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1364 {
1365   gboolean result;
1366
1367   switch (GST_EVENT_TYPE (event)) {
1368     case GST_EVENT_SEEK:
1369       /* is normally called when in push mode */
1370       if (!src->seekable)
1371         goto not_seekable;
1372
1373       result = gst_base_src_perform_seek (src, event, TRUE);
1374       break;
1375     case GST_EVENT_FLUSH_START:
1376       /* cancel any blocking getrange, is normally called
1377        * when in pull mode. */
1378       result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
1379       break;
1380     case GST_EVENT_FLUSH_STOP:
1381       result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
1382       break;
1383     default:
1384       result = TRUE;
1385       break;
1386   }
1387   return result;
1388
1389   /* ERRORS */
1390 not_seekable:
1391   {
1392     GST_DEBUG_OBJECT (src, "is not seekable");
1393     return FALSE;
1394   }
1395 }
1396
1397 static gboolean
1398 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1399 {
1400   GstBaseSrc *src;
1401   GstBaseSrcClass *bclass;
1402   gboolean result = FALSE;
1403
1404   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1405   bclass = GST_BASE_SRC_GET_CLASS (src);
1406
1407   if (bclass->event) {
1408     if (!(result = bclass->event (src, event)))
1409       goto subclass_failed;
1410   }
1411
1412 done:
1413   gst_event_unref (event);
1414   gst_object_unref (src);
1415
1416   return result;
1417
1418   /* ERRORS */
1419 subclass_failed:
1420   {
1421     GST_DEBUG_OBJECT (src, "subclass refused event");
1422     goto done;
1423   }
1424 }
1425
1426 static void
1427 gst_base_src_set_property (GObject * object, guint prop_id,
1428     const GValue * value, GParamSpec * pspec)
1429 {
1430   GstBaseSrc *src;
1431
1432   src = GST_BASE_SRC (object);
1433
1434   switch (prop_id) {
1435     case PROP_BLOCKSIZE:
1436       src->blocksize = g_value_get_ulong (value);
1437       break;
1438     case PROP_NUM_BUFFERS:
1439       src->num_buffers = g_value_get_int (value);
1440       break;
1441     case PROP_TYPEFIND:
1442       src->data.ABI.typefind = g_value_get_boolean (value);
1443       break;
1444     case PROP_DO_TIMESTAMP:
1445       src->priv->do_timestamp = g_value_get_boolean (value);
1446       break;
1447     default:
1448       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1449       break;
1450   }
1451 }
1452
1453 static void
1454 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1455     GParamSpec * pspec)
1456 {
1457   GstBaseSrc *src;
1458
1459   src = GST_BASE_SRC (object);
1460
1461   switch (prop_id) {
1462     case PROP_BLOCKSIZE:
1463       g_value_set_ulong (value, src->blocksize);
1464       break;
1465     case PROP_NUM_BUFFERS:
1466       g_value_set_int (value, src->num_buffers);
1467       break;
1468     case PROP_TYPEFIND:
1469       g_value_set_boolean (value, src->data.ABI.typefind);
1470       break;
1471     case PROP_DO_TIMESTAMP:
1472       g_value_set_boolean (value, src->priv->do_timestamp);
1473       break;
1474     default:
1475       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1476       break;
1477   }
1478 }
1479
1480 /* with STREAM_LOCK and LOCK */
1481 static GstClockReturn
1482 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
1483 {
1484   GstClockReturn ret;
1485   GstClockID id;
1486
1487   id = gst_clock_new_single_shot_id (clock, time);
1488
1489   basesrc->clock_id = id;
1490   /* release the live lock while waiting */
1491   GST_LIVE_UNLOCK (basesrc);
1492
1493   ret = gst_clock_id_wait (id, NULL);
1494
1495   GST_LIVE_LOCK (basesrc);
1496   gst_clock_id_unref (id);
1497   basesrc->clock_id = NULL;
1498
1499   return ret;
1500 }
1501
1502 /* perform synchronisation on a buffer. 
1503  * with STREAM_LOCK.
1504  */
1505 static GstClockReturn
1506 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1507 {
1508   GstClockReturn result;
1509   GstClockTime start, end;
1510   GstBaseSrcClass *bclass;
1511   GstClockTime base_time;
1512   GstClock *clock;
1513   GstClockTime now = GST_CLOCK_TIME_NONE, timestamp;
1514   gboolean do_timestamp, first, pseudo_live;
1515
1516   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1517
1518   start = end = -1;
1519   if (bclass->get_times)
1520     bclass->get_times (basesrc, buffer, &start, &end);
1521
1522   /* get buffer timestamp */
1523   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1524
1525   /* grab the lock to prepare for clocking and calculate the startup 
1526    * latency. */
1527   GST_OBJECT_LOCK (basesrc);
1528
1529   /* if we are asked to sync against the clock we are a pseudo live element */
1530   pseudo_live = (start != -1 && basesrc->is_live);
1531   /* check for the first buffer */
1532   first = (basesrc->priv->latency == -1);
1533
1534   if (timestamp != -1 && pseudo_live) {
1535     GstClockTime latency;
1536
1537     /* we have a timestamp and a sync time, latency is the diff */
1538     if (timestamp <= start)
1539       latency = start - timestamp;
1540     else
1541       latency = 0;
1542
1543     if (first) {
1544       GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
1545           GST_TIME_ARGS (latency));
1546       /* first time we calculate latency, just configure */
1547       basesrc->priv->latency = latency;
1548     } else {
1549       if (basesrc->priv->latency != latency) {
1550         /* we have a new latency, FIXME post latency message */
1551         basesrc->priv->latency = latency;
1552         GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
1553             GST_TIME_ARGS (latency));
1554       }
1555     }
1556   } else if (first) {
1557     GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
1558         basesrc->is_live, start != -1);
1559     basesrc->priv->latency = 0;
1560   }
1561
1562   /* get clock, if no clock, we can't sync or do timestamps */
1563   if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1564     goto no_clock;
1565
1566   base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1567
1568   do_timestamp = basesrc->priv->do_timestamp;
1569
1570   /* first buffer, calculate the timestamp offset */
1571   if (first) {
1572     GstClockTime running_time;
1573
1574     now = gst_clock_get_time (clock);
1575     running_time = now - base_time;
1576
1577     GST_LOG_OBJECT (basesrc,
1578         "startup timestamp: %" GST_TIME_FORMAT ", running_time %"
1579         GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
1580         GST_TIME_ARGS (running_time));
1581
1582     if (pseudo_live && timestamp != -1) {
1583       /* live source and we need to sync, add startup latency to all timestamps
1584        * to get the real running_time. Live sources should always timestamp
1585        * according to the current running time. */
1586       basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
1587
1588       GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
1589           GST_TIME_ARGS (basesrc->priv->ts_offset));
1590     } else {
1591       basesrc->priv->ts_offset = 0;
1592       GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
1593     }
1594
1595     if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1596       if (do_timestamp)
1597         timestamp = running_time;
1598       else
1599         timestamp = 0;
1600
1601       GST_BUFFER_TIMESTAMP (buffer) = timestamp;
1602
1603       GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1604           GST_TIME_ARGS (timestamp));
1605     }
1606
1607     /* add the timestamp offset we need for sync */
1608     timestamp += basesrc->priv->ts_offset;
1609   } else {
1610     /* not the first buffer, the timestamp is the diff between the clock and
1611      * base_time */
1612     if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (timestamp)) {
1613       now = gst_clock_get_time (clock);
1614
1615       GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
1616
1617       GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
1618           GST_TIME_ARGS (now - base_time));
1619     }
1620   }
1621
1622   /* if we don't have a buffer timestamp, we don't sync */
1623   if (!GST_CLOCK_TIME_IS_VALID (start))
1624     goto no_sync;
1625
1626   if (basesrc->is_live && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1627     /* for pseudo live sources, add our ts_offset to the timestamp */
1628     GST_BUFFER_TIMESTAMP (buffer) += basesrc->priv->ts_offset;
1629     start += basesrc->priv->ts_offset;
1630   }
1631
1632   GST_LOG_OBJECT (basesrc,
1633       "waiting for clock, base time %" GST_TIME_FORMAT
1634       ", stream_start %" GST_TIME_FORMAT,
1635       GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1636   GST_OBJECT_UNLOCK (basesrc);
1637
1638   result = gst_base_src_wait (basesrc, clock, start + base_time);
1639
1640   GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1641
1642   return result;
1643
1644   /* special cases */
1645 no_clock:
1646   {
1647     GST_DEBUG_OBJECT (basesrc, "we have no clock");
1648     GST_OBJECT_UNLOCK (basesrc);
1649     return GST_CLOCK_OK;
1650   }
1651 no_sync:
1652   {
1653     GST_DEBUG_OBJECT (basesrc, "no sync needed");
1654     GST_OBJECT_UNLOCK (basesrc);
1655     return GST_CLOCK_OK;
1656   }
1657 }
1658
1659 static gboolean
1660 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1661 {
1662   guint64 size, maxsize;
1663   GstBaseSrcClass *bclass;
1664
1665   bclass = GST_BASE_SRC_GET_CLASS (src);
1666
1667   /* only operate if we are working with bytes */
1668   if (src->segment.format != GST_FORMAT_BYTES)
1669     return TRUE;
1670
1671   /* get total file size */
1672   size = (guint64) src->segment.duration;
1673
1674   /* the max amount of bytes to read is the total size or
1675    * up to the segment.stop if present. */
1676   if (src->segment.stop != -1)
1677     maxsize = MIN (size, src->segment.stop);
1678   else
1679     maxsize = size;
1680
1681   GST_DEBUG_OBJECT (src,
1682       "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1683       ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1684       *length, size, src->segment.stop, maxsize);
1685
1686   /* check size if we have one */
1687   if (maxsize != -1) {
1688     /* if we run past the end, check if the file became bigger and 
1689      * retry. */
1690     if (G_UNLIKELY (offset + *length >= maxsize)) {
1691       /* see if length of the file changed */
1692       if (bclass->get_size)
1693         if (!bclass->get_size (src, &size))
1694           size = -1;
1695
1696       gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1697
1698       /* make sure we don't exceed the configured segment stop
1699        * if it was set */
1700       if (src->segment.stop != -1)
1701         maxsize = MIN (size, src->segment.stop);
1702       else
1703         maxsize = size;
1704
1705       /* if we are at or past the end, EOS */
1706       if (G_UNLIKELY (offset >= maxsize))
1707         goto unexpected_length;
1708
1709       /* else we can clip to the end */
1710       if (G_UNLIKELY (offset + *length >= maxsize))
1711         *length = maxsize - offset;
1712
1713     }
1714   }
1715
1716   /* keep track of current position. segment is in bytes, we checked 
1717    * that above. */
1718   gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1719
1720   return TRUE;
1721
1722   /* ERRORS */
1723 unexpected_length:
1724   {
1725     return FALSE;
1726   }
1727 }
1728
1729 /* must be called with LIVE_LOCK */
1730 static GstFlowReturn
1731 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1732     GstBuffer ** buf)
1733 {
1734   GstFlowReturn ret;
1735   GstBaseSrcClass *bclass;
1736   GstClockReturn status;
1737
1738   bclass = GST_BASE_SRC_GET_CLASS (src);
1739
1740   if (src->is_live) {
1741     while (G_UNLIKELY (!src->live_running)) {
1742       ret = gst_base_src_wait_playing (src);
1743       if (ret != GST_FLOW_OK)
1744         goto stopped;
1745     }
1746   }
1747
1748   if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1749     goto not_started;
1750
1751   if (G_UNLIKELY (!bclass->create))
1752     goto no_function;
1753
1754   if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1755     goto unexpected_length;
1756
1757   /* normally we don't count buffers */
1758   if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1759     if (src->num_buffers_left == 0)
1760       goto reached_num_buffers;
1761     else
1762       src->num_buffers_left--;
1763   }
1764
1765   GST_DEBUG_OBJECT (src,
1766       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1767       G_GINT64_FORMAT, offset, length, src->segment.time);
1768
1769   ret = bclass->create (src, offset, length, buf);
1770   if (G_UNLIKELY (ret != GST_FLOW_OK))
1771     goto not_ok;
1772
1773   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1774   if (offset == 0 && src->segment.time == 0
1775       && GST_BUFFER_TIMESTAMP (*buf) == -1)
1776     GST_BUFFER_TIMESTAMP (*buf) = 0;
1777
1778   /* now sync before pushing the buffer */
1779   status = gst_base_src_do_sync (src, *buf);
1780
1781   /* waiting for the clock could have made us flushing */
1782   if (G_UNLIKELY (src->priv->flushing))
1783     goto flushing;
1784
1785   if (G_UNLIKELY (src->priv->pending_eos))
1786     goto eos;
1787
1788   switch (status) {
1789     case GST_CLOCK_EARLY:
1790       /* the buffer is too late. We currently don't drop the buffer. */
1791       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1792       break;
1793     case GST_CLOCK_OK:
1794       /* buffer synchronised properly */
1795       GST_DEBUG_OBJECT (src, "buffer ok");
1796       break;
1797     case GST_CLOCK_UNSCHEDULED:
1798       /* this case is triggered when we were waiting for the clock and
1799        * it got unlocked because we did a state change. We return 
1800        * WRONG_STATE in this case to stop the dataflow also get rid of the
1801        * produced buffer. */
1802       GST_DEBUG_OBJECT (src,
1803           "clock was unscheduled (%d), returning WRONG_STATE", status);
1804       gst_buffer_unref (*buf);
1805       *buf = NULL;
1806       ret = GST_FLOW_WRONG_STATE;
1807       break;
1808     default:
1809       /* all other result values are unexpected and errors */
1810       GST_ELEMENT_ERROR (src, CORE, CLOCK,
1811           (_("Internal clock error.")),
1812           ("clock returned unexpected return value %d", status));
1813       gst_buffer_unref (*buf);
1814       *buf = NULL;
1815       ret = GST_FLOW_ERROR;
1816       break;
1817   }
1818   return ret;
1819
1820   /* ERROR */
1821 stopped:
1822   {
1823     GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
1824         gst_flow_get_name (ret));
1825     return ret;
1826   }
1827 not_ok:
1828   {
1829     GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
1830         gst_flow_get_name (ret));
1831     return ret;
1832   }
1833 not_started:
1834   {
1835     GST_DEBUG_OBJECT (src, "getrange but not started");
1836     return GST_FLOW_WRONG_STATE;
1837   }
1838 no_function:
1839   {
1840     GST_DEBUG_OBJECT (src, "no create function");
1841     return GST_FLOW_ERROR;
1842   }
1843 unexpected_length:
1844   {
1845     GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1846         ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1847     return GST_FLOW_UNEXPECTED;
1848   }
1849 reached_num_buffers:
1850   {
1851     GST_DEBUG_OBJECT (src, "sent all buffers");
1852     return GST_FLOW_UNEXPECTED;
1853   }
1854 flushing:
1855   {
1856     GST_DEBUG_OBJECT (src, "we are flushing");
1857     gst_buffer_unref (*buf);
1858     *buf = NULL;
1859     return GST_FLOW_WRONG_STATE;
1860   }
1861 eos:
1862   {
1863     GST_DEBUG_OBJECT (src, "we are EOS");
1864     gst_buffer_unref (*buf);
1865     *buf = NULL;
1866     return GST_FLOW_UNEXPECTED;
1867   }
1868 }
1869
1870 static GstFlowReturn
1871 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1872     GstBuffer ** buf)
1873 {
1874   GstBaseSrc *src;
1875   GstFlowReturn res;
1876
1877   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1878
1879   GST_LIVE_LOCK (src);
1880   if (G_UNLIKELY (src->priv->flushing))
1881     goto flushing;
1882
1883   /* if we're EOS, return right away */
1884   if (G_UNLIKELY (src->priv->pending_eos))
1885     goto eos;
1886
1887   res = gst_base_src_get_range (src, offset, length, buf);
1888
1889 done:
1890   GST_LIVE_UNLOCK (src);
1891
1892   gst_object_unref (src);
1893
1894   return res;
1895
1896   /* ERRORS */
1897 flushing:
1898   {
1899     GST_DEBUG_OBJECT (src, "we are flushing");
1900     res = GST_FLOW_WRONG_STATE;
1901     goto done;
1902   }
1903 eos:
1904   {
1905     GST_DEBUG_OBJECT (src, "we are EOS");
1906     res = GST_FLOW_UNEXPECTED;
1907     goto done;
1908   }
1909 }
1910
1911 static gboolean
1912 gst_base_src_default_check_get_range (GstBaseSrc * src)
1913 {
1914   gboolean res;
1915
1916   if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1917     GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1918     if (G_LIKELY (gst_base_src_start (src)))
1919       gst_base_src_stop (src);
1920   }
1921
1922   /* we can operate in getrange mode if the native format is bytes
1923    * and we are seekable, this condition is set in the random_access
1924    * flag and is set in the _start() method. */
1925   res = src->random_access;
1926
1927   return res;
1928 }
1929
1930 static gboolean
1931 gst_base_src_check_get_range (GstBaseSrc * src)
1932 {
1933   GstBaseSrcClass *bclass;
1934   gboolean res;
1935
1936   bclass = GST_BASE_SRC_GET_CLASS (src);
1937
1938   if (bclass->check_get_range == NULL)
1939     goto no_function;
1940
1941   res = bclass->check_get_range (src);
1942   GST_LOG_OBJECT (src, "%s() returned %d",
1943       GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1944
1945   return res;
1946
1947   /* ERRORS */
1948 no_function:
1949   {
1950     GST_WARNING_OBJECT (src, "no check_get_range function set");
1951     return FALSE;
1952   }
1953 }
1954
1955 static gboolean
1956 gst_base_src_pad_check_get_range (GstPad * pad)
1957 {
1958   GstBaseSrc *src;
1959   gboolean res;
1960
1961   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1962
1963   res = gst_base_src_check_get_range (src);
1964
1965   return res;
1966 }
1967
1968 static void
1969 gst_base_src_loop (GstPad * pad)
1970 {
1971   GstBaseSrc *src;
1972   GstBuffer *buf = NULL;
1973   GstFlowReturn ret;
1974   gint64 position;
1975   gboolean eos;
1976
1977   eos = FALSE;
1978
1979   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1980
1981   GST_LIVE_LOCK (src);
1982   if (G_UNLIKELY (src->priv->flushing))
1983     goto flushing;
1984
1985   /* if we're EOS, return right away */
1986   if (G_UNLIKELY (src->priv->pending_eos))
1987     goto eos;
1988
1989   src->priv->last_sent_eos = FALSE;
1990
1991   /* if we operate in bytes, we can calculate an offset */
1992   if (src->segment.format == GST_FORMAT_BYTES)
1993     position = src->segment.last_stop;
1994   else
1995     position = -1;
1996
1997   ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1998   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1999     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2000         gst_flow_get_name (ret));
2001     GST_LIVE_UNLOCK (src);
2002     goto pause;
2003   }
2004   /* this should not happen */
2005   if (G_UNLIKELY (buf == NULL))
2006     goto null_buffer;
2007
2008   /* push events to close/start our segment before we push the buffer. */
2009   if (G_UNLIKELY (src->priv->close_segment)) {
2010     gst_pad_push_event (pad, src->priv->close_segment);
2011     src->priv->close_segment = NULL;
2012   }
2013   if (G_UNLIKELY (src->priv->start_segment)) {
2014     gst_pad_push_event (pad, src->priv->start_segment);
2015     src->priv->start_segment = NULL;
2016   }
2017
2018   /* figure out the new position */
2019   switch (src->segment.format) {
2020     case GST_FORMAT_BYTES:
2021       position += GST_BUFFER_SIZE (buf);
2022       break;
2023     case GST_FORMAT_TIME:
2024     {
2025       GstClockTime start, duration;
2026
2027       start = GST_BUFFER_TIMESTAMP (buf);
2028       duration = GST_BUFFER_DURATION (buf);
2029
2030       if (GST_CLOCK_TIME_IS_VALID (start))
2031         position = start;
2032       else
2033         position = src->segment.last_stop;
2034
2035       if (GST_CLOCK_TIME_IS_VALID (duration))
2036         position += duration;
2037       break;
2038     }
2039     case GST_FORMAT_DEFAULT:
2040       position = GST_BUFFER_OFFSET_END (buf);
2041       break;
2042     default:
2043       position = -1;
2044       break;
2045   }
2046   if (position != -1) {
2047     if (src->segment.stop != -1) {
2048       if (position >= src->segment.stop) {
2049         eos = TRUE;
2050         position = src->segment.stop;
2051       }
2052     }
2053     gst_segment_set_last_stop (&src->segment, src->segment.format, position);
2054   }
2055
2056   if (G_UNLIKELY (src->priv->discont)) {
2057     buf = gst_buffer_make_metadata_writable (buf);
2058     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2059     src->priv->discont = FALSE;
2060   }
2061   GST_LIVE_UNLOCK (src);
2062
2063   ret = gst_pad_push (pad, buf);
2064   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2065     GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2066         gst_flow_get_name (ret));
2067     goto pause;
2068   }
2069
2070   if (G_UNLIKELY (eos)) {
2071     GST_INFO_OBJECT (src, "pausing after end of segment");
2072     ret = GST_FLOW_UNEXPECTED;
2073     goto pause;
2074   }
2075
2076 done:
2077   return;
2078
2079   /* special cases */
2080 flushing:
2081   {
2082     GST_DEBUG_OBJECT (src, "we are flushing");
2083     GST_LIVE_UNLOCK (src);
2084     ret = GST_FLOW_WRONG_STATE;
2085     goto pause;
2086   }
2087 eos:
2088   {
2089     GST_DEBUG_OBJECT (src, "we are EOS");
2090     GST_LIVE_UNLOCK (src);
2091     ret = GST_FLOW_UNEXPECTED;
2092     goto pause;
2093   }
2094 pause:
2095   {
2096     const gchar *reason = gst_flow_get_name (ret);
2097
2098     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2099     src->data.ABI.running = FALSE;
2100     gst_pad_pause_task (pad);
2101     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2102       if (ret == GST_FLOW_UNEXPECTED) {
2103         /* perform EOS logic */
2104         if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2105           gst_element_post_message (GST_ELEMENT_CAST (src),
2106               gst_message_new_segment_done (GST_OBJECT_CAST (src),
2107                   src->segment.format, src->segment.last_stop));
2108         } else {
2109           gst_pad_push_event (pad, gst_event_new_eos ());
2110           src->priv->last_sent_eos = TRUE;
2111         }
2112       } else {
2113         /* for fatal errors we post an error message, post the error
2114          * first so the app knows about the error first. */
2115         GST_ELEMENT_ERROR (src, STREAM, FAILED,
2116             (_("Internal data flow error.")),
2117             ("streaming task paused, reason %s (%d)", reason, ret));
2118         gst_pad_push_event (pad, gst_event_new_eos ());
2119         src->priv->last_sent_eos = TRUE;
2120       }
2121     }
2122     goto done;
2123   }
2124 null_buffer:
2125   {
2126     GST_ELEMENT_ERROR (src, STREAM, FAILED,
2127         (_("Internal data flow error.")), ("element returned NULL buffer"));
2128     GST_LIVE_UNLOCK (src);
2129     /* we finished the segment on error */
2130     ret = GST_FLOW_ERROR;
2131     goto done;
2132   }
2133 }
2134
2135 /* default negotiation code. 
2136  *
2137  * Take intersection between src and sink pads, take first
2138  * caps and fixate. 
2139  */
2140 static gboolean
2141 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
2142 {
2143   GstCaps *thiscaps;
2144   GstCaps *caps = NULL;
2145   GstCaps *peercaps = NULL;
2146   gboolean result = FALSE;
2147
2148   /* first see what is possible on our source pad */
2149   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
2150   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
2151   /* nothing or anything is allowed, we're done */
2152   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
2153     goto no_nego_needed;
2154
2155   /* get the peer caps */
2156   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
2157   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
2158   if (peercaps) {
2159     GstCaps *icaps;
2160
2161     /* get intersection */
2162     icaps = gst_caps_intersect (thiscaps, peercaps);
2163     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
2164     gst_caps_unref (thiscaps);
2165     gst_caps_unref (peercaps);
2166     if (icaps) {
2167       /* take first (and best, since they are sorted) possibility */
2168       caps = gst_caps_copy_nth (icaps, 0);
2169       gst_caps_unref (icaps);
2170     }
2171   } else {
2172     /* no peer, work with our own caps then */
2173     caps = thiscaps;
2174   }
2175   if (caps) {
2176     caps = gst_caps_make_writable (caps);
2177     gst_caps_truncate (caps);
2178
2179     /* now fixate */
2180     if (!gst_caps_is_empty (caps)) {
2181       gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
2182       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
2183
2184       if (gst_caps_is_any (caps)) {
2185         /* hmm, still anything, so element can do anything and
2186          * nego is not needed */
2187         result = TRUE;
2188       } else if (gst_caps_is_fixed (caps)) {
2189         /* yay, fixed caps, use those then */
2190         gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
2191         result = TRUE;
2192       }
2193     }
2194     gst_caps_unref (caps);
2195   }
2196   return result;
2197
2198 no_nego_needed:
2199   {
2200     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
2201     if (thiscaps)
2202       gst_caps_unref (thiscaps);
2203     return TRUE;
2204   }
2205 }
2206
2207 static gboolean
2208 gst_base_src_negotiate (GstBaseSrc * basesrc)
2209 {
2210   GstBaseSrcClass *bclass;
2211   gboolean result = TRUE;
2212
2213   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2214
2215   if (bclass->negotiate)
2216     result = bclass->negotiate (basesrc);
2217
2218   return result;
2219 }
2220
2221 static gboolean
2222 gst_base_src_start (GstBaseSrc * basesrc)
2223 {
2224   GstBaseSrcClass *bclass;
2225   gboolean result;
2226   guint64 size;
2227
2228   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2229     return TRUE;
2230
2231   GST_DEBUG_OBJECT (basesrc, "starting source");
2232
2233   basesrc->num_buffers_left = basesrc->num_buffers;
2234
2235   gst_segment_init (&basesrc->segment, basesrc->segment.format);
2236   basesrc->data.ABI.running = FALSE;
2237
2238   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2239   if (bclass->start)
2240     result = bclass->start (basesrc);
2241   else
2242     result = TRUE;
2243
2244   if (!result)
2245     goto could_not_start;
2246
2247   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
2248
2249   /* figure out the size */
2250   if (basesrc->segment.format == GST_FORMAT_BYTES) {
2251     if (bclass->get_size) {
2252       if (!(result = bclass->get_size (basesrc, &size)))
2253         size = -1;
2254     } else {
2255       result = FALSE;
2256       size = -1;
2257     }
2258     GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
2259     /* only update the size when operating in bytes, subclass is supposed
2260      * to set duration in the start method for other formats */
2261     gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
2262   } else {
2263     size = -1;
2264   }
2265
2266   GST_DEBUG_OBJECT (basesrc,
2267       "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
2268       G_GINT64_FORMAT, basesrc->segment.format, result, size,
2269       basesrc->segment.duration);
2270
2271   /* check if we can seek */
2272   if (bclass->is_seekable)
2273     basesrc->seekable = bclass->is_seekable (basesrc);
2274   else
2275     basesrc->seekable = FALSE;
2276
2277   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
2278
2279   /* update for random access flag */
2280   basesrc->random_access = basesrc->seekable &&
2281       basesrc->segment.format == GST_FORMAT_BYTES;
2282
2283   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
2284
2285   /* run typefind if we are random_access and the typefinding is enabled. */
2286   if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
2287     GstCaps *caps;
2288
2289     caps = gst_type_find_helper (basesrc->srcpad, size);
2290     gst_pad_set_caps (basesrc->srcpad, caps);
2291     gst_caps_unref (caps);
2292   } else {
2293     /* use class or default negotiate function */
2294     if (!gst_base_src_negotiate (basesrc))
2295       goto could_not_negotiate;
2296   }
2297
2298   return TRUE;
2299
2300   /* ERROR */
2301 could_not_start:
2302   {
2303     GST_DEBUG_OBJECT (basesrc, "could not start");
2304     /* subclass is supposed to post a message. We don't have to call _stop. */
2305     return FALSE;
2306   }
2307 could_not_negotiate:
2308   {
2309     GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
2310     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
2311         ("Could not negotiate format"), ("Check your filtered caps, if any"));
2312     /* we must call stop */
2313     gst_base_src_stop (basesrc);
2314     return FALSE;
2315   }
2316 }
2317
2318 static gboolean
2319 gst_base_src_stop (GstBaseSrc * basesrc)
2320 {
2321   GstBaseSrcClass *bclass;
2322   gboolean result = TRUE;
2323
2324   if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
2325     return TRUE;
2326
2327   GST_DEBUG_OBJECT (basesrc, "stopping source");
2328
2329   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2330   if (bclass->stop)
2331     result = bclass->stop (basesrc);
2332
2333   if (result)
2334     GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
2335
2336   return result;
2337 }
2338
2339 /* start or stop flushing dataprocessing 
2340  */
2341 static gboolean
2342 gst_base_src_set_flushing (GstBaseSrc * basesrc,
2343     gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
2344 {
2345   GstBaseSrcClass *bclass;
2346
2347   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2348
2349   if (flushing && unlock) {
2350     /* unlock any subclasses, we need to do this before grabbing the
2351      * LIVE_LOCK since we hold this lock before going into ::create. We pass an
2352      * unlock to the params because of backwards compat (see seek handler)*/
2353     if (bclass->unlock)
2354       bclass->unlock (basesrc);
2355   }
2356
2357   /* the live lock is released when we are blocked, waiting for playing or
2358    * when we sync to the clock. */
2359   GST_LIVE_LOCK (basesrc);
2360   if (playing)
2361     *playing = basesrc->live_running;
2362   basesrc->priv->flushing = flushing;
2363   if (flushing) {
2364     /* if we are locked in the live lock, signal it to make it flush */
2365     basesrc->live_running = TRUE;
2366     /* clear pending EOS if any */
2367     basesrc->priv->pending_eos = FALSE;
2368
2369     /* step 1, now that we have the LIVE lock, clear our unlock request */
2370     if (bclass->unlock_stop)
2371       bclass->unlock_stop (basesrc);
2372
2373     /* step 2, unblock clock sync (if any) or any other blocking thing */
2374     if (basesrc->clock_id)
2375       gst_clock_id_unschedule (basesrc->clock_id);
2376   } else {
2377     /* signal the live source that it can start playing */
2378     basesrc->live_running = live_play;
2379   }
2380   GST_LIVE_SIGNAL (basesrc);
2381   GST_LIVE_UNLOCK (basesrc);
2382
2383   return TRUE;
2384 }
2385
2386 /* the purpose of this function is to make sure that a live source blocks in the
2387  * LIVE lock or leaves the LIVE lock and continues playing. */
2388 static gboolean
2389 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
2390 {
2391   GstBaseSrcClass *bclass;
2392
2393   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2394
2395   /* unlock subclasses locked in ::create, we only do this when we stop playing. */
2396   if (!live_play) {
2397     GST_DEBUG_OBJECT (basesrc, "unlock");
2398     if (bclass->unlock)
2399       bclass->unlock (basesrc);
2400   }
2401
2402   /* we are now able to grab the LIVE lock, when we get it, we can be
2403    * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
2404    * for the clock. */
2405   GST_LIVE_LOCK (basesrc);
2406   GST_DEBUG_OBJECT (basesrc, "unschedule clock");
2407
2408   /* unblock clock sync (if any) */
2409   if (basesrc->clock_id)
2410     gst_clock_id_unschedule (basesrc->clock_id);
2411
2412   /* configure what to do when we get to the LIVE lock. */
2413   GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
2414   basesrc->live_running = live_play;
2415
2416   if (live_play) {
2417     gboolean start;
2418
2419     /* clear our unlock request when going to PLAYING */
2420     GST_DEBUG_OBJECT (basesrc, "unlock stop");
2421     if (bclass->unlock_stop)
2422       bclass->unlock_stop (basesrc);
2423
2424     /* for live sources we restart the timestamp correction */
2425     basesrc->priv->latency = -1;
2426     /* have to restart the task in case it stopped because of the unlock when
2427      * we went to PAUSED. Only do this if we operating in push mode. */
2428     GST_OBJECT_LOCK (basesrc->srcpad);
2429     start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
2430     GST_OBJECT_UNLOCK (basesrc->srcpad);
2431     if (start)
2432       gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
2433           basesrc->srcpad);
2434     GST_DEBUG_OBJECT (basesrc, "signal");
2435     GST_LIVE_SIGNAL (basesrc);
2436   }
2437   GST_LIVE_UNLOCK (basesrc);
2438
2439   return TRUE;
2440 }
2441
2442 static gboolean
2443 gst_base_src_activate_push (GstPad * pad, gboolean active)
2444 {
2445   GstBaseSrc *basesrc;
2446   GstEvent *event;
2447
2448   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2449
2450   /* prepare subclass first */
2451   if (active) {
2452     GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
2453
2454     if (G_UNLIKELY (!basesrc->can_activate_push))
2455       goto no_push_activation;
2456
2457     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2458       goto error_start;
2459
2460     basesrc->priv->last_sent_eos = FALSE;
2461     basesrc->priv->discont = TRUE;
2462     gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2463
2464     /* do initial seek, which will start the task */
2465     GST_OBJECT_LOCK (basesrc);
2466     event = basesrc->data.ABI.pending_seek;
2467     basesrc->data.ABI.pending_seek = NULL;
2468     GST_OBJECT_UNLOCK (basesrc);
2469
2470     /* no need to unlock anything, the task is certainly
2471      * not running here. The perform seek code will start the task when
2472      * finished. */
2473     if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
2474       goto seek_failed;
2475
2476     if (event)
2477       gst_event_unref (event);
2478   } else {
2479     GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
2480     /* flush all */
2481     gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2482     /* stop the task */
2483     gst_pad_stop_task (pad);
2484     /* now we can stop the source */
2485     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2486       goto error_stop;
2487   }
2488   return TRUE;
2489
2490   /* ERRORS */
2491 no_push_activation:
2492   {
2493     GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2494     return FALSE;
2495   }
2496 error_start:
2497   {
2498     GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2499     return FALSE;
2500   }
2501 seek_failed:
2502   {
2503     GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2504     gst_base_src_stop (basesrc);
2505     if (event)
2506       gst_event_unref (event);
2507     return FALSE;
2508   }
2509 error_stop:
2510   {
2511     GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2512     return FALSE;
2513   }
2514 }
2515
2516 static gboolean
2517 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2518 {
2519   GstBaseSrc *basesrc;
2520
2521   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2522
2523   /* prepare subclass first */
2524   if (active) {
2525     GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2526     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2527       goto error_start;
2528
2529     /* if not random_access, we cannot operate in pull mode for now */
2530     if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2531       goto no_get_range;
2532
2533     /* stop flushing now but for live sources, still block in the LIVE lock when
2534      * we are not yet PLAYING */
2535     gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
2536   } else {
2537     GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2538     /* flush all, there is no task to stop */
2539     gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
2540
2541     /* don't send EOS when going from PAUSED => READY when in pull mode */
2542     basesrc->priv->last_sent_eos = TRUE;
2543
2544     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2545       goto error_stop;
2546   }
2547   return TRUE;
2548
2549   /* ERRORS */
2550 error_start:
2551   {
2552     GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2553     return FALSE;
2554   }
2555 no_get_range:
2556   {
2557     GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2558     gst_base_src_stop (basesrc);
2559     return FALSE;
2560   }
2561 error_stop:
2562   {
2563     GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2564     return FALSE;
2565   }
2566 }
2567
2568 static GstStateChangeReturn
2569 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2570 {
2571   GstBaseSrc *basesrc;
2572   GstStateChangeReturn result;
2573   gboolean no_preroll = FALSE;
2574
2575   basesrc = GST_BASE_SRC (element);
2576
2577   switch (transition) {
2578     case GST_STATE_CHANGE_NULL_TO_READY:
2579       break;
2580     case GST_STATE_CHANGE_READY_TO_PAUSED:
2581       no_preroll = gst_base_src_is_live (basesrc);
2582       break;
2583     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2584       GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
2585       if (gst_base_src_is_live (basesrc)) {
2586         /* now we can start playback */
2587         gst_base_src_set_playing (basesrc, TRUE);
2588       }
2589       break;
2590     default:
2591       break;
2592   }
2593
2594   if ((result =
2595           GST_ELEMENT_CLASS (parent_class)->change_state (element,
2596               transition)) == GST_STATE_CHANGE_FAILURE)
2597     goto failure;
2598
2599   switch (transition) {
2600     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2601       GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
2602       if (gst_base_src_is_live (basesrc)) {
2603         /* make sure we block in the live lock in PAUSED */
2604         gst_base_src_set_playing (basesrc, FALSE);
2605         no_preroll = TRUE;
2606       }
2607       break;
2608     case GST_STATE_CHANGE_PAUSED_TO_READY:
2609     {
2610       GstEvent **event_p;
2611
2612       /* we don't need to unblock anything here, the pad deactivation code
2613        * already did this */
2614
2615       /* FIXME, deprecate this behaviour, it is very dangerous.
2616        * the prefered way of sending EOS downstream is by sending
2617        * the EOS event to the element */
2618       if (!basesrc->priv->last_sent_eos) {
2619         GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2620         gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2621         basesrc->priv->last_sent_eos = TRUE;
2622       }
2623       basesrc->priv->pending_eos = FALSE;
2624       event_p = &basesrc->data.ABI.pending_seek;
2625       gst_event_replace (event_p, NULL);
2626       event_p = &basesrc->priv->close_segment;
2627       gst_event_replace (event_p, NULL);
2628       event_p = &basesrc->priv->start_segment;
2629       gst_event_replace (event_p, NULL);
2630       break;
2631     }
2632     case GST_STATE_CHANGE_READY_TO_NULL:
2633       break;
2634     default:
2635       break;
2636   }
2637
2638   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2639     result = GST_STATE_CHANGE_NO_PREROLL;
2640
2641   return result;
2642
2643   /* ERRORS */
2644 failure:
2645   {
2646     GST_DEBUG_OBJECT (basesrc, "parent failed state change");
2647     return result;
2648   }
2649 }