Debian Codegraph @

libs/gst/base/gstbasesrc.c

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