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