libs/gst/base/gstbasesrc.c: Only push the segment events in the PLAYING state for...
[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 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
273
274 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
275 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
276 static void gst_base_src_set_property (GObject * object, guint prop_id,
277     const GValue * value, GParamSpec * pspec);
278 static void gst_base_src_get_property (GObject * object, guint prop_id,
279     GValue * value, GParamSpec * pspec);
280 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
281 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
282 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
283 static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
284
285 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
286
287 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
288 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
289     GstSegment * segment);
290 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
291
292 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
293 static gboolean gst_base_src_unlock_stop (GstBaseSrc * basesrc);
294 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
295 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
296
297 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
298     GstStateChange transition);
299
300 static void gst_base_src_loop (GstPad * pad);
301 static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
302 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
303 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
304     guint length, GstBuffer ** buf);
305 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
306     guint length, GstBuffer ** buf);
307
308 static void
309 gst_base_src_base_init (gpointer g_class)
310 {
311   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
312 }
313
314 static void
315 gst_base_src_class_init (GstBaseSrcClass * klass)
316 {
317   GObjectClass *gobject_class;
318   GstElementClass *gstelement_class;
319
320   gobject_class = G_OBJECT_CLASS (klass);
321   gstelement_class = GST_ELEMENT_CLASS (klass);
322
323   g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
324
325   parent_class = g_type_class_peek_parent (klass);
326
327   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
328   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
329   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
330
331   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
332       g_param_spec_ulong ("blocksize", "Block size",
333           "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
334           DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
335   g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
336       g_param_spec_int ("num-buffers", "num-buffers",
337           "Number of buffers to output before sending EOS", -1, G_MAXINT,
338           DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
339   g_object_class_install_property (gobject_class, PROP_TYPEFIND,
340       g_param_spec_boolean ("typefind", "Typefind",
341           "Run typefind before negotiating", DEFAULT_TYPEFIND,
342           G_PARAM_READWRITE));
343
344   gstelement_class->change_state =
345       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
346   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
347   gstelement_class->get_query_types =
348       GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
349
350   klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
351   klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
352   klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
353   klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
354   klass->check_get_range =
355       GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
356 }
357
358 static void
359 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
360 {
361   GstPad *pad;
362   GstPadTemplate *pad_template;
363
364   basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
365
366   basesrc->is_live = FALSE;
367   basesrc->live_lock = g_mutex_new ();
368   basesrc->live_cond = g_cond_new ();
369   basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
370   basesrc->num_buffers_left = -1;
371
372   basesrc->can_activate_push = TRUE;
373   basesrc->pad_mode = GST_ACTIVATE_NONE;
374
375   pad_template =
376       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
377   g_return_if_fail (pad_template != NULL);
378
379   GST_DEBUG_OBJECT (basesrc, "creating src pad");
380   pad = gst_pad_new_from_template (pad_template, "src");
381
382   GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
383   gst_pad_set_activatepush_function (pad,
384       GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
385   gst_pad_set_activatepull_function (pad,
386       GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
387   gst_pad_set_event_function (pad,
388       GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
389   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
390   gst_pad_set_checkgetrange_function (pad,
391       GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
392   gst_pad_set_getrange_function (pad,
393       GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
394   gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
395   gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
396   gst_pad_set_fixatecaps_function (pad,
397       GST_DEBUG_FUNCPTR (gst_base_src_fixate));
398
399   /* hold pointer to pad */
400   basesrc->srcpad = pad;
401   GST_DEBUG_OBJECT (basesrc, "adding src pad");
402   gst_element_add_pad (GST_ELEMENT (basesrc), pad);
403
404   basesrc->blocksize = DEFAULT_BLOCKSIZE;
405   basesrc->clock_id = NULL;
406   /* we operate in BYTES by default */
407   gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
408   basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
409
410   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
411
412   GST_DEBUG_OBJECT (basesrc, "init done");
413 }
414
415 static void
416 gst_base_src_finalize (GObject * object)
417 {
418   GstBaseSrc *basesrc;
419   GstEvent **event_p;
420
421   basesrc = GST_BASE_SRC (object);
422
423   g_mutex_free (basesrc->live_lock);
424   g_cond_free (basesrc->live_cond);
425
426   event_p = &basesrc->data.ABI.pending_seek;
427   gst_event_replace ((GstEvent **) event_p, NULL);
428
429   G_OBJECT_CLASS (parent_class)->finalize (object);
430 }
431
432 /**
433  * gst_base_src_wait_playing:
434  * @src: the src
435  *
436  * If the #GstBaseSrcClass::create method performs its own synchronisation against
437  * the clock it must unblock when going from PLAYING to the PAUSED state and call
438  * this method before continuing to produce the remaining data.
439  *
440  * This function will block until a state change to PLAYING happens (in which
441  * case this function returns #GST_FLOW_OK) or the processing must be stopped due
442  * to a state change to READY or a FLUSH event (in which case this function
443  * returns #GST_FLOW_WRONG_STATE).
444  *
445  * Since: 0.10.12
446  *
447  * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
448  * continue. Any other return value should be returned from the create vmethod.
449  */
450 GstFlowReturn
451 gst_base_src_wait_playing (GstBaseSrc * src)
452 {
453   /* block until the state changes, or we get a flush, or something */
454   GST_LIVE_LOCK (src);
455   if (src->is_live) {
456     while (G_UNLIKELY (!src->live_running)) {
457       GST_DEBUG ("live source signal waiting");
458       GST_LIVE_SIGNAL (src);
459       GST_DEBUG ("live source waiting for running state");
460       GST_LIVE_WAIT (src);
461       GST_DEBUG ("live source unlocked");
462     }
463     /* FIXME, use another variable to signal stopping so that we don't
464      * have to grab another lock. */
465     GST_OBJECT_LOCK (src->srcpad);
466     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
467       goto flushing;
468     GST_OBJECT_UNLOCK (src->srcpad);
469   }
470   GST_LIVE_UNLOCK (src);
471
472   return GST_FLOW_OK;
473
474   /* ERRORS */
475 flushing:
476   {
477     GST_DEBUG_OBJECT (src, "pad is flushing");
478     GST_OBJECT_UNLOCK (src->srcpad);
479     GST_LIVE_UNLOCK (src);
480     return GST_FLOW_WRONG_STATE;
481   }
482 }
483
484 /**
485  * gst_base_src_set_live:
486  * @src: base source instance
487  * @live: new live-mode
488  *
489  * If the element listens to a live source, @live should
490  * be set to %TRUE. 
491  *
492  * A live source will not produce data in the PAUSED state and
493  * will therefore not be able to participate in the PREROLL phase
494  * of a pipeline. To signal this fact to the application and the 
495  * pipeline, the state change return value of the live source will
496  * be GST_STATE_CHANGE_NO_PREROLL.
497  */
498 void
499 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
500 {
501   GST_LIVE_LOCK (src);
502   src->is_live = live;
503   GST_LIVE_UNLOCK (src);
504 }
505
506 /**
507  * gst_base_src_is_live:
508  * @src: base source instance
509  *
510  * Check if an element is in live mode.
511  *
512  * Returns: %TRUE if element is in live mode.
513  */
514 gboolean
515 gst_base_src_is_live (GstBaseSrc * src)
516 {
517   gboolean result;
518
519   GST_LIVE_LOCK (src);
520   result = src->is_live;
521   GST_LIVE_UNLOCK (src);
522
523   return result;
524 }
525
526 /**
527  * gst_base_src_set_format:
528  * @src: base source instance
529  * @format: the format to use
530  *
531  * Sets the default format of the source. This will be the format used
532  * for sending NEW_SEGMENT events and for performing seeks.
533  *
534  * If a format of GST_FORMAT_BYTES is set, the element will be able to
535  * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
536  *
537  * @Since: 0.10.1
538  */
539 void
540 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
541 {
542   gst_segment_init (&src->segment, format);
543 }
544
545 static gboolean
546 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
547 {
548   GstBaseSrcClass *bclass;
549   GstBaseSrc *bsrc;
550   gboolean res = TRUE;
551
552   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
553   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
554
555   if (bclass->set_caps)
556     res = bclass->set_caps (bsrc, caps);
557
558   return res;
559 }
560
561 static GstCaps *
562 gst_base_src_getcaps (GstPad * pad)
563 {
564   GstBaseSrcClass *bclass;
565   GstBaseSrc *bsrc;
566   GstCaps *caps = NULL;
567
568   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
569   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
570   if (bclass->get_caps)
571     caps = bclass->get_caps (bsrc);
572
573   if (caps == NULL) {
574     GstPadTemplate *pad_template;
575
576     pad_template =
577         gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
578     if (pad_template != NULL) {
579       caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
580     }
581   }
582   return caps;
583 }
584
585 static void
586 gst_base_src_fixate (GstPad * pad, GstCaps * caps)
587 {
588   GstBaseSrcClass *bclass;
589   GstBaseSrc *bsrc;
590
591   bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
592   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
593
594   if (bclass->fixate)
595     bclass->fixate (bsrc, caps);
596
597   gst_object_unref (bsrc);
598 }
599
600 static gboolean
601 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
602 {
603   gboolean res;
604
605   switch (GST_QUERY_TYPE (query)) {
606     case GST_QUERY_POSITION:
607     {
608       GstFormat format;
609
610       gst_query_parse_position (query, &format, NULL);
611       switch (format) {
612         case GST_FORMAT_PERCENT:
613         {
614           gint64 percent;
615           gint64 position;
616           gint64 duration;
617
618           position = src->segment.last_stop;
619           duration = src->segment.duration;
620
621           if (position != -1 && duration != -1) {
622             if (position < duration)
623               percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
624                   duration);
625             else
626               percent = GST_FORMAT_PERCENT_MAX;
627           } else
628             percent = -1;
629
630           gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
631           res = TRUE;
632           break;
633         }
634         default:
635         {
636           gint64 position;
637
638           position = src->segment.last_stop;
639
640           if (position != -1) {
641             /* convert to requested format */
642             res =
643                 gst_pad_query_convert (src->srcpad, src->segment.format,
644                 position, &format, &position);
645           } else
646             res = TRUE;
647
648           gst_query_set_position (query, format, position);
649           break;
650         }
651       }
652       break;
653     }
654     case GST_QUERY_DURATION:
655     {
656       GstFormat format;
657
658       gst_query_parse_duration (query, &format, NULL);
659       switch (format) {
660         case GST_FORMAT_PERCENT:
661           gst_query_set_duration (query, GST_FORMAT_PERCENT,
662               GST_FORMAT_PERCENT_MAX);
663           res = TRUE;
664           break;
665         default:
666         {
667           gint64 duration;
668
669           duration = src->segment.duration;
670
671           if (duration != -1) {
672             /* convert to requested format */
673             res =
674                 gst_pad_query_convert (src->srcpad, src->segment.format,
675                 duration, &format, &duration);
676           } else {
677             res = TRUE;
678           }
679           gst_query_set_duration (query, format, duration);
680           break;
681         }
682       }
683       break;
684     }
685
686     case GST_QUERY_SEEKING:
687     {
688       gst_query_set_seeking (query, src->segment.format,
689           src->seekable, 0, src->segment.duration);
690       res = TRUE;
691       break;
692     }
693     case GST_QUERY_SEGMENT:
694     {
695       gint64 start, stop;
696
697       /* no end segment configured, current duration then */
698       if ((stop = src->segment.stop) == -1)
699         stop = src->segment.duration;
700       start = src->segment.start;
701
702       /* adjust to stream time */
703       if (src->segment.time != -1) {
704         start -= src->segment.time;
705         if (stop != -1)
706           stop -= src->segment.time;
707       }
708       gst_query_set_segment (query, src->segment.rate, src->segment.format,
709           start, stop);
710       res = TRUE;
711       break;
712     }
713
714     case GST_QUERY_FORMATS:
715     {
716       gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
717           GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
718       res = TRUE;
719       break;
720     }
721     case GST_QUERY_CONVERT:
722     {
723       GstFormat src_fmt, dest_fmt;
724       gint64 src_val, dest_val;
725
726       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
727
728       /* we can only convert between equal formats... */
729       if (src_fmt == dest_fmt) {
730         dest_val = src_val;
731         res = TRUE;
732       } else
733         res = FALSE;
734
735       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
736       break;
737     }
738     case GST_QUERY_LATENCY:
739       /* we can only report the fact that we are live or not, we know nothing
740        * about latency. Subclasses should override and implement something
741        * usefull */
742       GST_LIVE_LOCK (src);
743       gst_query_set_latency (query, src->is_live, 0, -1);
744       GST_LIVE_UNLOCK (src);
745       res = TRUE;
746       break;
747     case GST_QUERY_JITTER:
748     case GST_QUERY_RATE:
749     default:
750       res = FALSE;
751       break;
752   }
753   return res;
754 }
755
756 static gboolean
757 gst_base_src_query (GstPad * pad, GstQuery * query)
758 {
759   GstBaseSrc *src;
760   GstBaseSrcClass *bclass;
761   gboolean result = FALSE;
762
763   src = GST_BASE_SRC (gst_pad_get_parent (pad));
764
765   bclass = GST_BASE_SRC_GET_CLASS (src);
766
767   if (bclass->query)
768     result = bclass->query (src, query);
769   else
770     result = gst_pad_query_default (pad, query);
771
772   gst_object_unref (src);
773
774   return result;
775 }
776
777 static gboolean
778 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
779 {
780   gboolean res = TRUE;
781
782   /* update our offset if the start/stop position was updated */
783   if (segment->format == GST_FORMAT_BYTES) {
784     segment->last_stop = segment->start;
785     segment->time = segment->start;
786   } else if (segment->start == 0) {
787     /* seek to start, we can implement a default for this. */
788     segment->last_stop = 0;
789     segment->time = 0;
790     res = TRUE;
791   } else
792     res = FALSE;
793
794   return res;
795 }
796
797 static gboolean
798 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
799 {
800   GstBaseSrcClass *bclass;
801   gboolean result = FALSE;
802
803   bclass = GST_BASE_SRC_GET_CLASS (src);
804
805   if (bclass->do_seek)
806     result = bclass->do_seek (src, segment);
807
808   return result;
809 }
810
811 /* this code implements the seeking. It is a good example
812  * handling all cases.
813  *
814  * A seek updates the currently configured segment.start
815  * and segment.stop values based on the SEEK_TYPE. If the
816  * segment.start value is updated, a seek to this new position
817  * should be performed.
818  *
819  * The seek can only be executed when we are not currently
820  * streaming any data, to make sure that this is the case, we
821  * acquire the STREAM_LOCK which is taken when we are in the
822  * _loop() function or when a getrange() is called. Normally
823  * we will not receive a seek if we are operating in pull mode
824  * though.
825  *
826  * When we are in the loop() function, we might be in the middle
827  * of pushing a buffer, which might block in a sink. To make sure
828  * that the push gets unblocked we push out a FLUSH_START event.
829  * Our loop function will get a WRONG_STATE return value from
830  * the push and will pause, effectively releasing the STREAM_LOCK.
831  *
832  * For a non-flushing seek, we pause the task, which might eventually
833  * release the STREAM_LOCK. We say eventually because when the sink
834  * blocks on the sample we might wait a very long time until the sink
835  * unblocks the sample. In any case we acquire the STREAM_LOCK and
836  * can continue the seek. A non-flushing seek is normally done in a 
837  * running pipeline to perform seamless playback.
838  * In the case of a non-flushing seek we need to make sure that the
839  * data we output after the seek is continuous with the previous data,
840  * this is because a non-flushing seek does not reset the stream-time
841  * to 0. We do this by closing the currently running segment, ie. sending
842  * a new_segment event with the stop position set to the last processed 
843  * position.
844  *
845  * After updating the segment.start/stop values, we prepare for
846  * streaming again. We push out a FLUSH_STOP to make the peer pad
847  * accept data again and we start our task again.
848  *
849  * A segment seek posts a message on the bus saying that the playback
850  * of the segment started. We store the segment flag internally because
851  * when we reach the segment.stop we have to post a segment.done
852  * instead of EOS when doing a segment seek.
853  */
854 /* FIXME (0.11), we have the unlock gboolean here because most current 
855  * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
856  * the streaming thread isn't running, resulting in bogus unlocks later when it 
857  * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
858  * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
859  * until 0.11
860  */
861 static gboolean
862 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
863 {
864   gboolean res;
865   gdouble rate;
866   GstFormat format;
867   GstSeekFlags flags;
868   GstSeekType cur_type, stop_type;
869   gint64 cur, stop;
870   gboolean flush;
871   gboolean update;
872   GstSegment seeksegment;
873
874   GST_DEBUG_OBJECT (src, "doing seek");
875
876   if (event) {
877     gst_event_parse_seek (event, &rate, &format, &flags,
878         &cur_type, &cur, &stop_type, &stop);
879
880     /* we have to have a format as the segment format. Try to convert
881      * if not. */
882     if (src->segment.format != format) {
883       GstFormat fmt;
884
885       fmt = src->segment.format;
886       res = TRUE;
887       if (cur_type != GST_SEEK_TYPE_NONE)
888         res = gst_pad_query_convert (src->srcpad, format, cur, &fmt, &cur);
889       if (res && stop_type != GST_SEEK_TYPE_NONE)
890         res = gst_pad_query_convert (src->srcpad, format, stop, &fmt, &stop);
891       if (!res)
892         goto no_format;
893
894       format = fmt;
895     }
896   } else {
897     flags = 0;
898   }
899
900   flush = flags & GST_SEEK_FLAG_FLUSH;
901
902   /* send flush start */
903   if (flush)
904     gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
905   else
906     gst_pad_pause_task (src->srcpad);
907
908   /* unblock streaming thread */
909   if (unlock)
910     gst_base_src_unlock (src);
911
912   /* grab streaming lock, this should eventually be possible, either
913    * because the task is paused or our streaming thread stopped 
914    * because our peer is flushing. */
915   GST_PAD_STREAM_LOCK (src->srcpad);
916
917   if (unlock)
918     gst_base_src_unlock_stop (src);
919
920   /* make copy into temp structure, we can only update the main one
921    * when the subclass actually could do the seek. */
922   memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
923
924   /* now configure the seek segment */
925   if (event) {
926     gst_segment_set_seek (&seeksegment, rate, format, flags,
927         cur_type, cur, stop_type, stop, &update);
928   }
929
930   GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
931       " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
932       seeksegment.start, seeksegment.stop, seeksegment.last_stop);
933
934   /* do the seek, segment.last_stop contains new position. */
935   res = gst_base_src_do_seek (src, &seeksegment);
936
937   /* and prepare to continue streaming */
938   if (flush) {
939     /* send flush stop, peer will accept data and events again. We
940      * are not yet providing data as we still have the STREAM_LOCK. */
941     gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
942   } else if (res && src->data.ABI.running) {
943     /* we are running the current segment and doing a non-flushing seek, 
944      * close the segment first based on the last_stop. */
945     GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
946         " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
947
948     /* queue the segment for sending in the stream thread */
949     if (src->priv->close_segment)
950       gst_event_unref (src->priv->close_segment);
951     src->priv->close_segment =
952         gst_event_new_new_segment_full (TRUE,
953         src->segment.rate, src->segment.applied_rate, src->segment.format,
954         src->segment.start, src->segment.last_stop, src->segment.time);
955   }
956
957   /* if successfull seek, we update our real segment and push
958    * out the new segment. */
959   if (res) {
960     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
961
962     if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
963       gst_element_post_message (GST_ELEMENT (src),
964           gst_message_new_segment_start (GST_OBJECT (src),
965               src->segment.format, src->segment.last_stop));
966     }
967
968     /* for deriving a stop position for the playback segment form the seek
969      * segment, we must take the duration when the stop is not set */
970     if ((stop = src->segment.stop) == -1)
971       stop = src->segment.duration;
972
973     GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
974         " to %" G_GINT64_FORMAT, src->segment.start, stop);
975
976     /* now replace the old segment so that we send it in the stream thread the
977      * next time it is scheduled. */
978     if (src->priv->start_segment)
979       gst_event_unref (src->priv->start_segment);
980     src->priv->start_segment =
981         gst_event_new_new_segment_full (FALSE,
982         src->segment.rate, src->segment.applied_rate, src->segment.format,
983         src->segment.last_stop, stop, src->segment.time);
984   }
985
986   src->priv->discont = TRUE;
987   src->data.ABI.running = TRUE;
988   /* and restart the task in case it got paused explicitely or by
989    * the FLUSH_START event we pushed out. */
990   gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
991       src->srcpad);
992
993   /* and release the lock again so we can continue streaming */
994   GST_PAD_STREAM_UNLOCK (src->srcpad);
995
996   return res;
997
998   /* ERROR */
999 no_format:
1000   {
1001     GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1002     return FALSE;
1003   }
1004 }
1005
1006 static const GstQueryType *
1007 gst_base_src_get_query_types (GstElement * element)
1008 {
1009   static const GstQueryType query_types[] = {
1010     GST_QUERY_DURATION,
1011     GST_QUERY_POSITION,
1012     GST_QUERY_SEEKING,
1013     GST_QUERY_SEGMENT,
1014     GST_QUERY_FORMATS,
1015     GST_QUERY_LATENCY,
1016     GST_QUERY_JITTER,
1017     GST_QUERY_RATE,
1018     GST_QUERY_CONVERT,
1019     0
1020   };
1021
1022   return query_types;
1023 }
1024
1025 /* all events send to this element directly
1026  */
1027 static gboolean
1028 gst_base_src_send_event (GstElement * element, GstEvent * event)
1029 {
1030   GstBaseSrc *src;
1031   gboolean result = FALSE;
1032
1033   src = GST_BASE_SRC (element);
1034
1035   switch (GST_EVENT_TYPE (event)) {
1036     case GST_EVENT_FLUSH_START:
1037     case GST_EVENT_FLUSH_STOP:
1038       /* sending random flushes downstream can break stuff,
1039        * especially sync since all segment info will get flushed */
1040       break;
1041     case GST_EVENT_EOS:
1042       /* FIXME, queue EOS and make sure the task or pull function 
1043        * perform the EOS actions. */
1044       break;
1045     case GST_EVENT_NEWSEGMENT:
1046       /* sending random NEWSEGMENT downstream can break sync. */
1047       break;
1048     case GST_EVENT_TAG:
1049     case GST_EVENT_BUFFERSIZE:
1050       break;
1051     case GST_EVENT_QOS:
1052       break;
1053     case GST_EVENT_SEEK:
1054     {
1055       gboolean started;
1056
1057       GST_OBJECT_LOCK (src->srcpad);
1058       if (GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PULL)
1059         goto wrong_mode;
1060       started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
1061       GST_OBJECT_UNLOCK (src->srcpad);
1062
1063       if (started) {
1064         /* when we are running in push mode, we can execute the
1065          * seek right now, we need to unlock. */
1066         result = gst_base_src_perform_seek (src, event, TRUE);
1067       } else {
1068         GstEvent **event_p;
1069
1070         /* else we store the event and execute the seek when we
1071          * get activated */
1072         GST_OBJECT_LOCK (src);
1073         event_p = &src->data.ABI.pending_seek;
1074         gst_event_replace ((GstEvent **) event_p, event);
1075         GST_OBJECT_UNLOCK (src);
1076         /* assume the seek will work */
1077         result = TRUE;
1078       }
1079       break;
1080     }
1081     case GST_EVENT_NAVIGATION:
1082       break;
1083     default:
1084       break;
1085   }
1086 done:
1087   gst_event_unref (event);
1088
1089   return result;
1090
1091   /* ERRORS */
1092 wrong_mode:
1093   {
1094     GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1095     GST_OBJECT_UNLOCK (src->srcpad);
1096     result = FALSE;
1097     goto done;
1098   }
1099 }
1100
1101 static gboolean
1102 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1103 {
1104   gboolean result;
1105
1106   switch (GST_EVENT_TYPE (event)) {
1107     case GST_EVENT_SEEK:
1108       /* is normally called when in push mode */
1109       if (!src->seekable)
1110         goto not_seekable;
1111
1112       result = gst_base_src_perform_seek (src, event, TRUE);
1113       break;
1114     case GST_EVENT_FLUSH_START:
1115       /* cancel any blocking getrange, is normally called
1116        * when in pull mode. */
1117       result = gst_base_src_unlock (src);
1118       break;
1119     case GST_EVENT_FLUSH_STOP:
1120       result = gst_base_src_unlock_stop (src);
1121       break;
1122     default:
1123       result = TRUE;
1124       break;
1125   }
1126   return result;
1127
1128   /* ERRORS */
1129 not_seekable:
1130   {
1131     GST_DEBUG_OBJECT (src, "is not seekable");
1132     return FALSE;
1133   }
1134 }
1135
1136 static gboolean
1137 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
1138 {
1139   GstBaseSrc *src;
1140   GstBaseSrcClass *bclass;
1141   gboolean result = FALSE;
1142
1143   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1144   bclass = GST_BASE_SRC_GET_CLASS (src);
1145
1146   if (bclass->event) {
1147     if (!(result = bclass->event (src, event)))
1148       goto subclass_failed;
1149   }
1150
1151 done:
1152   gst_event_unref (event);
1153   gst_object_unref (src);
1154
1155   return result;
1156
1157   /* ERRORS */
1158 subclass_failed:
1159   {
1160     GST_DEBUG_OBJECT (src, "subclass refused event");
1161     goto done;
1162   }
1163 }
1164
1165 static void
1166 gst_base_src_set_property (GObject * object, guint prop_id,
1167     const GValue * value, GParamSpec * pspec)
1168 {
1169   GstBaseSrc *src;
1170
1171   src = GST_BASE_SRC (object);
1172
1173   switch (prop_id) {
1174     case PROP_BLOCKSIZE:
1175       src->blocksize = g_value_get_ulong (value);
1176       break;
1177     case PROP_NUM_BUFFERS:
1178       src->num_buffers = g_value_get_int (value);
1179       break;
1180     case PROP_TYPEFIND:
1181       src->data.ABI.typefind = g_value_get_boolean (value);
1182       break;
1183     default:
1184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1185       break;
1186   }
1187 }
1188
1189 static void
1190 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
1191     GParamSpec * pspec)
1192 {
1193   GstBaseSrc *src;
1194
1195   src = GST_BASE_SRC (object);
1196
1197   switch (prop_id) {
1198     case PROP_BLOCKSIZE:
1199       g_value_set_ulong (value, src->blocksize);
1200       break;
1201     case PROP_NUM_BUFFERS:
1202       g_value_set_int (value, src->num_buffers);
1203       break;
1204     case PROP_TYPEFIND:
1205       g_value_set_boolean (value, src->data.ABI.typefind);
1206       break;
1207     default:
1208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1209       break;
1210   }
1211 }
1212
1213 /* with STREAM_LOCK and LOCK */
1214 static GstClockReturn
1215 gst_base_src_wait (GstBaseSrc * basesrc, GstClockTime time)
1216 {
1217   GstClockReturn ret;
1218   GstClockID id;
1219   GstClock *clock;
1220
1221   /* get clock, if no clock, we don't sync */
1222   if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
1223     return GST_CLOCK_OK;
1224
1225   id = gst_clock_new_single_shot_id (clock, time);
1226
1227   basesrc->clock_id = id;
1228   /* release the object lock while waiting */
1229   GST_OBJECT_UNLOCK (basesrc);
1230
1231   ret = gst_clock_id_wait (id, NULL);
1232
1233   GST_OBJECT_LOCK (basesrc);
1234   gst_clock_id_unref (id);
1235   basesrc->clock_id = NULL;
1236
1237   return ret;
1238 }
1239
1240 /* perform synchronisation on a buffer. 
1241  * with STREAM_LOCK.
1242  */
1243 static GstClockReturn
1244 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
1245 {
1246   GstClockReturn result;
1247   GstClockTime start, end;
1248   GstBaseSrcClass *bclass;
1249   GstClockTime base_time;
1250
1251   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1252
1253   start = end = -1;
1254   if (bclass->get_times)
1255     bclass->get_times (basesrc, buffer, &start, &end);
1256
1257   /* if we don't have a timestamp, we don't sync */
1258   if (!GST_CLOCK_TIME_IS_VALID (start))
1259     goto invalid_start;
1260
1261   /* now do clocking */
1262   GST_OBJECT_LOCK (basesrc);
1263   base_time = GST_ELEMENT_CAST (basesrc)->base_time;
1264
1265   GST_LOG_OBJECT (basesrc,
1266       "waiting for clock, base time %" GST_TIME_FORMAT
1267       ", stream_start %" GST_TIME_FORMAT,
1268       GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
1269
1270   result = gst_base_src_wait (basesrc, start + base_time);
1271   GST_OBJECT_UNLOCK (basesrc);
1272
1273   GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
1274
1275   return result;
1276
1277   /* special cases */
1278 invalid_start:
1279   {
1280     GST_DEBUG_OBJECT (basesrc, "get_times returned invalid start");
1281     return GST_CLOCK_OK;
1282   }
1283 }
1284
1285 static gboolean
1286 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
1287 {
1288   guint64 size, maxsize;
1289   GstBaseSrcClass *bclass;
1290
1291   bclass = GST_BASE_SRC_GET_CLASS (src);
1292
1293   /* only operate if we are working with bytes */
1294   if (src->segment.format != GST_FORMAT_BYTES)
1295     return TRUE;
1296
1297   /* get total file size */
1298   size = (guint64) src->segment.duration;
1299
1300   /* the max amount of bytes to read is the total size or
1301    * up to the segment.stop if present. */
1302   if (src->segment.stop != -1)
1303     maxsize = MIN (size, src->segment.stop);
1304   else
1305     maxsize = size;
1306
1307   GST_DEBUG_OBJECT (src,
1308       "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
1309       ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
1310       *length, size, src->segment.stop, maxsize);
1311
1312   /* check size if we have one */
1313   if (maxsize != -1) {
1314     /* if we run past the end, check if the file became bigger and 
1315      * retry. */
1316     if (G_UNLIKELY (offset + *length >= maxsize)) {
1317       /* see if length of the file changed */
1318       if (bclass->get_size)
1319         if (!bclass->get_size (src, &size))
1320           size = -1;
1321
1322       gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
1323
1324       /* make sure we don't exceed the configured segment stop
1325        * if it was set */
1326       if (src->segment.stop != -1)
1327         maxsize = MIN (size, src->segment.stop);
1328       else
1329         maxsize = size;
1330
1331       /* if we are at or past the end, EOS */
1332       if (G_UNLIKELY (offset >= maxsize))
1333         goto unexpected_length;
1334
1335       /* else we can clip to the end */
1336       if (G_UNLIKELY (offset + *length >= maxsize))
1337         *length = maxsize - offset;
1338
1339     }
1340   }
1341
1342   /* keep track of current position. segment is in bytes, we checked 
1343    * that above. */
1344   gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
1345
1346   return TRUE;
1347
1348   /* ERRORS */
1349 unexpected_length:
1350   {
1351     return FALSE;
1352   }
1353 }
1354
1355 static GstFlowReturn
1356 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
1357     GstBuffer ** buf)
1358 {
1359   GstFlowReturn ret;
1360   GstBaseSrcClass *bclass;
1361   GstClockReturn status;
1362
1363   bclass = GST_BASE_SRC_GET_CLASS (src);
1364
1365   ret = gst_base_src_wait_playing (src);
1366   if (ret != GST_FLOW_OK)
1367     goto stopped;
1368
1369   if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
1370     goto not_started;
1371
1372   if (G_UNLIKELY (!bclass->create))
1373     goto no_function;
1374
1375   if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
1376     goto unexpected_length;
1377
1378   /* normally we don't count buffers */
1379   if (G_UNLIKELY (src->num_buffers_left >= 0)) {
1380     if (src->num_buffers_left == 0)
1381       goto reached_num_buffers;
1382     else
1383       src->num_buffers_left--;
1384   }
1385
1386   GST_DEBUG_OBJECT (src,
1387       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
1388       G_GINT64_FORMAT, offset, length, src->segment.time);
1389
1390   ret = bclass->create (src, offset, length, buf);
1391   if (G_UNLIKELY (ret != GST_FLOW_OK))
1392     goto done;
1393
1394   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
1395   if (offset == 0 && src->segment.time == 0
1396       && GST_BUFFER_TIMESTAMP (*buf) == -1)
1397     GST_BUFFER_TIMESTAMP (*buf) = 0;
1398
1399   /* now sync before pushing the buffer */
1400   status = gst_base_src_do_sync (src, *buf);
1401   switch (status) {
1402     case GST_CLOCK_EARLY:
1403       /* the buffer is too late. We currently don't drop the buffer. */
1404       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
1405       break;
1406     case GST_CLOCK_OK:
1407       /* buffer synchronised properly */
1408       GST_DEBUG_OBJECT (src, "buffer ok");
1409       break;
1410     case GST_CLOCK_UNSCHEDULED:
1411       /* this case is triggered when we were waiting for the clock and
1412        * it got unlocked because we did a state change. We return 
1413        * WRONG_STATE in this case to stop the dataflow also get rid of the
1414        * produced buffer. */
1415       GST_DEBUG_OBJECT (src,
1416           "clock was unscheduled (%d), returning WRONG_STATE", status);
1417       gst_buffer_unref (*buf);
1418       *buf = NULL;
1419       ret = GST_FLOW_WRONG_STATE;
1420       break;
1421     default:
1422       /* all other result values are unexpected and errors */
1423       GST_ELEMENT_ERROR (src, CORE, CLOCK,
1424           (_("Internal clock error.")),
1425           ("clock returned unexpected return value %d", status));
1426       gst_buffer_unref (*buf);
1427       *buf = NULL;
1428       ret = GST_FLOW_ERROR;
1429       break;
1430   }
1431 done:
1432   return ret;
1433
1434   /* ERROR */
1435 stopped:
1436   {
1437     GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
1438     return ret;
1439   }
1440 not_started:
1441   {
1442     GST_DEBUG_OBJECT (src, "getrange but not started");
1443     return GST_FLOW_WRONG_STATE;
1444   }
1445 no_function:
1446   {
1447     GST_DEBUG_OBJECT (src, "no create function");
1448     return GST_FLOW_ERROR;
1449   }
1450 unexpected_length:
1451   {
1452     GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
1453         ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
1454     return GST_FLOW_UNEXPECTED;
1455   }
1456 reached_num_buffers:
1457   {
1458     GST_DEBUG_OBJECT (src, "sent all buffers");
1459     return GST_FLOW_UNEXPECTED;
1460   }
1461 }
1462
1463 static GstFlowReturn
1464 gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
1465     GstBuffer ** buf)
1466 {
1467   GstBaseSrc *src;
1468   GstFlowReturn res;
1469
1470   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1471
1472   res = gst_base_src_get_range (src, offset, length, buf);
1473
1474   gst_object_unref (src);
1475
1476   return res;
1477 }
1478
1479 static gboolean
1480 gst_base_src_default_check_get_range (GstBaseSrc * src)
1481 {
1482   gboolean res;
1483
1484   if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
1485     GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
1486     if (G_LIKELY (gst_base_src_start (src)))
1487       gst_base_src_stop (src);
1488   }
1489
1490   /* we can operate in getrange mode if the native format is bytes
1491    * and we are seekable, this condition is set in the random_access
1492    * flag and is set in the _start() method. */
1493   res = src->random_access;
1494
1495   return res;
1496 }
1497
1498 static gboolean
1499 gst_base_src_check_get_range (GstBaseSrc * src)
1500 {
1501   GstBaseSrcClass *bclass;
1502   gboolean res;
1503
1504   bclass = GST_BASE_SRC_GET_CLASS (src);
1505
1506   if (bclass->check_get_range == NULL)
1507     goto no_function;
1508
1509   res = bclass->check_get_range (src);
1510   GST_LOG_OBJECT (src, "%s() returned %d",
1511       GST_DEBUG_FUNCPTR_NAME (bclass->check_get_range), (gint) res);
1512
1513   return res;
1514
1515   /* ERRORS */
1516 no_function:
1517   {
1518     GST_WARNING_OBJECT (src, "no check_get_range function set");
1519     return FALSE;
1520   }
1521 }
1522
1523 static gboolean
1524 gst_base_src_pad_check_get_range (GstPad * pad)
1525 {
1526   GstBaseSrc *src;
1527   gboolean res;
1528
1529   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1530
1531   res = gst_base_src_check_get_range (src);
1532
1533   gst_object_unref (src);
1534
1535   return res;
1536 }
1537
1538 static void
1539 gst_base_src_loop (GstPad * pad)
1540 {
1541   GstBaseSrc *src;
1542   GstBuffer *buf = NULL;
1543   GstFlowReturn ret;
1544   gint64 position;
1545   gboolean eos;
1546
1547   eos = FALSE;
1548
1549   src = GST_BASE_SRC (gst_pad_get_parent (pad));
1550
1551   src->priv->last_sent_eos = FALSE;
1552
1553   /* if we operate in bytes, we can calculate an offset */
1554   if (src->segment.format == GST_FORMAT_BYTES)
1555     position = src->segment.last_stop;
1556   else
1557     position = -1;
1558
1559   ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
1560   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1561     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
1562         gst_flow_get_name (ret));
1563     goto pause;
1564   }
1565   /* this should not happen */
1566   if (G_UNLIKELY (buf == NULL))
1567     goto null_buffer;
1568
1569   /* push events to close/start our segment before we push the buffer. */
1570   if (src->priv->close_segment) {
1571     gst_pad_push_event (pad, src->priv->close_segment);
1572     src->priv->close_segment = NULL;
1573   }
1574   if (src->priv->start_segment) {
1575     gst_pad_push_event (pad, src->priv->start_segment);
1576     src->priv->start_segment = NULL;
1577   }
1578
1579   /* figure out the new position */
1580   switch (src->segment.format) {
1581     case GST_FORMAT_BYTES:
1582       position += GST_BUFFER_SIZE (buf);
1583       break;
1584     case GST_FORMAT_TIME:
1585     {
1586       GstClockTime start, duration;
1587
1588       start = GST_BUFFER_TIMESTAMP (buf);
1589       duration = GST_BUFFER_DURATION (buf);
1590
1591       if (GST_CLOCK_TIME_IS_VALID (start))
1592         position = start;
1593       else
1594         position = src->segment.last_stop;
1595
1596       if (GST_CLOCK_TIME_IS_VALID (duration))
1597         position += duration;
1598       break;
1599     }
1600     case GST_FORMAT_DEFAULT:
1601       position = GST_BUFFER_OFFSET_END (buf);
1602       break;
1603     default:
1604       position = -1;
1605       break;
1606   }
1607   if (position != -1) {
1608     if (src->segment.stop != -1) {
1609       if (position >= src->segment.stop) {
1610         eos = TRUE;
1611         position = src->segment.stop;
1612       }
1613     }
1614     gst_segment_set_last_stop (&src->segment, src->segment.format, position);
1615   }
1616
1617   if (G_UNLIKELY (src->priv->discont)) {
1618     buf = gst_buffer_make_metadata_writable (buf);
1619     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1620     src->priv->discont = FALSE;
1621   }
1622
1623   ret = gst_pad_push (pad, buf);
1624   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1625     GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
1626         gst_flow_get_name (ret));
1627     goto pause;
1628   }
1629
1630   if (eos) {
1631     GST_INFO_OBJECT (src, "pausing after EOS");
1632     ret = GST_FLOW_UNEXPECTED;
1633     goto pause;
1634   }
1635
1636 done:
1637   gst_object_unref (src);
1638   return;
1639
1640   /* special cases */
1641 pause:
1642   {
1643     const gchar *reason = gst_flow_get_name (ret);
1644
1645     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1646     src->data.ABI.running = FALSE;
1647     gst_pad_pause_task (pad);
1648     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1649       if (ret == GST_FLOW_UNEXPECTED) {
1650         /* perform EOS logic */
1651         if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1652           gst_element_post_message (GST_ELEMENT_CAST (src),
1653               gst_message_new_segment_done (GST_OBJECT_CAST (src),
1654                   src->segment.format, src->segment.last_stop));
1655         } else {
1656           gst_pad_push_event (pad, gst_event_new_eos ());
1657           src->priv->last_sent_eos = TRUE;
1658         }
1659       } else {
1660         /* for fatal errors we post an error message, post the error
1661          * first so the app knows about the error first. */
1662         GST_ELEMENT_ERROR (src, STREAM, FAILED,
1663             (_("Internal data flow error.")),
1664             ("streaming task paused, reason %s (%d)", reason, ret));
1665         gst_pad_push_event (pad, gst_event_new_eos ());
1666         src->priv->last_sent_eos = TRUE;
1667       }
1668     }
1669     goto done;
1670   }
1671 null_buffer:
1672   {
1673     GST_ELEMENT_ERROR (src, STREAM, FAILED,
1674         (_("Internal data flow error.")), ("element returned NULL buffer"));
1675     /* we finished the segment on error */
1676     src->data.ABI.running = FALSE;
1677     gst_pad_pause_task (pad);
1678     gst_pad_push_event (pad, gst_event_new_eos ());
1679     src->priv->last_sent_eos = TRUE;
1680     goto done;
1681   }
1682 }
1683
1684 /* this will always be called between start() and stop(). So you can rely on
1685  * resources allocated by start() and freed from stop(). This needs to be added
1686  * to the docs at some point. */
1687 static gboolean
1688 gst_base_src_unlock (GstBaseSrc * basesrc)
1689 {
1690   GstBaseSrcClass *bclass;
1691   gboolean result = TRUE;
1692
1693   GST_DEBUG ("unlock");
1694   /* unblock whatever the subclass is doing */
1695   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1696   if (bclass->unlock)
1697     result = bclass->unlock (basesrc);
1698
1699   GST_DEBUG ("unschedule clock");
1700   /* and unblock the clock as well, if any */
1701   GST_OBJECT_LOCK (basesrc);
1702   if (basesrc->clock_id) {
1703     gst_clock_id_unschedule (basesrc->clock_id);
1704   }
1705   GST_OBJECT_UNLOCK (basesrc);
1706
1707   GST_DEBUG ("unlock done");
1708
1709   return result;
1710 }
1711
1712 /* this will always be called between start() and stop(). So you can rely on
1713  * resources allocated by start() and freed from stop(). This needs to be added
1714  * to the docs at some point. */
1715 static gboolean
1716 gst_base_src_unlock_stop (GstBaseSrc * basesrc)
1717 {
1718   GstBaseSrcClass *bclass;
1719   gboolean result = TRUE;
1720
1721   GST_DEBUG_OBJECT (basesrc, "unlock stop");
1722
1723   /* Finish a previous unblock request, allowing subclasses to flush command
1724    * queues or whatever they need to do */
1725   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1726   if (bclass->unlock_stop)
1727     result = bclass->unlock_stop (basesrc);
1728
1729   GST_DEBUG_OBJECT (basesrc, "unlock stop done");
1730
1731   return result;
1732 }
1733
1734 /* default negotiation code. 
1735  *
1736  * Take intersection between src and sink pads, take first
1737  * caps and fixate. 
1738  */
1739 static gboolean
1740 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
1741 {
1742   GstCaps *thiscaps;
1743   GstCaps *caps = NULL;
1744   GstCaps *peercaps = NULL;
1745   gboolean result = FALSE;
1746
1747   /* first see what is possible on our source pad */
1748   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
1749   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1750   /* nothing or anything is allowed, we're done */
1751   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1752     goto no_nego_needed;
1753
1754   /* get the peer caps */
1755   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
1756   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1757   if (peercaps) {
1758     GstCaps *icaps;
1759
1760     /* get intersection */
1761     icaps = gst_caps_intersect (thiscaps, peercaps);
1762     GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
1763     gst_caps_unref (thiscaps);
1764     gst_caps_unref (peercaps);
1765     if (icaps) {
1766       /* take first (and best, since they are sorted) possibility */
1767       caps = gst_caps_copy_nth (icaps, 0);
1768       gst_caps_unref (icaps);
1769     }
1770   } else {
1771     /* no peer, work with our own caps then */
1772     caps = thiscaps;
1773   }
1774   if (caps) {
1775     caps = gst_caps_make_writable (caps);
1776     gst_caps_truncate (caps);
1777
1778     /* now fixate */
1779     if (!gst_caps_is_empty (caps)) {
1780       gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
1781       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1782
1783       if (gst_caps_is_any (caps)) {
1784         /* hmm, still anything, so element can do anything and
1785          * nego is not needed */
1786         result = TRUE;
1787       } else if (gst_caps_is_fixed (caps)) {
1788         /* yay, fixed caps, use those then */
1789         gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1790         result = TRUE;
1791       }
1792     }
1793     gst_caps_unref (caps);
1794   }
1795   return result;
1796
1797 no_nego_needed:
1798   {
1799     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1800     if (thiscaps)
1801       gst_caps_unref (thiscaps);
1802     return TRUE;
1803   }
1804 }
1805
1806 static gboolean
1807 gst_base_src_negotiate (GstBaseSrc * basesrc)
1808 {
1809   GstBaseSrcClass *bclass;
1810   gboolean result = TRUE;
1811
1812   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1813
1814   if (bclass->negotiate)
1815     result = bclass->negotiate (basesrc);
1816
1817   return result;
1818 }
1819
1820 static gboolean
1821 gst_base_src_start (GstBaseSrc * basesrc)
1822 {
1823   GstBaseSrcClass *bclass;
1824   gboolean result;
1825   guint64 size;
1826
1827   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1828     return TRUE;
1829
1830   GST_DEBUG_OBJECT (basesrc, "starting source");
1831
1832   basesrc->num_buffers_left = basesrc->num_buffers;
1833
1834   gst_segment_init (&basesrc->segment, basesrc->segment.format);
1835   basesrc->data.ABI.running = FALSE;
1836
1837   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1838   if (bclass->start)
1839     result = bclass->start (basesrc);
1840   else
1841     result = TRUE;
1842
1843   if (!result)
1844     goto could_not_start;
1845
1846   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
1847
1848   /* figure out the size */
1849   if (basesrc->segment.format == GST_FORMAT_BYTES) {
1850     if (bclass->get_size) {
1851       if (!(result = bclass->get_size (basesrc, &size)))
1852         size = -1;
1853     } else {
1854       result = FALSE;
1855       size = -1;
1856     }
1857     /* only update the size when operating in bytes, subclass is supposed
1858      * to set duration in the start method for other formats */
1859     gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
1860   } else {
1861     size = -1;
1862   }
1863
1864   GST_DEBUG_OBJECT (basesrc,
1865       "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
1866       G_GINT64_FORMAT, basesrc->segment.format, result, size,
1867       basesrc->segment.duration);
1868
1869   /* check if we can seek */
1870   if (bclass->is_seekable)
1871     basesrc->seekable = bclass->is_seekable (basesrc);
1872   else
1873     basesrc->seekable = FALSE;
1874
1875   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
1876
1877   /* update for random access flag */
1878   basesrc->random_access = basesrc->seekable &&
1879       basesrc->segment.format == GST_FORMAT_BYTES;
1880
1881   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
1882
1883   /* run typefind if we are random_access and the typefinding is enabled. */
1884   if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
1885     GstCaps *caps;
1886
1887     caps = gst_type_find_helper (basesrc->srcpad, size);
1888     gst_pad_set_caps (basesrc->srcpad, caps);
1889     gst_caps_unref (caps);
1890   } else {
1891     /* use class or default negotiate function */
1892     if (!gst_base_src_negotiate (basesrc))
1893       goto could_not_negotiate;
1894   }
1895
1896   return TRUE;
1897
1898   /* ERROR */
1899 could_not_start:
1900   {
1901     GST_DEBUG_OBJECT (basesrc, "could not start");
1902     /* subclass is supposed to post a message. We don't have to call _stop. */
1903     return FALSE;
1904   }
1905 could_not_negotiate:
1906   {
1907     GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
1908     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
1909         ("Could not negotiate format"), ("Check your filtered caps, if any"));
1910     /* we must call stop */
1911     gst_base_src_stop (basesrc);
1912     return FALSE;
1913   }
1914 }
1915
1916 static gboolean
1917 gst_base_src_stop (GstBaseSrc * basesrc)
1918 {
1919   GstBaseSrcClass *bclass;
1920   gboolean result = TRUE;
1921
1922   if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
1923     return TRUE;
1924
1925   GST_DEBUG_OBJECT (basesrc, "stopping source");
1926
1927   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
1928   if (bclass->stop)
1929     result = bclass->stop (basesrc);
1930
1931   if (result)
1932     GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
1933
1934   return result;
1935 }
1936
1937 static gboolean
1938 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
1939 {
1940   gboolean result;
1941
1942   GST_LIVE_LOCK (basesrc);
1943   basesrc->live_running = TRUE;
1944   GST_LIVE_SIGNAL (basesrc);
1945   GST_LIVE_UNLOCK (basesrc);
1946
1947   /* step 1, unblock clock sync (if any) */
1948   result = gst_base_src_unlock (basesrc);
1949
1950   /* step 2, make sure streaming finishes */
1951   result &= gst_pad_stop_task (pad);
1952
1953   /* step 3, clear the unblock condition */
1954   result &= gst_base_src_unlock_stop (basesrc);
1955
1956   return result;
1957 }
1958
1959 static gboolean
1960 gst_base_src_activate_push (GstPad * pad, gboolean active)
1961 {
1962   GstBaseSrc *basesrc;
1963   GstEvent *event;
1964
1965   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1966
1967   /* prepare subclass first */
1968   if (active) {
1969     GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1970
1971     if (G_UNLIKELY (!basesrc->can_activate_push))
1972       goto no_push_activation;
1973
1974     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
1975       goto error_start;
1976
1977     basesrc->priv->last_sent_eos = FALSE;
1978
1979     /* do initial seek, which will start the task */
1980     GST_OBJECT_LOCK (basesrc);
1981     event = basesrc->data.ABI.pending_seek;
1982     basesrc->data.ABI.pending_seek = NULL;
1983     GST_OBJECT_UNLOCK (basesrc);
1984
1985     /* no need to unlock anything, the task is certainly
1986      * not running here. The perform seek code will start the task when
1987      * finished. */
1988     if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
1989       goto seek_failed;
1990
1991     if (event)
1992       gst_event_unref (event);
1993   } else {
1994     GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1995     /* call the unlock function and stop the task */
1996     if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
1997       goto deactivate_failed;
1998
1999     /* now we can stop the source */
2000     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2001       goto error_stop;
2002   }
2003   return TRUE;
2004
2005   /* ERRORS */
2006 no_push_activation:
2007   {
2008     GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
2009     return FALSE;
2010   }
2011 error_start:
2012   {
2013     GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
2014     return FALSE;
2015   }
2016 seek_failed:
2017   {
2018     GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
2019     gst_base_src_stop (basesrc);
2020     if (event)
2021       gst_event_unref (event);
2022     return FALSE;
2023   }
2024 deactivate_failed:
2025   {
2026     GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
2027     return FALSE;
2028   }
2029 error_stop:
2030   {
2031     GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
2032     return FALSE;
2033   }
2034 }
2035
2036 static gboolean
2037 gst_base_src_activate_pull (GstPad * pad, gboolean active)
2038 {
2039   GstBaseSrc *basesrc;
2040
2041   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2042
2043   /* prepare subclass first */
2044   if (active) {
2045     GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
2046     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
2047       goto error_start;
2048
2049     /* if not random_access, we cannot operate in pull mode for now */
2050     if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
2051       goto no_get_range;
2052   } else {
2053     GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
2054     /* call the unlock function. We have no task to stop. */
2055     if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
2056       goto deactivate_failed;
2057
2058     /* don't send EOS when going from PAUSED => READY when in pull mode */
2059     basesrc->priv->last_sent_eos = TRUE;
2060
2061     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
2062       goto error_stop;
2063   }
2064   return TRUE;
2065
2066   /* ERRORS */
2067 error_start:
2068   {
2069     GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
2070     return FALSE;
2071   }
2072 no_get_range:
2073   {
2074     GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
2075     gst_base_src_stop (basesrc);
2076     return FALSE;
2077   }
2078 deactivate_failed:
2079   {
2080     GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
2081     return FALSE;
2082   }
2083 error_stop:
2084   {
2085     GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
2086     return FALSE;
2087   }
2088 }
2089
2090 static GstStateChangeReturn
2091 gst_base_src_change_state (GstElement * element, GstStateChange transition)
2092 {
2093   GstBaseSrc *basesrc;
2094   GstStateChangeReturn result;
2095   gboolean no_preroll = FALSE;
2096
2097   basesrc = GST_BASE_SRC (element);
2098
2099   switch (transition) {
2100     case GST_STATE_CHANGE_NULL_TO_READY:
2101       break;
2102     case GST_STATE_CHANGE_READY_TO_PAUSED:
2103       GST_LIVE_LOCK (element);
2104       if (basesrc->is_live) {
2105         no_preroll = TRUE;
2106         basesrc->live_running = FALSE;
2107       }
2108       basesrc->priv->last_sent_eos = FALSE;
2109       basesrc->priv->discont = TRUE;
2110       GST_LIVE_UNLOCK (element);
2111       break;
2112     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2113       GST_LIVE_LOCK (element);
2114       if (basesrc->is_live) {
2115         basesrc->live_running = TRUE;
2116         GST_LIVE_SIGNAL (element);
2117       }
2118       GST_LIVE_UNLOCK (element);
2119       break;
2120     default:
2121       break;
2122   }
2123
2124   if ((result =
2125           GST_ELEMENT_CLASS (parent_class)->change_state (element,
2126               transition)) == GST_STATE_CHANGE_FAILURE)
2127     goto failure;
2128
2129   switch (transition) {
2130     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2131       GST_LIVE_LOCK (element);
2132       if (basesrc->is_live) {
2133         no_preroll = TRUE;
2134         basesrc->live_running = FALSE;
2135       }
2136       GST_LIVE_UNLOCK (element);
2137       break;
2138     case GST_STATE_CHANGE_PAUSED_TO_READY:
2139     {
2140       GstEvent **event_p;
2141
2142       /* FIXME, deprecate this behaviour, it is very dangerous.
2143        * the prefered way of sending EOS downstream is by sending
2144        * the EOS event to the element */
2145       if (!basesrc->priv->last_sent_eos) {
2146         GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
2147         gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
2148         basesrc->priv->last_sent_eos = TRUE;
2149       }
2150       event_p = &basesrc->data.ABI.pending_seek;
2151       gst_event_replace (event_p, NULL);
2152       event_p = &basesrc->priv->close_segment;
2153       gst_event_replace (event_p, NULL);
2154       event_p = &basesrc->priv->start_segment;
2155       gst_event_replace (event_p, NULL);
2156       break;
2157     }
2158     case GST_STATE_CHANGE_READY_TO_NULL:
2159       break;
2160     default:
2161       break;
2162   }
2163
2164   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
2165     result = GST_STATE_CHANGE_NO_PREROLL;
2166
2167   return result;
2168
2169   /* ERRORS */
2170 failure:
2171   {
2172     GST_DEBUG_OBJECT (basesrc, "parent failed state change");
2173     return result;
2174   }
2175 }