]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - plugins/elements/gstqueue.c
gststructure: clarify _get docs about the returned reference
[glsdk/gstreamer0-10.git] / plugins / elements / gstqueue.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2003 Colin Walters <cwalters@gnome.org>
5  *                    2005 Wim Taymans <wim@fluendo.com>
6  *
7  * gstqueue.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-queue
27  *
28  * Data is queued until one of the limits specified by the
29  * #GstQueue:max-size-buffers, #GstQueue:max-size-bytes and/or
30  * #GstQueue:max-size-time properties has been reached. Any attempt to push
31  * more buffers into the queue will block the pushing thread until more space
32  * becomes available.
33  *
34  * The queue will create a new thread on the source pad to decouple the
35  * processing on sink and source pad.
36  *
37  * You can query how many buffers are queued by reading the
38  * #GstQueue:current-level-buffers property. You can track changes
39  * by connecting to the notify::current-level-buffers signal (which
40  * like all signals will be emitted from the streaming thread). The same
41  * applies to the #GstQueue:current-level-time and
42  * #GstQueue:current-level-bytes properties.
43  *
44  * The default queue size limits are 200 buffers, 10MB of data, or
45  * one second worth of data, whichever is reached first.
46  *
47  * As said earlier, the queue blocks by default when one of the specified
48  * maximums (bytes, time, buffers) has been reached. You can set the
49  * #GstQueue:leaky property to specify that instead of blocking it should
50  * leak (drop) new or old buffers.
51  *
52  * The #GstQueue::underrun signal is emitted when the queue has less data than
53  * the specified minimum thresholds require (by default: when the queue is
54  * empty). The #GstQueue::overrun signal is emitted when the queue is filled
55  * up. Both signals are emitted from the context of the streaming thread.
56  */
58 #include "gst/gst_private.h"
60 #include <gst/gst.h>
61 #include "gstqueue.h"
63 #include "../../gst/gst-i18n-lib.h"
64 #include "../../gst/glib-compat-private.h"
66 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS_ANY);
71 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS_ANY);
76 GST_DEBUG_CATEGORY_STATIC (queue_debug);
77 #define GST_CAT_DEFAULT (queue_debug)
78 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
80 #define STATUS(queue, pad, msg) \
81   GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
82                       "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
83                       "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
84                       "-%" G_GUINT64_FORMAT " ns, %u items", \
85                       GST_DEBUG_PAD_NAME (pad), \
86                       queue->cur_level.buffers, \
87                       queue->min_threshold.buffers, \
88                       queue->max_size.buffers, \
89                       queue->cur_level.bytes, \
90                       queue->min_threshold.bytes, \
91                       queue->max_size.bytes, \
92                       queue->cur_level.time, \
93                       queue->min_threshold.time, \
94                       queue->max_size.time, \
95                       queue->queue.length)
97 /* Queue signals and args */
98 enum
99 {
100   SIGNAL_UNDERRUN,
101   SIGNAL_RUNNING,
102   SIGNAL_OVERRUN,
103   SIGNAL_PUSHING,
104   LAST_SIGNAL
105 };
107 enum
109   PROP_0,
110   /* FIXME: don't we have another way of doing this
111    * "Gstreamer format" (frame/byte/time) queries? */
112   PROP_CUR_LEVEL_BUFFERS,
113   PROP_CUR_LEVEL_BYTES,
114   PROP_CUR_LEVEL_TIME,
115   PROP_MAX_SIZE_BUFFERS,
116   PROP_MAX_SIZE_BYTES,
117   PROP_MAX_SIZE_TIME,
118   PROP_MIN_THRESHOLD_BUFFERS,
119   PROP_MIN_THRESHOLD_BYTES,
120   PROP_MIN_THRESHOLD_TIME,
121   PROP_LEAKY,
122   PROP_SILENT
123 };
125 /* default property values */
126 #define DEFAULT_MAX_SIZE_BUFFERS  200   /* 200 buffers */
127 #define DEFAULT_MAX_SIZE_BYTES    (10 * 1024 * 1024)    /* 10 MB       */
128 #define DEFAULT_MAX_SIZE_TIME     GST_SECOND    /* 1 second    */
130 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START {                          \
131   g_mutex_lock (q->qlock);                                              \
132 } G_STMT_END
134 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START {              \
135   GST_QUEUE_MUTEX_LOCK (q);                                             \
136   if (q->srcresult != GST_FLOW_OK)                                      \
137     goto label;                                                         \
138 } G_STMT_END
140 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START {                        \
141   g_mutex_unlock (q->qlock);                                            \
142 } G_STMT_END
144 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START {               \
145   STATUS (q, q->sinkpad, "wait for DEL");                               \
146   q->waiting_del = TRUE;                                                \
147   g_cond_wait (q->item_del, q->qlock);                                  \
148   q->waiting_del = FALSE;                                               \
149   if (q->srcresult != GST_FLOW_OK) {                                    \
150     STATUS (q, q->srcpad, "received DEL wakeup");                       \
151     goto label;                                                         \
152   }                                                                     \
153   STATUS (q, q->sinkpad, "received DEL");                               \
154 } G_STMT_END
156 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START {               \
157   STATUS (q, q->srcpad, "wait for ADD");                                \
158   q->waiting_add = TRUE;                                                \
159   g_cond_wait (q->item_add, q->qlock);                                  \
160   q->waiting_add = FALSE;                                               \
161   if (q->srcresult != GST_FLOW_OK) {                                    \
162     STATUS (q, q->srcpad, "received ADD wakeup");                       \
163     goto label;                                                         \
164   }                                                                     \
165   STATUS (q, q->srcpad, "received ADD");                                \
166 } G_STMT_END
168 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START {                          \
169   if (q->waiting_del) {                                                 \
170     STATUS (q, q->srcpad, "signal DEL");                                \
171     g_cond_signal (q->item_del);                                        \
172   }                                                                     \
173 } G_STMT_END
175 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START {                          \
176   if (q->waiting_add) {                                                 \
177     STATUS (q, q->sinkpad, "signal ADD");                               \
178     g_cond_signal (q->item_add);                                        \
179   }                                                                     \
180 } G_STMT_END
182 #define _do_init(bla) \
183     GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
184     GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
185         "dataflow inside the queue element");
187 GST_BOILERPLATE_FULL (GstQueue, gst_queue, GstElement,
188     GST_TYPE_ELEMENT, _do_init);
190 static void gst_queue_finalize (GObject * object);
192 static void gst_queue_set_property (GObject * object,
193     guint prop_id, const GValue * value, GParamSpec * pspec);
194 static void gst_queue_get_property (GObject * object,
195     guint prop_id, GValue * value, GParamSpec * pspec);
197 static GstFlowReturn gst_queue_chain (GstPad * pad, GstBuffer * buffer);
198 static GstFlowReturn gst_queue_bufferalloc (GstPad * pad, guint64 offset,
199     guint size, GstCaps * caps, GstBuffer ** buf);
200 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
201 static void gst_queue_loop (GstPad * pad);
203 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstEvent * event);
205 static gboolean gst_queue_handle_src_event (GstPad * pad, GstEvent * event);
206 static gboolean gst_queue_handle_src_query (GstPad * pad, GstQuery * query);
208 static gboolean gst_queue_acceptcaps (GstPad * pad, GstCaps * caps);
209 static GstCaps *gst_queue_getcaps (GstPad * pad);
210 static GstPadLinkReturn gst_queue_link_sink (GstPad * pad, GstPad * peer);
211 static GstPadLinkReturn gst_queue_link_src (GstPad * pad, GstPad * peer);
212 static void gst_queue_locked_flush (GstQueue * queue);
214 static gboolean gst_queue_src_activate_push (GstPad * pad, gboolean active);
215 static gboolean gst_queue_sink_activate_push (GstPad * pad, gboolean active);
217 static gboolean gst_queue_is_empty (GstQueue * queue);
218 static gboolean gst_queue_is_filled (GstQueue * queue);
220 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
222 static GType
223 queue_leaky_get_type (void)
225   static GType queue_leaky_type = 0;
226   static const GEnumValue queue_leaky[] = {
227     {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
228     {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
229     {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
230         "downstream"},
231     {0, NULL, NULL},
232   };
234   if (!queue_leaky_type) {
235     queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
236   }
237   return queue_leaky_type;
240 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
242 static void
243 gst_queue_base_init (gpointer g_class)
245   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
247   gst_element_class_set_details_simple (gstelement_class,
248       "Queue",
249       "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
250   gst_element_class_add_pad_template (gstelement_class,
251       gst_static_pad_template_get (&srctemplate));
252   gst_element_class_add_pad_template (gstelement_class,
253       gst_static_pad_template_get (&sinktemplate));
256 static void
257 gst_queue_class_init (GstQueueClass * klass)
259   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
261   gobject_class->set_property = gst_queue_set_property;
262   gobject_class->get_property = gst_queue_get_property;
264   /* signals */
265   /**
266    * GstQueue::underrun:
267    * @queue: the queue instance
268    *
269    * Reports that the buffer became empty (underrun).
270    * A buffer is empty if the total amount of data inside it (num-buffers, time,
271    * size) is lower than the boundary values which can be set through the
272    * GObject properties.
273    */
274   gst_queue_signals[SIGNAL_UNDERRUN] =
275       g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
276       G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
277       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
278   /**
279    * GstQueue::running:
280    * @queue: the queue instance
281    *
282    * Reports that enough (min-threshold) data is in the queue. Use this signal
283    * together with the underrun signal to pause the pipeline on underrun and
284    * wait for the queue to fill-up before resume playback.
285    */
286   gst_queue_signals[SIGNAL_RUNNING] =
287       g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
288       G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
289       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
290   /**
291    * GstQueue::overrun:
292    * @queue: the queue instance
293    *
294    * Reports that the buffer became full (overrun).
295    * A buffer is full if the total amount of data inside it (num-buffers, time,
296    * size) is higher than the boundary values which can be set through the
297    * GObject properties.
298    */
299   gst_queue_signals[SIGNAL_OVERRUN] =
300       g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
301       G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
302       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
303   /**
304    * GstQueue::pushing:
305    * @queue: the queue instance
306    *
307    * Reports when the queue has enough data to start pushing data again on the
308    * source pad.
309    */
310   gst_queue_signals[SIGNAL_PUSHING] =
311       g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
312       G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
313       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
315   /* properties */
316   g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
317       g_param_spec_uint ("current-level-bytes", "Current level (kB)",
318           "Current amount of data in the queue (bytes)",
319           0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320   g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
321       g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
322           "Current number of buffers in the queue",
323           0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
324   g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
325       g_param_spec_uint64 ("current-level-time", "Current level (ns)",
326           "Current amount of data in the queue (in ns)",
327           0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
329   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
330       g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
331           "Max. amount of data in the queue (bytes, 0=disable)",
332           0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
333           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
335       g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
336           "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
337           DEFAULT_MAX_SIZE_BUFFERS,
338           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339   g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
340       g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
341           "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
342           DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344   g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
345       g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
346           "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
347           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348   g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
349       g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
350           "Min. number of buffers in the queue to allow reading (0=disable)",
351           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352   g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
353       g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
354           "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
355           0, G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357   g_object_class_install_property (gobject_class, PROP_LEAKY,
358       g_param_spec_enum ("leaky", "Leaky",
359           "Where the queue leaks, if at all",
360           GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
361           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363   /**
364    * GstQueue:silent
365    *
366    * Don't emit queue signals. Makes queues more lightweight if no signals are
367    * needed.
368    *
369    * Since: 0.10.31
370    */
371   g_object_class_install_property (gobject_class, PROP_SILENT,
372       g_param_spec_boolean ("silent", "Silent",
373           "Don't emit queue signals", FALSE,
374           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376   gobject_class->finalize = gst_queue_finalize;
378   /* Registering debug symbols for function pointers */
379   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
380   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_sink_activate_push);
381   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
382   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_link_sink);
383   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_getcaps);
384   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_acceptcaps);
385   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_bufferalloc);
386   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_push);
387   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_link_src);
388   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
389   GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
392 static void
393 gst_queue_init (GstQueue * queue, GstQueueClass * g_class)
395   queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
397   gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
398   gst_pad_set_activatepush_function (queue->sinkpad,
399       gst_queue_sink_activate_push);
400   gst_pad_set_event_function (queue->sinkpad, gst_queue_handle_sink_event);
401   gst_pad_set_link_function (queue->sinkpad, gst_queue_link_sink);
402   gst_pad_set_getcaps_function (queue->sinkpad, gst_queue_getcaps);
403   gst_pad_set_acceptcaps_function (queue->sinkpad, gst_queue_acceptcaps);
404   gst_pad_set_bufferalloc_function (queue->sinkpad, gst_queue_bufferalloc);
405   gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
407   queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
409   gst_pad_set_activatepush_function (queue->srcpad,
410       gst_queue_src_activate_push);
411   gst_pad_set_link_function (queue->srcpad, gst_queue_link_src);
412   gst_pad_set_acceptcaps_function (queue->srcpad, gst_queue_acceptcaps);
413   gst_pad_set_getcaps_function (queue->srcpad, gst_queue_getcaps);
414   gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
415   gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
416   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
418   GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
419   queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
420   queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
421   queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
422   GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
423   GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
424   gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
425   gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
426   queue->head_needs_discont = queue->tail_needs_discont = FALSE;
428   queue->leaky = GST_QUEUE_NO_LEAK;
429   queue->srcresult = GST_FLOW_WRONG_STATE;
431   queue->qlock = g_mutex_new ();
432   queue->item_add = g_cond_new ();
433   queue->item_del = g_cond_new ();
435   g_queue_init (&queue->queue);
437   queue->sinktime = GST_CLOCK_TIME_NONE;
438   queue->srctime = GST_CLOCK_TIME_NONE;
440   queue->sink_tainted = TRUE;
441   queue->src_tainted = TRUE;
443   queue->newseg_applied_to_src = FALSE;
445   GST_DEBUG_OBJECT (queue,
446       "initialized queue's not_empty & not_full conditions");
449 /* called only once, as opposed to dispose */
450 static void
451 gst_queue_finalize (GObject * object)
453   GstMiniObject *data;
454   GstQueue *queue = GST_QUEUE (object);
456   GST_DEBUG_OBJECT (queue, "finalizing queue");
458   while ((data = g_queue_pop_head (&queue->queue)))
459     gst_mini_object_unref (data);
461   g_queue_clear (&queue->queue);
462   g_mutex_free (queue->qlock);
463   g_cond_free (queue->item_add);
464   g_cond_free (queue->item_del);
466   G_OBJECT_CLASS (parent_class)->finalize (object);
469 static gboolean
470 gst_queue_acceptcaps (GstPad * pad, GstCaps * caps)
472   gboolean result;
473   GstQueue *queue;
474   GstPad *otherpad;
476   queue = GST_QUEUE (gst_pad_get_parent (pad));
477   if (G_UNLIKELY (queue == NULL))
478     return FALSE;
480   otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
481   result = gst_pad_peer_accept_caps (otherpad, caps);
483   gst_object_unref (queue);
485   return result;
488 static GstCaps *
489 gst_queue_getcaps (GstPad * pad)
491   GstQueue *queue;
492   GstPad *otherpad;
493   GstCaps *result;
495   queue = GST_QUEUE (gst_pad_get_parent (pad));
496   if (G_UNLIKELY (queue == NULL))
497     return gst_caps_new_any ();
499   otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
500   result = gst_pad_peer_get_caps (otherpad);
501   if (result == NULL)
502     result = gst_caps_new_any ();
504   gst_object_unref (queue);
506   return result;
509 static GstPadLinkReturn
510 gst_queue_link_sink (GstPad * pad, GstPad * peer)
512   return GST_PAD_LINK_OK;
515 static GstPadLinkReturn
516 gst_queue_link_src (GstPad * pad, GstPad * peer)
518   GstPadLinkReturn result = GST_PAD_LINK_OK;
519   GstQueue *queue;
521   queue = GST_QUEUE (gst_pad_get_parent (pad));
523   GST_DEBUG_OBJECT (queue, "queue linking source pad");
525   if (GST_PAD_LINKFUNC (peer)) {
526     result = GST_PAD_LINKFUNC (peer) (peer, pad);
527   }
529   if (GST_PAD_LINK_SUCCESSFUL (result)) {
530     GST_QUEUE_MUTEX_LOCK (queue);
531     if (queue->srcresult == GST_FLOW_OK) {
532       queue->push_newsegment = TRUE;
533       gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
534       GST_DEBUG_OBJECT (queue, "starting task as pad is linked");
535     } else {
536       GST_DEBUG_OBJECT (queue, "not starting task reason %s",
537           gst_flow_get_name (queue->srcresult));
538     }
539     GST_QUEUE_MUTEX_UNLOCK (queue);
540   }
541   gst_object_unref (queue);
543   return result;
546 static GstFlowReturn
547 gst_queue_bufferalloc (GstPad * pad, guint64 offset, guint size, GstCaps * caps,
548     GstBuffer ** buf)
550   GstQueue *queue;
551   GstFlowReturn result;
553   queue = GST_QUEUE (gst_pad_get_parent (pad));
554   if (G_UNLIKELY (queue == NULL))
555     return GST_FLOW_WRONG_STATE;
557   /* Forward to src pad, without setting caps on the src pad */
558   result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
560   gst_object_unref (queue);
561   return result;
564 /* calculate the diff between running time on the sink and src of the queue.
565  * This is the total amount of time in the queue. */
566 static void
567 update_time_level (GstQueue * queue)
569   gint64 sink_time, src_time;
571   if (queue->sink_tainted) {
572     queue->sinktime =
573         gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
574         queue->sink_segment.last_stop);
575     queue->sink_tainted = FALSE;
576   }
577   sink_time = queue->sinktime;
579   if (queue->src_tainted) {
580     queue->srctime =
581         gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
582         queue->src_segment.last_stop);
583     queue->src_tainted = FALSE;
584   }
585   src_time = queue->srctime;
587   GST_LOG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
588       GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
590   if (sink_time >= src_time)
591     queue->cur_level.time = sink_time - src_time;
592   else
593     queue->cur_level.time = 0;
596 /* take a NEWSEGMENT event and apply the values to segment, updating the time
597  * level of queue. */
598 static void
599 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
600     gboolean sink)
602   gboolean update;
603   GstFormat format;
604   gdouble rate, arate;
605   gint64 start, stop, time;
607   gst_event_parse_new_segment_full (event, &update, &rate, &arate,
608       &format, &start, &stop, &time);
610   /* now configure the values, we use these to track timestamps on the
611    * sinkpad. */
612   if (format != GST_FORMAT_TIME) {
613     /* non-time format, pretent the current time segment is closed with a
614      * 0 start and unknown stop time. */
615     update = FALSE;
616     format = GST_FORMAT_TIME;
617     start = 0;
618     stop = -1;
619     time = 0;
620   }
621   gst_segment_set_newsegment_full (segment, update,
622       rate, arate, format, start, stop, time);
624   if (sink)
625     queue->sink_tainted = TRUE;
626   else
627     queue->src_tainted = TRUE;
629   GST_DEBUG_OBJECT (queue,
630       "configured NEWSEGMENT %" GST_SEGMENT_FORMAT, segment);
632   /* segment can update the time level of the queue */
633   update_time_level (queue);
636 /* take a buffer and update segment, updating the time level of the queue. */
637 static void
638 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
639     gboolean with_duration, gboolean sink)
641   GstClockTime duration, timestamp;
643   timestamp = GST_BUFFER_TIMESTAMP (buffer);
644   duration = GST_BUFFER_DURATION (buffer);
646   /* if no timestamp is set, assume it's continuous with the previous
647    * time */
648   if (timestamp == GST_CLOCK_TIME_NONE)
649     timestamp = segment->last_stop;
651   /* add duration */
652   if (with_duration && duration != GST_CLOCK_TIME_NONE)
653     timestamp += duration;
655   GST_LOG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
656       GST_TIME_ARGS (timestamp));
658   gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
659   if (sink)
660     queue->sink_tainted = TRUE;
661   else
662     queue->src_tainted = TRUE;
665   /* calc diff with other end */
666   update_time_level (queue);
669 static void
670 gst_queue_locked_flush (GstQueue * queue)
672   GstMiniObject *data;
674   while ((data = g_queue_pop_head (&queue->queue))) {
675     /* Then lose another reference because we are supposed to destroy that
676        data when flushing */
677     gst_mini_object_unref (data);
678   }
679   GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
680   queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
681   queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
682   queue->min_threshold.time = queue->orig_min_threshold.time;
683   gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
684   gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
685   queue->head_needs_discont = queue->tail_needs_discont = FALSE;
687   queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
688   queue->sink_tainted = queue->src_tainted = TRUE;
690   /* we deleted a lot of something */
691   GST_QUEUE_SIGNAL_DEL (queue);
694 /* enqueue an item an update the level stats, with QUEUE_LOCK */
695 static inline void
696 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
698   GstBuffer *buffer = GST_BUFFER_CAST (item);
700   /* add buffer to the statistics */
701   queue->cur_level.buffers++;
702   queue->cur_level.bytes += GST_BUFFER_SIZE (buffer);
703   apply_buffer (queue, buffer, &queue->sink_segment, TRUE, TRUE);
705   g_queue_push_tail (&queue->queue, item);
706   GST_QUEUE_SIGNAL_ADD (queue);
709 static inline void
710 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
712   GstEvent *event = GST_EVENT_CAST (item);
714   switch (GST_EVENT_TYPE (event)) {
715     case GST_EVENT_EOS:
716       /* Zero the thresholds, this makes sure the queue is completely
717        * filled and we can read all data from the queue. */
718       GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
719       /* mark the queue as EOS. This prevents us from accepting more data. */
720       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
721       queue->eos = TRUE;
722       break;
723     case GST_EVENT_NEWSEGMENT:
724       apply_segment (queue, event, &queue->sink_segment, TRUE);
725       /* if the queue is empty, apply sink segment on the source */
726       if (queue->queue.length == 0) {
727         GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
728         apply_segment (queue, event, &queue->src_segment, FALSE);
729         queue->newseg_applied_to_src = TRUE;
730       }
731       /* a new segment allows us to accept more buffers if we got UNEXPECTED
732        * from downstream */
733       queue->unexpected = FALSE;
734       break;
735     default:
736       break;
737   }
739   g_queue_push_tail (&queue->queue, item);
740   GST_QUEUE_SIGNAL_ADD (queue);
743 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
744 static GstMiniObject *
745 gst_queue_locked_dequeue (GstQueue * queue, gboolean * is_buffer)
747   GstMiniObject *item;
749   item = g_queue_pop_head (&queue->queue);
750   if (item == NULL)
751     goto no_item;
753   if (GST_IS_BUFFER (item)) {
754     GstBuffer *buffer = GST_BUFFER_CAST (item);
756     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
757         "retrieved buffer %p from queue", buffer);
759     queue->cur_level.buffers--;
760     queue->cur_level.bytes -= GST_BUFFER_SIZE (buffer);
761     apply_buffer (queue, buffer, &queue->src_segment, TRUE, FALSE);
763     /* if the queue is empty now, update the other side */
764     if (queue->cur_level.buffers == 0)
765       queue->cur_level.time = 0;
767     *is_buffer = TRUE;
768   } else if (GST_IS_EVENT (item)) {
769     GstEvent *event = GST_EVENT_CAST (item);
771     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
772         "retrieved event %p from queue", event);
774     switch (GST_EVENT_TYPE (event)) {
775       case GST_EVENT_EOS:
776         /* queue is empty now that we dequeued the EOS */
777         GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
778         break;
779       case GST_EVENT_NEWSEGMENT:
780         /* apply newsegment if it has not already been applied */
781         if (G_LIKELY (!queue->newseg_applied_to_src)) {
782           apply_segment (queue, event, &queue->src_segment, FALSE);
783         } else {
784           queue->newseg_applied_to_src = FALSE;
785         }
786         break;
787       default:
788         break;
789     }
791     *is_buffer = FALSE;
792   } else {
793     g_warning
794         ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
795         item, GST_OBJECT_NAME (queue));
796     item = NULL;
797   }
798   GST_QUEUE_SIGNAL_DEL (queue);
800   return item;
802   /* ERRORS */
803 no_item:
804   {
805     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
806     return NULL;
807   }
810 static gboolean
811 gst_queue_handle_sink_event (GstPad * pad, GstEvent * event)
813   GstQueue *queue;
815   queue = GST_QUEUE (gst_pad_get_parent (pad));
816   if (G_UNLIKELY (queue == NULL)) {
817     gst_event_unref (event);
818     return FALSE;
819   }
821   switch (GST_EVENT_TYPE (event)) {
822     case GST_EVENT_FLUSH_START:
823     {
824       STATUS (queue, pad, "received flush start event");
825       /* forward event */
826       gst_pad_push_event (queue->srcpad, event);
828       /* now unblock the chain function */
829       GST_QUEUE_MUTEX_LOCK (queue);
830       queue->srcresult = GST_FLOW_WRONG_STATE;
831       /* unblock the loop and chain functions */
832       GST_QUEUE_SIGNAL_ADD (queue);
833       GST_QUEUE_SIGNAL_DEL (queue);
834       GST_QUEUE_MUTEX_UNLOCK (queue);
836       /* make sure it pauses, this should happen since we sent
837        * flush_start downstream. */
838       gst_pad_pause_task (queue->srcpad);
839       GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
840       goto done;
841     }
842     case GST_EVENT_FLUSH_STOP:
843     {
844       STATUS (queue, pad, "received flush stop event");
845       /* forward event */
846       gst_pad_push_event (queue->srcpad, event);
848       GST_QUEUE_MUTEX_LOCK (queue);
849       gst_queue_locked_flush (queue);
850       queue->srcresult = GST_FLOW_OK;
851       queue->eos = FALSE;
852       queue->unexpected = FALSE;
853       if (gst_pad_is_linked (queue->srcpad)) {
854         gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
855             queue->srcpad);
856       } else {
857         GST_INFO_OBJECT (queue, "not re-starting task as pad is not linked");
858       }
859       GST_QUEUE_MUTEX_UNLOCK (queue);
861       STATUS (queue, pad, "after flush");
862       goto done;
863     }
864     default:
865       if (GST_EVENT_IS_SERIALIZED (event)) {
866         /* serialized events go in the queue */
867         GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
868         /* refuse more events on EOS */
869         if (queue->eos)
870           goto out_eos;
871         gst_queue_locked_enqueue_event (queue, event);
872         GST_QUEUE_MUTEX_UNLOCK (queue);
873       } else {
874         /* non-serialized events are passed upstream. */
875         gst_pad_push_event (queue->srcpad, event);
876       }
877       break;
878   }
879 done:
880   gst_object_unref (queue);
881   return TRUE;
883   /* ERRORS */
884 out_flushing:
885   {
886     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
887         "refusing event, we are flushing");
888     GST_QUEUE_MUTEX_UNLOCK (queue);
889     gst_object_unref (queue);
890     gst_event_unref (event);
891     return FALSE;
892   }
893 out_eos:
894   {
895     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
896     GST_QUEUE_MUTEX_UNLOCK (queue);
897     gst_object_unref (queue);
898     gst_event_unref (event);
899     return FALSE;
900   }
903 static gboolean
904 gst_queue_is_empty (GstQueue * queue)
906   if (queue->queue.length == 0)
907     return TRUE;
909   /* It is possible that a max size is reached before all min thresholds are.
910    * Therefore, only consider it empty if it is not filled. */
911   return ((queue->min_threshold.buffers > 0 &&
912           queue->cur_level.buffers < queue->min_threshold.buffers) ||
913       (queue->min_threshold.bytes > 0 &&
914           queue->cur_level.bytes < queue->min_threshold.bytes) ||
915       (queue->min_threshold.time > 0 &&
916           queue->cur_level.time < queue->min_threshold.time)) &&
917       !gst_queue_is_filled (queue);
920 static gboolean
921 gst_queue_is_filled (GstQueue * queue)
923   return (((queue->max_size.buffers > 0 &&
924               queue->cur_level.buffers >= queue->max_size.buffers) ||
925           (queue->max_size.bytes > 0 &&
926               queue->cur_level.bytes >= queue->max_size.bytes) ||
927           (queue->max_size.time > 0 &&
928               queue->cur_level.time >= queue->max_size.time)));
931 static void
932 gst_queue_leak_downstream (GstQueue * queue)
934   /* for as long as the queue is filled, dequeue an item and discard it */
935   while (gst_queue_is_filled (queue)) {
936     GstMiniObject *leak;
937     gboolean is_buffer;
939     leak = gst_queue_locked_dequeue (queue, &is_buffer);
940     /* there is nothing to dequeue and the queue is still filled.. This should
941      * not happen */
942     g_assert (leak != NULL);
944     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
945         "queue is full, leaking item %p on downstream end", leak);
946     gst_mini_object_unref (leak);
948     /* last buffer needs to get a DISCONT flag */
949     queue->head_needs_discont = TRUE;
950   }
953 static GstFlowReturn
954 gst_queue_chain (GstPad * pad, GstBuffer * buffer)
956   GstQueue *queue;
957   GstClockTime duration, timestamp;
959   queue = (GstQueue *) GST_OBJECT_PARENT (pad);
961   /* we have to lock the queue since we span threads */
962   GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
963   /* when we received EOS, we refuse any more data */
964   if (queue->eos)
965     goto out_eos;
966   if (queue->unexpected)
967     goto out_unexpected;
969   timestamp = GST_BUFFER_TIMESTAMP (buffer);
970   duration = GST_BUFFER_DURATION (buffer);
972   GST_CAT_LOG_OBJECT (queue_dataflow, queue,
973       "received buffer %p of size %d, time %" GST_TIME_FORMAT ", duration %"
974       GST_TIME_FORMAT, buffer, GST_BUFFER_SIZE (buffer),
975       GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
977   /* We make space available if we're "full" according to whatever
978    * the user defined as "full". Note that this only applies to buffers.
979    * We always handle events and they don't count in our statistics. */
980   while (gst_queue_is_filled (queue)) {
981     if (!queue->silent) {
982       GST_QUEUE_MUTEX_UNLOCK (queue);
983       g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
984       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
985       /* we recheck, the signal could have changed the thresholds */
986       if (!gst_queue_is_filled (queue))
987         break;
988     }
990     /* how are we going to make space for this buffer? */
991     switch (queue->leaky) {
992       case GST_QUEUE_LEAK_UPSTREAM:
993         /* next buffer needs to get a DISCONT flag */
994         queue->tail_needs_discont = TRUE;
995         /* leak current buffer */
996         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
997             "queue is full, leaking buffer on upstream end");
998         /* now we can clean up and exit right away */
999         goto out_unref;
1000       case GST_QUEUE_LEAK_DOWNSTREAM:
1001         gst_queue_leak_downstream (queue);
1002         break;
1003       default:
1004         g_warning ("Unknown leaky type, using default");
1005         /* fall-through */
1006       case GST_QUEUE_NO_LEAK:
1007       {
1008         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1009             "queue is full, waiting for free space");
1011         /* don't leak. Instead, wait for space to be available */
1012         do {
1013           /* for as long as the queue is filled, wait till an item was deleted. */
1014           GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1015         } while (gst_queue_is_filled (queue));
1017         GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1019         if (!queue->silent) {
1020           GST_QUEUE_MUTEX_UNLOCK (queue);
1021           g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1022           GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1023         }
1024         break;
1025       }
1026     }
1027   }
1029   if (queue->tail_needs_discont) {
1030     GstBuffer *subbuffer = gst_buffer_make_metadata_writable (buffer);
1032     if (subbuffer) {
1033       buffer = subbuffer;
1034       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1035     } else {
1036       GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1037     }
1038     queue->tail_needs_discont = FALSE;
1039   }
1041   /* put buffer in queue now */
1042   gst_queue_locked_enqueue_buffer (queue, buffer);
1043   GST_QUEUE_MUTEX_UNLOCK (queue);
1045   return GST_FLOW_OK;
1047   /* special conditions */
1048 out_unref:
1049   {
1050     GST_QUEUE_MUTEX_UNLOCK (queue);
1052     gst_buffer_unref (buffer);
1054     return GST_FLOW_OK;
1055   }
1056 out_flushing:
1057   {
1058     GstFlowReturn ret = queue->srcresult;
1060     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1061         "exit because task paused, reason: %s", gst_flow_get_name (ret));
1062     GST_QUEUE_MUTEX_UNLOCK (queue);
1063     gst_buffer_unref (buffer);
1065     return ret;
1066   }
1067 out_eos:
1068   {
1069     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1070     GST_QUEUE_MUTEX_UNLOCK (queue);
1072     gst_buffer_unref (buffer);
1074     return GST_FLOW_UNEXPECTED;
1075   }
1076 out_unexpected:
1077   {
1078     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1079         "exit because we received UNEXPECTED");
1080     GST_QUEUE_MUTEX_UNLOCK (queue);
1082     gst_buffer_unref (buffer);
1084     return GST_FLOW_UNEXPECTED;
1085   }
1088 static void
1089 gst_queue_push_newsegment (GstQueue * queue)
1091   GstSegment *s;
1092   GstEvent *event;
1094   s = &queue->src_segment;
1096   if (s->accum != 0) {
1097     event = gst_event_new_new_segment_full (FALSE, 1.0, 1.0, s->format, 0,
1098         s->accum, 0);
1099     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1100         "pushing accum newsegment event");
1101     gst_pad_push_event (queue->srcpad, event);
1102   }
1104   event = gst_event_new_new_segment_full (FALSE, s->rate, s->applied_rate,
1105       s->format, s->start, s->stop, s->time);
1106   GST_CAT_LOG_OBJECT (queue_dataflow, queue, "pushing real newsegment event");
1107   gst_pad_push_event (queue->srcpad, event);
1110 /* dequeue an item from the queue an push it downstream. This functions returns
1111  * the result of the push. */
1112 static GstFlowReturn
1113 gst_queue_push_one (GstQueue * queue)
1115   GstFlowReturn result = GST_FLOW_OK;
1116   GstMiniObject *data;
1117   gboolean is_buffer;
1119   data = gst_queue_locked_dequeue (queue, &is_buffer);
1120   if (data == NULL)
1121     goto no_item;
1123 next:
1124   if (is_buffer) {
1125     GstBuffer *buffer;
1126     GstCaps *caps;
1128     buffer = GST_BUFFER_CAST (data);
1130     if (queue->head_needs_discont) {
1131       GstBuffer *subbuffer = gst_buffer_make_metadata_writable (buffer);
1133       if (subbuffer) {
1134         buffer = subbuffer;
1135         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1136       } else {
1137         GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1138       }
1139       queue->head_needs_discont = FALSE;
1140     }
1142     caps = GST_BUFFER_CAPS (buffer);
1144     GST_QUEUE_MUTEX_UNLOCK (queue);
1145     /* set the right caps on the pad now. We do this before pushing the buffer
1146      * because the pad_push call will check (using acceptcaps) if the buffer can
1147      * be set on the pad, which might fail because this will be propagated
1148      * upstream. Also note that if the buffer has NULL caps, it means that the
1149      * caps did not change, so we don't have to change caps on the pad. */
1150     if (caps && caps != GST_PAD_CAPS (queue->srcpad))
1151       gst_pad_set_caps (queue->srcpad, caps);
1153     if (queue->push_newsegment) {
1154       gst_queue_push_newsegment (queue);
1155     }
1156     result = gst_pad_push (queue->srcpad, buffer);
1158     /* need to check for srcresult here as well */
1159     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1161     if (result == GST_FLOW_UNEXPECTED) {
1162       GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1163           "got UNEXPECTED from downstream");
1164       /* stop pushing buffers, we dequeue all items until we see an item that we
1165        * can push again, which is EOS or NEWSEGMENT. If there is nothing in the
1166        * queue we can push, we set a flag to make the sinkpad refuse more
1167        * buffers with an UNEXPECTED return value. */
1168       while ((data = gst_queue_locked_dequeue (queue, &is_buffer))) {
1169         if (is_buffer) {
1170           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1171               "dropping UNEXPECTED buffer %p", data);
1172           gst_buffer_unref (GST_BUFFER_CAST (data));
1173         } else {
1174           GstEvent *event = GST_EVENT_CAST (data);
1175           GstEventType type = GST_EVENT_TYPE (event);
1177           if (type == GST_EVENT_EOS || type == GST_EVENT_NEWSEGMENT) {
1178             /* we found a pushable item in the queue, push it out */
1179             GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1180                 "pushing pushable event %s after UNEXPECTED",
1181                 GST_EVENT_TYPE_NAME (event));
1182             goto next;
1183           }
1184           GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1185               "dropping UNEXPECTED event %p", event);
1186           gst_event_unref (event);
1187         }
1188       }
1189       /* no more items in the queue. Set the unexpected flag so that upstream
1190        * make us refuse any more buffers on the sinkpad. Since we will still
1191        * accept EOS and NEWSEGMENT we return _FLOW_OK to the caller so that the
1192        * task function does not shut down. */
1193       queue->unexpected = TRUE;
1194       result = GST_FLOW_OK;
1195     }
1196   } else {
1197     GstEvent *event = GST_EVENT_CAST (data);
1198     GstEventType type = GST_EVENT_TYPE (event);
1200     GST_QUEUE_MUTEX_UNLOCK (queue);
1202     if (queue->push_newsegment && type != GST_EVENT_NEWSEGMENT) {
1203       gst_queue_push_newsegment (queue);
1204     }
1205     gst_pad_push_event (queue->srcpad, event);
1207     GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1208     /* if we're EOS, return UNEXPECTED so that the task pauses. */
1209     if (type == GST_EVENT_EOS) {
1210       GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1211           "pushed EOS event %p, return UNEXPECTED", event);
1212       result = GST_FLOW_UNEXPECTED;
1213     }
1214   }
1215   return result;
1217   /* ERRORS */
1218 no_item:
1219   {
1220     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1221         "exit because we have no item in the queue");
1222     return GST_FLOW_ERROR;
1223   }
1224 out_flushing:
1225   {
1226     GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1227     return GST_FLOW_WRONG_STATE;
1228   }
1231 static void
1232 gst_queue_loop (GstPad * pad)
1234   GstQueue *queue;
1235   GstFlowReturn ret;
1237   queue = (GstQueue *) GST_PAD_PARENT (pad);
1239   /* have to lock for thread-safety */
1240   GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1242   while (gst_queue_is_empty (queue)) {
1243     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1244     if (!queue->silent) {
1245       GST_QUEUE_MUTEX_UNLOCK (queue);
1246       g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1247       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1248     }
1250     /* we recheck, the signal could have changed the thresholds */
1251     while (gst_queue_is_empty (queue)) {
1252       GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1253     }
1255     GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1256     if (!queue->silent) {
1257       GST_QUEUE_MUTEX_UNLOCK (queue);
1258       g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1259       g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1260       GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1261     }
1262   }
1264   ret = gst_queue_push_one (queue);
1265   queue->push_newsegment = FALSE;
1266   queue->srcresult = ret;
1267   if (ret != GST_FLOW_OK)
1268     goto out_flushing;
1270   GST_QUEUE_MUTEX_UNLOCK (queue);
1272   return;
1274   /* ERRORS */
1275 out_flushing:
1276   {
1277     gboolean eos = queue->eos;
1278     GstFlowReturn ret = queue->srcresult;
1280     gst_pad_pause_task (queue->srcpad);
1281     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1282         "pause task, reason:  %s", gst_flow_get_name (ret));
1283     GST_QUEUE_SIGNAL_DEL (queue);
1284     GST_QUEUE_MUTEX_UNLOCK (queue);
1285     /* let app know about us giving up if upstream is not expected to do so */
1286     /* UNEXPECTED is already taken care of elsewhere */
1287     if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED)) {
1288       GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1289           (_("Internal data flow error.")),
1290           ("streaming task paused, reason %s (%d)",
1291               gst_flow_get_name (ret), ret));
1292       gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1293     }
1294     return;
1295   }
1298 static gboolean
1299 gst_queue_handle_src_event (GstPad * pad, GstEvent * event)
1301   gboolean res = TRUE;
1302   GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
1304   if (G_UNLIKELY (queue == NULL)) {
1305     gst_event_unref (event);
1306     return FALSE;
1307   }
1308 #ifndef GST_DISABLE_GST_DEBUG
1309   GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1310       event, GST_EVENT_TYPE (event));
1311 #endif
1313   res = gst_pad_push_event (queue->sinkpad, event);
1315   gst_object_unref (queue);
1316   return res;
1319 static gboolean
1320 gst_queue_handle_src_query (GstPad * pad, GstQuery * query)
1322   GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
1323   GstPad *peer;
1324   gboolean res;
1326   if (G_UNLIKELY (queue == NULL))
1327     return FALSE;
1329   if (!(peer = gst_pad_get_peer (queue->sinkpad))) {
1330     gst_object_unref (queue);
1331     return FALSE;
1332   }
1334   res = gst_pad_query (peer, query);
1335   gst_object_unref (peer);
1336   if (!res) {
1337     gst_object_unref (queue);
1338     return FALSE;
1339   }
1341   switch (GST_QUERY_TYPE (query)) {
1342     case GST_QUERY_POSITION:
1343     {
1344       gint64 peer_pos;
1345       GstFormat format;
1347       /* get peer position */
1348       gst_query_parse_position (query, &format, &peer_pos);
1350       /* FIXME: this code assumes that there's no discont in the queue */
1351       switch (format) {
1352         case GST_FORMAT_BYTES:
1353           peer_pos -= queue->cur_level.bytes;
1354           break;
1355         case GST_FORMAT_TIME:
1356           peer_pos -= queue->cur_level.time;
1357           break;
1358         default:
1359           GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1360               "know how to adjust value", gst_format_get_name (format));
1361           return TRUE;
1362       }
1363       /* set updated position */
1364       gst_query_set_position (query, format, peer_pos);
1365       break;
1366     }
1367     case GST_QUERY_LATENCY:
1368     {
1369       gboolean live;
1370       GstClockTime min, max;
1372       gst_query_parse_latency (query, &live, &min, &max);
1374       /* we can delay up to the limit of the queue in time. If we have no time
1375        * limit, the best thing we can do is to return an infinite delay. In
1376        * reality a better estimate would be the byte/buffer rate but that is not
1377        * possible right now. */
1378       if (queue->max_size.time > 0 && max != -1)
1379         max += queue->max_size.time;
1380       else
1381         max = -1;
1383       /* adjust for min-threshold */
1384       if (queue->min_threshold.time > 0 && min != -1)
1385         min += queue->min_threshold.time;
1387       gst_query_set_latency (query, live, min, max);
1388       break;
1389     }
1390     default:
1391       /* peer handled other queries */
1392       break;
1393   }
1395   gst_object_unref (queue);
1396   return TRUE;
1399 static gboolean
1400 gst_queue_sink_activate_push (GstPad * pad, gboolean active)
1402   gboolean result = TRUE;
1403   GstQueue *queue;
1405   queue = GST_QUEUE (gst_pad_get_parent (pad));
1407   if (active) {
1408     GST_QUEUE_MUTEX_LOCK (queue);
1409     queue->srcresult = GST_FLOW_OK;
1410     queue->eos = FALSE;
1411     queue->unexpected = FALSE;
1412     GST_QUEUE_MUTEX_UNLOCK (queue);
1413   } else {
1414     /* step 1, unblock chain function */
1415     GST_QUEUE_MUTEX_LOCK (queue);
1416     queue->srcresult = GST_FLOW_WRONG_STATE;
1417     gst_queue_locked_flush (queue);
1418     GST_QUEUE_MUTEX_UNLOCK (queue);
1419   }
1421   gst_object_unref (queue);
1423   return result;
1426 static gboolean
1427 gst_queue_src_activate_push (GstPad * pad, gboolean active)
1429   gboolean result = FALSE;
1430   GstQueue *queue;
1432   queue = GST_QUEUE (gst_pad_get_parent (pad));
1434   if (active) {
1435     GST_QUEUE_MUTEX_LOCK (queue);
1436     queue->srcresult = GST_FLOW_OK;
1437     queue->eos = FALSE;
1438     queue->unexpected = FALSE;
1439     /* we do not start the task yet if the pad is not connected */
1440     if (gst_pad_is_linked (pad))
1441       result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
1442     else {
1443       GST_INFO_OBJECT (queue, "not starting task as pad is not linked");
1444       result = TRUE;
1445     }
1446     GST_QUEUE_MUTEX_UNLOCK (queue);
1447   } else {
1448     /* step 1, unblock loop function */
1449     GST_QUEUE_MUTEX_LOCK (queue);
1450     queue->srcresult = GST_FLOW_WRONG_STATE;
1451     /* the item add signal will unblock */
1452     g_cond_signal (queue->item_add);
1453     GST_QUEUE_MUTEX_UNLOCK (queue);
1455     /* step 2, make sure streaming finishes */
1456     result = gst_pad_stop_task (pad);
1457   }
1459   gst_object_unref (queue);
1461   return result;
1464 static void
1465 queue_capacity_change (GstQueue * queue)
1467   if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1468     gst_queue_leak_downstream (queue);
1469   }
1471   /* changing the capacity of the queue must wake up
1472    * the _chain function, it might have more room now
1473    * to store the buffer/event in the queue */
1474   GST_QUEUE_SIGNAL_DEL (queue);
1477 /* Changing the minimum required fill level must
1478  * wake up the _loop function as it might now
1479  * be able to preceed.
1480  */
1481 #define QUEUE_THRESHOLD_CHANGE(q)\
1482   GST_QUEUE_SIGNAL_ADD (q);
1484 static void
1485 gst_queue_set_property (GObject * object,
1486     guint prop_id, const GValue * value, GParamSpec * pspec)
1488   GstQueue *queue = GST_QUEUE (object);
1490   /* someone could change levels here, and since this
1491    * affects the get/put funcs, we need to lock for safety. */
1492   GST_QUEUE_MUTEX_LOCK (queue);
1494   switch (prop_id) {
1495     case PROP_MAX_SIZE_BYTES:
1496       queue->max_size.bytes = g_value_get_uint (value);
1497       queue_capacity_change (queue);
1498       break;
1499     case PROP_MAX_SIZE_BUFFERS:
1500       queue->max_size.buffers = g_value_get_uint (value);
1501       queue_capacity_change (queue);
1502       break;
1503     case PROP_MAX_SIZE_TIME:
1504       queue->max_size.time = g_value_get_uint64 (value);
1505       queue_capacity_change (queue);
1506       break;
1507     case PROP_MIN_THRESHOLD_BYTES:
1508       queue->min_threshold.bytes = g_value_get_uint (value);
1509       queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1510       QUEUE_THRESHOLD_CHANGE (queue);
1511       break;
1512     case PROP_MIN_THRESHOLD_BUFFERS:
1513       queue->min_threshold.buffers = g_value_get_uint (value);
1514       queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1515       QUEUE_THRESHOLD_CHANGE (queue);
1516       break;
1517     case PROP_MIN_THRESHOLD_TIME:
1518       queue->min_threshold.time = g_value_get_uint64 (value);
1519       queue->orig_min_threshold.time = queue->min_threshold.time;
1520       QUEUE_THRESHOLD_CHANGE (queue);
1521       break;
1522     case PROP_LEAKY:
1523       queue->leaky = g_value_get_enum (value);
1524       break;
1525     case PROP_SILENT:
1526       queue->silent = g_value_get_boolean (value);
1527       break;
1528     default:
1529       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1530       break;
1531   }
1533   GST_QUEUE_MUTEX_UNLOCK (queue);
1536 static void
1537 gst_queue_get_property (GObject * object,
1538     guint prop_id, GValue * value, GParamSpec * pspec)
1540   GstQueue *queue = GST_QUEUE (object);
1542   GST_QUEUE_MUTEX_LOCK (queue);
1544   switch (prop_id) {
1545     case PROP_CUR_LEVEL_BYTES:
1546       g_value_set_uint (value, queue->cur_level.bytes);
1547       break;
1548     case PROP_CUR_LEVEL_BUFFERS:
1549       g_value_set_uint (value, queue->cur_level.buffers);
1550       break;
1551     case PROP_CUR_LEVEL_TIME:
1552       g_value_set_uint64 (value, queue->cur_level.time);
1553       break;
1554     case PROP_MAX_SIZE_BYTES:
1555       g_value_set_uint (value, queue->max_size.bytes);
1556       break;
1557     case PROP_MAX_SIZE_BUFFERS:
1558       g_value_set_uint (value, queue->max_size.buffers);
1559       break;
1560     case PROP_MAX_SIZE_TIME:
1561       g_value_set_uint64 (value, queue->max_size.time);
1562       break;
1563     case PROP_MIN_THRESHOLD_BYTES:
1564       g_value_set_uint (value, queue->min_threshold.bytes);
1565       break;
1566     case PROP_MIN_THRESHOLD_BUFFERS:
1567       g_value_set_uint (value, queue->min_threshold.buffers);
1568       break;
1569     case PROP_MIN_THRESHOLD_TIME:
1570       g_value_set_uint64 (value, queue->min_threshold.time);
1571       break;
1572     case PROP_LEAKY:
1573       g_value_set_enum (value, queue->leaky);
1574       break;
1575     case PROP_SILENT:
1576       g_value_set_boolean (value, queue->silent);
1577       break;
1578     default:
1579       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1580       break;
1581   }
1583   GST_QUEUE_MUTEX_UNLOCK (queue);