gst-gmyth/multiqueue/gstmultiqueue.c
author melunko
Fri Aug 03 21:42:55 2007 +0100 (2007-08-03)
branchtrunk
changeset 794 02ba1859ddc0
permissions -rw-r--r--
[svn r800] Fixed the gmencoder.py get_all_log print that were writing PROGRESS twice
     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  *
     6  * gstmultiqueue.c:
     7  *
     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.
    12  *
    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.
    17  *
    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.
    22  */
    23 
    24 #ifdef HAVE_CONFIG_H
    25 #  include "config.h"
    26 #endif
    27 
    28 #include <gst/gst.h>
    29 #include "gstmultiqueue.h"
    30 
    31 /**
    32  * GstSingleQueue:
    33  * @sinkpad: associated sink #GstPad
    34  * @srcpad: associated source #GstPad
    35  *
    36  * Structure containing all information and properties about
    37  * a single queue.
    38  */
    39 typedef struct _GstSingleQueue GstSingleQueue;
    40 
    41 struct _GstSingleQueue
    42 {
    43   /* unique identifier of the queue */
    44   guint id;
    45 
    46   GstMultiQueue *mqueue;
    47 
    48   GstPad *sinkpad;
    49   GstPad *srcpad;
    50 
    51   /* flowreturn of previous srcpad push */
    52   GstFlowReturn srcresult;
    53   GstSegment sink_segment;
    54   GstSegment src_segment;
    55 
    56   /* queue of data */
    57   GstDataQueue *queue;
    58   GstDataQueueSize max_size, extra_size;
    59   GstClockTime cur_time;
    60   gboolean is_eos;
    61   gboolean inextra;             /* TRUE if the queue is currently in extradata mode */
    62 
    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 */
    67 };
    68 
    69 
    70 /* Extension of GstDataQueueItem structure for our usage */
    71 typedef struct _GstMultiQueueItem GstMultiQueueItem;
    72 
    73 struct _GstMultiQueueItem
    74 {
    75   GstMiniObject *object;
    76   guint size;
    77   guint64 duration;
    78   gboolean visible;
    79 
    80   GDestroyNotify destroy;
    81   guint32 posid;
    82 };
    83 
    84 static GstSingleQueue *gst_single_queue_new (GstMultiQueue * mqueue);
    85 static void gst_single_queue_free (GstSingleQueue * squeue);
    86 
    87 static void wake_up_next_non_linked (GstMultiQueue * mq);
    88 static void compute_high_id (GstMultiQueue * mq);
    89 
    90 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink%d",
    91     GST_PAD_SINK,
    92     GST_PAD_REQUEST,
    93     GST_STATIC_CAPS_ANY);
    94 
    95 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
    96     GST_PAD_SRC,
    97     GST_PAD_SOMETIMES,
    98     GST_STATIC_CAPS_ANY);
    99 
   100 GST_DEBUG_CATEGORY_STATIC (multi_queue_debug);
   101 #define GST_CAT_DEFAULT (multi_queue_debug)
   102 
   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
   110 
   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
   117 
   118 /* Signals and args */
   119 enum
   120 {
   121   SIGNAL_UNDERRUN,
   122   SIGNAL_OVERRUN,
   123   LAST_SIGNAL
   124 };
   125 
   126 enum
   127 {
   128   ARG_0,
   129   ARG_EXTRA_SIZE_BYTES,
   130   ARG_EXTRA_SIZE_BUFFERS,
   131   ARG_EXTRA_SIZE_TIME,
   132   ARG_MAX_SIZE_BYTES,
   133   ARG_MAX_SIZE_BUFFERS,
   134   ARG_MAX_SIZE_TIME,
   135 };
   136 
   137 
   138 static const GstElementDetails  gst_multiqueue_details = 
   139 GST_ELEMENT_DETAILS ("MultiQueue",
   140       "Generic",
   141       "Multiple data queue",
   142       "Edward Hervey <edward@fluendo.com>");
   143 
   144 
   145 #define GST_MULTI_QUEUE_MUTEX_LOCK(q) G_STMT_START {                          \
   146   g_mutex_lock (q->qlock);                                              \
   147 } G_STMT_END
   148 
   149 #define GST_MULTI_QUEUE_MUTEX_UNLOCK(q) G_STMT_START {                        \
   150   g_mutex_unlock (q->qlock);                                            \
   151 } G_STMT_END
   152 
   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);
   158 
   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);
   162 
   163 static void gst_multi_queue_loop (GstPad * pad);
   164 
   165 #define _do_init(bla) \
   166   GST_DEBUG_CATEGORY_INIT (multi_queue_debug, "multiqueue", 0, "multiqueue element");
   167 
   168 GST_BOILERPLATE_FULL (GstMultiQueue, gst_multi_queue, GstElement,
   169     GST_TYPE_ELEMENT, _do_init);
   170 
   171 static guint gst_multi_queue_signals[LAST_SIGNAL] = { 0 };
   172 
   173 
   174 
   175 static void
   176 gst_multi_queue_base_init (gpointer g_class)
   177 {
   178   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
   179 
   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));
   185 }
   186 
   187 static void
   188 gst_multi_queue_class_init (GstMultiQueueClass * klass)
   189 {
   190   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   191   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   192 
   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);
   197 
   198   /* SIGNALS */
   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);
   203 
   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);
   208 
   209   /* PROPERTIES */
   210 
   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));
   223 
   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));
   236 
   237   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_multi_queue_finalize);
   238 
   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);
   243 }
   244 
   245 static void
   246 gst_multi_queue_init (GstMultiQueue * mqueue, GstMultiQueueClass * klass)
   247 {
   248   mqueue->nbqueues = 0;
   249   mqueue->queues = NULL;
   250 
   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;
   254 
   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;
   258 
   259   mqueue->counter = 1;
   260   mqueue->highid = -1;
   261   mqueue->nextnotlinked = -1;
   262 
   263   mqueue->qlock = g_mutex_new ();
   264 }
   265 
   266 static void
   267 gst_multi_queue_finalize (GObject * object)
   268 {
   269   GstMultiQueue *mqueue = GST_MULTI_QUEUE (object);
   270 
   271   g_list_foreach (mqueue->queues, (GFunc) gst_single_queue_free, NULL);
   272   g_list_free (mqueue->queues);
   273   mqueue->queues = NULL;
   274 
   275   /* free/unref instance data */
   276   g_mutex_free (mqueue->qlock);
   277 
   278   G_OBJECT_CLASS (parent_class)->finalize (object);
   279 }
   280 
   281 
   282 
   283 #define SET_CHILD_PROPERTY(mq,format) G_STMT_START {	        \
   284     GList * tmp = mq->queues;					\
   285     while (tmp) {						\
   286       GstSingleQueue *q = (GstSingleQueue*)tmp->data;		\
   287       q->max_size.format = mq->max_size.format;                 \
   288       tmp = g_list_next(tmp);					\
   289     };								\
   290 } G_STMT_END
   291 
   292 static void
   293 gst_multi_queue_set_property (GObject * object, guint prop_id,
   294     const GValue * value, GParamSpec * pspec)
   295 {
   296   GstMultiQueue *mq = GST_MULTI_QUEUE (object);
   297 
   298   switch (prop_id) {
   299     case ARG_MAX_SIZE_BYTES:
   300       mq->max_size.bytes = g_value_get_uint (value);
   301       SET_CHILD_PROPERTY (mq, bytes);
   302       break;
   303     case ARG_MAX_SIZE_BUFFERS:
   304       mq->max_size.visible = g_value_get_uint (value);
   305       SET_CHILD_PROPERTY (mq, visible);
   306       break;
   307     case ARG_MAX_SIZE_TIME:
   308       mq->max_size.time = g_value_get_uint64 (value);
   309       SET_CHILD_PROPERTY (mq, time);
   310       break;
   311     case ARG_EXTRA_SIZE_BYTES:
   312       mq->extra_size.bytes = g_value_get_uint (value);
   313       break;
   314     case ARG_EXTRA_SIZE_BUFFERS:
   315       mq->extra_size.visible = g_value_get_uint (value);
   316       break;
   317     case ARG_EXTRA_SIZE_TIME:
   318       mq->extra_size.time = g_value_get_uint64 (value);
   319       break;
   320     default:
   321       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   322       break;
   323   }
   324 }
   325 
   326 static void
   327 gst_multi_queue_get_property (GObject * object, guint prop_id,
   328     GValue * value, GParamSpec * pspec)
   329 {
   330   GstMultiQueue *mq = GST_MULTI_QUEUE (object);
   331 
   332   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   333 
   334   switch (prop_id) {
   335     case ARG_EXTRA_SIZE_BYTES:
   336       g_value_set_uint (value, mq->extra_size.bytes);
   337       break;
   338     case ARG_EXTRA_SIZE_BUFFERS:
   339       g_value_set_uint (value, mq->extra_size.visible);
   340       break;
   341     case ARG_EXTRA_SIZE_TIME:
   342       g_value_set_uint64 (value, mq->extra_size.time);
   343       break;
   344     case ARG_MAX_SIZE_BYTES:
   345       g_value_set_uint (value, mq->max_size.bytes);
   346       break;
   347     case ARG_MAX_SIZE_BUFFERS:
   348       g_value_set_uint (value, mq->max_size.visible);
   349       break;
   350     case ARG_MAX_SIZE_TIME:
   351       g_value_set_uint64 (value, mq->max_size.time);
   352       break;
   353     default:
   354       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   355       break;
   356   }
   357 
   358   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   359 }
   360 
   361 GList *
   362 gst_multi_queue_get_internal_links (GstPad * pad)
   363 {
   364   GList *res = NULL;
   365   GstMultiQueue *mqueue;
   366   GstSingleQueue *sq = NULL;
   367   GList *tmp;
   368 
   369   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
   370 
   371   mqueue = GST_MULTI_QUEUE (GST_PAD_PARENT (pad));
   372   if (!mqueue)
   373     goto no_parent;
   374 
   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;
   379 
   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);
   384   }
   385 
   386   if (!res)
   387     GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
   388   GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
   389 
   390   return res;
   391 
   392 no_parent:
   393   {
   394     GST_DEBUG_OBJECT (pad, "no parent");
   395     return NULL;
   396   }
   397 }
   398 
   399 
   400 /*
   401  * GstElement methods
   402  */
   403 
   404 static GstPad *
   405 gst_multi_queue_request_new_pad (GstElement * element, GstPadTemplate * temp,
   406     const gchar * name)
   407 {
   408   GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
   409   GstSingleQueue *squeue;
   410 
   411   GST_LOG_OBJECT (element, "name : %s", name);
   412 
   413   /* Create a new single queue, add the sink and source pad and return the sink pad */
   414   squeue = gst_single_queue_new (mqueue);
   415 
   416   GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
   417   mqueue->queues = g_list_append (mqueue->queues, squeue);
   418   GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
   419 
   420   /*
   421   GST_DEBUG_OBJECT (mqueue, "Returning pad %s:%s",
   422       GST_DEBUG_PAD_NAME (squeue->sinkpad));
   423     */
   424   return squeue->sinkpad;
   425 }
   426 
   427 static void
   428 gst_multi_queue_release_pad (GstElement * element, GstPad * pad)
   429 {
   430   GstMultiQueue *mqueue = GST_MULTI_QUEUE (element);
   431   GstSingleQueue *sq = NULL;
   432   GList *tmp;
   433 
   434 //  GST_LOG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
   435 
   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;
   440 
   441     if (sq->sinkpad == pad)
   442       break;
   443   }
   444 
   445   if (!tmp) {
   446     GST_WARNING_OBJECT (mqueue, "That pad doesn't belong to this element ???");
   447     GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
   448     return;
   449   }
   450 
   451   /* FIXME: The removal of the singlequeue should probably not happen until it
   452    * finishes draining */
   453 
   454   /* remove it from the list */
   455   mqueue->queues = g_list_delete_link (mqueue->queues, tmp);
   456 
   457   /* FIXME : recompute next-non-linked */
   458   GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
   459 
   460   /* delete SingleQueue */
   461   gst_data_queue_set_flushing (sq->queue, TRUE);
   462 
   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);
   468 }
   469 
   470 static gboolean
   471 gst_single_queue_flush (GstMultiQueue * mq, GstSingleQueue * sq, gboolean flush)
   472 {
   473   gboolean result;
   474 
   475   GST_DEBUG_OBJECT (mq, "flush %s queue %d", (flush ? "start" : "stop"),
   476       sq->id);
   477 
   478   if (flush) {
   479     sq->srcresult = GST_FLOW_WRONG_STATE;
   480     gst_data_queue_set_flushing (sq->queue, TRUE);
   481 
   482     /* wake up non-linked task */
   483     GST_LOG_OBJECT (mq, "SingleQueue %d : waking up eventually waiting task",
   484         sq->id);
   485     GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   486     g_cond_signal (sq->turn);
   487     GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   488 
   489     GST_LOG_OBJECT (mq, "SingleQueue %d : pausing task", sq->id);
   490     result = gst_pad_pause_task (sq->srcpad);
   491   } else {
   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;
   497     sq->cur_time = 0;
   498     sq->max_size.visible = mq->max_size.visible;
   499     sq->is_eos = FALSE;
   500     sq->inextra = FALSE;
   501     sq->nextid = 0;
   502     sq->oldid = 0;
   503     gst_data_queue_set_flushing (sq->queue, FALSE);
   504 
   505     GST_LOG_OBJECT (mq, "SingleQueue %d : starting task", sq->id);
   506     result =
   507         gst_pad_start_task (sq->srcpad, (GstTaskFunction) gst_multi_queue_loop,
   508         sq->srcpad);
   509   }
   510   return result;
   511 }
   512 
   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. 
   515  * WITH LOCK TAKEN */
   516 static void
   517 update_time_level (GstMultiQueue * mq, GstSingleQueue * sq)
   518 {
   519   gint64 sink_time, src_time;
   520 
   521   sink_time =
   522       gst_segment_to_running_time (&sq->sink_segment, GST_FORMAT_TIME,
   523       sq->sink_segment.last_stop);
   524 
   525   src_time = gst_segment_to_running_time (&sq->src_segment, GST_FORMAT_TIME,
   526       sq->src_segment.last_stop);
   527 
   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));
   531 
   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;
   536   else
   537     sq->cur_time = 0;
   538 }
   539 
   540 /* take a NEWSEGMENT event and apply the values to segment, updating the time
   541  * level of queue. */
   542 static void
   543 apply_segment (GstMultiQueue * mq, GstSingleQueue * sq, GstEvent * event,
   544     GstSegment * segment)
   545 {
   546   gboolean update;
   547   GstFormat format;
   548   gdouble rate, arate;
   549   gint64 start, stop, time;
   550 
   551   gst_event_parse_new_segment_full (event, &update, &rate, &arate,
   552       &format, &start, &stop, &time);
   553 
   554   /* now configure the values, we use these to track timestamps on the
   555    * sinkpad. */
   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. */
   559     update = FALSE;
   560     format = GST_FORMAT_TIME;
   561     start = 0;
   562     stop = -1;
   563     time = 0;
   564   }
   565 
   566   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   567 
   568   gst_segment_set_newsegment_full (segment, update,
   569       rate, arate, format, start, stop, time);
   570 
   571   GST_DEBUG_OBJECT (mq,
   572       "queue %d, configured NEWSEGMENT %" GST_SEGMENT_FORMAT, sq->id, segment);
   573 
   574   /* segment can update the time level of the queue */
   575   update_time_level (mq, sq);
   576 
   577   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   578 }
   579 
   580 /* take a buffer and update segment, updating the time level of the queue. */
   581 static void
   582 apply_buffer (GstMultiQueue * mq, GstSingleQueue * sq, GstClockTime timestamp,
   583     GstClockTime duration, GstSegment * segment)
   584 {
   585   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   586 
   587   /* if no timestamp is set, assume it's continuous with the previous 
   588    * time */
   589   if (timestamp == GST_CLOCK_TIME_NONE)
   590     timestamp = segment->last_stop;
   591 
   592   /* add duration */
   593   if (duration != GST_CLOCK_TIME_NONE)
   594     timestamp += duration;
   595 
   596   GST_DEBUG_OBJECT (mq, "queue %d, last_stop updated to %" GST_TIME_FORMAT,
   597       sq->id, GST_TIME_ARGS (timestamp));
   598 
   599   gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
   600 
   601   /* calc diff with other end */
   602   update_time_level (mq, sq);
   603   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   604 }
   605 
   606 static GstFlowReturn
   607 gst_single_queue_push_one (GstMultiQueue * mq, GstSingleQueue * sq,
   608     GstMiniObject * object)
   609 {
   610   GstFlowReturn result = GST_FLOW_OK;
   611 
   612   if (GST_IS_BUFFER (object)) {
   613     GstBuffer *buffer;
   614     GstClockTime timestamp, duration;
   615 
   616     buffer = GST_BUFFER_CAST (object);
   617     timestamp = GST_BUFFER_TIMESTAMP (buffer);
   618     duration = GST_BUFFER_DURATION (buffer);
   619 
   620     apply_buffer (mq, sq, timestamp, duration, &sq->src_segment);
   621 
   622     /* Applying the buffer may have made the queue non-full again, unblock it if needed */
   623     gst_data_queue_limits_changed (sq->queue);
   624 
   625     GST_DEBUG_OBJECT (mq,
   626         "SingleQueue %d : Pushing buffer %p with ts %" GST_TIME_FORMAT,
   627         sq->id, buffer, GST_TIME_ARGS (timestamp));
   628 
   629     result = gst_pad_push (sq->srcpad, buffer);
   630   } else if (GST_IS_EVENT (object)) {
   631     GstEvent *event;
   632 
   633     event = GST_EVENT_CAST (object);
   634 
   635     switch (GST_EVENT_TYPE (event)) {
   636       case GST_EVENT_EOS:
   637         result = GST_FLOW_UNEXPECTED;
   638         break;
   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);
   643         break;
   644       default:
   645         break;
   646     }
   647 
   648     GST_DEBUG_OBJECT (mq,
   649         "SingleQueue %d : Pushing event %p of type %s",
   650         sq->id, event, GST_EVENT_TYPE_NAME (event));
   651 
   652     gst_pad_push_event (sq->srcpad, event);
   653   } else {
   654     g_warning ("Unexpected object in singlequeue %d (refcounting problem?)",
   655         sq->id);
   656   }
   657   return result;
   658 
   659   /* ERRORS */
   660 }
   661 
   662 static GstMiniObject *
   663 gst_multi_queue_item_steal_object (GstMultiQueueItem * item)
   664 {
   665   GstMiniObject *res;
   666 
   667   res = item->object;
   668   item->object = NULL;
   669 
   670   return res;
   671 }
   672 
   673 static void
   674 gst_multi_queue_item_destroy (GstMultiQueueItem * item)
   675 {
   676   if (item->object)
   677     gst_mini_object_unref (item->object);
   678   g_free (item);
   679 }
   680 
   681 /* takes ownership of passed mini object! */
   682 static GstMultiQueueItem *
   683 gst_multi_queue_item_new (GstMiniObject * object, guint32 curid)
   684 {
   685   GstMultiQueueItem *item;
   686 
   687   item = g_new (GstMultiQueueItem, 1);
   688   item->object = object;
   689   item->destroy = (GDestroyNotify) gst_multi_queue_item_destroy;
   690   item->posid = curid;
   691 
   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)
   696       item->duration = 0;
   697     item->visible = TRUE;
   698   } else {
   699     item->size = 0;
   700     item->duration = 0;
   701     item->visible = FALSE;
   702   }
   703   return item;
   704 }
   705 
   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'.
   709  */
   710 static void
   711 gst_multi_queue_loop (GstPad * pad)
   712 {
   713   GstSingleQueue *sq;
   714   GstMultiQueueItem *item;
   715   GstDataQueueItem *sitem;
   716   GstMultiQueue *mq;
   717   GstMiniObject *object;
   718   guint32 newid;
   719   guint32 oldid = -1;
   720   GstFlowReturn result;
   721 
   722   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
   723   mq = sq->mqueue;
   724 
   725   do {
   726     GST_DEBUG_OBJECT (mq, "SingleQueue %d : trying to pop an object", sq->id);
   727 
   728     /* Get something from the queue, blocking until that happens, or we get
   729      * flushed */
   730     if (!(gst_data_queue_pop (sq->queue, &sitem)))
   731       goto out_flushing;
   732 
   733     item = (GstMultiQueueItem *) sitem;
   734     newid = item->posid;
   735 
   736     /* steal the object and destroy the item */
   737     object = gst_multi_queue_item_steal_object (item);
   738     gst_multi_queue_item_destroy (item);
   739 
   740     GST_LOG_OBJECT (mq, "SingleQueue %d : newid:%d , oldid:%d",
   741         sq->id, newid, oldid);
   742 
   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 
   747      * there too */
   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));
   752 
   753       GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   754 
   755       /* Update the nextid so other threads know when to wake us up */
   756       sq->nextid = newid;
   757 
   758       /* Update the oldid (the last ID we output) for highid tracking */
   759       if (oldid != -1)
   760         sq->oldid = oldid;
   761 
   762       if (sq->srcresult == GST_FLOW_NOT_LINKED) {
   763         /* Go to sleep until it's time to push this buffer */
   764 
   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);
   770 
   771 
   772           /* Wake up all non-linked pads before we sleep */
   773           wake_up_next_non_linked (mq);
   774 
   775           mq->numwaiting++;
   776           g_cond_wait (sq->turn, mq->qlock);
   777           mq->numwaiting--;
   778 
   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);
   781         }
   782 
   783         /* Re-compute the high_id in case someone else pushed */
   784         compute_high_id (mq);
   785       } else {
   786         compute_high_id (mq);
   787         /* Wake up all non-linked pads */
   788         wake_up_next_non_linked (mq);
   789       }
   790       /* We're done waiting, we can clear the nextid */
   791       sq->nextid = 0;
   792 
   793       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   794     }
   795 
   796     GST_LOG_OBJECT (mq, "BEFORE PUSHING sq->srcresult: %s",
   797         gst_flow_get_name (sq->srcresult));
   798 
   799     /* Try to push out the new object */
   800     result = gst_single_queue_push_one (mq, sq, object);
   801     sq->srcresult = result;
   802 
   803     if (result != GST_FLOW_OK && result != GST_FLOW_NOT_LINKED)
   804       goto out_flushing;
   805 
   806     GST_LOG_OBJECT (mq, "AFTER PUSHING sq->srcresult: %s",
   807         gst_flow_get_name (sq->srcresult));
   808 
   809     oldid = newid;
   810   }
   811   while (TRUE);
   812 
   813 out_flushing:
   814   {
   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);
   820 
   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));
   826     return;
   827   }
   828 }
   829 
   830 /**
   831  * gst_multi_queue_chain:
   832  *
   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)
   836  */
   837 static GstFlowReturn
   838 gst_multi_queue_chain (GstPad * pad, GstBuffer * buffer)
   839 {
   840   GstSingleQueue *sq;
   841   GstMultiQueue *mq;
   842   GstMultiQueueItem *item;
   843   GstFlowReturn ret = GST_FLOW_OK;
   844   guint32 curid;
   845   GstClockTime timestamp, duration;
   846 
   847   sq = gst_pad_get_element_private (pad);
   848   mq = (GstMultiQueue *) gst_pad_get_parent (pad);
   849 
   850   /* Get a unique incrementing id */
   851   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   852   curid = mq->counter++;
   853   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   854 
   855   GST_LOG_OBJECT (mq, "SingleQueue %d : about to enqueue buffer %p with id %d",
   856       sq->id, buffer, curid);
   857 
   858   item = gst_multi_queue_item_new (GST_MINI_OBJECT_CAST (buffer), curid);
   859 
   860   timestamp = GST_BUFFER_TIMESTAMP (buffer);
   861   duration = GST_BUFFER_DURATION (buffer);
   862 
   863   if (!(gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
   864     goto flushing;
   865 
   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);
   869 
   870 done:
   871   gst_object_unref (mq);
   872 
   873   return ret;
   874 
   875   /* ERRORS */
   876 flushing:
   877   {
   878     ret = sq->srcresult;
   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);
   882     goto done;
   883   }
   884 }
   885 
   886 static gboolean
   887 gst_multi_queue_sink_activate_push (GstPad * pad, gboolean active)
   888 {
   889   GstSingleQueue *sq;
   890 
   891   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
   892 
   893   if (active) {
   894     /* All pads start off not-linked for a smooth kick-off */
   895     sq->srcresult = GST_FLOW_NOT_LINKED;
   896   } else {
   897     sq->srcresult = GST_FLOW_WRONG_STATE;
   898     gst_data_queue_flush (sq->queue);
   899   }
   900   return TRUE;
   901 }
   902 
   903 static gboolean
   904 gst_multi_queue_sink_event (GstPad * pad, GstEvent * event)
   905 {
   906   GstSingleQueue *sq;
   907   GstMultiQueue *mq;
   908   guint32 curid;
   909   GstMultiQueueItem *item;
   910   gboolean res;
   911   GstEventType type;
   912   GstEvent *sref = NULL;
   913 
   914   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
   915   mq = (GstMultiQueue *) gst_pad_get_parent (pad);
   916 
   917   type = GST_EVENT_TYPE (event);
   918 
   919   switch (type) {
   920     case GST_EVENT_FLUSH_START:
   921       GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush start event",
   922           sq->id);
   923 
   924       res = gst_pad_push_event (sq->srcpad, event);
   925 
   926       gst_single_queue_flush (mq, sq, TRUE);
   927       goto done;
   928 
   929     case GST_EVENT_FLUSH_STOP:
   930       GST_DEBUG_OBJECT (mq, "SingleQueue %d : received flush stop event",
   931           sq->id);
   932 
   933       res = gst_pad_push_event (sq->srcpad, event);
   934 
   935       gst_single_queue_flush (mq, sq, FALSE);
   936       goto done;
   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);
   941       break;
   942 
   943     default:
   944       if (!(GST_EVENT_IS_SERIALIZED (event))) {
   945         res = gst_pad_push_event (sq->srcpad, event);
   946         goto done;
   947       }
   948       break;
   949   }
   950 
   951   /* Get an unique incrementing id */
   952   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
   953   curid = mq->counter++;
   954   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
   955 
   956   item = gst_multi_queue_item_new ((GstMiniObject *) event, curid);
   957 
   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);
   961 
   962   if (!(res = gst_data_queue_push (sq->queue, (GstDataQueueItem *) item)))
   963     goto flushing;
   964 
   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
   968    * into dataqueue. */
   969   switch (type) {
   970     case GST_EVENT_EOS:
   971       sq->is_eos = TRUE;
   972       break;
   973     case GST_EVENT_NEWSEGMENT:
   974       apply_segment (mq, sq, sref, &sq->sink_segment);
   975       gst_event_unref (sref);
   976       break;
   977     default:
   978       break;
   979   }
   980 done:
   981   gst_object_unref (mq);
   982   return res;
   983 
   984 flushing:
   985   {
   986     GST_LOG_OBJECT (mq, "SingleQueue %d : exit because task paused, reason: %s",
   987         sq->id, gst_flow_get_name (sq->srcresult));
   988     if (sref)
   989       gst_event_unref (sref);
   990     gst_multi_queue_item_destroy (item);
   991     goto done;
   992   }
   993 }
   994 
   995 static GstCaps *
   996 gst_multi_queue_getcaps (GstPad * pad)
   997 {
   998   GstSingleQueue *sq = gst_pad_get_element_private (pad);
   999   GstPad *otherpad;
  1000   GstCaps *result;
  1001 
  1002   otherpad = (pad == sq->srcpad) ? sq->sinkpad : sq->srcpad;
  1003 
  1004   GST_LOG_OBJECT (otherpad, "Getting caps from the peer of this pad");
  1005 
  1006   result = gst_pad_peer_get_caps (otherpad);
  1007   if (result == NULL)
  1008     result = gst_caps_new_any ();
  1009 
  1010   return result;
  1011 }
  1012 
  1013 static GstFlowReturn
  1014 gst_multi_queue_bufferalloc (GstPad * pad, guint64 offset, guint size,
  1015     GstCaps * caps, GstBuffer ** buf)
  1016 {
  1017   GstSingleQueue *sq = gst_pad_get_element_private (pad);
  1018 
  1019   return gst_pad_alloc_buffer (sq->srcpad, offset, size, caps, buf);
  1020 }
  1021 
  1022 static gboolean
  1023 gst_multi_queue_src_activate_push (GstPad * pad, gboolean active)
  1024 {
  1025   GstMultiQueue *mq;
  1026   GstSingleQueue *sq;
  1027   gboolean result = FALSE;
  1028 
  1029   sq = (GstSingleQueue *) gst_pad_get_element_private (pad);
  1030   mq = sq->mqueue;
  1031 
  1032   GST_DEBUG_OBJECT (mq, "SingleQueue %d", sq->id);
  1033 
  1034   if (active) {
  1035     result = gst_single_queue_flush (mq, sq, FALSE);
  1036   } else {
  1037     result = gst_single_queue_flush (mq, sq, TRUE);
  1038     /* make sure streaming finishes */
  1039     result |= gst_pad_stop_task (pad);
  1040   }
  1041   return result;
  1042 }
  1043 
  1044 static gboolean
  1045 gst_multi_queue_acceptcaps (GstPad * pad, GstCaps * caps)
  1046 {
  1047   return TRUE;
  1048 }
  1049 
  1050 static gboolean
  1051 gst_multi_queue_src_event (GstPad * pad, GstEvent * event)
  1052 {
  1053   GstSingleQueue *sq = gst_pad_get_element_private (pad);
  1054 
  1055   return gst_pad_push_event (sq->sinkpad, event);
  1056 }
  1057 
  1058 static gboolean
  1059 gst_multi_queue_src_query (GstPad * pad, GstQuery * query)
  1060 {
  1061   GstSingleQueue *sq = gst_pad_get_element_private (pad);
  1062   GstPad *peerpad;
  1063   gboolean res;
  1064 
  1065   /* FIXME, Handle position offset depending on queue size */
  1066 
  1067   /* default handling */
  1068   if (!(peerpad = gst_pad_get_peer (sq->sinkpad)))
  1069     goto no_peer;
  1070 
  1071   res = gst_pad_query (peerpad, query);
  1072 
  1073   gst_object_unref (peerpad);
  1074 
  1075   return res;
  1076 
  1077   /* ERRORS */
  1078 no_peer:
  1079   {
  1080     GST_LOG_OBJECT (sq->sinkpad, "Couldn't send query because we have no peer");
  1081     return FALSE;
  1082   }
  1083 }
  1084 
  1085 /*
  1086  * Next-non-linked functions
  1087  */
  1088 
  1089 /* WITH LOCK TAKEN */
  1090 static void
  1091 wake_up_next_non_linked (GstMultiQueue * mq)
  1092 {
  1093   GList *tmp;
  1094 
  1095   /* maybe no-one is waiting */
  1096   if (mq->numwaiting < 1)
  1097     return;
  1098 
  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;
  1102 
  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);
  1107       }
  1108     }
  1109   }
  1110 }
  1111 
  1112 /* WITH LOCK TAKEN */
  1113 static void
  1114 compute_high_id (GstMultiQueue * mq)
  1115 {
  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 */
  1118   GList *tmp;
  1119   guint32 lowest = G_MAXUINT32;
  1120   guint32 highid = G_MAXUINT32;
  1121 
  1122   for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
  1123     GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
  1124 
  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));
  1127 
  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);
  1132         continue;
  1133       }
  1134 
  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))
  1142         highid = sq->oldid;
  1143     }
  1144   }
  1145 
  1146   if (highid == G_MAXUINT32 || lowest < highid)
  1147     mq->highid = lowest;
  1148   else
  1149     mq->highid = highid;
  1150 
  1151   GST_LOG_OBJECT (mq, "Highid is now : %u, lowest non-linked %u", mq->highid,
  1152       lowest);
  1153 }
  1154 
  1155 #define IS_FILLED(format, value) ((sq->max_size.format) != 0 && \
  1156      (sq->max_size.format) <= (value))
  1157 
  1158 /*
  1159  * GstSingleQueue functions
  1160  */
  1161 static void
  1162 single_queue_overrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
  1163 {
  1164   GstMultiQueue *mq = sq->mqueue;
  1165   GList *tmp;
  1166   GstDataQueueSize size;
  1167   gboolean filled = FALSE;
  1168 
  1169   gst_data_queue_get_level (sq->queue, &size);
  1170 
  1171   GST_LOG_OBJECT (mq, "Single Queue %d is full", sq->id);
  1172 
  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;
  1177 
  1178     GST_LOG_OBJECT (mq, "Checking Queue %d", ssq->id);
  1179 
  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);
  1187       }
  1188       GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
  1189       goto beach;
  1190     }
  1191     /* check if we reached the hard time/bytes limits */
  1192     gst_data_queue_get_level (ssq->queue, &ssize);
  1193 
  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);
  1198 
  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);
  1202       filled = TRUE;
  1203     }
  1204   }
  1205   /* no queues were empty */
  1206   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
  1207 
  1208   /* Overrun is always forwarded, since this is blocking the upstream element */
  1209   if (filled) {
  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);
  1212   }
  1213 
  1214 beach:
  1215   return;
  1216 }
  1217 
  1218 static void
  1219 single_queue_underrun_cb (GstDataQueue * dq, GstSingleQueue * sq)
  1220 {
  1221   gboolean empty = TRUE;
  1222   GstMultiQueue *mq = sq->mqueue;
  1223   GList *tmp;
  1224 
  1225   GST_LOG_OBJECT (mq,
  1226       "Single Queue %d is empty, Checking other single queues", sq->id);
  1227 
  1228   GST_MULTI_QUEUE_MUTEX_LOCK (mq);
  1229   for (tmp = mq->queues; tmp; tmp = g_list_next (tmp)) {
  1230     GstSingleQueue *sq = (GstSingleQueue *) tmp->data;
  1231 
  1232     if (gst_data_queue_is_full (sq->queue)) {
  1233       GstDataQueueSize size;
  1234 
  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);
  1242       }
  1243     }
  1244     if (!gst_data_queue_is_empty (sq->queue))
  1245       empty = FALSE;
  1246   }
  1247   GST_MULTI_QUEUE_MUTEX_UNLOCK (mq);
  1248 
  1249   if (empty) {
  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);
  1252   }
  1253 }
  1254 
  1255 static gboolean
  1256 single_queue_check_full (GstDataQueue * dataq, guint visible, guint bytes,
  1257     guint64 time, GstSingleQueue * sq)
  1258 {
  1259   gboolean res;
  1260 
  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);
  1264 
  1265   /* we are always filled on EOS */
  1266   if (sq->is_eos)
  1267     return TRUE;
  1268 
  1269   /* we never go past the max visible items */
  1270   if (IS_FILLED (visible, visible))
  1271     return TRUE;
  1272 
  1273   if (sq->cur_time != 0) {
  1274     /* if we have valid time in the queue, check */
  1275     res = IS_FILLED (time, sq->cur_time);
  1276   } else {
  1277     /* no valid time, check bytes */
  1278     res = IS_FILLED (bytes, bytes);
  1279   }
  1280   return res;
  1281 }
  1282 
  1283 static void
  1284 gst_single_queue_free (GstSingleQueue * sq)
  1285 {
  1286   /* DRAIN QUEUE */
  1287   gst_data_queue_flush (sq->queue);
  1288   g_object_unref (sq->queue);
  1289   g_cond_free (sq->turn);
  1290   g_free (sq);
  1291 }
  1292 
  1293 static GstSingleQueue *
  1294 gst_single_queue_new (GstMultiQueue * mqueue)
  1295 {
  1296   GstSingleQueue *sq;
  1297   gchar *tmp;
  1298 
  1299   sq = g_new0 (GstSingleQueue, 1);
  1300 
  1301   GST_MULTI_QUEUE_MUTEX_LOCK (mqueue);
  1302   sq->id = mqueue->nbqueues++;
  1303 
  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;
  1309 
  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;
  1313 
  1314   GST_MULTI_QUEUE_MUTEX_UNLOCK (mqueue);
  1315 
  1316   GST_DEBUG_OBJECT (mqueue, "Creating GstSingleQueue id:%d", sq->id);
  1317 
  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);
  1322   sq->is_eos = FALSE;
  1323   gst_segment_init (&sq->sink_segment, GST_FORMAT_TIME);
  1324   gst_segment_init (&sq->src_segment, GST_FORMAT_TIME);
  1325 
  1326   sq->nextid = 0;
  1327   sq->oldid = 0;
  1328   sq->turn = g_cond_new ();
  1329 
  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);
  1335 
  1336   tmp = g_strdup_printf ("sink%d", sq->id);
  1337   sq->sinkpad = gst_pad_new_from_static_template (&sinktemplate, tmp);
  1338   g_free (tmp);
  1339 
  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));
  1352 
  1353   tmp = g_strdup_printf ("src%d", sq->id);
  1354   sq->srcpad = gst_pad_new_from_static_template (&srctemplate, tmp);
  1355   g_free (tmp);
  1356 
  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));
  1369 
  1370   gst_pad_set_element_private (sq->sinkpad, (gpointer) sq);
  1371   gst_pad_set_element_private (sq->srcpad, (gpointer) sq);
  1372 
  1373   gst_pad_set_active (sq->srcpad, TRUE);
  1374   gst_element_add_pad (GST_ELEMENT (mqueue), sq->srcpad);
  1375 
  1376   gst_pad_set_active (sq->sinkpad, TRUE);
  1377   gst_element_add_pad (GST_ELEMENT (mqueue), sq->sinkpad);
  1378 
  1379   GST_DEBUG_OBJECT (mqueue, "GstSingleQueue [%d] created and pads added",
  1380       sq->id);
  1381 
  1382   return sq;
  1383 }
  1384 
  1385 static gboolean
  1386 plugin_init (GstPlugin * plugin)
  1387 {
  1388   return gst_element_register (plugin, "multiqueue", GST_RANK_NONE,
  1389       GST_TYPE_MULTI_QUEUE);
  1390 }
  1391 
  1392 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  1393     GST_VERSION_MINOR,
  1394     "multiqueue",
  1395     "multiqueue", plugin_init, VERSION, GST_LICENSE,
  1396     GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
  1397