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