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