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