[svn r804] Added function to retrieve backend details (total space, free space, etc)
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>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 #include "gstmultiqueue.h"
33 * @sinkpad: associated sink #GstPad
34 * @srcpad: associated source #GstPad
36 * Structure containing all information and properties about
39 typedef struct _GstSingleQueue GstSingleQueue;
41 struct _GstSingleQueue
43 /* unique identifier of the queue */
46 GstMultiQueue *mqueue;
51 /* flowreturn of previous srcpad push */
52 GstFlowReturn srcresult;
53 GstSegment sink_segment;
54 GstSegment src_segment;
58 GstDataQueueSize max_size, extra_size;
59 GstClockTime cur_time;
61 gboolean inextra; /* TRUE if the queue is currently in extradata mode */
63 /* Protected by global lock */
64 guint32 nextid; /* ID of the next object waiting to be pushed */
65 guint32 oldid; /* ID of the last object pushed (last in a series) */
66 GCond *turn; /* SingleQueue turn waiting conditional */
70 /* Extension of GstDataQueueItem structure for our usage */
71 typedef struct _GstMultiQueueItem GstMultiQueueItem;
73 struct _GstMultiQueueItem
75 GstMiniObject *object;
80 GDestroyNotify destroy;
84 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue);
85 static void gst_single_queue_free (GstSingleQueue * squeue);
87 static void wake_up_next_non_linked (GstMultiQueue * mq);
88 static void compute_high_id (GstMultiQueue * mq);
90 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
95 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
100 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
101 #define GST_CAT_DEFAULT (multi_queue_debug)
103 /* default limits, we try to keep up to 2 seconds of data and if there is not
104 * time, up to 10 MB. The number of buffers is dynamically scaled to make sure
105 * there is data in the queues. Normally, the byte and time limits are not hit
106 * in theses conditions. */
107 #define DEFAULT_MAX_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
108 #define DEFAULT_MAX_SIZE_BUFFERS 5
109 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND
111 /* second limits. When we hit one of the above limits we are probably dealing
112 * with a badly muxed file and we scale the limits to these emergency values.
113 * This is currently not yet implemented. */
114 #define DEFAULT_EXTRA_SIZE_BYTES 10 * 1024 * 1024 /* 10 MB */
115 #define DEFAULT_EXTRA_SIZE_BUFFERS 5
116 #define DEFAULT_EXTRA_SIZE_TIME 3 * GST_SECOND
118 /* Signals and args */
129 ARG_EXTRA_SIZE_BYTES,
130 ARG_EXTRA_SIZE_BUFFERS,
133 ARG_MAX_SIZE_BUFFERS,
138 static const GstElementDetails gst_multiqueue_details =
139 GST_ELEMENT_DETAILS ("MultiQueue",
141 "Multiple data queue",
142 "Edward Hervey <edward@fluendo.com>");
145 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
146 g_mutex_lock (q->qlock); \
149 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
150 g_mutex_unlock (q->qlock); \
153 static void gst_multi_queue_finalize (GObject * object);
154 static void gst_multi_queue_set_property (GObject * object,
155 guint prop_id, const GValue * value, GParamSpec * pspec);
156 static void gst_multi_queue_get_property (GObject * object,
157 guint prop_id, GValue * value, GParamSpec * pspec);
159 static GstPad *gst_multi_queue_request_new_pad (GstElement * element,
160 GstPadTemplate * temp, const gchar * name);
161 static void gst_multi_queue_release_pad (GstElement * element, GstPad * pad);
163 static void gst_multi_queue_loop (GstPad * pad);
165 #define _do_init(bla) \
166 GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
168 GST_BOILERPLATE_FULL (GstMultiQueue, gst_multi_queue, GstElement,
169 GST_TYPE_ELEMENT, _do_init);
171 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
176 gst_multi_queue_base_init (gpointer g_class)
178 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
180 gst_element_class_set_details (gstelement_class, &gst_multiqueue_details);
181 gst_element_class_add_pad_template (gstelement_class,
182 gst_static_pad_template_get (&sinktemplate));
183 gst_element_class_add_pad_template (gstelement_class,
184 gst_static_pad_template_get (&srctemplate));
188 gst_multi_queue_class_init (GstMultiQueueClass * klass)
190 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
191 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
193 gobject_class->set_property =
194 GST_DEBUG_FUNCPTR (gst_multi_queue_set_property);
195 gobject_class->get_property =
196 GST_DEBUG_FUNCPTR (gst_multi_queue_get_property);
199 gst_multi_queue_signals[SIGNAL_UNDERRUN] =
200 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
201 G_STRUCT_OFFSET (GstMultiQueueClass, underrun), NULL, NULL,
202 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
204 gst_multi_queue_signals[SIGNAL_OVERRUN] =
205 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
206 G_STRUCT_OFFSET (GstMultiQueueClass, overrun), NULL, NULL,
207 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
211 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BYTES,
212 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
213 "Max. amount of data in the queue (bytes, 0=disable)",
214 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES, G_PARAM_READWRITE));
215 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BUFFERS,
216 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
217 "Max. number of buffers in the queue (0=disable)",
218 0, G_MAXUINT, DEFAULT_MAX_SIZE_BUFFERS, G_PARAM_READWRITE));
219 g_object_class_install_property (gobject_class, ARG_MAX_SIZE_TIME,
220 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
221 "Max. amount of data in the queue (in ns, 0=disable)",
222 0, G_MAXUINT64, DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE));
224 g_object_class_install_property (gobject_class, ARG_EXTRA_SIZE_BYTES,
225 g_param_spec_uint ("extra-size-bytes", "Extra Size (kB)",
226 "Amount of data the queues can grow if one of them is empty (bytes, 0=disable)",
227 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BYTES, G_PARAM_READWRITE));
228 g_object_class_install_property (gobject_class, ARG_EXTRA_SIZE_BUFFERS,
229 g_param_spec_uint ("extra-size-buffers", "Extra Size (buffers)",
230 "Amount of buffers the queues can grow if one of them is empty (0=disable)",
231 0, G_MAXUINT, DEFAULT_EXTRA_SIZE_BUFFERS, G_PARAM_READWRITE));
232 g_object_class_install_property (gobject_class, ARG_EXTRA_SIZE_TIME,
233 g_param_spec_uint64 ("extra-size-time", "Extra Size (ns)",
234 "Amount of time the queues can grow if one of them is empty (in ns, 0=disable)",
235 0, G_MAXUINT64, DEFAULT_EXTRA_SIZE_TIME, G_PARAM_READWRITE));
237 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_multi_queue_finalize);
239 gstelement_class->request_new_pad =
240 GST_DEBUG_FUNCPTR (gst_multi_queue_request_new_pad);
241 gstelement_class->release_pad =
242 GST_DEBUG_FUNCPTR (gst_multi_queue_release_pad);
246 gst_multi_queue_init (GstMultiQueue * mqueue, GstMultiQueueClass * klass)
248 mqueue->nbqueues = 0;
249 mqueue->queues = NULL;
251 mqueue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
252 mqueue->max_size.visible = DEFAULT_MAX_SIZE_BUFFERS;
253 mqueue->max_size.time = DEFAULT_MAX_SIZE_TIME;
255 mqueue->extra_size.bytes = DEFAULT_EXTRA_SIZE_BYTES;
256 mqueue->extra_size.visible = DEFAULT_EXTRA_SIZE_BUFFERS;
257 mqueue->extra_size.time = DEFAULT_EXTRA_SIZE_TIME;
261 mqueue->nextnotlinked = -1;
263 mqueue->qlock = g_mutex_new ();
267 gst_multi_queue_finalize (GObject * object)
269 GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
271 g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
272 g_list_free (mqueue->queues);
273 mqueue->queues = NULL;
275 /* free/unref instance data */
276 g_mutex_free (mqueue->qlock);
278 G_OBJECT_CLASS (parent_class)->finalize (object);
283 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START { \
284 GList * tmp = mq->queues; \
286 GstSingleQueue *q = (GstSingleQueue*)tmp->data; \
287 q->max_size.format = mq->max_size.format; \
288 tmp = g_list_next(tmp); \
293 gst_multi_queue_set_property (GObject * object, guint prop_id,
294 const GValue * value, GParamSpec * pspec)
296 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
299 case ARG_MAX_SIZE_BYTES:
300 mq->max_size.bytes = g_value_get_uint (value);
301 SET_CHILD_PROPERTY (mq, bytes);
303 case ARG_MAX_SIZE_BUFFERS:
304 mq->max_size.visible = g_value_get_uint (value);
305 SET_CHILD_PROPERTY (mq, visible);
307 case ARG_MAX_SIZE_TIME:
308 mq->max_size.time = g_value_get_uint64 (value);
309 SET_CHILD_PROPERTY (mq, time);
311 case ARG_EXTRA_SIZE_BYTES:
312 mq->extra_size.bytes = g_value_get_uint (value);
314 case ARG_EXTRA_SIZE_BUFFERS:
315 mq->extra_size.visible = g_value_get_uint (value);
317 case ARG_EXTRA_SIZE_TIME:
318 mq->extra_size.time = g_value_get_uint64 (value);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 gst_multi_queue_get_property (GObject * object, guint prop_id,
328 GValue * value, GParamSpec * pspec)
330 GstMultiQueue *mq = GST_MULTI_QUEUE (object);
332 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
335 case ARG_EXTRA_SIZE_BYTES:
336 g_value_set_uint (value, mq->extra_size.bytes);
338 case ARG_EXTRA_SIZE_BUFFERS:
339 g_value_set_uint (value, mq->extra_size.visible);
341 case ARG_EXTRA_SIZE_TIME:
342 g_value_set_uint64 (value, mq->extra_size.time);
344 case ARG_MAX_SIZE_BYTES:
345 g_value_set_uint (value, mq->max_size.bytes);
347 case ARG_MAX_SIZE_BUFFERS:
348 g_value_set_uint (value, mq->max_size.visible);
350 case ARG_MAX_SIZE_TIME:
351 g_value_set_uint64 (value, mq->max_size.time);
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
362 gst_multi_queue_get_internal_links (GstPad * pad)
365 GstMultiQueue *mqueue;
366 GstSingleQueue *sq = NULL;
369 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
371 mqueue = GST_MULTI_QUEUE (GST_PAD_PARENT (pad));
375 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
376 /* Find which single queue it belongs to */
377 for (tmp = mqueue->queues; tmp && !res; tmp = g_list_next (tmp)) {
378 sq = (GstSingleQueue *) tmp->data;
380 if (sq->sinkpad == pad)
381 res = g_list_prepend (res, sq->srcpad);
382 if (sq->srcpad == pad)
383 res = g_list_prepend (res, sq->sinkpad);
387 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
388 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
394 GST_DEBUG_OBJECT (pad, "no parent");
405 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
408 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
409 GstSingleQueue *squeue;
411 GST_LOG_OBJECT (element, "name : %s", name);
413 /* Create a new single queue, add the sink and source pad and return the sink pad */
414 squeue = gst_single_queue_new (mqueue);
416 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
417 mqueue->queues = g_list_append (mqueue->queues, squeue);
418 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
421 GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
422 GST_DEBUG_PAD_NAME (squeue->sinkpad));
424 return squeue->sinkpad;
428 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
430 GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
431 GstSingleQueue *sq = NULL;
434 // GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
436 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
437 /* Find which single queue it belongs to, knowing that it should be a sinkpad */
438 for (tmp = mqueue->queues; tmp; tmp = g_list_next (tmp)) {
439 sq = (GstSingleQueue *) tmp->data;
441 if (sq->sinkpad == pad)
446 GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
447 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
451 /* FIXME: The removal of the singlequeue should probably not happen until it
452 * finishes draining */
454 /* remove it from the list */
455 mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
457 /* FIXME : recompute next-non-linked */
458 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
460 /* delete SingleQueue */
461 gst_data_queue_set_flushing (sq->queue, TRUE);
463 gst_pad_set_active (sq->srcpad, FALSE);
464 gst_pad_set_active (sq->sinkpad, FALSE);
465 gst_element_remove_pad (element, sq->srcpad);
466 gst_element_remove_pad (element, sq->sinkpad);
467 gst_single_queue_free (sq);
471 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
475 GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
479 sq->srcresult = GST_FLOW_WRONG_STATE;
480 gst_data_queue_set_flushing (sq->queue, TRUE);
482 /* wake up non-linked task */
483 GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
485 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
486 g_cond_signal (sq->turn);
487 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
489 GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
490 result = gst_pad_pause_task (sq->srcpad);
492 gst_data_queue_flush (sq->queue);
493 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
494 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
495 /* All pads start off not-linked for a smooth kick-off */
496 sq->srcresult = GST_FLOW_NOT_LINKED;
498 sq->max_size.visible = mq->max_size.visible;
503 gst_data_queue_set_flushing (sq->queue, FALSE);
505 GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
507 gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
513 /* calculate the diff between running time on the sink and src of the queue.
514 * This is the total amount of time in the queue.
517 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
519 gint64 sink_time, src_time;
522 gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
523 sq->sink_segment.last_stop);
525 src_time = gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
526 sq->src_segment.last_stop);
528 GST_DEBUG_OBJECT (mq,
529 "queue %d, sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT, sq->id,
530 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
532 /* This allows for streams with out of order timestamping - sometimes the
533 * emerging timestamp is later than the arriving one(s) */
534 if (sink_time >= src_time)
535 sq->cur_time = sink_time - src_time;
540 /* take a NEWSEGMENT event and apply the values to segment, updating the time
543 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
544 GstSegment * segment)
549 gint64 start, stop, time;
551 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
552 &format, &start, &stop, &time);
554 /* now configure the values, we use these to track timestamps on the
556 if (format != GST_FORMAT_TIME) {
557 /* non-time format, pretent the current time segment is closed with a
558 * 0 start and unknown stop time. */
560 format = GST_FORMAT_TIME;
566 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
568 gst_segment_set_newsegment_full (segment, update,
569 rate, arate, format, start, stop, time);
571 GST_DEBUG_OBJECT (mq,
572 "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
574 /* segment can update the time level of the queue */
575 update_time_level (mq, sq);
577 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
580 /* take a buffer and update segment, updating the time level of the queue. */
582 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
583 GstClockTime duration, GstSegment * segment)
585 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
587 /* if no timestamp is set, assume it's continuous with the previous
589 if (timestamp == GST_CLOCK_TIME_NONE)
590 timestamp = segment->last_stop;
593 if (duration != GST_CLOCK_TIME_NONE)
594 timestamp += duration;
596 GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
597 sq->id, GST_TIME_ARGS (timestamp));
599 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
601 /* calc diff with other end */
602 update_time_level (mq, sq);
603 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
607 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
608 GstMiniObject * object)
610 GstFlowReturn result = GST_FLOW_OK;
612 if (GST_IS_BUFFER (object)) {
614 GstClockTime timestamp, duration;
616 buffer = GST_BUFFER_CAST (object);
617 timestamp = GST_BUFFER_TIMESTAMP (buffer);
618 duration = GST_BUFFER_DURATION (buffer);
620 apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
622 /* Applying the buffer may have made the queue non-full again, unblock it if needed */
623 gst_data_queue_limits_changed (sq->queue);
625 GST_DEBUG_OBJECT (mq,
626 "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
627 sq->id, buffer, GST_TIME_ARGS (timestamp));
629 result = gst_pad_push (sq->srcpad, buffer);
630 } else if (GST_IS_EVENT (object)) {
633 event = GST_EVENT_CAST (object);
635 switch (GST_EVENT_TYPE (event)) {
637 result = GST_FLOW_UNEXPECTED;
639 case GST_EVENT_NEWSEGMENT:
640 apply_segment (mq, sq, event, &sq->src_segment);
641 /* Applying the segment may have made the queue non-full again, unblock it if needed */
642 gst_data_queue_limits_changed (sq->queue);
648 GST_DEBUG_OBJECT (mq,
649 "SingleQueue %d : Pushing event %p of type %s",
650 sq->id, event, GST_EVENT_TYPE_NAME (event));
652 gst_pad_push_event (sq->srcpad, event);
654 g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
662 static GstMiniObject *
663 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
674 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
677 gst_mini_object_unref (item->object);
681 /* takes ownership of passed mini object! */
682 static GstMultiQueueItem *
683 gst_multi_queue_item_new (GstMiniObject * object, guint32 curid)
685 GstMultiQueueItem *item;
687 item = g_new (GstMultiQueueItem, 1);
688 item->object = object;
689 item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
692 if (GST_IS_BUFFER (object)) {
693 item->size = GST_BUFFER_SIZE (object);
694 item->duration = GST_BUFFER_DURATION (object);
695 if (item->duration == GST_CLOCK_TIME_NONE)
697 item->visible = TRUE;
701 item->visible = FALSE;
706 /* Each main loop attempts to push buffers until the return value
707 * is not-linked. not-linked pads are not allowed to push data beyond
708 * any linked pads, so they don't 'rush ahead of the pack'.
711 gst_multi_queue_loop (GstPad * pad)
714 GstMultiQueueItem *item;
715 GstDataQueueItem *sitem;
717 GstMiniObject *object;
720 GstFlowReturn result;
722 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
726 GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
728 /* Get something from the queue, blocking until that happens, or we get
730 if (!(gst_data_queue_pop (sq->queue, &sitem)))
733 item = (GstMultiQueueItem *) sitem;
736 /* steal the object and destroy the item */
737 object = gst_multi_queue_item_steal_object (item);
738 gst_multi_queue_item_destroy (item);
740 GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
741 sq->id, newid, oldid);
743 /* If we're not-linked, we do some extra work because we might need to
744 * wait before pushing. If we're linked but there's a gap in the IDs,
745 * or it's the first loop, or we just passed the previous highid,
746 * we might need to wake some sleeping pad up, so there's extra work
748 if (sq->srcresult == GST_FLOW_NOT_LINKED ||
749 (oldid == -1) || (newid != (oldid + 1)) || oldid > mq->highid) {
750 GST_LOG_OBJECT (mq, "CHECKING sq->srcresult: %s",
751 gst_flow_get_name (sq->srcresult));
753 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
755 /* Update the nextid so other threads know when to wake us up */
758 /* Update the oldid (the last ID we output) for highid tracking */
762 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
763 /* Go to sleep until it's time to push this buffer */
765 /* Recompute the highid */
766 compute_high_id (mq);
767 while (newid > mq->highid && sq->srcresult == GST_FLOW_NOT_LINKED) {
768 GST_DEBUG_OBJECT (mq, "queue %d sleeping for not-linked wakeup with "
769 "newid %u and highid %u", sq->id, newid, mq->highid);
772 /* Wake up all non-linked pads before we sleep */
773 wake_up_next_non_linked (mq);
776 g_cond_wait (sq->turn, mq->qlock);
779 GST_DEBUG_OBJECT (mq, "queue %d woken from sleeping for not-linked "
780 "wakeup with newid %u and highid %u", sq->id, newid, mq->highid);
783 /* Re-compute the high_id in case someone else pushed */
784 compute_high_id (mq);
786 compute_high_id (mq);
787 /* Wake up all non-linked pads */
788 wake_up_next_non_linked (mq);
790 /* We're done waiting, we can clear the nextid */
793 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
796 GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
797 gst_flow_get_name (sq->srcresult));
799 /* Try to push out the new object */
800 result = gst_single_queue_push_one (mq, sq, object);
801 sq->srcresult = result;
803 if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED)
806 GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
807 gst_flow_get_name (sq->srcresult));
815 /* Need to make sure wake up any sleeping pads when we exit */
816 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
817 compute_high_id (mq);
818 wake_up_next_non_linked (mq);
819 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
821 gst_data_queue_set_flushing (sq->queue, TRUE);
822 gst_pad_pause_task (sq->srcpad);
823 GST_CAT_LOG_OBJECT (multi_queue_debug, mq,
824 "SingleQueue[%d] task paused, reason:%s",
825 sq->id, gst_flow_get_name (sq->srcresult));
831 * gst_multi_queue_chain:
833 * This is similar to GstQueue's chain function, except:
834 * _ we don't have leak behavioures,
835 * _ we push with a unique id (curid)
838 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
842 GstMultiQueueItem *item;
843 GstFlowReturn ret = GST_FLOW_OK;
845 GstClockTime timestamp, duration;
847 sq = gst_pad_get_element_private (pad);
848 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
850 /* Get a unique incrementing id */
851 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
852 curid = mq->counter++;
853 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
855 GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
856 sq->id, buffer, curid);
858 item = gst_multi_queue_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
860 timestamp = GST_BUFFER_TIMESTAMP (buffer);
861 duration = GST_BUFFER_DURATION (buffer);
863 if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
866 /* update time level, we must do this after pushing the data in the queue so
867 * that we never end up filling the queue first. */
868 apply_buffer (mq, sq, timestamp, duration, &sq->sink_segment);
871 gst_object_unref (mq);
879 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
880 sq->id, gst_flow_get_name (ret));
881 gst_multi_queue_item_destroy (item);
887 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
891 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
894 /* All pads start off not-linked for a smooth kick-off */
895 sq->srcresult = GST_FLOW_NOT_LINKED;
897 sq->srcresult = GST_FLOW_WRONG_STATE;
898 gst_data_queue_flush (sq->queue);
904 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
909 GstMultiQueueItem *item;
912 GstEvent *sref = NULL;
914 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
915 mq = (GstMultiQueue *) gst_pad_get_parent (pad);
917 type = GST_EVENT_TYPE (event);
920 case GST_EVENT_FLUSH_START:
921 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
924 res = gst_pad_push_event (sq->srcpad, event);
926 gst_single_queue_flush (mq, sq, TRUE);
929 case GST_EVENT_FLUSH_STOP:
930 GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
933 res = gst_pad_push_event (sq->srcpad, event);
935 gst_single_queue_flush (mq, sq, FALSE);
937 case GST_EVENT_NEWSEGMENT:
938 /* take ref because the queue will take ownership and we need the event
939 * afterwards to update the segment */
940 sref = gst_event_ref (event);
944 if (!(GST_EVENT_IS_SERIALIZED (event))) {
945 res = gst_pad_push_event (sq->srcpad, event);
951 /* Get an unique incrementing id */
952 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
953 curid = mq->counter++;
954 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
956 item = gst_multi_queue_item_new ((GstMiniObject *) event, curid);
958 GST_DEBUG_OBJECT (mq,
959 "SingleQueue %d : Enqueuing event %p of type %s with id %d",
960 sq->id, event, GST_EVENT_TYPE_NAME (event), curid);
962 if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
965 /* mark EOS when we received one, we must do that after putting the
966 * buffer in the queue because EOS marks the buffer as filled. No need to take
967 * a lock, the _check_full happens from this thread only, right before pushing
973 case GST_EVENT_NEWSEGMENT:
974 apply_segment (mq, sq, sref, &sq->sink_segment);
975 gst_event_unref (sref);
981 gst_object_unref (mq);
986 GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
987 sq->id, gst_flow_get_name (sq->srcresult));
989 gst_event_unref (sref);
990 gst_multi_queue_item_destroy (item);
996 gst_multi_queue_getcaps (GstPad * pad)
998 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1002 otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
1004 GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
1006 result = gst_pad_peer_get_caps (otherpad);
1008 result = gst_caps_new_any ();
1013 static GstFlowReturn
1014 gst_multi_queue_bufferalloc (GstPad * pad, guint64 offset, guint size,
1015 GstCaps * caps, GstBuffer ** buf)
1017 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1019 return gst_pad_alloc_buffer (sq->srcpad, offset, size, caps, buf);
1023 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
1027 gboolean result = FALSE;
1029 sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
1032 GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
1035 result = gst_single_queue_flush (mq, sq, FALSE);
1037 result = gst_single_queue_flush (mq, sq, TRUE);
1038 /* make sure streaming finishes */
1039 result |= gst_pad_stop_task (pad);
1045 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
1051 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
1053 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1055 return gst_pad_push_event (sq->sinkpad, event);
1059 gst_multi_queue_src_query (GstPad * pad, GstQuery * query)
1061 GstSingleQueue *sq = gst_pad_get_element_private (pad);
1065 /* FIXME, Handle position offset depending on queue size */
1067 /* default handling */
1068 if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
1071 res = gst_pad_query (peerpad, query);
1073 gst_object_unref (peerpad);
1080 GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
1086 * Next-non-linked functions
1089 /* WITH LOCK TAKEN */
1091 wake_up_next_non_linked (GstMultiQueue * mq)
1095 /* maybe no-one is waiting */
1096 if (mq->numwaiting < 1)
1099 /* Else figure out which singlequeue(s) need waking up */
1100 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1101 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1103 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1104 if (sq->nextid != 0 && sq->nextid <= mq->highid) {
1105 GST_LOG_OBJECT (mq, "Waking up singlequeue %d", sq->id);
1106 g_cond_signal (sq->turn);
1112 /* WITH LOCK TAKEN */
1114 compute_high_id (GstMultiQueue * mq)
1116 /* The high-id is either the highest id among the linked pads, or if all
1117 * pads are not-linked, it's the lowest not-linked pad */
1119 guint32 lowest = G_MAXUINT32;
1120 guint32 highid = G_MAXUINT32;
1122 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1123 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1125 GST_LOG_OBJECT (mq, "inspecting sq:%d , nextid:%d, oldid:%d, srcresult:%s",
1126 sq->id, sq->nextid, sq->oldid, gst_flow_get_name (sq->srcresult));
1128 if (sq->srcresult == GST_FLOW_NOT_LINKED) {
1129 /* No need to consider queues which are not waiting */
1130 if (sq->nextid == 0) {
1131 GST_LOG_OBJECT (mq, "sq:%d is not waiting - ignoring", sq->id);
1135 if (sq->nextid < lowest)
1136 lowest = sq->nextid;
1137 } else if (sq->srcresult != GST_FLOW_UNEXPECTED) {
1138 /* If we don't have a global highid, or the global highid is lower than
1139 * this single queue's last outputted id, store the queue's one,
1140 * unless the singlequeue is at EOS (srcresult = UNEXPECTED) */
1141 if ((highid == G_MAXUINT32) || (sq->oldid > highid))
1146 if (highid == G_MAXUINT32 || lowest < highid)
1147 mq->highid = lowest;
1149 mq->highid = highid;
1151 GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
1155 #define IS_FILLED(format, value) ((sq->max_size.format) != 0 && \
1156 (sq->max_size.format) <= (value))
1159 * GstSingleQueue functions
1162 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1164 GstMultiQueue *mq = sq->mqueue;
1166 GstDataQueueSize size;
1167 gboolean filled = FALSE;
1169 gst_data_queue_get_level (sq->queue, &size);
1171 GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
1173 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1174 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1175 GstSingleQueue *ssq = (GstSingleQueue *) tmp->data;
1176 GstDataQueueSize ssize;
1178 GST_LOG_OBJECT (mq, "Checking Queue %d", ssq->id);
1180 if (gst_data_queue_is_empty (ssq->queue)) {
1181 GST_LOG_OBJECT (mq, "Queue %d is empty", ssq->id);
1182 if (IS_FILLED (visible, size.visible)) {
1183 sq->max_size.visible++;
1184 GST_DEBUG_OBJECT (mq,
1185 "Another queue is empty, bumping single queue %d max visible to %d",
1186 sq->id, sq->max_size.visible);
1188 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1191 /* check if we reached the hard time/bytes limits */
1192 gst_data_queue_get_level (ssq->queue, &ssize);
1194 GST_DEBUG_OBJECT (mq,
1195 "queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT "/%"
1196 G_GUINT64_FORMAT, ssq->id, ssize.visible, sq->max_size.visible,
1197 ssize.bytes, sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1199 /* if this queue is filled completely we must signal overrun */
1200 if (IS_FILLED (bytes, ssize.bytes) || IS_FILLED (time, sq->cur_time)) {
1201 GST_LOG_OBJECT (mq, "Queue %d is filled", ssq->id);
1205 /* no queues were empty */
1206 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1208 /* Overrun is always forwarded, since this is blocking the upstream element */
1210 GST_DEBUG_OBJECT (mq, "A queue is filled, signalling overrun");
1211 g_signal_emit (G_OBJECT (mq), gst_multi_queue_signals[SIGNAL_OVERRUN], 0);
1219 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
1221 gboolean empty = TRUE;
1222 GstMultiQueue *mq = sq->mqueue;
1226 "Single Queue %d is empty, Checking other single queues", sq->id);
1228 GST_MULTI_QUEUE_MUTEX_LOCK (mq);
1229 for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
1230 GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
1232 if (gst_data_queue_is_full (sq->queue)) {
1233 GstDataQueueSize size;
1235 gst_data_queue_get_level (sq->queue, &size);
1236 if (IS_FILLED (visible, size.visible)) {
1237 sq->max_size.visible++;
1238 GST_DEBUG_OBJECT (mq,
1239 "queue %d is filled, bumping its max visible to %d", sq->id,
1240 sq->max_size.visible);
1241 gst_data_queue_limits_changed (sq->queue);
1244 if (!gst_data_queue_is_empty (sq->queue))
1247 GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
1250 GST_DEBUG_OBJECT (mq, "All queues are empty, signalling it");
1251 g_signal_emit (G_OBJECT (mq), gst_multi_queue_signals[SIGNAL_UNDERRUN], 0);
1256 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
1257 guint64 time, GstSingleQueue * sq)
1261 GST_DEBUG ("queue %d: visible %u/%u, bytes %u/%u, time %" G_GUINT64_FORMAT
1262 "/%" G_GUINT64_FORMAT, sq->id, visible, sq->max_size.visible, bytes,
1263 sq->max_size.bytes, sq->cur_time, sq->max_size.time);
1265 /* we are always filled on EOS */
1269 /* we never go past the max visible items */
1270 if (IS_FILLED (visible, visible))
1273 if (sq->cur_time != 0) {
1274 /* if we have valid time in the queue, check */
1275 res = IS_FILLED (time, sq->cur_time);
1277 /* no valid time, check bytes */
1278 res = IS_FILLED (bytes, bytes);
1284 gst_single_queue_free (GstSingleQueue * sq)
1287 gst_data_queue_flush (sq->queue);
1288 g_object_unref (sq->queue);
1289 g_cond_free (sq->turn);
1293 static GstSingleQueue *
1294 gst_single_queue_new (GstMultiQueue * mqueue)
1299 sq = g_new0 (GstSingleQueue, 1);
1301 GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
1302 sq->id = mqueue->nbqueues++;
1304 /* copy over max_size and extra_size so we don't need to take the lock
1305 * any longer when checking if the queue is full. */
1306 sq->max_size.visible = mqueue->max_size.visible;
1307 sq->max_size.bytes = mqueue->max_size.bytes;
1308 sq->max_size.time = mqueue->max_size.time;
1310 sq->extra_size.visible = mqueue->extra_size.visible;
1311 sq->extra_size.bytes = mqueue->extra_size.bytes;
1312 sq->extra_size.time = mqueue->extra_size.time;
1314 GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
1316 GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
1318 sq->mqueue = mqueue;
1319 sq->srcresult = GST_FLOW_WRONG_STATE;
1320 sq->queue = gst_data_queue_new ((GstDataQueueCheckFullFunction)
1321 single_queue_check_full, sq);
1323 gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
1324 gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
1328 sq->turn = g_cond_new ();
1330 /* attach to underrun/overrun signals to handle non-starvation */
1331 g_signal_connect (G_OBJECT (sq->queue), "full",
1332 G_CALLBACK (single_queue_overrun_cb), sq);
1333 g_signal_connect (G_OBJECT (sq->queue), "empty",
1334 G_CALLBACK (single_queue_underrun_cb), sq);
1336 tmp = g_strdup_printf ("sink%d", sq->id);
1337 sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, tmp);
1340 gst_pad_set_chain_function (sq->sinkpad,
1341 GST_DEBUG_FUNCPTR (gst_multi_queue_chain));
1342 gst_pad_set_activatepush_function (sq->sinkpad,
1343 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_activate_push));
1344 gst_pad_set_event_function (sq->sinkpad,
1345 GST_DEBUG_FUNCPTR (gst_multi_queue_sink_event));
1346 gst_pad_set_getcaps_function (sq->sinkpad,
1347 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1348 gst_pad_set_bufferalloc_function (sq->sinkpad,
1349 GST_DEBUG_FUNCPTR (gst_multi_queue_bufferalloc));
1350 gst_pad_set_internal_link_function (sq->sinkpad,
1351 GST_DEBUG_FUNCPTR (gst_multi_queue_get_internal_links));
1353 tmp = g_strdup_printf ("src%d", sq->id);
1354 sq->srcpad = gst_pad_new_from_static_template (&srctemplate, tmp);
1357 gst_pad_set_activatepush_function (sq->srcpad,
1358 GST_DEBUG_FUNCPTR (gst_multi_queue_src_activate_push));
1359 gst_pad_set_acceptcaps_function (sq->srcpad,
1360 GST_DEBUG_FUNCPTR (gst_multi_queue_acceptcaps));
1361 gst_pad_set_getcaps_function (sq->srcpad,
1362 GST_DEBUG_FUNCPTR (gst_multi_queue_getcaps));
1363 gst_pad_set_event_function (sq->srcpad,
1364 GST_DEBUG_FUNCPTR (gst_multi_queue_src_event));
1365 gst_pad_set_query_function (sq->srcpad,
1366 GST_DEBUG_FUNCPTR (gst_multi_queue_src_query));
1367 gst_pad_set_internal_link_function (sq->srcpad,
1368 GST_DEBUG_FUNCPTR (gst_multi_queue_get_internal_links));
1370 gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
1371 gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
1373 gst_pad_set_active (sq->srcpad, TRUE);
1374 gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
1376 gst_pad_set_active (sq->sinkpad, TRUE);
1377 gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
1379 GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",
1386 plugin_init (GstPlugin * plugin)
1388 return gst_element_register (plugin, "multiqueue", GST_RANK_NONE,
1389 GST_TYPE_MULTI_QUEUE);
1392 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1395 "multiqueue", plugin_init, VERSION, GST_LICENSE,
1396 GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)