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., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25 /**
26 * SECTION:element-queue
27 * @title: queue
28 *
29 * Data is queued until one of the limits specified by the
30 * #GstQueue:max-size-buffers, #GstQueue:max-size-bytes and/or
31 * #GstQueue:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
33 * becomes available.
34 *
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
37 *
38 * You can query how many buffers are queued by reading the
39 * #GstQueue:current-level-buffers property. You can track changes
40 * by connecting to the notify::current-level-buffers signal (which
41 * like all signals will be emitted from the streaming thread). The same
42 * applies to the #GstQueue:current-level-time and
43 * #GstQueue:current-level-bytes properties.
44 *
45 * The default queue size limits are 200 buffers, 10MB of data, or
46 * one second worth of data, whichever is reached first.
47 *
48 * As said earlier, the queue blocks by default when one of the specified
49 * maximums (bytes, time, buffers) has been reached. You can set the
50 * #GstQueue:leaky property to specify that instead of blocking it should
51 * leak (drop) new or old buffers.
52 *
53 * The #GstQueue::underrun signal is emitted when the queue has less data than
54 * the specified minimum thresholds require (by default: when the queue is
55 * empty). The #GstQueue::overrun signal is emitted when the queue is filled
56 * up. Both signals are emitted from the context of the streaming thread.
57 */
58
59 #include "gst/gst_private.h"
60
61 #include <gst/gst.h>
62 #include "gstqueue.h"
63
64 #include "../../gst/gst-i18n-lib.h"
65 #include "../../gst/glib-compat-private.h"
66
67 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_PAD_SINK,
69 GST_PAD_ALWAYS,
70 GST_STATIC_CAPS_ANY);
71
72 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
73 GST_PAD_SRC,
74 GST_PAD_ALWAYS,
75 GST_STATIC_CAPS_ANY);
76
77 GST_DEBUG_CATEGORY_STATIC (queue_debug);
78 #define GST_CAT_DEFAULT (queue_debug)
79 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
80
81 #define STATUS(queue, pad, msg) \
82 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
83 "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
84 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
85 "-%" G_GUINT64_FORMAT " ns, %u items", \
86 GST_DEBUG_PAD_NAME (pad), \
87 queue->cur_level.buffers, \
88 queue->min_threshold.buffers, \
89 queue->max_size.buffers, \
90 queue->cur_level.bytes, \
91 queue->min_threshold.bytes, \
92 queue->max_size.bytes, \
93 queue->cur_level.time, \
94 queue->min_threshold.time, \
95 queue->max_size.time, \
96 gst_queue_array_get_length (queue->queue))
97
98 /* Queue signals and args */
99 enum
100 {
101 SIGNAL_UNDERRUN,
102 SIGNAL_RUNNING,
103 SIGNAL_OVERRUN,
104 SIGNAL_PUSHING,
105 LAST_SIGNAL
106 };
107
108 enum
109 {
110 PROP_0,
111 /* FIXME: don't we have another way of doing this
112 * "Gstreamer format" (frame/byte/time) queries? */
113 PROP_CUR_LEVEL_BUFFERS,
114 PROP_CUR_LEVEL_BYTES,
115 PROP_CUR_LEVEL_TIME,
116 PROP_MAX_SIZE_BUFFERS,
117 PROP_MAX_SIZE_BYTES,
118 PROP_MAX_SIZE_TIME,
119 PROP_MIN_THRESHOLD_BUFFERS,
120 PROP_MIN_THRESHOLD_BYTES,
121 PROP_MIN_THRESHOLD_TIME,
122 PROP_LEAKY,
123 PROP_SILENT,
124 PROP_FLUSH_ON_EOS
125 };
126
127 /* default property values */
128 #define DEFAULT_MAX_SIZE_BUFFERS 200 /* 200 buffers */
129 #define DEFAULT_MAX_SIZE_BYTES (10 * 1024 * 1024) /* 10 MB */
130 #define DEFAULT_MAX_SIZE_TIME GST_SECOND /* 1 second */
131
132 #define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START { \
133 g_mutex_lock (&q->qlock); \
134 } G_STMT_END
135
136 #define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
137 GST_QUEUE_MUTEX_LOCK (q); \
138 if (q->srcresult != GST_FLOW_OK) \
139 goto label; \
140 } G_STMT_END
141
142 #define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START { \
143 g_mutex_unlock (&q->qlock); \
144 } G_STMT_END
145
146 #define GST_QUEUE_WAIT_DEL_CHECK(q, label) G_STMT_START { \
147 STATUS (q, q->sinkpad, "wait for DEL"); \
148 q->waiting_del = TRUE; \
149 g_cond_wait (&q->item_del, &q->qlock); \
150 q->waiting_del = FALSE; \
151 if (q->srcresult != GST_FLOW_OK) { \
152 STATUS (q, q->srcpad, "received DEL wakeup"); \
153 goto label; \
154 } \
155 STATUS (q, q->sinkpad, "received DEL"); \
156 } G_STMT_END
157
158 #define GST_QUEUE_WAIT_ADD_CHECK(q, label) G_STMT_START { \
159 STATUS (q, q->srcpad, "wait for ADD"); \
160 q->waiting_add = TRUE; \
161 g_cond_wait (&q->item_add, &q->qlock); \
162 q->waiting_add = FALSE; \
163 if (q->srcresult != GST_FLOW_OK) { \
164 STATUS (q, q->srcpad, "received ADD wakeup"); \
165 goto label; \
166 } \
167 STATUS (q, q->srcpad, "received ADD"); \
168 } G_STMT_END
169
170 #define GST_QUEUE_SIGNAL_DEL(q) G_STMT_START { \
171 if (q->waiting_del) { \
172 STATUS (q, q->srcpad, "signal DEL"); \
173 g_cond_signal (&q->item_del); \
174 } \
175 } G_STMT_END
176
177 #define GST_QUEUE_SIGNAL_ADD(q) G_STMT_START { \
178 if (q->waiting_add) { \
179 STATUS (q, q->sinkpad, "signal ADD"); \
180 g_cond_signal (&q->item_add); \
181 } \
182 } G_STMT_END
183
184 #define _do_init \
185 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue", 0, "queue element"); \
186 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0, \
187 "dataflow inside the queue element");
188 #define gst_queue_parent_class parent_class
189 G_DEFINE_TYPE_WITH_CODE (GstQueue, gst_queue, GST_TYPE_ELEMENT, _do_init);
190
191 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);
196
197 static GstFlowReturn gst_queue_chain (GstPad * pad, GstObject * parent,
198 GstBuffer * buffer);
199 static GstFlowReturn gst_queue_chain_list (GstPad * pad, GstObject * parent,
200 GstBufferList * buffer_list);
201 static GstFlowReturn gst_queue_push_one (GstQueue * queue);
202 static void gst_queue_loop (GstPad * pad);
203
204 static GstFlowReturn gst_queue_handle_sink_event (GstPad * pad,
205 GstObject * parent, GstEvent * event);
206 static gboolean gst_queue_handle_sink_query (GstPad * pad, GstObject * parent,
207 GstQuery * query);
208
209 static gboolean gst_queue_handle_src_event (GstPad * pad, GstObject * parent,
210 GstEvent * event);
211 static gboolean gst_queue_handle_src_query (GstPad * pad, GstObject * parent,
212 GstQuery * query);
213
214 static void gst_queue_locked_flush (GstQueue * queue, gboolean full);
215
216 static gboolean gst_queue_src_activate_mode (GstPad * pad, GstObject * parent,
217 GstPadMode mode, gboolean active);
218 static gboolean gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent,
219 GstPadMode mode, gboolean active);
220
221 static gboolean gst_queue_is_empty (GstQueue * queue);
222 static gboolean gst_queue_is_filled (GstQueue * queue);
223
224
225 typedef struct
226 {
227 GstMiniObject *item;
228 gsize size;
229 gboolean is_query;
230 } GstQueueItem;
231
232 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
233
234 static GType
queue_leaky_get_type(void)235 queue_leaky_get_type (void)
236 {
237 static GType queue_leaky_type = 0;
238 static const GEnumValue queue_leaky[] = {
239 {GST_QUEUE_NO_LEAK, "Not Leaky", "no"},
240 {GST_QUEUE_LEAK_UPSTREAM, "Leaky on upstream (new buffers)", "upstream"},
241 {GST_QUEUE_LEAK_DOWNSTREAM, "Leaky on downstream (old buffers)",
242 "downstream"},
243 {0, NULL, NULL},
244 };
245
246 if (!queue_leaky_type) {
247 queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
248 }
249 return queue_leaky_type;
250 }
251
252 static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
253
254 static void
gst_queue_class_init(GstQueueClass * klass)255 gst_queue_class_init (GstQueueClass * klass)
256 {
257 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
258 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
259
260 gobject_class->set_property = gst_queue_set_property;
261 gobject_class->get_property = gst_queue_get_property;
262
263 /* signals */
264 /**
265 * GstQueue::underrun:
266 * @queue: the queue instance
267 *
268 * Reports that the buffer became empty (underrun).
269 * A buffer is empty if the total amount of data inside it (num-buffers, time,
270 * size) is lower than the boundary values which can be set through the
271 * GObject properties.
272 */
273 gst_queue_signals[SIGNAL_UNDERRUN] =
274 g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
275 G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
276 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
277 /**
278 * GstQueue::running:
279 * @queue: the queue instance
280 *
281 * Reports that enough (min-threshold) data is in the queue. Use this signal
282 * together with the underrun signal to pause the pipeline on underrun and
283 * wait for the queue to fill-up before resume playback.
284 */
285 gst_queue_signals[SIGNAL_RUNNING] =
286 g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
287 G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
288 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
289 /**
290 * GstQueue::overrun:
291 * @queue: the queue instance
292 *
293 * Reports that the buffer became full (overrun).
294 * A buffer is full if the total amount of data inside it (num-buffers, time,
295 * size) is higher than the boundary values which can be set through the
296 * GObject properties.
297 */
298 gst_queue_signals[SIGNAL_OVERRUN] =
299 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
300 G_STRUCT_OFFSET (GstQueueClass, overrun), NULL, NULL,
301 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
302 /**
303 * GstQueue::pushing:
304 * @queue: the queue instance
305 *
306 * Reports when the queue has enough data to start pushing data again on the
307 * source pad.
308 */
309 gst_queue_signals[SIGNAL_PUSHING] =
310 g_signal_new ("pushing", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
311 G_STRUCT_OFFSET (GstQueueClass, pushing), NULL, NULL,
312 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
313
314 /* properties */
315 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
316 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
317 "Current amount of data in the queue (bytes)",
318 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
320 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
321 "Current number of buffers in the queue",
322 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
324 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
325 "Current amount of data in the queue (in ns)",
326 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327
328 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
329 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
330 "Max. amount of data in the queue (bytes, 0=disable)",
331 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
332 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
333 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 | GST_PARAM_MUTABLE_PLAYING |
339 G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
341 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
342 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
343 DEFAULT_MAX_SIZE_TIME,
344 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
345 G_PARAM_STATIC_STRINGS));
346
347 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BYTES,
348 g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
349 "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
350 0, G_MAXUINT, 0,
351 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
352 G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_BUFFERS,
354 g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
355 "Min. number of buffers in the queue to allow reading (0=disable)", 0,
356 G_MAXUINT, 0,
357 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
358 G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_MIN_THRESHOLD_TIME,
360 g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
361 "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
362 0, G_MAXUINT64, 0,
363 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
364 G_PARAM_STATIC_STRINGS));
365
366 g_object_class_install_property (gobject_class, PROP_LEAKY,
367 g_param_spec_enum ("leaky", "Leaky",
368 "Where the queue leaks, if at all",
369 GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK,
370 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
371 G_PARAM_STATIC_STRINGS));
372
373 /**
374 * GstQueue:silent
375 *
376 * Don't emit queue signals. Makes queues more lightweight if no signals are
377 * needed.
378 */
379 g_object_class_install_property (gobject_class, PROP_SILENT,
380 g_param_spec_boolean ("silent", "Silent",
381 "Don't emit queue signals", FALSE,
382 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
383 G_PARAM_STATIC_STRINGS));
384
385 /**
386 * GstQueue:flush-on-eos
387 *
388 * Discard all data in the queue when an EOS event is received, and pass
389 * on the EOS event as soon as possible (instead of waiting until all
390 * buffers in the queue have been processed, which is the default behaviour).
391 *
392 * Flushing the queue on EOS might be useful when capturing and encoding
393 * from a live source, to finish up the recording quickly in cases when
394 * the encoder is slow. Note that this might mean some data from the end of
395 * the recording data might be lost though (never more than the configured
396 * max. sizes though).
397 *
398 * Since: 1.2
399 */
400 g_object_class_install_property (gobject_class, PROP_FLUSH_ON_EOS,
401 g_param_spec_boolean ("flush-on-eos", "Flush on EOS",
402 "Discard all data in the queue when an EOS event is received", FALSE,
403 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
404 G_PARAM_STATIC_STRINGS));
405
406 gobject_class->finalize = gst_queue_finalize;
407
408 gst_element_class_set_static_metadata (gstelement_class,
409 "Queue",
410 "Generic", "Simple data queue", "Erik Walthinsen <omega@cse.ogi.edu>");
411 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
412 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
413
414 /* Registering debug symbols for function pointers */
415 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_src_activate_mode);
416 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_event);
417 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_sink_query);
418 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_event);
419 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_handle_src_query);
420 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain);
421 GST_DEBUG_REGISTER_FUNCPTR (gst_queue_chain_list);
422 }
423
424 static void
gst_queue_init(GstQueue * queue)425 gst_queue_init (GstQueue * queue)
426 {
427 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
428
429 gst_pad_set_chain_function (queue->sinkpad, gst_queue_chain);
430 gst_pad_set_chain_list_function (queue->sinkpad, gst_queue_chain_list);
431 gst_pad_set_activatemode_function (queue->sinkpad,
432 gst_queue_sink_activate_mode);
433 gst_pad_set_event_full_function (queue->sinkpad, gst_queue_handle_sink_event);
434 gst_pad_set_query_function (queue->sinkpad, gst_queue_handle_sink_query);
435 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
436 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
437
438 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
439
440 gst_pad_set_activatemode_function (queue->srcpad,
441 gst_queue_src_activate_mode);
442 gst_pad_set_event_function (queue->srcpad, gst_queue_handle_src_event);
443 gst_pad_set_query_function (queue->srcpad, gst_queue_handle_src_query);
444 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
445 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
446
447 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
448 queue->max_size.buffers = DEFAULT_MAX_SIZE_BUFFERS;
449 queue->max_size.bytes = DEFAULT_MAX_SIZE_BYTES;
450 queue->max_size.time = DEFAULT_MAX_SIZE_TIME;
451 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
452 GST_QUEUE_CLEAR_LEVEL (queue->orig_min_threshold);
453 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
454 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
455 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
456
457 queue->leaky = GST_QUEUE_NO_LEAK;
458 queue->srcresult = GST_FLOW_FLUSHING;
459
460 g_mutex_init (&queue->qlock);
461 g_cond_init (&queue->item_add);
462 g_cond_init (&queue->item_del);
463 g_cond_init (&queue->query_handled);
464
465 queue->queue =
466 gst_queue_array_new_for_struct (sizeof (GstQueueItem),
467 DEFAULT_MAX_SIZE_BUFFERS * 3 / 2);
468
469 queue->sinktime = GST_CLOCK_STIME_NONE;
470 queue->srctime = GST_CLOCK_STIME_NONE;
471
472 queue->sink_tainted = TRUE;
473 queue->src_tainted = TRUE;
474
475 queue->newseg_applied_to_src = FALSE;
476
477 GST_DEBUG_OBJECT (queue,
478 "initialized queue's not_empty & not_full conditions");
479 }
480
481 /* called only once, as opposed to dispose */
482 static void
gst_queue_finalize(GObject * object)483 gst_queue_finalize (GObject * object)
484 {
485 GstQueue *queue = GST_QUEUE (object);
486 GstQueueItem *qitem;
487
488 GST_DEBUG_OBJECT (queue, "finalizing queue");
489
490 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
491 /* FIXME: if it's a query, shouldn't we unref that too? */
492 if (!qitem->is_query)
493 gst_mini_object_unref (qitem->item);
494 }
495 gst_queue_array_free (queue->queue);
496
497 g_mutex_clear (&queue->qlock);
498 g_cond_clear (&queue->item_add);
499 g_cond_clear (&queue->item_del);
500 g_cond_clear (&queue->query_handled);
501
502 G_OBJECT_CLASS (parent_class)->finalize (object);
503 }
504
505 /* Convenience function */
506 static inline GstClockTimeDiff
my_segment_to_running_time(GstSegment * segment,GstClockTime val)507 my_segment_to_running_time (GstSegment * segment, GstClockTime val)
508 {
509 GstClockTimeDiff res = GST_CLOCK_STIME_NONE;
510
511 if (GST_CLOCK_TIME_IS_VALID (val)) {
512 gboolean sign =
513 gst_segment_to_running_time_full (segment, GST_FORMAT_TIME, val, &val);
514 if (sign > 0)
515 res = val;
516 else if (sign < 0)
517 res = -val;
518 }
519 return res;
520 }
521
522 /* calculate the diff between running time on the sink and src of the queue.
523 * This is the total amount of time in the queue. */
524 static void
update_time_level(GstQueue * queue)525 update_time_level (GstQueue * queue)
526 {
527 gint64 sink_time, src_time;
528
529 if (queue->sink_tainted) {
530 GST_LOG_OBJECT (queue, "update sink time");
531 queue->sinktime =
532 my_segment_to_running_time (&queue->sink_segment,
533 queue->sink_segment.position);
534 queue->sink_tainted = FALSE;
535 }
536 sink_time = queue->sinktime;
537
538 if (queue->src_tainted) {
539 GST_LOG_OBJECT (queue, "update src time");
540 queue->srctime =
541 my_segment_to_running_time (&queue->src_segment,
542 queue->src_segment.position);
543 queue->src_tainted = FALSE;
544 }
545 src_time = queue->srctime;
546
547 GST_LOG_OBJECT (queue, "sink %" GST_STIME_FORMAT ", src %" GST_STIME_FORMAT,
548 GST_STIME_ARGS (sink_time), GST_STIME_ARGS (src_time));
549
550 if (GST_CLOCK_STIME_IS_VALID (src_time)
551 && GST_CLOCK_STIME_IS_VALID (sink_time) && sink_time >= src_time)
552 queue->cur_level.time = sink_time - src_time;
553 else
554 queue->cur_level.time = 0;
555 }
556
557 /* take a SEGMENT event and apply the values to segment, updating the time
558 * level of queue. */
559 static void
apply_segment(GstQueue * queue,GstEvent * event,GstSegment * segment,gboolean sink)560 apply_segment (GstQueue * queue, GstEvent * event, GstSegment * segment,
561 gboolean sink)
562 {
563 gst_event_copy_segment (event, segment);
564
565 /* now configure the values, we use these to track timestamps on the
566 * sinkpad. */
567 if (segment->format != GST_FORMAT_TIME) {
568 /* non-time format, pretent the current time segment is closed with a
569 * 0 start and unknown stop time. */
570 segment->format = GST_FORMAT_TIME;
571 segment->start = 0;
572 segment->stop = -1;
573 segment->time = 0;
574 }
575 if (sink)
576 queue->sink_tainted = TRUE;
577 else
578 queue->src_tainted = TRUE;
579
580 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
581
582 /* segment can update the time level of the queue */
583 update_time_level (queue);
584 }
585
586 static void
apply_gap(GstQueue * queue,GstEvent * event,GstSegment * segment,gboolean is_sink)587 apply_gap (GstQueue * queue, GstEvent * event,
588 GstSegment * segment, gboolean is_sink)
589 {
590 GstClockTime timestamp;
591 GstClockTime duration;
592
593 gst_event_parse_gap (event, ×tamp, &duration);
594
595 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
596
597 if (GST_CLOCK_TIME_IS_VALID (duration)) {
598 timestamp += duration;
599 }
600
601 segment->position = timestamp;
602
603 if (is_sink)
604 queue->sink_tainted = TRUE;
605 else
606 queue->src_tainted = TRUE;
607
608 /* calc diff with other end */
609 update_time_level (queue);
610 }
611 }
612
613
614 /* take a buffer and update segment, updating the time level of the queue. */
615 static void
apply_buffer(GstQueue * queue,GstBuffer * buffer,GstSegment * segment,gboolean sink)616 apply_buffer (GstQueue * queue, GstBuffer * buffer, GstSegment * segment,
617 gboolean sink)
618 {
619 GstClockTime duration, timestamp;
620
621 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
622 duration = GST_BUFFER_DURATION (buffer);
623
624 /* if no timestamp is set, assume it's continuous with the previous
625 * time */
626 if (timestamp == GST_CLOCK_TIME_NONE)
627 timestamp = segment->position;
628
629 /* add duration */
630 if (duration != GST_CLOCK_TIME_NONE)
631 timestamp += duration;
632
633 GST_LOG_OBJECT (queue, "%s position updated to %" GST_TIME_FORMAT,
634 segment == &queue->sink_segment ? "sink" : "src",
635 GST_TIME_ARGS (timestamp));
636
637 segment->position = timestamp;
638 if (sink)
639 queue->sink_tainted = TRUE;
640 else
641 queue->src_tainted = TRUE;
642
643
644 /* calc diff with other end */
645 update_time_level (queue);
646 }
647
648 static gboolean
buffer_list_apply_time(GstBuffer ** buf,guint idx,gpointer user_data)649 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer user_data)
650 {
651 GstClockTime *timestamp = user_data;
652 GstClockTime btime;
653
654 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
655 " duration %" GST_TIME_FORMAT, idx, GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
656 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
657 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
658
659 btime = GST_BUFFER_DTS_OR_PTS (*buf);
660 if (GST_CLOCK_TIME_IS_VALID (btime))
661 *timestamp = btime;
662
663 if (GST_BUFFER_DURATION_IS_VALID (*buf))
664 *timestamp += GST_BUFFER_DURATION (*buf);
665
666 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
667
668 return TRUE;
669 }
670
671 /* take a buffer list and update segment, updating the time level of the queue */
672 static void
apply_buffer_list(GstQueue * queue,GstBufferList * buffer_list,GstSegment * segment,gboolean sink)673 apply_buffer_list (GstQueue * queue, GstBufferList * buffer_list,
674 GstSegment * segment, gboolean sink)
675 {
676 GstClockTime timestamp;
677
678 /* if no timestamp is set, assume it's continuous with the previous time */
679 timestamp = segment->position;
680
681 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
682
683 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
684 GST_TIME_ARGS (timestamp));
685
686 segment->position = timestamp;
687
688 if (sink)
689 queue->sink_tainted = TRUE;
690 else
691 queue->src_tainted = TRUE;
692
693 /* calc diff with other end */
694 update_time_level (queue);
695 }
696
697 static void
gst_queue_locked_flush(GstQueue * queue,gboolean full)698 gst_queue_locked_flush (GstQueue * queue, gboolean full)
699 {
700 GstQueueItem *qitem;
701
702 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
703 /* Then lose another reference because we are supposed to destroy that
704 data when flushing */
705 if (!full && !qitem->is_query && GST_IS_EVENT (qitem->item)
706 && GST_EVENT_IS_STICKY (qitem->item)
707 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
708 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
709 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (qitem->item));
710 }
711 if (!qitem->is_query)
712 gst_mini_object_unref (qitem->item);
713 memset (qitem, 0, sizeof (GstQueueItem));
714 }
715 queue->last_query = FALSE;
716 g_cond_signal (&queue->query_handled);
717 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
718 queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
719 queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
720 queue->min_threshold.time = queue->orig_min_threshold.time;
721 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
722 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
723 queue->head_needs_discont = queue->tail_needs_discont = FALSE;
724
725 queue->sinktime = queue->srctime = GST_CLOCK_STIME_NONE;
726 queue->sink_tainted = queue->src_tainted = TRUE;
727
728 /* we deleted a lot of something */
729 GST_QUEUE_SIGNAL_DEL (queue);
730 }
731
732 /* enqueue an item an update the level stats, with QUEUE_LOCK */
733 static inline void
gst_queue_locked_enqueue_buffer(GstQueue * queue,gpointer item)734 gst_queue_locked_enqueue_buffer (GstQueue * queue, gpointer item)
735 {
736 GstQueueItem qitem;
737 GstBuffer *buffer = GST_BUFFER_CAST (item);
738 gsize bsize = gst_buffer_get_size (buffer);
739
740 /* add buffer to the statistics */
741 queue->cur_level.buffers++;
742 queue->cur_level.bytes += bsize;
743 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
744
745 qitem.item = item;
746 qitem.is_query = FALSE;
747 qitem.size = bsize;
748 gst_queue_array_push_tail_struct (queue->queue, &qitem);
749 GST_QUEUE_SIGNAL_ADD (queue);
750 }
751
752 static inline void
gst_queue_locked_enqueue_buffer_list(GstQueue * queue,gpointer item)753 gst_queue_locked_enqueue_buffer_list (GstQueue * queue, gpointer item)
754 {
755 GstQueueItem qitem;
756 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
757 gsize bsize;
758
759 bsize = gst_buffer_list_calculate_size (buffer_list);
760
761 /* add buffer to the statistics */
762 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
763 queue->cur_level.bytes += bsize;
764 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
765
766 qitem.item = item;
767 qitem.is_query = FALSE;
768 qitem.size = bsize;
769 gst_queue_array_push_tail_struct (queue->queue, &qitem);
770 GST_QUEUE_SIGNAL_ADD (queue);
771 }
772
773 static inline void
gst_queue_locked_enqueue_event(GstQueue * queue,gpointer item)774 gst_queue_locked_enqueue_event (GstQueue * queue, gpointer item)
775 {
776 GstQueueItem qitem;
777 GstEvent *event = GST_EVENT_CAST (item);
778
779 switch (GST_EVENT_TYPE (event)) {
780 case GST_EVENT_EOS:
781 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from upstream");
782 /* Zero the thresholds, this makes sure the queue is completely
783 * filled and we can read all data from the queue. */
784 if (queue->flush_on_eos)
785 gst_queue_locked_flush (queue, FALSE);
786 else
787 GST_QUEUE_CLEAR_LEVEL (queue->min_threshold);
788 /* mark the queue as EOS. This prevents us from accepting more data. */
789 queue->eos = TRUE;
790 break;
791 case GST_EVENT_SEGMENT:
792 apply_segment (queue, event, &queue->sink_segment, TRUE);
793 /* if the queue is empty, apply sink segment on the source */
794 if (gst_queue_array_is_empty (queue->queue)) {
795 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Apply segment on srcpad");
796 apply_segment (queue, event, &queue->src_segment, FALSE);
797 queue->newseg_applied_to_src = TRUE;
798 }
799 /* a new segment allows us to accept more buffers if we got EOS
800 * from downstream */
801 queue->unexpected = FALSE;
802 break;
803 case GST_EVENT_GAP:
804 apply_gap (queue, event, &queue->sink_segment, TRUE);
805 break;
806 default:
807 break;
808 }
809
810 qitem.item = item;
811 qitem.is_query = FALSE;
812 qitem.size = 0;
813 gst_queue_array_push_tail_struct (queue->queue, &qitem);
814 GST_QUEUE_SIGNAL_ADD (queue);
815 }
816
817 /* dequeue an item from the queue and update level stats, with QUEUE_LOCK */
818 static GstMiniObject *
gst_queue_locked_dequeue(GstQueue * queue)819 gst_queue_locked_dequeue (GstQueue * queue)
820 {
821 GstQueueItem *qitem;
822 GstMiniObject *item;
823 gsize bufsize;
824
825 qitem = gst_queue_array_pop_head_struct (queue->queue);
826 if (qitem == NULL)
827 goto no_item;
828
829 item = qitem->item;
830 bufsize = qitem->size;
831
832 if (GST_IS_BUFFER (item)) {
833 GstBuffer *buffer = GST_BUFFER_CAST (item);
834
835 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
836 "retrieved buffer %p from queue", buffer);
837
838 queue->cur_level.buffers--;
839 queue->cur_level.bytes -= bufsize;
840 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
841
842 /* if the queue is empty now, update the other side */
843 if (queue->cur_level.buffers == 0)
844 queue->cur_level.time = 0;
845 } else if (GST_IS_BUFFER_LIST (item)) {
846 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (item);
847
848 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
849 "retrieved buffer list %p from queue", buffer_list);
850
851 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
852 queue->cur_level.bytes -= bufsize;
853 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
854
855 /* if the queue is empty now, update the other side */
856 if (queue->cur_level.buffers == 0)
857 queue->cur_level.time = 0;
858 } else if (GST_IS_EVENT (item)) {
859 GstEvent *event = GST_EVENT_CAST (item);
860
861 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
862 "retrieved event %p from queue", event);
863
864 switch (GST_EVENT_TYPE (event)) {
865 case GST_EVENT_EOS:
866 /* queue is empty now that we dequeued the EOS */
867 GST_QUEUE_CLEAR_LEVEL (queue->cur_level);
868 break;
869 case GST_EVENT_SEGMENT:
870 /* apply newsegment if it has not already been applied */
871 if (G_LIKELY (!queue->newseg_applied_to_src)) {
872 apply_segment (queue, event, &queue->src_segment, FALSE);
873 } else {
874 queue->newseg_applied_to_src = FALSE;
875 }
876 break;
877 case GST_EVENT_GAP:
878 apply_gap (queue, event, &queue->src_segment, FALSE);
879 break;
880 default:
881 break;
882 }
883 } else if (GST_IS_QUERY (item)) {
884 GstQuery *query = GST_QUERY_CAST (item);
885
886 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
887 "retrieved query %p from queue", query);
888 } else {
889 g_warning
890 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
891 item, GST_OBJECT_NAME (queue));
892 item = NULL;
893 }
894 GST_QUEUE_SIGNAL_DEL (queue);
895
896 return item;
897
898 /* ERRORS */
899 no_item:
900 {
901 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "the queue is empty");
902 return NULL;
903 }
904 }
905
906 static GstFlowReturn
gst_queue_handle_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)907 gst_queue_handle_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
908 {
909 gboolean ret = TRUE;
910 GstQueue *queue;
911
912 queue = GST_QUEUE (parent);
913
914 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
915 GST_EVENT_TYPE_NAME (event));
916
917 switch (GST_EVENT_TYPE (event)) {
918 case GST_EVENT_FLUSH_START:
919 /* forward event */
920 ret = gst_pad_push_event (queue->srcpad, event);
921
922 /* now unblock the chain function */
923 GST_QUEUE_MUTEX_LOCK (queue);
924 queue->srcresult = GST_FLOW_FLUSHING;
925 /* unblock the loop and chain functions */
926 GST_QUEUE_SIGNAL_ADD (queue);
927 GST_QUEUE_SIGNAL_DEL (queue);
928 GST_QUEUE_MUTEX_UNLOCK (queue);
929
930 /* make sure it pauses, this should happen since we sent
931 * flush_start downstream. */
932 gst_pad_pause_task (queue->srcpad);
933 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
934
935 /* unblock query handler after the streaming thread is shut down.
936 * Otherwise downstream might have a query that is already unreffed
937 * upstream */
938 GST_QUEUE_MUTEX_LOCK (queue);
939 queue->last_query = FALSE;
940 g_cond_signal (&queue->query_handled);
941 GST_QUEUE_MUTEX_UNLOCK (queue);
942 break;
943 case GST_EVENT_FLUSH_STOP:
944 /* forward event */
945 ret = gst_pad_push_event (queue->srcpad, event);
946
947 GST_QUEUE_MUTEX_LOCK (queue);
948 gst_queue_locked_flush (queue, FALSE);
949 queue->srcresult = GST_FLOW_OK;
950 queue->eos = FALSE;
951 queue->unexpected = FALSE;
952 if (gst_pad_is_active (queue->srcpad)) {
953 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
954 queue->srcpad, NULL);
955 } else {
956 GST_INFO_OBJECT (queue->srcpad, "not re-starting task on srcpad, "
957 "pad not active any longer");
958 }
959 GST_QUEUE_MUTEX_UNLOCK (queue);
960
961 STATUS (queue, pad, "after flush");
962 break;
963 default:
964 if (GST_EVENT_IS_SERIALIZED (event)) {
965 /* serialized events go in the queue */
966 GST_QUEUE_MUTEX_LOCK (queue);
967
968 /* STREAM_START and SEGMENT reset the EOS status of a
969 * pad. Change the cached sinkpad flow result accordingly */
970 if (queue->srcresult == GST_FLOW_EOS
971 && (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START
972 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
973 queue->srcresult = GST_FLOW_OK;
974
975 if (queue->srcresult != GST_FLOW_OK) {
976 /* Errors in sticky event pushing are no problem and ignored here
977 * as they will cause more meaningful errors during data flow.
978 * For EOS events, that are not followed by data flow, we still
979 * return FALSE here though and report an error.
980 */
981 if (!GST_EVENT_IS_STICKY (event)) {
982 GST_QUEUE_MUTEX_UNLOCK (queue);
983 goto out_flow_error;
984 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
985 if (queue->srcresult == GST_FLOW_NOT_LINKED
986 || queue->srcresult < GST_FLOW_EOS) {
987 GST_QUEUE_MUTEX_UNLOCK (queue);
988 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
989 } else {
990 GST_QUEUE_MUTEX_UNLOCK (queue);
991 }
992 goto out_flow_error;
993 }
994 }
995
996 /* refuse more events on EOS unless they unset the EOS status */
997 if (queue->eos) {
998 switch (GST_EVENT_TYPE (event)) {
999 case GST_EVENT_STREAM_START:
1000 case GST_EVENT_SEGMENT:
1001 /* Restart the loop */
1002 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
1003 queue->srcresult = GST_FLOW_OK;
1004 queue->eos = FALSE;
1005 queue->unexpected = FALSE;
1006 gst_pad_start_task (queue->srcpad,
1007 (GstTaskFunction) gst_queue_loop, queue->srcpad, NULL);
1008 } else {
1009 queue->eos = FALSE;
1010 queue->unexpected = FALSE;
1011 }
1012
1013 break;
1014 default:
1015 goto out_eos;
1016 }
1017 }
1018
1019 gst_queue_locked_enqueue_event (queue, event);
1020 GST_QUEUE_MUTEX_UNLOCK (queue);
1021 } else {
1022 /* non-serialized events are forwarded downstream immediately */
1023 ret = gst_pad_push_event (queue->srcpad, event);
1024 }
1025 break;
1026 }
1027 if (ret == FALSE) {
1028 GST_ERROR_OBJECT (queue, "Failed to push event");
1029 return GST_FLOW_ERROR;
1030 }
1031 return GST_FLOW_OK;
1032
1033 /* ERRORS */
1034 out_eos:
1035 {
1036 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "refusing event, we are EOS");
1037 GST_QUEUE_MUTEX_UNLOCK (queue);
1038 gst_event_unref (event);
1039 return GST_FLOW_EOS;
1040 }
1041 out_flow_error:
1042 {
1043 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1044 "refusing event, we have a downstream flow error: %s",
1045 gst_flow_get_name (queue->srcresult));
1046 gst_event_unref (event);
1047 return queue->srcresult;
1048 }
1049 }
1050
1051 static gboolean
gst_queue_handle_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)1052 gst_queue_handle_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1053 {
1054 GstQueue *queue = GST_QUEUE_CAST (parent);
1055 gboolean res;
1056
1057 switch (GST_QUERY_TYPE (query)) {
1058 default:
1059 if (G_UNLIKELY (GST_QUERY_IS_SERIALIZED (query))) {
1060 GstQueueItem qitem;
1061
1062 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1063 GST_LOG_OBJECT (queue, "queuing query %p (%s)", query,
1064 GST_QUERY_TYPE_NAME (query));
1065 qitem.item = GST_MINI_OBJECT_CAST (query);
1066 qitem.is_query = TRUE;
1067 qitem.size = 0;
1068 gst_queue_array_push_tail_struct (queue->queue, &qitem);
1069 GST_QUEUE_SIGNAL_ADD (queue);
1070 while (queue->srcresult == GST_FLOW_OK &&
1071 queue->last_handled_query != query)
1072 g_cond_wait (&queue->query_handled, &queue->qlock);
1073 queue->last_handled_query = NULL;
1074 if (queue->srcresult != GST_FLOW_OK)
1075 goto out_flushing;
1076 res = queue->last_query;
1077 GST_QUEUE_MUTEX_UNLOCK (queue);
1078 } else {
1079 res = gst_pad_query_default (pad, parent, query);
1080 }
1081 break;
1082 }
1083 return res;
1084
1085 /* ERRORS */
1086 out_flushing:
1087 {
1088 GST_DEBUG_OBJECT (queue, "we are flushing");
1089 GST_QUEUE_MUTEX_UNLOCK (queue);
1090 return FALSE;
1091 }
1092 }
1093
1094 static gboolean
gst_queue_is_empty(GstQueue * queue)1095 gst_queue_is_empty (GstQueue * queue)
1096 {
1097 GstQueueItem *tail;
1098
1099 tail = gst_queue_array_peek_tail_struct (queue->queue);
1100
1101 if (tail == NULL)
1102 return TRUE;
1103
1104 /* Only consider the queue empty if the minimum thresholds
1105 * are not reached and data is at the queue tail. Otherwise
1106 * we would block forever on serialized queries.
1107 */
1108 if (!GST_IS_BUFFER (tail->item) && !GST_IS_BUFFER_LIST (tail->item))
1109 return FALSE;
1110
1111 /* It is possible that a max size is reached before all min thresholds are.
1112 * Therefore, only consider it empty if it is not filled. */
1113 return ((queue->min_threshold.buffers > 0 &&
1114 queue->cur_level.buffers < queue->min_threshold.buffers) ||
1115 (queue->min_threshold.bytes > 0 &&
1116 queue->cur_level.bytes < queue->min_threshold.bytes) ||
1117 (queue->min_threshold.time > 0 &&
1118 queue->cur_level.time < queue->min_threshold.time)) &&
1119 !gst_queue_is_filled (queue);
1120 }
1121
1122 static gboolean
gst_queue_is_filled(GstQueue * queue)1123 gst_queue_is_filled (GstQueue * queue)
1124 {
1125 return (((queue->max_size.buffers > 0 &&
1126 queue->cur_level.buffers >= queue->max_size.buffers) ||
1127 (queue->max_size.bytes > 0 &&
1128 queue->cur_level.bytes >= queue->max_size.bytes) ||
1129 (queue->max_size.time > 0 &&
1130 queue->cur_level.time >= queue->max_size.time)));
1131 }
1132
1133 static void
gst_queue_leak_downstream(GstQueue * queue)1134 gst_queue_leak_downstream (GstQueue * queue)
1135 {
1136 /* for as long as the queue is filled, dequeue an item and discard it */
1137 while (gst_queue_is_filled (queue)) {
1138 GstMiniObject *leak;
1139
1140 leak = gst_queue_locked_dequeue (queue);
1141 /* there is nothing to dequeue and the queue is still filled.. This should
1142 * not happen */
1143 g_assert (leak != NULL);
1144
1145 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1146 "queue is full, leaking item %p on downstream end", leak);
1147 if (GST_IS_EVENT (leak) && GST_EVENT_IS_STICKY (leak)) {
1148 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1149 "Storing sticky event %s on srcpad", GST_EVENT_TYPE_NAME (leak));
1150 gst_pad_store_sticky_event (queue->srcpad, GST_EVENT_CAST (leak));
1151 }
1152
1153 if (!GST_IS_QUERY (leak))
1154 gst_mini_object_unref (leak);
1155
1156 /* last buffer needs to get a DISCONT flag */
1157 queue->head_needs_discont = TRUE;
1158 }
1159 }
1160
1161 static gboolean
discont_first_buffer(GstBuffer ** buffer,guint i,gpointer user_data)1162 discont_first_buffer (GstBuffer ** buffer, guint i, gpointer user_data)
1163 {
1164 GstQueue *queue = user_data;
1165 GstBuffer *subbuffer = gst_buffer_make_writable (*buffer);
1166
1167 if (subbuffer) {
1168 *buffer = subbuffer;
1169 GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
1170 } else {
1171 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1172 }
1173
1174 return FALSE;
1175 }
1176
1177 static GstFlowReturn
gst_queue_chain_buffer_or_list(GstPad * pad,GstObject * parent,GstMiniObject * obj,gboolean is_list)1178 gst_queue_chain_buffer_or_list (GstPad * pad, GstObject * parent,
1179 GstMiniObject * obj, gboolean is_list)
1180 {
1181 GstQueue *queue;
1182
1183 queue = GST_QUEUE_CAST (parent);
1184
1185 /* we have to lock the queue since we span threads */
1186 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1187 /* when we received EOS, we refuse any more data */
1188 if (queue->eos)
1189 goto out_eos;
1190 if (queue->unexpected)
1191 goto out_unexpected;
1192
1193 if (!is_list) {
1194 GstClockTime duration, timestamp;
1195 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1196
1197 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1198 duration = GST_BUFFER_DURATION (buffer);
1199
1200 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
1201 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1202 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1203 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1204 } else {
1205 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1206 "received buffer list %p with %u buffers", obj,
1207 gst_buffer_list_length (GST_BUFFER_LIST_CAST (obj)));
1208 }
1209
1210 /* We make space available if we're "full" according to whatever
1211 * the user defined as "full". Note that this only applies to buffers.
1212 * We always handle events and they don't count in our statistics. */
1213 while (gst_queue_is_filled (queue)) {
1214 if (!queue->silent) {
1215 GST_QUEUE_MUTEX_UNLOCK (queue);
1216 g_signal_emit (queue, gst_queue_signals[SIGNAL_OVERRUN], 0);
1217 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1218 /* we recheck, the signal could have changed the thresholds */
1219 if (!gst_queue_is_filled (queue))
1220 break;
1221 }
1222
1223 /* how are we going to make space for this buffer? */
1224 switch (queue->leaky) {
1225 case GST_QUEUE_LEAK_UPSTREAM:
1226 /* next buffer needs to get a DISCONT flag */
1227 queue->tail_needs_discont = TRUE;
1228 /* leak current buffer */
1229 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1230 "queue is full, leaking buffer on upstream end");
1231 /* now we can clean up and exit right away */
1232 goto out_unref;
1233 case GST_QUEUE_LEAK_DOWNSTREAM:
1234 gst_queue_leak_downstream (queue);
1235 break;
1236 default:
1237 g_warning ("Unknown leaky type, using default");
1238 /* fall-through */
1239 case GST_QUEUE_NO_LEAK:
1240 {
1241 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1242 "queue is full, waiting for free space");
1243
1244 /* don't leak. Instead, wait for space to be available */
1245 do {
1246 /* for as long as the queue is filled, wait till an item was deleted. */
1247 GST_QUEUE_WAIT_DEL_CHECK (queue, out_flushing);
1248 } while (gst_queue_is_filled (queue));
1249
1250 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not full");
1251
1252 if (!queue->silent) {
1253 GST_QUEUE_MUTEX_UNLOCK (queue);
1254 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1255 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1256 }
1257 break;
1258 }
1259 }
1260 }
1261
1262 if (queue->tail_needs_discont) {
1263 if (!is_list) {
1264 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1265 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1266
1267 if (subbuffer) {
1268 buffer = subbuffer;
1269 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1270 } else {
1271 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1272 }
1273
1274 obj = GST_MINI_OBJECT_CAST (buffer);
1275 } else {
1276 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
1277
1278 buffer_list = gst_buffer_list_make_writable (buffer_list);
1279 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1280 obj = GST_MINI_OBJECT_CAST (buffer_list);
1281 }
1282 queue->tail_needs_discont = FALSE;
1283 }
1284
1285 /* put buffer in queue now */
1286 if (is_list)
1287 gst_queue_locked_enqueue_buffer_list (queue, obj);
1288 else
1289 gst_queue_locked_enqueue_buffer (queue, obj);
1290 GST_QUEUE_MUTEX_UNLOCK (queue);
1291
1292 return GST_FLOW_OK;
1293
1294 /* special conditions */
1295 out_unref:
1296 {
1297 GST_QUEUE_MUTEX_UNLOCK (queue);
1298
1299 gst_mini_object_unref (obj);
1300
1301 return GST_FLOW_OK;
1302 }
1303 out_flushing:
1304 {
1305 GstFlowReturn ret = queue->srcresult;
1306
1307 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1308 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1309 GST_QUEUE_MUTEX_UNLOCK (queue);
1310 gst_mini_object_unref (obj);
1311
1312 return ret;
1313 }
1314 out_eos:
1315 {
1316 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1317 GST_QUEUE_MUTEX_UNLOCK (queue);
1318
1319 gst_mini_object_unref (obj);
1320
1321 return GST_FLOW_EOS;
1322 }
1323 out_unexpected:
1324 {
1325 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1326 GST_QUEUE_MUTEX_UNLOCK (queue);
1327
1328 gst_mini_object_unref (obj);
1329
1330 return GST_FLOW_EOS;
1331 }
1332 }
1333
1334 static GstFlowReturn
gst_queue_chain_list(GstPad * pad,GstObject * parent,GstBufferList * buffer_list)1335 gst_queue_chain_list (GstPad * pad, GstObject * parent,
1336 GstBufferList * buffer_list)
1337 {
1338 return gst_queue_chain_buffer_or_list (pad, parent,
1339 GST_MINI_OBJECT_CAST (buffer_list), TRUE);
1340 }
1341
1342 static GstFlowReturn
gst_queue_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)1343 gst_queue_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1344 {
1345 return gst_queue_chain_buffer_or_list (pad, parent,
1346 GST_MINI_OBJECT_CAST (buffer), FALSE);
1347 }
1348
1349 /* dequeue an item from the queue an push it downstream. This functions returns
1350 * the result of the push. */
1351 static GstFlowReturn
gst_queue_push_one(GstQueue * queue)1352 gst_queue_push_one (GstQueue * queue)
1353 {
1354 GstFlowReturn result = queue->srcresult;
1355 GstMiniObject *data;
1356 gboolean is_list;
1357
1358 data = gst_queue_locked_dequeue (queue);
1359 if (data == NULL)
1360 goto no_item;
1361
1362 next:
1363 is_list = GST_IS_BUFFER_LIST (data);
1364
1365 if (GST_IS_BUFFER (data) || is_list) {
1366 if (!is_list) {
1367 GstBuffer *buffer;
1368
1369 buffer = GST_BUFFER_CAST (data);
1370
1371 if (queue->head_needs_discont) {
1372 GstBuffer *subbuffer = gst_buffer_make_writable (buffer);
1373
1374 if (subbuffer) {
1375 buffer = subbuffer;
1376 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1377 } else {
1378 GST_DEBUG_OBJECT (queue, "Could not mark buffer as DISCONT");
1379 }
1380 queue->head_needs_discont = FALSE;
1381 }
1382
1383 GST_QUEUE_MUTEX_UNLOCK (queue);
1384 result = gst_pad_push (queue->srcpad, buffer);
1385 } else {
1386 GstBufferList *buffer_list;
1387
1388 buffer_list = GST_BUFFER_LIST_CAST (data);
1389
1390 if (queue->head_needs_discont) {
1391 buffer_list = gst_buffer_list_make_writable (buffer_list);
1392 gst_buffer_list_foreach (buffer_list, discont_first_buffer, queue);
1393 queue->head_needs_discont = FALSE;
1394 }
1395
1396 GST_QUEUE_MUTEX_UNLOCK (queue);
1397 result = gst_pad_push_list (queue->srcpad, buffer_list);
1398 }
1399
1400 /* need to check for srcresult here as well */
1401 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1402
1403 if (result == GST_FLOW_EOS) {
1404 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
1405 /* stop pushing buffers, we dequeue all items until we see an item that we
1406 * can push again, which is EOS or SEGMENT. If there is nothing in the
1407 * queue we can push, we set a flag to make the sinkpad refuse more
1408 * buffers with an EOS return value. */
1409 while ((data = gst_queue_locked_dequeue (queue))) {
1410 if (GST_IS_BUFFER (data)) {
1411 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1412 "dropping EOS buffer %p", data);
1413 gst_buffer_unref (GST_BUFFER_CAST (data));
1414 } else if (GST_IS_BUFFER_LIST (data)) {
1415 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1416 "dropping EOS buffer list %p", data);
1417 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
1418 } else if (GST_IS_EVENT (data)) {
1419 GstEvent *event = GST_EVENT_CAST (data);
1420 GstEventType type = GST_EVENT_TYPE (event);
1421
1422 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT
1423 || type == GST_EVENT_STREAM_START) {
1424 /* we found a pushable item in the queue, push it out */
1425 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1426 "pushing pushable event %s after EOS",
1427 GST_EVENT_TYPE_NAME (event));
1428 goto next;
1429 }
1430 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1431 "dropping EOS event %p", event);
1432 gst_event_unref (event);
1433 } else if (GST_IS_QUERY (data)) {
1434 GstQuery *query = GST_QUERY_CAST (data);
1435
1436 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1437 "dropping query %p because of EOS", query);
1438 queue->last_query = FALSE;
1439 g_cond_signal (&queue->query_handled);
1440 }
1441 }
1442 /* no more items in the queue. Set the unexpected flag so that upstream
1443 * make us refuse any more buffers on the sinkpad. Since we will still
1444 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
1445 * task function does not shut down. */
1446 queue->unexpected = TRUE;
1447 result = GST_FLOW_OK;
1448 }
1449 } else if (GST_IS_EVENT (data)) {
1450 GstEvent *event = GST_EVENT_CAST (data);
1451 GstEventType type = GST_EVENT_TYPE (event);
1452
1453 GST_QUEUE_MUTEX_UNLOCK (queue);
1454
1455 gst_pad_push_event (queue->srcpad, event);
1456
1457 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1458 /* if we're EOS, return EOS so that the task pauses. */
1459 if (type == GST_EVENT_EOS) {
1460 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1461 "pushed EOS event %p, return EOS", event);
1462 result = GST_FLOW_EOS;
1463 }
1464 } else if (GST_IS_QUERY (data)) {
1465 GstQuery *query = GST_QUERY_CAST (data);
1466 gboolean ret;
1467
1468 GST_QUEUE_MUTEX_UNLOCK (queue);
1469 ret = gst_pad_peer_query (queue->srcpad, query);
1470 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing_query);
1471 queue->last_query = ret;
1472 queue->last_handled_query = query;
1473 g_cond_signal (&queue->query_handled);
1474 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1475 "did query %p, return %d", query, queue->last_query);
1476 }
1477 return result;
1478
1479 /* ERRORS */
1480 no_item:
1481 {
1482 GST_CAT_ERROR_OBJECT (queue_dataflow, queue,
1483 "exit because we have no item in the queue");
1484 return GST_FLOW_ERROR;
1485 }
1486 out_flushing:
1487 {
1488 GstFlowReturn ret = queue->srcresult;
1489 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1490 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1491 return ret;
1492 }
1493 out_flushing_query:
1494 {
1495 GstFlowReturn ret = queue->srcresult;
1496 queue->last_query = FALSE;
1497 g_cond_signal (&queue->query_handled);
1498 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1499 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1500 return ret;
1501 }
1502 }
1503
1504 static void
gst_queue_loop(GstPad * pad)1505 gst_queue_loop (GstPad * pad)
1506 {
1507 GstQueue *queue;
1508 GstFlowReturn ret;
1509
1510 queue = (GstQueue *) GST_PAD_PARENT (pad);
1511
1512 /* have to lock for thread-safety */
1513 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1514
1515 while (gst_queue_is_empty (queue)) {
1516 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is empty");
1517 if (!queue->silent) {
1518 GST_QUEUE_MUTEX_UNLOCK (queue);
1519 g_signal_emit (queue, gst_queue_signals[SIGNAL_UNDERRUN], 0);
1520 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1521 }
1522
1523 /* we recheck, the signal could have changed the thresholds */
1524 while (gst_queue_is_empty (queue)) {
1525 GST_QUEUE_WAIT_ADD_CHECK (queue, out_flushing);
1526 }
1527
1528 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is not empty");
1529 if (!queue->silent) {
1530 GST_QUEUE_MUTEX_UNLOCK (queue);
1531 g_signal_emit (queue, gst_queue_signals[SIGNAL_RUNNING], 0);
1532 g_signal_emit (queue, gst_queue_signals[SIGNAL_PUSHING], 0);
1533 GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
1534 }
1535 }
1536
1537 ret = gst_queue_push_one (queue);
1538 queue->srcresult = ret;
1539 if (ret != GST_FLOW_OK)
1540 goto out_flushing;
1541
1542 GST_QUEUE_MUTEX_UNLOCK (queue);
1543
1544 return;
1545
1546 /* ERRORS */
1547 out_flushing:
1548 {
1549 gboolean eos = queue->eos;
1550 GstFlowReturn ret = queue->srcresult;
1551
1552 gst_pad_pause_task (queue->srcpad);
1553 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1554 "pause task, reason: %s", gst_flow_get_name (ret));
1555 if (ret == GST_FLOW_FLUSHING) {
1556 gst_queue_locked_flush (queue, FALSE);
1557 } else {
1558 GST_QUEUE_SIGNAL_DEL (queue);
1559 queue->last_query = FALSE;
1560 g_cond_signal (&queue->query_handled);
1561 }
1562 GST_QUEUE_MUTEX_UNLOCK (queue);
1563 /* let app know about us giving up if upstream is not expected to do so */
1564 /* EOS is already taken care of elsewhere */
1565 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1566 GST_ELEMENT_FLOW_ERROR (queue, ret);
1567 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1568 }
1569 return;
1570 }
1571 }
1572
1573 static gboolean
gst_queue_handle_src_event(GstPad * pad,GstObject * parent,GstEvent * event)1574 gst_queue_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1575 {
1576 gboolean res = TRUE;
1577 GstQueue *queue = GST_QUEUE (parent);
1578
1579 #ifndef GST_DISABLE_GST_DEBUG
1580 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
1581 event, GST_EVENT_TYPE (event));
1582 #endif
1583
1584 switch (GST_EVENT_TYPE (event)) {
1585 case GST_EVENT_RECONFIGURE:
1586 GST_QUEUE_MUTEX_LOCK (queue);
1587 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
1588 /* when we got not linked, assume downstream is linked again now and we
1589 * can try to start pushing again */
1590 queue->srcresult = GST_FLOW_OK;
1591 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad, NULL);
1592 }
1593 GST_QUEUE_MUTEX_UNLOCK (queue);
1594
1595 res = gst_pad_push_event (queue->sinkpad, event);
1596 break;
1597 default:
1598 res = gst_pad_event_default (pad, parent, event);
1599 break;
1600 }
1601
1602
1603 return res;
1604 }
1605
1606 static gboolean
gst_queue_handle_src_query(GstPad * pad,GstObject * parent,GstQuery * query)1607 gst_queue_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1608 {
1609 GstQueue *queue = GST_QUEUE (parent);
1610 gboolean res;
1611
1612 switch (GST_QUERY_TYPE (query)) {
1613 case GST_QUERY_SCHEDULING:{
1614 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1615 res = TRUE;
1616 break;
1617 }
1618 default:
1619 res = gst_pad_query_default (pad, parent, query);
1620 break;
1621 }
1622
1623 if (!res)
1624 return FALSE;
1625
1626 /* Adjust peer response for data contained in queue */
1627 switch (GST_QUERY_TYPE (query)) {
1628 case GST_QUERY_POSITION:
1629 {
1630 gint64 peer_pos;
1631 GstFormat format;
1632
1633 /* get peer position */
1634 gst_query_parse_position (query, &format, &peer_pos);
1635
1636 /* FIXME: this code assumes that there's no discont in the queue */
1637 switch (format) {
1638 case GST_FORMAT_BYTES:
1639 peer_pos -= queue->cur_level.bytes;
1640 if (peer_pos < 0) /* Clamp result to 0 */
1641 peer_pos = 0;
1642 break;
1643 case GST_FORMAT_TIME:
1644 peer_pos -= queue->cur_level.time;
1645 if (peer_pos < 0) /* Clamp result to 0 */
1646 peer_pos = 0;
1647 break;
1648 default:
1649 GST_DEBUG_OBJECT (queue, "Can't adjust query in %s format, don't "
1650 "know how to adjust value", gst_format_get_name (format));
1651 return TRUE;
1652 }
1653 /* set updated position */
1654 gst_query_set_position (query, format, peer_pos);
1655 break;
1656 }
1657 case GST_QUERY_LATENCY:
1658 {
1659 gboolean live;
1660 GstClockTime min, max;
1661
1662 gst_query_parse_latency (query, &live, &min, &max);
1663
1664 /* we can delay up to the limit of the queue in time. If we have no time
1665 * limit, the best thing we can do is to return an infinite delay. In
1666 * reality a better estimate would be the byte/buffer rate but that is not
1667 * possible right now. */
1668 /* TODO: Use CONVERT query? */
1669 if (queue->max_size.time > 0 && max != -1
1670 && queue->leaky == GST_QUEUE_NO_LEAK)
1671 max += queue->max_size.time;
1672 else if (queue->max_size.time > 0 && queue->leaky != GST_QUEUE_NO_LEAK)
1673 max = MIN (queue->max_size.time, max);
1674 else
1675 max = -1;
1676
1677 /* adjust for min-threshold */
1678 if (queue->min_threshold.time > 0)
1679 min += queue->min_threshold.time;
1680
1681 gst_query_set_latency (query, live, min, max);
1682 break;
1683 }
1684 default:
1685 /* peer handled other queries */
1686 break;
1687 }
1688
1689 return TRUE;
1690 }
1691
1692 static gboolean
gst_queue_sink_activate_mode(GstPad * pad,GstObject * parent,GstPadMode mode,gboolean active)1693 gst_queue_sink_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1694 gboolean active)
1695 {
1696 gboolean result;
1697 GstQueue *queue;
1698
1699 queue = GST_QUEUE (parent);
1700
1701 switch (mode) {
1702 case GST_PAD_MODE_PUSH:
1703 if (active) {
1704 GST_QUEUE_MUTEX_LOCK (queue);
1705 queue->srcresult = GST_FLOW_OK;
1706 queue->eos = FALSE;
1707 queue->unexpected = FALSE;
1708 GST_QUEUE_MUTEX_UNLOCK (queue);
1709 } else {
1710 /* step 1, unblock chain function */
1711 GST_QUEUE_MUTEX_LOCK (queue);
1712 queue->srcresult = GST_FLOW_FLUSHING;
1713 /* the item del signal will unblock */
1714 GST_QUEUE_SIGNAL_DEL (queue);
1715 GST_QUEUE_MUTEX_UNLOCK (queue);
1716
1717 /* step 2, wait until streaming thread stopped and flush queue */
1718 GST_PAD_STREAM_LOCK (pad);
1719 GST_QUEUE_MUTEX_LOCK (queue);
1720 gst_queue_locked_flush (queue, TRUE);
1721 GST_QUEUE_MUTEX_UNLOCK (queue);
1722 GST_PAD_STREAM_UNLOCK (pad);
1723 }
1724 result = TRUE;
1725 break;
1726 default:
1727 result = FALSE;
1728 break;
1729 }
1730 return result;
1731 }
1732
1733 static gboolean
gst_queue_src_activate_mode(GstPad * pad,GstObject * parent,GstPadMode mode,gboolean active)1734 gst_queue_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
1735 gboolean active)
1736 {
1737 gboolean result;
1738 GstQueue *queue;
1739
1740 queue = GST_QUEUE (parent);
1741
1742 switch (mode) {
1743 case GST_PAD_MODE_PUSH:
1744 if (active) {
1745 GST_QUEUE_MUTEX_LOCK (queue);
1746 queue->srcresult = GST_FLOW_OK;
1747 queue->eos = FALSE;
1748 queue->unexpected = FALSE;
1749 result =
1750 gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad,
1751 NULL);
1752 GST_QUEUE_MUTEX_UNLOCK (queue);
1753 } else {
1754 /* step 1, unblock loop function */
1755 GST_QUEUE_MUTEX_LOCK (queue);
1756 queue->srcresult = GST_FLOW_FLUSHING;
1757 /* the item add signal will unblock */
1758 g_cond_signal (&queue->item_add);
1759 GST_QUEUE_MUTEX_UNLOCK (queue);
1760
1761 /* step 2, make sure streaming finishes */
1762 result = gst_pad_stop_task (pad);
1763
1764 GST_QUEUE_MUTEX_LOCK (queue);
1765 gst_queue_locked_flush (queue, FALSE);
1766 GST_QUEUE_MUTEX_UNLOCK (queue);
1767 }
1768 break;
1769 default:
1770 result = FALSE;
1771 break;
1772 }
1773 return result;
1774 }
1775
1776 static void
queue_capacity_change(GstQueue * queue)1777 queue_capacity_change (GstQueue * queue)
1778 {
1779 if (queue->leaky == GST_QUEUE_LEAK_DOWNSTREAM) {
1780 gst_queue_leak_downstream (queue);
1781 }
1782
1783 /* changing the capacity of the queue must wake up
1784 * the _chain function, it might have more room now
1785 * to store the buffer/event in the queue */
1786 GST_QUEUE_SIGNAL_DEL (queue);
1787 }
1788
1789 /* Changing the minimum required fill level must
1790 * wake up the _loop function as it might now
1791 * be able to preceed.
1792 */
1793 #define QUEUE_THRESHOLD_CHANGE(q)\
1794 GST_QUEUE_SIGNAL_ADD (q);
1795
1796 static void
gst_queue_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1797 gst_queue_set_property (GObject * object,
1798 guint prop_id, const GValue * value, GParamSpec * pspec)
1799 {
1800 GstQueue *queue = GST_QUEUE (object);
1801
1802 /* someone could change levels here, and since this
1803 * affects the get/put funcs, we need to lock for safety. */
1804 GST_QUEUE_MUTEX_LOCK (queue);
1805
1806 switch (prop_id) {
1807 case PROP_MAX_SIZE_BYTES:
1808 queue->max_size.bytes = g_value_get_uint (value);
1809 queue_capacity_change (queue);
1810 break;
1811 case PROP_MAX_SIZE_BUFFERS:
1812 queue->max_size.buffers = g_value_get_uint (value);
1813 queue_capacity_change (queue);
1814 break;
1815 case PROP_MAX_SIZE_TIME:
1816 queue->max_size.time = g_value_get_uint64 (value);
1817 queue_capacity_change (queue);
1818 break;
1819 case PROP_MIN_THRESHOLD_BYTES:
1820 queue->min_threshold.bytes = g_value_get_uint (value);
1821 queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
1822 QUEUE_THRESHOLD_CHANGE (queue);
1823 break;
1824 case PROP_MIN_THRESHOLD_BUFFERS:
1825 queue->min_threshold.buffers = g_value_get_uint (value);
1826 queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
1827 QUEUE_THRESHOLD_CHANGE (queue);
1828 break;
1829 case PROP_MIN_THRESHOLD_TIME:
1830 queue->min_threshold.time = g_value_get_uint64 (value);
1831 queue->orig_min_threshold.time = queue->min_threshold.time;
1832 QUEUE_THRESHOLD_CHANGE (queue);
1833 break;
1834 case PROP_LEAKY:
1835 queue->leaky = g_value_get_enum (value);
1836 break;
1837 case PROP_SILENT:
1838 queue->silent = g_value_get_boolean (value);
1839 break;
1840 case PROP_FLUSH_ON_EOS:
1841 queue->flush_on_eos = g_value_get_boolean (value);
1842 break;
1843 default:
1844 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1845 break;
1846 }
1847
1848 GST_QUEUE_MUTEX_UNLOCK (queue);
1849 }
1850
1851 static void
gst_queue_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1852 gst_queue_get_property (GObject * object,
1853 guint prop_id, GValue * value, GParamSpec * pspec)
1854 {
1855 GstQueue *queue = GST_QUEUE (object);
1856
1857 GST_QUEUE_MUTEX_LOCK (queue);
1858
1859 switch (prop_id) {
1860 case PROP_CUR_LEVEL_BYTES:
1861 g_value_set_uint (value, queue->cur_level.bytes);
1862 break;
1863 case PROP_CUR_LEVEL_BUFFERS:
1864 g_value_set_uint (value, queue->cur_level.buffers);
1865 break;
1866 case PROP_CUR_LEVEL_TIME:
1867 g_value_set_uint64 (value, queue->cur_level.time);
1868 break;
1869 case PROP_MAX_SIZE_BYTES:
1870 g_value_set_uint (value, queue->max_size.bytes);
1871 break;
1872 case PROP_MAX_SIZE_BUFFERS:
1873 g_value_set_uint (value, queue->max_size.buffers);
1874 break;
1875 case PROP_MAX_SIZE_TIME:
1876 g_value_set_uint64 (value, queue->max_size.time);
1877 break;
1878 case PROP_MIN_THRESHOLD_BYTES:
1879 g_value_set_uint (value, queue->min_threshold.bytes);
1880 break;
1881 case PROP_MIN_THRESHOLD_BUFFERS:
1882 g_value_set_uint (value, queue->min_threshold.buffers);
1883 break;
1884 case PROP_MIN_THRESHOLD_TIME:
1885 g_value_set_uint64 (value, queue->min_threshold.time);
1886 break;
1887 case PROP_LEAKY:
1888 g_value_set_enum (value, queue->leaky);
1889 break;
1890 case PROP_SILENT:
1891 g_value_set_boolean (value, queue->silent);
1892 break;
1893 case PROP_FLUSH_ON_EOS:
1894 g_value_set_boolean (value, queue->flush_on_eos);
1895 break;
1896 default:
1897 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1898 break;
1899 }
1900
1901 GST_QUEUE_MUTEX_UNLOCK (queue);
1902 }
1903