docs: annotate C examples as such
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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 generic 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_SEGMENT
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  * If all the conditions are met for operating in pull mode, #GstBaseSrc is
51  * automatically seekable in push mode as well. The following conditions must
52  * be met to make the element seekable in push mode when the format is not
53  * %GST_FORMAT_BYTES:
54  * <itemizedlist>
55  *   <listitem><para>
56  *     #GstBaseSrcClass.is_seekable() returns %TRUE.
57  *   </para></listitem>
58  *   <listitem><para>
59  *     #GstBaseSrcClass.query() can convert all supported seek formats to the
60  *     internal format as set with gst_base_src_set_format().
61  *   </para></listitem>
62  *   <listitem><para>
63  *     #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
64  *      %TRUE.
65  *   </para></listitem>
66  * </itemizedlist>
67  *
68  * When the element does not meet the requirements to operate in pull mode, the
69  * offset and length in the #GstBaseSrcClass.create() method should be ignored.
70  * It is recommended to subclass #GstPushSrc instead, in this situation. If the
71  * element can operate in pull mode but only with specific offsets and
72  * lengths, it is allowed to generate an error when the wrong values are passed
73  * to the #GstBaseSrcClass.create() function.
74  *
75  * #GstBaseSrc has support for live sources. Live sources are sources that when
76  * paused discard data, such as audio or video capture devices. A typical live
77  * source also produces data at a fixed rate and thus provides a clock to publish
78  * this rate.
79  * Use gst_base_src_set_live() to activate the live source mode.
80  *
81  * A live source does not produce data in the PAUSED state. This means that the
82  * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
83  * PLAYING. To signal the pipeline that the element will not produce data, the
84  * return value from the READY to PAUSED state will be
85  * %GST_STATE_CHANGE_NO_PREROLL.
86  *
87  * A typical live source will timestamp the buffers it creates with the
88  * current running time of the pipeline. This is one reason why a live source
89  * can only produce data in the PLAYING state, when the clock is actually
90  * distributed and running.
91  *
92  * Live sources that synchronize and block on the clock (an audio source, for
93  * example) can use gst_base_src_wait_playing() when the
94  * #GstBaseSrcClass.create() function was interrupted by a state change to
95  * PAUSED.
96  *
97  * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
98  * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
99  * function if the source is a live source. The #GstBaseSrcClass.get_times()
100  * function should return timestamps starting from 0, as if it were a non-live
101  * source. The base class will make sure that the timestamps are transformed
102  * into the current running_time. The base source will then wait for the
103  * calculated running_time before pushing out the buffer.
104  *
105  * For live sources, the base class will by default report a latency of 0.
106  * For pseudo live sources, the base class will by default measure the difference
107  * between the first buffer timestamp and the start time of get_times and will
108  * report this value as the latency.
109  * Subclasses should override the query function when this behaviour is not
110  * acceptable.
111  *
112  * There is only support in #GstBaseSrc for exactly one source pad, which
113  * should be named "src". A source implementation (subclass of #GstBaseSrc)
114  * should install a pad template in its class_init function, like so:
115  * |[<!-- language="C" -->
116  * static void
117  * my_element_class_init (GstMyElementClass *klass)
118  * {
119  *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
120  *   // srctemplate should be a #GstStaticPadTemplate with direction
121  *   // %GST_PAD_SRC and name "src"
122  *   gst_element_class_add_pad_template (gstelement_class,
123  *       gst_static_pad_template_get (&amp;srctemplate));
124  *
125  *   gst_element_class_set_static_metadata (gstelement_class,
126  *      "Source name",
127  *      "Source",
128  *      "My Source element",
129  *      "The author &lt;my.sink@my.email&gt;");
130  * }
131  * ]|
132  *
133  * <refsect2>
134  * <title>Controlled shutdown of live sources in applications</title>
135  * <para>
136  * Applications that record from a live source may want to stop recording
137  * in a controlled way, so that the recording is stopped, but the data
138  * already in the pipeline is processed to the end (remember that many live
139  * sources would go on recording forever otherwise). For that to happen the
140  * application needs to make the source stop recording and send an EOS
141  * event down the pipeline. The application would then wait for an
142  * EOS message posted on the pipeline's bus to know when all data has
143  * been processed and the pipeline can safely be stopped.
144  *
145  * An application may send an EOS event to a source element to make it
146  * perform the EOS logic (send EOS event downstream or post a
147  * %GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
148  * with the gst_element_send_event() function on the element or its parent bin.
149  *
150  * After the EOS has been sent to the element, the application should wait for
151  * an EOS message to be posted on the pipeline's bus. Once this EOS message is
152  * received, it may safely shut down the entire pipeline.
153  * </para>
154  * </refsect2>
155  */
156
157 #ifdef HAVE_CONFIG_H
158 #  include "config.h"
159 #endif
160
161 #include <stdlib.h>
162 #include <string.h>
163
164 #include <gst/gst_private.h>
165 #include <gst/glib-compat-private.h>
166
167 #include "gstbasesrc.h"
168 #include "gsttypefindhelper.h"
169 #include <gst/gst-i18n-lib.h>
170
171 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
172 #define GST_CAT_DEFAULT gst_base_src_debug
173
174 #define GST_LIVE_GET_LOCK(elem)               (&GST_BASE_SRC_CAST(elem)->live_lock)
175 #define GST_LIVE_LOCK(elem)                   g_mutex_lock(GST_LIVE_GET_LOCK(elem))
176 #define GST_LIVE_TRYLOCK(elem)                g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
177 #define GST_LIVE_UNLOCK(elem)                 g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
178 #define GST_LIVE_GET_COND(elem)               (&GST_BASE_SRC_CAST(elem)->live_cond)
179 #define GST_LIVE_WAIT(elem)                   g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
180 #define GST_LIVE_WAIT_UNTIL(elem, end_time)   g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem), end_time)
181 #define GST_LIVE_SIGNAL(elem)                 g_cond_signal (GST_LIVE_GET_COND (elem));
182 #define GST_LIVE_BROADCAST(elem)              g_cond_broadcast (GST_LIVE_GET_COND (elem));
183
184
185 #define GST_ASYNC_GET_COND(elem)              (&GST_BASE_SRC_CAST(elem)->priv->async_cond)
186 #define GST_ASYNC_WAIT(elem)                  g_cond_wait (GST_ASYNC_GET_COND (elem), GST_OBJECT_GET_LOCK (elem))
187 #define GST_ASYNC_SIGNAL(elem)                g_cond_signal (GST_ASYNC_GET_COND (elem));
188
189 #define CLEAR_PENDING_EOS(bsrc) \
190   G_STMT_START { \
191     g_atomic_int_set (&bsrc->priv->has_pending_eos, FALSE); \
192     gst_event_replace (&bsrc->priv->pending_eos, NULL); \
193   } G_STMT_END
194
195
196 /* BaseSrc signals and args */
197 enum
198 {
199   /* FILL ME */
200   LAST_SIGNAL
201 };
202
203 #define DEFAULT_BLOCKSIZE       4096
204 #define DEFAULT_NUM_BUFFERS     -1
205 #define DEFAULT_TYPEFIND        FALSE
206 #define DEFAULT_DO_TIMESTAMP    FALSE
207
208 enum
209 {
210   PROP_0,
211   PROP_BLOCKSIZE,
212   PROP_NUM_BUFFERS,
213   PROP_TYPEFIND,
214   PROP_DO_TIMESTAMP
215 };
216
217 #define GST_BASE_SRC_GET_PRIVATE(obj)  \
218    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))
219
220 struct _GstBaseSrcPrivate
221 {
222   gboolean discont;
223   gboolean flushing;
224
225   GstFlowReturn start_result;
226   gboolean async;
227
228   /* if a stream-start event should be sent */
229   gboolean stream_start_pending;
230
231   /* if segment should be sent and a
232    * seqnum if it was originated by a seek */
233   gboolean segment_pending;
234   guint32 segment_seqnum;
235
236   /* if EOS is pending (atomic) */
237   GstEvent *pending_eos;
238   gint has_pending_eos;
239
240   /* if the eos was caused by a forced eos from the application */
241   gboolean forced_eos;
242
243   /* startup latency is the time it takes between going to PLAYING and producing
244    * the first BUFFER with running_time 0. This value is included in the latency
245    * reporting. */
246   GstClockTime latency;
247   /* timestamp offset, this is the offset add to the values of gst_times for
248    * pseudo live sources */
249   GstClockTimeDiff ts_offset;
250
251   gboolean do_timestamp;
252   volatile gint dynamic_size;
253   volatile gint automatic_eos;
254
255   /* stream sequence number */
256   guint32 seqnum;
257
258   /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
259    * pushed in the data stream */
260   GList *pending_events;
261   volatile gint have_events;
262
263   /* QoS *//* with LOCK */
264   gboolean qos_enabled;
265   gdouble proportion;
266   GstClockTime earliest_time;
267
268   GstBufferPool *pool;
269   GstAllocator *allocator;
270   GstAllocationParams params;
271
272   GCond async_cond;
273 };
274
275 static GstElementClass *parent_class = NULL;
276
277 static void gst_base_src_class_init (GstBaseSrcClass * klass);
278 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
279 static void gst_base_src_finalize (GObject * object);
280
281
282 GType
283 gst_base_src_get_type (void)
284 {
285   static volatile gsize base_src_type = 0;
286
287   if (g_once_init_enter (&base_src_type)) {
288     GType _type;
289     static const GTypeInfo base_src_info = {
290       sizeof (GstBaseSrcClass),
291       NULL,
292       NULL,
293       (GClassInitFunc) gst_base_src_class_init,
294       NULL,
295       NULL,
296       sizeof (GstBaseSrc),
297       0,
298       (GInstanceInitFunc) gst_base_src_init,
299     };
300
301     _type = g_type_register_static (GST_TYPE_ELEMENT,
302         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
303     g_once_init_leave (&base_src_type, _type);
304   }
305   return base_src_type;
306 }
307
308 static GstCaps *gst_base_src_default_get_caps (GstBaseSrc * bsrc,
309     GstCaps * filter);
310 static GstCaps *gst_base_src_default_fixate (GstBaseSrc * src, GstCaps * caps);
311 static GstCaps *gst_base_src_fixate (GstBaseSrc * src, GstCaps * caps);
312
313 static gboolean gst_base_src_is_random_access (GstBaseSrc * src);
314 static gboolean gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
315     GstPadMode mode, gboolean active);
316 static void gst_base_src_set_property (GObject * object, guint prop_id,
317     const GValue * value, GParamSpec * pspec);
318 static void gst_base_src_get_property (GObject * object, guint prop_id,
319     GValue * value, GParamSpec * pspec);
320 static gboolean gst_base_src_event (GstPad * pad, GstObject * parent,
321     GstEvent * event);
322 static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
323 static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
324
325 static gboolean gst_base_src_query (GstPad * pad, GstObject * parent,
326     GstQuery * query);
327
328 static gboolean gst_base_src_activate_pool (GstBaseSrc * basesrc,
329     gboolean active);
330 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
331 static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
332     GstSegment * segment);
333 static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
334 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
335     GstEvent * event, GstSegment * segment);
336 static GstFlowReturn gst_base_src_default_create (GstBaseSrc * basesrc,
337     guint64 offset, guint size, GstBuffer ** buf);
338 static GstFlowReturn gst_base_src_default_alloc (GstBaseSrc * basesrc,
339     guint64 offset, guint size, GstBuffer ** buf);
340 static gboolean gst_base_src_decide_allocation_default (GstBaseSrc * basesrc,
341     GstQuery * query);
342
343 static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
344     gboolean flushing, gboolean live_play, gboolean * playing);
345
346 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
347 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
348
349 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
350     GstStateChange transition);
351
352 static void gst_base_src_loop (GstPad * pad);
353 static GstFlowReturn gst_base_src_getrange (GstPad * pad, GstObject * parent,
354     guint64 offset, guint length, GstBuffer ** buf);
355 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
356     guint length, GstBuffer ** buf);
357 static gboolean gst_base_src_seekable (GstBaseSrc * src);
358 static gboolean gst_base_src_negotiate (GstBaseSrc * basesrc);
359 static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
360     guint * length, gboolean force);
361
362 static void
363 gst_base_src_class_init (GstBaseSrcClass * klass)
364 {
365   GObjectClass *gobject_class;
366   GstElementClass *gstelement_class;
367
368   gobject_class = G_OBJECT_CLASS (klass);
369   gstelement_class = GST_ELEMENT_CLASS (klass);
370
371   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
372
373   g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));
374
375   parent_class = g_type_class_peek_parent (klass);
376
377   gobject_class->finalize = gst_base_src_finalize;
378   gobject_class->set_property = gst_base_src_set_property;
379   gobject_class->get_property = gst_base_src_get_property;
380
381   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
382       g_param_spec_uint ("blocksize", "Block size",
383           "Size in bytes to read per buffer (-1 = default)", 0, G_MAXUINT,
384           DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385   g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
386       g_param_spec_int ("num-buffers", "num-buffers",
387           "Number of buffers to output before sending EOS (-1 = unlimited)",
388           -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
389           G_PARAM_STATIC_STRINGS));
390   g_object_class_install_property (gobject_class, PROP_TYPEFIND,
391       g_param_spec_boolean ("typefind", "Typefind",
392           "Run typefind before negotiating", DEFAULT_TYPEFIND,
393           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394   g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
395       g_param_spec_boolean ("do-timestamp", "Do timestamp",
396           "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
397           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398
399   gstelement_class->change_state =
400       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
401   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
402
403   klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_src_default_get_caps);
404   klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
405   klass->fixate = GST_DEBUG_FUNCPTR (gst_base_src_default_fixate);
406   klass->prepare_seek_segment =
407       GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
408   klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
409   klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
410   klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
411   klass->create = GST_DEBUG_FUNCPTR (gst_base_src_default_create);
412   klass->alloc = GST_DEBUG_FUNCPTR (gst_base_src_default_alloc);
413   klass->decide_allocation =
414       GST_DEBUG_FUNCPTR (gst_base_src_decide_allocation_default);
415
416   /* Registering debug symbols for function pointers */
417   GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_mode);
418   GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event);
419   GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
420   GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getrange);
421   GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
422 }
423
424 static void
425 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
426 {
427   GstPad *pad;
428   GstPadTemplate *pad_template;
429
430   basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);
431
432   basesrc->is_live = FALSE;
433   g_mutex_init (&basesrc->live_lock);
434   g_cond_init (&basesrc->live_cond);
435   basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
436   basesrc->num_buffers_left = -1;
437   basesrc->priv->automatic_eos = TRUE;
438
439   basesrc->can_activate_push = TRUE;
440
441   pad_template =
442       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
443   g_return_if_fail (pad_template != NULL);
444
445   GST_DEBUG_OBJECT (basesrc, "creating src pad");
446   pad = gst_pad_new_from_template (pad_template, "src");
447
448   GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
449   gst_pad_set_activatemode_function (pad, gst_base_src_activate_mode);
450   gst_pad_set_event_function (pad, gst_base_src_event);
451   gst_pad_set_query_function (pad, gst_base_src_query);
452   gst_pad_set_getrange_function (pad, gst_base_src_getrange);
453
454   /* hold pointer to pad */
455   basesrc->srcpad = pad;
456   GST_DEBUG_OBJECT (basesrc, "adding src pad");
457   gst_element_add_pad (GST_ELEMENT (basesrc), pad);
458
459   basesrc->blocksize = DEFAULT_BLOCKSIZE;
460   basesrc->clock_id = NULL;
461   /* we operate in BYTES by default */
462   gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
463   basesrc->typefind = DEFAULT_TYPEFIND;
464   basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
465   g_atomic_int_set (&basesrc->priv->have_events, FALSE);
466
467   g_cond_init (&basesrc->priv->async_cond);
468   basesrc->priv->start_result = GST_FLOW_FLUSHING;
469   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
470   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
471   GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_FLAG_SOURCE);
472
473   GST_DEBUG_OBJECT (basesrc, "init done");
474 }
475
476 static void
477 gst_base_src_finalize (GObject * object)
478 {
479   GstBaseSrc *basesrc;
480   GstEvent **event_p;
481
482   basesrc = GST_BASE_SRC (object);
483
484   g_mutex_clear (&basesrc->live_lock);
485   g_cond_clear (&basesrc->live_cond);
486   g_cond_clear (&basesrc->priv->async_cond);
487
488   event_p = &basesrc->pending_seek;
489   gst_event_replace (event_p, NULL);
490
491   if (basesrc->priv->pending_events) {
492     g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
493         NULL);
494     g_list_free (basesrc->priv->pending_events);
495   }
496
497   G_OBJECT_CLASS (parent_class)->finalize (object);
498 }
499
500 /**
501  * gst_base_src_wait_playing:
502  * @src: the src
503  *
504  * If the #GstBaseSrcClass.create() method performs its own synchronisation
505  * against the clock it must unblock when going from PLAYING to the PAUSED state
506  * and call this method before continuing to produce the remaining data.
507  *
508  * This function will block until a state change to PLAYING happens (in which
509  * case this function returns %GST_FLOW_OK) or the processing must be stopped due
510  * to a state change to READY or a FLUSH event (in which case this function
511  * returns %GST_FLOW_FLUSHING).
512  *
513  * Returns: %GST_FLOW_OK if @src is PLAYING and processing can
514  * continue. Any other return value should be returned from the create vmethod.
515  */
516 GstFlowReturn
517 gst_base_src_wait_playing (GstBaseSrc * src)
518 {
519   g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
520
521   do {
522     /* block until the state changes, or we get a flush, or something */
523     GST_DEBUG_OBJECT (src, "live source waiting for running state");
524     GST_LIVE_WAIT (src);
525     GST_DEBUG_OBJECT (src, "live source unlocked");
526     if (src->priv->flushing)
527       goto flushing;
528   } while (G_UNLIKELY (!src->live_running));
529
530   return GST_FLOW_OK;
531
532   /* ERRORS */
533 flushing:
534   {
535     GST_DEBUG_OBJECT (src, "we are flushing");
536     return GST_FLOW_FLUSHING;
537   }
538 }
539
540 /**
541  * gst_base_src_set_live:
542  * @src: base source instance
543  * @live: new live-mode
544  *
545  * If the element listens to a live source, @live should
546  * be set to %TRUE.
547  *
548  * A live source will not produce data in the PAUSED state and
549  * will therefore not be able to participate in the PREROLL phase
550  * of a pipeline. To signal this fact to the application and the
551  * pipeline, the state change return value of the live source will
552  * be GST_STATE_CHANGE_NO_PREROLL.
553  */
554 void
555 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
556 {
557   g_return_if_fail (GST_IS_BASE_SRC (src));
558
559   GST_OBJECT_LOCK (src);
560   src->is_live = live;
561   GST_OBJECT_UNLOCK (src);
562 }
563
564 /**
565  * gst_base_src_is_live:
566  * @src: base source instance
567  *
568  * Check if an element is in live mode.
569  *
570  * Returns: %TRUE if element is in live mode.
571  */
572 gboolean
573 gst_base_src_is_live (GstBaseSrc * src)
574 {
575   gboolean result;
576
577   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
578
579   GST_OBJECT_LOCK (src);
580   result = src->is_live;
581   GST_OBJECT_UNLOCK (src);
582
583   return result;
584 }
585
586 /**
587  * gst_base_src_set_format:
588  * @src: base source instance
589  * @format: the format to use
590  *
591  * Sets the default format of the source. This will be the format used
592  * for sending SEGMENT events and for performing seeks.
593  *
594  * If a format of GST_FORMAT_BYTES is set, the element will be able to
595  * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns %TRUE.
596  *
597  * This function must only be called in states < %GST_STATE_PAUSED.
598  */
599 void
600 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
601 {
602   g_return_if_fail (GST_IS_BASE_SRC (src));
603   g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
604
605   GST_OBJECT_LOCK (src);
606   gst_segment_init (&src->segment, format);
607   GST_OBJECT_UNLOCK (src);
608 }
609
610 /**
611  * gst_base_src_set_dynamic_size:
612  * @src: base source instance
613  * @dynamic: new dynamic size mode
614  *
615  * If not @dynamic, size is only updated when needed, such as when trying to
616  * read past current tracked size.  Otherwise, size is checked for upon each
617  * read.
618  */
619 void
620 gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
621 {
622   g_return_if_fail (GST_IS_BASE_SRC (src));
623
624   g_atomic_int_set (&src->priv->dynamic_size, dynamic);
625 }
626
627 /**
628  * gst_base_src_set_automatic_eos:
629  * @src: base source instance
630  * @automatic_eos: automatic eos
631  *
632  * If @automatic_eos is %TRUE, @src will automatically go EOS if a buffer
633  * after the total size is returned. By default this is %TRUE but sources
634  * that can't return an authoritative size and only know that they're EOS
635  * when trying to read more should set this to %FALSE.
636  *
637  * Since: 1.4
638  */
639 void
640 gst_base_src_set_automatic_eos (GstBaseSrc * src, gboolean automatic_eos)
641 {
642   g_return_if_fail (GST_IS_BASE_SRC (src));
643
644   g_atomic_int_set (&src->priv->automatic_eos, automatic_eos);
645 }
646
647 /**
648  * gst_base_src_set_async:
649  * @src: base source instance
650  * @async: new async mode
651  *
652  * Configure async behaviour in @src, no state change will block. The open,
653  * close, start, stop, play and pause virtual methods will be executed in a
654  * different thread and are thus allowed to perform blocking operations. Any
655  * blocking operation should be unblocked with the unlock vmethod.
656  */
657 void
658 gst_base_src_set_async (GstBaseSrc * src, gboolean async)
659 {
660   g_return_if_fail (GST_IS_BASE_SRC (src));
661
662   GST_OBJECT_LOCK (src);
663   src->priv->async = async;
664   GST_OBJECT_UNLOCK (src);
665 }
666
667 /**
668  * gst_base_src_is_async:
669  * @src: base source instance
670  *
671  * Get the current async behaviour of @src. See also gst_base_src_set_async().
672  *
673  * Returns: %TRUE if @src is operating in async mode.
674  */
675 gboolean
676 gst_base_src_is_async (GstBaseSrc * src)
677 {
678   gboolean res;
679
680   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
681
682   GST_OBJECT_LOCK (src);
683   res = src->priv->async;
684   GST_OBJECT_UNLOCK (src);
685
686   return res;
687 }
688
689
690 /**
691  * gst_base_src_query_latency:
692  * @src: the source
693  * @live: (out) (allow-none): if the source is live
694  * @min_latency: (out) (allow-none): the min latency of the source
695  * @max_latency: (out) (allow-none): the max latency of the source
696  *
697  * Query the source for the latency parameters. @live will be %TRUE when @src is
698  * configured as a live source. @min_latency and @max_latency will be set
699  * to the difference between the running time and the timestamp of the first
700  * buffer.
701  *
702  * This function is mostly used by subclasses.
703  *
704  * Returns: %TRUE if the query succeeded.
705  */
706 gboolean
707 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
708     GstClockTime * min_latency, GstClockTime * max_latency)
709 {
710   GstClockTime min;
711
712   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
713
714   GST_OBJECT_LOCK (src);
715   if (live)
716     *live = src->is_live;
717
718   /* if we have a startup latency, report this one, else report 0. Subclasses
719    * are supposed to override the query function if they want something
720    * else. */
721   if (src->priv->latency != -1)
722     min = src->priv->latency;
723   else
724     min = 0;
725
726   if (min_latency)
727     *min_latency = min;
728   if (max_latency)
729     *max_latency = min;
730
731   GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
732       ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
733       GST_TIME_ARGS (min));
734   GST_OBJECT_UNLOCK (src);
735
736   return TRUE;
737 }
738
739 /**
740  * gst_base_src_set_blocksize:
741  * @src: the source
742  * @blocksize: the new blocksize in bytes
743  *
744  * Set the number of bytes that @src will push out with each buffer. When
745  * @blocksize is set to -1, a default length will be used.
746  */
747 void
748 gst_base_src_set_blocksize (GstBaseSrc * src, guint blocksize)
749 {
750   g_return_if_fail (GST_IS_BASE_SRC (src));
751
752   GST_OBJECT_LOCK (src);
753   src->blocksize = blocksize;
754   GST_OBJECT_UNLOCK (src);
755 }
756
757 /**
758  * gst_base_src_get_blocksize:
759  * @src: the source
760  *
761  * Get the number of bytes that @src will push out with each buffer.
762  *
763  * Returns: the number of bytes pushed with each buffer.
764  */
765 guint
766 gst_base_src_get_blocksize (GstBaseSrc * src)
767 {
768   gint res;
769
770   g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
771
772   GST_OBJECT_LOCK (src);
773   res = src->blocksize;
774   GST_OBJECT_UNLOCK (src);
775
776   return res;
777 }
778
779
780 /**
781  * gst_base_src_set_do_timestamp:
782  * @src: the source
783  * @timestamp: enable or disable timestamping
784  *
785  * Configure @src to automatically timestamp outgoing buffers based on the
786  * current running_time of the pipeline. This property is mostly useful for live
787  * sources.
788  */
789 void
790 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
791 {
792   g_return_if_fail (GST_IS_BASE_SRC (src));
793
794   GST_OBJECT_LOCK (src);
795   src->priv->do_timestamp = timestamp;
796   if (timestamp && src->segment.format != GST_FORMAT_TIME)
797     gst_segment_init (&src->segment, GST_FORMAT_TIME);
798   GST_OBJECT_UNLOCK (src);
799 }
800
801 /**
802  * gst_base_src_get_do_timestamp:
803  * @src: the source
804  *
805  * Query if @src timestamps outgoing buffers based on the current running_time.
806  *
807  * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
808  */
809 gboolean
810 gst_base_src_get_do_timestamp (GstBaseSrc * src)
811 {
812   gboolean res;
813
814   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
815
816   GST_OBJECT_LOCK (src);
817   res = src->priv->do_timestamp;
818   GST_OBJECT_UNLOCK (src);
819
820   return res;
821 }
822
823 /**
824  * gst_base_src_new_seamless_segment:
825  * @src: The source
826  * @start: The new start value for the segment
827  * @stop: Stop value for the new segment
828  * @time: The new time value for the start of the new segment
829  *
830  * Prepare a new seamless segment for emission downstream. This function must
831  * only be called by derived sub-classes, and only from the create() function,
832  * as the stream-lock needs to be held.
833  *
834  * The format for the new segment will be the current format of the source, as
835  * configured with gst_base_src_set_format()
836  *
837  * Returns: %TRUE if preparation of the seamless segment succeeded.
838  */
839 gboolean
840 gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
841     gint64 time)
842 {
843   gboolean res = TRUE;
844
845   GST_OBJECT_LOCK (src);
846
847   src->segment.base = gst_segment_to_running_time (&src->segment,
848       src->segment.format, src->segment.position);
849   src->segment.position = src->segment.start = start;
850   src->segment.stop = stop;
851   src->segment.time = time;
852
853   /* Mark pending segment. Will be sent before next data */
854   src->priv->segment_pending = TRUE;
855   src->priv->segment_seqnum = gst_util_seqnum_next ();
856
857   GST_DEBUG_OBJECT (src,
858       "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
859       GST_TIME_FORMAT " time %" GST_TIME_FORMAT " base %" GST_TIME_FORMAT,
860       GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time),
861       GST_TIME_ARGS (src->segment.base));
862
863   GST_OBJECT_UNLOCK (src);
864
865   src->priv->discont = TRUE;
866   src->running = TRUE;
867
868   return res;
869 }
870
871 static gboolean
872 gst_base_src_send_stream_start (GstBaseSrc * src)
873 {
874   gboolean ret = TRUE;
875
876   if (src->priv->stream_start_pending) {
877     gchar *stream_id;
878     GstEvent *event;
879
880     stream_id =
881         gst_pad_create_stream_id (src->srcpad, GST_ELEMENT_CAST (src), NULL);
882
883     GST_DEBUG_OBJECT (src, "Pushing STREAM_START");
884     event = gst_event_new_stream_start (stream_id);
885     gst_event_set_group_id (event, gst_util_group_id_next ());
886
887     ret = gst_pad_push_event (src->srcpad, event);
888     src->priv->stream_start_pending = FALSE;
889     g_free (stream_id);
890   }
891
892   return ret;
893 }
894
895 /**
896  * gst_base_src_set_caps:
897  * @src: a #GstBaseSrc
898  * @caps: (transfer none): a #GstCaps
899  *
900  * Set new caps on the basesrc source pad.
901  *
902  * Returns: %TRUE if the caps could be set
903  */
904 gboolean
905 gst_base_src_set_caps (GstBaseSrc * src, GstCaps * caps)
906 {
907   GstBaseSrcClass *bclass;
908   gboolean res = TRUE;
909   GstCaps *current_caps;
910
911   bclass = GST_BASE_SRC_GET_CLASS (src);
912
913   gst_base_src_send_stream_start (src);
914
915   current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (src));
916   if (current_caps && gst_caps_is_equal (current_caps, caps)) {
917     GST_DEBUG_OBJECT (src, "New caps equal to old ones: %" GST_PTR_FORMAT,
918         caps);
919     res = TRUE;
920   } else {
921     if (bclass->set_caps)
922       res = bclass->set_caps (src, caps);
923
924     if (res)
925       res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
926   }
927
928   if (current_caps)
929     gst_caps_unref (current_caps);
930
931   return res;
932 }
933
934 static GstCaps *
935 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
936 {
937   GstCaps *caps = NULL;
938   GstPadTemplate *pad_template;
939   GstBaseSrcClass *bclass;
940
941   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
942
943   pad_template =
944       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
945
946   if (pad_template != NULL) {
947     caps = gst_pad_template_get_caps (pad_template);
948
949     if (filter) {
950       GstCaps *intersection;
951
952       intersection =
953           gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
954       gst_caps_unref (caps);
955       caps = intersection;
956     }
957   }
958   return caps;
959 }
960
961 static GstCaps *
962 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
963 {
964   GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
965   return gst_caps_fixate (caps);
966 }
967
968 static GstCaps *
969 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
970 {
971   GstBaseSrcClass *bclass;
972
973   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
974
975   if (bclass->fixate)
976     caps = bclass->fixate (bsrc, caps);
977
978   return caps;
979 }
980
981 static gboolean
982 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
983 {
984   gboolean res;
985
986   switch (GST_QUERY_TYPE (query)) {
987     case GST_QUERY_POSITION:
988     {
989       GstFormat format;
990
991       gst_query_parse_position (query, &format, NULL);
992
993       GST_DEBUG_OBJECT (src, "position query in format %s",
994           gst_format_get_name (format));
995
996       switch (format) {
997         case GST_FORMAT_PERCENT:
998         {
999           gint64 percent;
1000           gint64 position;
1001           gint64 duration;
1002
1003           GST_OBJECT_LOCK (src);
1004           position = src->segment.position;
1005           duration = src->segment.duration;
1006           GST_OBJECT_UNLOCK (src);
1007
1008           if (position != -1 && duration != -1) {
1009             if (position < duration)
1010               percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1011                   duration);
1012             else
1013               percent = GST_FORMAT_PERCENT_MAX;
1014           } else
1015             percent = -1;
1016
1017           gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1018           res = TRUE;
1019           break;
1020         }
1021         default:
1022         {
1023           gint64 position;
1024           GstFormat seg_format;
1025
1026           GST_OBJECT_LOCK (src);
1027           position =
1028               gst_segment_to_stream_time (&src->segment, src->segment.format,
1029               src->segment.position);
1030           seg_format = src->segment.format;
1031           GST_OBJECT_UNLOCK (src);
1032
1033           if (position != -1) {
1034             /* convert to requested format */
1035             res =
1036                 gst_pad_query_convert (src->srcpad, seg_format,
1037                 position, format, &position);
1038           } else
1039             res = TRUE;
1040
1041           if (res)
1042             gst_query_set_position (query, format, position);
1043
1044           break;
1045         }
1046       }
1047       break;
1048     }
1049     case GST_QUERY_DURATION:
1050     {
1051       GstFormat format;
1052
1053       gst_query_parse_duration (query, &format, NULL);
1054
1055       GST_DEBUG_OBJECT (src, "duration query in format %s",
1056           gst_format_get_name (format));
1057
1058       switch (format) {
1059         case GST_FORMAT_PERCENT:
1060           gst_query_set_duration (query, GST_FORMAT_PERCENT,
1061               GST_FORMAT_PERCENT_MAX);
1062           res = TRUE;
1063           break;
1064         default:
1065         {
1066           gint64 duration;
1067           GstFormat seg_format;
1068           guint length = 0;
1069
1070           /* may have to refresh duration */
1071           gst_base_src_update_length (src, 0, &length,
1072               g_atomic_int_get (&src->priv->dynamic_size));
1073
1074           /* this is the duration as configured by the subclass. */
1075           GST_OBJECT_LOCK (src);
1076           duration = src->segment.duration;
1077           seg_format = src->segment.format;
1078           GST_OBJECT_UNLOCK (src);
1079
1080           GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1081               duration, gst_format_get_name (seg_format));
1082
1083           if (duration != -1) {
1084             /* convert to requested format, if this fails, we have a duration
1085              * but we cannot answer the query, we must return FALSE. */
1086             res =
1087                 gst_pad_query_convert (src->srcpad, seg_format,
1088                 duration, format, &duration);
1089           } else {
1090             /* The subclass did not configure a duration, we assume that the
1091              * media has an unknown duration then and we return TRUE to report
1092              * this. Note that this is not the same as returning FALSE, which
1093              * means that we cannot report the duration at all. */
1094             res = TRUE;
1095           }
1096
1097           if (res)
1098             gst_query_set_duration (query, format, duration);
1099
1100           break;
1101         }
1102       }
1103       break;
1104     }
1105
1106     case GST_QUERY_SEEKING:
1107     {
1108       GstFormat format, seg_format;
1109       gint64 duration;
1110
1111       GST_OBJECT_LOCK (src);
1112       duration = src->segment.duration;
1113       seg_format = src->segment.format;
1114       GST_OBJECT_UNLOCK (src);
1115
1116       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1117       if (format == seg_format) {
1118         gst_query_set_seeking (query, seg_format,
1119             gst_base_src_seekable (src), 0, duration);
1120         res = TRUE;
1121       } else {
1122         /* FIXME 2.0: return TRUE + seekable=FALSE for SEEKING query here */
1123         /* Don't reply to the query to make up for demuxers which don't
1124          * handle the SEEKING query yet. Players like Totem will fall back
1125          * to the duration when the SEEKING query isn't answered. */
1126         res = FALSE;
1127       }
1128       break;
1129     }
1130     case GST_QUERY_SEGMENT:
1131     {
1132       GstFormat format;
1133       gint64 start, stop;
1134
1135       GST_OBJECT_LOCK (src);
1136
1137       format = src->segment.format;
1138
1139       start =
1140           gst_segment_to_stream_time (&src->segment, format,
1141           src->segment.start);
1142       if ((stop = src->segment.stop) == -1)
1143         stop = src->segment.duration;
1144       else
1145         stop = gst_segment_to_stream_time (&src->segment, format, stop);
1146
1147       gst_query_set_segment (query, src->segment.rate, format, start, stop);
1148
1149       GST_OBJECT_UNLOCK (src);
1150       res = TRUE;
1151       break;
1152     }
1153
1154     case GST_QUERY_FORMATS:
1155     {
1156       gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1157           GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1158       res = TRUE;
1159       break;
1160     }
1161     case GST_QUERY_CONVERT:
1162     {
1163       GstFormat src_fmt, dest_fmt;
1164       gint64 src_val, dest_val;
1165
1166       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1167
1168       /* we can only convert between equal formats... */
1169       if (src_fmt == dest_fmt) {
1170         dest_val = src_val;
1171         res = TRUE;
1172       } else
1173         res = FALSE;
1174
1175       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1176       break;
1177     }
1178     case GST_QUERY_LATENCY:
1179     {
1180       GstClockTime min, max;
1181       gboolean live;
1182
1183       /* Subclasses should override and implement something useful */
1184       res = gst_base_src_query_latency (src, &live, &min, &max);
1185
1186       GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1187           ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1188           GST_TIME_ARGS (max));
1189
1190       gst_query_set_latency (query, live, min, max);
1191       break;
1192     }
1193     case GST_QUERY_JITTER:
1194     case GST_QUERY_RATE:
1195       res = FALSE;
1196       break;
1197     case GST_QUERY_BUFFERING:
1198     {
1199       GstFormat format, seg_format;
1200       gint64 start, stop, estimated;
1201
1202       gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1203
1204       GST_DEBUG_OBJECT (src, "buffering query in format %s",
1205           gst_format_get_name (format));
1206
1207       GST_OBJECT_LOCK (src);
1208       if (src->random_access) {
1209         estimated = 0;
1210         start = 0;
1211         if (format == GST_FORMAT_PERCENT)
1212           stop = GST_FORMAT_PERCENT_MAX;
1213         else
1214           stop = src->segment.duration;
1215       } else {
1216         estimated = -1;
1217         start = -1;
1218         stop = -1;
1219       }
1220       seg_format = src->segment.format;
1221       GST_OBJECT_UNLOCK (src);
1222
1223       /* convert to required format. When the conversion fails, we can't answer
1224        * the query. When the value is unknown, we can don't perform conversion
1225        * but report TRUE. */
1226       if (format != GST_FORMAT_PERCENT && stop != -1) {
1227         res = gst_pad_query_convert (src->srcpad, seg_format,
1228             stop, format, &stop);
1229       } else {
1230         res = TRUE;
1231       }
1232       if (res && format != GST_FORMAT_PERCENT && start != -1)
1233         res = gst_pad_query_convert (src->srcpad, seg_format,
1234             start, format, &start);
1235
1236       gst_query_set_buffering_range (query, format, start, stop, estimated);
1237       break;
1238     }
1239     case GST_QUERY_SCHEDULING:
1240     {
1241       gboolean random_access;
1242
1243       random_access = gst_base_src_is_random_access (src);
1244
1245       /* we can operate in getrange mode if the native format is bytes
1246        * and we are seekable, this condition is set in the random_access
1247        * flag and is set in the _start() method. */
1248       gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1249       if (random_access)
1250         gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1251       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1252
1253       res = TRUE;
1254       break;
1255     }
1256     case GST_QUERY_CAPS:
1257     {
1258       GstBaseSrcClass *bclass;
1259       GstCaps *caps, *filter;
1260
1261       bclass = GST_BASE_SRC_GET_CLASS (src);
1262       if (bclass->get_caps) {
1263         gst_query_parse_caps (query, &filter);
1264         if ((caps = bclass->get_caps (src, filter))) {
1265           gst_query_set_caps_result (query, caps);
1266           gst_caps_unref (caps);
1267           res = TRUE;
1268         } else {
1269           res = FALSE;
1270         }
1271       } else
1272         res = FALSE;
1273       break;
1274     }
1275     case GST_QUERY_URI:{
1276       if (GST_IS_URI_HANDLER (src)) {
1277         gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1278
1279         if (uri != NULL) {
1280           gst_query_set_uri (query, uri);
1281           g_free (uri);
1282           res = TRUE;
1283         } else {
1284           res = FALSE;
1285         }
1286       } else {
1287         res = FALSE;
1288       }
1289       break;
1290     }
1291     default:
1292       res = FALSE;
1293       break;
1294   }
1295   GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1296       res);
1297
1298   return res;
1299 }
1300
1301 static gboolean
1302 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1303 {
1304   GstBaseSrc *src;
1305   GstBaseSrcClass *bclass;
1306   gboolean result = FALSE;
1307
1308   src = GST_BASE_SRC (parent);
1309   bclass = GST_BASE_SRC_GET_CLASS (src);
1310
1311   if (bclass->query)
1312     result = bclass->query (src, query);
1313
1314   return result;
1315 }
1316
1317 static gboolean
1318 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1319 {
1320   gboolean res = TRUE;
1321
1322   /* update our offset if the start/stop position was updated */
1323   if (segment->format == GST_FORMAT_BYTES) {
1324     segment->time = segment->start;
1325   } else if (segment->start == 0) {
1326     /* seek to start, we can implement a default for this. */
1327     segment->time = 0;
1328   } else {
1329     res = FALSE;
1330     GST_INFO_OBJECT (src, "Can't do a default seek");
1331   }
1332
1333   return res;
1334 }
1335
1336 static gboolean
1337 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1338 {
1339   GstBaseSrcClass *bclass;
1340   gboolean result = FALSE;
1341
1342   bclass = GST_BASE_SRC_GET_CLASS (src);
1343
1344   GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1345
1346   if (bclass->do_seek)
1347     result = bclass->do_seek (src, segment);
1348
1349   return result;
1350 }
1351
1352 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1353
1354 static gboolean
1355 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1356     GstSegment * segment)
1357 {
1358   /* By default, we try one of 2 things:
1359    *   - For absolute seek positions, convert the requested position to our
1360    *     configured processing format and place it in the output segment \
1361    *   - For relative seek positions, convert our current (input) values to the
1362    *     seek format, adjust by the relative seek offset and then convert back to
1363    *     the processing format
1364    */
1365   GstSeekType start_type, stop_type;
1366   gint64 start, stop;
1367   GstSeekFlags flags;
1368   GstFormat seek_format, dest_format;
1369   gdouble rate;
1370   gboolean update;
1371   gboolean res = TRUE;
1372
1373   gst_event_parse_seek (event, &rate, &seek_format, &flags,
1374       &start_type, &start, &stop_type, &stop);
1375   dest_format = segment->format;
1376
1377   if (seek_format == dest_format) {
1378     gst_segment_do_seek (segment, rate, seek_format, flags,
1379         start_type, start, stop_type, stop, &update);
1380     return TRUE;
1381   }
1382
1383   if (start_type != GST_SEEK_TYPE_NONE) {
1384     /* FIXME: Handle seek_end by converting the input segment vals */
1385     res =
1386         gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1387         &start);
1388     start_type = GST_SEEK_TYPE_SET;
1389   }
1390
1391   if (res && stop_type != GST_SEEK_TYPE_NONE) {
1392     /* FIXME: Handle seek_end by converting the input segment vals */
1393     res =
1394         gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1395         &stop);
1396     stop_type = GST_SEEK_TYPE_SET;
1397   }
1398
1399   /* And finally, configure our output segment in the desired format */
1400   gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1401       stop_type, stop, &update);
1402
1403   if (!res)
1404     goto no_format;
1405
1406   return res;
1407
1408 no_format:
1409   {
1410     GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1411     return FALSE;
1412   }
1413 }
1414
1415 static gboolean
1416 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1417     GstSegment * seeksegment)
1418 {
1419   GstBaseSrcClass *bclass;
1420   gboolean result = FALSE;
1421
1422   bclass = GST_BASE_SRC_GET_CLASS (src);
1423
1424   if (bclass->prepare_seek_segment)
1425     result = bclass->prepare_seek_segment (src, event, seeksegment);
1426
1427   return result;
1428 }
1429
1430 static GstFlowReturn
1431 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1432     guint size, GstBuffer ** buffer)
1433 {
1434   GstFlowReturn ret;
1435   GstBaseSrcPrivate *priv = src->priv;
1436
1437   if (priv->pool) {
1438     ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1439   } else if (size != -1) {
1440     *buffer = gst_buffer_new_allocate (priv->allocator, size, &priv->params);
1441     if (G_UNLIKELY (*buffer == NULL))
1442       goto alloc_failed;
1443
1444     ret = GST_FLOW_OK;
1445   } else {
1446     GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1447         size);
1448     goto alloc_failed;
1449   }
1450   return ret;
1451
1452   /* ERRORS */
1453 alloc_failed:
1454   {
1455     GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1456     return GST_FLOW_ERROR;
1457   }
1458 }
1459
1460 static GstFlowReturn
1461 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1462     guint size, GstBuffer ** buffer)
1463 {
1464   GstBaseSrcClass *bclass;
1465   GstFlowReturn ret;
1466   GstBuffer *res_buf;
1467
1468   bclass = GST_BASE_SRC_GET_CLASS (src);
1469
1470   if (G_UNLIKELY (!bclass->alloc))
1471     goto no_function;
1472   if (G_UNLIKELY (!bclass->fill))
1473     goto no_function;
1474
1475   if (*buffer == NULL) {
1476     /* downstream did not provide us with a buffer to fill, allocate one
1477      * ourselves */
1478     ret = bclass->alloc (src, offset, size, &res_buf);
1479     if (G_UNLIKELY (ret != GST_FLOW_OK))
1480       goto alloc_failed;
1481   } else {
1482     res_buf = *buffer;
1483   }
1484
1485   if (G_LIKELY (size > 0)) {
1486     /* only call fill when there is a size */
1487     ret = bclass->fill (src, offset, size, res_buf);
1488     if (G_UNLIKELY (ret != GST_FLOW_OK))
1489       goto not_ok;
1490   }
1491
1492   *buffer = res_buf;
1493
1494   return GST_FLOW_OK;
1495
1496   /* ERRORS */
1497 no_function:
1498   {
1499     GST_DEBUG_OBJECT (src, "no fill or alloc function");
1500     return GST_FLOW_NOT_SUPPORTED;
1501   }
1502 alloc_failed:
1503   {
1504     GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1505     return ret;
1506   }
1507 not_ok:
1508   {
1509     GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1510         gst_flow_get_name (ret));
1511     if (*buffer == NULL)
1512       gst_buffer_unref (res_buf);
1513     return ret;
1514   }
1515 }
1516
1517 /* this code implements the seeking. It is a good example
1518  * handling all cases.
1519  *
1520  * A seek updates the currently configured segment.start
1521  * and segment.stop values based on the SEEK_TYPE. If the
1522  * segment.start value is updated, a seek to this new position
1523  * should be performed.
1524  *
1525  * The seek can only be executed when we are not currently
1526  * streaming any data, to make sure that this is the case, we
1527  * acquire the STREAM_LOCK which is taken when we are in the
1528  * _loop() function or when a getrange() is called. Normally
1529  * we will not receive a seek if we are operating in pull mode
1530  * though. When we operate as a live source we might block on the live
1531  * cond, which does not release the STREAM_LOCK. Therefore we will try
1532  * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1533  * safe to perform the seek.
1534  *
1535  * When we are in the loop() function, we might be in the middle
1536  * of pushing a buffer, which might block in a sink. To make sure
1537  * that the push gets unblocked we push out a FLUSH_START event.
1538  * Our loop function will get a FLUSHING return value from
1539  * the push and will pause, effectively releasing the STREAM_LOCK.
1540  *
1541  * For a non-flushing seek, we pause the task, which might eventually
1542  * release the STREAM_LOCK. We say eventually because when the sink
1543  * blocks on the sample we might wait a very long time until the sink
1544  * unblocks the sample. In any case we acquire the STREAM_LOCK and
1545  * can continue the seek. A non-flushing seek is normally done in a
1546  * running pipeline to perform seamless playback, this means that the sink is
1547  * PLAYING and will return from its chain function.
1548  * In the case of a non-flushing seek we need to make sure that the
1549  * data we output after the seek is continuous with the previous data,
1550  * this is because a non-flushing seek does not reset the running-time
1551  * to 0. We do this by closing the currently running segment, ie. sending
1552  * a new_segment event with the stop position set to the last processed
1553  * position.
1554  *
1555  * After updating the segment.start/stop values, we prepare for
1556  * streaming again. We push out a FLUSH_STOP to make the peer pad
1557  * accept data again and we start our task again.
1558  *
1559  * A segment seek posts a message on the bus saying that the playback
1560  * of the segment started. We store the segment flag internally because
1561  * when we reach the segment.stop we have to post a segment.done
1562  * instead of EOS when doing a segment seek.
1563  */
1564 static gboolean
1565 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1566 {
1567   gboolean res = TRUE, tres;
1568   gdouble rate;
1569   GstFormat seek_format, dest_format;
1570   GstSeekFlags flags;
1571   GstSeekType start_type, stop_type;
1572   gint64 start, stop;
1573   gboolean flush, playing;
1574   gboolean update;
1575   gboolean relative_seek = FALSE;
1576   gboolean seekseg_configured = FALSE;
1577   GstSegment seeksegment;
1578   guint32 seqnum;
1579   GstEvent *tevent;
1580
1581   GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1582
1583   GST_OBJECT_LOCK (src);
1584   dest_format = src->segment.format;
1585   GST_OBJECT_UNLOCK (src);
1586
1587   if (event) {
1588     gst_event_parse_seek (event, &rate, &seek_format, &flags,
1589         &start_type, &start, &stop_type, &stop);
1590
1591     relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1592         SEEK_TYPE_IS_RELATIVE (stop_type);
1593
1594     if (dest_format != seek_format && !relative_seek) {
1595       /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1596        * here before taking the stream lock, otherwise we must convert it later,
1597        * once we have the stream lock and can read the last configures segment
1598        * start and stop positions */
1599       gst_segment_init (&seeksegment, dest_format);
1600
1601       if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1602         goto prepare_failed;
1603
1604       seekseg_configured = TRUE;
1605     }
1606
1607     flush = flags & GST_SEEK_FLAG_FLUSH;
1608     seqnum = gst_event_get_seqnum (event);
1609   } else {
1610     flush = FALSE;
1611     /* get next seqnum */
1612     seqnum = gst_util_seqnum_next ();
1613   }
1614
1615   /* send flush start */
1616   if (flush) {
1617     tevent = gst_event_new_flush_start ();
1618     gst_event_set_seqnum (tevent, seqnum);
1619     gst_pad_push_event (src->srcpad, tevent);
1620   } else
1621     gst_pad_pause_task (src->srcpad);
1622
1623   /* unblock streaming thread. */
1624   if (unlock)
1625     gst_base_src_set_flushing (src, TRUE, FALSE, &playing);
1626
1627   /* grab streaming lock, this should eventually be possible, either
1628    * because the task is paused, our streaming thread stopped
1629    * or because our peer is flushing. */
1630   GST_PAD_STREAM_LOCK (src->srcpad);
1631   if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1632     /* we have seen this event before, issue a warning for now */
1633     GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1634         seqnum);
1635   } else {
1636     src->priv->seqnum = seqnum;
1637     GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1638   }
1639
1640   if (unlock)
1641     gst_base_src_set_flushing (src, FALSE, playing, NULL);
1642
1643   /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1644    * copy the current segment info into the temp segment that we can actually
1645    * attempt the seek with. We only update the real segment if the seek succeeds. */
1646   if (!seekseg_configured) {
1647     memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1648
1649     /* now configure the final seek segment */
1650     if (event) {
1651       if (seeksegment.format != seek_format) {
1652         /* OK, here's where we give the subclass a chance to convert the relative
1653          * seek into an absolute one in the processing format. We set up any
1654          * absolute seek above, before taking the stream lock. */
1655         if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1656           GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1657               "Aborting seek");
1658           res = FALSE;
1659         }
1660       } else {
1661         /* The seek format matches our processing format, no need to ask the
1662          * the subclass to configure the segment. */
1663         gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1664             start_type, start, stop_type, stop, &update);
1665       }
1666     }
1667     /* Else, no seek event passed, so we're just (re)starting the
1668        current segment. */
1669   }
1670
1671   if (res) {
1672     GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1673         " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1674         seeksegment.start, seeksegment.stop, seeksegment.position);
1675
1676     /* do the seek, segment.position contains the new position. */
1677     res = gst_base_src_do_seek (src, &seeksegment);
1678   }
1679
1680   /* and prepare to continue streaming */
1681   if (flush) {
1682     tevent = gst_event_new_flush_stop (TRUE);
1683     gst_event_set_seqnum (tevent, seqnum);
1684     /* send flush stop, peer will accept data and events again. We
1685      * are not yet providing data as we still have the STREAM_LOCK. */
1686     gst_pad_push_event (src->srcpad, tevent);
1687   }
1688
1689   /* The subclass must have converted the segment to the processing format
1690    * by now */
1691   if (res && seeksegment.format != dest_format) {
1692     GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1693         "in the correct format. Aborting seek.");
1694     res = FALSE;
1695   }
1696
1697   /* if the seek was successful, we update our real segment and push
1698    * out the new segment. */
1699   if (res) {
1700     GST_OBJECT_LOCK (src);
1701     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1702     GST_OBJECT_UNLOCK (src);
1703
1704     if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1705       GstMessage *message;
1706
1707       message = gst_message_new_segment_start (GST_OBJECT (src),
1708           seeksegment.format, seeksegment.position);
1709       gst_message_set_seqnum (message, seqnum);
1710
1711       gst_element_post_message (GST_ELEMENT (src), message);
1712     }
1713
1714     src->priv->segment_pending = TRUE;
1715     src->priv->segment_seqnum = seqnum;
1716   }
1717
1718   src->priv->discont = TRUE;
1719   src->running = TRUE;
1720   /* and restart the task in case it got paused explicitly or by
1721    * the FLUSH_START event we pushed out. */
1722   tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1723       src->srcpad, NULL);
1724   if (res && !tres)
1725     res = FALSE;
1726
1727   /* and release the lock again so we can continue streaming */
1728   GST_PAD_STREAM_UNLOCK (src->srcpad);
1729
1730   return res;
1731
1732   /* ERROR */
1733 prepare_failed:
1734   GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1735       "Aborting seek");
1736   return FALSE;
1737 }
1738
1739 /* all events send to this element directly. This is mainly done from the
1740  * application.
1741  */
1742 static gboolean
1743 gst_base_src_send_event (GstElement * element, GstEvent * event)
1744 {
1745   GstBaseSrc *src;
1746   gboolean result = FALSE;
1747   GstBaseSrcClass *bclass;
1748
1749   src = GST_BASE_SRC (element);
1750   bclass = GST_BASE_SRC_GET_CLASS (src);
1751
1752   GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1753
1754   switch (GST_EVENT_TYPE (event)) {
1755       /* bidirectional events */
1756     case GST_EVENT_FLUSH_START:
1757       GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1758       result = gst_pad_push_event (src->srcpad, event);
1759       /* also unblock the create function */
1760       gst_base_src_activate_pool (src, FALSE);
1761       /* unlock any subclasses, we need to do this before grabbing the
1762        * LIVE_LOCK since we hold this lock before going into ::create. We pass an
1763        * unlock to the params because of backwards compat (see seek handler)*/
1764       if (bclass->unlock)
1765         bclass->unlock (src);
1766
1767       /* the live lock is released when we are blocked, waiting for playing or
1768        * when we sync to the clock. */
1769       GST_LIVE_LOCK (src);
1770       src->priv->flushing = TRUE;
1771       /* clear pending EOS if any */
1772       if (g_atomic_int_get (&src->priv->has_pending_eos)) {
1773         GST_OBJECT_LOCK (src);
1774         CLEAR_PENDING_EOS (src);
1775         src->priv->forced_eos = FALSE;
1776         GST_OBJECT_UNLOCK (src);
1777       }
1778       if (bclass->unlock_stop)
1779         bclass->unlock_stop (src);
1780       if (src->clock_id)
1781         gst_clock_id_unschedule (src->clock_id);
1782       GST_DEBUG_OBJECT (src, "signal");
1783       GST_LIVE_SIGNAL (src);
1784       GST_LIVE_UNLOCK (src);
1785       event = NULL;
1786       break;
1787     case GST_EVENT_FLUSH_STOP:
1788     {
1789       gboolean start;
1790
1791       GST_LIVE_LOCK (src);
1792       src->priv->segment_pending = TRUE;
1793       src->priv->flushing = FALSE;
1794       GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1795       result = gst_pad_push_event (src->srcpad, event);
1796
1797       gst_base_src_activate_pool (src, TRUE);
1798
1799       GST_OBJECT_LOCK (src->srcpad);
1800       start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1801       GST_OBJECT_UNLOCK (src->srcpad);
1802
1803       if (src->is_live) {
1804         if (!src->live_running)
1805           start = FALSE;
1806       }
1807
1808       if (start)
1809         gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1810             src->srcpad, NULL);
1811       GST_LIVE_UNLOCK (src);
1812       event = NULL;
1813       break;
1814     }
1815
1816       /* downstream serialized events */
1817     case GST_EVENT_EOS:
1818     {
1819       /* queue EOS and make sure the task or pull function performs the EOS
1820        * actions.
1821        *
1822        * We have two possibilities:
1823        *
1824        *  - Before we are to enter the _create function, we check the has_pending_eos
1825        *    first and do EOS instead of entering it.
1826        *  - If we are in the _create function or we did not manage to set the
1827        *    flag fast enough and we are about to enter the _create function,
1828        *    we unlock it so that we exit with FLUSHING immediately. We then
1829        *    check the EOS flag and do the EOS logic.
1830        */
1831       GST_OBJECT_LOCK (src);
1832       g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1833       if (src->priv->pending_eos)
1834         gst_event_unref (src->priv->pending_eos);
1835       src->priv->pending_eos = event;
1836       event = NULL;
1837       GST_OBJECT_UNLOCK (src);
1838
1839       GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1840
1841       /* unlock the _create function so that we can check the has_pending_eos flag
1842        * and we can do EOS. This will eventually release the LIVE_LOCK again so
1843        * that we can grab it and stop the unlock again. We don't take the stream
1844        * lock so that this operation is guaranteed to never block. */
1845       gst_base_src_activate_pool (src, FALSE);
1846       if (bclass->unlock)
1847         bclass->unlock (src);
1848
1849       GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1850
1851       GST_LIVE_LOCK (src);
1852       GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1853       /* now stop the unlock of the streaming thread again. Grabbing the live
1854        * lock is enough because that protects the create function. */
1855       if (bclass->unlock_stop)
1856         bclass->unlock_stop (src);
1857       gst_base_src_activate_pool (src, TRUE);
1858       GST_LIVE_UNLOCK (src);
1859
1860       result = TRUE;
1861       break;
1862     }
1863     case GST_EVENT_SEGMENT:
1864       /* sending random SEGMENT downstream can break sync. */
1865       break;
1866     case GST_EVENT_TAG:
1867     case GST_EVENT_CUSTOM_DOWNSTREAM:
1868     case GST_EVENT_CUSTOM_BOTH:
1869       /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1870       GST_OBJECT_LOCK (src);
1871       src->priv->pending_events =
1872           g_list_append (src->priv->pending_events, event);
1873       g_atomic_int_set (&src->priv->have_events, TRUE);
1874       GST_OBJECT_UNLOCK (src);
1875       event = NULL;
1876       result = TRUE;
1877       break;
1878     case GST_EVENT_BUFFERSIZE:
1879       /* does not seem to make much sense currently */
1880       break;
1881
1882       /* upstream events */
1883     case GST_EVENT_QOS:
1884       /* elements should override send_event and do something */
1885       break;
1886     case GST_EVENT_SEEK:
1887     {
1888       gboolean started;
1889
1890       GST_OBJECT_LOCK (src->srcpad);
1891       if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1892         goto wrong_mode;
1893       started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1894       GST_OBJECT_UNLOCK (src->srcpad);
1895
1896       if (started) {
1897         GST_DEBUG_OBJECT (src, "performing seek");
1898         /* when we are running in push mode, we can execute the
1899          * seek right now. */
1900         result = gst_base_src_perform_seek (src, event, TRUE);
1901       } else {
1902         GstEvent **event_p;
1903
1904         /* else we store the event and execute the seek when we
1905          * get activated */
1906         GST_OBJECT_LOCK (src);
1907         GST_DEBUG_OBJECT (src, "queueing seek");
1908         event_p = &src->pending_seek;
1909         gst_event_replace ((GstEvent **) event_p, event);
1910         GST_OBJECT_UNLOCK (src);
1911         /* assume the seek will work */
1912         result = TRUE;
1913       }
1914       break;
1915     }
1916     case GST_EVENT_NAVIGATION:
1917       /* could make sense for elements that do something with navigation events
1918        * but then they would need to override the send_event function */
1919       break;
1920     case GST_EVENT_LATENCY:
1921       /* does not seem to make sense currently */
1922       break;
1923
1924       /* custom events */
1925     case GST_EVENT_CUSTOM_UPSTREAM:
1926       /* override send_event if you want this */
1927       break;
1928     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1929     case GST_EVENT_CUSTOM_BOTH_OOB:
1930       /* insert a random custom event into the pipeline */
1931       GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1932       result = gst_pad_push_event (src->srcpad, event);
1933       /* we gave away the ref to the event in the push */
1934       event = NULL;
1935       break;
1936     default:
1937       break;
1938   }
1939 done:
1940   /* if we still have a ref to the event, unref it now */
1941   if (event)
1942     gst_event_unref (event);
1943
1944   return result;
1945
1946   /* ERRORS */
1947 wrong_mode:
1948   {
1949     GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1950     GST_OBJECT_UNLOCK (src->srcpad);
1951     result = FALSE;
1952     goto done;
1953   }
1954 }
1955
1956 static gboolean
1957 gst_base_src_seekable (GstBaseSrc * src)
1958 {
1959   GstBaseSrcClass *bclass;
1960   bclass = GST_BASE_SRC_GET_CLASS (src);
1961   if (bclass->is_seekable)
1962     return bclass->is_seekable (src);
1963   else
1964     return FALSE;
1965 }
1966
1967 static void
1968 gst_base_src_update_qos (GstBaseSrc * src,
1969     gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1970 {
1971   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1972       "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1973       GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1974
1975   GST_OBJECT_LOCK (src);
1976   src->priv->proportion = proportion;
1977   src->priv->earliest_time = timestamp + diff;
1978   GST_OBJECT_UNLOCK (src);
1979 }
1980
1981
1982 static gboolean
1983 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1984 {
1985   gboolean result;
1986
1987   GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1988
1989   switch (GST_EVENT_TYPE (event)) {
1990     case GST_EVENT_SEEK:
1991       /* is normally called when in push mode */
1992       if (!gst_base_src_seekable (src))
1993         goto not_seekable;
1994
1995       result = gst_base_src_perform_seek (src, event, TRUE);
1996       break;
1997     case GST_EVENT_FLUSH_START:
1998       /* cancel any blocking getrange, is normally called
1999        * when in pull mode. */
2000       result = gst_base_src_set_flushing (src, TRUE, FALSE, NULL);
2001       break;
2002     case GST_EVENT_FLUSH_STOP:
2003       result = gst_base_src_set_flushing (src, FALSE, TRUE, NULL);
2004       break;
2005     case GST_EVENT_QOS:
2006     {
2007       gdouble proportion;
2008       GstClockTimeDiff diff;
2009       GstClockTime timestamp;
2010
2011       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
2012       gst_base_src_update_qos (src, proportion, diff, timestamp);
2013       result = TRUE;
2014       break;
2015     }
2016     case GST_EVENT_RECONFIGURE:
2017       result = TRUE;
2018       break;
2019     case GST_EVENT_LATENCY:
2020       result = TRUE;
2021       break;
2022     default:
2023       result = FALSE;
2024       break;
2025   }
2026   return result;
2027
2028   /* ERRORS */
2029 not_seekable:
2030   {
2031     GST_DEBUG_OBJECT (src, "is not seekable");
2032     return FALSE;
2033   }
2034 }
2035
2036 static gboolean
2037 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2038 {
2039   GstBaseSrc *src;
2040   GstBaseSrcClass *bclass;
2041   gboolean result = FALSE;
2042
2043   src = GST_BASE_SRC (parent);
2044   bclass = GST_BASE_SRC_GET_CLASS (src);
2045
2046   if (bclass->event) {
2047     if (!(result = bclass->event (src, event)))
2048       goto subclass_failed;
2049   }
2050
2051 done:
2052   gst_event_unref (event);
2053
2054   return result;
2055
2056   /* ERRORS */
2057 subclass_failed:
2058   {
2059     GST_DEBUG_OBJECT (src, "subclass refused event");
2060     goto done;
2061   }
2062 }
2063
2064 static void
2065 gst_base_src_set_property (GObject * object, guint prop_id,
2066     const GValue * value, GParamSpec * pspec)
2067 {
2068   GstBaseSrc *src;
2069
2070   src = GST_BASE_SRC (object);
2071
2072   switch (prop_id) {
2073     case PROP_BLOCKSIZE:
2074       gst_base_src_set_blocksize (src, g_value_get_uint (value));
2075       break;
2076     case PROP_NUM_BUFFERS:
2077       src->num_buffers = g_value_get_int (value);
2078       break;
2079     case PROP_TYPEFIND:
2080       src->typefind = g_value_get_boolean (value);
2081       break;
2082     case PROP_DO_TIMESTAMP:
2083       gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2084       break;
2085     default:
2086       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2087       break;
2088   }
2089 }
2090
2091 static void
2092 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2093     GParamSpec * pspec)
2094 {
2095   GstBaseSrc *src;
2096
2097   src = GST_BASE_SRC (object);
2098
2099   switch (prop_id) {
2100     case PROP_BLOCKSIZE:
2101       g_value_set_uint (value, gst_base_src_get_blocksize (src));
2102       break;
2103     case PROP_NUM_BUFFERS:
2104       g_value_set_int (value, src->num_buffers);
2105       break;
2106     case PROP_TYPEFIND:
2107       g_value_set_boolean (value, src->typefind);
2108       break;
2109     case PROP_DO_TIMESTAMP:
2110       g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2111       break;
2112     default:
2113       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2114       break;
2115   }
2116 }
2117
2118 /* with STREAM_LOCK and LOCK */
2119 static GstClockReturn
2120 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2121 {
2122   GstClockReturn ret;
2123   GstClockID id;
2124
2125   id = gst_clock_new_single_shot_id (clock, time);
2126
2127   basesrc->clock_id = id;
2128   /* release the live lock while waiting */
2129   GST_LIVE_UNLOCK (basesrc);
2130
2131   ret = gst_clock_id_wait (id, NULL);
2132
2133   GST_LIVE_LOCK (basesrc);
2134   gst_clock_id_unref (id);
2135   basesrc->clock_id = NULL;
2136
2137   return ret;
2138 }
2139
2140 /* perform synchronisation on a buffer.
2141  * with STREAM_LOCK.
2142  */
2143 static GstClockReturn
2144 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2145 {
2146   GstClockReturn result;
2147   GstClockTime start, end;
2148   GstBaseSrcClass *bclass;
2149   GstClockTime base_time;
2150   GstClock *clock;
2151   GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2152   gboolean do_timestamp, first, pseudo_live, is_live;
2153
2154   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2155
2156   start = end = -1;
2157   if (bclass->get_times)
2158     bclass->get_times (basesrc, buffer, &start, &end);
2159
2160   /* get buffer timestamp */
2161   dts = GST_BUFFER_DTS (buffer);
2162   pts = GST_BUFFER_PTS (buffer);
2163
2164   if (GST_CLOCK_TIME_IS_VALID (dts))
2165     timestamp = dts;
2166   else
2167     timestamp = pts;
2168
2169   /* grab the lock to prepare for clocking and calculate the startup
2170    * latency. */
2171   GST_OBJECT_LOCK (basesrc);
2172
2173   is_live = basesrc->is_live;
2174   /* if we are asked to sync against the clock we are a pseudo live element */
2175   pseudo_live = (start != -1 && is_live);
2176   /* check for the first buffer */
2177   first = (basesrc->priv->latency == -1);
2178
2179   if (timestamp != -1 && pseudo_live) {
2180     GstClockTime latency;
2181
2182     /* we have a timestamp and a sync time, latency is the diff */
2183     if (timestamp <= start)
2184       latency = start - timestamp;
2185     else
2186       latency = 0;
2187
2188     if (first) {
2189       GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2190           GST_TIME_ARGS (latency));
2191       /* first time we calculate latency, just configure */
2192       basesrc->priv->latency = latency;
2193     } else {
2194       if (basesrc->priv->latency != latency) {
2195         /* we have a new latency, FIXME post latency message */
2196         basesrc->priv->latency = latency;
2197         GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2198             GST_TIME_ARGS (latency));
2199       }
2200     }
2201   } else if (first) {
2202     GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2203         is_live, start != -1);
2204     basesrc->priv->latency = 0;
2205   }
2206
2207   /* get clock, if no clock, we can't sync or do timestamps */
2208   if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2209     goto no_clock;
2210   else
2211     gst_object_ref (clock);
2212
2213   base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2214
2215   do_timestamp = basesrc->priv->do_timestamp;
2216   GST_OBJECT_UNLOCK (basesrc);
2217
2218   /* first buffer, calculate the timestamp offset */
2219   if (first) {
2220     GstClockTime running_time;
2221
2222     now = gst_clock_get_time (clock);
2223     running_time = now - base_time;
2224
2225     GST_LOG_OBJECT (basesrc,
2226         "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2227         ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2228         GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2229
2230     if (pseudo_live && timestamp != -1) {
2231       /* live source and we need to sync, add startup latency to all timestamps
2232        * to get the real running_time. Live sources should always timestamp
2233        * according to the current running time. */
2234       basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2235
2236       GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2237           GST_TIME_ARGS (basesrc->priv->ts_offset));
2238     } else {
2239       basesrc->priv->ts_offset = 0;
2240       GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2241     }
2242
2243     if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2244       if (do_timestamp) {
2245         dts = running_time;
2246       } else if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2247         if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2248           dts = basesrc->segment.start;
2249         } else {
2250           dts = 0;
2251         }
2252       }
2253       GST_BUFFER_DTS (buffer) = dts;
2254
2255       GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2256           GST_TIME_ARGS (dts));
2257     }
2258   } else {
2259     /* not the first buffer, the timestamp is the diff between the clock and
2260      * base_time */
2261     if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2262       now = gst_clock_get_time (clock);
2263
2264       dts = now - base_time;
2265       GST_BUFFER_DTS (buffer) = dts;
2266
2267       GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2268           GST_TIME_ARGS (dts));
2269     }
2270   }
2271   if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2272     if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2273       pts = dts;
2274
2275     GST_BUFFER_PTS (buffer) = dts;
2276
2277     GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2278         GST_TIME_ARGS (pts));
2279   }
2280
2281   /* if we don't have a buffer timestamp, we don't sync */
2282   if (!GST_CLOCK_TIME_IS_VALID (start))
2283     goto no_sync;
2284
2285   if (is_live) {
2286     /* for pseudo live sources, add our ts_offset to the timestamp */
2287     if (GST_CLOCK_TIME_IS_VALID (pts))
2288       GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2289     if (GST_CLOCK_TIME_IS_VALID (dts))
2290       GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2291     start += basesrc->priv->ts_offset;
2292   }
2293
2294   GST_LOG_OBJECT (basesrc,
2295       "waiting for clock, base time %" GST_TIME_FORMAT
2296       ", stream_start %" GST_TIME_FORMAT,
2297       GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2298
2299   result = gst_base_src_wait (basesrc, clock, start + base_time);
2300
2301   gst_object_unref (clock);
2302
2303   GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2304
2305   return result;
2306
2307   /* special cases */
2308 no_clock:
2309   {
2310     GST_DEBUG_OBJECT (basesrc, "we have no clock");
2311     GST_OBJECT_UNLOCK (basesrc);
2312     return GST_CLOCK_OK;
2313   }
2314 no_sync:
2315   {
2316     GST_DEBUG_OBJECT (basesrc, "no sync needed");
2317     gst_object_unref (clock);
2318     return GST_CLOCK_OK;
2319   }
2320 }
2321
2322 /* Called with STREAM_LOCK and LIVE_LOCK */
2323 static gboolean
2324 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2325     gboolean force)
2326 {
2327   guint64 size, maxsize;
2328   GstBaseSrcClass *bclass;
2329   gint64 stop;
2330
2331   /* only operate if we are working with bytes */
2332   if (src->segment.format != GST_FORMAT_BYTES)
2333     return TRUE;
2334
2335   bclass = GST_BASE_SRC_GET_CLASS (src);
2336
2337   stop = src->segment.stop;
2338   /* get total file size */
2339   size = src->segment.duration;
2340
2341   /* when not doing automatic EOS, just use the stop position. We don't use
2342    * the size to check for EOS */
2343   if (!g_atomic_int_get (&src->priv->automatic_eos))
2344     maxsize = stop;
2345   /* Otherwise, the max amount of bytes to read is the total
2346    * size or up to the segment.stop if present. */
2347   else if (stop != -1)
2348     maxsize = size != -1 ? MIN (size, stop) : stop;
2349   else
2350     maxsize = size;
2351
2352   GST_DEBUG_OBJECT (src,
2353       "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2354       ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2355       *length, size, stop, maxsize);
2356
2357   /* check size if we have one */
2358   if (maxsize != -1) {
2359     /* if we run past the end, check if the file became bigger and
2360      * retry.  Mind wrap when checking. */
2361     if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2362       /* see if length of the file changed */
2363       if (bclass->get_size)
2364         if (!bclass->get_size (src, &size))
2365           size = -1;
2366
2367       /* make sure we don't exceed the configured segment stop
2368        * if it was set */
2369       if (stop != -1)
2370         maxsize = MIN (size, stop);
2371       else
2372         maxsize = size;
2373
2374       /* if we are at or past the end, EOS */
2375       if (G_UNLIKELY (offset >= maxsize))
2376         goto unexpected_length;
2377
2378       /* else we can clip to the end */
2379       if (G_UNLIKELY (offset + *length >= maxsize))
2380         *length = maxsize - offset;
2381
2382     }
2383   }
2384
2385   /* keep track of current duration. segment is in bytes, we checked
2386    * that above. */
2387   GST_OBJECT_LOCK (src);
2388   src->segment.duration = size;
2389   GST_OBJECT_UNLOCK (src);
2390
2391   return TRUE;
2392
2393   /* ERRORS */
2394 unexpected_length:
2395   {
2396     GST_WARNING_OBJECT (src, "processing at or past EOS");
2397     return FALSE;
2398   }
2399 }
2400
2401 /* must be called with LIVE_LOCK */
2402 static GstFlowReturn
2403 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2404     GstBuffer ** buf)
2405 {
2406   GstFlowReturn ret;
2407   GstBaseSrcClass *bclass;
2408   GstClockReturn status;
2409   GstBuffer *res_buf;
2410   GstBuffer *in_buf;
2411
2412   bclass = GST_BASE_SRC_GET_CLASS (src);
2413
2414 again:
2415   if (src->is_live) {
2416     if (G_UNLIKELY (!src->live_running)) {
2417       ret = gst_base_src_wait_playing (src);
2418       if (ret != GST_FLOW_OK)
2419         goto stopped;
2420     }
2421   }
2422
2423   if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2424           && !GST_BASE_SRC_IS_STARTING (src)))
2425     goto not_started;
2426
2427   if (G_UNLIKELY (!bclass->create))
2428     goto no_function;
2429
2430   if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2431     goto unexpected_length;
2432
2433   /* track position */
2434   GST_OBJECT_LOCK (src);
2435   if (src->segment.format == GST_FORMAT_BYTES)
2436     src->segment.position = offset;
2437   GST_OBJECT_UNLOCK (src);
2438
2439   /* normally we don't count buffers */
2440   if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2441     if (src->num_buffers_left == 0)
2442       goto reached_num_buffers;
2443     else
2444       src->num_buffers_left--;
2445   }
2446
2447   /* don't enter the create function if a pending EOS event was set. For the
2448    * logic of the has_pending_eos, check the event function of this class. */
2449   if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2450     src->priv->forced_eos = TRUE;
2451     goto eos;
2452   }
2453
2454   GST_DEBUG_OBJECT (src,
2455       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2456       G_GINT64_FORMAT, offset, length, src->segment.time);
2457
2458   res_buf = in_buf = *buf;
2459
2460   ret = bclass->create (src, offset, length, &res_buf);
2461
2462   /* The create function could be unlocked because we have a pending EOS. It's
2463    * possible that we have a valid buffer from create that we need to
2464    * discard when the create function returned _OK. */
2465   if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2466     if (ret == GST_FLOW_OK) {
2467       if (*buf == NULL)
2468         gst_buffer_unref (res_buf);
2469     }
2470     src->priv->forced_eos = TRUE;
2471     goto eos;
2472   }
2473
2474   if (G_UNLIKELY (ret != GST_FLOW_OK))
2475     goto not_ok;
2476
2477   /* fallback in case the create function didn't fill a provided buffer */
2478   if (in_buf != NULL && res_buf != in_buf) {
2479     GstMapInfo info;
2480     gsize copied_size;
2481
2482     GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2483         "fill the provided buffer, copying");
2484
2485     if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2486       goto map_failed;
2487
2488     copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2489     gst_buffer_unmap (in_buf, &info);
2490     gst_buffer_set_size (in_buf, copied_size);
2491
2492     gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2493
2494     gst_buffer_unref (res_buf);
2495     res_buf = in_buf;
2496   }
2497
2498   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2499   if (offset == 0 && src->segment.time == 0
2500       && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2501     GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2502     res_buf = gst_buffer_make_writable (res_buf);
2503     GST_BUFFER_DTS (res_buf) = 0;
2504   }
2505
2506   /* now sync before pushing the buffer */
2507   status = gst_base_src_do_sync (src, res_buf);
2508
2509   /* waiting for the clock could have made us flushing */
2510   if (G_UNLIKELY (src->priv->flushing))
2511     goto flushing;
2512
2513   switch (status) {
2514     case GST_CLOCK_EARLY:
2515       /* the buffer is too late. We currently don't drop the buffer. */
2516       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2517       break;
2518     case GST_CLOCK_OK:
2519       /* buffer synchronised properly */
2520       GST_DEBUG_OBJECT (src, "buffer ok");
2521       break;
2522     case GST_CLOCK_UNSCHEDULED:
2523       /* this case is triggered when we were waiting for the clock and
2524        * it got unlocked because we did a state change. In any case, get rid of
2525        * the buffer. */
2526       if (*buf == NULL)
2527         gst_buffer_unref (res_buf);
2528
2529       if (!src->live_running) {
2530         /* We return FLUSHING when we are not running to stop the dataflow also
2531          * get rid of the produced buffer. */
2532         GST_DEBUG_OBJECT (src,
2533             "clock was unscheduled (%d), returning FLUSHING", status);
2534         ret = GST_FLOW_FLUSHING;
2535       } else {
2536         /* If we are running when this happens, we quickly switched between
2537          * pause and playing. We try to produce a new buffer */
2538         GST_DEBUG_OBJECT (src,
2539             "clock was unscheduled (%d), but we are running", status);
2540         goto again;
2541       }
2542       break;
2543     default:
2544       /* all other result values are unexpected and errors */
2545       GST_ELEMENT_ERROR (src, CORE, CLOCK,
2546           (_("Internal clock error.")),
2547           ("clock returned unexpected return value %d", status));
2548       if (*buf == NULL)
2549         gst_buffer_unref (res_buf);
2550       ret = GST_FLOW_ERROR;
2551       break;
2552   }
2553   if (G_LIKELY (ret == GST_FLOW_OK))
2554     *buf = res_buf;
2555
2556   return ret;
2557
2558   /* ERROR */
2559 stopped:
2560   {
2561     GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2562         gst_flow_get_name (ret));
2563     return ret;
2564   }
2565 not_ok:
2566   {
2567     GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2568         gst_flow_get_name (ret));
2569     return ret;
2570   }
2571 map_failed:
2572   {
2573     GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2574         (_("Failed to map buffer.")),
2575         ("failed to map result buffer in WRITE mode"));
2576     if (*buf == NULL)
2577       gst_buffer_unref (res_buf);
2578     return GST_FLOW_ERROR;
2579   }
2580 not_started:
2581   {
2582     GST_DEBUG_OBJECT (src, "getrange but not started");
2583     return GST_FLOW_FLUSHING;
2584   }
2585 no_function:
2586   {
2587     GST_DEBUG_OBJECT (src, "no create function");
2588     return GST_FLOW_NOT_SUPPORTED;
2589   }
2590 unexpected_length:
2591   {
2592     GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2593         ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2594     return GST_FLOW_EOS;
2595   }
2596 reached_num_buffers:
2597   {
2598     GST_DEBUG_OBJECT (src, "sent all buffers");
2599     return GST_FLOW_EOS;
2600   }
2601 flushing:
2602   {
2603     GST_DEBUG_OBJECT (src, "we are flushing");
2604     if (*buf == NULL)
2605       gst_buffer_unref (res_buf);
2606     return GST_FLOW_FLUSHING;
2607   }
2608 eos:
2609   {
2610     GST_DEBUG_OBJECT (src, "we are EOS");
2611     return GST_FLOW_EOS;
2612   }
2613 }
2614
2615 static GstFlowReturn
2616 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2617     guint length, GstBuffer ** buf)
2618 {
2619   GstBaseSrc *src;
2620   GstFlowReturn res;
2621
2622   src = GST_BASE_SRC_CAST (parent);
2623
2624   GST_LIVE_LOCK (src);
2625   if (G_UNLIKELY (src->priv->flushing))
2626     goto flushing;
2627
2628   res = gst_base_src_get_range (src, offset, length, buf);
2629
2630 done:
2631   GST_LIVE_UNLOCK (src);
2632
2633   return res;
2634
2635   /* ERRORS */
2636 flushing:
2637   {
2638     GST_DEBUG_OBJECT (src, "we are flushing");
2639     res = GST_FLOW_FLUSHING;
2640     goto done;
2641   }
2642 }
2643
2644 static gboolean
2645 gst_base_src_is_random_access (GstBaseSrc * src)
2646 {
2647   /* we need to start the basesrc to check random access */
2648   if (!GST_BASE_SRC_IS_STARTED (src)) {
2649     GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2650     if (G_LIKELY (gst_base_src_start (src))) {
2651       if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2652         goto start_failed;
2653       gst_base_src_stop (src);
2654     }
2655   }
2656
2657   return src->random_access;
2658
2659   /* ERRORS */
2660 start_failed:
2661   {
2662     GST_DEBUG_OBJECT (src, "failed to start");
2663     return FALSE;
2664   }
2665 }
2666
2667 static void
2668 gst_base_src_loop (GstPad * pad)
2669 {
2670   GstBaseSrc *src;
2671   GstBuffer *buf = NULL;
2672   GstFlowReturn ret;
2673   gint64 position;
2674   gboolean eos;
2675   guint blocksize;
2676   GList *pending_events = NULL, *tmp;
2677
2678   eos = FALSE;
2679
2680   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2681
2682   /* Just leave immediately if we're flushing */
2683   GST_LIVE_LOCK (src);
2684   if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2685     goto flushing;
2686   GST_LIVE_UNLOCK (src);
2687
2688   gst_base_src_send_stream_start (src);
2689
2690   /* The stream-start event could've caused something to flush us */
2691   GST_LIVE_LOCK (src);
2692   if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2693     goto flushing;
2694   GST_LIVE_UNLOCK (src);
2695
2696   /* check if we need to renegotiate */
2697   if (gst_pad_check_reconfigure (pad)) {
2698     if (!gst_base_src_negotiate (src)) {
2699       gst_pad_mark_reconfigure (pad);
2700       if (GST_PAD_IS_FLUSHING (pad)) {
2701         GST_LIVE_LOCK (src);
2702         goto flushing;
2703       } else {
2704         goto negotiate_failed;
2705       }
2706     }
2707   }
2708
2709   GST_LIVE_LOCK (src);
2710
2711   if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2712     goto flushing;
2713
2714   blocksize = src->blocksize;
2715
2716   /* if we operate in bytes, we can calculate an offset */
2717   if (src->segment.format == GST_FORMAT_BYTES) {
2718     position = src->segment.position;
2719     /* for negative rates, start with subtracting the blocksize */
2720     if (src->segment.rate < 0.0) {
2721       /* we cannot go below segment.start */
2722       if (position > src->segment.start + blocksize)
2723         position -= blocksize;
2724       else {
2725         /* last block, remainder up to segment.start */
2726         blocksize = position - src->segment.start;
2727         position = src->segment.start;
2728       }
2729     }
2730   } else
2731     position = -1;
2732
2733   GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2734       GST_TIME_ARGS (position), blocksize);
2735
2736   ret = gst_base_src_get_range (src, position, blocksize, &buf);
2737   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2738     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2739         gst_flow_get_name (ret));
2740     GST_LIVE_UNLOCK (src);
2741     goto pause;
2742   }
2743   /* this should not happen */
2744   if (G_UNLIKELY (buf == NULL))
2745     goto null_buffer;
2746
2747   /* push events to close/start our segment before we push the buffer. */
2748   if (G_UNLIKELY (src->priv->segment_pending)) {
2749     GstEvent *seg_event = gst_event_new_segment (&src->segment);
2750
2751     gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2752     src->priv->segment_seqnum = gst_util_seqnum_next ();
2753     gst_pad_push_event (pad, seg_event);
2754     src->priv->segment_pending = FALSE;
2755   }
2756
2757   if (g_atomic_int_get (&src->priv->have_events)) {
2758     GST_OBJECT_LOCK (src);
2759     /* take the events */
2760     pending_events = src->priv->pending_events;
2761     src->priv->pending_events = NULL;
2762     g_atomic_int_set (&src->priv->have_events, FALSE);
2763     GST_OBJECT_UNLOCK (src);
2764   }
2765
2766   /* Push out pending events if any */
2767   if (G_UNLIKELY (pending_events != NULL)) {
2768     for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2769       GstEvent *ev = (GstEvent *) tmp->data;
2770       gst_pad_push_event (pad, ev);
2771     }
2772     g_list_free (pending_events);
2773   }
2774
2775   /* figure out the new position */
2776   switch (src->segment.format) {
2777     case GST_FORMAT_BYTES:
2778     {
2779       guint bufsize = gst_buffer_get_size (buf);
2780
2781       /* we subtracted above for negative rates */
2782       if (src->segment.rate >= 0.0)
2783         position += bufsize;
2784       break;
2785     }
2786     case GST_FORMAT_TIME:
2787     {
2788       GstClockTime start, duration;
2789
2790       start = GST_BUFFER_TIMESTAMP (buf);
2791       duration = GST_BUFFER_DURATION (buf);
2792
2793       if (GST_CLOCK_TIME_IS_VALID (start))
2794         position = start;
2795       else
2796         position = src->segment.position;
2797
2798       if (GST_CLOCK_TIME_IS_VALID (duration)) {
2799         if (src->segment.rate >= 0.0)
2800           position += duration;
2801         else if (position > duration)
2802           position -= duration;
2803         else
2804           position = 0;
2805       }
2806       break;
2807     }
2808     case GST_FORMAT_DEFAULT:
2809       if (src->segment.rate >= 0.0)
2810         position = GST_BUFFER_OFFSET_END (buf);
2811       else
2812         position = GST_BUFFER_OFFSET (buf);
2813       break;
2814     default:
2815       position = -1;
2816       break;
2817   }
2818   if (position != -1) {
2819     if (src->segment.rate >= 0.0) {
2820       /* positive rate, check if we reached the stop */
2821       if (src->segment.stop != -1) {
2822         if (position >= src->segment.stop) {
2823           eos = TRUE;
2824           position = src->segment.stop;
2825         }
2826       }
2827     } else {
2828       /* negative rate, check if we reached the start. start is always set to
2829        * something different from -1 */
2830       if (position <= src->segment.start) {
2831         eos = TRUE;
2832         position = src->segment.start;
2833       }
2834       /* when going reverse, all buffers are DISCONT */
2835       src->priv->discont = TRUE;
2836     }
2837     GST_OBJECT_LOCK (src);
2838     src->segment.position = position;
2839     GST_OBJECT_UNLOCK (src);
2840   }
2841
2842   if (G_UNLIKELY (src->priv->discont)) {
2843     GST_INFO_OBJECT (src, "marking pending DISCONT");
2844     buf = gst_buffer_make_writable (buf);
2845     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2846     src->priv->discont = FALSE;
2847   }
2848   GST_LIVE_UNLOCK (src);
2849
2850   ret = gst_pad_push (pad, buf);
2851   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2852     if (ret == GST_FLOW_NOT_NEGOTIATED) {
2853       goto not_negotiated;
2854     }
2855     GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2856         gst_flow_get_name (ret));
2857     goto pause;
2858   }
2859
2860   /* Segment pending means that a new segment was configured
2861    * during this loop run */
2862   if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2863     GST_INFO_OBJECT (src, "pausing after end of segment");
2864     ret = GST_FLOW_EOS;
2865     goto pause;
2866   }
2867
2868 done:
2869   return;
2870
2871   /* special cases */
2872 not_negotiated:
2873   {
2874     if (gst_pad_needs_reconfigure (pad)) {
2875       GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2876       return;
2877     }
2878     /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2879      * a pending negotiation request on our srcpad */
2880   }
2881 negotiate_failed:
2882   {
2883     GST_DEBUG_OBJECT (src, "Not negotiated");
2884     ret = GST_FLOW_NOT_NEGOTIATED;
2885     goto pause;
2886   }
2887 flushing:
2888   {
2889     GST_DEBUG_OBJECT (src, "we are flushing");
2890     GST_LIVE_UNLOCK (src);
2891     ret = GST_FLOW_FLUSHING;
2892     goto pause;
2893   }
2894 pause:
2895   {
2896     const gchar *reason = gst_flow_get_name (ret);
2897     GstEvent *event;
2898
2899     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2900     src->running = FALSE;
2901     gst_pad_pause_task (pad);
2902     if (ret == GST_FLOW_EOS) {
2903       gboolean flag_segment;
2904       GstFormat format;
2905       gint64 position;
2906
2907       flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2908       format = src->segment.format;
2909       position = src->segment.position;
2910
2911       /* perform EOS logic */
2912       if (src->priv->forced_eos) {
2913         g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
2914         GST_OBJECT_LOCK (src);
2915         event = src->priv->pending_eos;
2916         src->priv->pending_eos = NULL;
2917         GST_OBJECT_UNLOCK (src);
2918
2919       } else if (flag_segment) {
2920         GstMessage *message;
2921
2922         message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2923             format, position);
2924         gst_message_set_seqnum (message, src->priv->seqnum);
2925         gst_element_post_message (GST_ELEMENT_CAST (src), message);
2926         event = gst_event_new_segment_done (format, position);
2927         gst_event_set_seqnum (event, src->priv->seqnum);
2928
2929       } else {
2930         event = gst_event_new_eos ();
2931         gst_event_set_seqnum (event, src->priv->seqnum);
2932       }
2933
2934       gst_pad_push_event (pad, event);
2935       src->priv->forced_eos = FALSE;
2936
2937     } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2938       event = gst_event_new_eos ();
2939       gst_event_set_seqnum (event, src->priv->seqnum);
2940       /* for fatal errors we post an error message, post the error
2941        * first so the app knows about the error first.
2942        * Also don't do this for FLUSHING because it happens
2943        * due to flushing and posting an error message because of
2944        * that is the wrong thing to do, e.g. when we're doing
2945        * a flushing seek. */
2946       GST_ELEMENT_ERROR (src, STREAM, FAILED,
2947           (_("Internal data flow error.")),
2948           ("streaming task paused, reason %s (%d)", reason, ret));
2949       gst_pad_push_event (pad, event);
2950     }
2951     goto done;
2952   }
2953 null_buffer:
2954   {
2955     GST_ELEMENT_ERROR (src, STREAM, FAILED,
2956         (_("Internal data flow error.")), ("element returned NULL buffer"));
2957     GST_LIVE_UNLOCK (src);
2958     goto done;
2959   }
2960 }
2961
2962 static gboolean
2963 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2964     GstAllocator * allocator, GstAllocationParams * params)
2965 {
2966   GstAllocator *oldalloc;
2967   GstBufferPool *oldpool;
2968   GstBaseSrcPrivate *priv = basesrc->priv;
2969
2970   if (pool) {
2971     GST_DEBUG_OBJECT (basesrc, "activate pool");
2972     if (!gst_buffer_pool_set_active (pool, TRUE))
2973       goto activate_failed;
2974   }
2975
2976   GST_OBJECT_LOCK (basesrc);
2977   oldpool = priv->pool;
2978   priv->pool = pool;
2979
2980   oldalloc = priv->allocator;
2981   priv->allocator = allocator;
2982
2983   if (priv->pool)
2984     gst_object_ref (priv->pool);
2985   if (priv->allocator)
2986     gst_object_ref (priv->allocator);
2987
2988   if (params)
2989     priv->params = *params;
2990   else
2991     gst_allocation_params_init (&priv->params);
2992   GST_OBJECT_UNLOCK (basesrc);
2993
2994   if (oldpool) {
2995     /* only deactivate if the pool is not the one we're using */
2996     if (oldpool != pool) {
2997       GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2998       gst_buffer_pool_set_active (oldpool, FALSE);
2999     }
3000     gst_object_unref (oldpool);
3001   }
3002   if (oldalloc) {
3003     gst_object_unref (oldalloc);
3004   }
3005   return TRUE;
3006
3007   /* ERRORS */
3008 activate_failed:
3009   {
3010     GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
3011     return FALSE;
3012   }
3013 }
3014
3015 static gboolean
3016 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
3017 {
3018   GstBaseSrcPrivate *priv = basesrc->priv;
3019   GstBufferPool *pool;
3020   gboolean res = TRUE;
3021
3022   GST_OBJECT_LOCK (basesrc);
3023   if ((pool = priv->pool))
3024     pool = gst_object_ref (pool);
3025   GST_OBJECT_UNLOCK (basesrc);
3026
3027   if (pool) {
3028     res = gst_buffer_pool_set_active (pool, active);
3029     gst_object_unref (pool);
3030   }
3031   return res;
3032 }
3033
3034
3035 static gboolean
3036 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3037 {
3038   GstCaps *outcaps;
3039   GstBufferPool *pool;
3040   guint size, min, max;
3041   GstAllocator *allocator;
3042   GstAllocationParams params;
3043   GstStructure *config;
3044   gboolean update_allocator;
3045
3046   gst_query_parse_allocation (query, &outcaps, NULL);
3047
3048   /* we got configuration from our peer or the decide_allocation method,
3049    * parse them */
3050   if (gst_query_get_n_allocation_params (query) > 0) {
3051     /* try the allocator */
3052     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
3053     update_allocator = TRUE;
3054   } else {
3055     allocator = NULL;
3056     gst_allocation_params_init (&params);
3057     update_allocator = FALSE;
3058   }
3059
3060   if (gst_query_get_n_allocation_pools (query) > 0) {
3061     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3062
3063     if (pool == NULL) {
3064       /* no pool, we can make our own */
3065       GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3066       pool = gst_buffer_pool_new ();
3067     }
3068   } else {
3069     pool = NULL;
3070     size = min = max = 0;
3071   }
3072
3073   /* now configure */
3074   if (pool) {
3075     config = gst_buffer_pool_get_config (pool);
3076     gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3077     gst_buffer_pool_config_set_allocator (config, allocator, &params);
3078
3079     /* buffer pool may have to do some changes */
3080     if (!gst_buffer_pool_set_config (pool, config)) {
3081       config = gst_buffer_pool_get_config (pool);
3082
3083       /* If change are not acceptable, fallback to generic pool */
3084       if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3085               max)) {
3086         GST_DEBUG_OBJECT (basesrc, "unsupported pool, making new pool");
3087
3088         gst_object_unref (pool);
3089         pool = gst_buffer_pool_new ();
3090         gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3091         gst_buffer_pool_config_set_allocator (config, allocator, &params);
3092       }
3093
3094       if (!gst_buffer_pool_set_config (pool, config))
3095         goto config_failed;
3096     }
3097   }
3098
3099   if (update_allocator)
3100     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
3101   else
3102     gst_query_add_allocation_param (query, allocator, &params);
3103   if (allocator)
3104     gst_object_unref (allocator);
3105
3106   if (pool) {
3107     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3108     gst_object_unref (pool);
3109   }
3110
3111   return TRUE;
3112
3113 config_failed:
3114   GST_ELEMENT_ERROR (basesrc, RESOURCE, SETTINGS,
3115       ("Failed to configure the buffer pool"),
3116       ("Configuration is most likely invalid, please report this issue."));
3117   gst_object_unref (pool);
3118   return FALSE;
3119 }
3120
3121 static gboolean
3122 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3123 {
3124   GstBaseSrcClass *bclass;
3125   gboolean result = TRUE;
3126   GstQuery *query;
3127   GstBufferPool *pool = NULL;
3128   GstAllocator *allocator = NULL;
3129   GstAllocationParams params;
3130
3131   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3132
3133   /* make query and let peer pad answer, we don't really care if it worked or
3134    * not, if it failed, the allocation query would contain defaults and the
3135    * subclass would then set better values if needed */
3136   query = gst_query_new_allocation (caps, TRUE);
3137   if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3138     /* not a problem, just debug a little */
3139     GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3140   }
3141
3142   g_assert (bclass->decide_allocation != NULL);
3143   result = bclass->decide_allocation (basesrc, query);
3144
3145   GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3146       query);
3147
3148   if (!result)
3149     goto no_decide_allocation;
3150
3151   /* we got configuration from our peer or the decide_allocation method,
3152    * parse them */
3153   if (gst_query_get_n_allocation_params (query) > 0) {
3154     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
3155   } else {
3156     allocator = NULL;
3157     gst_allocation_params_init (&params);
3158   }
3159
3160   if (gst_query_get_n_allocation_pools (query) > 0)
3161     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3162
3163   result = gst_base_src_set_allocation (basesrc, pool, allocator, &params);
3164
3165   if (allocator)
3166     gst_object_unref (allocator);
3167   if (pool)
3168     gst_object_unref (pool);
3169
3170   gst_query_unref (query);
3171
3172   return result;
3173
3174   /* Errors */
3175 no_decide_allocation:
3176   {
3177     GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3178     gst_query_unref (query);
3179
3180     return result;
3181   }
3182 }
3183
3184 /* default negotiation code.
3185  *
3186  * Take intersection between src and sink pads, take first
3187  * caps and fixate.
3188  */
3189 static gboolean
3190 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3191 {
3192   GstCaps *thiscaps;
3193   GstCaps *caps = NULL;
3194   GstCaps *peercaps = NULL;
3195   gboolean result = FALSE;
3196
3197   /* first see what is possible on our source pad */
3198   thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3199   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3200   /* nothing or anything is allowed, we're done */
3201   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3202     goto no_nego_needed;
3203
3204   if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3205     goto no_caps;
3206
3207   /* get the peer caps */
3208   peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3209   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3210   if (peercaps) {
3211     /* The result is already a subset of our caps */
3212     caps = peercaps;
3213     gst_caps_unref (thiscaps);
3214   } else {
3215     /* no peer, work with our own caps then */
3216     caps = thiscaps;
3217   }
3218   if (caps && !gst_caps_is_empty (caps)) {
3219     /* now fixate */
3220     GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3221     if (gst_caps_is_any (caps)) {
3222       GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3223       /* hmm, still anything, so element can do anything and
3224        * nego is not needed */
3225       result = TRUE;
3226     } else {
3227       caps = gst_base_src_fixate (basesrc, caps);
3228       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3229       if (gst_caps_is_fixed (caps)) {
3230         /* yay, fixed caps, use those then, it's possible that the subclass does
3231          * not accept this caps after all and we have to fail. */
3232         result = gst_base_src_set_caps (basesrc, caps);
3233       }
3234     }
3235     gst_caps_unref (caps);
3236   } else {
3237     if (caps)
3238       gst_caps_unref (caps);
3239     GST_DEBUG_OBJECT (basesrc, "no common caps");
3240   }
3241   return result;
3242
3243 no_nego_needed:
3244   {
3245     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3246     if (thiscaps)
3247       gst_caps_unref (thiscaps);
3248     return TRUE;
3249   }
3250 no_caps:
3251   {
3252     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3253         ("No supported formats found"),
3254         ("This element did not produce valid caps"));
3255     if (thiscaps)
3256       gst_caps_unref (thiscaps);
3257     return TRUE;
3258   }
3259 }
3260
3261 static gboolean
3262 gst_base_src_negotiate (GstBaseSrc * basesrc)
3263 {
3264   GstBaseSrcClass *bclass;
3265   gboolean result;
3266
3267   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3268
3269   GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3270
3271   if (G_LIKELY (bclass->negotiate))
3272     result = bclass->negotiate (basesrc);
3273   else
3274     result = TRUE;
3275
3276   if (G_LIKELY (result)) {
3277     GstCaps *caps;
3278
3279     caps = gst_pad_get_current_caps (basesrc->srcpad);
3280
3281     result = gst_base_src_prepare_allocation (basesrc, caps);
3282
3283     if (caps)
3284       gst_caps_unref (caps);
3285   }
3286   return result;
3287 }
3288
3289 static gboolean
3290 gst_base_src_start (GstBaseSrc * basesrc)
3291 {
3292   GstBaseSrcClass *bclass;
3293   gboolean result;
3294
3295   GST_LIVE_LOCK (basesrc);
3296
3297   GST_OBJECT_LOCK (basesrc);
3298   if (GST_BASE_SRC_IS_STARTING (basesrc))
3299     goto was_starting;
3300   if (GST_BASE_SRC_IS_STARTED (basesrc))
3301     goto was_started;
3302
3303   basesrc->priv->start_result = GST_FLOW_FLUSHING;
3304   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3305   gst_segment_init (&basesrc->segment, basesrc->segment.format);
3306   GST_OBJECT_UNLOCK (basesrc);
3307
3308   basesrc->num_buffers_left = basesrc->num_buffers;
3309   basesrc->running = FALSE;
3310   basesrc->priv->segment_pending = FALSE;
3311   basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3312   basesrc->priv->forced_eos = FALSE;
3313   GST_LIVE_UNLOCK (basesrc);
3314
3315   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3316   if (bclass->start)
3317     result = bclass->start (basesrc);
3318   else
3319     result = TRUE;
3320
3321   if (!result)
3322     goto could_not_start;
3323
3324   if (!gst_base_src_is_async (basesrc)) {
3325     gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3326     /* not really waiting here, we call this to get the result
3327      * from the start_complete call */
3328     result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3329   }
3330
3331   return result;
3332
3333   /* ERROR */
3334 was_starting:
3335   {
3336     GST_DEBUG_OBJECT (basesrc, "was starting");
3337     GST_OBJECT_UNLOCK (basesrc);
3338     GST_LIVE_UNLOCK (basesrc);
3339     return TRUE;
3340   }
3341 was_started:
3342   {
3343     GST_DEBUG_OBJECT (basesrc, "was started");
3344     GST_OBJECT_UNLOCK (basesrc);
3345     GST_LIVE_UNLOCK (basesrc);
3346     return TRUE;
3347   }
3348 could_not_start:
3349   {
3350     GST_DEBUG_OBJECT (basesrc, "could not start");
3351     /* subclass is supposed to post a message. We don't have to call _stop. */
3352     gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3353     return FALSE;
3354   }
3355 }
3356
3357 /**
3358  * gst_base_src_start_complete:
3359  * @basesrc: base source instance
3360  * @ret: a #GstFlowReturn
3361  *
3362  * Complete an asynchronous start operation. When the subclass overrides the
3363  * start method, it should call gst_base_src_start_complete() when the start
3364  * operation completes either from the same thread or from an asynchronous
3365  * helper thread.
3366  */
3367 void
3368 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3369 {
3370   gboolean have_size;
3371   guint64 size;
3372   gboolean seekable;
3373   GstFormat format;
3374   GstPadMode mode;
3375   GstEvent *event;
3376
3377   if (ret != GST_FLOW_OK)
3378     goto error;
3379
3380   GST_DEBUG_OBJECT (basesrc, "starting source");
3381   format = basesrc->segment.format;
3382
3383   /* figure out the size */
3384   have_size = FALSE;
3385   size = -1;
3386   if (format == GST_FORMAT_BYTES) {
3387     GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3388
3389     if (bclass->get_size) {
3390       if (!(have_size = bclass->get_size (basesrc, &size)))
3391         size = -1;
3392     }
3393     GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3394     /* only update the size when operating in bytes, subclass is supposed
3395      * to set duration in the start method for other formats */
3396     GST_OBJECT_LOCK (basesrc);
3397     basesrc->segment.duration = size;
3398     GST_OBJECT_UNLOCK (basesrc);
3399   }
3400
3401   GST_DEBUG_OBJECT (basesrc,
3402       "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3403       G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3404       basesrc->segment.duration);
3405
3406   seekable = gst_base_src_seekable (basesrc);
3407   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3408
3409   /* update for random access flag */
3410   basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3411
3412   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3413
3414   /* stop flushing now but for live sources, still block in the LIVE lock when
3415    * we are not yet PLAYING */
3416   gst_base_src_set_flushing (basesrc, FALSE, FALSE, NULL);
3417
3418   gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3419
3420   GST_OBJECT_LOCK (basesrc->srcpad);
3421   mode = GST_PAD_MODE (basesrc->srcpad);
3422   GST_OBJECT_UNLOCK (basesrc->srcpad);
3423
3424   /* take the stream lock here, we only want to let the task run when we have
3425    * set the STARTED flag */
3426   GST_PAD_STREAM_LOCK (basesrc->srcpad);
3427   switch (mode) {
3428     case GST_PAD_MODE_PUSH:
3429       /* do initial seek, which will start the task */
3430       GST_OBJECT_LOCK (basesrc);
3431       event = basesrc->pending_seek;
3432       basesrc->pending_seek = NULL;
3433       GST_OBJECT_UNLOCK (basesrc);
3434
3435       /* The perform seek code will start the task when finished. We don't have to
3436        * unlock the streaming thread because it is not running yet */
3437       if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3438         goto seek_failed;
3439
3440       if (event)
3441         gst_event_unref (event);
3442       break;
3443     case GST_PAD_MODE_PULL:
3444       /* if not random_access, we cannot operate in pull mode for now */
3445       if (G_UNLIKELY (!basesrc->random_access))
3446         goto no_get_range;
3447       break;
3448     default:
3449       goto not_activated_yet;
3450       break;
3451   }
3452
3453   GST_OBJECT_LOCK (basesrc);
3454   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3455   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3456   basesrc->priv->start_result = ret;
3457   GST_ASYNC_SIGNAL (basesrc);
3458   GST_OBJECT_UNLOCK (basesrc);
3459
3460   GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3461
3462   return;
3463
3464 seek_failed:
3465   {
3466     GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3467     GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3468     gst_base_src_stop (basesrc);
3469     if (event)
3470       gst_event_unref (event);
3471     ret = GST_FLOW_ERROR;
3472     goto error;
3473   }
3474 no_get_range:
3475   {
3476     GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3477     gst_base_src_stop (basesrc);
3478     GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3479     ret = GST_FLOW_ERROR;
3480     goto error;
3481   }
3482 not_activated_yet:
3483   {
3484     GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3485     gst_base_src_stop (basesrc);
3486     GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3487     ret = GST_FLOW_ERROR;
3488     goto error;
3489   }
3490 error:
3491   {
3492     GST_OBJECT_LOCK (basesrc);
3493     basesrc->priv->start_result = ret;
3494     GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3495     GST_ASYNC_SIGNAL (basesrc);
3496     GST_OBJECT_UNLOCK (basesrc);
3497     return;
3498   }
3499 }
3500
3501 /**
3502  * gst_base_src_start_wait:
3503  * @basesrc: base source instance
3504  *
3505  * Wait until the start operation completes.
3506  *
3507  * Returns: a #GstFlowReturn.
3508  */
3509 GstFlowReturn
3510 gst_base_src_start_wait (GstBaseSrc * basesrc)
3511 {
3512   GstFlowReturn result;
3513
3514   GST_OBJECT_LOCK (basesrc);
3515   while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3516     GST_ASYNC_WAIT (basesrc);
3517   }
3518   result = basesrc->priv->start_result;
3519   GST_OBJECT_UNLOCK (basesrc);
3520
3521   GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3522
3523   return result;
3524 }
3525
3526 static gboolean
3527 gst_base_src_stop (GstBaseSrc * basesrc)
3528 {
3529   GstBaseSrcClass *bclass;
3530   gboolean result = TRUE;
3531
3532   GST_DEBUG_OBJECT (basesrc, "stopping source");
3533
3534   /* flush all */
3535   gst_base_src_set_flushing (basesrc, TRUE, FALSE, NULL);
3536   /* stop the task */
3537   gst_pad_stop_task (basesrc->srcpad);
3538
3539   GST_OBJECT_LOCK (basesrc);
3540   if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3541     goto was_stopped;
3542
3543   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3544   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3545   basesrc->priv->start_result = GST_FLOW_FLUSHING;
3546   GST_ASYNC_SIGNAL (basesrc);
3547   GST_OBJECT_UNLOCK (basesrc);
3548
3549   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3550   if (bclass->stop)
3551     result = bclass->stop (basesrc);
3552
3553   gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3554
3555   return result;
3556
3557 was_stopped:
3558   {
3559     GST_DEBUG_OBJECT (basesrc, "was stopped");
3560     GST_OBJECT_UNLOCK (basesrc);
3561     return TRUE;
3562   }
3563 }
3564
3565 /* start or stop flushing dataprocessing
3566  */
3567 static gboolean
3568 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3569     gboolean flushing, gboolean live_play, gboolean * playing)
3570 {
3571   GstBaseSrcClass *bclass;
3572
3573   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3574
3575   GST_DEBUG_OBJECT (basesrc, "flushing %d, live_play %d", flushing, live_play);
3576
3577   if (flushing) {
3578     gst_base_src_activate_pool (basesrc, FALSE);
3579     /* unlock any subclasses, we need to do this before grabbing the
3580      * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3581      * unlock to the params because of backwards compat (see seek handler)*/
3582     if (bclass->unlock)
3583       bclass->unlock (basesrc);
3584   }
3585
3586   /* the live lock is released when we are blocked, waiting for playing or
3587    * when we sync to the clock. */
3588   GST_LIVE_LOCK (basesrc);
3589   if (playing)
3590     *playing = basesrc->live_running;
3591   basesrc->priv->flushing = flushing;
3592   if (flushing) {
3593     /* if we are locked in the live lock, signal it to make it flush */
3594     basesrc->live_running = TRUE;
3595
3596     /* clear pending EOS if any */
3597     if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3598       GST_OBJECT_LOCK (basesrc);
3599       CLEAR_PENDING_EOS (basesrc);
3600       basesrc->priv->forced_eos = FALSE;
3601       GST_OBJECT_UNLOCK (basesrc);
3602     }
3603
3604     /* step 1, now that we have the LIVE lock, clear our unlock request */
3605     if (bclass->unlock_stop)
3606       bclass->unlock_stop (basesrc);
3607
3608     /* step 2, unblock clock sync (if any) or any other blocking thing */
3609     if (basesrc->clock_id)
3610       gst_clock_id_unschedule (basesrc->clock_id);
3611   } else {
3612     /* signal the live source that it can start playing */
3613     basesrc->live_running = live_play;
3614
3615     gst_base_src_activate_pool (basesrc, TRUE);
3616
3617     /* Drop all delayed events */
3618     GST_OBJECT_LOCK (basesrc);
3619     if (basesrc->priv->pending_events) {
3620       g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3621           NULL);
3622       g_list_free (basesrc->priv->pending_events);
3623       basesrc->priv->pending_events = NULL;
3624       g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3625     }
3626     GST_OBJECT_UNLOCK (basesrc);
3627   }
3628   GST_LIVE_SIGNAL (basesrc);
3629   GST_LIVE_UNLOCK (basesrc);
3630
3631   return TRUE;
3632 }
3633
3634 /* the purpose of this function is to make sure that a live source blocks in the
3635  * LIVE lock or leaves the LIVE lock and continues playing. */
3636 static gboolean
3637 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3638 {
3639   GstBaseSrcClass *bclass;
3640
3641   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3642
3643   /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3644   if (!live_play) {
3645     GST_DEBUG_OBJECT (basesrc, "unlock");
3646     if (bclass->unlock)
3647       bclass->unlock (basesrc);
3648   }
3649
3650   /* we are now able to grab the LIVE lock, when we get it, we can be
3651    * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3652    * for the clock. */
3653   GST_LIVE_LOCK (basesrc);
3654   GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3655
3656   /* unblock clock sync (if any) */
3657   if (basesrc->clock_id)
3658     gst_clock_id_unschedule (basesrc->clock_id);
3659
3660   /* configure what to do when we get to the LIVE lock. */
3661   GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3662   basesrc->live_running = live_play;
3663
3664   if (live_play) {
3665     gboolean start;
3666
3667     /* clear our unlock request when going to PLAYING */
3668     GST_DEBUG_OBJECT (basesrc, "unlock stop");
3669     if (bclass->unlock_stop)
3670       bclass->unlock_stop (basesrc);
3671
3672     /* for live sources we restart the timestamp correction */
3673     basesrc->priv->latency = -1;
3674     /* have to restart the task in case it stopped because of the unlock when
3675      * we went to PAUSED. Only do this if we operating in push mode. */
3676     GST_OBJECT_LOCK (basesrc->srcpad);
3677     start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3678     GST_OBJECT_UNLOCK (basesrc->srcpad);
3679     if (start)
3680       gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3681           basesrc->srcpad, NULL);
3682     GST_DEBUG_OBJECT (basesrc, "signal");
3683     GST_LIVE_SIGNAL (basesrc);
3684   }
3685   GST_LIVE_UNLOCK (basesrc);
3686
3687   return TRUE;
3688 }
3689
3690 static gboolean
3691 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3692 {
3693   GstBaseSrc *basesrc;
3694
3695   basesrc = GST_BASE_SRC (parent);
3696
3697   /* prepare subclass first */
3698   if (active) {
3699     GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3700
3701     if (G_UNLIKELY (!basesrc->can_activate_push))
3702       goto no_push_activation;
3703
3704     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3705       goto error_start;
3706   } else {
3707     GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3708     /* now we can stop the source */
3709     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3710       goto error_stop;
3711   }
3712   return TRUE;
3713
3714   /* ERRORS */
3715 no_push_activation:
3716   {
3717     GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3718     return FALSE;
3719   }
3720 error_start:
3721   {
3722     GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3723     return FALSE;
3724   }
3725 error_stop:
3726   {
3727     GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3728     return FALSE;
3729   }
3730 }
3731
3732 static gboolean
3733 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3734 {
3735   GstBaseSrc *basesrc;
3736
3737   basesrc = GST_BASE_SRC (parent);
3738
3739   /* prepare subclass first */
3740   if (active) {
3741     GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3742     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3743       goto error_start;
3744   } else {
3745     GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3746     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3747       goto error_stop;
3748   }
3749   return TRUE;
3750
3751   /* ERRORS */
3752 error_start:
3753   {
3754     GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3755     return FALSE;
3756   }
3757 error_stop:
3758   {
3759     GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3760     return FALSE;
3761   }
3762 }
3763
3764 static gboolean
3765 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3766     GstPadMode mode, gboolean active)
3767 {
3768   gboolean res;
3769   GstBaseSrc *src = GST_BASE_SRC (parent);
3770
3771   src->priv->stream_start_pending = FALSE;
3772
3773   GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3774
3775   switch (mode) {
3776     case GST_PAD_MODE_PULL:
3777       res = gst_base_src_activate_pull (pad, parent, active);
3778       break;
3779     case GST_PAD_MODE_PUSH:
3780       src->priv->stream_start_pending = active;
3781       res = gst_base_src_activate_push (pad, parent, active);
3782       break;
3783     default:
3784       GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3785       res = FALSE;
3786       break;
3787   }
3788   return res;
3789 }
3790
3791
3792 static GstStateChangeReturn
3793 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3794 {
3795   GstBaseSrc *basesrc;
3796   GstStateChangeReturn result;
3797   gboolean no_preroll = FALSE;
3798
3799   basesrc = GST_BASE_SRC (element);
3800
3801   switch (transition) {
3802     case GST_STATE_CHANGE_NULL_TO_READY:
3803       break;
3804     case GST_STATE_CHANGE_READY_TO_PAUSED:
3805       no_preroll = gst_base_src_is_live (basesrc);
3806       break;
3807     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3808       GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3809       if (gst_base_src_is_live (basesrc)) {
3810         /* now we can start playback */
3811         gst_base_src_set_playing (basesrc, TRUE);
3812       }
3813       break;
3814     default:
3815       break;
3816   }
3817
3818   if ((result =
3819           GST_ELEMENT_CLASS (parent_class)->change_state (element,
3820               transition)) == GST_STATE_CHANGE_FAILURE)
3821     goto failure;
3822
3823   switch (transition) {
3824     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3825       GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3826       if (gst_base_src_is_live (basesrc)) {
3827         /* make sure we block in the live lock in PAUSED */
3828         gst_base_src_set_playing (basesrc, FALSE);
3829         no_preroll = TRUE;
3830       }
3831       break;
3832     case GST_STATE_CHANGE_PAUSED_TO_READY:
3833     {
3834       /* we don't need to unblock anything here, the pad deactivation code
3835        * already did this */
3836       if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3837         GST_OBJECT_LOCK (basesrc);
3838         CLEAR_PENDING_EOS (basesrc);
3839         GST_OBJECT_UNLOCK (basesrc);
3840       }
3841       gst_event_replace (&basesrc->pending_seek, NULL);
3842       break;
3843     }
3844     case GST_STATE_CHANGE_READY_TO_NULL:
3845       break;
3846     default:
3847       break;
3848   }
3849
3850   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3851     result = GST_STATE_CHANGE_NO_PREROLL;
3852
3853   return result;
3854
3855   /* ERRORS */
3856 failure:
3857   {
3858     GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3859     return result;
3860   }
3861 }
3862
3863 /**
3864  * gst_base_src_get_buffer_pool:
3865  * @src: a #GstBaseSrc
3866  *
3867  * Returns: (transfer full): the instance of the #GstBufferPool used
3868  * by the src; unref it after usage.
3869  */
3870 GstBufferPool *
3871 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3872 {
3873   g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3874
3875   if (src->priv->pool)
3876     return gst_object_ref (src->priv->pool);
3877
3878   return NULL;
3879 }
3880
3881 /**
3882  * gst_base_src_get_allocator:
3883  * @src: a #GstBaseSrc
3884  * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3885  * used
3886  * @params: (out) (allow-none) (transfer full): the
3887  * #GstAllocationParams of @allocator
3888  *
3889  * Lets #GstBaseSrc sub-classes to know the memory @allocator
3890  * used by the base class and its @params.
3891  *
3892  * Unref the @allocator after usage.
3893  */
3894 void
3895 gst_base_src_get_allocator (GstBaseSrc * src,
3896     GstAllocator ** allocator, GstAllocationParams * params)
3897 {
3898   g_return_if_fail (GST_IS_BASE_SRC (src));
3899
3900   if (allocator)
3901     *allocator = src->priv->allocator ?
3902         gst_object_ref (src->priv->allocator) : NULL;
3903
3904   if (params)
3905     *params = src->priv->params;
3906 }