1 /* GStreamer
2 * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
3 * Copyright (C) 2007 Jan Schmidt <jan@fluendo.com>
4 * Copyright (C) 2007 Wim Taymans <wim@fluendo.com>
5 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 *
7 * gstmultiqueue.c:
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 */
25 /**
26 * SECTION:element-multiqueue
27 * @see_also: #GstQueue
28 *
29 * <refsect2>
30 * <para>
31 * Multiqueue is similar to a normal #GstQueue with the following additional
32 * features:
33 * <orderedlist>
34 * <listitem>
35 * <itemizedlist><title>Multiple streamhandling</title>
36 * <listitem><para>
37 * The element handles queueing data on more than one stream at once. To
38 * achieve such a feature it has request sink pads (sink%d) and
39 * 'sometimes' src pads (src%d).
40 * </para><para>
41 * When requesting a given sinkpad with gst_element_get_request_pad(),
42 * the associated srcpad for that stream will be created.
43 * Example: requesting sink1 will generate src1.
44 * </para></listitem>
45 * </itemizedlist>
46 * </listitem>
47 * <listitem>
48 * <itemizedlist><title>Non-starvation on multiple streams</title>
49 * <listitem><para>
50 * If more than one stream is used with the element, the streams' queues
51 * will be dynamically grown (up to a limit), in order to ensure that no
52 * stream is risking data starvation. This guarantees that at any given
53 * time there are at least N bytes queued and available for each individual
54 * stream.
55 * </para><para>
56 * If an EOS event comes through a srcpad, the associated queue will be
57 * considered as 'not-empty' in the queue-size-growing algorithm.
58 * </para></listitem>
59 * </itemizedlist>
60 * </listitem>
61 * <listitem>
62 * <itemizedlist><title>Non-linked srcpads graceful handling</title>
63 * <listitem><para>
64 * In order to better support dynamic switching between streams, the multiqueue
65 * (unlike the current GStreamer queue) continues to push buffers on non-linked
66 * pads rather than shutting down.
67 * </para><para>
68 * In addition, to prevent a non-linked stream from very quickly consuming all
69 * available buffers and thus 'racing ahead' of the other streams, the element
70 * must ensure that buffers and inlined events for a non-linked stream are pushed
71 * in the same order as they were received, relative to the other streams
72 * controlled by the element. This means that a buffer cannot be pushed to a
73 * non-linked pad any sooner than buffers in any other stream which were received
74 * before it.
75 * </para></listitem>
76 * </itemizedlist>
77 * </listitem>
78 * </orderedlist>
79 * </para>
80 * <para>
81 * Data is queued until one of the limits specified by the
82 * #GstMultiQueue:max-size-buffers, #GstMultiQueue:max-size-bytes and/or
83 * #GstMultiQueue:max-size-time properties has been reached. Any attempt to push
84 * more buffers into the queue will block the pushing thread until more space
85 * becomes available. #GstMultiQueue:extra-size-buffers,
86 * </para>
87 * <para>
88 * #GstMultiQueue:extra-size-bytes and #GstMultiQueue:extra-size-time are
89 * currently unused.
90 * </para>
91 * <para>
92 * The default queue size limits are 5 buffers, 10MB of data, or
93 * two second worth of data, whichever is reached first. Note that the number
94 * of buffers will dynamically grow depending on the fill level of
95 * other queues.
96 * </para>
97 * <para>
98 * The #GstMultiQueue::underrun signal is emitted when all of the queues
99 * are empty. The #GstMultiQueue::overrun signal is emitted when one of the
100 * queues is filled.
101 * Both signals are emitted from the context of the streaming thread.
102 * </para>
103 * </refsect2>
104 *
105 * Last reviewed on 2008-01-25 (0.10.17)
106 */
108 #ifdef HAVE_CONFIG_H
109 # include "config.h"
110 #endif
112 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
113 * with newer GLib versions (>= 2.31.0) */
114 #define GLIB_DISABLE_DEPRECATION_WARNINGS
116 #include <gst/gst.h>
117 #include <stdio.h>
118 #include "gstmultiqueue.h"
119 #include <gst/glib-compat-private.h>
121 /**
122 * GstSingleQueue:
123 * @sinkpad: associated sink #GstPad
124 * @srcpad: associated source #GstPad
125 *
126 * Structure containing all information and properties about
127 * a single queue.
128 */
129 typedef struct _GstSingleQueue GstSingleQueue;
131 struct _GstSingleQueue
132 {
133 /* unique identifier of the queue */
134 guint id;
136 GstMultiQueue *mqueue;
138 GstPad *sinkpad;
139 GstPad *srcpad;
141 /* flowreturn of previous srcpad push */
142 GstFlowReturn srcresult;
144 /* segments */
145 GstSegment sink_segment;
146 GstSegment src_segment;
148 /* position of src/sink */
149 GstClockTime sinktime, srctime;
150 /* TRUE if either position needs to be recalculated */
151 gboolean sink_tainted, src_tainted;
153 /* queue of data */
154 GstDataQueue *queue;
155 GstDataQueueSize max_size, extra_size;
156 GstClockTime cur_time;
157 gboolean is_eos;
158 gboolean flushing;
160 /* Protected by global lock */
161 guint32 nextid; /* ID of the next object waiting to be pushed */
162 guint32 oldid; /* ID of the last object pushed (last in a series) */
163 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
164 GstClockTime next_time; /* End running time of next buffer to be pushed */
165 GstClockTime last_time; /* Start running time of last pushed buffer */
166 GCond *turn; /* SingleQueue turn waiting conditional */
167 };
170 /* Extension of GstDataQueueItem structure for our usage */
171 typedef struct _GstMultiQueueItem GstMultiQueueItem;
173 struct _GstMultiQueueItem
174 {
175 GstMiniObject *object;
176 guint size;
177 guint64 duration;
178 gboolean visible;
180 GDestroyNotify destroy;
181 guint32 posid;
182 };
184 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, gint id);
185 static void gst_single_queue_free (GstSingleQueue * squeue);
187 static void wake_up_next_non_linked (GstMultiQueue * mq);
188 static void compute_high_id (GstMultiQueue * mq);
189 static void compute_high_time (GstMultiQueue * mq);
190 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
191 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
193 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
194 GST_PAD_SINK,
195 GST_PAD_REQUEST,
196 GST_STATIC_CAPS_ANY);
198 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
199 GST_PAD_SRC,
200 GST_PAD_SOMETIMES,
201 GST_STATIC_CAPS_ANY);
203 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
204 #define GST_CAT_DEFAULT (multi_queue_debug)
206 /* Signals and args */
207 enum
208 {
209 SIGNAL_UNDERRUN,
210 SIGNAL_OVERRUN,
211 LAST_SIGNAL
212 };
214 /* default limits, we try to keep up to 2 seconds of data and if there is not
215 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
216 * there is data in the queues. Normally, the byte and time limits are not hit
217 * in theses conditions. */
218 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
219 #define DEFAULT_MAX_SIZE_BUFFERS 5
220 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
222 /* second limits. When we hit one of the above limits we are probably dealing
223 * with a badly muxed file and we scale the limits to these emergency values.
224 * This is currently not yet implemented.
225 * Since we dynamically scale the queue buffer size up to the limits but avoid
226 * going above the max-size-buffers when we can, we don't really need this
227 * aditional extra size. */
228 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
229 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
230 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
232 #define DEFAULT_USE_BUFFERING FALSE
233 #define DEFAULT_LOW_PERCENT 10
234 #define DEFAULT_HIGH_PERCENT 99
235 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
237 enum
238 {
239 PROP_0,
240 PROP_EXTRA_SIZE_BYTES,
241 PROP_EXTRA_SIZE_BUFFERS,
242 PROP_EXTRA_SIZE_TIME,
243 PROP_MAX_SIZE_BYTES,
244 PROP_MAX_SIZE_BUFFERS,
245 PROP_MAX_SIZE_TIME,
246 PROP_USE_BUFFERING,
247 PROP_LOW_PERCENT,
248 PROP_HIGH_PERCENT,
249 PROP_SYNC_BY_RUNNING_TIME,
250 PROP_LAST
251 };
253 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
254 g_mutex_lock (q->qlock); \
255 } G_STMT_END
257 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
258 g_mutex_unlock (q->qlock); \
259 } G_STMT_END
261 static void gst_multi_queue_finalize (GObject * object);
262 static void gst_multi_queue_set_property (GObject * object,
263 guint prop_id, const GValue * value, GParamSpec * pspec);
264 static void gst_multi_queue_get_property (GObject * object,
265 guint prop_id, GValue * value, GParamSpec * pspec);
267 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
268 GstPadTemplate * temp, const gchar * name);
269 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
270 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
271 element, GstStateChange transition);
273 static void gst_multi_queue_loop (GstPad * pad);
275 #define _do_init(bla) \
276 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
278 GST_BOILERPLATE_FULL (GstMultiQueue, gst_multi_queue, GstElement,
279 GST_TYPE_ELEMENT, _do_init);
281 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
283 static void
284 gst_multi_queue_base_init (gpointer g_class)
285 {
286 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
288 gst_element_class_set_details_simple (gstelement_class,
289 "MultiQueue",
290 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
291 gst_element_class_add_pad_template (gstelement_class,
292 gst_static_pad_template_get (&sinktemplate));
293 gst_element_class_add_pad_template (gstelement_class,
294 gst_static_pad_template_get (&srctemplate));
295 }
297 static void
298 gst_multi_queue_class_init (GstMultiQueueClass * klass)
299 {
300 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
301 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
303 gobject_class->set_property = gst_multi_queue_set_property;
304 gobject_class->get_property = gst_multi_queue_get_property;
306 /* SIGNALS */
308 /**
309 * GstMultiQueue::underrun:
310 * @multiqueue: the multqueue instance
311 *
312 * This signal is emitted from the streaming thread when there is
313 * no data in any of the queues inside the multiqueue instance (underrun).
314 *
315 * This indicates either starvation or EOS from the upstream data sources.
316 */
317 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
318 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
319 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
320 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
322 /**
323 * GstMultiQueue::overrun:
324 * @multiqueue: the multiqueue instance
325 *
326 * Reports that one of the queues in the multiqueue is full (overrun).
327 * A queue is full if the total amount of data inside it (num-buffers, time,
328 * size) is higher than the boundary values which can be set through the
329 * GObject properties.
330 *
331 * This can be used as an indicator of pre-roll.
332 */
333 gst_multi_queue_signals[SIGNAL_OVERRUN] =
334 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
335 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
336 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
338 /* PROPERTIES */
340 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
341 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
342 "Max. amount of data in the queue (bytes, 0=disable)",
343 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
346 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
347 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
348 DEFAULT_MAX_SIZE_BUFFERS,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
351 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
352 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
353 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
356 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
357 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
358 " (NOT IMPLEMENTED)",
359 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
362 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
363 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
364 " (NOT IMPLEMENTED)",
365 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
368 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
369 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
370 " (NOT IMPLEMENTED)",
371 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 /**
375 * GstMultiQueue:use-buffering
376 *
377 * Enable the buffering option in multiqueue so that BUFFERING messages are
378 * emited based on low-/high-percent thresholds.
379 *
380 * Since: 0.10.26
381 */
382 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
383 g_param_spec_boolean ("use-buffering", "Use buffering",
384 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
385 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 /**
387 * GstMultiQueue:low-percent
388 *
389 * Low threshold percent for buffering to start.
390 *
391 * Since: 0.10.26
392 */
393 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
394 g_param_spec_int ("low-percent", "Low percent",
395 "Low threshold for buffering to start", 0, 100,
396 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 /**
398 * GstMultiQueue:high-percent
399 *
400 * High threshold percent for buffering to finish.
401 *
402 * Since: 0.10.26
403 */
404 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
405 g_param_spec_int ("high-percent", "High percent",
406 "High threshold for buffering to finish", 0, 100,
407 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 /**
410 * GstMultiQueue:sync-by-running-time
411 *
412 * If enabled multiqueue will synchronize deactivated or not-linked streams
413 * to the activated and linked streams by taking the running time.
414 * Otherwise multiqueue will synchronize the deactivated or not-linked
415 * streams by keeping the order in which buffers and events arrived compared
416 * to active and linked streams.
417 *
418 * Since: 0.10.36
419 */
420 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
421 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
422 "Synchronize deactivated or not-linked streams by running time",
423 DEFAULT_SYNC_BY_RUNNING_TIME,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 gobject_class->finalize = gst_multi_queue_finalize;
428 gstelement_class->request_new_pad =
429 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
430 gstelement_class->release_pad =
431 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
432 gstelement_class->change_state =
433 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
434 }
436 static void
437 gst_multi_queue_init (GstMultiQueue * mqueue, GstMultiQueueClass * klass)
438 {
439 mqueue->nbqueues = 0;
440 mqueue->queues = NULL;
442 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
443 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
444 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
446 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
447 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
448 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
450 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
451 mqueue->low_percent = DEFAULT_LOW_PERCENT;
452 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
454 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
456 mqueue->counter = 1;
457 mqueue->highid = -1;
458 mqueue->high_time = GST_CLOCK_TIME_NONE;
460 mqueue->qlock = g_mutex_new ();
461 }
463 static void
464 gst_multi_queue_finalize (GObject * object)
465 {
466 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
468 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
469 g_list_free (mqueue->queues);
470 mqueue->queues = NULL;
471 mqueue->queues_cookie++;
473 /* free/unref instance data */
474 g_mutex_free (mqueue->qlock);
476 G_OBJECT_CLASS (parent_class)->finalize (object);
477 }
479 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
480 GList * tmp = mq->queues; \
481 while (tmp) { \
482 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
483 q->max_size.format = mq->max_size.format; \
484 tmp = g_list_next(tmp); \
485 }; \
486 } G_STMT_END
488 static void
489 gst_multi_queue_set_property (GObject * object, guint prop_id,
490 const GValue * value, GParamSpec * pspec)
491 {
492 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
494 switch (prop_id) {
495 case PROP_MAX_SIZE_BYTES:
496 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
497 mq->max_size.bytes = g_value_get_uint (value);
498 SET_CHILD_PROPERTY (mq, bytes);
499 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
500 break;
501 case PROP_MAX_SIZE_BUFFERS:
502 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
503 mq->max_size.visible = g_value_get_uint (value);
504 SET_CHILD_PROPERTY (mq, visible);
505 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
506 break;
507 case PROP_MAX_SIZE_TIME:
508 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
509 mq->max_size.time = g_value_get_uint64 (value);
510 SET_CHILD_PROPERTY (mq, time);
511 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
512 break;
513 case PROP_EXTRA_SIZE_BYTES:
514 mq->extra_size.bytes = g_value_get_uint (value);
515 break;
516 case PROP_EXTRA_SIZE_BUFFERS:
517 mq->extra_size.visible = g_value_get_uint (value);
518 break;
519 case PROP_EXTRA_SIZE_TIME:
520 mq->extra_size.time = g_value_get_uint64 (value);
521 break;
522 case PROP_USE_BUFFERING:
523 mq->use_buffering = g_value_get_boolean (value);
524 break;
525 case PROP_LOW_PERCENT:
526 mq->low_percent = g_value_get_int (value);
527 break;
528 case PROP_HIGH_PERCENT:
529 mq->high_percent = g_value_get_int (value);
530 break;
531 case PROP_SYNC_BY_RUNNING_TIME:
532 mq->sync_by_running_time = g_value_get_boolean (value);
533 break;
534 default:
535 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536 break;
537 }
538 }
540 static void
541 gst_multi_queue_get_property (GObject * object, guint prop_id,
542 GValue * value, GParamSpec * pspec)
543 {
544 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
546 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
548 switch (prop_id) {
549 case PROP_EXTRA_SIZE_BYTES:
550 g_value_set_uint (value, mq->extra_size.bytes);
551 break;
552 case PROP_EXTRA_SIZE_BUFFERS:
553 g_value_set_uint (value, mq->extra_size.visible);
554 break;
555 case PROP_EXTRA_SIZE_TIME:
556 g_value_set_uint64 (value, mq->extra_size.time);
557 break;
558 case PROP_MAX_SIZE_BYTES:
559 g_value_set_uint (value, mq->max_size.bytes);
560 break;
561 case PROP_MAX_SIZE_BUFFERS:
562 g_value_set_uint (value, mq->max_size.visible);
563 break;
564 case PROP_MAX_SIZE_TIME:
565 g_value_set_uint64 (value, mq->max_size.time);
566 break;
567 case PROP_USE_BUFFERING:
568 g_value_set_boolean (value, mq->use_buffering);
569 break;
570 case PROP_LOW_PERCENT:
571 g_value_set_int (value, mq->low_percent);
572 break;
573 case PROP_HIGH_PERCENT:
574 g_value_set_int (value, mq->high_percent);
575 break;
576 case PROP_SYNC_BY_RUNNING_TIME:
577 g_value_set_boolean (value, mq->sync_by_running_time);
578 break;
579 default:
580 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581 break;
582 }
584 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
585 }
587 static GstIterator *
588 gst_multi_queue_iterate_internal_links (GstPad * pad)
589 {
590 GstIterator *it = NULL;
591 GstPad *opad;
592 GstSingleQueue *squeue;
593 GstMultiQueue *mq = GST_MULTI_QUEUE (gst_pad_get_parent (pad));
595 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
596 squeue = gst_pad_get_element_private (pad);
597 if (!squeue)
598 goto out;
600 if (squeue->sinkpad == pad)
601 opad = gst_object_ref (squeue->srcpad);
602 else if (squeue->srcpad == pad)
603 opad = gst_object_ref (squeue->sinkpad);
604 else
605 goto out;
607 it = gst_iterator_new_single (GST_TYPE_PAD, opad,
608 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
610 gst_object_unref (opad);
612 out:
613 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
614 gst_object_unref (mq);
616 return it;
617 }
620 /*
621 * GstElement methods
622 */
624 static GstPad *
625 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
626 const gchar * name)
627 {
628 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
629 GstSingleQueue *squeue;
630 gint temp_id = -1;
632 if (name) {
633 sscanf (name + 4, "%d", &temp_id);
634 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
635 }
637 /* Create a new single queue, add the sink and source pad and return the sink pad */
638 squeue = gst_single_queue_new (mqueue, temp_id);
640 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
641 GST_DEBUG_PAD_NAME (squeue->sinkpad));
643 return squeue ? squeue->sinkpad : NULL;
644 }
646 static void
647 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
648 {
649 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
650 GstSingleQueue *sq = NULL;
651 GList *tmp;
653 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
655 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
656 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
657 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
658 sq = (GstSingleQueue *) tmp->data;
660 if (sq->sinkpad == pad)
661 break;
662 }
664 if (!tmp) {
665 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
666 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
667 return;
668 }
670 /* FIXME: The removal of the singlequeue should probably not happen until it
671 * finishes draining */
673 /* remove it from the list */
674 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
675 mqueue->queues_cookie++;
677 /* FIXME : recompute next-non-linked */
678 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
680 /* delete SingleQueue */
681 gst_data_queue_set_flushing (sq->queue, TRUE);
683 gst_pad_set_active (sq->srcpad, FALSE);
684 gst_pad_set_active (sq->sinkpad, FALSE);
685 gst_pad_set_element_private (sq->srcpad, NULL);
686 gst_pad_set_element_private (sq->sinkpad, NULL);
687 gst_element_remove_pad (element, sq->srcpad);
688 gst_element_remove_pad (element, sq->sinkpad);
689 gst_single_queue_free (sq);
690 }
692 static GstStateChangeReturn
693 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
694 {
695 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
696 GstSingleQueue *sq = NULL;
697 GstStateChangeReturn result;
699 switch (transition) {
700 case GST_STATE_CHANGE_READY_TO_PAUSED:{
701 GList *tmp;
703 /* Set all pads to non-flushing */
704 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
705 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
706 sq = (GstSingleQueue *) tmp->data;
707 sq->flushing = FALSE;
708 }
709 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
710 break;
711 }
712 case GST_STATE_CHANGE_PAUSED_TO_READY:{
713 GList *tmp;
715 /* Un-wait all waiting pads */
716 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
717 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
718 sq = (GstSingleQueue *) tmp->data;
719 sq->flushing = TRUE;
720 g_cond_signal (sq->turn);
721 }
722 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
723 break;
724 }
725 default:
726 break;
727 }
729 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
731 switch (transition) {
732 default:
733 break;
734 }
736 return result;
740 }
742 static gboolean
743 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
744 {
745 gboolean result;
747 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
748 sq->id);
750 if (flush) {
751 sq->srcresult = GST_FLOW_WRONG_STATE;
752 gst_data_queue_set_flushing (sq->queue, TRUE);
754 sq->flushing = TRUE;
756 /* wake up non-linked task */
757 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
758 sq->id);
759 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
760 g_cond_signal (sq->turn);
761 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
763 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
764 result = gst_pad_pause_task (sq->srcpad);
765 sq->sink_tainted = sq->src_tainted = TRUE;
766 } else {
767 gst_data_queue_flush (sq->queue);
768 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
769 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
770 /* All pads start off not-linked for a smooth kick-off */
771 sq->srcresult = GST_FLOW_OK;
772 sq->cur_time = 0;
773 sq->max_size.visible = mq->max_size.visible;
774 sq->is_eos = FALSE;
775 sq->nextid = 0;
776 sq->oldid = 0;
777 sq->last_oldid = G_MAXUINT32;
778 sq->next_time = GST_CLOCK_TIME_NONE;
779 sq->last_time = GST_CLOCK_TIME_NONE;
780 gst_data_queue_set_flushing (sq->queue, FALSE);
782 /* Reset high time to be recomputed next */
783 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
784 mq->high_time = GST_CLOCK_TIME_NONE;
785 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
787 sq->flushing = FALSE;
789 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
790 result =
791 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
792 sq->srcpad);
793 }
794 return result;
795 }
797 static void
798 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
799 {
800 GstDataQueueSize size;
801 gint percent, tmp;
802 gboolean post = FALSE;
804 /* nothing to dowhen we are not in buffering mode */
805 if (!mq->use_buffering)
806 return;
808 gst_data_queue_get_level (sq->queue, &size);
810 GST_DEBUG_OBJECT (mq,
811 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
812 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
813 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
815 /* get bytes and time percentages and take the max */
816 if (sq->is_eos) {
817 percent = 100;
818 } else {
819 percent = 0;
820 if (sq->max_size.time > 0) {
821 tmp = (sq->cur_time * 100) / sq->max_size.time;
822 percent = MAX (percent, tmp);
823 }
824 if (sq->max_size.bytes > 0) {
825 tmp = (size.bytes * 100) / sq->max_size.bytes;
826 percent = MAX (percent, tmp);
827 }
828 }
830 if (mq->buffering) {
831 post = TRUE;
832 if (percent >= mq->high_percent) {
833 mq->buffering = FALSE;
834 }
835 /* make sure it increases */
836 percent = MAX (mq->percent, percent);
838 if (percent == mq->percent)
839 /* don't post if nothing changed */
840 post = FALSE;
841 else
842 /* else keep last value we posted */
843 mq->percent = percent;
844 } else {
845 if (percent < mq->low_percent) {
846 mq->buffering = TRUE;
847 mq->percent = percent;
848 post = TRUE;
849 }
850 }
851 if (post) {
852 GstMessage *message;
854 /* scale to high percent so that it becomes the 100% mark */
855 percent = percent * 100 / mq->high_percent;
856 /* clip */
857 if (percent > 100)
858 percent = 100;
860 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
861 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
863 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
864 } else {
865 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
866 }
867 }
869 /* calculate the diff between running time on the sink and src of the queue.
870 * This is the total amount of time in the queue.
871 * WITH LOCK TAKEN */
872 static void
873 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
874 {
875 gint64 sink_time, src_time;
877 if (sq->sink_tainted) {
878 sink_time = sq->sinktime =
879 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
880 sq->sink_segment.last_stop);
882 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
883 /* if we have a time, we become untainted and use the time */
884 sq->sink_tainted = FALSE;
885 } else
886 sink_time = sq->sinktime;
888 if (sq->src_tainted) {
889 src_time = sq->srctime =
890 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
891 sq->src_segment.last_stop);
892 /* if we have a time, we become untainted and use the time */
893 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
894 sq->src_tainted = FALSE;
895 } else
896 src_time = sq->srctime;
898 GST_DEBUG_OBJECT (mq,
899 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
900 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
902 /* This allows for streams with out of order timestamping - sometimes the
903 * emerging timestamp is later than the arriving one(s) */
904 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
905 sq->cur_time = sink_time - src_time;
906 else
907 sq->cur_time = 0;
909 /* updating the time level can change the buffering state */
910 update_buffering (mq, sq);
912 return;
913 }
915 /* take a NEWSEGMENT event and apply the values to segment, updating the time
916 * level of queue. */
917 static void
918 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
919 GstSegment * segment)
920 {
921 gboolean update;
922 GstFormat format;
923 gdouble rate, arate;
924 gint64 start, stop, time;
926 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
927 &format, &start, &stop, &time);
929 /* now configure the values, we use these to track timestamps on the
930 * sinkpad. */
931 if (format != GST_FORMAT_TIME) {
932 /* non-time format, pretent the current time segment is closed with a
933 * 0 start and unknown stop time. */
934 update = FALSE;
935 format = GST_FORMAT_TIME;
936 start = 0;
937 stop = -1;
938 time = 0;
939 }
941 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
943 gst_segment_set_newsegment_full (segment, update,
944 rate, arate, format, start, stop, time);
946 if (segment == &sq->sink_segment)
947 sq->sink_tainted = TRUE;
948 else
949 sq->src_tainted = TRUE;
951 GST_DEBUG_OBJECT (mq,
952 "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
954 /* segment can update the time level of the queue */
955 update_time_level (mq, sq);
957 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
958 }
960 /* take a buffer and update segment, updating the time level of the queue. */
961 static void
962 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
963 GstClockTime duration, GstSegment * segment)
964 {
965 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
967 /* if no timestamp is set, assume it's continuous with the previous
968 * time */
969 if (timestamp == GST_CLOCK_TIME_NONE)
970 timestamp = segment->last_stop;
972 /* add duration */
973 if (duration != GST_CLOCK_TIME_NONE)
974 timestamp += duration;
976 GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
977 sq->id, GST_TIME_ARGS (timestamp));
979 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
981 if (segment == &sq->sink_segment)
982 sq->sink_tainted = TRUE;
983 else
984 sq->src_tainted = TRUE;
986 /* calc diff with other end */
987 update_time_level (mq, sq);
988 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
989 }
991 static GstClockTime
992 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
993 {
994 GstClockTime time = GST_CLOCK_TIME_NONE;
996 if (GST_IS_BUFFER (object)) {
997 GstBuffer *buf = GST_BUFFER_CAST (object);
999 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1000 time = GST_BUFFER_TIMESTAMP (buf);
1001 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1002 time += GST_BUFFER_DURATION (buf);
1003 if (time > segment->stop)
1004 time = segment->stop;
1005 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1006 }
1007 } else if (GST_IS_BUFFER_LIST (object)) {
1008 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1009 GstBufferListIterator *it = gst_buffer_list_iterate (list);
1010 GstBuffer *buf;
1012 do {
1013 while ((buf = gst_buffer_list_iterator_next (it))) {
1014 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1015 time = GST_BUFFER_TIMESTAMP (buf);
1016 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1017 time += GST_BUFFER_DURATION (buf);
1018 if (time > segment->stop)
1019 time = segment->stop;
1020 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1021 if (!end)
1022 goto done;
1023 } else if (!end) {
1024 goto done;
1025 }
1026 }
1027 } while (gst_buffer_list_iterator_next_group (it));
1028 } else if (GST_IS_EVENT (object)) {
1029 GstEvent *event = GST_EVENT_CAST (object);
1031 /* For newsegment events return the running time of the start position */
1032 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
1033 GstSegment new_segment = *segment;
1034 gboolean update;
1035 gdouble rate, applied_rate;
1036 GstFormat format;
1037 gint64 start, stop, position;
1039 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1040 &format, &start, &stop, &position);
1041 if (format == GST_FORMAT_TIME) {
1042 gst_segment_set_newsegment_full (&new_segment, update, rate,
1043 applied_rate, format, start, stop, position);
1045 time =
1046 gst_segment_to_running_time (&new_segment, GST_FORMAT_TIME,
1047 new_segment.start);
1048 }
1049 }
1050 }
1052 done:
1053 return time;
1054 }
1056 static GstFlowReturn
1057 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1058 GstMiniObject * object)
1059 {
1060 GstFlowReturn result = GST_FLOW_OK;
1062 if (GST_IS_BUFFER (object)) {
1063 GstBuffer *buffer;
1064 GstClockTime timestamp, duration;
1065 GstCaps *caps;
1067 buffer = GST_BUFFER_CAST (object);
1068 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1069 duration = GST_BUFFER_DURATION (buffer);
1070 caps = GST_BUFFER_CAPS (buffer);
1072 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1074 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1075 gst_data_queue_limits_changed (sq->queue);
1077 GST_DEBUG_OBJECT (mq,
1078 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1079 sq->id, buffer, GST_TIME_ARGS (timestamp));
1081 /* Set caps on pad before pushing, this avoids core calling the acceptcaps
1082 * function on the srcpad, which will call acceptcaps upstream, which might
1083 * not accept these caps (anymore). */
1084 if (caps && caps != GST_PAD_CAPS (sq->srcpad))
1085 gst_pad_set_caps (sq->srcpad, caps);
1087 result = gst_pad_push (sq->srcpad, buffer);
1088 } else if (GST_IS_EVENT (object)) {
1089 GstEvent *event;
1091 event = GST_EVENT_CAST (object);
1093 switch (GST_EVENT_TYPE (event)) {
1094 case GST_EVENT_EOS:
1095 result = GST_FLOW_UNEXPECTED;
1096 break;
1097 case GST_EVENT_NEWSEGMENT:
1098 apply_segment (mq, sq, event, &sq->src_segment);
1099 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1100 gst_data_queue_limits_changed (sq->queue);
1101 break;
1102 default:
1103 break;
1104 }
1106 GST_DEBUG_OBJECT (mq,
1107 "SingleQueue %d : Pushing event %p of type %s",
1108 sq->id, event, GST_EVENT_TYPE_NAME (event));
1110 gst_pad_push_event (sq->srcpad, event);
1111 } else {
1112 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
1113 sq->id);
1114 }
1115 return result;
1117 /* ERRORS */
1118 }
1120 static GstMiniObject *
1121 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1122 {
1123 GstMiniObject *res;
1125 res = item->object;
1126 item->object = NULL;
1128 return res;
1129 }
1131 static void
1132 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1133 {
1134 if (item->object)
1135 gst_mini_object_unref (item->object);
1136 g_slice_free (GstMultiQueueItem, item);
1137 }
1139 /* takes ownership of passed mini object! */
1140 static GstMultiQueueItem *
1141 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1142 {
1143 GstMultiQueueItem *item;
1145 item = g_slice_new (GstMultiQueueItem);
1146 item->object = object;
1147 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1148 item->posid = curid;
1150 item->size = GST_BUFFER_SIZE (object);
1151 item->duration = GST_BUFFER_DURATION (object);
1152 if (item->duration == GST_CLOCK_TIME_NONE)
1153 item->duration = 0;
1154 item->visible = TRUE;
1155 return item;
1156 }
1158 static GstMultiQueueItem *
1159 gst_multi_queue_event_item_new (GstMiniObject * object, guint32 curid)
1160 {
1161 GstMultiQueueItem *item;
1163 item = g_slice_new (GstMultiQueueItem);
1164 item->object = object;
1165 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1166 item->posid = curid;
1168 item->size = 0;
1169 item->duration = 0;
1170 item->visible = FALSE;
1171 return item;
1172 }
1174 /* Each main loop attempts to push buffers until the return value
1175 * is not-linked. not-linked pads are not allowed to push data beyond
1176 * any linked pads, so they don't 'rush ahead of the pack'.
1177 */
1178 static void
1179 gst_multi_queue_loop (GstPad * pad)
1180 {
1181 GstSingleQueue *sq;
1182 GstMultiQueueItem *item;
1183 GstDataQueueItem *sitem;
1184 GstMultiQueue *mq;
1185 GstMiniObject *object = NULL;
1186 guint32 newid;
1187 GstFlowReturn result;
1188 GstClockTime next_time;
1190 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1191 mq = sq->mqueue;
1193 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1195 if (sq->flushing)
1196 goto out_flushing;
1198 /* Get something from the queue, blocking until that happens, or we get
1199 * flushed */
1200 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1201 goto out_flushing;
1203 item = (GstMultiQueueItem *) sitem;
1204 newid = item->posid;
1206 /* steal the object and destroy the item */
1207 object = gst_multi_queue_item_steal_object (item);
1208 gst_multi_queue_item_destroy (item);
1210 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1211 next_time = get_running_time (&sq->src_segment, object, TRUE);
1213 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1214 sq->id, newid, sq->last_oldid);
1216 /* If we're not-linked, we do some extra work because we might need to
1217 * wait before pushing. If we're linked but there's a gap in the IDs,
1218 * or it's the first loop, or we just passed the previous highid,
1219 * we might need to wake some sleeping pad up, so there's extra work
1220 * there too */
1221 if (sq->srcresult == GST_FLOW_NOT_LINKED
1222 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1223 || sq->last_oldid > mq->highid) {
1224 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1225 gst_flow_get_name (sq->srcresult));
1227 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1229 /* Check again if we're flushing after the lock is taken,
1230 * the flush flag might have been changed in the meantime */
1231 if (sq->flushing) {
1232 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1233 goto out_flushing;
1234 }
1236 /* Update the nextid so other threads know when to wake us up */
1237 sq->nextid = newid;
1238 sq->next_time = next_time;
1240 /* Update the oldid (the last ID we output) for highid tracking */
1241 if (sq->last_oldid != G_MAXUINT32)
1242 sq->oldid = sq->last_oldid;
1244 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1245 /* Go to sleep until it's time to push this buffer */
1247 /* Recompute the highid */
1248 compute_high_id (mq);
1249 /* Recompute the high time */
1250 compute_high_time (mq);
1252 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1253 (mq->high_time == GST_CLOCK_TIME_NONE
1254 || next_time >= mq->high_time))
1255 || (!mq->sync_by_running_time && newid > mq->highid))
1256 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1258 GST_DEBUG_OBJECT (mq,
1259 "queue %d sleeping for not-linked wakeup with "
1260 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1261 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1262 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1264 /* Wake up all non-linked pads before we sleep */
1265 wake_up_next_non_linked (mq);
1267 mq->numwaiting++;
1268 g_cond_wait (sq->turn, mq->qlock);
1269 mq->numwaiting--;
1271 if (sq->flushing) {
1272 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1273 goto out_flushing;
1274 }
1276 /* Recompute the high time */
1277 compute_high_time (mq);
1279 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1280 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1281 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1282 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1283 }
1285 /* Re-compute the high_id in case someone else pushed */
1286 compute_high_id (mq);
1287 } else {
1288 compute_high_id (mq);
1289 /* Wake up all non-linked pads */
1290 wake_up_next_non_linked (mq);
1291 }
1292 /* We're done waiting, we can clear the nextid and nexttime */
1293 sq->nextid = 0;
1294 sq->next_time = GST_CLOCK_TIME_NONE;
1296 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1297 }
1299 if (sq->flushing)
1300 goto out_flushing;
1302 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1303 gst_flow_get_name (sq->srcresult));
1305 /* Update time stats */
1306 next_time = get_running_time (&sq->src_segment, object, FALSE);
1307 if (next_time != GST_CLOCK_TIME_NONE) {
1308 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1309 sq->last_time = next_time;
1310 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1311 /* Wake up all non-linked pads now that we advanced the high time */
1312 mq->high_time = next_time;
1313 wake_up_next_non_linked (mq);
1314 }
1315 }
1317 /* Try to push out the new object */
1318 result = gst_single_queue_push_one (mq, sq, object);
1319 sq->srcresult = result;
1320 object = NULL;
1322 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1323 && result != GST_FLOW_UNEXPECTED)
1324 goto out_flushing;
1326 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1327 gst_flow_get_name (sq->srcresult));
1329 sq->last_oldid = newid;
1331 return;
1333 out_flushing:
1334 {
1335 if (object)
1336 gst_mini_object_unref (object);
1338 /* Need to make sure wake up any sleeping pads when we exit */
1339 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1340 compute_high_id (mq);
1341 wake_up_next_non_linked (mq);
1342 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1344 /* upstream needs to see fatal result ASAP to shut things down,
1345 * but might be stuck in one of our other full queues;
1346 * so empty this one and trigger dynamic queue growth. At
1347 * this point the srcresult is not OK, NOT_LINKED
1348 * or UNEXPECTED, i.e. a real failure */
1349 gst_data_queue_flush (sq->queue);
1350 single_queue_underrun_cb (sq->queue, sq);
1351 gst_data_queue_set_flushing (sq->queue, TRUE);
1352 gst_pad_pause_task (sq->srcpad);
1353 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1354 "SingleQueue[%d] task paused, reason:%s",
1355 sq->id, gst_flow_get_name (sq->srcresult));
1356 return;
1357 }
1358 }
1360 /**
1361 * gst_multi_queue_chain:
1362 *
1363 * This is similar to GstQueue's chain function, except:
1364 * _ we don't have leak behaviours,
1365 * _ we push with a unique id (curid)
1366 */
1367 static GstFlowReturn
1368 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
1369 {
1370 GstSingleQueue *sq;
1371 GstMultiQueue *mq;
1372 GstMultiQueueItem *item;
1373 guint32 curid;
1374 GstClockTime timestamp, duration;
1376 sq = gst_pad_get_element_private (pad);
1377 mq = sq->mqueue;
1379 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1380 if (sq->is_eos)
1381 goto was_eos;
1383 /* Get a unique incrementing id */
1384 curid = G_ATOMIC_INT_ADD ((gint *) & mq->counter, 1);
1386 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1387 sq->id, buffer, curid);
1389 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1391 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1392 duration = GST_BUFFER_DURATION (buffer);
1394 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1395 goto flushing;
1397 /* update time level, we must do this after pushing the data in the queue so
1398 * that we never end up filling the queue first. */
1399 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1401 done:
1402 return sq->srcresult;
1404 /* ERRORS */
1405 flushing:
1406 {
1407 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1408 sq->id, gst_flow_get_name (sq->srcresult));
1409 gst_multi_queue_item_destroy (item);
1410 goto done;
1411 }
1412 was_eos:
1413 {
1414 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return UNEXPECTED");
1415 gst_buffer_unref (buffer);
1416 return GST_FLOW_UNEXPECTED;
1417 }
1418 }
1420 static gboolean
1421 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
1422 {
1423 GstSingleQueue *sq;
1425 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1427 if (active) {
1428 /* All pads start off linked until they push one buffer */
1429 sq->srcresult = GST_FLOW_OK;
1430 } else {
1431 sq->srcresult = GST_FLOW_WRONG_STATE;
1432 gst_data_queue_flush (sq->queue);
1433 }
1434 return TRUE;
1435 }
1437 static gboolean
1438 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
1439 {
1440 GstSingleQueue *sq;
1441 GstMultiQueue *mq;
1442 guint32 curid;
1443 GstMultiQueueItem *item;
1444 gboolean res;
1445 GstEventType type;
1446 GstEvent *sref = NULL;
1448 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1449 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1451 type = GST_EVENT_TYPE (event);
1453 switch (type) {
1454 case GST_EVENT_FLUSH_START:
1455 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1456 sq->id);
1458 res = gst_pad_push_event (sq->srcpad, event);
1460 gst_single_queue_flush (mq, sq, TRUE);
1461 goto done;
1463 case GST_EVENT_FLUSH_STOP:
1464 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1465 sq->id);
1467 res = gst_pad_push_event (sq->srcpad, event);
1469 gst_single_queue_flush (mq, sq, FALSE);
1470 goto done;
1471 case GST_EVENT_NEWSEGMENT:
1472 /* take ref because the queue will take ownership and we need the event
1473 * afterwards to update the segment */
1474 sref = gst_event_ref (event);
1475 break;
1477 default:
1478 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1479 res = gst_pad_push_event (sq->srcpad, event);
1480 goto done;
1481 }
1482 break;
1483 }
1485 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1486 if (sq->is_eos)
1487 goto was_eos;
1489 /* Get an unique incrementing id. */
1490 curid = G_ATOMIC_INT_ADD ((gint *) & mq->counter, 1);
1492 item = gst_multi_queue_event_item_new ((GstMiniObject *) event, curid);
1494 GST_DEBUG_OBJECT (mq,
1495 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1496 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1498 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1499 goto flushing;
1501 /* mark EOS when we received one, we must do that after putting the
1502 * buffer in the queue because EOS marks the buffer as filled. No need to take
1503 * a lock, the _check_full happens from this thread only, right before pushing
1504 * into dataqueue. */
1505 switch (type) {
1506 case GST_EVENT_EOS:
1507 sq->is_eos = TRUE;
1508 /* EOS affects the buffering state */
1509 update_buffering (mq, sq);
1510 single_queue_overrun_cb (sq->queue, sq);
1511 break;
1512 case GST_EVENT_NEWSEGMENT:
1513 apply_segment (mq, sq, sref, &sq->sink_segment);
1514 gst_event_unref (sref);
1515 break;
1516 default:
1517 break;
1518 }
1519 done:
1520 gst_object_unref (mq);
1521 return res;
1523 flushing:
1524 {
1525 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1526 sq->id, gst_flow_get_name (sq->srcresult));
1527 if (sref)
1528 gst_event_unref (sref);
1529 gst_multi_queue_item_destroy (item);
1530 goto done;
1531 }
1532 was_eos:
1533 {
1534 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1535 gst_event_unref (event);
1536 res = FALSE;
1537 goto done;
1538 }
1539 }
1541 static GstCaps *
1542 gst_multi_queue_getcaps (GstPad * pad)
1543 {
1544 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1545 GstPad *otherpad;
1546 GstCaps *result;
1548 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1550 GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
1552 result = gst_pad_peer_get_caps (otherpad);
1553 if (result == NULL)
1554 result = gst_caps_new_any ();
1556 return result;
1557 }
1559 static gboolean
1560 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
1561 {
1562 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1563 GstPad *otherpad;
1564 gboolean result;
1566 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1568 GST_LOG_OBJECT (otherpad, "Accept caps from the peer of this pad");
1570 result = gst_pad_peer_accept_caps (otherpad, caps);
1572 return result;
1573 }
1575 static GstFlowReturn
1576 gst_multi_queue_bufferalloc (GstPad * pad, guint64 offset, guint size,
1577 GstCaps * caps, GstBuffer ** buf)
1578 {
1579 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1581 return gst_pad_alloc_buffer (sq->srcpad, offset, size, caps, buf);
1582 }
1584 static gboolean
1585 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
1586 {
1587 GstMultiQueue *mq;
1588 GstSingleQueue *sq;
1589 gboolean result = FALSE;
1591 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1592 mq = sq->mqueue;
1594 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1596 if (active) {
1597 result = gst_single_queue_flush (mq, sq, FALSE);
1598 } else {
1599 result = gst_single_queue_flush (mq, sq, TRUE);
1600 /* make sure streaming finishes */
1601 result |= gst_pad_stop_task (pad);
1602 }
1603 return result;
1604 }
1606 static gboolean
1607 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
1608 {
1609 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1611 return gst_pad_push_event (sq->sinkpad, event);
1612 }
1614 static gboolean
1615 gst_multi_queue_src_query (GstPad * pad, GstQuery * query)
1616 {
1617 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1618 GstPad *peerpad;
1619 gboolean res;
1621 /* FIXME, Handle position offset depending on queue size */
1623 /* default handling */
1624 if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
1625 goto no_peer;
1627 res = gst_pad_query (peerpad, query);
1629 gst_object_unref (peerpad);
1631 return res;
1633 /* ERRORS */
1634 no_peer:
1635 {
1636 GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
1637 return FALSE;
1638 }
1639 }
1641 /*
1642 * Next-non-linked functions
1643 */
1645 /* WITH LOCK TAKEN */
1646 static void
1647 wake_up_next_non_linked (GstMultiQueue * mq)
1648 {
1649 GList *tmp;
1651 /* maybe no-one is waiting */
1652 if (mq->numwaiting < 1)
1653 return;
1655 /* Else figure out which singlequeue(s) need waking up */
1656 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1657 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1659 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1660 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1661 && sq->next_time != GST_CLOCK_TIME_NONE
1662 && sq->next_time >= mq->high_time)
1663 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1664 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1665 g_cond_signal (sq->turn);
1666 }
1667 }
1668 }
1669 }
1671 /* WITH LOCK TAKEN */
1672 static void
1673 compute_high_id (GstMultiQueue * mq)
1674 {
1675 /* The high-id is either the highest id among the linked pads, or if all
1676 * pads are not-linked, it's the lowest not-linked pad */
1677 GList *tmp;
1678 guint32 lowest = G_MAXUINT32;
1679 guint32 highid = G_MAXUINT32;
1681 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1682 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1684 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1685 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1687 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1688 /* No need to consider queues which are not waiting */
1689 if (sq->nextid == 0) {
1690 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1691 continue;
1692 }
1694 if (sq->nextid < lowest)
1695 lowest = sq->nextid;
1696 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1697 /* If we don't have a global highid, or the global highid is lower than
1698 * this single queue's last outputted id, store the queue's one,
1699 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1700 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1701 highid = sq->oldid;
1702 }
1703 }
1705 if (highid == G_MAXUINT32 || lowest < highid)
1706 mq->highid = lowest;
1707 else
1708 mq->highid = highid;
1710 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1711 lowest);
1712 }
1714 /* WITH LOCK TAKEN */
1715 static void
1716 compute_high_time (GstMultiQueue * mq)
1717 {
1718 /* The high-id is either the highest id among the linked pads, or if all
1719 * pads are not-linked, it's the lowest not-linked pad */
1720 GList *tmp;
1721 GstClockTime highest = GST_CLOCK_TIME_NONE;
1722 GstClockTime lowest = GST_CLOCK_TIME_NONE;
1724 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1725 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1727 GST_LOG_OBJECT (mq,
1728 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
1729 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
1730 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
1732 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1733 /* No need to consider queues which are not waiting */
1734 if (sq->next_time == GST_CLOCK_TIME_NONE) {
1735 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1736 continue;
1737 }
1739 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
1740 lowest = sq->next_time;
1741 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1742 /* If we don't have a global highid, or the global highid is lower than
1743 * this single queue's last outputted id, store the queue's one,
1744 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1745 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
1746 highest = sq->last_time;
1747 }
1748 }
1750 mq->high_time = highest;
1752 GST_LOG_OBJECT (mq,
1753 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
1754 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
1755 }
1757 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1758 ((q)->max_size.format) <= (value))
1760 /*
1761 * GstSingleQueue functions
1762 */
1763 static void
1764 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1765 {
1766 GstMultiQueue *mq = sq->mqueue;
1767 GList *tmp;
1768 GstDataQueueSize size;
1769 gboolean filled = FALSE;
1771 gst_data_queue_get_level (sq->queue, &size);
1773 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1775 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1776 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1777 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1778 GstDataQueueSize ssize;
1780 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1782 if (gst_data_queue_is_empty (oq->queue)) {
1783 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1784 if (IS_FILLED (sq, visible, size.visible)) {
1785 sq->max_size.visible = size.visible + 1;
1786 GST_DEBUG_OBJECT (mq,
1787 "Another queue is empty, bumping single queue %d max visible to %d",
1788 sq->id, sq->max_size.visible);
1789 }
1790 }
1791 /* check if we reached the hard time/bytes limits */
1792 gst_data_queue_get_level (oq->queue, &ssize);
1794 GST_DEBUG_OBJECT (mq,
1795 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1796 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1797 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1799 /* if this queue is filled completely we must signal overrun.
1800 * FIXME, this seems wrong in many ways
1801 * - we're comparing the filled level of this queue against the
1802 * values of the other one
1803 * - we should only do this after we found no empty queues, ie, move
1804 * this check outside of the loop
1805 * - the debug statement talks about a different queue than the one
1806 * we are checking here.
1807 */
1808 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1809 IS_FILLED (sq, time, sq->cur_time)) {
1810 GST_LOG_OBJECT (mq, "Queue %d is filled", oq->id);
1811 filled = TRUE;
1812 }
1813 }
1814 /* no queues were empty */
1815 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1817 /* Overrun is always forwarded, since this is blocking the upstream element */
1818 if (filled) {
1819 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1820 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1821 }
1823 return;
1824 }
1826 static void
1827 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1828 {
1829 gboolean empty = TRUE;
1830 GstMultiQueue *mq = sq->mqueue;
1831 GList *tmp;
1833 GST_LOG_OBJECT (mq,
1834 "Single Queue %d is empty, Checking other single queues", sq->id);
1836 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1837 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1838 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1840 if (gst_data_queue_is_full (oq->queue)) {
1841 GstDataQueueSize size;
1843 gst_data_queue_get_level (oq->queue, &size);
1844 if (IS_FILLED (oq, visible, size.visible)) {
1845 oq->max_size.visible = size.visible + 1;
1846 GST_DEBUG_OBJECT (mq,
1847 "queue %d is filled, bumping its max visible to %d", oq->id,
1848 oq->max_size.visible);
1849 gst_data_queue_limits_changed (oq->queue);
1850 }
1851 }
1852 if (!gst_data_queue_is_empty (oq->queue))
1853 empty = FALSE;
1854 }
1855 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1857 if (empty) {
1858 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1859 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1860 }
1861 }
1863 static gboolean
1864 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1865 guint64 time, GstSingleQueue * sq)
1866 {
1867 gboolean res;
1868 GstMultiQueue *mq = sq->mqueue;
1870 GST_DEBUG_OBJECT (mq,
1871 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1872 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1873 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1875 /* we are always filled on EOS */
1876 if (sq->is_eos)
1877 return TRUE;
1879 /* we never go past the max visible items unless we are in buffering mode */
1880 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1881 return TRUE;
1883 /* check time or bytes */
1884 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1886 return res;
1887 }
1889 static void
1890 gst_single_queue_free (GstSingleQueue * sq)
1891 {
1892 /* DRAIN QUEUE */
1893 gst_data_queue_flush (sq->queue);
1894 g_object_unref (sq->queue);
1895 g_cond_free (sq->turn);
1896 g_free (sq);
1897 }
1899 static GstSingleQueue *
1900 gst_single_queue_new (GstMultiQueue * mqueue, gint id)
1901 {
1902 GstSingleQueue *sq;
1903 gchar *name;
1904 GList *tmp;
1905 gint temp_id = (id == -1) ? 0 : id;
1907 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1909 /* Find an unused queue ID, if possible the passed one */
1910 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1911 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1912 /* This works because the IDs are sorted in ascending order */
1913 if (sq2->id == temp_id) {
1914 /* If this ID was requested by the caller return NULL,
1915 * otherwise just get us the next one */
1916 if (id == -1)
1917 temp_id = sq2->id + 1;
1918 else
1919 return NULL;
1920 } else if (sq2->id > temp_id) {
1921 break;
1922 }
1923 }
1925 sq = g_new0 (GstSingleQueue, 1);
1926 mqueue->nbqueues++;
1927 sq->id = temp_id;
1929 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
1930 mqueue->queues_cookie++;
1932 /* copy over max_size and extra_size so we don't need to take the lock
1933 * any longer when checking if the queue is full. */
1934 sq->max_size.visible = mqueue->max_size.visible;
1935 sq->max_size.bytes = mqueue->max_size.bytes;
1936 sq->max_size.time = mqueue->max_size.time;
1938 sq->extra_size.visible = mqueue->extra_size.visible;
1939 sq->extra_size.bytes = mqueue->extra_size.bytes;
1940 sq->extra_size.time = mqueue->extra_size.time;
1942 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1944 sq->mqueue = mqueue;
1945 sq->srcresult = GST_FLOW_WRONG_STATE;
1946 sq->queue = gst_data_queue_new_full ((GstDataQueueCheckFullFunction)
1947 single_queue_check_full,
1948 (GstDataQueueFullCallback) single_queue_overrun_cb,
1949 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1950 sq->is_eos = FALSE;
1951 sq->flushing = FALSE;
1952 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1953 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1955 sq->nextid = 0;
1956 sq->oldid = 0;
1957 sq->next_time = GST_CLOCK_TIME_NONE;
1958 sq->last_time = GST_CLOCK_TIME_NONE;
1959 sq->turn = g_cond_new ();
1961 sq->sinktime = GST_CLOCK_TIME_NONE;
1962 sq->srctime = GST_CLOCK_TIME_NONE;
1963 sq->sink_tainted = TRUE;
1964 sq->src_tainted = TRUE;
1966 name = g_strdup_printf ("sink%d", sq->id);
1967 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
1968 g_free (name);
1970 gst_pad_set_chain_function (sq->sinkpad,
1971 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1972 gst_pad_set_activatepush_function (sq->sinkpad,
1973 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_push));
1974 gst_pad_set_event_function (sq->sinkpad,
1975 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1976 gst_pad_set_getcaps_function (sq->sinkpad,
1977 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1978 gst_pad_set_acceptcaps_function (sq->sinkpad,
1979 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1980 gst_pad_set_bufferalloc_function (sq->sinkpad,
1981 GST_DEBUG_FUNCPTR (gst_multi_queue_bufferalloc));
1982 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
1983 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1985 name = g_strdup_printf ("src%d", sq->id);
1986 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
1987 g_free (name);
1989 gst_pad_set_activatepush_function (sq->srcpad,
1990 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_push));
1991 gst_pad_set_getcaps_function (sq->srcpad,
1992 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1993 gst_pad_set_acceptcaps_function (sq->srcpad,
1994 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1995 gst_pad_set_event_function (sq->srcpad,
1996 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1997 gst_pad_set_query_function (sq->srcpad,
1998 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1999 gst_pad_set_iterate_internal_links_function (sq->srcpad,
2000 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2002 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2003 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2005 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2007 /* only activate the pads when we are not in the NULL state
2008 * and add the pad under the state_lock to prevend state changes
2009 * between activating and adding */
2010 g_static_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2011 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2012 gst_pad_set_active (sq->srcpad, TRUE);
2013 gst_pad_set_active (sq->sinkpad, TRUE);
2014 }
2015 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2016 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2017 g_static_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2019 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",
2020 sq->id);
2022 return sq;
2023 }