basesrc: handle reference in set_allocation rather than in prepare_allocation
[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  * |[
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 will be set to the difference
699  * between the running time and the timestamp of the first buffer.
700  * @max_latency is always the undefined value of -1.
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 = -1;
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 (-1));
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
910   bclass = GST_BASE_SRC_GET_CLASS (src);
911
912   gst_base_src_send_stream_start (src);
913
914   if (bclass->set_caps)
915     res = bclass->set_caps (src, caps);
916
917   if (res)
918     res = gst_pad_push_event (src->srcpad, gst_event_new_caps (caps));
919
920   return res;
921 }
922
923 static GstCaps *
924 gst_base_src_default_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
925 {
926   GstCaps *caps = NULL;
927   GstPadTemplate *pad_template;
928   GstBaseSrcClass *bclass;
929
930   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
931
932   pad_template =
933       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
934
935   if (pad_template != NULL) {
936     caps = gst_pad_template_get_caps (pad_template);
937
938     if (filter) {
939       GstCaps *intersection;
940
941       intersection =
942           gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
943       gst_caps_unref (caps);
944       caps = intersection;
945     }
946   }
947   return caps;
948 }
949
950 static GstCaps *
951 gst_base_src_default_fixate (GstBaseSrc * bsrc, GstCaps * caps)
952 {
953   GST_DEBUG_OBJECT (bsrc, "using default caps fixate function");
954   return gst_caps_fixate (caps);
955 }
956
957 static GstCaps *
958 gst_base_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
959 {
960   GstBaseSrcClass *bclass;
961
962   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
963
964   if (bclass->fixate)
965     caps = bclass->fixate (bsrc, caps);
966
967   return caps;
968 }
969
970 static gboolean
971 gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
972 {
973   gboolean res;
974
975   switch (GST_QUERY_TYPE (query)) {
976     case GST_QUERY_POSITION:
977     {
978       GstFormat format;
979
980       gst_query_parse_position (query, &format, NULL);
981
982       GST_DEBUG_OBJECT (src, "position query in format %s",
983           gst_format_get_name (format));
984
985       switch (format) {
986         case GST_FORMAT_PERCENT:
987         {
988           gint64 percent;
989           gint64 position;
990           gint64 duration;
991
992           GST_OBJECT_LOCK (src);
993           position = src->segment.position;
994           duration = src->segment.duration;
995           GST_OBJECT_UNLOCK (src);
996
997           if (position != -1 && duration != -1) {
998             if (position < duration)
999               percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
1000                   duration);
1001             else
1002               percent = GST_FORMAT_PERCENT_MAX;
1003           } else
1004             percent = -1;
1005
1006           gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
1007           res = TRUE;
1008           break;
1009         }
1010         default:
1011         {
1012           gint64 position;
1013           GstFormat seg_format;
1014
1015           GST_OBJECT_LOCK (src);
1016           position =
1017               gst_segment_to_stream_time (&src->segment, src->segment.format,
1018               src->segment.position);
1019           seg_format = src->segment.format;
1020           GST_OBJECT_UNLOCK (src);
1021
1022           if (position != -1) {
1023             /* convert to requested format */
1024             res =
1025                 gst_pad_query_convert (src->srcpad, seg_format,
1026                 position, format, &position);
1027           } else
1028             res = TRUE;
1029
1030           gst_query_set_position (query, format, position);
1031           break;
1032         }
1033       }
1034       break;
1035     }
1036     case GST_QUERY_DURATION:
1037     {
1038       GstFormat format;
1039
1040       gst_query_parse_duration (query, &format, NULL);
1041
1042       GST_DEBUG_OBJECT (src, "duration query in format %s",
1043           gst_format_get_name (format));
1044
1045       switch (format) {
1046         case GST_FORMAT_PERCENT:
1047           gst_query_set_duration (query, GST_FORMAT_PERCENT,
1048               GST_FORMAT_PERCENT_MAX);
1049           res = TRUE;
1050           break;
1051         default:
1052         {
1053           gint64 duration;
1054           GstFormat seg_format;
1055           guint length = 0;
1056
1057           /* may have to refresh duration */
1058           gst_base_src_update_length (src, 0, &length,
1059               g_atomic_int_get (&src->priv->dynamic_size));
1060
1061           /* this is the duration as configured by the subclass. */
1062           GST_OBJECT_LOCK (src);
1063           duration = src->segment.duration;
1064           seg_format = src->segment.format;
1065           GST_OBJECT_UNLOCK (src);
1066
1067           GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
1068               duration, gst_format_get_name (seg_format));
1069
1070           if (duration != -1) {
1071             /* convert to requested format, if this fails, we have a duration
1072              * but we cannot answer the query, we must return FALSE. */
1073             res =
1074                 gst_pad_query_convert (src->srcpad, seg_format,
1075                 duration, format, &duration);
1076           } else {
1077             /* The subclass did not configure a duration, we assume that the
1078              * media has an unknown duration then and we return TRUE to report
1079              * this. Note that this is not the same as returning FALSE, which
1080              * means that we cannot report the duration at all. */
1081             res = TRUE;
1082           }
1083           gst_query_set_duration (query, format, duration);
1084           break;
1085         }
1086       }
1087       break;
1088     }
1089
1090     case GST_QUERY_SEEKING:
1091     {
1092       GstFormat format, seg_format;
1093       gint64 duration;
1094
1095       GST_OBJECT_LOCK (src);
1096       duration = src->segment.duration;
1097       seg_format = src->segment.format;
1098       GST_OBJECT_UNLOCK (src);
1099
1100       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1101       if (format == seg_format) {
1102         gst_query_set_seeking (query, seg_format,
1103             gst_base_src_seekable (src), 0, duration);
1104         res = TRUE;
1105       } else {
1106         /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
1107         /* Don't reply to the query to make up for demuxers which don't
1108          * handle the SEEKING query yet. Players like Totem will fall back
1109          * to the duration when the SEEKING query isn't answered. */
1110         res = FALSE;
1111       }
1112       break;
1113     }
1114     case GST_QUERY_SEGMENT:
1115     {
1116       GstFormat format;
1117       gint64 start, stop;
1118
1119       GST_OBJECT_LOCK (src);
1120
1121       format = src->segment.format;
1122
1123       start =
1124           gst_segment_to_stream_time (&src->segment, format,
1125           src->segment.start);
1126       if ((stop = src->segment.stop) == -1)
1127         stop = src->segment.duration;
1128       else
1129         stop = gst_segment_to_stream_time (&src->segment, format, stop);
1130
1131       gst_query_set_segment (query, src->segment.rate, format, start, stop);
1132
1133       GST_OBJECT_UNLOCK (src);
1134       res = TRUE;
1135       break;
1136     }
1137
1138     case GST_QUERY_FORMATS:
1139     {
1140       gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
1141           GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
1142       res = TRUE;
1143       break;
1144     }
1145     case GST_QUERY_CONVERT:
1146     {
1147       GstFormat src_fmt, dest_fmt;
1148       gint64 src_val, dest_val;
1149
1150       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
1151
1152       /* we can only convert between equal formats... */
1153       if (src_fmt == dest_fmt) {
1154         dest_val = src_val;
1155         res = TRUE;
1156       } else
1157         res = FALSE;
1158
1159       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
1160       break;
1161     }
1162     case GST_QUERY_LATENCY:
1163     {
1164       GstClockTime min, max;
1165       gboolean live;
1166
1167       /* Subclasses should override and implement something useful */
1168       res = gst_base_src_query_latency (src, &live, &min, &max);
1169
1170       GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
1171           ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
1172           GST_TIME_ARGS (max));
1173
1174       gst_query_set_latency (query, live, min, max);
1175       break;
1176     }
1177     case GST_QUERY_JITTER:
1178     case GST_QUERY_RATE:
1179       res = FALSE;
1180       break;
1181     case GST_QUERY_BUFFERING:
1182     {
1183       GstFormat format, seg_format;
1184       gint64 start, stop, estimated;
1185
1186       gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1187
1188       GST_DEBUG_OBJECT (src, "buffering query in format %s",
1189           gst_format_get_name (format));
1190
1191       GST_OBJECT_LOCK (src);
1192       if (src->random_access) {
1193         estimated = 0;
1194         start = 0;
1195         if (format == GST_FORMAT_PERCENT)
1196           stop = GST_FORMAT_PERCENT_MAX;
1197         else
1198           stop = src->segment.duration;
1199       } else {
1200         estimated = -1;
1201         start = -1;
1202         stop = -1;
1203       }
1204       seg_format = src->segment.format;
1205       GST_OBJECT_UNLOCK (src);
1206
1207       /* convert to required format. When the conversion fails, we can't answer
1208        * the query. When the value is unknown, we can don't perform conversion
1209        * but report TRUE. */
1210       if (format != GST_FORMAT_PERCENT && stop != -1) {
1211         res = gst_pad_query_convert (src->srcpad, seg_format,
1212             stop, format, &stop);
1213       } else {
1214         res = TRUE;
1215       }
1216       if (res && format != GST_FORMAT_PERCENT && start != -1)
1217         res = gst_pad_query_convert (src->srcpad, seg_format,
1218             start, format, &start);
1219
1220       gst_query_set_buffering_range (query, format, start, stop, estimated);
1221       break;
1222     }
1223     case GST_QUERY_SCHEDULING:
1224     {
1225       gboolean random_access;
1226
1227       random_access = gst_base_src_is_random_access (src);
1228
1229       /* we can operate in getrange mode if the native format is bytes
1230        * and we are seekable, this condition is set in the random_access
1231        * flag and is set in the _start() method. */
1232       gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1233       if (random_access)
1234         gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1235       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1236
1237       res = TRUE;
1238       break;
1239     }
1240     case GST_QUERY_CAPS:
1241     {
1242       GstBaseSrcClass *bclass;
1243       GstCaps *caps, *filter;
1244
1245       bclass = GST_BASE_SRC_GET_CLASS (src);
1246       if (bclass->get_caps) {
1247         gst_query_parse_caps (query, &filter);
1248         if ((caps = bclass->get_caps (src, filter))) {
1249           gst_query_set_caps_result (query, caps);
1250           gst_caps_unref (caps);
1251           res = TRUE;
1252         } else {
1253           res = FALSE;
1254         }
1255       } else
1256         res = FALSE;
1257       break;
1258     }
1259     case GST_QUERY_URI:{
1260       if (GST_IS_URI_HANDLER (src)) {
1261         gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (src));
1262
1263         if (uri != NULL) {
1264           gst_query_set_uri (query, uri);
1265           g_free (uri);
1266           res = TRUE;
1267         } else {
1268           res = FALSE;
1269         }
1270       } else {
1271         res = FALSE;
1272       }
1273       break;
1274     }
1275     default:
1276       res = FALSE;
1277       break;
1278   }
1279   GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
1280       res);
1281
1282   return res;
1283 }
1284
1285 static gboolean
1286 gst_base_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1287 {
1288   GstBaseSrc *src;
1289   GstBaseSrcClass *bclass;
1290   gboolean result = FALSE;
1291
1292   src = GST_BASE_SRC (parent);
1293   bclass = GST_BASE_SRC_GET_CLASS (src);
1294
1295   if (bclass->query)
1296     result = bclass->query (src, query);
1297
1298   return result;
1299 }
1300
1301 static gboolean
1302 gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
1303 {
1304   gboolean res = TRUE;
1305
1306   /* update our offset if the start/stop position was updated */
1307   if (segment->format == GST_FORMAT_BYTES) {
1308     segment->time = segment->start;
1309   } else if (segment->start == 0) {
1310     /* seek to start, we can implement a default for this. */
1311     segment->time = 0;
1312   } else {
1313     res = FALSE;
1314     GST_INFO_OBJECT (src, "Can't do a default seek");
1315   }
1316
1317   return res;
1318 }
1319
1320 static gboolean
1321 gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
1322 {
1323   GstBaseSrcClass *bclass;
1324   gboolean result = FALSE;
1325
1326   bclass = GST_BASE_SRC_GET_CLASS (src);
1327
1328   GST_INFO_OBJECT (src, "seeking: %" GST_SEGMENT_FORMAT, segment);
1329
1330   if (bclass->do_seek)
1331     result = bclass->do_seek (src, segment);
1332
1333   return result;
1334 }
1335
1336 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
1337
1338 static gboolean
1339 gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1340     GstSegment * segment)
1341 {
1342   /* By default, we try one of 2 things:
1343    *   - For absolute seek positions, convert the requested position to our
1344    *     configured processing format and place it in the output segment \
1345    *   - For relative seek positions, convert our current (input) values to the
1346    *     seek format, adjust by the relative seek offset and then convert back to
1347    *     the processing format
1348    */
1349   GstSeekType start_type, stop_type;
1350   gint64 start, stop;
1351   GstSeekFlags flags;
1352   GstFormat seek_format, dest_format;
1353   gdouble rate;
1354   gboolean update;
1355   gboolean res = TRUE;
1356
1357   gst_event_parse_seek (event, &rate, &seek_format, &flags,
1358       &start_type, &start, &stop_type, &stop);
1359   dest_format = segment->format;
1360
1361   if (seek_format == dest_format) {
1362     gst_segment_do_seek (segment, rate, seek_format, flags,
1363         start_type, start, stop_type, stop, &update);
1364     return TRUE;
1365   }
1366
1367   if (start_type != GST_SEEK_TYPE_NONE) {
1368     /* FIXME: Handle seek_end by converting the input segment vals */
1369     res =
1370         gst_pad_query_convert (src->srcpad, seek_format, start, dest_format,
1371         &start);
1372     start_type = GST_SEEK_TYPE_SET;
1373   }
1374
1375   if (res && stop_type != GST_SEEK_TYPE_NONE) {
1376     /* FIXME: Handle seek_end by converting the input segment vals */
1377     res =
1378         gst_pad_query_convert (src->srcpad, seek_format, stop, dest_format,
1379         &stop);
1380     stop_type = GST_SEEK_TYPE_SET;
1381   }
1382
1383   /* And finally, configure our output segment in the desired format */
1384   gst_segment_do_seek (segment, rate, dest_format, flags, start_type, start,
1385       stop_type, stop, &update);
1386
1387   if (!res)
1388     goto no_format;
1389
1390   return res;
1391
1392 no_format:
1393   {
1394     GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
1395     return FALSE;
1396   }
1397 }
1398
1399 static gboolean
1400 gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
1401     GstSegment * seeksegment)
1402 {
1403   GstBaseSrcClass *bclass;
1404   gboolean result = FALSE;
1405
1406   bclass = GST_BASE_SRC_GET_CLASS (src);
1407
1408   if (bclass->prepare_seek_segment)
1409     result = bclass->prepare_seek_segment (src, event, seeksegment);
1410
1411   return result;
1412 }
1413
1414 static GstFlowReturn
1415 gst_base_src_default_alloc (GstBaseSrc * src, guint64 offset,
1416     guint size, GstBuffer ** buffer)
1417 {
1418   GstFlowReturn ret;
1419   GstBaseSrcPrivate *priv = src->priv;
1420
1421   if (priv->pool) {
1422     ret = gst_buffer_pool_acquire_buffer (priv->pool, buffer, NULL);
1423   } else if (size != -1) {
1424     *buffer = gst_buffer_new_allocate (priv->allocator, size, &priv->params);
1425     if (G_UNLIKELY (*buffer == NULL))
1426       goto alloc_failed;
1427
1428     ret = GST_FLOW_OK;
1429   } else {
1430     GST_WARNING_OBJECT (src, "Not trying to alloc %u bytes. Blocksize not set?",
1431         size);
1432     goto alloc_failed;
1433   }
1434   return ret;
1435
1436   /* ERRORS */
1437 alloc_failed:
1438   {
1439     GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
1440     return GST_FLOW_ERROR;
1441   }
1442 }
1443
1444 static GstFlowReturn
1445 gst_base_src_default_create (GstBaseSrc * src, guint64 offset,
1446     guint size, GstBuffer ** buffer)
1447 {
1448   GstBaseSrcClass *bclass;
1449   GstFlowReturn ret;
1450   GstBuffer *res_buf;
1451
1452   bclass = GST_BASE_SRC_GET_CLASS (src);
1453
1454   if (G_UNLIKELY (!bclass->alloc))
1455     goto no_function;
1456   if (G_UNLIKELY (!bclass->fill))
1457     goto no_function;
1458
1459   if (*buffer == NULL) {
1460     /* downstream did not provide us with a buffer to fill, allocate one
1461      * ourselves */
1462     ret = bclass->alloc (src, offset, size, &res_buf);
1463     if (G_UNLIKELY (ret != GST_FLOW_OK))
1464       goto alloc_failed;
1465   } else {
1466     res_buf = *buffer;
1467   }
1468
1469   if (G_LIKELY (size > 0)) {
1470     /* only call fill when there is a size */
1471     ret = bclass->fill (src, offset, size, res_buf);
1472     if (G_UNLIKELY (ret != GST_FLOW_OK))
1473       goto not_ok;
1474   }
1475
1476   *buffer = res_buf;
1477
1478   return GST_FLOW_OK;
1479
1480   /* ERRORS */
1481 no_function:
1482   {
1483     GST_DEBUG_OBJECT (src, "no fill or alloc function");
1484     return GST_FLOW_NOT_SUPPORTED;
1485   }
1486 alloc_failed:
1487   {
1488     GST_DEBUG_OBJECT (src, "Failed to allocate buffer of %u bytes", size);
1489     return ret;
1490   }
1491 not_ok:
1492   {
1493     GST_DEBUG_OBJECT (src, "fill returned %d (%s)", ret,
1494         gst_flow_get_name (ret));
1495     if (*buffer == NULL)
1496       gst_buffer_unref (res_buf);
1497     return ret;
1498   }
1499 }
1500
1501 /* this code implements the seeking. It is a good example
1502  * handling all cases.
1503  *
1504  * A seek updates the currently configured segment.start
1505  * and segment.stop values based on the SEEK_TYPE. If the
1506  * segment.start value is updated, a seek to this new position
1507  * should be performed.
1508  *
1509  * The seek can only be executed when we are not currently
1510  * streaming any data, to make sure that this is the case, we
1511  * acquire the STREAM_LOCK which is taken when we are in the
1512  * _loop() function or when a getrange() is called. Normally
1513  * we will not receive a seek if we are operating in pull mode
1514  * though. When we operate as a live source we might block on the live
1515  * cond, which does not release the STREAM_LOCK. Therefore we will try
1516  * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
1517  * safe to perform the seek.
1518  *
1519  * When we are in the loop() function, we might be in the middle
1520  * of pushing a buffer, which might block in a sink. To make sure
1521  * that the push gets unblocked we push out a FLUSH_START event.
1522  * Our loop function will get a FLUSHING return value from
1523  * the push and will pause, effectively releasing the STREAM_LOCK.
1524  *
1525  * For a non-flushing seek, we pause the task, which might eventually
1526  * release the STREAM_LOCK. We say eventually because when the sink
1527  * blocks on the sample we might wait a very long time until the sink
1528  * unblocks the sample. In any case we acquire the STREAM_LOCK and
1529  * can continue the seek. A non-flushing seek is normally done in a
1530  * running pipeline to perform seamless playback, this means that the sink is
1531  * PLAYING and will return from its chain function.
1532  * In the case of a non-flushing seek we need to make sure that the
1533  * data we output after the seek is continuous with the previous data,
1534  * this is because a non-flushing seek does not reset the running-time
1535  * to 0. We do this by closing the currently running segment, ie. sending
1536  * a new_segment event with the stop position set to the last processed
1537  * position.
1538  *
1539  * After updating the segment.start/stop values, we prepare for
1540  * streaming again. We push out a FLUSH_STOP to make the peer pad
1541  * accept data again and we start our task again.
1542  *
1543  * A segment seek posts a message on the bus saying that the playback
1544  * of the segment started. We store the segment flag internally because
1545  * when we reach the segment.stop we have to post a segment.done
1546  * instead of EOS when doing a segment seek.
1547  */
1548 static gboolean
1549 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
1550 {
1551   gboolean res = TRUE, tres;
1552   gdouble rate;
1553   GstFormat seek_format, dest_format;
1554   GstSeekFlags flags;
1555   GstSeekType start_type, stop_type;
1556   gint64 start, stop;
1557   gboolean flush, playing;
1558   gboolean update;
1559   gboolean relative_seek = FALSE;
1560   gboolean seekseg_configured = FALSE;
1561   GstSegment seeksegment;
1562   guint32 seqnum;
1563   GstEvent *tevent;
1564
1565   GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
1566
1567   GST_OBJECT_LOCK (src);
1568   dest_format = src->segment.format;
1569   GST_OBJECT_UNLOCK (src);
1570
1571   if (event) {
1572     gst_event_parse_seek (event, &rate, &seek_format, &flags,
1573         &start_type, &start, &stop_type, &stop);
1574
1575     relative_seek = SEEK_TYPE_IS_RELATIVE (start_type) ||
1576         SEEK_TYPE_IS_RELATIVE (stop_type);
1577
1578     if (dest_format != seek_format && !relative_seek) {
1579       /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
1580        * here before taking the stream lock, otherwise we must convert it later,
1581        * once we have the stream lock and can read the last configures segment
1582        * start and stop positions */
1583       gst_segment_init (&seeksegment, dest_format);
1584
1585       if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
1586         goto prepare_failed;
1587
1588       seekseg_configured = TRUE;
1589     }
1590
1591     flush = flags & GST_SEEK_FLAG_FLUSH;
1592     seqnum = gst_event_get_seqnum (event);
1593   } else {
1594     flush = FALSE;
1595     /* get next seqnum */
1596     seqnum = gst_util_seqnum_next ();
1597   }
1598
1599   /* send flush start */
1600   if (flush) {
1601     tevent = gst_event_new_flush_start ();
1602     gst_event_set_seqnum (tevent, seqnum);
1603     gst_pad_push_event (src->srcpad, tevent);
1604   } else
1605     gst_pad_pause_task (src->srcpad);
1606
1607   /* unblock streaming thread. */
1608   if (unlock)
1609     gst_base_src_set_flushing (src, TRUE, FALSE, &playing);
1610
1611   /* grab streaming lock, this should eventually be possible, either
1612    * because the task is paused, our streaming thread stopped
1613    * or because our peer is flushing. */
1614   GST_PAD_STREAM_LOCK (src->srcpad);
1615   if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
1616     /* we have seen this event before, issue a warning for now */
1617     GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
1618         seqnum);
1619   } else {
1620     src->priv->seqnum = seqnum;
1621     GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
1622   }
1623
1624   if (unlock)
1625     gst_base_src_set_flushing (src, FALSE, playing, NULL);
1626
1627   /* If we configured the seeksegment above, don't overwrite it now. Otherwise
1628    * copy the current segment info into the temp segment that we can actually
1629    * attempt the seek with. We only update the real segment if the seek succeeds. */
1630   if (!seekseg_configured) {
1631     memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1632
1633     /* now configure the final seek segment */
1634     if (event) {
1635       if (seeksegment.format != seek_format) {
1636         /* OK, here's where we give the subclass a chance to convert the relative
1637          * seek into an absolute one in the processing format. We set up any
1638          * absolute seek above, before taking the stream lock. */
1639         if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment)) {
1640           GST_DEBUG_OBJECT (src, "Preparing the seek failed after flushing. "
1641               "Aborting seek");
1642           res = FALSE;
1643         }
1644       } else {
1645         /* The seek format matches our processing format, no need to ask the
1646          * the subclass to configure the segment. */
1647         gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
1648             start_type, start, stop_type, stop, &update);
1649       }
1650     }
1651     /* Else, no seek event passed, so we're just (re)starting the
1652        current segment. */
1653   }
1654
1655   if (res) {
1656     GST_DEBUG_OBJECT (src, "segment configured from %" G_GINT64_FORMAT
1657         " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1658         seeksegment.start, seeksegment.stop, seeksegment.position);
1659
1660     /* do the seek, segment.position contains the new position. */
1661     res = gst_base_src_do_seek (src, &seeksegment);
1662   }
1663
1664   /* and prepare to continue streaming */
1665   if (flush) {
1666     tevent = gst_event_new_flush_stop (TRUE);
1667     gst_event_set_seqnum (tevent, seqnum);
1668     /* send flush stop, peer will accept data and events again. We
1669      * are not yet providing data as we still have the STREAM_LOCK. */
1670     gst_pad_push_event (src->srcpad, tevent);
1671   }
1672
1673   /* The subclass must have converted the segment to the processing format
1674    * by now */
1675   if (res && seeksegment.format != dest_format) {
1676     GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
1677         "in the correct format. Aborting seek.");
1678     res = FALSE;
1679   }
1680
1681   /* if the seek was successful, we update our real segment and push
1682    * out the new segment. */
1683   if (res) {
1684     GST_OBJECT_LOCK (src);
1685     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1686     GST_OBJECT_UNLOCK (src);
1687
1688     if (seeksegment.flags & GST_SEGMENT_FLAG_SEGMENT) {
1689       GstMessage *message;
1690
1691       message = gst_message_new_segment_start (GST_OBJECT (src),
1692           seeksegment.format, seeksegment.position);
1693       gst_message_set_seqnum (message, seqnum);
1694
1695       gst_element_post_message (GST_ELEMENT (src), message);
1696     }
1697
1698     /* for deriving a stop position for the playback segment from the seek
1699      * segment, we must take the duration when the stop is not set */
1700     /* FIXME: This is never used below */
1701     if ((stop = seeksegment.stop) == -1)
1702       stop = seeksegment.duration;
1703
1704     src->priv->segment_pending = TRUE;
1705     src->priv->segment_seqnum = seqnum;
1706   }
1707
1708   src->priv->discont = TRUE;
1709   src->running = TRUE;
1710   /* and restart the task in case it got paused explicitly or by
1711    * the FLUSH_START event we pushed out. */
1712   tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1713       src->srcpad, NULL);
1714   if (res && !tres)
1715     res = FALSE;
1716
1717   /* and release the lock again so we can continue streaming */
1718   GST_PAD_STREAM_UNLOCK (src->srcpad);
1719
1720   return res;
1721
1722   /* ERROR */
1723 prepare_failed:
1724   GST_DEBUG_OBJECT (src, "Preparing the seek failed before flushing. "
1725       "Aborting seek");
1726   return FALSE;
1727 }
1728
1729 /* all events send to this element directly. This is mainly done from the
1730  * application.
1731  */
1732 static gboolean
1733 gst_base_src_send_event (GstElement * element, GstEvent * event)
1734 {
1735   GstBaseSrc *src;
1736   gboolean result = FALSE;
1737   GstBaseSrcClass *bclass;
1738
1739   src = GST_BASE_SRC (element);
1740   bclass = GST_BASE_SRC_GET_CLASS (src);
1741
1742   GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
1743
1744   switch (GST_EVENT_TYPE (event)) {
1745       /* bidirectional events */
1746     case GST_EVENT_FLUSH_START:
1747       GST_DEBUG_OBJECT (src, "pushing flush-start event downstream");
1748       result = gst_pad_push_event (src->srcpad, event);
1749       /* also unblock the create function */
1750       gst_base_src_activate_pool (src, FALSE);
1751       /* unlock any subclasses, we need to do this before grabbing the
1752        * LIVE_LOCK since we hold this lock before going into ::create. We pass an
1753        * unlock to the params because of backwards compat (see seek handler)*/
1754       if (bclass->unlock)
1755         bclass->unlock (src);
1756
1757       /* the live lock is released when we are blocked, waiting for playing or
1758        * when we sync to the clock. */
1759       GST_LIVE_LOCK (src);
1760       src->priv->flushing = TRUE;
1761       /* clear pending EOS if any */
1762       if (g_atomic_int_get (&src->priv->has_pending_eos)) {
1763         GST_OBJECT_LOCK (src);
1764         CLEAR_PENDING_EOS (src);
1765         src->priv->forced_eos = FALSE;
1766         GST_OBJECT_UNLOCK (src);
1767       }
1768       if (bclass->unlock_stop)
1769         bclass->unlock_stop (src);
1770       if (src->clock_id)
1771         gst_clock_id_unschedule (src->clock_id);
1772       GST_DEBUG_OBJECT (src, "signal");
1773       GST_LIVE_SIGNAL (src);
1774       GST_LIVE_UNLOCK (src);
1775       event = NULL;
1776       break;
1777     case GST_EVENT_FLUSH_STOP:
1778     {
1779       gboolean start;
1780
1781       GST_LIVE_LOCK (src);
1782       src->priv->segment_pending = TRUE;
1783       src->priv->flushing = FALSE;
1784       GST_DEBUG_OBJECT (src, "pushing flush-stop event downstream");
1785       result = gst_pad_push_event (src->srcpad, event);
1786
1787       gst_base_src_activate_pool (src, TRUE);
1788
1789       GST_OBJECT_LOCK (src->srcpad);
1790       start = (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH);
1791       GST_OBJECT_UNLOCK (src->srcpad);
1792       if (start)
1793         gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
1794             src->srcpad, NULL);
1795       GST_LIVE_UNLOCK (src);
1796       event = NULL;
1797       break;
1798     }
1799
1800       /* downstream serialized events */
1801     case GST_EVENT_EOS:
1802     {
1803       /* queue EOS and make sure the task or pull function performs the EOS
1804        * actions.
1805        *
1806        * We have two possibilities:
1807        *
1808        *  - Before we are to enter the _create function, we check the has_pending_eos
1809        *    first and do EOS instead of entering it.
1810        *  - If we are in the _create function or we did not manage to set the
1811        *    flag fast enough and we are about to enter the _create function,
1812        *    we unlock it so that we exit with FLUSHING immediately. We then
1813        *    check the EOS flag and do the EOS logic.
1814        */
1815       GST_OBJECT_LOCK (src);
1816       g_atomic_int_set (&src->priv->has_pending_eos, TRUE);
1817       if (src->priv->pending_eos)
1818         gst_event_unref (src->priv->pending_eos);
1819       src->priv->pending_eos = event;
1820       event = NULL;
1821       GST_OBJECT_UNLOCK (src);
1822
1823       GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
1824
1825       /* unlock the _create function so that we can check the has_pending_eos flag
1826        * and we can do EOS. This will eventually release the LIVE_LOCK again so
1827        * that we can grab it and stop the unlock again. We don't take the stream
1828        * lock so that this operation is guaranteed to never block. */
1829       gst_base_src_activate_pool (src, FALSE);
1830       if (bclass->unlock)
1831         bclass->unlock (src);
1832
1833       GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
1834
1835       GST_LIVE_LOCK (src);
1836       GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
1837       /* now stop the unlock of the streaming thread again. Grabbing the live
1838        * lock is enough because that protects the create function. */
1839       if (bclass->unlock_stop)
1840         bclass->unlock_stop (src);
1841       gst_base_src_activate_pool (src, TRUE);
1842       GST_LIVE_UNLOCK (src);
1843
1844       result = TRUE;
1845       break;
1846     }
1847     case GST_EVENT_SEGMENT:
1848       /* sending random SEGMENT downstream can break sync. */
1849       break;
1850     case GST_EVENT_TAG:
1851     case GST_EVENT_CUSTOM_DOWNSTREAM:
1852     case GST_EVENT_CUSTOM_BOTH:
1853       /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
1854       GST_OBJECT_LOCK (src);
1855       src->priv->pending_events =
1856           g_list_append (src->priv->pending_events, event);
1857       g_atomic_int_set (&src->priv->have_events, TRUE);
1858       GST_OBJECT_UNLOCK (src);
1859       event = NULL;
1860       result = TRUE;
1861       break;
1862     case GST_EVENT_BUFFERSIZE:
1863       /* does not seem to make much sense currently */
1864       break;
1865
1866       /* upstream events */
1867     case GST_EVENT_QOS:
1868       /* elements should override send_event and do something */
1869       break;
1870     case GST_EVENT_SEEK:
1871     {
1872       gboolean started;
1873
1874       GST_OBJECT_LOCK (src->srcpad);
1875       if (GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PULL)
1876         goto wrong_mode;
1877       started = GST_PAD_MODE (src->srcpad) == GST_PAD_MODE_PUSH;
1878       GST_OBJECT_UNLOCK (src->srcpad);
1879
1880       if (started) {
1881         GST_DEBUG_OBJECT (src, "performing seek");
1882         /* when we are running in push mode, we can execute the
1883          * seek right now. */
1884         result = gst_base_src_perform_seek (src, event, TRUE);
1885       } else {
1886         GstEvent **event_p;
1887
1888         /* else we store the event and execute the seek when we
1889          * get activated */
1890         GST_OBJECT_LOCK (src);
1891         GST_DEBUG_OBJECT (src, "queueing seek");
1892         event_p = &src->pending_seek;
1893         gst_event_replace ((GstEvent **) event_p, event);
1894         GST_OBJECT_UNLOCK (src);
1895         /* assume the seek will work */
1896         result = TRUE;
1897       }
1898       break;
1899     }
1900     case GST_EVENT_NAVIGATION:
1901       /* could make sense for elements that do something with navigation events
1902        * but then they would need to override the send_event function */
1903       break;
1904     case GST_EVENT_LATENCY:
1905       /* does not seem to make sense currently */
1906       break;
1907
1908       /* custom events */
1909     case GST_EVENT_CUSTOM_UPSTREAM:
1910       /* override send_event if you want this */
1911       break;
1912     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
1913     case GST_EVENT_CUSTOM_BOTH_OOB:
1914       /* insert a random custom event into the pipeline */
1915       GST_DEBUG_OBJECT (src, "pushing custom OOB event downstream");
1916       result = gst_pad_push_event (src->srcpad, event);
1917       /* we gave away the ref to the event in the push */
1918       event = NULL;
1919       break;
1920     default:
1921       break;
1922   }
1923 done:
1924   /* if we still have a ref to the event, unref it now */
1925   if (event)
1926     gst_event_unref (event);
1927
1928   return result;
1929
1930   /* ERRORS */
1931 wrong_mode:
1932   {
1933     GST_DEBUG_OBJECT (src, "cannot perform seek when operating in pull mode");
1934     GST_OBJECT_UNLOCK (src->srcpad);
1935     result = FALSE;
1936     goto done;
1937   }
1938 }
1939
1940 static gboolean
1941 gst_base_src_seekable (GstBaseSrc * src)
1942 {
1943   GstBaseSrcClass *bclass;
1944   bclass = GST_BASE_SRC_GET_CLASS (src);
1945   if (bclass->is_seekable)
1946     return bclass->is_seekable (src);
1947   else
1948     return FALSE;
1949 }
1950
1951 static void
1952 gst_base_src_update_qos (GstBaseSrc * src,
1953     gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
1954 {
1955   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
1956       "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1957       GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
1958
1959   GST_OBJECT_LOCK (src);
1960   src->priv->proportion = proportion;
1961   src->priv->earliest_time = timestamp + diff;
1962   GST_OBJECT_UNLOCK (src);
1963 }
1964
1965
1966 static gboolean
1967 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
1968 {
1969   gboolean result;
1970
1971   GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
1972
1973   switch (GST_EVENT_TYPE (event)) {
1974     case GST_EVENT_SEEK:
1975       /* is normally called when in push mode */
1976       if (!gst_base_src_seekable (src))
1977         goto not_seekable;
1978
1979       result = gst_base_src_perform_seek (src, event, TRUE);
1980       break;
1981     case GST_EVENT_FLUSH_START:
1982       /* cancel any blocking getrange, is normally called
1983        * when in pull mode. */
1984       result = gst_base_src_set_flushing (src, TRUE, FALSE, NULL);
1985       break;
1986     case GST_EVENT_FLUSH_STOP:
1987       result = gst_base_src_set_flushing (src, FALSE, TRUE, NULL);
1988       break;
1989     case GST_EVENT_QOS:
1990     {
1991       gdouble proportion;
1992       GstClockTimeDiff diff;
1993       GstClockTime timestamp;
1994
1995       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
1996       gst_base_src_update_qos (src, proportion, diff, timestamp);
1997       result = TRUE;
1998       break;
1999     }
2000     case GST_EVENT_RECONFIGURE:
2001       result = TRUE;
2002       break;
2003     case GST_EVENT_LATENCY:
2004       result = TRUE;
2005       break;
2006     default:
2007       result = FALSE;
2008       break;
2009   }
2010   return result;
2011
2012   /* ERRORS */
2013 not_seekable:
2014   {
2015     GST_DEBUG_OBJECT (src, "is not seekable");
2016     return FALSE;
2017   }
2018 }
2019
2020 static gboolean
2021 gst_base_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2022 {
2023   GstBaseSrc *src;
2024   GstBaseSrcClass *bclass;
2025   gboolean result = FALSE;
2026
2027   src = GST_BASE_SRC (parent);
2028   bclass = GST_BASE_SRC_GET_CLASS (src);
2029
2030   if (bclass->event) {
2031     if (!(result = bclass->event (src, event)))
2032       goto subclass_failed;
2033   }
2034
2035 done:
2036   gst_event_unref (event);
2037
2038   return result;
2039
2040   /* ERRORS */
2041 subclass_failed:
2042   {
2043     GST_DEBUG_OBJECT (src, "subclass refused event");
2044     goto done;
2045   }
2046 }
2047
2048 static void
2049 gst_base_src_set_property (GObject * object, guint prop_id,
2050     const GValue * value, GParamSpec * pspec)
2051 {
2052   GstBaseSrc *src;
2053
2054   src = GST_BASE_SRC (object);
2055
2056   switch (prop_id) {
2057     case PROP_BLOCKSIZE:
2058       gst_base_src_set_blocksize (src, g_value_get_uint (value));
2059       break;
2060     case PROP_NUM_BUFFERS:
2061       src->num_buffers = g_value_get_int (value);
2062       break;
2063     case PROP_TYPEFIND:
2064       src->typefind = g_value_get_boolean (value);
2065       break;
2066     case PROP_DO_TIMESTAMP:
2067       gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
2068       break;
2069     default:
2070       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2071       break;
2072   }
2073 }
2074
2075 static void
2076 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
2077     GParamSpec * pspec)
2078 {
2079   GstBaseSrc *src;
2080
2081   src = GST_BASE_SRC (object);
2082
2083   switch (prop_id) {
2084     case PROP_BLOCKSIZE:
2085       g_value_set_uint (value, gst_base_src_get_blocksize (src));
2086       break;
2087     case PROP_NUM_BUFFERS:
2088       g_value_set_int (value, src->num_buffers);
2089       break;
2090     case PROP_TYPEFIND:
2091       g_value_set_boolean (value, src->typefind);
2092       break;
2093     case PROP_DO_TIMESTAMP:
2094       g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
2095       break;
2096     default:
2097       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2098       break;
2099   }
2100 }
2101
2102 /* with STREAM_LOCK and LOCK */
2103 static GstClockReturn
2104 gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
2105 {
2106   GstClockReturn ret;
2107   GstClockID id;
2108
2109   id = gst_clock_new_single_shot_id (clock, time);
2110
2111   basesrc->clock_id = id;
2112   /* release the live lock while waiting */
2113   GST_LIVE_UNLOCK (basesrc);
2114
2115   ret = gst_clock_id_wait (id, NULL);
2116
2117   GST_LIVE_LOCK (basesrc);
2118   gst_clock_id_unref (id);
2119   basesrc->clock_id = NULL;
2120
2121   return ret;
2122 }
2123
2124 /* perform synchronisation on a buffer.
2125  * with STREAM_LOCK.
2126  */
2127 static GstClockReturn
2128 gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
2129 {
2130   GstClockReturn result;
2131   GstClockTime start, end;
2132   GstBaseSrcClass *bclass;
2133   GstClockTime base_time;
2134   GstClock *clock;
2135   GstClockTime now = GST_CLOCK_TIME_NONE, pts, dts, timestamp;
2136   gboolean do_timestamp, first, pseudo_live, is_live;
2137
2138   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
2139
2140   start = end = -1;
2141   if (bclass->get_times)
2142     bclass->get_times (basesrc, buffer, &start, &end);
2143
2144   /* get buffer timestamp */
2145   dts = GST_BUFFER_DTS (buffer);
2146   pts = GST_BUFFER_PTS (buffer);
2147
2148   if (GST_CLOCK_TIME_IS_VALID (dts))
2149     timestamp = dts;
2150   else
2151     timestamp = pts;
2152
2153   /* grab the lock to prepare for clocking and calculate the startup
2154    * latency. */
2155   GST_OBJECT_LOCK (basesrc);
2156
2157   is_live = basesrc->is_live;
2158   /* if we are asked to sync against the clock we are a pseudo live element */
2159   pseudo_live = (start != -1 && is_live);
2160   /* check for the first buffer */
2161   first = (basesrc->priv->latency == -1);
2162
2163   if (timestamp != -1 && pseudo_live) {
2164     GstClockTime latency;
2165
2166     /* we have a timestamp and a sync time, latency is the diff */
2167     if (timestamp <= start)
2168       latency = start - timestamp;
2169     else
2170       latency = 0;
2171
2172     if (first) {
2173       GST_DEBUG_OBJECT (basesrc, "pseudo_live with latency %" GST_TIME_FORMAT,
2174           GST_TIME_ARGS (latency));
2175       /* first time we calculate latency, just configure */
2176       basesrc->priv->latency = latency;
2177     } else {
2178       if (basesrc->priv->latency != latency) {
2179         /* we have a new latency, FIXME post latency message */
2180         basesrc->priv->latency = latency;
2181         GST_DEBUG_OBJECT (basesrc, "latency changed to %" GST_TIME_FORMAT,
2182             GST_TIME_ARGS (latency));
2183       }
2184     }
2185   } else if (first) {
2186     GST_DEBUG_OBJECT (basesrc, "no latency needed, live %d, sync %d",
2187         is_live, start != -1);
2188     basesrc->priv->latency = 0;
2189   }
2190
2191   /* get clock, if no clock, we can't sync or do timestamps */
2192   if ((clock = GST_ELEMENT_CLOCK (basesrc)) == NULL)
2193     goto no_clock;
2194   else
2195     gst_object_ref (clock);
2196
2197   base_time = GST_ELEMENT_CAST (basesrc)->base_time;
2198
2199   do_timestamp = basesrc->priv->do_timestamp;
2200   GST_OBJECT_UNLOCK (basesrc);
2201
2202   /* first buffer, calculate the timestamp offset */
2203   if (first) {
2204     GstClockTime running_time;
2205
2206     now = gst_clock_get_time (clock);
2207     running_time = now - base_time;
2208
2209     GST_LOG_OBJECT (basesrc,
2210         "startup PTS: %" GST_TIME_FORMAT ", DTS %" GST_TIME_FORMAT
2211         ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (pts),
2212         GST_TIME_ARGS (dts), GST_TIME_ARGS (running_time));
2213
2214     if (pseudo_live && timestamp != -1) {
2215       /* live source and we need to sync, add startup latency to all timestamps
2216        * to get the real running_time. Live sources should always timestamp
2217        * according to the current running time. */
2218       basesrc->priv->ts_offset = GST_CLOCK_DIFF (timestamp, running_time);
2219
2220       GST_LOG_OBJECT (basesrc, "live with sync, ts_offset %" GST_TIME_FORMAT,
2221           GST_TIME_ARGS (basesrc->priv->ts_offset));
2222     } else {
2223       basesrc->priv->ts_offset = 0;
2224       GST_LOG_OBJECT (basesrc, "no timestamp offset needed");
2225     }
2226
2227     if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2228       if (do_timestamp) {
2229         dts = running_time;
2230       } else {
2231         if (GST_CLOCK_TIME_IS_VALID (basesrc->segment.start)) {
2232           dts = basesrc->segment.start;
2233         } else {
2234           dts = 0;
2235         }
2236       }
2237       GST_BUFFER_DTS (buffer) = dts;
2238
2239       GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2240           GST_TIME_ARGS (dts));
2241     }
2242   } else {
2243     /* not the first buffer, the timestamp is the diff between the clock and
2244      * base_time */
2245     if (do_timestamp && !GST_CLOCK_TIME_IS_VALID (dts)) {
2246       now = gst_clock_get_time (clock);
2247
2248       dts = now - base_time;
2249       GST_BUFFER_DTS (buffer) = dts;
2250
2251       GST_LOG_OBJECT (basesrc, "created DTS %" GST_TIME_FORMAT,
2252           GST_TIME_ARGS (dts));
2253     }
2254   }
2255   if (!GST_CLOCK_TIME_IS_VALID (pts)) {
2256     if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
2257       pts = dts;
2258
2259     GST_BUFFER_PTS (buffer) = dts;
2260
2261     GST_LOG_OBJECT (basesrc, "created PTS %" GST_TIME_FORMAT,
2262         GST_TIME_ARGS (pts));
2263   }
2264
2265   /* if we don't have a buffer timestamp, we don't sync */
2266   if (!GST_CLOCK_TIME_IS_VALID (start))
2267     goto no_sync;
2268
2269   if (is_live) {
2270     /* for pseudo live sources, add our ts_offset to the timestamp */
2271     if (GST_CLOCK_TIME_IS_VALID (pts))
2272       GST_BUFFER_PTS (buffer) += basesrc->priv->ts_offset;
2273     if (GST_CLOCK_TIME_IS_VALID (dts))
2274       GST_BUFFER_DTS (buffer) += basesrc->priv->ts_offset;
2275     start += basesrc->priv->ts_offset;
2276   }
2277
2278   GST_LOG_OBJECT (basesrc,
2279       "waiting for clock, base time %" GST_TIME_FORMAT
2280       ", stream_start %" GST_TIME_FORMAT,
2281       GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
2282
2283   result = gst_base_src_wait (basesrc, clock, start + base_time);
2284
2285   gst_object_unref (clock);
2286
2287   GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
2288
2289   return result;
2290
2291   /* special cases */
2292 no_clock:
2293   {
2294     GST_DEBUG_OBJECT (basesrc, "we have no clock");
2295     GST_OBJECT_UNLOCK (basesrc);
2296     return GST_CLOCK_OK;
2297   }
2298 no_sync:
2299   {
2300     GST_DEBUG_OBJECT (basesrc, "no sync needed");
2301     gst_object_unref (clock);
2302     return GST_CLOCK_OK;
2303   }
2304 }
2305
2306 /* Called with STREAM_LOCK and LIVE_LOCK */
2307 static gboolean
2308 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length,
2309     gboolean force)
2310 {
2311   guint64 size, maxsize;
2312   GstBaseSrcClass *bclass;
2313   GstFormat format;
2314   gint64 stop;
2315
2316   bclass = GST_BASE_SRC_GET_CLASS (src);
2317
2318   format = src->segment.format;
2319   stop = src->segment.stop;
2320   /* get total file size */
2321   size = src->segment.duration;
2322
2323   /* only operate if we are working with bytes */
2324   if (format != GST_FORMAT_BYTES)
2325     return TRUE;
2326
2327   /* when not doing automatic EOS, just use the stop position. We don't use
2328    * the size to check for EOS */
2329   if (!g_atomic_int_get (&src->priv->automatic_eos))
2330     maxsize = stop;
2331   /* Otherwise, the max amount of bytes to read is the total
2332    * size or up to the segment.stop if present. */
2333   else if (stop != -1)
2334     maxsize = size != -1 ? MIN (size, stop) : stop;
2335   else
2336     maxsize = size;
2337
2338   GST_DEBUG_OBJECT (src,
2339       "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
2340       ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
2341       *length, size, stop, maxsize);
2342
2343   /* check size if we have one */
2344   if (maxsize != -1) {
2345     /* if we run past the end, check if the file became bigger and
2346      * retry.  Mind wrap when checking. */
2347     if (G_UNLIKELY (offset >= maxsize || offset + *length >= maxsize || force)) {
2348       /* see if length of the file changed */
2349       if (bclass->get_size)
2350         if (!bclass->get_size (src, &size))
2351           size = -1;
2352
2353       /* make sure we don't exceed the configured segment stop
2354        * if it was set */
2355       if (stop != -1)
2356         maxsize = MIN (size, stop);
2357       else
2358         maxsize = size;
2359
2360       /* if we are at or past the end, EOS */
2361       if (G_UNLIKELY (offset >= maxsize))
2362         goto unexpected_length;
2363
2364       /* else we can clip to the end */
2365       if (G_UNLIKELY (offset + *length >= maxsize))
2366         *length = maxsize - offset;
2367
2368     }
2369   }
2370
2371   /* keep track of current duration. segment is in bytes, we checked
2372    * that above. */
2373   GST_OBJECT_LOCK (src);
2374   src->segment.duration = size;
2375   GST_OBJECT_UNLOCK (src);
2376
2377   return TRUE;
2378
2379   /* ERRORS */
2380 unexpected_length:
2381   {
2382     return FALSE;
2383   }
2384 }
2385
2386 /* must be called with LIVE_LOCK */
2387 static GstFlowReturn
2388 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
2389     GstBuffer ** buf)
2390 {
2391   GstFlowReturn ret;
2392   GstBaseSrcClass *bclass;
2393   GstClockReturn status;
2394   GstBuffer *res_buf;
2395   GstBuffer *in_buf;
2396
2397   bclass = GST_BASE_SRC_GET_CLASS (src);
2398
2399 again:
2400   if (src->is_live) {
2401     if (G_UNLIKELY (!src->live_running)) {
2402       ret = gst_base_src_wait_playing (src);
2403       if (ret != GST_FLOW_OK)
2404         goto stopped;
2405     }
2406   }
2407
2408   if (G_UNLIKELY (!GST_BASE_SRC_IS_STARTED (src)
2409           && !GST_BASE_SRC_IS_STARTING (src)))
2410     goto not_started;
2411
2412   if (G_UNLIKELY (!bclass->create))
2413     goto no_function;
2414
2415   if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length, FALSE)))
2416     goto unexpected_length;
2417
2418   /* track position */
2419   GST_OBJECT_LOCK (src);
2420   if (src->segment.format == GST_FORMAT_BYTES)
2421     src->segment.position = offset;
2422   GST_OBJECT_UNLOCK (src);
2423
2424   /* normally we don't count buffers */
2425   if (G_UNLIKELY (src->num_buffers_left >= 0)) {
2426     if (src->num_buffers_left == 0)
2427       goto reached_num_buffers;
2428     else
2429       src->num_buffers_left--;
2430   }
2431
2432   /* don't enter the create function if a pending EOS event was set. For the
2433    * logic of the has_pending_eos, check the event function of this class. */
2434   if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2435     src->priv->forced_eos = TRUE;
2436     goto eos;
2437   }
2438
2439   GST_DEBUG_OBJECT (src,
2440       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
2441       G_GINT64_FORMAT, offset, length, src->segment.time);
2442
2443   res_buf = in_buf = *buf;
2444
2445   ret = bclass->create (src, offset, length, &res_buf);
2446
2447   /* The create function could be unlocked because we have a pending EOS. It's
2448    * possible that we have a valid buffer from create that we need to
2449    * discard when the create function returned _OK. */
2450   if (G_UNLIKELY (g_atomic_int_get (&src->priv->has_pending_eos))) {
2451     if (ret == GST_FLOW_OK) {
2452       if (*buf == NULL)
2453         gst_buffer_unref (res_buf);
2454     }
2455     src->priv->forced_eos = TRUE;
2456     goto eos;
2457   }
2458
2459   if (G_UNLIKELY (ret != GST_FLOW_OK))
2460     goto not_ok;
2461
2462   /* fallback in case the create function didn't fill a provided buffer */
2463   if (in_buf != NULL && res_buf != in_buf) {
2464     GstMapInfo info;
2465     gsize copied_size;
2466
2467     GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, src, "create function didn't "
2468         "fill the provided buffer, copying");
2469
2470     if (!gst_buffer_map (in_buf, &info, GST_MAP_WRITE))
2471       goto map_failed;
2472
2473     copied_size = gst_buffer_extract (res_buf, 0, info.data, info.size);
2474     gst_buffer_unmap (in_buf, &info);
2475     gst_buffer_set_size (in_buf, copied_size);
2476
2477     gst_buffer_copy_into (in_buf, res_buf, GST_BUFFER_COPY_METADATA, 0, -1);
2478
2479     gst_buffer_unref (res_buf);
2480     res_buf = in_buf;
2481   }
2482
2483   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
2484   if (offset == 0 && src->segment.time == 0
2485       && GST_BUFFER_DTS (res_buf) == -1 && !src->is_live) {
2486     GST_DEBUG_OBJECT (src, "setting first timestamp to 0");
2487     res_buf = gst_buffer_make_writable (res_buf);
2488     GST_BUFFER_DTS (res_buf) = 0;
2489   }
2490
2491   /* now sync before pushing the buffer */
2492   status = gst_base_src_do_sync (src, res_buf);
2493
2494   /* waiting for the clock could have made us flushing */
2495   if (G_UNLIKELY (src->priv->flushing))
2496     goto flushing;
2497
2498   switch (status) {
2499     case GST_CLOCK_EARLY:
2500       /* the buffer is too late. We currently don't drop the buffer. */
2501       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
2502       break;
2503     case GST_CLOCK_OK:
2504       /* buffer synchronised properly */
2505       GST_DEBUG_OBJECT (src, "buffer ok");
2506       break;
2507     case GST_CLOCK_UNSCHEDULED:
2508       /* this case is triggered when we were waiting for the clock and
2509        * it got unlocked because we did a state change. In any case, get rid of
2510        * the buffer. */
2511       if (*buf == NULL)
2512         gst_buffer_unref (res_buf);
2513
2514       if (!src->live_running) {
2515         /* We return FLUSHING when we are not running to stop the dataflow also
2516          * get rid of the produced buffer. */
2517         GST_DEBUG_OBJECT (src,
2518             "clock was unscheduled (%d), returning FLUSHING", status);
2519         ret = GST_FLOW_FLUSHING;
2520       } else {
2521         /* If we are running when this happens, we quickly switched between
2522          * pause and playing. We try to produce a new buffer */
2523         GST_DEBUG_OBJECT (src,
2524             "clock was unscheduled (%d), but we are running", status);
2525         goto again;
2526       }
2527       break;
2528     default:
2529       /* all other result values are unexpected and errors */
2530       GST_ELEMENT_ERROR (src, CORE, CLOCK,
2531           (_("Internal clock error.")),
2532           ("clock returned unexpected return value %d", status));
2533       if (*buf == NULL)
2534         gst_buffer_unref (res_buf);
2535       ret = GST_FLOW_ERROR;
2536       break;
2537   }
2538   if (G_LIKELY (ret == GST_FLOW_OK))
2539     *buf = res_buf;
2540
2541   return ret;
2542
2543   /* ERROR */
2544 stopped:
2545   {
2546     GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
2547         gst_flow_get_name (ret));
2548     return ret;
2549   }
2550 not_ok:
2551   {
2552     GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
2553         gst_flow_get_name (ret));
2554     return ret;
2555   }
2556 map_failed:
2557   {
2558     GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
2559         (_("Failed to map buffer.")),
2560         ("failed to map result buffer in WRITE mode"));
2561     if (*buf == NULL)
2562       gst_buffer_unref (res_buf);
2563     return GST_FLOW_ERROR;
2564   }
2565 not_started:
2566   {
2567     GST_DEBUG_OBJECT (src, "getrange but not started");
2568     return GST_FLOW_FLUSHING;
2569   }
2570 no_function:
2571   {
2572     GST_DEBUG_OBJECT (src, "no create function");
2573     return GST_FLOW_NOT_SUPPORTED;
2574   }
2575 unexpected_length:
2576   {
2577     GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
2578         ", size=%" G_GINT64_FORMAT ")", length, offset, src->segment.duration);
2579     return GST_FLOW_EOS;
2580   }
2581 reached_num_buffers:
2582   {
2583     GST_DEBUG_OBJECT (src, "sent all buffers");
2584     return GST_FLOW_EOS;
2585   }
2586 flushing:
2587   {
2588     GST_DEBUG_OBJECT (src, "we are flushing");
2589     if (*buf == NULL)
2590       gst_buffer_unref (res_buf);
2591     return GST_FLOW_FLUSHING;
2592   }
2593 eos:
2594   {
2595     GST_DEBUG_OBJECT (src, "we are EOS");
2596     return GST_FLOW_EOS;
2597   }
2598 }
2599
2600 static GstFlowReturn
2601 gst_base_src_getrange (GstPad * pad, GstObject * parent, guint64 offset,
2602     guint length, GstBuffer ** buf)
2603 {
2604   GstBaseSrc *src;
2605   GstFlowReturn res;
2606
2607   src = GST_BASE_SRC_CAST (parent);
2608
2609   GST_LIVE_LOCK (src);
2610   if (G_UNLIKELY (src->priv->flushing))
2611     goto flushing;
2612
2613   res = gst_base_src_get_range (src, offset, length, buf);
2614
2615 done:
2616   GST_LIVE_UNLOCK (src);
2617
2618   return res;
2619
2620   /* ERRORS */
2621 flushing:
2622   {
2623     GST_DEBUG_OBJECT (src, "we are flushing");
2624     res = GST_FLOW_FLUSHING;
2625     goto done;
2626   }
2627 }
2628
2629 static gboolean
2630 gst_base_src_is_random_access (GstBaseSrc * src)
2631 {
2632   /* we need to start the basesrc to check random access */
2633   if (!GST_BASE_SRC_IS_STARTED (src)) {
2634     GST_LOG_OBJECT (src, "doing start/stop to check get_range support");
2635     if (G_LIKELY (gst_base_src_start (src))) {
2636       if (gst_base_src_start_wait (src) != GST_FLOW_OK)
2637         goto start_failed;
2638       gst_base_src_stop (src);
2639     }
2640   }
2641
2642   return src->random_access;
2643
2644   /* ERRORS */
2645 start_failed:
2646   {
2647     GST_DEBUG_OBJECT (src, "failed to start");
2648     return FALSE;
2649   }
2650 }
2651
2652 static void
2653 gst_base_src_loop (GstPad * pad)
2654 {
2655   GstBaseSrc *src;
2656   GstBuffer *buf = NULL;
2657   GstFlowReturn ret;
2658   gint64 position;
2659   gboolean eos;
2660   guint blocksize;
2661   GList *pending_events = NULL, *tmp;
2662
2663   eos = FALSE;
2664
2665   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
2666
2667   /* Just leave immediately if we're flushing */
2668   GST_LIVE_LOCK (src);
2669   if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2670     goto flushing;
2671   GST_LIVE_UNLOCK (src);
2672
2673   gst_base_src_send_stream_start (src);
2674
2675   /* The stream-start event could've caused something to flush us */
2676   GST_LIVE_LOCK (src);
2677   if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2678     goto flushing;
2679   GST_LIVE_UNLOCK (src);
2680
2681   /* check if we need to renegotiate */
2682   if (gst_pad_check_reconfigure (pad)) {
2683     if (!gst_base_src_negotiate (src)) {
2684       gst_pad_mark_reconfigure (pad);
2685       if (GST_PAD_IS_FLUSHING (pad)) {
2686         GST_LIVE_LOCK (src);
2687         goto flushing;
2688       } else {
2689         goto negotiate_failed;
2690       }
2691     }
2692   }
2693
2694   GST_LIVE_LOCK (src);
2695
2696   if (G_UNLIKELY (src->priv->flushing || GST_PAD_IS_FLUSHING (pad)))
2697     goto flushing;
2698
2699   blocksize = src->blocksize;
2700
2701   /* if we operate in bytes, we can calculate an offset */
2702   if (src->segment.format == GST_FORMAT_BYTES) {
2703     position = src->segment.position;
2704     /* for negative rates, start with subtracting the blocksize */
2705     if (src->segment.rate < 0.0) {
2706       /* we cannot go below segment.start */
2707       if (position > src->segment.start + blocksize)
2708         position -= blocksize;
2709       else {
2710         /* last block, remainder up to segment.start */
2711         blocksize = position - src->segment.start;
2712         position = src->segment.start;
2713       }
2714     }
2715   } else
2716     position = -1;
2717
2718   GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %u",
2719       GST_TIME_ARGS (position), blocksize);
2720
2721   ret = gst_base_src_get_range (src, position, blocksize, &buf);
2722   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2723     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
2724         gst_flow_get_name (ret));
2725     GST_LIVE_UNLOCK (src);
2726     goto pause;
2727   }
2728   /* this should not happen */
2729   if (G_UNLIKELY (buf == NULL))
2730     goto null_buffer;
2731
2732   /* push events to close/start our segment before we push the buffer. */
2733   if (G_UNLIKELY (src->priv->segment_pending)) {
2734     GstEvent *seg_event = gst_event_new_segment (&src->segment);
2735
2736     gst_event_set_seqnum (seg_event, src->priv->segment_seqnum);
2737     src->priv->segment_seqnum = gst_util_seqnum_next ();
2738     gst_pad_push_event (pad, seg_event);
2739     src->priv->segment_pending = FALSE;
2740   }
2741
2742   if (g_atomic_int_get (&src->priv->have_events)) {
2743     GST_OBJECT_LOCK (src);
2744     /* take the events */
2745     pending_events = src->priv->pending_events;
2746     src->priv->pending_events = NULL;
2747     g_atomic_int_set (&src->priv->have_events, FALSE);
2748     GST_OBJECT_UNLOCK (src);
2749   }
2750
2751   /* Push out pending events if any */
2752   if (G_UNLIKELY (pending_events != NULL)) {
2753     for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
2754       GstEvent *ev = (GstEvent *) tmp->data;
2755       gst_pad_push_event (pad, ev);
2756     }
2757     g_list_free (pending_events);
2758   }
2759
2760   /* figure out the new position */
2761   switch (src->segment.format) {
2762     case GST_FORMAT_BYTES:
2763     {
2764       guint bufsize = gst_buffer_get_size (buf);
2765
2766       /* we subtracted above for negative rates */
2767       if (src->segment.rate >= 0.0)
2768         position += bufsize;
2769       break;
2770     }
2771     case GST_FORMAT_TIME:
2772     {
2773       GstClockTime start, duration;
2774
2775       start = GST_BUFFER_TIMESTAMP (buf);
2776       duration = GST_BUFFER_DURATION (buf);
2777
2778       if (GST_CLOCK_TIME_IS_VALID (start))
2779         position = start;
2780       else
2781         position = src->segment.position;
2782
2783       if (GST_CLOCK_TIME_IS_VALID (duration)) {
2784         if (src->segment.rate >= 0.0)
2785           position += duration;
2786         else if (position > duration)
2787           position -= duration;
2788         else
2789           position = 0;
2790       }
2791       break;
2792     }
2793     case GST_FORMAT_DEFAULT:
2794       if (src->segment.rate >= 0.0)
2795         position = GST_BUFFER_OFFSET_END (buf);
2796       else
2797         position = GST_BUFFER_OFFSET (buf);
2798       break;
2799     default:
2800       position = -1;
2801       break;
2802   }
2803   if (position != -1) {
2804     if (src->segment.rate >= 0.0) {
2805       /* positive rate, check if we reached the stop */
2806       if (src->segment.stop != -1) {
2807         if (position >= src->segment.stop) {
2808           eos = TRUE;
2809           position = src->segment.stop;
2810         }
2811       }
2812     } else {
2813       /* negative rate, check if we reached the start. start is always set to
2814        * something different from -1 */
2815       if (position <= src->segment.start) {
2816         eos = TRUE;
2817         position = src->segment.start;
2818       }
2819       /* when going reverse, all buffers are DISCONT */
2820       src->priv->discont = TRUE;
2821     }
2822     GST_OBJECT_LOCK (src);
2823     src->segment.position = position;
2824     GST_OBJECT_UNLOCK (src);
2825   }
2826
2827   if (G_UNLIKELY (src->priv->discont)) {
2828     GST_INFO_OBJECT (src, "marking pending DISCONT");
2829     buf = gst_buffer_make_writable (buf);
2830     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2831     src->priv->discont = FALSE;
2832   }
2833   GST_LIVE_UNLOCK (src);
2834
2835   ret = gst_pad_push (pad, buf);
2836   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2837     if (ret == GST_FLOW_NOT_NEGOTIATED) {
2838       goto not_negotiated;
2839     }
2840     GST_INFO_OBJECT (src, "pausing after gst_pad_push() = %s",
2841         gst_flow_get_name (ret));
2842     goto pause;
2843   }
2844
2845   /* Segment pending means that a new segment was configured
2846    * during this loop run */
2847   if (G_UNLIKELY (eos && !src->priv->segment_pending)) {
2848     GST_INFO_OBJECT (src, "pausing after end of segment");
2849     ret = GST_FLOW_EOS;
2850     goto pause;
2851   }
2852
2853 done:
2854   return;
2855
2856   /* special cases */
2857 not_negotiated:
2858   {
2859     if (gst_pad_needs_reconfigure (pad)) {
2860       GST_DEBUG_OBJECT (src, "Retrying to renegotiate");
2861       return;
2862     }
2863     /* fallthrough when push returns NOT_NEGOTIATED and we don't have
2864      * a pending negotiation request on our srcpad */
2865   }
2866 negotiate_failed:
2867   {
2868     GST_DEBUG_OBJECT (src, "Not negotiated");
2869     ret = GST_FLOW_NOT_NEGOTIATED;
2870     goto pause;
2871   }
2872 flushing:
2873   {
2874     GST_DEBUG_OBJECT (src, "we are flushing");
2875     GST_LIVE_UNLOCK (src);
2876     ret = GST_FLOW_FLUSHING;
2877     goto pause;
2878   }
2879 pause:
2880   {
2881     const gchar *reason = gst_flow_get_name (ret);
2882     GstEvent *event;
2883
2884     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
2885     src->running = FALSE;
2886     gst_pad_pause_task (pad);
2887     if (ret == GST_FLOW_EOS) {
2888       gboolean flag_segment;
2889       GstFormat format;
2890       gint64 position;
2891
2892       flag_segment = (src->segment.flags & GST_SEGMENT_FLAG_SEGMENT) != 0;
2893       format = src->segment.format;
2894       position = src->segment.position;
2895
2896       /* perform EOS logic */
2897       if (src->priv->forced_eos) {
2898         g_assert (g_atomic_int_get (&src->priv->has_pending_eos));
2899         GST_OBJECT_LOCK (src);
2900         event = src->priv->pending_eos;
2901         src->priv->pending_eos = NULL;
2902         GST_OBJECT_UNLOCK (src);
2903
2904       } else if (flag_segment) {
2905         GstMessage *message;
2906
2907         message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
2908             format, position);
2909         gst_message_set_seqnum (message, src->priv->seqnum);
2910         gst_element_post_message (GST_ELEMENT_CAST (src), message);
2911         event = gst_event_new_segment_done (format, position);
2912         gst_event_set_seqnum (event, src->priv->seqnum);
2913
2914       } else {
2915         event = gst_event_new_eos ();
2916         gst_event_set_seqnum (event, src->priv->seqnum);
2917       }
2918
2919       gst_pad_push_event (pad, event);
2920       src->priv->forced_eos = FALSE;
2921
2922     } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_EOS) {
2923       event = gst_event_new_eos ();
2924       gst_event_set_seqnum (event, src->priv->seqnum);
2925       /* for fatal errors we post an error message, post the error
2926        * first so the app knows about the error first.
2927        * Also don't do this for FLUSHING because it happens
2928        * due to flushing and posting an error message because of
2929        * that is the wrong thing to do, e.g. when we're doing
2930        * a flushing seek. */
2931       GST_ELEMENT_ERROR (src, STREAM, FAILED,
2932           (_("Internal data flow error.")),
2933           ("streaming task paused, reason %s (%d)", reason, ret));
2934       gst_pad_push_event (pad, event);
2935     }
2936     goto done;
2937   }
2938 null_buffer:
2939   {
2940     GST_ELEMENT_ERROR (src, STREAM, FAILED,
2941         (_("Internal data flow error.")), ("element returned NULL buffer"));
2942     GST_LIVE_UNLOCK (src);
2943     goto done;
2944   }
2945 }
2946
2947 static gboolean
2948 gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
2949     GstAllocator * allocator, GstAllocationParams * params)
2950 {
2951   GstAllocator *oldalloc;
2952   GstBufferPool *oldpool;
2953   GstBaseSrcPrivate *priv = basesrc->priv;
2954
2955   if (pool) {
2956     GST_DEBUG_OBJECT (basesrc, "activate pool");
2957     if (!gst_buffer_pool_set_active (pool, TRUE))
2958       goto activate_failed;
2959   }
2960
2961   GST_OBJECT_LOCK (basesrc);
2962   oldpool = priv->pool;
2963   priv->pool = pool;
2964
2965   oldalloc = priv->allocator;
2966   priv->allocator = allocator;
2967
2968   if (priv->pool)
2969     gst_object_ref (priv->pool);
2970   if (priv->allocator)
2971     gst_object_ref (priv->allocator);
2972
2973   if (params)
2974     priv->params = *params;
2975   else
2976     gst_allocation_params_init (&priv->params);
2977   GST_OBJECT_UNLOCK (basesrc);
2978
2979   if (oldpool) {
2980     /* only deactivate if the pool is not the one we're using */
2981     if (oldpool != pool) {
2982       GST_DEBUG_OBJECT (basesrc, "deactivate old pool");
2983       gst_buffer_pool_set_active (oldpool, FALSE);
2984     }
2985     gst_object_unref (oldpool);
2986   }
2987   if (oldalloc) {
2988     gst_object_unref (oldalloc);
2989   }
2990   return TRUE;
2991
2992   /* ERRORS */
2993 activate_failed:
2994   {
2995     GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
2996     return FALSE;
2997   }
2998 }
2999
3000 static gboolean
3001 gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
3002 {
3003   GstBaseSrcPrivate *priv = basesrc->priv;
3004   GstBufferPool *pool;
3005   gboolean res = TRUE;
3006
3007   GST_OBJECT_LOCK (basesrc);
3008   if ((pool = priv->pool))
3009     pool = gst_object_ref (pool);
3010   GST_OBJECT_UNLOCK (basesrc);
3011
3012   if (pool) {
3013     res = gst_buffer_pool_set_active (pool, active);
3014     gst_object_unref (pool);
3015   }
3016   return res;
3017 }
3018
3019
3020 static gboolean
3021 gst_base_src_decide_allocation_default (GstBaseSrc * basesrc, GstQuery * query)
3022 {
3023   GstCaps *outcaps;
3024   GstBufferPool *pool;
3025   guint size, min, max;
3026   GstAllocator *allocator;
3027   GstAllocationParams params;
3028   GstStructure *config;
3029   gboolean update_allocator;
3030
3031   gst_query_parse_allocation (query, &outcaps, NULL);
3032
3033   /* we got configuration from our peer or the decide_allocation method,
3034    * parse them */
3035   if (gst_query_get_n_allocation_params (query) > 0) {
3036     /* try the allocator */
3037     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
3038     update_allocator = TRUE;
3039   } else {
3040     allocator = NULL;
3041     gst_allocation_params_init (&params);
3042     update_allocator = FALSE;
3043   }
3044
3045   if (gst_query_get_n_allocation_pools (query) > 0) {
3046     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
3047
3048     if (pool == NULL) {
3049       /* no pool, we can make our own */
3050       GST_DEBUG_OBJECT (basesrc, "no pool, making new pool");
3051       pool = gst_buffer_pool_new ();
3052     }
3053   } else {
3054     pool = NULL;
3055     size = min = max = 0;
3056   }
3057
3058   /* now configure */
3059   if (pool) {
3060     config = gst_buffer_pool_get_config (pool);
3061     gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3062     gst_buffer_pool_config_set_allocator (config, allocator, &params);
3063
3064     /* buffer pool may have to do some changes */
3065     if (!gst_buffer_pool_set_config (pool, config)) {
3066       config = gst_buffer_pool_get_config (pool);
3067
3068       /* If change are not acceptable, fallback to generic pool */
3069       if (!gst_buffer_pool_config_validate_params (config, outcaps, size, min,
3070               max)) {
3071         GST_DEBUG_OBJECT (basesrc, "unsuported pool, making new pool");
3072
3073         gst_object_unref (pool);
3074         pool = gst_buffer_pool_new ();
3075         gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
3076         gst_buffer_pool_config_set_allocator (config, allocator, &params);
3077       }
3078
3079       if (!gst_buffer_pool_set_config (pool, config))
3080         goto config_failed;
3081     }
3082   }
3083
3084   if (update_allocator)
3085     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
3086   else
3087     gst_query_add_allocation_param (query, allocator, &params);
3088   if (allocator)
3089     gst_object_unref (allocator);
3090
3091   if (pool) {
3092     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
3093     gst_object_unref (pool);
3094   }
3095
3096   return TRUE;
3097
3098 config_failed:
3099   GST_ELEMENT_ERROR (basesrc, RESOURCE, SETTINGS,
3100       ("Failed to configure the buffer pool"),
3101       ("Configuration is most likely invalid, please report this issue."));
3102   return FALSE;
3103 }
3104
3105 static gboolean
3106 gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
3107 {
3108   GstBaseSrcClass *bclass;
3109   gboolean result = TRUE;
3110   GstQuery *query;
3111   GstBufferPool *pool = NULL;
3112   GstAllocator *allocator = NULL;
3113   GstAllocationParams params;
3114
3115   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3116
3117   /* make query and let peer pad answer, we don't really care if it worked or
3118    * not, if it failed, the allocation query would contain defaults and the
3119    * subclass would then set better values if needed */
3120   query = gst_query_new_allocation (caps, TRUE);
3121   if (!gst_pad_peer_query (basesrc->srcpad, query)) {
3122     /* not a problem, just debug a little */
3123     GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
3124   }
3125
3126   g_assert (bclass->decide_allocation != NULL);
3127   result = bclass->decide_allocation (basesrc, query);
3128
3129   GST_DEBUG_OBJECT (basesrc, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
3130       query);
3131
3132   if (!result)
3133     goto no_decide_allocation;
3134
3135   /* we got configuration from our peer or the decide_allocation method,
3136    * parse them */
3137   if (gst_query_get_n_allocation_params (query) > 0) {
3138     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
3139   } else {
3140     allocator = NULL;
3141     gst_allocation_params_init (&params);
3142   }
3143
3144   if (gst_query_get_n_allocation_pools (query) > 0)
3145     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
3146
3147   result = gst_base_src_set_allocation (basesrc, pool, allocator, &params);
3148
3149   if (allocator)
3150     gst_object_unref (allocator);
3151   if (pool)
3152     gst_object_unref (pool);
3153
3154   gst_query_unref (query);
3155
3156   return result;
3157
3158   /* Errors */
3159 no_decide_allocation:
3160   {
3161     GST_WARNING_OBJECT (basesrc, "Subclass failed to decide allocation");
3162     gst_query_unref (query);
3163
3164     return result;
3165   }
3166 }
3167
3168 /* default negotiation code.
3169  *
3170  * Take intersection between src and sink pads, take first
3171  * caps and fixate.
3172  */
3173 static gboolean
3174 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
3175 {
3176   GstCaps *thiscaps;
3177   GstCaps *caps = NULL;
3178   GstCaps *peercaps = NULL;
3179   gboolean result = FALSE;
3180
3181   /* first see what is possible on our source pad */
3182   thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
3183   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
3184   /* nothing or anything is allowed, we're done */
3185   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
3186     goto no_nego_needed;
3187
3188   if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
3189     goto no_caps;
3190
3191   /* get the peer caps */
3192   peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
3193   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
3194   if (peercaps) {
3195     /* The result is already a subset of our caps */
3196     caps = peercaps;
3197     gst_caps_unref (thiscaps);
3198   } else {
3199     /* no peer, work with our own caps then */
3200     caps = thiscaps;
3201   }
3202   if (caps && !gst_caps_is_empty (caps)) {
3203     /* now fixate */
3204     GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
3205     if (gst_caps_is_any (caps)) {
3206       GST_DEBUG_OBJECT (basesrc, "any caps, we stop");
3207       /* hmm, still anything, so element can do anything and
3208        * nego is not needed */
3209       result = TRUE;
3210     } else {
3211       caps = gst_base_src_fixate (basesrc, caps);
3212       GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
3213       if (gst_caps_is_fixed (caps)) {
3214         /* yay, fixed caps, use those then, it's possible that the subclass does
3215          * not accept this caps after all and we have to fail. */
3216         result = gst_base_src_set_caps (basesrc, caps);
3217       }
3218     }
3219     gst_caps_unref (caps);
3220   } else {
3221     if (caps)
3222       gst_caps_unref (caps);
3223     GST_DEBUG_OBJECT (basesrc, "no common caps");
3224   }
3225   return result;
3226
3227 no_nego_needed:
3228   {
3229     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
3230     if (thiscaps)
3231       gst_caps_unref (thiscaps);
3232     return TRUE;
3233   }
3234 no_caps:
3235   {
3236     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
3237         ("No supported formats found"),
3238         ("This element did not produce valid caps"));
3239     if (thiscaps)
3240       gst_caps_unref (thiscaps);
3241     return TRUE;
3242   }
3243 }
3244
3245 static gboolean
3246 gst_base_src_negotiate (GstBaseSrc * basesrc)
3247 {
3248   GstBaseSrcClass *bclass;
3249   gboolean result;
3250
3251   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3252
3253   GST_DEBUG_OBJECT (basesrc, "starting negotiation");
3254
3255   if (G_LIKELY (bclass->negotiate))
3256     result = bclass->negotiate (basesrc);
3257   else
3258     result = TRUE;
3259
3260   if (G_LIKELY (result)) {
3261     GstCaps *caps;
3262
3263     caps = gst_pad_get_current_caps (basesrc->srcpad);
3264
3265     result = gst_base_src_prepare_allocation (basesrc, caps);
3266
3267     if (caps)
3268       gst_caps_unref (caps);
3269   }
3270   return result;
3271 }
3272
3273 static gboolean
3274 gst_base_src_start (GstBaseSrc * basesrc)
3275 {
3276   GstBaseSrcClass *bclass;
3277   gboolean result;
3278
3279   GST_LIVE_LOCK (basesrc);
3280
3281   GST_OBJECT_LOCK (basesrc);
3282   if (GST_BASE_SRC_IS_STARTING (basesrc))
3283     goto was_starting;
3284   if (GST_BASE_SRC_IS_STARTED (basesrc))
3285     goto was_started;
3286
3287   basesrc->priv->start_result = GST_FLOW_FLUSHING;
3288   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3289   gst_segment_init (&basesrc->segment, basesrc->segment.format);
3290   GST_OBJECT_UNLOCK (basesrc);
3291
3292   basesrc->num_buffers_left = basesrc->num_buffers;
3293   basesrc->running = FALSE;
3294   basesrc->priv->segment_pending = FALSE;
3295   basesrc->priv->segment_seqnum = gst_util_seqnum_next ();
3296   basesrc->priv->forced_eos = FALSE;
3297   GST_LIVE_UNLOCK (basesrc);
3298
3299   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3300   if (bclass->start)
3301     result = bclass->start (basesrc);
3302   else
3303     result = TRUE;
3304
3305   if (!result)
3306     goto could_not_start;
3307
3308   if (!gst_base_src_is_async (basesrc)) {
3309     gst_base_src_start_complete (basesrc, GST_FLOW_OK);
3310     /* not really waiting here, we call this to get the result
3311      * from the start_complete call */
3312     result = gst_base_src_start_wait (basesrc) == GST_FLOW_OK;
3313   }
3314
3315   return result;
3316
3317   /* ERROR */
3318 was_starting:
3319   {
3320     GST_DEBUG_OBJECT (basesrc, "was starting");
3321     GST_OBJECT_UNLOCK (basesrc);
3322     GST_LIVE_UNLOCK (basesrc);
3323     return TRUE;
3324   }
3325 was_started:
3326   {
3327     GST_DEBUG_OBJECT (basesrc, "was started");
3328     GST_OBJECT_UNLOCK (basesrc);
3329     GST_LIVE_UNLOCK (basesrc);
3330     return TRUE;
3331   }
3332 could_not_start:
3333   {
3334     GST_DEBUG_OBJECT (basesrc, "could not start");
3335     /* subclass is supposed to post a message. We don't have to call _stop. */
3336     gst_base_src_start_complete (basesrc, GST_FLOW_ERROR);
3337     return FALSE;
3338   }
3339 }
3340
3341 /**
3342  * gst_base_src_start_complete:
3343  * @basesrc: base source instance
3344  * @ret: a #GstFlowReturn
3345  *
3346  * Complete an asynchronous start operation. When the subclass overrides the
3347  * start method, it should call gst_base_src_start_complete() when the start
3348  * operation completes either from the same thread or from an asynchronous
3349  * helper thread.
3350  */
3351 void
3352 gst_base_src_start_complete (GstBaseSrc * basesrc, GstFlowReturn ret)
3353 {
3354   gboolean have_size;
3355   guint64 size;
3356   gboolean seekable;
3357   GstFormat format;
3358   GstPadMode mode;
3359   GstEvent *event;
3360
3361   if (ret != GST_FLOW_OK)
3362     goto error;
3363
3364   GST_DEBUG_OBJECT (basesrc, "starting source");
3365   format = basesrc->segment.format;
3366
3367   /* figure out the size */
3368   have_size = FALSE;
3369   size = -1;
3370   if (format == GST_FORMAT_BYTES) {
3371     GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3372
3373     if (bclass->get_size) {
3374       if (!(have_size = bclass->get_size (basesrc, &size)))
3375         size = -1;
3376     }
3377     GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
3378     /* only update the size when operating in bytes, subclass is supposed
3379      * to set duration in the start method for other formats */
3380     GST_OBJECT_LOCK (basesrc);
3381     basesrc->segment.duration = size;
3382     GST_OBJECT_UNLOCK (basesrc);
3383   }
3384
3385   GST_DEBUG_OBJECT (basesrc,
3386       "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
3387       G_GINT64_FORMAT, gst_format_get_name (format), have_size, size,
3388       basesrc->segment.duration);
3389
3390   seekable = gst_base_src_seekable (basesrc);
3391   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
3392
3393   /* update for random access flag */
3394   basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
3395
3396   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
3397
3398   /* stop flushing now but for live sources, still block in the LIVE lock when
3399    * we are not yet PLAYING */
3400   gst_base_src_set_flushing (basesrc, FALSE, FALSE, NULL);
3401
3402   gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (basesrc));
3403
3404   GST_OBJECT_LOCK (basesrc->srcpad);
3405   mode = GST_PAD_MODE (basesrc->srcpad);
3406   GST_OBJECT_UNLOCK (basesrc->srcpad);
3407
3408   /* take the stream lock here, we only want to let the task run when we have
3409    * set the STARTED flag */
3410   GST_PAD_STREAM_LOCK (basesrc->srcpad);
3411   switch (mode) {
3412     case GST_PAD_MODE_PUSH:
3413       /* do initial seek, which will start the task */
3414       GST_OBJECT_LOCK (basesrc);
3415       event = basesrc->pending_seek;
3416       basesrc->pending_seek = NULL;
3417       GST_OBJECT_UNLOCK (basesrc);
3418
3419       /* The perform seek code will start the task when finished. We don't have to
3420        * unlock the streaming thread because it is not running yet */
3421       if (G_UNLIKELY (!gst_base_src_perform_seek (basesrc, event, FALSE)))
3422         goto seek_failed;
3423
3424       if (event)
3425         gst_event_unref (event);
3426       break;
3427     case GST_PAD_MODE_PULL:
3428       /* if not random_access, we cannot operate in pull mode for now */
3429       if (G_UNLIKELY (!basesrc->random_access))
3430         goto no_get_range;
3431       break;
3432     default:
3433       goto not_activated_yet;
3434       break;
3435   }
3436
3437   GST_OBJECT_LOCK (basesrc);
3438   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3439   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3440   basesrc->priv->start_result = ret;
3441   GST_ASYNC_SIGNAL (basesrc);
3442   GST_OBJECT_UNLOCK (basesrc);
3443
3444   GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3445
3446   return;
3447
3448 seek_failed:
3449   {
3450     GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3451     GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
3452     gst_base_src_stop (basesrc);
3453     if (event)
3454       gst_event_unref (event);
3455     ret = GST_FLOW_ERROR;
3456     goto error;
3457   }
3458 no_get_range:
3459   {
3460     GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3461     gst_base_src_stop (basesrc);
3462     GST_ERROR_OBJECT (basesrc, "Cannot operate in pull mode, stopping");
3463     ret = GST_FLOW_ERROR;
3464     goto error;
3465   }
3466 not_activated_yet:
3467   {
3468     GST_PAD_STREAM_UNLOCK (basesrc->srcpad);
3469     gst_base_src_stop (basesrc);
3470     GST_WARNING_OBJECT (basesrc, "pad not activated yet");
3471     ret = GST_FLOW_ERROR;
3472     goto error;
3473   }
3474 error:
3475   {
3476     GST_OBJECT_LOCK (basesrc);
3477     basesrc->priv->start_result = ret;
3478     GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3479     GST_ASYNC_SIGNAL (basesrc);
3480     GST_OBJECT_UNLOCK (basesrc);
3481     return;
3482   }
3483 }
3484
3485 /**
3486  * gst_base_src_start_wait:
3487  * @basesrc: base source instance
3488  *
3489  * Wait until the start operation completes.
3490  *
3491  * Returns: a #GstFlowReturn.
3492  */
3493 GstFlowReturn
3494 gst_base_src_start_wait (GstBaseSrc * basesrc)
3495 {
3496   GstFlowReturn result;
3497
3498   GST_OBJECT_LOCK (basesrc);
3499   while (GST_BASE_SRC_IS_STARTING (basesrc)) {
3500     GST_ASYNC_WAIT (basesrc);
3501   }
3502   result = basesrc->priv->start_result;
3503   GST_OBJECT_UNLOCK (basesrc);
3504
3505   GST_DEBUG_OBJECT (basesrc, "got %s", gst_flow_get_name (result));
3506
3507   return result;
3508 }
3509
3510 static gboolean
3511 gst_base_src_stop (GstBaseSrc * basesrc)
3512 {
3513   GstBaseSrcClass *bclass;
3514   gboolean result = TRUE;
3515
3516   GST_DEBUG_OBJECT (basesrc, "stopping source");
3517
3518   /* flush all */
3519   gst_base_src_set_flushing (basesrc, TRUE, FALSE, NULL);
3520   /* stop the task */
3521   gst_pad_stop_task (basesrc->srcpad);
3522
3523   GST_OBJECT_LOCK (basesrc);
3524   if (!GST_BASE_SRC_IS_STARTED (basesrc) && !GST_BASE_SRC_IS_STARTING (basesrc))
3525     goto was_stopped;
3526
3527   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTING);
3528   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_FLAG_STARTED);
3529   basesrc->priv->start_result = GST_FLOW_FLUSHING;
3530   GST_ASYNC_SIGNAL (basesrc);
3531   GST_OBJECT_UNLOCK (basesrc);
3532
3533   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3534   if (bclass->stop)
3535     result = bclass->stop (basesrc);
3536
3537   gst_base_src_set_allocation (basesrc, NULL, NULL, NULL);
3538
3539   return result;
3540
3541 was_stopped:
3542   {
3543     GST_DEBUG_OBJECT (basesrc, "was stopped");
3544     GST_OBJECT_UNLOCK (basesrc);
3545     return TRUE;
3546   }
3547 }
3548
3549 /* start or stop flushing dataprocessing
3550  */
3551 static gboolean
3552 gst_base_src_set_flushing (GstBaseSrc * basesrc,
3553     gboolean flushing, gboolean live_play, gboolean * playing)
3554 {
3555   GstBaseSrcClass *bclass;
3556
3557   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3558
3559   GST_DEBUG_OBJECT (basesrc, "flushing %d, live_play %d", flushing, live_play);
3560
3561   if (flushing) {
3562     gst_base_src_activate_pool (basesrc, FALSE);
3563     /* unlock any subclasses, we need to do this before grabbing the
3564      * LIVE_LOCK since we hold this lock before going into ::create. We pass an
3565      * unlock to the params because of backwards compat (see seek handler)*/
3566     if (bclass->unlock)
3567       bclass->unlock (basesrc);
3568   }
3569
3570   /* the live lock is released when we are blocked, waiting for playing or
3571    * when we sync to the clock. */
3572   GST_LIVE_LOCK (basesrc);
3573   if (playing)
3574     *playing = basesrc->live_running;
3575   basesrc->priv->flushing = flushing;
3576   if (flushing) {
3577     /* if we are locked in the live lock, signal it to make it flush */
3578     basesrc->live_running = TRUE;
3579
3580     /* clear pending EOS if any */
3581     if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3582       GST_OBJECT_LOCK (basesrc);
3583       CLEAR_PENDING_EOS (basesrc);
3584       basesrc->priv->forced_eos = FALSE;
3585       GST_OBJECT_UNLOCK (basesrc);
3586     }
3587
3588     /* step 1, now that we have the LIVE lock, clear our unlock request */
3589     if (bclass->unlock_stop)
3590       bclass->unlock_stop (basesrc);
3591
3592     /* step 2, unblock clock sync (if any) or any other blocking thing */
3593     if (basesrc->clock_id)
3594       gst_clock_id_unschedule (basesrc->clock_id);
3595   } else {
3596     /* signal the live source that it can start playing */
3597     basesrc->live_running = live_play;
3598
3599     gst_base_src_activate_pool (basesrc, TRUE);
3600
3601     /* Drop all delayed events */
3602     GST_OBJECT_LOCK (basesrc);
3603     if (basesrc->priv->pending_events) {
3604       g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
3605           NULL);
3606       g_list_free (basesrc->priv->pending_events);
3607       basesrc->priv->pending_events = NULL;
3608       g_atomic_int_set (&basesrc->priv->have_events, FALSE);
3609     }
3610     GST_OBJECT_UNLOCK (basesrc);
3611   }
3612   GST_LIVE_SIGNAL (basesrc);
3613   GST_LIVE_UNLOCK (basesrc);
3614
3615   return TRUE;
3616 }
3617
3618 /* the purpose of this function is to make sure that a live source blocks in the
3619  * LIVE lock or leaves the LIVE lock and continues playing. */
3620 static gboolean
3621 gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
3622 {
3623   GstBaseSrcClass *bclass;
3624
3625   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
3626
3627   /* unlock subclasses locked in ::create, we only do this when we stop playing. */
3628   if (!live_play) {
3629     GST_DEBUG_OBJECT (basesrc, "unlock");
3630     if (bclass->unlock)
3631       bclass->unlock (basesrc);
3632   }
3633
3634   /* we are now able to grab the LIVE lock, when we get it, we can be
3635    * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
3636    * for the clock. */
3637   GST_LIVE_LOCK (basesrc);
3638   GST_DEBUG_OBJECT (basesrc, "unschedule clock");
3639
3640   /* unblock clock sync (if any) */
3641   if (basesrc->clock_id)
3642     gst_clock_id_unschedule (basesrc->clock_id);
3643
3644   /* configure what to do when we get to the LIVE lock. */
3645   GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
3646   basesrc->live_running = live_play;
3647
3648   if (live_play) {
3649     gboolean start;
3650
3651     /* clear our unlock request when going to PLAYING */
3652     GST_DEBUG_OBJECT (basesrc, "unlock stop");
3653     if (bclass->unlock_stop)
3654       bclass->unlock_stop (basesrc);
3655
3656     /* for live sources we restart the timestamp correction */
3657     basesrc->priv->latency = -1;
3658     /* have to restart the task in case it stopped because of the unlock when
3659      * we went to PAUSED. Only do this if we operating in push mode. */
3660     GST_OBJECT_LOCK (basesrc->srcpad);
3661     start = (GST_PAD_MODE (basesrc->srcpad) == GST_PAD_MODE_PUSH);
3662     GST_OBJECT_UNLOCK (basesrc->srcpad);
3663     if (start)
3664       gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
3665           basesrc->srcpad, NULL);
3666     GST_DEBUG_OBJECT (basesrc, "signal");
3667     GST_LIVE_SIGNAL (basesrc);
3668   }
3669   GST_LIVE_UNLOCK (basesrc);
3670
3671   return TRUE;
3672 }
3673
3674 static gboolean
3675 gst_base_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3676 {
3677   GstBaseSrc *basesrc;
3678
3679   basesrc = GST_BASE_SRC (parent);
3680
3681   /* prepare subclass first */
3682   if (active) {
3683     GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
3684
3685     if (G_UNLIKELY (!basesrc->can_activate_push))
3686       goto no_push_activation;
3687
3688     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3689       goto error_start;
3690   } else {
3691     GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
3692     /* now we can stop the source */
3693     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3694       goto error_stop;
3695   }
3696   return TRUE;
3697
3698   /* ERRORS */
3699 no_push_activation:
3700   {
3701     GST_WARNING_OBJECT (basesrc, "Subclass disabled push-mode activation");
3702     return FALSE;
3703   }
3704 error_start:
3705   {
3706     GST_WARNING_OBJECT (basesrc, "Failed to start in push mode");
3707     return FALSE;
3708   }
3709 error_stop:
3710   {
3711     GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
3712     return FALSE;
3713   }
3714 }
3715
3716 static gboolean
3717 gst_base_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3718 {
3719   GstBaseSrc *basesrc;
3720
3721   basesrc = GST_BASE_SRC (parent);
3722
3723   /* prepare subclass first */
3724   if (active) {
3725     GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
3726     if (G_UNLIKELY (!gst_base_src_start (basesrc)))
3727       goto error_start;
3728   } else {
3729     GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
3730     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
3731       goto error_stop;
3732   }
3733   return TRUE;
3734
3735   /* ERRORS */
3736 error_start:
3737   {
3738     GST_ERROR_OBJECT (basesrc, "Failed to start in pull mode");
3739     return FALSE;
3740   }
3741 error_stop:
3742   {
3743     GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
3744     return FALSE;
3745   }
3746 }
3747
3748 static gboolean
3749 gst_base_src_activate_mode (GstPad * pad, GstObject * parent,
3750     GstPadMode mode, gboolean active)
3751 {
3752   gboolean res;
3753   GstBaseSrc *src = GST_BASE_SRC (parent);
3754
3755   src->priv->stream_start_pending = FALSE;
3756
3757   GST_DEBUG_OBJECT (pad, "activating in mode %d", mode);
3758
3759   switch (mode) {
3760     case GST_PAD_MODE_PULL:
3761       res = gst_base_src_activate_pull (pad, parent, active);
3762       break;
3763     case GST_PAD_MODE_PUSH:
3764       src->priv->stream_start_pending = active;
3765       res = gst_base_src_activate_push (pad, parent, active);
3766       break;
3767     default:
3768       GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3769       res = FALSE;
3770       break;
3771   }
3772   return res;
3773 }
3774
3775
3776 static GstStateChangeReturn
3777 gst_base_src_change_state (GstElement * element, GstStateChange transition)
3778 {
3779   GstBaseSrc *basesrc;
3780   GstStateChangeReturn result;
3781   gboolean no_preroll = FALSE;
3782
3783   basesrc = GST_BASE_SRC (element);
3784
3785   switch (transition) {
3786     case GST_STATE_CHANGE_NULL_TO_READY:
3787       break;
3788     case GST_STATE_CHANGE_READY_TO_PAUSED:
3789       no_preroll = gst_base_src_is_live (basesrc);
3790       break;
3791     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3792       GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
3793       if (gst_base_src_is_live (basesrc)) {
3794         /* now we can start playback */
3795         gst_base_src_set_playing (basesrc, TRUE);
3796       }
3797       break;
3798     default:
3799       break;
3800   }
3801
3802   if ((result =
3803           GST_ELEMENT_CLASS (parent_class)->change_state (element,
3804               transition)) == GST_STATE_CHANGE_FAILURE)
3805     goto failure;
3806
3807   switch (transition) {
3808     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3809       GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
3810       if (gst_base_src_is_live (basesrc)) {
3811         /* make sure we block in the live lock in PAUSED */
3812         gst_base_src_set_playing (basesrc, FALSE);
3813         no_preroll = TRUE;
3814       }
3815       break;
3816     case GST_STATE_CHANGE_PAUSED_TO_READY:
3817     {
3818       /* we don't need to unblock anything here, the pad deactivation code
3819        * already did this */
3820       if (g_atomic_int_get (&basesrc->priv->has_pending_eos)) {
3821         GST_OBJECT_LOCK (basesrc);
3822         CLEAR_PENDING_EOS (basesrc);
3823         GST_OBJECT_UNLOCK (basesrc);
3824       }
3825       gst_event_replace (&basesrc->pending_seek, NULL);
3826       break;
3827     }
3828     case GST_STATE_CHANGE_READY_TO_NULL:
3829       break;
3830     default:
3831       break;
3832   }
3833
3834   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
3835     result = GST_STATE_CHANGE_NO_PREROLL;
3836
3837   return result;
3838
3839   /* ERRORS */
3840 failure:
3841   {
3842     GST_DEBUG_OBJECT (basesrc, "parent failed state change");
3843     return result;
3844   }
3845 }
3846
3847 /**
3848  * gst_base_src_get_buffer_pool:
3849  * @src: a #GstBaseSrc
3850  *
3851  * Returns: (transfer full): the instance of the #GstBufferPool used
3852  * by the src; free it after use it
3853  */
3854 GstBufferPool *
3855 gst_base_src_get_buffer_pool (GstBaseSrc * src)
3856 {
3857   g_return_val_if_fail (GST_IS_BASE_SRC (src), NULL);
3858
3859   if (src->priv->pool)
3860     return gst_object_ref (src->priv->pool);
3861
3862   return NULL;
3863 }
3864
3865 /**
3866  * gst_base_src_get_allocator:
3867  * @src: a #GstBaseSrc
3868  * @allocator: (out) (allow-none) (transfer full): the #GstAllocator
3869  * used
3870  * @params: (out) (allow-none) (transfer full): the
3871  * #GstAllocationParams of @allocator
3872  *
3873  * Lets #GstBaseSrc sub-classes to know the memory @allocator
3874  * used by the base class and its @params.
3875  *
3876  * Unref the @allocator after use it.
3877  */
3878 void
3879 gst_base_src_get_allocator (GstBaseSrc * src,
3880     GstAllocator ** allocator, GstAllocationParams * params)
3881 {
3882   g_return_if_fail (GST_IS_BASE_SRC (src));
3883
3884   if (allocator)
3885     *allocator = src->priv->allocator ?
3886         gst_object_ref (src->priv->allocator) : NULL;
3887
3888   if (params)
3889     *params = src->priv->params;
3890 }