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 #include <gst/gst.h>
113 #include <stdio.h>
114 #include "gstmultiqueue.h"
116 /**
117 * GstSingleQueue:
118 * @sinkpad: associated sink #GstPad
119 * @srcpad: associated source #GstPad
120 *
121 * Structure containing all information and properties about
122 * a single queue.
123 */
124 typedef struct _GstSingleQueue GstSingleQueue;
126 struct _GstSingleQueue
127 {
128 /* unique identifier of the queue */
129 guint id;
131 GstMultiQueue *mqueue;
133 GstPad *sinkpad;
134 GstPad *srcpad;
136 /* flowreturn of previous srcpad push */
137 GstFlowReturn srcresult;
139 /* segments */
140 GstSegment sink_segment;
141 GstSegment src_segment;
143 /* position of src/sink */
144 GstClockTime sinktime, srctime;
145 /* TRUE if either position needs to be recalculated */
146 gboolean sink_tainted, src_tainted;
148 /* queue of data */
149 GstDataQueue *queue;
150 GstDataQueueSize max_size, extra_size;
151 GstClockTime cur_time;
152 gboolean is_eos;
153 gboolean flushing;
155 /* Protected by global lock */
156 guint32 nextid; /* ID of the next object waiting to be pushed */
157 guint32 oldid; /* ID of the last object pushed (last in a series) */
158 guint32 last_oldid; /* Previously observed old_id, reset to MAXUINT32 on flush */
159 GstClockTime next_time; /* End running time of next buffer to be pushed */
160 GstClockTime last_time; /* Start running time of last pushed buffer */
161 GCond *turn; /* SingleQueue turn waiting conditional */
162 };
165 /* Extension of GstDataQueueItem structure for our usage */
166 typedef struct _GstMultiQueueItem GstMultiQueueItem;
168 struct _GstMultiQueueItem
169 {
170 GstMiniObject *object;
171 guint size;
172 guint64 duration;
173 gboolean visible;
175 GDestroyNotify destroy;
176 guint32 posid;
177 };
179 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue, gint id);
180 static void gst_single_queue_free (GstSingleQueue * squeue);
182 static void wake_up_next_non_linked (GstMultiQueue * mq);
183 static void compute_high_id (GstMultiQueue * mq);
184 static void compute_high_time (GstMultiQueue * mq);
185 static void single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
186 static void single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq);
188 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
189 GST_PAD_SINK,
190 GST_PAD_REQUEST,
191 GST_STATIC_CAPS_ANY);
193 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
194 GST_PAD_SRC,
195 GST_PAD_SOMETIMES,
196 GST_STATIC_CAPS_ANY);
198 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
199 #define GST_CAT_DEFAULT (multi_queue_debug)
201 /* Signals and args */
202 enum
203 {
204 SIGNAL_UNDERRUN,
205 SIGNAL_OVERRUN,
206 LAST_SIGNAL
207 };
209 /* default limits, we try to keep up to 2 seconds of data and if there is not
210 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
211 * there is data in the queues. Normally, the byte and time limits are not hit
212 * in theses conditions. */
213 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
214 #define DEFAULT_MAX_SIZE_BUFFERS 5
215 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
217 /* second limits. When we hit one of the above limits we are probably dealing
218 * with a badly muxed file and we scale the limits to these emergency values.
219 * This is currently not yet implemented.
220 * Since we dynamically scale the queue buffer size up to the limits but avoid
221 * going above the max-size-buffers when we can, we don't really need this
222 * aditional extra size. */
223 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
224 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
225 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
227 #define DEFAULT_USE_BUFFERING FALSE
228 #define DEFAULT_LOW_PERCENT 10
229 #define DEFAULT_HIGH_PERCENT 99
230 #define DEFAULT_SYNC_BY_RUNNING_TIME FALSE
232 enum
233 {
234 PROP_0,
235 PROP_EXTRA_SIZE_BYTES,
236 PROP_EXTRA_SIZE_BUFFERS,
237 PROP_EXTRA_SIZE_TIME,
238 PROP_MAX_SIZE_BYTES,
239 PROP_MAX_SIZE_BUFFERS,
240 PROP_MAX_SIZE_TIME,
241 PROP_USE_BUFFERING,
242 PROP_LOW_PERCENT,
243 PROP_HIGH_PERCENT,
244 PROP_SYNC_BY_RUNNING_TIME,
245 PROP_LAST
246 };
248 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
249 g_mutex_lock (q->qlock); \
250 } G_STMT_END
252 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
253 g_mutex_unlock (q->qlock); \
254 } G_STMT_END
256 static void gst_multi_queue_finalize (GObject * object);
257 static void gst_multi_queue_set_property (GObject * object,
258 guint prop_id, const GValue * value, GParamSpec * pspec);
259 static void gst_multi_queue_get_property (GObject * object,
260 guint prop_id, GValue * value, GParamSpec * pspec);
262 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
263 GstPadTemplate * temp, const gchar * name);
264 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
265 static GstStateChangeReturn gst_multi_queue_change_state (GstElement *
266 element, GstStateChange transition);
268 static void gst_multi_queue_loop (GstPad * pad);
270 #define _do_init(bla) \
271 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
273 GST_BOILERPLATE_FULL (GstMultiQueue, gst_multi_queue, GstElement,
274 GST_TYPE_ELEMENT, _do_init);
276 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
278 static void
279 gst_multi_queue_base_init (gpointer g_class)
280 {
281 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
283 gst_element_class_set_details_simple (gstelement_class,
284 "MultiQueue",
285 "Generic", "Multiple data queue", "Edward Hervey <edward@fluendo.com>");
286 gst_element_class_add_pad_template (gstelement_class,
287 gst_static_pad_template_get (&sinktemplate));
288 gst_element_class_add_pad_template (gstelement_class,
289 gst_static_pad_template_get (&srctemplate));
290 }
292 static void
293 gst_multi_queue_class_init (GstMultiQueueClass * klass)
294 {
295 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
296 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
298 gobject_class->set_property = gst_multi_queue_set_property;
299 gobject_class->get_property = gst_multi_queue_get_property;
301 /* SIGNALS */
303 /**
304 * GstMultiQueue::underrun:
305 * @multiqueue: the multqueue instance
306 *
307 * This signal is emitted from the streaming thread when there is
308 * no data in any of the queues inside the multiqueue instance (underrun).
309 *
310 * This indicates either starvation or EOS from the upstream data sources.
311 */
312 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
313 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
314 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
315 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
317 /**
318 * GstMultiQueue::overrun:
319 * @multiqueue: the multiqueue instance
320 *
321 * Reports that one of the queues in the multiqueue is full (overrun).
322 * A queue is full if the total amount of data inside it (num-buffers, time,
323 * size) is higher than the boundary values which can be set through the
324 * GObject properties.
325 *
326 * This can be used as an indicator of pre-roll.
327 */
328 gst_multi_queue_signals[SIGNAL_OVERRUN] =
329 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
330 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
331 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
333 /* PROPERTIES */
335 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
336 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
337 "Max. amount of data in the queue (bytes, 0=disable)",
338 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
341 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
342 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
343 DEFAULT_MAX_SIZE_BUFFERS,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
346 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
347 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
348 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BYTES,
351 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
352 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)"
353 " (NOT IMPLEMENTED)",
354 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES,
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_BUFFERS,
357 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
358 "Amount of buffers the queues can grow if one of them is empty (0=disable)"
359 " (NOT IMPLEMENTED)",
360 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS,
361 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362 g_object_class_install_property (gobject_class, PROP_EXTRA_SIZE_TIME,
363 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
364 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)"
365 " (NOT IMPLEMENTED)",
366 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 /**
370 * GstMultiQueue:use-buffering
371 *
372 * Enable the buffering option in multiqueue so that BUFFERING messages are
373 * emited based on low-/high-percent thresholds.
374 *
375 * Since: 0.10.26
376 */
377 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
378 g_param_spec_boolean ("use-buffering", "Use buffering",
379 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
380 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 /**
382 * GstMultiQueue:low-percent
383 *
384 * Low threshold percent for buffering to start.
385 *
386 * Since: 0.10.26
387 */
388 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
389 g_param_spec_int ("low-percent", "Low percent",
390 "Low threshold for buffering to start", 0, 100,
391 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 /**
393 * GstMultiQueue:high-percent
394 *
395 * High threshold percent for buffering to finish.
396 *
397 * Since: 0.10.26
398 */
399 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
400 g_param_spec_int ("high-percent", "High percent",
401 "High threshold for buffering to finish", 0, 100,
402 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 /**
405 * GstMultiQueue:sync-by-running-time
406 *
407 * If enabled multiqueue will synchronize deactivated or not-linked streams
408 * to the activated and linked streams by taking the running time.
409 * Otherwise multiqueue will synchronize the deactivated or not-linked
410 * streams by keeping the order in which buffers and events arrived compared
411 * to active and linked streams.
412 *
413 * Since: 0.10.33
414 */
415 g_object_class_install_property (gobject_class, PROP_SYNC_BY_RUNNING_TIME,
416 g_param_spec_boolean ("sync-by-running-time", "Sync By Running Time",
417 "Synchronize deactivated or not-linked streams by running time",
418 DEFAULT_SYNC_BY_RUNNING_TIME,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 gobject_class->finalize = gst_multi_queue_finalize;
423 gstelement_class->request_new_pad =
424 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
425 gstelement_class->release_pad =
426 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
427 gstelement_class->change_state =
428 GST_DEBUG_FUNCPTR (gst_multi_queue_change_state);
429 }
431 static void
432 gst_multi_queue_init (GstMultiQueue * mqueue, GstMultiQueueClass * klass)
433 {
434 mqueue->nbqueues = 0;
435 mqueue->queues = NULL;
437 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
438 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
439 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
441 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
442 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
443 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
445 mqueue->use_buffering = DEFAULT_USE_BUFFERING;
446 mqueue->low_percent = DEFAULT_LOW_PERCENT;
447 mqueue->high_percent = DEFAULT_HIGH_PERCENT;
449 mqueue->sync_by_running_time = DEFAULT_SYNC_BY_RUNNING_TIME;
451 mqueue->counter = 1;
452 mqueue->highid = -1;
453 mqueue->high_time = GST_CLOCK_TIME_NONE;
455 mqueue->qlock = g_mutex_new ();
456 }
458 static void
459 gst_multi_queue_finalize (GObject * object)
460 {
461 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
463 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
464 g_list_free (mqueue->queues);
465 mqueue->queues = NULL;
466 mqueue->queues_cookie++;
468 /* free/unref instance data */
469 g_mutex_free (mqueue->qlock);
471 G_OBJECT_CLASS (parent_class)->finalize (object);
472 }
474 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
475 GList * tmp = mq->queues; \
476 while (tmp) { \
477 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
478 q->max_size.format = mq->max_size.format; \
479 tmp = g_list_next(tmp); \
480 }; \
481 } G_STMT_END
483 static void
484 gst_multi_queue_set_property (GObject * object, guint prop_id,
485 const GValue * value, GParamSpec * pspec)
486 {
487 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
489 switch (prop_id) {
490 case PROP_MAX_SIZE_BYTES:
491 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
492 mq->max_size.bytes = g_value_get_uint (value);
493 SET_CHILD_PROPERTY (mq, bytes);
494 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
495 break;
496 case PROP_MAX_SIZE_BUFFERS:
497 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
498 mq->max_size.visible = g_value_get_uint (value);
499 SET_CHILD_PROPERTY (mq, visible);
500 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
501 break;
502 case PROP_MAX_SIZE_TIME:
503 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
504 mq->max_size.time = g_value_get_uint64 (value);
505 SET_CHILD_PROPERTY (mq, time);
506 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
507 break;
508 case PROP_EXTRA_SIZE_BYTES:
509 mq->extra_size.bytes = g_value_get_uint (value);
510 break;
511 case PROP_EXTRA_SIZE_BUFFERS:
512 mq->extra_size.visible = g_value_get_uint (value);
513 break;
514 case PROP_EXTRA_SIZE_TIME:
515 mq->extra_size.time = g_value_get_uint64 (value);
516 break;
517 case PROP_USE_BUFFERING:
518 mq->use_buffering = g_value_get_boolean (value);
519 break;
520 case PROP_LOW_PERCENT:
521 mq->low_percent = g_value_get_int (value);
522 break;
523 case PROP_HIGH_PERCENT:
524 mq->high_percent = g_value_get_int (value);
525 break;
526 case PROP_SYNC_BY_RUNNING_TIME:
527 mq->sync_by_running_time = g_value_get_boolean (value);
528 break;
529 default:
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531 break;
532 }
533 }
535 static void
536 gst_multi_queue_get_property (GObject * object, guint prop_id,
537 GValue * value, GParamSpec * pspec)
538 {
539 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
541 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
543 switch (prop_id) {
544 case PROP_EXTRA_SIZE_BYTES:
545 g_value_set_uint (value, mq->extra_size.bytes);
546 break;
547 case PROP_EXTRA_SIZE_BUFFERS:
548 g_value_set_uint (value, mq->extra_size.visible);
549 break;
550 case PROP_EXTRA_SIZE_TIME:
551 g_value_set_uint64 (value, mq->extra_size.time);
552 break;
553 case PROP_MAX_SIZE_BYTES:
554 g_value_set_uint (value, mq->max_size.bytes);
555 break;
556 case PROP_MAX_SIZE_BUFFERS:
557 g_value_set_uint (value, mq->max_size.visible);
558 break;
559 case PROP_MAX_SIZE_TIME:
560 g_value_set_uint64 (value, mq->max_size.time);
561 break;
562 case PROP_USE_BUFFERING:
563 g_value_set_boolean (value, mq->use_buffering);
564 break;
565 case PROP_LOW_PERCENT:
566 g_value_set_int (value, mq->low_percent);
567 break;
568 case PROP_HIGH_PERCENT:
569 g_value_set_int (value, mq->high_percent);
570 break;
571 case PROP_SYNC_BY_RUNNING_TIME:
572 g_value_set_boolean (value, mq->sync_by_running_time);
573 break;
574 default:
575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
576 break;
577 }
579 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
580 }
582 static GstIterator *
583 gst_multi_queue_iterate_internal_links (GstPad * pad)
584 {
585 GstIterator *it = NULL;
586 GstPad *opad;
587 GstSingleQueue *squeue;
588 GstMultiQueue *mq = GST_MULTI_QUEUE (gst_pad_get_parent (pad));
590 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
591 squeue = gst_pad_get_element_private (pad);
592 if (!squeue)
593 goto out;
595 if (squeue->sinkpad == pad)
596 opad = gst_object_ref (squeue->srcpad);
597 else if (squeue->srcpad == pad)
598 opad = gst_object_ref (squeue->sinkpad);
599 else
600 goto out;
602 it = gst_iterator_new_single (GST_TYPE_PAD, opad,
603 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
605 gst_object_unref (opad);
607 out:
608 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
609 gst_object_unref (mq);
611 return it;
612 }
615 /*
616 * GstElement methods
617 */
619 static GstPad *
620 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
621 const gchar * name)
622 {
623 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
624 GstSingleQueue *squeue;
625 gint temp_id = -1;
627 if (name) {
628 sscanf (name + 4, "%d", &temp_id);
629 GST_LOG_OBJECT (element, "name : %s (id %d)", GST_STR_NULL (name), temp_id);
630 }
632 /* Create a new single queue, add the sink and source pad and return the sink pad */
633 squeue = gst_single_queue_new (mqueue, temp_id);
635 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
636 GST_DEBUG_PAD_NAME (squeue->sinkpad));
638 return squeue ? squeue->sinkpad : NULL;
639 }
641 static void
642 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
643 {
644 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
645 GstSingleQueue *sq = NULL;
646 GList *tmp;
648 GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
650 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
651 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
652 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
653 sq = (GstSingleQueue *) tmp->data;
655 if (sq->sinkpad == pad)
656 break;
657 }
659 if (!tmp) {
660 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
661 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
662 return;
663 }
665 /* FIXME: The removal of the singlequeue should probably not happen until it
666 * finishes draining */
668 /* remove it from the list */
669 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
670 mqueue->queues_cookie++;
672 /* FIXME : recompute next-non-linked */
673 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
675 /* delete SingleQueue */
676 gst_data_queue_set_flushing (sq->queue, TRUE);
678 gst_pad_set_active (sq->srcpad, FALSE);
679 gst_pad_set_active (sq->sinkpad, FALSE);
680 gst_pad_set_element_private (sq->srcpad, NULL);
681 gst_pad_set_element_private (sq->sinkpad, NULL);
682 gst_element_remove_pad (element, sq->srcpad);
683 gst_element_remove_pad (element, sq->sinkpad);
684 gst_single_queue_free (sq);
685 }
687 static GstStateChangeReturn
688 gst_multi_queue_change_state (GstElement * element, GstStateChange transition)
689 {
690 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
691 GstSingleQueue *sq = NULL;
692 GstStateChangeReturn result;
694 switch (transition) {
695 case GST_STATE_CHANGE_READY_TO_PAUSED:{
696 GList *tmp;
698 /* Set all pads to non-flushing */
699 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
700 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
701 sq = (GstSingleQueue *) tmp->data;
702 sq->flushing = FALSE;
703 }
704 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
705 break;
706 }
707 case GST_STATE_CHANGE_PAUSED_TO_READY:{
708 GList *tmp;
710 /* Un-wait all waiting pads */
711 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
712 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
713 sq = (GstSingleQueue *) tmp->data;
714 sq->flushing = TRUE;
715 g_cond_signal (sq->turn);
716 }
717 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
718 break;
719 }
720 default:
721 break;
722 }
724 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
726 switch (transition) {
727 default:
728 break;
729 }
731 return result;
735 }
737 static gboolean
738 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
739 {
740 gboolean result;
742 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
743 sq->id);
745 if (flush) {
746 sq->srcresult = GST_FLOW_WRONG_STATE;
747 gst_data_queue_set_flushing (sq->queue, TRUE);
749 sq->flushing = TRUE;
751 /* wake up non-linked task */
752 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
753 sq->id);
754 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
755 g_cond_signal (sq->turn);
756 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
758 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
759 result = gst_pad_pause_task (sq->srcpad);
760 sq->sink_tainted = sq->src_tainted = TRUE;
761 } else {
762 gst_data_queue_flush (sq->queue);
763 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
764 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
765 /* All pads start off not-linked for a smooth kick-off */
766 sq->srcresult = GST_FLOW_OK;
767 sq->cur_time = 0;
768 sq->max_size.visible = mq->max_size.visible;
769 sq->is_eos = FALSE;
770 sq->nextid = 0;
771 sq->oldid = 0;
772 sq->last_oldid = G_MAXUINT32;
773 sq->next_time = GST_CLOCK_TIME_NONE;
774 sq->last_time = GST_CLOCK_TIME_NONE;
775 gst_data_queue_set_flushing (sq->queue, FALSE);
777 /* Reset high time to be recomputed next */
778 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
779 mq->high_time = GST_CLOCK_TIME_NONE;
780 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
782 sq->flushing = FALSE;
784 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
785 result =
786 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
787 sq->srcpad);
788 }
789 return result;
790 }
792 static void
793 update_buffering (GstMultiQueue * mq, GstSingleQueue * sq)
794 {
795 GstDataQueueSize size;
796 gint percent, tmp;
797 gboolean post = FALSE;
799 /* nothing to dowhen we are not in buffering mode */
800 if (!mq->use_buffering)
801 return;
803 gst_data_queue_get_level (sq->queue, &size);
805 GST_DEBUG_OBJECT (mq,
806 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
807 G_GUINT64_FORMAT, sq->id, size.visible, sq->max_size.visible,
808 size.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
810 /* get bytes and time percentages and take the max */
811 if (sq->is_eos) {
812 percent = 100;
813 } else {
814 percent = 0;
815 if (sq->max_size.time > 0) {
816 tmp = (sq->cur_time * 100) / sq->max_size.time;
817 percent = MAX (percent, tmp);
818 }
819 if (sq->max_size.bytes > 0) {
820 tmp = (size.bytes * 100) / sq->max_size.bytes;
821 percent = MAX (percent, tmp);
822 }
823 }
825 if (mq->buffering) {
826 post = TRUE;
827 if (percent >= mq->high_percent) {
828 mq->buffering = FALSE;
829 }
830 /* make sure it increases */
831 percent = MAX (mq->percent, percent);
833 if (percent == mq->percent)
834 /* don't post if nothing changed */
835 post = FALSE;
836 else
837 /* else keep last value we posted */
838 mq->percent = percent;
839 } else {
840 if (percent < mq->low_percent) {
841 mq->buffering = TRUE;
842 mq->percent = percent;
843 post = TRUE;
844 }
845 }
846 if (post) {
847 GstMessage *message;
849 /* scale to high percent so that it becomes the 100% mark */
850 percent = percent * 100 / mq->high_percent;
851 /* clip */
852 if (percent > 100)
853 percent = 100;
855 GST_DEBUG_OBJECT (mq, "buffering %d percent", percent);
856 message = gst_message_new_buffering (GST_OBJECT_CAST (mq), percent);
858 gst_element_post_message (GST_ELEMENT_CAST (mq), message);
859 } else {
860 GST_DEBUG_OBJECT (mq, "filled %d percent", percent);
861 }
862 }
864 /* calculate the diff between running time on the sink and src of the queue.
865 * This is the total amount of time in the queue.
866 * WITH LOCK TAKEN */
867 static void
868 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
869 {
870 gint64 sink_time, src_time;
872 if (sq->sink_tainted) {
873 sink_time = sq->sinktime =
874 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
875 sq->sink_segment.last_stop);
877 if (G_UNLIKELY (sink_time != GST_CLOCK_TIME_NONE))
878 /* if we have a time, we become untainted and use the time */
879 sq->sink_tainted = FALSE;
880 } else
881 sink_time = sq->sinktime;
883 if (sq->src_tainted) {
884 src_time = sq->srctime =
885 gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
886 sq->src_segment.last_stop);
887 /* if we have a time, we become untainted and use the time */
888 if (G_UNLIKELY (src_time != GST_CLOCK_TIME_NONE))
889 sq->src_tainted = FALSE;
890 } else
891 src_time = sq->srctime;
893 GST_DEBUG_OBJECT (mq,
894 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
895 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
897 /* This allows for streams with out of order timestamping - sometimes the
898 * emerging timestamp is later than the arriving one(s) */
899 if (G_LIKELY (sink_time != -1 && src_time != -1 && sink_time > src_time))
900 sq->cur_time = sink_time - src_time;
901 else
902 sq->cur_time = 0;
904 /* updating the time level can change the buffering state */
905 update_buffering (mq, sq);
907 return;
908 }
910 /* take a NEWSEGMENT event and apply the values to segment, updating the time
911 * level of queue. */
912 static void
913 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
914 GstSegment * segment)
915 {
916 gboolean update;
917 GstFormat format;
918 gdouble rate, arate;
919 gint64 start, stop, time;
921 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
922 &format, &start, &stop, &time);
924 /* now configure the values, we use these to track timestamps on the
925 * sinkpad. */
926 if (format != GST_FORMAT_TIME) {
927 /* non-time format, pretent the current time segment is closed with a
928 * 0 start and unknown stop time. */
929 update = FALSE;
930 format = GST_FORMAT_TIME;
931 start = 0;
932 stop = -1;
933 time = 0;
934 }
936 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
938 gst_segment_set_newsegment_full (segment, update,
939 rate, arate, format, start, stop, time);
941 if (segment == &sq->sink_segment)
942 sq->sink_tainted = TRUE;
943 else
944 sq->src_tainted = TRUE;
946 GST_DEBUG_OBJECT (mq,
947 "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
949 /* segment can update the time level of the queue */
950 update_time_level (mq, sq);
952 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
953 }
955 /* take a buffer and update segment, updating the time level of the queue. */
956 static void
957 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
958 GstClockTime duration, GstSegment * segment)
959 {
960 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
962 /* if no timestamp is set, assume it's continuous with the previous
963 * time */
964 if (timestamp == GST_CLOCK_TIME_NONE)
965 timestamp = segment->last_stop;
967 /* add duration */
968 if (duration != GST_CLOCK_TIME_NONE)
969 timestamp += duration;
971 GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
972 sq->id, GST_TIME_ARGS (timestamp));
974 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
976 if (segment == &sq->sink_segment)
977 sq->sink_tainted = TRUE;
978 else
979 sq->src_tainted = TRUE;
981 /* calc diff with other end */
982 update_time_level (mq, sq);
983 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
984 }
986 static GstClockTime
987 get_running_time (GstSegment * segment, GstMiniObject * object, gboolean end)
988 {
989 GstClockTime time = GST_CLOCK_TIME_NONE;
991 if (GST_IS_BUFFER (object)) {
992 GstBuffer *buf = GST_BUFFER_CAST (object);
994 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
995 time = GST_BUFFER_TIMESTAMP (buf);
996 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
997 time += GST_BUFFER_DURATION (buf);
998 if (time > segment->stop)
999 time = segment->stop;
1000 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1001 }
1002 } else if (GST_IS_BUFFER_LIST (object)) {
1003 GstBufferList *list = GST_BUFFER_LIST_CAST (object);
1004 GstBufferListIterator *it = gst_buffer_list_iterate (list);
1005 GstBuffer *buf;
1007 do {
1008 while ((buf = gst_buffer_list_iterator_next (it))) {
1009 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1010 time = GST_BUFFER_TIMESTAMP (buf);
1011 if (end && GST_BUFFER_DURATION_IS_VALID (buf))
1012 time += GST_BUFFER_DURATION (buf);
1013 if (time > segment->stop)
1014 time = segment->stop;
1015 time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, time);
1016 if (!end)
1017 goto done;
1018 } else if (!end) {
1019 goto done;
1020 }
1021 }
1022 } while (gst_buffer_list_iterator_next_group (it));
1023 } else if (GST_IS_EVENT (object)) {
1024 GstEvent *event = GST_EVENT_CAST (object);
1026 /* For newsegment events return the running time of the start position */
1027 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
1028 GstSegment new_segment = *segment;
1029 gboolean update;
1030 gdouble rate, applied_rate;
1031 GstFormat format;
1032 gint64 start, stop, position;
1034 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
1035 &format, &start, &stop, &position);
1036 if (format == GST_FORMAT_TIME) {
1037 gst_segment_set_newsegment_full (&new_segment, update, rate,
1038 applied_rate, format, start, stop, position);
1040 time =
1041 gst_segment_to_running_time (&new_segment, GST_FORMAT_TIME,
1042 new_segment.start);
1043 }
1044 }
1045 }
1047 done:
1048 return time;
1049 }
1051 static GstFlowReturn
1052 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
1053 GstMiniObject * object)
1054 {
1055 GstFlowReturn result = GST_FLOW_OK;
1057 if (GST_IS_BUFFER (object)) {
1058 GstBuffer *buffer;
1059 GstClockTime timestamp, duration;
1060 GstCaps *caps;
1062 buffer = GST_BUFFER_CAST (object);
1063 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1064 duration = GST_BUFFER_DURATION (buffer);
1065 caps = GST_BUFFER_CAPS (buffer);
1067 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
1069 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
1070 gst_data_queue_limits_changed (sq->queue);
1072 GST_DEBUG_OBJECT (mq,
1073 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
1074 sq->id, buffer, GST_TIME_ARGS (timestamp));
1076 /* Set caps on pad before pushing, this avoids core calling the acceptcaps
1077 * function on the srcpad, which will call acceptcaps upstream, which might
1078 * not accept these caps (anymore). */
1079 if (caps && caps != GST_PAD_CAPS (sq->srcpad))
1080 gst_pad_set_caps (sq->srcpad, caps);
1082 result = gst_pad_push (sq->srcpad, buffer);
1083 } else if (GST_IS_EVENT (object)) {
1084 GstEvent *event;
1086 event = GST_EVENT_CAST (object);
1088 switch (GST_EVENT_TYPE (event)) {
1089 case GST_EVENT_EOS:
1090 result = GST_FLOW_UNEXPECTED;
1091 break;
1092 case GST_EVENT_NEWSEGMENT:
1093 apply_segment (mq, sq, event, &sq->src_segment);
1094 /* Applying the segment may have made the queue non-full again, unblock it if needed */
1095 gst_data_queue_limits_changed (sq->queue);
1096 break;
1097 default:
1098 break;
1099 }
1101 GST_DEBUG_OBJECT (mq,
1102 "SingleQueue %d : Pushing event %p of type %s",
1103 sq->id, event, GST_EVENT_TYPE_NAME (event));
1105 gst_pad_push_event (sq->srcpad, event);
1106 } else {
1107 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
1108 sq->id);
1109 }
1110 return result;
1112 /* ERRORS */
1113 }
1115 static GstMiniObject *
1116 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
1117 {
1118 GstMiniObject *res;
1120 res = item->object;
1121 item->object = NULL;
1123 return res;
1124 }
1126 static void
1127 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
1128 {
1129 if (item->object)
1130 gst_mini_object_unref (item->object);
1131 g_slice_free (GstMultiQueueItem, item);
1132 }
1134 /* takes ownership of passed mini object! */
1135 static GstMultiQueueItem *
1136 gst_multi_queue_buffer_item_new (GstMiniObject * object, guint32 curid)
1137 {
1138 GstMultiQueueItem *item;
1140 item = g_slice_new (GstMultiQueueItem);
1141 item->object = object;
1142 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1143 item->posid = curid;
1145 item->size = GST_BUFFER_SIZE (object);
1146 item->duration = GST_BUFFER_DURATION (object);
1147 if (item->duration == GST_CLOCK_TIME_NONE)
1148 item->duration = 0;
1149 item->visible = TRUE;
1150 return item;
1151 }
1153 static GstMultiQueueItem *
1154 gst_multi_queue_event_item_new (GstMiniObject * object, guint32 curid)
1155 {
1156 GstMultiQueueItem *item;
1158 item = g_slice_new (GstMultiQueueItem);
1159 item->object = object;
1160 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
1161 item->posid = curid;
1163 item->size = 0;
1164 item->duration = 0;
1165 item->visible = FALSE;
1166 return item;
1167 }
1169 /* Each main loop attempts to push buffers until the return value
1170 * is not-linked. not-linked pads are not allowed to push data beyond
1171 * any linked pads, so they don't 'rush ahead of the pack'.
1172 */
1173 static void
1174 gst_multi_queue_loop (GstPad * pad)
1175 {
1176 GstSingleQueue *sq;
1177 GstMultiQueueItem *item;
1178 GstDataQueueItem *sitem;
1179 GstMultiQueue *mq;
1180 GstMiniObject *object = NULL;
1181 guint32 newid;
1182 GstFlowReturn result;
1183 GstClockTime next_time;
1185 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1186 mq = sq->mqueue;
1188 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
1190 if (sq->flushing)
1191 goto out_flushing;
1193 /* Get something from the queue, blocking until that happens, or we get
1194 * flushed */
1195 if (!(gst_data_queue_pop (sq->queue, &sitem)))
1196 goto out_flushing;
1198 item = (GstMultiQueueItem *) sitem;
1199 newid = item->posid;
1201 /* steal the object and destroy the item */
1202 object = gst_multi_queue_item_steal_object (item);
1203 gst_multi_queue_item_destroy (item);
1205 /* Get running time of the item. Events will have GST_CLOCK_TIME_NONE */
1206 next_time = get_running_time (&sq->src_segment, object, TRUE);
1208 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
1209 sq->id, newid, sq->last_oldid);
1211 /* If we're not-linked, we do some extra work because we might need to
1212 * wait before pushing. If we're linked but there's a gap in the IDs,
1213 * or it's the first loop, or we just passed the previous highid,
1214 * we might need to wake some sleeping pad up, so there's extra work
1215 * there too */
1216 if (sq->srcresult == GST_FLOW_NOT_LINKED
1217 || (sq->last_oldid == G_MAXUINT32) || (newid != (sq->last_oldid + 1))
1218 || sq->last_oldid > mq->highid) {
1219 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
1220 gst_flow_get_name (sq->srcresult));
1222 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1224 /* Check again if we're flushing after the lock is taken,
1225 * the flush flag might have been changed in the meantime */
1226 if (sq->flushing) {
1227 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1228 goto out_flushing;
1229 }
1231 /* Update the nextid so other threads know when to wake us up */
1232 sq->nextid = newid;
1233 sq->next_time = next_time;
1235 /* Update the oldid (the last ID we output) for highid tracking */
1236 if (sq->last_oldid != G_MAXUINT32)
1237 sq->oldid = sq->last_oldid;
1239 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1240 /* Go to sleep until it's time to push this buffer */
1242 /* Recompute the highid */
1243 compute_high_id (mq);
1244 /* Recompute the high time */
1245 compute_high_time (mq);
1247 while (((mq->sync_by_running_time && next_time != GST_CLOCK_TIME_NONE &&
1248 (mq->high_time == GST_CLOCK_TIME_NONE
1249 || next_time >= mq->high_time))
1250 || (!mq->sync_by_running_time && newid > mq->highid))
1251 && sq->srcresult == GST_FLOW_NOT_LINKED) {
1253 GST_DEBUG_OBJECT (mq,
1254 "queue %d sleeping for not-linked wakeup with "
1255 "newid %u, highid %u, next_time %" GST_TIME_FORMAT
1256 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1257 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1259 /* Wake up all non-linked pads before we sleep */
1260 wake_up_next_non_linked (mq);
1262 mq->numwaiting++;
1263 g_cond_wait (sq->turn, mq->qlock);
1264 mq->numwaiting--;
1266 if (sq->flushing) {
1267 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1268 goto out_flushing;
1269 }
1271 /* Recompute the high time */
1272 compute_high_time (mq);
1274 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
1275 "wakeup with newid %u, highid %u, next_time %" GST_TIME_FORMAT
1276 ", high_time %" GST_TIME_FORMAT, sq->id, newid, mq->highid,
1277 GST_TIME_ARGS (next_time), GST_TIME_ARGS (mq->high_time));
1278 }
1280 /* Re-compute the high_id in case someone else pushed */
1281 compute_high_id (mq);
1282 } else {
1283 compute_high_id (mq);
1284 /* Wake up all non-linked pads */
1285 wake_up_next_non_linked (mq);
1286 }
1287 /* We're done waiting, we can clear the nextid and nexttime */
1288 sq->nextid = 0;
1289 sq->next_time = GST_CLOCK_TIME_NONE;
1291 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1292 }
1294 if (sq->flushing)
1295 goto out_flushing;
1297 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
1298 gst_flow_get_name (sq->srcresult));
1300 /* Update time stats */
1301 next_time = get_running_time (&sq->src_segment, object, FALSE);
1302 if (next_time != GST_CLOCK_TIME_NONE) {
1303 if (sq->last_time == GST_CLOCK_TIME_NONE || sq->last_time < next_time)
1304 sq->last_time = next_time;
1305 if (mq->high_time == GST_CLOCK_TIME_NONE || mq->high_time <= next_time) {
1306 /* Wake up all non-linked pads now that we advanceed the high time */
1307 mq->high_time = next_time;
1308 wake_up_next_non_linked (mq);
1309 }
1310 }
1312 /* Try to push out the new object */
1313 result = gst_single_queue_push_one (mq, sq, object);
1314 sq->srcresult = result;
1315 object = NULL;
1317 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED
1318 && result != GST_FLOW_UNEXPECTED)
1319 goto out_flushing;
1321 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
1322 gst_flow_get_name (sq->srcresult));
1324 sq->last_oldid = newid;
1326 return;
1328 out_flushing:
1329 {
1330 if (object)
1331 gst_mini_object_unref (object);
1333 /* Need to make sure wake up any sleeping pads when we exit */
1334 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1335 compute_high_id (mq);
1336 wake_up_next_non_linked (mq);
1337 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1339 /* upstream needs to see fatal result ASAP to shut things down,
1340 * but might be stuck in one of our other full queues;
1341 * so empty this one and trigger dynamic queue growth. At
1342 * this point the srcresult is not OK, NOT_LINKED
1343 * or UNEXPECTED, i.e. a real failure */
1344 gst_data_queue_flush (sq->queue);
1345 single_queue_underrun_cb (sq->queue, sq);
1346 gst_data_queue_set_flushing (sq->queue, TRUE);
1347 gst_pad_pause_task (sq->srcpad);
1348 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
1349 "SingleQueue[%d] task paused, reason:%s",
1350 sq->id, gst_flow_get_name (sq->srcresult));
1351 return;
1352 }
1353 }
1355 /**
1356 * gst_multi_queue_chain:
1357 *
1358 * This is similar to GstQueue's chain function, except:
1359 * _ we don't have leak behavioures,
1360 * _ we push with a unique id (curid)
1361 */
1362 static GstFlowReturn
1363 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
1364 {
1365 GstSingleQueue *sq;
1366 GstMultiQueue *mq;
1367 GstMultiQueueItem *item;
1368 guint32 curid;
1369 GstClockTime timestamp, duration;
1371 sq = gst_pad_get_element_private (pad);
1372 mq = sq->mqueue;
1374 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1375 if (sq->is_eos)
1376 goto was_eos;
1378 /* Get a unique incrementing id */
1379 curid = g_atomic_int_exchange_and_add ((gint *) & mq->counter, 1);
1381 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
1382 sq->id, buffer, curid);
1384 item = gst_multi_queue_buffer_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
1386 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1387 duration = GST_BUFFER_DURATION (buffer);
1389 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1390 goto flushing;
1392 /* update time level, we must do this after pushing the data in the queue so
1393 * that we never end up filling the queue first. */
1394 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
1396 done:
1397 return sq->srcresult;
1399 /* ERRORS */
1400 flushing:
1401 {
1402 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1403 sq->id, gst_flow_get_name (sq->srcresult));
1404 gst_multi_queue_item_destroy (item);
1405 goto done;
1406 }
1407 was_eos:
1408 {
1409 GST_DEBUG_OBJECT (mq, "we are EOS, dropping buffer, return UNEXPECTED");
1410 gst_buffer_unref (buffer);
1411 return GST_FLOW_UNEXPECTED;
1412 }
1413 }
1415 static gboolean
1416 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
1417 {
1418 GstSingleQueue *sq;
1420 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1422 if (active) {
1423 /* All pads start off linked until they push one buffer */
1424 sq->srcresult = GST_FLOW_OK;
1425 } else {
1426 sq->srcresult = GST_FLOW_WRONG_STATE;
1427 gst_data_queue_flush (sq->queue);
1428 }
1429 return TRUE;
1430 }
1432 static gboolean
1433 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
1434 {
1435 GstSingleQueue *sq;
1436 GstMultiQueue *mq;
1437 guint32 curid;
1438 GstMultiQueueItem *item;
1439 gboolean res;
1440 GstEventType type;
1441 GstEvent *sref = NULL;
1443 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1444 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
1446 type = GST_EVENT_TYPE (event);
1448 switch (type) {
1449 case GST_EVENT_FLUSH_START:
1450 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
1451 sq->id);
1453 res = gst_pad_push_event (sq->srcpad, event);
1455 gst_single_queue_flush (mq, sq, TRUE);
1456 goto done;
1458 case GST_EVENT_FLUSH_STOP:
1459 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
1460 sq->id);
1462 res = gst_pad_push_event (sq->srcpad, event);
1464 gst_single_queue_flush (mq, sq, FALSE);
1465 goto done;
1466 case GST_EVENT_NEWSEGMENT:
1467 /* take ref because the queue will take ownership and we need the event
1468 * afterwards to update the segment */
1469 sref = gst_event_ref (event);
1470 break;
1472 default:
1473 if (!(GST_EVENT_IS_SERIALIZED (event))) {
1474 res = gst_pad_push_event (sq->srcpad, event);
1475 goto done;
1476 }
1477 break;
1478 }
1480 /* if eos, we are always full, so avoid hanging incoming indefinitely */
1481 if (sq->is_eos)
1482 goto was_eos;
1484 /* Get an unique incrementing id. */
1485 curid = g_atomic_int_exchange_and_add ((gint *) & mq->counter, 1);
1487 item = gst_multi_queue_event_item_new ((GstMiniObject *) event, curid);
1489 GST_DEBUG_OBJECT (mq,
1490 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
1491 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
1493 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
1494 goto flushing;
1496 /* mark EOS when we received one, we must do that after putting the
1497 * buffer in the queue because EOS marks the buffer as filled. No need to take
1498 * a lock, the _check_full happens from this thread only, right before pushing
1499 * into dataqueue. */
1500 switch (type) {
1501 case GST_EVENT_EOS:
1502 sq->is_eos = TRUE;
1503 /* EOS affects the buffering state */
1504 update_buffering (mq, sq);
1505 single_queue_overrun_cb (sq->queue, sq);
1506 break;
1507 case GST_EVENT_NEWSEGMENT:
1508 apply_segment (mq, sq, sref, &sq->sink_segment);
1509 gst_event_unref (sref);
1510 break;
1511 default:
1512 break;
1513 }
1514 done:
1515 gst_object_unref (mq);
1516 return res;
1518 flushing:
1519 {
1520 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
1521 sq->id, gst_flow_get_name (sq->srcresult));
1522 if (sref)
1523 gst_event_unref (sref);
1524 gst_multi_queue_item_destroy (item);
1525 goto done;
1526 }
1527 was_eos:
1528 {
1529 GST_DEBUG_OBJECT (mq, "we are EOS, dropping event, return FALSE");
1530 gst_event_unref (event);
1531 res = FALSE;
1532 goto done;
1533 }
1534 }
1536 static GstCaps *
1537 gst_multi_queue_getcaps (GstPad * pad)
1538 {
1539 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1540 GstPad *otherpad;
1541 GstCaps *result;
1543 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1545 GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
1547 result = gst_pad_peer_get_caps (otherpad);
1548 if (result == NULL)
1549 result = gst_caps_new_any ();
1551 return result;
1552 }
1554 static gboolean
1555 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
1556 {
1557 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1558 GstPad *otherpad;
1559 gboolean result;
1561 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1563 GST_LOG_OBJECT (otherpad, "Accept caps from the peer of this pad");
1565 result = gst_pad_peer_accept_caps (otherpad, caps);
1567 return result;
1568 }
1570 static GstFlowReturn
1571 gst_multi_queue_bufferalloc (GstPad * pad, guint64 offset, guint size,
1572 GstCaps * caps, GstBuffer ** buf)
1573 {
1574 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1576 return gst_pad_alloc_buffer (sq->srcpad, offset, size, caps, buf);
1577 }
1579 static gboolean
1580 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
1581 {
1582 GstMultiQueue *mq;
1583 GstSingleQueue *sq;
1584 gboolean result = FALSE;
1586 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1587 mq = sq->mqueue;
1589 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1591 if (active) {
1592 result = gst_single_queue_flush (mq, sq, FALSE);
1593 } else {
1594 result = gst_single_queue_flush (mq, sq, TRUE);
1595 /* make sure streaming finishes */
1596 result |= gst_pad_stop_task (pad);
1597 }
1598 return result;
1599 }
1601 static gboolean
1602 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
1603 {
1604 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1606 return gst_pad_push_event (sq->sinkpad, event);
1607 }
1609 static gboolean
1610 gst_multi_queue_src_query (GstPad * pad, GstQuery * query)
1611 {
1612 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1613 GstPad *peerpad;
1614 gboolean res;
1616 /* FIXME, Handle position offset depending on queue size */
1618 /* default handling */
1619 if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
1620 goto no_peer;
1622 res = gst_pad_query (peerpad, query);
1624 gst_object_unref (peerpad);
1626 return res;
1628 /* ERRORS */
1629 no_peer:
1630 {
1631 GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
1632 return FALSE;
1633 }
1634 }
1636 /*
1637 * Next-non-linked functions
1638 */
1640 /* WITH LOCK TAKEN */
1641 static void
1642 wake_up_next_non_linked (GstMultiQueue * mq)
1643 {
1644 GList *tmp;
1646 /* maybe no-one is waiting */
1647 if (mq->numwaiting < 1)
1648 return;
1650 /* Else figure out which singlequeue(s) need waking up */
1651 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1652 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1654 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1655 if ((mq->sync_by_running_time && mq->high_time != GST_CLOCK_TIME_NONE
1656 && sq->next_time != GST_CLOCK_TIME_NONE
1657 && sq->next_time >= mq->high_time)
1658 || (sq->nextid != 0 && sq->nextid <= mq->highid)) {
1659 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1660 g_cond_signal (sq->turn);
1661 }
1662 }
1663 }
1664 }
1666 /* WITH LOCK TAKEN */
1667 static void
1668 compute_high_id (GstMultiQueue * mq)
1669 {
1670 /* The high-id is either the highest id among the linked pads, or if all
1671 * pads are not-linked, it's the lowest not-linked pad */
1672 GList *tmp;
1673 guint32 lowest = G_MAXUINT32;
1674 guint32 highid = G_MAXUINT32;
1676 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1677 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1679 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1680 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1682 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1683 /* No need to consider queues which are not waiting */
1684 if (sq->nextid == 0) {
1685 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1686 continue;
1687 }
1689 if (sq->nextid < lowest)
1690 lowest = sq->nextid;
1691 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1692 /* If we don't have a global highid, or the global highid is lower than
1693 * this single queue's last outputted id, store the queue's one,
1694 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1695 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1696 highid = sq->oldid;
1697 }
1698 }
1700 if (highid == G_MAXUINT32 || lowest < highid)
1701 mq->highid = lowest;
1702 else
1703 mq->highid = highid;
1705 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1706 lowest);
1707 }
1709 /* WITH LOCK TAKEN */
1710 static void
1711 compute_high_time (GstMultiQueue * mq)
1712 {
1713 /* The high-id is either the highest id among the linked pads, or if all
1714 * pads are not-linked, it's the lowest not-linked pad */
1715 GList *tmp;
1716 GstClockTime highest = GST_CLOCK_TIME_NONE;
1717 GstClockTime lowest = GST_CLOCK_TIME_NONE;
1719 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1720 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1722 GST_LOG_OBJECT (mq,
1723 "inspecting sq:%d , next_time:%" GST_TIME_FORMAT ", last_time:%"
1724 GST_TIME_FORMAT ", srcresult:%s", sq->id, GST_TIME_ARGS (sq->next_time),
1725 GST_TIME_ARGS (sq->last_time), gst_flow_get_name (sq->srcresult));
1727 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1728 /* No need to consider queues which are not waiting */
1729 if (sq->next_time == GST_CLOCK_TIME_NONE) {
1730 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1731 continue;
1732 }
1734 if (lowest == GST_CLOCK_TIME_NONE || sq->next_time < lowest)
1735 lowest = sq->next_time;
1736 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1737 /* If we don't have a global highid, or the global highid is lower than
1738 * this single queue's last outputted id, store the queue's one,
1739 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1740 if (highest == GST_CLOCK_TIME_NONE || sq->last_time > highest)
1741 highest = sq->last_time;
1742 }
1743 }
1745 mq->high_time = highest;
1747 GST_LOG_OBJECT (mq,
1748 "High time is now : %" GST_TIME_FORMAT ", lowest non-linked %"
1749 GST_TIME_FORMAT, GST_TIME_ARGS (mq->high_time), GST_TIME_ARGS (lowest));
1750 }
1752 #define IS_FILLED(q, format, value) (((q)->max_size.format) != 0 && \
1753 ((q)->max_size.format) <= (value))
1755 /*
1756 * GstSingleQueue functions
1757 */
1758 static void
1759 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1760 {
1761 GstMultiQueue *mq = sq->mqueue;
1762 GList *tmp;
1763 GstDataQueueSize size;
1764 gboolean filled = FALSE;
1766 gst_data_queue_get_level (sq->queue, &size);
1768 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1770 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1771 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1772 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1773 GstDataQueueSize ssize;
1775 GST_LOG_OBJECT (mq, "Checking Queue %d", oq->id);
1777 if (gst_data_queue_is_empty (oq->queue)) {
1778 GST_LOG_OBJECT (mq, "Queue %d is empty", oq->id);
1779 if (IS_FILLED (sq, visible, size.visible)) {
1780 sq->max_size.visible = size.visible + 1;
1781 GST_DEBUG_OBJECT (mq,
1782 "Another queue is empty, bumping single queue %d max visible to %d",
1783 sq->id, sq->max_size.visible);
1784 }
1785 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1786 goto beach;
1787 }
1788 /* check if we reached the hard time/bytes limits */
1789 gst_data_queue_get_level (oq->queue, &ssize);
1791 GST_DEBUG_OBJECT (mq,
1792 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1793 G_GUINT64_FORMAT, oq->id, ssize.visible, oq->max_size.visible,
1794 ssize.bytes, oq->max_size.bytes, oq->cur_time, oq->max_size.time);
1796 /* if this queue is filled completely we must signal overrun.
1797 * FIXME, this seems wrong in many ways
1798 * - we're comparing the filled level of this queue against the
1799 * values of the other one
1800 * - we should only do this after we found no empty queues, ie, move
1801 * this check outside of the loop
1802 * - the debug statement talks about a different queue than the one
1803 * we are checking here.
1804 */
1805 if (sq->is_eos || IS_FILLED (sq, bytes, ssize.bytes) ||
1806 IS_FILLED (sq, time, sq->cur_time)) {
1807 GST_LOG_OBJECT (mq, "Queue %d is filled", oq->id);
1808 filled = TRUE;
1809 }
1810 }
1811 /* no queues were empty */
1812 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1814 /* Overrun is always forwarded, since this is blocking the upstream element */
1815 if (filled) {
1816 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1817 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1818 }
1820 beach:
1821 return;
1822 }
1824 static void
1825 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1826 {
1827 gboolean empty = TRUE;
1828 GstMultiQueue *mq = sq->mqueue;
1829 GList *tmp;
1831 GST_LOG_OBJECT (mq,
1832 "Single Queue %d is empty, Checking other single queues", sq->id);
1834 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1835 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1836 GstSingleQueue *oq = (GstSingleQueue *) tmp->data;
1838 if (gst_data_queue_is_full (oq->queue)) {
1839 GstDataQueueSize size;
1841 gst_data_queue_get_level (oq->queue, &size);
1842 if (IS_FILLED (oq, visible, size.visible)) {
1843 oq->max_size.visible = size.visible + 1;
1844 GST_DEBUG_OBJECT (mq,
1845 "queue %d is filled, bumping its max visible to %d", oq->id,
1846 oq->max_size.visible);
1847 gst_data_queue_limits_changed (oq->queue);
1848 }
1849 }
1850 if (!gst_data_queue_is_empty (oq->queue))
1851 empty = FALSE;
1852 }
1853 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1855 if (empty) {
1856 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1857 g_signal_emit (mq, gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1858 }
1859 }
1861 static gboolean
1862 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1863 guint64 time, GstSingleQueue * sq)
1864 {
1865 gboolean res;
1866 GstMultiQueue *mq = sq->mqueue;
1868 GST_DEBUG_OBJECT (mq,
1869 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1870 G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1871 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1873 /* we are always filled on EOS */
1874 if (sq->is_eos)
1875 return TRUE;
1877 /* we never go past the max visible items unless we are in buffering mode */
1878 if (!mq->use_buffering && IS_FILLED (sq, visible, visible))
1879 return TRUE;
1881 /* check time or bytes */
1882 res = IS_FILLED (sq, time, sq->cur_time) || IS_FILLED (sq, bytes, bytes);
1884 return res;
1885 }
1887 static void
1888 gst_single_queue_free (GstSingleQueue * sq)
1889 {
1890 /* DRAIN QUEUE */
1891 gst_data_queue_flush (sq->queue);
1892 g_object_unref (sq->queue);
1893 g_cond_free (sq->turn);
1894 g_free (sq);
1895 }
1897 static GstSingleQueue *
1898 gst_single_queue_new (GstMultiQueue * mqueue, gint id)
1899 {
1900 GstSingleQueue *sq;
1901 gchar *name;
1902 GList *tmp;
1903 gint temp_id = (id == -1) ? 0 : id;
1905 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1907 /* Find an unused queue ID, if possible the passed one */
1908 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
1909 GstSingleQueue *sq2 = (GstSingleQueue *) tmp->data;
1910 /* This works because the IDs are sorted in ascending order */
1911 if (sq2->id == temp_id) {
1912 /* If this ID was requested by the caller return NULL,
1913 * otherwise just get us the next one */
1914 if (id == -1)
1915 temp_id = sq2->id + 1;
1916 else
1917 return NULL;
1918 } else if (sq2->id > temp_id) {
1919 break;
1920 }
1921 }
1923 sq = g_new0 (GstSingleQueue, 1);
1924 mqueue->nbqueues++;
1925 sq->id = temp_id;
1927 mqueue->queues = g_list_insert_before (mqueue->queues, tmp, sq);
1928 mqueue->queues_cookie++;
1930 /* copy over max_size and extra_size so we don't need to take the lock
1931 * any longer when checking if the queue is full. */
1932 sq->max_size.visible = mqueue->max_size.visible;
1933 sq->max_size.bytes = mqueue->max_size.bytes;
1934 sq->max_size.time = mqueue->max_size.time;
1936 sq->extra_size.visible = mqueue->extra_size.visible;
1937 sq->extra_size.bytes = mqueue->extra_size.bytes;
1938 sq->extra_size.time = mqueue->extra_size.time;
1940 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1942 sq->mqueue = mqueue;
1943 sq->srcresult = GST_FLOW_WRONG_STATE;
1944 sq->queue = gst_data_queue_new_full ((GstDataQueueCheckFullFunction)
1945 single_queue_check_full,
1946 (GstDataQueueFullCallback) single_queue_overrun_cb,
1947 (GstDataQueueEmptyCallback) single_queue_underrun_cb, sq);
1948 sq->is_eos = FALSE;
1949 sq->flushing = FALSE;
1950 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1951 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1953 sq->nextid = 0;
1954 sq->oldid = 0;
1955 sq->next_time = GST_CLOCK_TIME_NONE;
1956 sq->last_time = GST_CLOCK_TIME_NONE;
1957 sq->turn = g_cond_new ();
1959 sq->sinktime = GST_CLOCK_TIME_NONE;
1960 sq->srctime = GST_CLOCK_TIME_NONE;
1961 sq->sink_tainted = TRUE;
1962 sq->src_tainted = TRUE;
1964 name = g_strdup_printf ("sink%d", sq->id);
1965 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, name);
1966 g_free (name);
1968 gst_pad_set_chain_function (sq->sinkpad,
1969 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1970 gst_pad_set_activatepush_function (sq->sinkpad,
1971 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_push));
1972 gst_pad_set_event_function (sq->sinkpad,
1973 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1974 gst_pad_set_getcaps_function (sq->sinkpad,
1975 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1976 gst_pad_set_acceptcaps_function (sq->sinkpad,
1977 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1978 gst_pad_set_bufferalloc_function (sq->sinkpad,
1979 GST_DEBUG_FUNCPTR (gst_multi_queue_bufferalloc));
1980 gst_pad_set_iterate_internal_links_function (sq->sinkpad,
1981 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
1983 name = g_strdup_printf ("src%d", sq->id);
1984 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, name);
1985 g_free (name);
1987 gst_pad_set_activatepush_function (sq->srcpad,
1988 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_push));
1989 gst_pad_set_getcaps_function (sq->srcpad,
1990 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1991 gst_pad_set_acceptcaps_function (sq->srcpad,
1992 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1993 gst_pad_set_event_function (sq->srcpad,
1994 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1995 gst_pad_set_query_function (sq->srcpad,
1996 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1997 gst_pad_set_iterate_internal_links_function (sq->srcpad,
1998 GST_DEBUG_FUNCPTR (gst_multi_queue_iterate_internal_links));
2000 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
2001 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
2003 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
2005 /* only activate the pads when we are not in the NULL state
2006 * and add the pad under the state_lock to prevend state changes
2007 * between activating and adding */
2008 g_static_rec_mutex_lock (GST_STATE_GET_LOCK (mqueue));
2009 if (GST_STATE_TARGET (mqueue) != GST_STATE_NULL) {
2010 gst_pad_set_active (sq->srcpad, TRUE);
2011 gst_pad_set_active (sq->sinkpad, TRUE);
2012 }
2013 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
2014 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
2015 g_static_rec_mutex_unlock (GST_STATE_GET_LOCK (mqueue));
2017 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",
2018 sq->id);
2020 return sq;
2021 }