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