Debian Codegraph @

libs/gst/base/gstaggregator.c

raw
1 /* GStreamer aggregator base class
2 * Copyright (C) 2014 Mathieu Duponchelle <[email protected]>
3 * Copyright (C) 2014 Thibault Saunier <[email protected]>
4 *
5 * gstaggregator.c:
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 /**
23 * SECTION: gstaggregator
24 * @title: GstAggregator
25 * @short_description: Base class for mixers and muxers, manages a set of input
26 * pads and aggregates their streams
27 * @see_also: gstcollectpads for historical reasons.
28 *
29 * Manages a set of pads with the purpose of aggregating their buffers.
30 * Control is given to the subclass when all pads have data.
31 *
32 * * Base class for mixers and muxers. Subclasses should at least implement
33 * the #GstAggregatorClass::aggregate virtual method.
34 *
35 * * Installs a #GstPadChainFunction, a #GstPadEventFullFunction and a
36 * #GstPadQueryFunction to queue all serialized data packets per sink pad.
37 * Subclasses should not overwrite those, but instead implement
38 * #GstAggregatorClass::sink_event and #GstAggregatorClass::sink_query as
39 * needed.
40 *
41 * * When data is queued on all pads, the aggregate vmethod is called.
42 *
43 * * One can peek at the data on any given GstAggregatorPad with the
44 * gst_aggregator_pad_peek_buffer() method, and remove it from the pad
45 * with the gst_aggregator_pad_pop_buffer () method. When a buffer
46 * has been taken with pop_buffer (), a new buffer can be queued
47 * on that pad.
48 *
49 * * When gst_aggregator_pad_peek_buffer() or gst_aggregator_pad_has_buffer()
50 * are called, a reference is taken to the returned buffer, which stays
51 * valid until either:
52 *
53 * - gst_aggregator_pad_pop_buffer() is called, in which case the caller
54 * is guaranteed that the buffer they receive is the same as the peeked
55 * buffer.
56 * - gst_aggregator_pad_drop_buffer() is called, in which case the caller
57 * is guaranteed that the dropped buffer is the one that was peeked.
58 * - the subclass implementation of #GstAggregatorClass.aggregate returns.
59 *
60 * Subsequent calls to gst_aggregator_pad_peek_buffer() or
61 * gst_aggregator_pad_has_buffer() return / check the same buffer that was
62 * returned / checked, until one of the conditions listed above is met.
63 *
64 * Subclasses are only allowed to call these methods from the aggregate
65 * thread.
66 *
67 * * If the subclass wishes to push a buffer downstream in its aggregate
68 * implementation, it should do so through the
69 * gst_aggregator_finish_buffer() method. This method will take care
70 * of sending and ordering mandatory events such as stream start, caps
71 * and segment. Buffer lists can also be pushed out with
72 * gst_aggregator_finish_buffer_list().
73 *
74 * * Same goes for EOS events, which should not be pushed directly by the
75 * subclass, it should instead return GST_FLOW_EOS in its aggregate
76 * implementation.
77 *
78 * * Note that the aggregator logic regarding gap event handling is to turn
79 * these into gap buffers with matching PTS and duration. It will also
80 * flag these buffers with GST_BUFFER_FLAG_GAP and GST_BUFFER_FLAG_DROPPABLE
81 * to ease their identification and subsequent processing.
82 * In addition, if the gap event was flagged with GST_GAP_FLAG_MISSING_DATA,
83 * a custom meta is added to the resulting gap buffer (GstAggregatorMissingDataMeta).
84 *
85 * * Subclasses must use (a subclass of) #GstAggregatorPad for both their
86 * sink and source pads.
87 * See gst_element_class_add_static_pad_template_with_gtype().
88 *
89 * This class used to live in gst-plugins-bad and was moved to core.
90 *
91 * Since: 1.14
92 */
93
94 /**
95 * SECTION: gstaggregatorpad
96 * @title: GstAggregatorPad
97 * @short_description: #GstPad subclass for pads managed by #GstAggregator
98 * @see_also: gstcollectpads for historical reasons.
99 *
100 * Pads managed by a #GstAggregator subclass.
101 *
102 * This class used to live in gst-plugins-bad and was moved to core.
103 *
104 * Since: 1.14
105 */
106
107 #ifdef HAVE_CONFIG_H
108 # include "config.h"
109 #endif
110
111 #include <string.h> /* strlen */
112
113 #include "gstaggregator.h"
114
115 GType
116 gst_aggregator_start_time_selection_get_type (void)
117 {
118 static GType gtype = 0;
119
120 if (g_once_init_enter (&gtype)) {
121 static const GEnumValue values[] = {
122 {GST_AGGREGATOR_START_TIME_SELECTION_ZERO,
123 "GST_AGGREGATOR_START_TIME_SELECTION_ZERO", "zero"},
124 {GST_AGGREGATOR_START_TIME_SELECTION_FIRST,
125 "GST_AGGREGATOR_START_TIME_SELECTION_FIRST", "first"},
126 {GST_AGGREGATOR_START_TIME_SELECTION_SET,
127 "GST_AGGREGATOR_START_TIME_SELECTION_SET", "set"},
128 {GST_AGGREGATOR_START_TIME_SELECTION_NOW,
129 "GST_AGGREGATOR_START_TIME_SELECTION_NOW", "now"},
130 {0, NULL, NULL}
131 };
132 GType new_type =
133 g_enum_register_static ("GstAggregatorStartTimeSelection", values);
134
135 g_once_init_leave (&gtype, new_type);
136 }
137 return gtype;
138 }
139
140 static void gst_aggregator_set_latency_property (GstAggregator * agg,
141 GstClockTime latency);
142 static GstClockTime gst_aggregator_get_latency_property (GstAggregator * agg);
143
144 static GstClockTime gst_aggregator_get_latency_unlocked (GstAggregator * self);
145
146 static void gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad,
147 GstBuffer * buffer, gboolean dequeued);
148
149 GST_DEBUG_CATEGORY_STATIC (aggregator_debug);
150 #define GST_CAT_DEFAULT aggregator_debug
151
152 /* Locking order, locks in this element must always be taken in this order
153 *
154 * standard sink pad stream lock -> GST_PAD_STREAM_LOCK (aggpad)
155 * Aggregator pad flush lock -> PAD_FLUSH_LOCK(aggpad)
156 * standard src pad stream lock -> GST_PAD_STREAM_LOCK (srcpad)
157 * Aggregator src lock -> SRC_LOCK(agg) w/ SRC_WAIT/BROADCAST
158 * standard element object lock -> GST_OBJECT_LOCK(agg)
159 * Aggregator pad lock -> PAD_LOCK (aggpad) w/ PAD_WAIT/BROADCAST_EVENT(aggpad)
160 * standard src pad object lock -> GST_OBJECT_LOCK(srcpad)
161 * standard sink pad object lock -> GST_OBJECT_LOCK(aggpad)
162 */
163
164 /* GstAggregatorPad definitions */
165 #define PAD_LOCK(pad) G_STMT_START { \
166 GST_TRACE_OBJECT (pad, "Taking PAD lock from thread %p", \
167 g_thread_self()); \
168 g_mutex_lock(&pad->priv->lock); \
169 GST_TRACE_OBJECT (pad, "Took PAD lock from thread %p", \
170 g_thread_self()); \
171 } G_STMT_END
172
173 #define PAD_UNLOCK(pad) G_STMT_START { \
174 GST_TRACE_OBJECT (pad, "Releasing PAD lock from thread %p", \
175 g_thread_self()); \
176 g_mutex_unlock(&pad->priv->lock); \
177 GST_TRACE_OBJECT (pad, "Release PAD lock from thread %p", \
178 g_thread_self()); \
179 } G_STMT_END
180
181
182 #define PAD_WAIT_EVENT(pad) G_STMT_START { \
183 GST_LOG_OBJECT (pad, "Waiting for buffer to be consumed thread %p", \
184 g_thread_self()); \
185 g_cond_wait(&(((GstAggregatorPad* )pad)->priv->event_cond), \
186 (&((GstAggregatorPad*)pad)->priv->lock)); \
187 GST_LOG_OBJECT (pad, "DONE Waiting for buffer to be consumed on thread %p", \
188 g_thread_self()); \
189 } G_STMT_END
190
191 #define PAD_BROADCAST_EVENT(pad) G_STMT_START { \
192 GST_LOG_OBJECT (pad, "Signaling buffer consumed from thread %p", \
193 g_thread_self()); \
194 g_cond_broadcast(&(((GstAggregatorPad* )pad)->priv->event_cond)); \
195 } G_STMT_END
196
197
198 #define PAD_FLUSH_LOCK(pad) G_STMT_START { \
199 GST_TRACE_OBJECT (pad, "Taking lock from thread %p", \
200 g_thread_self()); \
201 g_mutex_lock(&pad->priv->flush_lock); \
202 GST_TRACE_OBJECT (pad, "Took lock from thread %p", \
203 g_thread_self()); \
204 } G_STMT_END
205
206 #define PAD_FLUSH_UNLOCK(pad) G_STMT_START { \
207 GST_TRACE_OBJECT (pad, "Releasing lock from thread %p", \
208 g_thread_self()); \
209 g_mutex_unlock(&pad->priv->flush_lock); \
210 GST_TRACE_OBJECT (pad, "Release lock from thread %p", \
211 g_thread_self()); \
212 } G_STMT_END
213
214 #define SRC_LOCK(self) G_STMT_START { \
215 GST_TRACE_OBJECT (self, "Taking src lock from thread %p", \
216 g_thread_self()); \
217 g_mutex_lock(&self->priv->src_lock); \
218 GST_TRACE_OBJECT (self, "Took src lock from thread %p", \
219 g_thread_self()); \
220 } G_STMT_END
221
222 #define SRC_UNLOCK(self) G_STMT_START { \
223 GST_TRACE_OBJECT (self, "Releasing src lock from thread %p", \
224 g_thread_self()); \
225 g_mutex_unlock(&self->priv->src_lock); \
226 GST_TRACE_OBJECT (self, "Released src lock from thread %p", \
227 g_thread_self()); \
228 } G_STMT_END
229
230 #define SRC_WAIT(self) G_STMT_START { \
231 GST_LOG_OBJECT (self, "Waiting for src on thread %p", \
232 g_thread_self()); \
233 g_cond_wait(&(self->priv->src_cond), &(self->priv->src_lock)); \
234 GST_LOG_OBJECT (self, "DONE Waiting for src on thread %p", \
235 g_thread_self()); \
236 } G_STMT_END
237
238 #define SRC_BROADCAST(self) G_STMT_START { \
239 GST_LOG_OBJECT (self, "Signaling src from thread %p", \
240 g_thread_self()); \
241 if (self->priv->aggregate_id) \
242 gst_clock_id_unschedule (self->priv->aggregate_id); \
243 g_cond_broadcast(&(self->priv->src_cond)); \
244 } G_STMT_END
245
246 struct _GstAggregatorPadPrivate
247 {
248 /* Following fields are protected by the PAD_LOCK */
249 GstFlowReturn flow_return;
250
251 guint32 last_flush_start_seqnum;
252 guint32 last_flush_stop_seqnum;
253
254 /* Whether the pad hasn't received a first buffer yet */
255 gboolean first_buffer;
256 /* Whether we waited once for the pad's first buffer */
257 gboolean waited_once;
258
259 GQueue data; /* buffers, events and queries */
260 GstBuffer *clipped_buffer;
261 guint num_buffers;
262 guint64 num_bytes;
263 GstBuffer *peeked_buffer;
264
265 /* TRUE if the serialized query is in the proccess of handling at some
266 * exact moment. This will obligate the sinkpad streaming thread wait
267 * until the handling finishes.
268 * Always protected by the PAD_LOCK. */
269 gboolean query_in_proccess;
270
271 /* used to track fill state of queues, only used with live-src and when
272 * latency property is set to > 0 */
273 GstClockTime head_position;
274 GstClockTime tail_position;
275 GstClockTime head_time; /* running time */
276 GstClockTime tail_time;
277 GstClockTime time_level; /* how much head is ahead of tail */
278 GstSegment head_segment; /* segment before the queue */
279
280 gboolean negotiated;
281
282 gboolean eos;
283
284 /* number of queued stream-start */
285 gboolean stream_start_pending;
286
287 GMutex lock;
288 GCond event_cond;
289 /* This lock prevents a flush start processing happening while
290 * the chain function is also happening.
291 */
292 GMutex flush_lock;
293
294 /* properties */
295 gboolean emit_signals;
296 };
297
298 /* Must be called with PAD_LOCK held */
299 static void
300 gst_aggregator_pad_reset_unlocked (GstAggregatorPad * aggpad)
301 {
302 aggpad->priv->eos = FALSE;
303 aggpad->priv->flow_return = GST_FLOW_OK;
304 GST_OBJECT_LOCK (aggpad);
305 gst_segment_init (&aggpad->segment, GST_FORMAT_UNDEFINED);
306 gst_segment_init (&aggpad->priv->head_segment, GST_FORMAT_UNDEFINED);
307 GST_OBJECT_UNLOCK (aggpad);
308 aggpad->priv->head_position = GST_CLOCK_TIME_NONE;
309 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
310 aggpad->priv->head_time = GST_CLOCK_TIME_NONE;
311 aggpad->priv->tail_time = GST_CLOCK_TIME_NONE;
312 aggpad->priv->time_level = 0;
313 aggpad->priv->first_buffer = TRUE;
314 aggpad->priv->waited_once = FALSE;
315 aggpad->priv->stream_start_pending = FALSE;
316 }
317
318 static gboolean
319 gst_aggregator_pad_flush (GstAggregatorPad * aggpad, GstAggregator * agg)
320 {
321 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
322
323 PAD_LOCK (aggpad);
324 gst_aggregator_pad_reset_unlocked (aggpad);
325 PAD_UNLOCK (aggpad);
326
327 if (klass->flush)
328 return (klass->flush (aggpad, agg) == GST_FLOW_OK);
329
330 return TRUE;
331 }
332
333 /**
334 * gst_aggregator_peek_next_sample:
335 *
336 * Use this function to determine what input buffers will be aggregated
337 * to produce the next output buffer. This should only be called from
338 * a #GstAggregator::samples-selected handler, and can be used to precisely
339 * control aggregating parameters for a given set of input samples.
340 *
341 * Returns: (nullable) (transfer full): The sample that is about to be aggregated. It may hold a #GstBuffer
342 * or a #GstBufferList. The contents of its info structure is subclass-dependent,
343 * and documented on a subclass basis. The buffers held by the sample are
344 * not writable.
345 * Since: 1.18
346 */
347 GstSample *
348 gst_aggregator_peek_next_sample (GstAggregator * agg, GstAggregatorPad * aggpad)
349 {
350 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (agg);
351
352 if (klass->peek_next_sample)
353 return (klass->peek_next_sample (agg, aggpad));
354
355 return NULL;
356 }
357
358 /*************************************
359 * GstAggregator implementation *
360 *************************************/
361 static GstElementClass *aggregator_parent_class = NULL;
362 static gint aggregator_private_offset = 0;
363
364 /* All members are protected by the object lock unless otherwise noted */
365
366 struct _GstAggregatorPrivate
367 {
368 gint max_padserial;
369
370 /* Our state is >= PAUSED */
371 gboolean running; /* protected by src_lock */
372 /* Our state is >= PAUSED but < PLAYING */
373 gboolean blocked; /* protected by src_lock */
374
375 /* seqnum from last seek or common seqnum to flush start events received
376 * on all pads, for flushing without a seek */
377 guint32 next_seqnum;
378 /* seqnum to apply to synthetic segment/eos events */
379 guint32 seqnum;
380 gboolean send_stream_start; /* protected by srcpad stream lock */
381 gboolean send_segment;
382 gboolean flushing;
383 gboolean send_eos; /* protected by srcpad stream lock */
384 gboolean got_eos_event; /* protected by srcpad stream lock */
385
386 GstCaps *srccaps; /* protected by the srcpad stream lock */
387
388 gboolean peer_latency_live; /* protected by src_lock */
389 GstClockTime peer_latency_min; /* protected by src_lock */
390 GstClockTime peer_latency_max; /* protected by src_lock */
391 gboolean has_peer_latency; /* protected by src_lock */
392
393 GstClockTime sub_latency_min; /* protected by src_lock */
394 GstClockTime sub_latency_max; /* protected by src_lock */
395 /* Tracks whether the latency message was posted at least once */
396 gboolean posted_latency_msg;
397
398 GstClockTime upstream_latency_min; /* protected by src_lock */
399
400 /* aggregate */
401 GstClockID aggregate_id; /* protected by src_lock */
402 gboolean selected_samples_called_or_warned; /* protected by src_lock */
403 GMutex src_lock;
404 GCond src_cond;
405
406 gboolean first_buffer; /* protected by object lock */
407 GstAggregatorStartTimeSelection start_time_selection;
408 GstClockTime start_time;
409
410 /* protected by the object lock */
411 GstQuery *allocation_query;
412 GstAllocator *allocator;
413 GstBufferPool *pool;
414 GstAllocationParams allocation_params;
415
416 /* properties */
417 gint64 latency; /* protected by both src_lock and all pad locks */
418 gboolean emit_signals;
419 gboolean ignore_inactive_pads;
420 gboolean force_live; /* Construct only, doesn't need any locking */
421 };
422
423 /* With SRC_LOCK */
424 static gboolean
425 is_live_unlocked (GstAggregator * self)
426 {
427 return self->priv->peer_latency_live || self->priv->force_live;
428 }
429
430 /* Seek event forwarding helper */
431 typedef struct
432 {
433 /* parameters */
434 GstEvent *event;
435 gboolean flush;
436 gboolean only_to_active_pads;
437
438 /* results */
439 gboolean result;
440 gboolean one_actually_seeked;
441 } EventData;
442
443 #define DEFAULT_LATENCY 0
444 #define DEFAULT_MIN_UPSTREAM_LATENCY 0
445 #define DEFAULT_START_TIME_SELECTION GST_AGGREGATOR_START_TIME_SELECTION_ZERO
446 #define DEFAULT_START_TIME (-1)
447 #define DEFAULT_EMIT_SIGNALS FALSE
448 #define DEFAULT_FORCE_LIVE FALSE
449
450 enum
451 {
452 PROP_0,
453 PROP_LATENCY,
454 PROP_MIN_UPSTREAM_LATENCY,
455 PROP_START_TIME_SELECTION,
456 PROP_START_TIME,
457 PROP_EMIT_SIGNALS,
458 PROP_LAST
459 };
460
461 enum
462 {
463 SIGNAL_SAMPLES_SELECTED,
464 LAST_SIGNAL,
465 };
466
467 static guint gst_aggregator_signals[LAST_SIGNAL] = { 0 };
468
469 static GstFlowReturn gst_aggregator_pad_chain_internal (GstAggregator * self,
470 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head);
471
472 static gboolean
473 gst_aggregator_pad_queue_is_empty (GstAggregatorPad * pad)
474 {
475 return (g_queue_peek_tail (&pad->priv->data) == NULL &&
476 pad->priv->clipped_buffer == NULL);
477 }
478
479 /* Will return FALSE if there's no buffer available on every non-EOS pad, or
480 * if at least one of the pads has an event or query at the top of its queue.
481 *
482 * Only returns TRUE if all non-EOS pads have a buffer available at the top of
483 * their queue or a clipped buffer already.
484 */
485 static gboolean
486 gst_aggregator_check_pads_ready (GstAggregator * self,
487 gboolean * have_event_or_query_ret)
488 {
489 GstAggregatorPad *pad = NULL;
490 GList *l, *sinkpads;
491 gboolean have_buffer = TRUE;
492 gboolean have_event_or_query = FALSE;
493 guint n_ready = 0;
494
495 GST_LOG_OBJECT (self, "checking pads");
496
497 GST_OBJECT_LOCK (self);
498
499 sinkpads = GST_ELEMENT_CAST (self)->sinkpads;
500 if (sinkpads == NULL)
501 goto no_sinkpads;
502
503 for (l = sinkpads; l != NULL; l = l->next) {
504 pad = l->data;
505
506 PAD_LOCK (pad);
507
508 /* If there's an event or query at the top of the queue and we don't yet
509 * have taken the top buffer out and stored it as clip_buffer, remember
510 * that and exit the loop. We first have to handle all events/queries
511 * before we handle any buffers. */
512 if (!pad->priv->clipped_buffer
513 && (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data))
514 || GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data)))) {
515 PAD_UNLOCK (pad);
516 have_event_or_query = TRUE;
517 GST_LOG_OBJECT (pad, "Have pending serialized query/event");
518 break;
519 }
520
521 if (self->priv->ignore_inactive_pads && is_live_unlocked (self) &&
522 pad->priv->waited_once && pad->priv->first_buffer && !pad->priv->eos) {
523 PAD_UNLOCK (pad);
524 GST_LOG_OBJECT (pad, "Ignoring inactive pad");
525 continue;
526 }
527
528 /* Otherwise check if we have a clipped buffer or a buffer at the top of
529 * the queue, and if not then this pad is not ready unless it is also EOS */
530 if (!pad->priv->clipped_buffer
531 && !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
532 /* We must not have any buffers at all in this pad then as otherwise we
533 * would've had an event/query at the top of the queue */
534 g_assert (pad->priv->num_buffers == 0);
535 g_assert (pad->priv->num_bytes == 0);
536
537 /* Only consider this pad as worth waiting for if it's not already EOS.
538 * There's no point in waiting for buffers on EOS pads */
539 if (!pad->priv->eos) {
540 GST_LOG_OBJECT (pad, "Have no buffer and not EOS yet");
541 have_buffer = FALSE;
542 } else {
543 GST_LOG_OBJECT (pad, "Have no buffer and already EOS");
544 n_ready++;
545 }
546 } else {
547 GST_TRACE_OBJECT (pad,
548 "Have %" GST_TIME_FORMAT " queued in %u buffers (%" G_GUINT64_FORMAT
549 " bytes)", GST_TIME_ARGS (pad->priv->time_level),
550 pad->priv->num_buffers, pad->priv->num_bytes);
551 if (is_live_unlocked (self)) {
552 /* In live mode, having a single pad with buffers is enough to
553 * generate a start time from it. In non-live mode all pads need
554 * to have a buffer
555 */
556 self->priv->first_buffer = FALSE;
557 n_ready++;
558 }
559 }
560
561 PAD_UNLOCK (pad);
562 }
563
564 if (self->priv->ignore_inactive_pads && is_live_unlocked (self)
565 && n_ready == 0)
566 goto no_sinkpads;
567
568 if (have_event_or_query)
569 goto pad_not_ready_but_event_or_query;
570
571 if (!have_buffer)
572 goto pad_not_ready;
573
574 if (have_buffer)
575 self->priv->first_buffer = FALSE;
576
577 GST_OBJECT_UNLOCK (self);
578 GST_LOG_OBJECT (self, "pads are ready");
579
580 if (have_event_or_query_ret)
581 *have_event_or_query_ret = have_event_or_query;
582
583 return TRUE;
584
585 no_sinkpads:
586 {
587 GST_LOG_OBJECT (self, "pads not ready: no sink pads");
588 GST_OBJECT_UNLOCK (self);
589
590 if (have_event_or_query_ret)
591 *have_event_or_query_ret = have_event_or_query;
592
593 return FALSE;
594 }
595 pad_not_ready:
596 {
597 GST_LOG_OBJECT (self, "pad not ready to be aggregated yet");
598 GST_OBJECT_UNLOCK (self);
599
600 if (have_event_or_query_ret)
601 *have_event_or_query_ret = have_event_or_query;
602
603 return FALSE;
604 }
605 pad_not_ready_but_event_or_query:
606 {
607 GST_LOG_OBJECT (self,
608 "pad not ready to be aggregated yet, need to handle serialized event or query first");
609 GST_OBJECT_UNLOCK (self);
610
611 if (have_event_or_query_ret)
612 *have_event_or_query_ret = have_event_or_query;
613
614 return FALSE;
615 }
616 }
617
618 static void
619 gst_aggregator_reset_flow_values (GstAggregator * self)
620 {
621 GST_OBJECT_LOCK (self);
622 self->priv->send_stream_start = TRUE;
623 self->priv->send_segment = TRUE;
624 gst_segment_init (&GST_AGGREGATOR_PAD (self->srcpad)->segment,
625 GST_FORMAT_TIME);
626 /* Initialize to -1 so we set it to the start position once the first buffer
627 * is handled in gst_aggregator_pad_chain_internal() */
628 GST_AGGREGATOR_PAD (self->srcpad)->segment.position = -1;
629 self->priv->first_buffer = TRUE;
630 GST_OBJECT_UNLOCK (self);
631 }
632
633 static inline void
634 gst_aggregator_push_mandatory_events (GstAggregator * self, gboolean up_to_caps)
635 {
636 GstEvent *segment = NULL;
637
638 if (self->priv->send_stream_start) {
639 gchar s_id[32];
640
641 GST_INFO_OBJECT (self, "pushing stream start");
642 /* stream-start (FIXME: create id based on input ids) */
643 g_snprintf (s_id, sizeof (s_id), "agg-%08x", g_random_int ());
644 if (!gst_pad_push_event (GST_PAD (self->srcpad),
645 gst_event_new_stream_start (s_id))) {
646 GST_WARNING_OBJECT (self->srcpad, "Sending stream start event failed");
647 }
648 self->priv->send_stream_start = FALSE;
649 }
650
651 if (self->priv->srccaps) {
652 GST_INFO_OBJECT (self, "pushing caps: %" GST_PTR_FORMAT,
653 self->priv->srccaps);
654 if (!gst_pad_push_event (GST_PAD (self->srcpad),
655 gst_event_new_caps (self->priv->srccaps))) {
656 GST_WARNING_OBJECT (self->srcpad, "Sending caps event failed");
657 }
658 gst_caps_unref (self->priv->srccaps);
659 self->priv->srccaps = NULL;
660 }
661
662 if (up_to_caps)
663 return;
664
665 GST_OBJECT_LOCK (self);
666 if (self->priv->send_segment && !self->priv->flushing) {
667 segment =
668 gst_event_new_segment (&GST_AGGREGATOR_PAD (self->srcpad)->segment);
669
670 if (!self->priv->seqnum)
671 /* This code-path is in preparation to be able to run without a source
672 * connected. Then we won't have a seq-num from a segment event. */
673 self->priv->seqnum = gst_event_get_seqnum (segment);
674 else
675 gst_event_set_seqnum (segment, self->priv->seqnum);
676 self->priv->send_segment = FALSE;
677
678 GST_DEBUG_OBJECT (self, "pushing segment %" GST_PTR_FORMAT, segment);
679 }
680 GST_OBJECT_UNLOCK (self);
681
682 if (segment)
683 gst_pad_push_event (self->srcpad, segment);
684 }
685
686 /**
687 * gst_aggregator_set_src_caps:
688 * @self: The #GstAggregator
689 * @caps: The #GstCaps to set on the src pad.
690 *
691 * Sets the caps to be used on the src pad.
692 */
693 void
694 gst_aggregator_set_src_caps (GstAggregator * self, GstCaps * caps)
695 {
696 GstCaps *old_caps;
697
698 GST_PAD_STREAM_LOCK (self->srcpad);
699
700 if (caps && (old_caps = gst_pad_get_current_caps (self->srcpad))) {
701 if (gst_caps_is_equal (caps, old_caps)) {
702 GST_DEBUG_OBJECT (self,
703 "New caps are the same as the previously set caps %" GST_PTR_FORMAT,
704 old_caps);
705 gst_caps_unref (old_caps);
706 GST_PAD_STREAM_UNLOCK (self->srcpad);
707 return;
708 }
709 gst_caps_unref (old_caps);
710 }
711
712 gst_caps_replace (&self->priv->srccaps, caps);
713 gst_aggregator_push_mandatory_events (self, TRUE);
714 GST_PAD_STREAM_UNLOCK (self->srcpad);
715 }
716
717 static GstFlowReturn
718 gst_aggregator_default_finish_buffer (GstAggregator * self, GstBuffer * buffer)
719 {
720 gst_aggregator_push_mandatory_events (self, FALSE);
721
722 GST_OBJECT_LOCK (self);
723 if (!self->priv->flushing && gst_pad_is_active (self->srcpad)) {
724 GST_TRACE_OBJECT (self, "pushing buffer %" GST_PTR_FORMAT, buffer);
725 GST_OBJECT_UNLOCK (self);
726 return gst_pad_push (self->srcpad, buffer);
727 } else {
728 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
729 gst_pad_is_active (self->srcpad), self->priv->flushing);
730 GST_OBJECT_UNLOCK (self);
731 gst_buffer_unref (buffer);
732 return GST_FLOW_OK;
733 }
734 }
735
736 /**
737 * gst_aggregator_finish_buffer:
738 * @aggregator: The #GstAggregator
739 * @buffer: (transfer full): the #GstBuffer to push.
740 *
741 * This method will push the provided output buffer downstream. If needed,
742 * mandatory events such as stream-start, caps, and segment events will be
743 * sent before pushing the buffer.
744 */
745 GstFlowReturn
746 gst_aggregator_finish_buffer (GstAggregator * aggregator, GstBuffer * buffer)
747 {
748 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (aggregator);
749
750 g_assert (klass->finish_buffer != NULL);
751
752 return klass->finish_buffer (aggregator, buffer);
753 }
754
755 static GstFlowReturn
756 gst_aggregator_default_finish_buffer_list (GstAggregator * self,
757 GstBufferList * bufferlist)
758 {
759 gst_aggregator_push_mandatory_events (self, FALSE);
760
761 GST_OBJECT_LOCK (self);
762 if (!self->priv->flushing && gst_pad_is_active (self->srcpad)) {
763 GST_TRACE_OBJECT (self, "pushing bufferlist%" GST_PTR_FORMAT, bufferlist);
764 GST_OBJECT_UNLOCK (self);
765 return gst_pad_push_list (self->srcpad, bufferlist);
766 } else {
767 GST_INFO_OBJECT (self, "Not pushing (active: %i, flushing: %i)",
768 gst_pad_is_active (self->srcpad), self->priv->flushing);
769 GST_OBJECT_UNLOCK (self);
770 gst_buffer_list_unref (bufferlist);
771 return GST_FLOW_OK;
772 }
773 }
774
775 /**
776 * gst_aggregator_finish_buffer_list:
777 * @aggregator: The #GstAggregator
778 * @bufferlist: (transfer full): the #GstBufferList to push.
779 *
780 * This method will push the provided output buffer list downstream. If needed,
781 * mandatory events such as stream-start, caps, and segment events will be
782 * sent before pushing the buffer.
783 *
784 * Since: 1.18
785 */
786 GstFlowReturn
787 gst_aggregator_finish_buffer_list (GstAggregator * aggregator,
788 GstBufferList * bufferlist)
789 {
790 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (aggregator);
791
792 g_assert (klass->finish_buffer_list != NULL);
793
794 return klass->finish_buffer_list (aggregator, bufferlist);
795 }
796
797 /**
798 * gst_aggregator_push_src_event:
799 * @aggregator: The #GstAggregator
800 * @event: (transfer full): the #GstEvent to push.
801 *
802 * This method will push the provided event downstream. If needed, mandatory
803 * events such as stream-start, caps, and segment events will be sent before
804 * pushing the event.
805 *
806 * This API does not allow pushing stream-start, caps, segment and EOS events.
807 * Specific API like gst_aggregator_set_src_caps() should be used for these.
808 *
809 * Since: 1.26
810 */
811 gboolean
812 gst_aggregator_push_src_event (GstAggregator * aggregator, GstEvent * event)
813 {
814 g_return_val_if_fail (GST_EVENT_IS_DOWNSTREAM (event), FALSE);
815 g_return_val_if_fail (GST_EVENT_TYPE (event) != GST_EVENT_STREAM_START &&
816 GST_EVENT_TYPE (event) != GST_EVENT_CAPS &&
817 GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT &&
818 GST_EVENT_TYPE (event) != GST_EVENT_EOS, FALSE);
819
820 if (GST_EVENT_IS_SERIALIZED (event)) {
821 gst_aggregator_push_mandatory_events (aggregator, FALSE);
822 }
823
824 return gst_pad_push_event (aggregator->srcpad, event);
825 }
826
827 static void
828 gst_aggregator_push_eos (GstAggregator * self)
829 {
830 GstEvent *event;
831 gst_aggregator_push_mandatory_events (self, FALSE);
832
833 event = gst_event_new_eos ();
834
835 GST_OBJECT_LOCK (self);
836 self->priv->send_eos = FALSE;
837 if (self->priv->seqnum != GST_SEQNUM_INVALID)
838 gst_event_set_seqnum (event, self->priv->seqnum);
839 GST_OBJECT_UNLOCK (self);
840
841 gst_pad_push_event (self->srcpad, event);
842 }
843
844 static GstClockTime
845 gst_aggregator_get_next_time (GstAggregator * self)
846 {
847 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
848
849 if (klass->get_next_time)
850 return klass->get_next_time (self);
851
852 return GST_CLOCK_TIME_NONE;
853 }
854
855 static gboolean
856 gst_aggregator_wait_and_check (GstAggregator * self, gboolean * timeout)
857 {
858 GstClockTime latency;
859 GstClockTime start;
860 gboolean res;
861 gboolean have_event_or_query = FALSE;
862
863 *timeout = FALSE;
864
865 SRC_LOCK (self);
866
867 latency = gst_aggregator_get_latency_unlocked (self);
868
869 if (gst_aggregator_check_pads_ready (self, &have_event_or_query)) {
870 GST_DEBUG_OBJECT (self, "all pads have data");
871 SRC_UNLOCK (self);
872
873 return TRUE;
874 }
875
876 /* If we have an event or query, immediately return FALSE instead of waiting
877 * and handle it immediately */
878 if (have_event_or_query) {
879 GST_DEBUG_OBJECT (self, "Have serialized event or query to handle first");
880 SRC_UNLOCK (self);
881 return FALSE;
882 }
883
884 /* Before waiting, check if we're actually still running */
885 if (!self->priv->running || !self->priv->send_eos) {
886 SRC_UNLOCK (self);
887
888 return FALSE;
889 }
890
891 /* Try to select start time if we're PLAYING */
892 if (!self->priv->blocked && (
893 (self->priv->force_live && self->priv->first_buffer
894 && self->priv->start_time_selection ==
895 GST_AGGREGATOR_START_TIME_SELECTION_FIRST) ||
896 (self->priv->start_time_selection ==
897 GST_AGGREGATOR_START_TIME_SELECTION_NOW
898 && self->priv->first_buffer))) {
899 GstClockTime start_time;
900 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (self->srcpad);
901 start_time = gst_element_get_current_running_time (GST_ELEMENT (self));
902
903 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
904 if (srcpad->segment.position == -1)
905 srcpad->segment.position = start_time;
906 else
907 srcpad->segment.position = MIN (start_time, srcpad->segment.position);
908
909 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
910 GST_TIME_ARGS (start_time));
911 self->priv->first_buffer = FALSE;
912 }
913 }
914
915
916 start = gst_aggregator_get_next_time (self);
917
918 /* If we're not live, or if we use the running time
919 * of the first buffer as start time, we wait until
920 * all pads have buffers.
921 * Otherwise (i.e. if we are live!), we wait on the clock
922 * and if a pad does not have a buffer in time we ignore
923 * that pad.
924 */
925 GST_OBJECT_LOCK (self);
926 if (!GST_CLOCK_TIME_IS_VALID (latency) || /* not live */
927 self->priv->blocked || /* live but not PLAYING yet */
928 !GST_IS_CLOCK (GST_ELEMENT_CLOCK (self)) ||
929 !GST_CLOCK_TIME_IS_VALID (start) ||
930 (self->priv->first_buffer
931 && self->priv->start_time_selection ==
932 GST_AGGREGATOR_START_TIME_SELECTION_FIRST)) {
933 /* We wake up here when something happened, and below
934 * then check if we're ready now. If we return FALSE,
935 * we will be directly called again.
936 */
937 GST_OBJECT_UNLOCK (self);
938 SRC_WAIT (self);
939
940 /* After waiting, check if we're actually still running */
941 if (!self->priv->running || !self->priv->send_eos) {
942 SRC_UNLOCK (self);
943
944 return FALSE;
945 }
946 } else {
947 GstClockTime base_time, time;
948 GstClock *clock;
949 GstClockReturn status;
950 GstClockTimeDiff jitter;
951
952 GST_DEBUG_OBJECT (self, "got subclass start time: %" GST_TIME_FORMAT,
953 GST_TIME_ARGS (start));
954
955 base_time = GST_ELEMENT_CAST (self)->base_time;
956 clock = gst_object_ref (GST_ELEMENT_CLOCK (self));
957 GST_OBJECT_UNLOCK (self);
958
959 time = base_time + start;
960 time += latency;
961
962 GST_DEBUG_OBJECT (self, "possibly waiting for clock to reach %"
963 GST_TIME_FORMAT " (base %" GST_TIME_FORMAT " start %" GST_TIME_FORMAT
964 " latency %" GST_TIME_FORMAT " current %" GST_TIME_FORMAT ")",
965 GST_TIME_ARGS (time),
966 GST_TIME_ARGS (base_time),
967 GST_TIME_ARGS (start), GST_TIME_ARGS (latency),
968 GST_TIME_ARGS (gst_clock_get_time (clock)));
969
970 self->priv->aggregate_id = gst_clock_new_single_shot_id (clock, time);
971 gst_object_unref (clock);
972 SRC_UNLOCK (self);
973
974 jitter = 0;
975 status = gst_clock_id_wait (self->priv->aggregate_id, &jitter);
976
977 SRC_LOCK (self);
978 if (self->priv->aggregate_id) {
979 gst_clock_id_unref (self->priv->aggregate_id);
980 self->priv->aggregate_id = NULL;
981 }
982
983 GST_DEBUG_OBJECT (self,
984 "clock returned %d (jitter: %" GST_STIME_FORMAT ")",
985 status, GST_STIME_ARGS (jitter));
986
987 /* After waiting, check if we're actually still running */
988 if (!self->priv->running || !self->priv->send_eos || self->priv->blocked) {
989 SRC_UNLOCK (self);
990
991 return FALSE;
992 }
993
994 /* we timed out */
995 if (status == GST_CLOCK_OK || status == GST_CLOCK_EARLY) {
996 GList *l;
997
998 GST_OBJECT_LOCK (self);
999 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
1000 GstAggregatorPad *pad = GST_AGGREGATOR_PAD (l->data);
1001
1002 PAD_LOCK (pad);
1003 pad->priv->waited_once = TRUE;
1004 PAD_UNLOCK (pad);
1005 }
1006 GST_OBJECT_UNLOCK (self);
1007
1008 SRC_UNLOCK (self);
1009 *timeout = TRUE;
1010 return TRUE;
1011 }
1012 }
1013
1014 res = gst_aggregator_check_pads_ready (self, NULL);
1015 SRC_UNLOCK (self);
1016
1017 return res;
1018 }
1019
1020 typedef struct
1021 {
1022 gboolean processed_event;
1023 GstFlowReturn flow_ret;
1024 } DoHandleEventsAndQueriesData;
1025
1026 static gboolean
1027 gst_aggregator_do_events_and_queries (GstElement * self, GstPad * epad,
1028 gpointer user_data)
1029 {
1030 GstAggregatorPad *pad = GST_AGGREGATOR_PAD_CAST (epad);
1031 GstAggregator *aggregator = GST_AGGREGATOR_CAST (self);
1032 GstEvent *event = NULL;
1033 GstQuery *query = NULL;
1034 GstAggregatorClass *klass = NULL;
1035 DoHandleEventsAndQueriesData *data = user_data;
1036
1037 do {
1038 event = NULL;
1039 query = NULL;
1040
1041 PAD_LOCK (pad);
1042 if (pad->priv->clipped_buffer == NULL &&
1043 !GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
1044 if (GST_IS_EVENT (g_queue_peek_tail (&pad->priv->data)))
1045 event = gst_event_ref (g_queue_peek_tail (&pad->priv->data));
1046 if (GST_IS_QUERY (g_queue_peek_tail (&pad->priv->data))) {
1047 query = g_queue_peek_tail (&pad->priv->data);
1048 pad->priv->query_in_proccess = TRUE;
1049 }
1050 }
1051 PAD_UNLOCK (pad);
1052 if (event || query) {
1053 gboolean ret;
1054
1055 data->processed_event = TRUE;
1056 if (klass == NULL)
1057 klass = GST_AGGREGATOR_GET_CLASS (self);
1058
1059 if (event) {
1060 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, event);
1061 gst_event_ref (event);
1062 ret = klass->sink_event (aggregator, pad, event);
1063
1064 PAD_LOCK (pad);
1065 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
1066 pad->priv->negotiated = ret;
1067 }
1068 if (g_queue_peek_tail (&pad->priv->data) == event)
1069 gst_event_unref (g_queue_pop_tail (&pad->priv->data));
1070 gst_event_unref (event);
1071 } else if (query) {
1072 GST_LOG_OBJECT (pad, "Processing %" GST_PTR_FORMAT, query);
1073 ret = klass->sink_query (aggregator, pad, query);
1074
1075 PAD_LOCK (pad);
1076 if (g_queue_peek_tail (&pad->priv->data) == query) {
1077 GstStructure *s;
1078
1079 s = gst_query_writable_structure (query);
1080 gst_structure_set (s, "gst-aggregator-retval", G_TYPE_BOOLEAN, ret,
1081 NULL);
1082 g_queue_pop_tail (&pad->priv->data);
1083 }
1084
1085 pad->priv->query_in_proccess = FALSE;
1086 }
1087
1088 PAD_BROADCAST_EVENT (pad);
1089 PAD_UNLOCK (pad);
1090 }
1091 } while (event || query);
1092
1093 return TRUE;
1094 }
1095
1096 static gboolean
1097 gst_aggregator_pad_skip_buffers (GstElement * self, GstPad * epad,
1098 gpointer user_data)
1099 {
1100 GList *item;
1101 GstAggregatorPad *aggpad = (GstAggregatorPad *) epad;
1102 GstAggregator *agg = (GstAggregator *) self;
1103 GstAggregatorPadClass *klass = GST_AGGREGATOR_PAD_GET_CLASS (aggpad);
1104
1105 if (!klass->skip_buffer)
1106 return FALSE;
1107
1108 PAD_LOCK (aggpad);
1109
1110 item = g_queue_peek_tail_link (&aggpad->priv->data);
1111 while (item) {
1112 GList *prev = item->prev;
1113
1114 if (GST_IS_BUFFER (item->data)
1115 && klass->skip_buffer (aggpad, agg, item->data)) {
1116 GST_LOG_OBJECT (aggpad, "Skipping %" GST_PTR_FORMAT, item->data);
1117 gst_aggregator_pad_buffer_consumed (aggpad, GST_BUFFER (item->data),
1118 TRUE);
1119 gst_buffer_unref (item->data);
1120 g_queue_delete_link (&aggpad->priv->data, item);
1121 } else {
1122 break;
1123 }
1124
1125 item = prev;
1126 }
1127
1128 PAD_UNLOCK (aggpad);
1129
1130 return TRUE;
1131 }
1132
1133 static gboolean
1134 gst_aggregator_pad_reset_peeked_buffer (GstElement * self, GstPad * epad,
1135 gpointer user_data)
1136 {
1137 GstAggregatorPad *aggpad = (GstAggregatorPad *) epad;
1138
1139 PAD_LOCK (aggpad);
1140
1141 gst_buffer_replace (&aggpad->priv->peeked_buffer, NULL);
1142
1143 PAD_UNLOCK (aggpad);
1144
1145 return TRUE;
1146 }
1147
1148
1149 static void
1150 gst_aggregator_pad_set_flushing (GstAggregatorPad * aggpad,
1151 GstFlowReturn flow_return, gboolean full)
1152 {
1153 GList *item;
1154
1155 PAD_LOCK (aggpad);
1156 if (flow_return == GST_FLOW_NOT_LINKED)
1157 aggpad->priv->flow_return = MIN (flow_return, aggpad->priv->flow_return);
1158 else
1159 aggpad->priv->flow_return = flow_return;
1160
1161 item = g_queue_peek_head_link (&aggpad->priv->data);
1162 while (item) {
1163 GList *next = item->next;
1164
1165 /* In partial flush, we do like the pad, we get rid of non-sticky events
1166 * and EOS/SEGMENT.
1167 */
1168 if (full || GST_IS_BUFFER (item->data) ||
1169 GST_EVENT_TYPE (item->data) == GST_EVENT_EOS ||
1170 GST_EVENT_TYPE (item->data) == GST_EVENT_SEGMENT ||
1171 !GST_EVENT_IS_STICKY (item->data)) {
1172 if (!GST_IS_QUERY (item->data))
1173 gst_mini_object_unref (item->data);
1174 g_queue_delete_link (&aggpad->priv->data, item);
1175 }
1176 item = next;
1177 }
1178 aggpad->priv->num_buffers = 0;
1179 aggpad->priv->num_bytes = 0;
1180 aggpad->priv->stream_start_pending = FALSE;
1181 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
1182
1183 PAD_BROADCAST_EVENT (aggpad);
1184 PAD_UNLOCK (aggpad);
1185 }
1186
1187 static GstFlowReturn
1188 gst_aggregator_default_update_src_caps (GstAggregator * agg, GstCaps * caps,
1189 GstCaps ** ret)
1190 {
1191 *ret = gst_caps_ref (caps);
1192
1193 return GST_FLOW_OK;
1194 }
1195
1196 static GstCaps *
1197 gst_aggregator_default_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
1198 {
1199 caps = gst_caps_fixate (caps);
1200
1201 return caps;
1202 }
1203
1204 static gboolean
1205 gst_aggregator_default_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
1206 {
1207 return TRUE;
1208 }
1209
1210
1211 /* takes ownership of the pool, allocator and query */
1212 static gboolean
1213 gst_aggregator_set_allocation (GstAggregator * self,
1214 GstBufferPool * pool, GstAllocator * allocator,
1215 const GstAllocationParams * params, GstQuery * query)
1216 {
1217 GstAllocator *oldalloc;
1218 GstBufferPool *oldpool;
1219 GstQuery *oldquery;
1220
1221 GST_DEBUG ("storing allocation query");
1222
1223 GST_OBJECT_LOCK (self);
1224 oldpool = self->priv->pool;
1225 self->priv->pool = pool;
1226
1227 oldalloc = self->priv->allocator;
1228 self->priv->allocator = allocator;
1229
1230 oldquery = self->priv->allocation_query;
1231 self->priv->allocation_query = query;
1232
1233 if (params)
1234 self->priv->allocation_params = *params;
1235 else
1236 gst_allocation_params_init (&self->priv->allocation_params);
1237 GST_OBJECT_UNLOCK (self);
1238
1239 if (oldpool) {
1240 GST_DEBUG_OBJECT (self, "deactivating old pool %p", oldpool);
1241 gst_buffer_pool_set_active (oldpool, FALSE);
1242 gst_object_unref (oldpool);
1243 }
1244 if (oldalloc) {
1245 gst_object_unref (oldalloc);
1246 }
1247 if (oldquery) {
1248 gst_query_unref (oldquery);
1249 }
1250 return TRUE;
1251 }
1252
1253
1254 static gboolean
1255 gst_aggregator_decide_allocation (GstAggregator * self, GstQuery * query)
1256 {
1257 GstAggregatorClass *aggclass = GST_AGGREGATOR_GET_CLASS (self);
1258
1259 if (aggclass->decide_allocation)
1260 if (!aggclass->decide_allocation (self, query))
1261 return FALSE;
1262
1263 return TRUE;
1264 }
1265
1266 static gboolean
1267 gst_aggregator_do_allocation (GstAggregator * self, GstCaps * caps)
1268 {
1269 GstQuery *query;
1270 gboolean result = TRUE;
1271 GstBufferPool *pool = NULL;
1272 GstAllocator *allocator;
1273 GstAllocationParams params;
1274
1275 /* find a pool for the negotiated caps now */
1276 GST_DEBUG_OBJECT (self, "doing allocation query");
1277 query = gst_query_new_allocation (caps, TRUE);
1278 if (!gst_pad_peer_query (self->srcpad, query)) {
1279 /* not a problem, just debug a little */
1280 GST_DEBUG_OBJECT (self, "peer ALLOCATION query failed");
1281 }
1282
1283 GST_DEBUG_OBJECT (self, "calling decide_allocation");
1284 result = gst_aggregator_decide_allocation (self, query);
1285
1286 GST_DEBUG_OBJECT (self, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
1287 query);
1288
1289 if (!result)
1290 goto no_decide_allocation;
1291
1292 /* we got configuration from our peer or the decide_allocation method,
1293 * parse them */
1294 if (gst_query_get_n_allocation_params (query) > 0) {
1295 gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
1296 } else {
1297 allocator = NULL;
1298 gst_allocation_params_init (&params);
1299 }
1300
1301 if (gst_query_get_n_allocation_pools (query) > 0)
1302 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
1303
1304 /* now store */
1305 result =
1306 gst_aggregator_set_allocation (self, pool, allocator, &params, query);
1307
1308 return result;
1309
1310 /* Errors */
1311 no_decide_allocation:
1312 {
1313 GST_WARNING_OBJECT (self, "Failed to decide allocation");
1314 gst_query_unref (query);
1315
1316 return result;
1317 }
1318
1319 }
1320
1321 static gboolean
1322 gst_aggregator_default_negotiate (GstAggregator * self)
1323 {
1324 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
1325 GstCaps *downstream_caps, *template_caps, *caps = NULL;
1326 GstFlowReturn ret = GST_FLOW_OK;
1327
1328 template_caps = gst_pad_get_pad_template_caps (self->srcpad);
1329 downstream_caps = gst_pad_peer_query_caps (self->srcpad, template_caps);
1330
1331 if (gst_caps_is_empty (downstream_caps)) {
1332 GST_INFO_OBJECT (self, "Downstream caps (%"
1333 GST_PTR_FORMAT ") not compatible with pad template caps (%"
1334 GST_PTR_FORMAT ")", downstream_caps, template_caps);
1335 ret = GST_FLOW_NOT_NEGOTIATED;
1336 goto done;
1337 }
1338
1339 g_assert (agg_klass->update_src_caps);
1340 GST_DEBUG_OBJECT (self, "updating caps from %" GST_PTR_FORMAT,
1341 downstream_caps);
1342 ret = agg_klass->update_src_caps (self, downstream_caps, &caps);
1343 if (ret == GST_AGGREGATOR_FLOW_NEED_DATA) {
1344 GST_DEBUG_OBJECT (self, "Subclass needs more data to decide on caps");
1345 goto done;
1346 } else if (ret < GST_FLOW_OK) {
1347 GST_WARNING_OBJECT (self, "Subclass failed to update provided caps");
1348 goto done;
1349 }
1350 if ((caps == NULL || gst_caps_is_empty (caps)) && ret >= GST_FLOW_OK) {
1351 ret = GST_FLOW_NOT_NEGOTIATED;
1352 goto done;
1353 }
1354 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1355
1356 #ifdef GST_ENABLE_EXTRA_CHECKS
1357 if (!gst_caps_is_subset (caps, template_caps)) {
1358 GstCaps *intersection;
1359
1360 GST_ERROR_OBJECT (self,
1361 "update_src_caps returned caps %" GST_PTR_FORMAT
1362 " which are not a real subset of the template caps %"
1363 GST_PTR_FORMAT, caps, template_caps);
1364 g_warning ("%s: update_src_caps returned caps which are not a real "
1365 "subset of the filter caps", GST_ELEMENT_NAME (self));
1366
1367 intersection =
1368 gst_caps_intersect_full (template_caps, caps, GST_CAPS_INTERSECT_FIRST);
1369 gst_caps_unref (caps);
1370 caps = intersection;
1371 }
1372 #endif
1373
1374 if (gst_caps_is_any (caps)) {
1375 goto done;
1376 }
1377
1378 if (!gst_caps_is_fixed (caps)) {
1379 g_assert (agg_klass->fixate_src_caps);
1380
1381 GST_DEBUG_OBJECT (self, "fixate caps from %" GST_PTR_FORMAT, caps);
1382 if (!(caps = agg_klass->fixate_src_caps (self, caps))) {
1383 GST_WARNING_OBJECT (self, "Subclass failed to fixate provided caps");
1384 ret = GST_FLOW_NOT_NEGOTIATED;
1385 goto done;
1386 }
1387 GST_DEBUG_OBJECT (self, " to %" GST_PTR_FORMAT, caps);
1388 }
1389
1390 if (agg_klass->negotiated_src_caps) {
1391 if (!agg_klass->negotiated_src_caps (self, caps)) {
1392 GST_WARNING_OBJECT (self, "Subclass failed to accept negotiated caps");
1393 ret = GST_FLOW_NOT_NEGOTIATED;
1394 goto done;
1395 }
1396 }
1397
1398 gst_aggregator_set_src_caps (self, caps);
1399
1400 if (!gst_aggregator_do_allocation (self, caps)) {
1401 GST_WARNING_OBJECT (self, "Allocation negotiation failed");
1402 ret = GST_FLOW_NOT_NEGOTIATED;
1403 }
1404
1405 done:
1406 gst_caps_unref (downstream_caps);
1407 gst_caps_unref (template_caps);
1408
1409 if (caps)
1410 gst_caps_unref (caps);
1411
1412 return ret >= GST_FLOW_OK || ret == GST_AGGREGATOR_FLOW_NEED_DATA;
1413 }
1414
1415 /* WITH SRC_LOCK held */
1416 static gboolean
1417 gst_aggregator_negotiate_unlocked (GstAggregator * self)
1418 {
1419 GstAggregatorClass *agg_klass = GST_AGGREGATOR_GET_CLASS (self);
1420
1421 if (agg_klass->negotiate)
1422 return agg_klass->negotiate (self);
1423
1424 return TRUE;
1425 }
1426
1427 /**
1428 * gst_aggregator_negotiate:
1429 * @self: a #GstAggregator
1430 *
1431 * Negotiates src pad caps with downstream elements.
1432 * Unmarks GST_PAD_FLAG_NEED_RECONFIGURE in any case. But marks it again
1433 * if #GstAggregatorClass::negotiate fails.
1434 *
1435 * Returns: %TRUE if the negotiation succeeded, else %FALSE.
1436 *
1437 * Since: 1.18
1438 */
1439 gboolean
1440 gst_aggregator_negotiate (GstAggregator * self)
1441 {
1442 gboolean ret = TRUE;
1443
1444 g_return_val_if_fail (GST_IS_AGGREGATOR (self), FALSE);
1445
1446 GST_PAD_STREAM_LOCK (GST_AGGREGATOR_SRC_PAD (self));
1447 gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1448 ret = gst_aggregator_negotiate_unlocked (self);
1449 if (!ret)
1450 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1451 GST_PAD_STREAM_UNLOCK (GST_AGGREGATOR_SRC_PAD (self));
1452
1453 return ret;
1454 }
1455
1456 static gboolean
1457 gst_aggregator_check_pending_new_stream (GstElement * self, GstPad * pad,
1458 gboolean * have_new_stream)
1459 {
1460 GstAggregatorPad *aggpad = (GstAggregatorPad *) pad;
1461 gboolean new_stream = FALSE;
1462
1463 PAD_LOCK (aggpad);
1464 if (aggpad->priv->stream_start_pending)
1465 new_stream = TRUE;
1466 PAD_UNLOCK (aggpad);
1467
1468 if (new_stream) {
1469 *have_new_stream = TRUE;
1470 return FALSE;
1471 }
1472
1473 return TRUE;
1474 }
1475
1476 static GstFlowReturn
1477 gst_aggregator_loop (GstAggregator * self)
1478 {
1479 GstAggregatorPrivate *priv = self->priv;
1480 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1481 gboolean timeout = FALSE;
1482 GstFlowReturn flow_return = GST_FLOW_OK;
1483
1484 if (self->priv->running == FALSE) {
1485 GST_DEBUG_OBJECT (self, "Not running anymore");
1486 return GST_FLOW_OK;
1487 }
1488
1489 GST_LOG_OBJECT (self, "Checking aggregate");
1490 while (priv->send_eos && priv->running) {
1491 flow_return = GST_FLOW_OK;
1492 DoHandleEventsAndQueriesData events_query_data = { FALSE, GST_FLOW_OK };
1493
1494 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1495 gst_aggregator_do_events_and_queries, &events_query_data);
1496
1497 if ((flow_return = events_query_data.flow_ret) != GST_FLOW_OK)
1498 goto handle_error;
1499
1500 if (is_live_unlocked (self))
1501 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1502 gst_aggregator_pad_skip_buffers, NULL);
1503
1504 SRC_LOCK (self);
1505 if (self->priv->got_eos_event) {
1506 self->priv->got_eos_event = FALSE;
1507 SRC_UNLOCK (self);
1508 gst_aggregator_push_eos (self);
1509 continue;
1510 }
1511 SRC_UNLOCK (self);
1512
1513 /* Ensure we have buffers ready (either in clipped_buffer or at the head of
1514 * the queue */
1515 if (!gst_aggregator_wait_and_check (self, &timeout)) {
1516 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1517 gst_aggregator_pad_reset_peeked_buffer, NULL);
1518 continue;
1519 }
1520
1521 if (gst_pad_check_reconfigure (GST_AGGREGATOR_SRC_PAD (self))) {
1522 if (!gst_aggregator_negotiate_unlocked (self)) {
1523 gst_pad_mark_reconfigure (GST_AGGREGATOR_SRC_PAD (self));
1524 if (GST_PAD_IS_FLUSHING (GST_AGGREGATOR_SRC_PAD (self))) {
1525 flow_return = GST_FLOW_FLUSHING;
1526 } else {
1527 flow_return = GST_FLOW_NOT_NEGOTIATED;
1528 }
1529 }
1530 }
1531
1532 if (timeout || flow_return >= GST_FLOW_OK) {
1533 GST_LOG_OBJECT (self, "Actually aggregating, timeout: %d", timeout);
1534 flow_return = klass->aggregate (self, timeout);
1535 }
1536
1537 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1538 gst_aggregator_pad_reset_peeked_buffer, NULL);
1539
1540 if (!priv->selected_samples_called_or_warned) {
1541 GST_FIXME_OBJECT (self,
1542 "Subclass should call gst_aggregator_selected_samples() from its "
1543 "aggregate implementation.");
1544 priv->selected_samples_called_or_warned = TRUE;
1545 }
1546
1547 if (flow_return == GST_AGGREGATOR_FLOW_NEED_DATA)
1548 continue;
1549
1550 GST_OBJECT_LOCK (self);
1551 if (flow_return == GST_FLOW_FLUSHING && priv->flushing) {
1552 /* We don't want to set the pads to flushing, but we want to
1553 * stop the thread, so just break here */
1554 GST_OBJECT_UNLOCK (self);
1555 return GST_FLOW_FLUSHING;
1556 }
1557 GST_OBJECT_UNLOCK (self);
1558
1559 if (flow_return == GST_FLOW_EOS || flow_return == GST_FLOW_ERROR) {
1560 gst_aggregator_push_eos (self);
1561 }
1562
1563 handle_error:
1564 GST_LOG_OBJECT (self, "flow return is %s", gst_flow_get_name (flow_return));
1565
1566 /* Don't flush buffer/event/queries on EOS. We may do restart pad task
1567 * on new stream-start */
1568 if (flow_return == GST_FLOW_EOS)
1569 return GST_FLOW_EOS;
1570
1571 if (flow_return != GST_FLOW_OK) {
1572 GList *item;
1573
1574 GST_OBJECT_LOCK (self);
1575 for (item = GST_ELEMENT (self)->sinkpads; item; item = item->next) {
1576 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
1577
1578 gst_aggregator_pad_set_flushing (aggpad, flow_return, TRUE);
1579 }
1580 GST_OBJECT_UNLOCK (self);
1581 break;
1582 }
1583 }
1584
1585 return flow_return;
1586 }
1587
1588 static void
1589 gst_aggregator_aggregate_func (GstAggregator * self)
1590 {
1591 GstAggregatorPrivate *priv = self->priv;
1592
1593 while (1) {
1594 GstFlowReturn ret;
1595 gboolean pending_new_stream = FALSE;
1596
1597 ret = gst_aggregator_loop (self);
1598
1599 SRC_LOCK (self);
1600 if (ret != GST_FLOW_EOS)
1601 break;
1602
1603 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (self),
1604 (GstElementForeachPadFunc) gst_aggregator_check_pending_new_stream,
1605 &pending_new_stream);
1606
1607 if (!pending_new_stream)
1608 break;
1609
1610 GST_INFO_OBJECT (self, "Have pending new stream, keep aggregate");
1611 gst_aggregator_reset_flow_values (self);
1612 priv->send_eos = TRUE;
1613 SRC_UNLOCK (self);
1614 };
1615
1616 /* Pause the task here, the only ways to get here are:
1617 * 1) We're stopping, in which case the task is stopped anyway
1618 * 2) We got a flow error above, in which case it might take
1619 * some time to forward the flow return upstream and we
1620 * would otherwise call the task function over and over
1621 * again without doing anything
1622 */
1623 gst_pad_pause_task (self->srcpad);
1624 SRC_UNLOCK (self);
1625 }
1626
1627 static gboolean
1628 gst_aggregator_start (GstAggregator * self)
1629 {
1630 GstAggregatorClass *klass;
1631 gboolean result;
1632
1633 self->priv->send_stream_start = TRUE;
1634 self->priv->send_segment = TRUE;
1635 self->priv->send_eos = TRUE;
1636 self->priv->got_eos_event = FALSE;
1637 self->priv->srccaps = NULL;
1638
1639 self->priv->has_peer_latency = FALSE;
1640 self->priv->peer_latency_live = FALSE;
1641 self->priv->peer_latency_min = self->priv->peer_latency_max = 0;
1642
1643 self->priv->blocked = TRUE;
1644
1645 gst_aggregator_set_allocation (self, NULL, NULL, NULL, NULL);
1646
1647 klass = GST_AGGREGATOR_GET_CLASS (self);
1648
1649 if (klass->start)
1650 result = klass->start (self);
1651 else
1652 result = TRUE;
1653
1654 return result;
1655 }
1656
1657 static gboolean
1658 gst_aggregator_stop_srcpad_task (GstAggregator * self, GstEvent * flush_start)
1659 {
1660 gboolean res = TRUE;
1661
1662 GST_INFO_OBJECT (self, "%s srcpad task",
1663 flush_start ? "Pausing" : "Stopping");
1664
1665 SRC_LOCK (self);
1666 self->priv->running = FALSE;
1667 SRC_BROADCAST (self);
1668 SRC_UNLOCK (self);
1669
1670 if (flush_start) {
1671 res = gst_pad_push_event (self->srcpad, flush_start);
1672 }
1673
1674 gst_pad_stop_task (self->srcpad);
1675
1676 return res;
1677 }
1678
1679 static void
1680 gst_aggregator_start_srcpad_task (GstAggregator * self)
1681 {
1682 GST_INFO_OBJECT (self, "Starting srcpad task");
1683
1684 self->priv->running = TRUE;
1685 gst_pad_start_task (GST_PAD (self->srcpad),
1686 (GstTaskFunction) gst_aggregator_aggregate_func, self, NULL);
1687 }
1688
1689 static GstFlowReturn
1690 gst_aggregator_flush (GstAggregator * self)
1691 {
1692 GstFlowReturn ret = GST_FLOW_OK;
1693 GstAggregatorPrivate *priv = self->priv;
1694 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
1695
1696 GST_DEBUG_OBJECT (self, "Flushing everything");
1697 GST_OBJECT_LOCK (self);
1698 priv->send_segment = TRUE;
1699 priv->flushing = FALSE;
1700 GST_OBJECT_UNLOCK (self);
1701 if (klass->flush)
1702 ret = klass->flush (self);
1703
1704 return ret;
1705 }
1706
1707
1708 /* Called with GstAggregator's object lock held */
1709
1710 static gboolean
1711 gst_aggregator_all_flush_stop_received (GstAggregator * self, guint32 seqnum)
1712 {
1713 GList *tmp;
1714 GstAggregatorPad *tmppad;
1715
1716 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1717 tmppad = (GstAggregatorPad *) tmp->data;
1718
1719 if (tmppad->priv->last_flush_stop_seqnum != seqnum)
1720 return FALSE;
1721 }
1722
1723 return TRUE;
1724 }
1725
1726 /* Called with GstAggregator's object lock held */
1727
1728 static gboolean
1729 gst_aggregator_all_flush_start_received (GstAggregator * self, guint32 seqnum)
1730 {
1731 GList *tmp;
1732 GstAggregatorPad *tmppad;
1733
1734 for (tmp = GST_ELEMENT (self)->sinkpads; tmp; tmp = tmp->next) {
1735 tmppad = (GstAggregatorPad *) tmp->data;
1736
1737 if (tmppad->priv->last_flush_start_seqnum != seqnum) {
1738 return FALSE;
1739 }
1740 }
1741
1742 return TRUE;
1743 }
1744
1745 static void
1746 gst_aggregator_flush_start (GstAggregator * self, GstAggregatorPad * aggpad,
1747 GstEvent * event)
1748 {
1749 GstAggregatorPrivate *priv = self->priv;
1750 GstAggregatorPadPrivate *padpriv = aggpad->priv;
1751 guint32 seqnum = gst_event_get_seqnum (event);
1752
1753 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, FALSE);
1754
1755 PAD_FLUSH_LOCK (aggpad);
1756 PAD_LOCK (aggpad);
1757 padpriv->last_flush_start_seqnum = seqnum;
1758 PAD_UNLOCK (aggpad);
1759
1760 GST_OBJECT_LOCK (self);
1761
1762 if (!priv->flushing && gst_aggregator_all_flush_start_received (self, seqnum)) {
1763 /* Make sure we don't forward more than one FLUSH_START */
1764 priv->flushing = TRUE;
1765 priv->next_seqnum = seqnum;
1766 GST_OBJECT_UNLOCK (self);
1767
1768 GST_INFO_OBJECT (self, "Flushing, pausing srcpad task");
1769 gst_aggregator_stop_srcpad_task (self, event);
1770
1771 event = NULL;
1772 } else {
1773 gst_event_unref (event);
1774 GST_OBJECT_UNLOCK (self);
1775 }
1776
1777 PAD_FLUSH_UNLOCK (aggpad);
1778 }
1779
1780 /* Must be called with the PAD_LOCK and OBJECT_LOCK held */
1781 static void
1782 update_time_level (GstAggregatorPad * aggpad, gboolean head)
1783 {
1784 GstAggregatorPadPrivate *priv = aggpad->priv;
1785
1786 if (head) {
1787 if (GST_CLOCK_TIME_IS_VALID (priv->head_position) &&
1788 priv->head_segment.format == GST_FORMAT_TIME)
1789 priv->head_time = gst_segment_to_running_time (&priv->head_segment,
1790 GST_FORMAT_TIME, priv->head_position);
1791 else
1792 priv->head_time = GST_CLOCK_TIME_NONE;
1793
1794 if (!GST_CLOCK_TIME_IS_VALID (priv->tail_time))
1795 priv->tail_time = priv->head_time;
1796 } else {
1797 if (GST_CLOCK_TIME_IS_VALID (priv->tail_position) &&
1798 aggpad->segment.format == GST_FORMAT_TIME)
1799 priv->tail_time = gst_segment_to_running_time (&aggpad->segment,
1800 GST_FORMAT_TIME, priv->tail_position);
1801 else
1802 priv->tail_time = priv->head_time;
1803 }
1804
1805 if (priv->head_time == GST_CLOCK_TIME_NONE ||
1806 priv->tail_time == GST_CLOCK_TIME_NONE) {
1807 priv->time_level = 0;
1808 return;
1809 }
1810
1811 if (priv->tail_time > priv->head_time)
1812 priv->time_level = 0;
1813 else
1814 priv->time_level = priv->head_time - priv->tail_time;
1815 }
1816
1817
1818 /* GstAggregator vmethods default implementations */
1819 static gboolean
1820 gst_aggregator_default_sink_event (GstAggregator * self,
1821 GstAggregatorPad * aggpad, GstEvent * event)
1822 {
1823 gboolean res = TRUE;
1824 GstPad *pad = GST_PAD (aggpad);
1825 GstAggregatorPrivate *priv = self->priv;
1826
1827 GST_DEBUG_OBJECT (aggpad, "Got event: %" GST_PTR_FORMAT, event);
1828
1829 switch (GST_EVENT_TYPE (event)) {
1830 case GST_EVENT_FLUSH_START:
1831 {
1832 gst_aggregator_flush_start (self, aggpad, event);
1833 /* We forward only in one case: right after flushing */
1834 event = NULL;
1835 goto eat;
1836 }
1837 case GST_EVENT_FLUSH_STOP:
1838 {
1839 guint32 seqnum = gst_event_get_seqnum (event);
1840
1841 PAD_FLUSH_LOCK (aggpad);
1842 PAD_LOCK (aggpad);
1843 aggpad->priv->last_flush_stop_seqnum = seqnum;
1844 PAD_UNLOCK (aggpad);
1845
1846 gst_aggregator_pad_flush (aggpad, self);
1847
1848 GST_OBJECT_LOCK (self);
1849 if (priv->flushing
1850 && gst_aggregator_all_flush_stop_received (self, seqnum)) {
1851 GST_OBJECT_UNLOCK (self);
1852 /* That means we received FLUSH_STOP/FLUSH_STOP on
1853 * all sinkpads -- Seeking is Done... sending FLUSH_STOP */
1854 gst_aggregator_flush (self);
1855 gst_pad_push_event (self->srcpad, event);
1856 event = NULL;
1857 SRC_LOCK (self);
1858 priv->send_eos = TRUE;
1859 priv->got_eos_event = FALSE;
1860 SRC_BROADCAST (self);
1861 SRC_UNLOCK (self);
1862
1863 GST_INFO_OBJECT (self, "Flush stopped");
1864
1865 gst_aggregator_start_srcpad_task (self);
1866 } else {
1867 GST_OBJECT_UNLOCK (self);
1868 }
1869
1870 PAD_FLUSH_UNLOCK (aggpad);
1871
1872 /* We never forward the event */
1873 goto eat;
1874 }
1875 case GST_EVENT_EOS:
1876 {
1877 SRC_LOCK (self);
1878 PAD_LOCK (aggpad);
1879 aggpad->priv->eos = TRUE;
1880 PAD_UNLOCK (aggpad);
1881 SRC_BROADCAST (self);
1882 SRC_UNLOCK (self);
1883 goto eat;
1884 }
1885 case GST_EVENT_SEGMENT:
1886 {
1887 PAD_LOCK (aggpad);
1888 GST_OBJECT_LOCK (aggpad);
1889 gst_event_copy_segment (event, &aggpad->segment);
1890 /* We've got a new segment, tail_position is now meaningless
1891 * and may interfere with the time_level calculation
1892 */
1893 aggpad->priv->tail_position = GST_CLOCK_TIME_NONE;
1894 update_time_level (aggpad, FALSE);
1895 GST_OBJECT_UNLOCK (aggpad);
1896 PAD_UNLOCK (aggpad);
1897
1898 GST_OBJECT_LOCK (self);
1899 self->priv->seqnum = gst_event_get_seqnum (event);
1900 GST_OBJECT_UNLOCK (self);
1901 goto eat;
1902 }
1903 case GST_EVENT_STREAM_START:
1904 {
1905 SRC_LOCK (self);
1906 PAD_LOCK (aggpad);
1907 GST_DEBUG_OBJECT (aggpad, "Clear EOS on STREAM-START");
1908 aggpad->priv->eos = FALSE;
1909 aggpad->priv->stream_start_pending = FALSE;
1910 PAD_UNLOCK (aggpad);
1911 SRC_BROADCAST (self);
1912 SRC_UNLOCK (self);
1913 goto eat;
1914 }
1915 case GST_EVENT_GAP:
1916 {
1917 GstClockTime pts, endpts;
1918 GstClockTime duration;
1919 GstBuffer *gapbuf;
1920 GstGapFlags flags = 0;
1921
1922 gst_event_parse_gap (event, &pts, &duration);
1923
1924 if (GST_CLOCK_TIME_IS_VALID (duration))
1925 endpts = pts + duration;
1926 else
1927 endpts = GST_CLOCK_TIME_NONE;
1928
1929 GST_OBJECT_LOCK (aggpad);
1930 if (aggpad->segment.format != GST_FORMAT_TIME) {
1931 GST_WARNING_OBJECT (self, "GAP event before segment, dropping");
1932 res = FALSE;
1933 GST_OBJECT_UNLOCK (aggpad);
1934 goto eat;
1935 }
1936
1937 res = gst_segment_clip (&aggpad->segment, GST_FORMAT_TIME, pts, endpts,
1938 &pts, &endpts);
1939 GST_OBJECT_UNLOCK (aggpad);
1940
1941 if (!res) {
1942 GST_WARNING_OBJECT (self, "GAP event outside segment, dropping");
1943 goto eat;
1944 }
1945
1946 if (GST_CLOCK_TIME_IS_VALID (endpts) && GST_CLOCK_TIME_IS_VALID (pts))
1947 duration = endpts - pts;
1948 else
1949 duration = GST_CLOCK_TIME_NONE;
1950
1951 gapbuf = gst_buffer_new ();
1952 GST_BUFFER_PTS (gapbuf) = pts;
1953 GST_BUFFER_DURATION (gapbuf) = duration;
1954 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_GAP);
1955 GST_BUFFER_FLAG_SET (gapbuf, GST_BUFFER_FLAG_DROPPABLE);
1956
1957 gst_event_parse_gap_flags (event, &flags);
1958 if (flags & GST_GAP_FLAG_MISSING_DATA) {
1959 gst_buffer_add_custom_meta (gapbuf, "GstAggregatorMissingDataMeta");
1960 }
1961
1962 /* Remove GAP event so we can replace it with the buffer */
1963 PAD_LOCK (aggpad);
1964 if (g_queue_peek_tail (&aggpad->priv->data) == event)
1965 gst_event_unref (g_queue_pop_tail (&aggpad->priv->data));
1966 PAD_UNLOCK (aggpad);
1967
1968 if (gst_aggregator_pad_chain_internal (self, aggpad, gapbuf, FALSE) !=
1969 GST_FLOW_OK) {
1970 GST_WARNING_OBJECT (self, "Failed to chain gap buffer");
1971 res = FALSE;
1972 }
1973
1974 goto eat;
1975 }
1976 case GST_EVENT_TAG:
1977 goto eat;
1978 default:
1979 {
1980 break;
1981 }
1982 }
1983
1984 GST_DEBUG_OBJECT (pad, "Forwarding event: %" GST_PTR_FORMAT, event);
1985 return gst_pad_event_default (pad, GST_OBJECT (self), event);
1986
1987 eat:
1988 GST_DEBUG_OBJECT (pad, "Eating event: %" GST_PTR_FORMAT, event);
1989 if (event)
1990 gst_event_unref (event);
1991
1992 return res;
1993 }
1994
1995 /* Queue serialized events and let the others go through directly.
1996 * The queued events with be handled from the src-pad task in
1997 * gst_aggregator_do_events_and_queries().
1998 */
1999 static GstFlowReturn
2000 gst_aggregator_default_sink_event_pre_queue (GstAggregator * self,
2001 GstAggregatorPad * aggpad, GstEvent * event)
2002 {
2003 GstAggregatorPrivate *priv = self->priv;
2004 GstFlowReturn ret = GST_FLOW_OK;
2005 GstEventType event_type = GST_EVENT_TYPE (event);
2006
2007 if (GST_EVENT_IS_SERIALIZED (event) && event_type != GST_EVENT_FLUSH_STOP) {
2008 SRC_LOCK (self);
2009 PAD_LOCK (aggpad);
2010
2011 if (event_type == GST_EVENT_STREAM_START && priv->running) {
2012 GstTaskState task_state;
2013 aggpad->priv->flow_return = GST_FLOW_OK;
2014
2015 aggpad->priv->stream_start_pending = TRUE;
2016
2017 task_state = gst_pad_get_task_state (self->srcpad);
2018 if (task_state == GST_TASK_PAUSED) {
2019 GST_DEBUG_OBJECT (aggpad, "Resuming pad task");
2020 priv->send_eos = TRUE;
2021 gst_aggregator_reset_flow_values (self);
2022 gst_aggregator_start_srcpad_task (self);
2023 } else {
2024 GST_DEBUG_OBJECT (aggpad, "Pad task function is running already");
2025 }
2026 }
2027
2028 if (aggpad->priv->flow_return != GST_FLOW_OK)
2029 goto flushing;
2030
2031 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2032 GST_OBJECT_LOCK (aggpad);
2033 gst_event_copy_segment (event, &aggpad->priv->head_segment);
2034 aggpad->priv->head_position = aggpad->priv->head_segment.position;
2035 update_time_level (aggpad, TRUE);
2036 GST_OBJECT_UNLOCK (aggpad);
2037 }
2038
2039 GST_DEBUG_OBJECT (aggpad, "Store event in queue: %" GST_PTR_FORMAT, event);
2040 g_queue_push_head (&aggpad->priv->data, event);
2041 SRC_BROADCAST (self);
2042 PAD_UNLOCK (aggpad);
2043 SRC_UNLOCK (self);
2044 } else {
2045 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
2046
2047 if (!klass->sink_event (self, aggpad, event)) {
2048 /* Copied from GstPad to convert boolean to a GstFlowReturn in
2049 * the event handling func */
2050 ret = GST_FLOW_ERROR;
2051 }
2052 }
2053
2054 return ret;
2055
2056 flushing:
2057 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping event",
2058 gst_flow_get_name (aggpad->priv->flow_return));
2059 PAD_UNLOCK (aggpad);
2060 SRC_UNLOCK (self);
2061 if (GST_EVENT_IS_STICKY (event))
2062 gst_pad_store_sticky_event (GST_PAD (aggpad), event);
2063 gst_event_unref (event);
2064
2065 return aggpad->priv->flow_return;
2066 }
2067
2068 static gboolean
2069 gst_aggregator_stop_pad (GstElement * self, GstPad * epad, gpointer user_data)
2070 {
2071 GstAggregatorPad *pad = GST_AGGREGATOR_PAD_CAST (epad);
2072 GstAggregator *agg = GST_AGGREGATOR_CAST (self);
2073
2074 gst_aggregator_pad_flush (pad, agg);
2075
2076 PAD_LOCK (pad);
2077 pad->priv->flow_return = GST_FLOW_FLUSHING;
2078 pad->priv->negotiated = FALSE;
2079 PAD_BROADCAST_EVENT (pad);
2080 PAD_UNLOCK (pad);
2081
2082 return TRUE;
2083 }
2084
2085 static gboolean
2086 gst_aggregator_stop (GstAggregator * agg)
2087 {
2088 GstAggregatorClass *klass;
2089 gboolean result;
2090
2091 gst_aggregator_reset_flow_values (agg);
2092
2093 /* Application needs to make sure no pads are added while it shuts us down */
2094 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (agg),
2095 gst_aggregator_stop_pad, NULL);
2096
2097 klass = GST_AGGREGATOR_GET_CLASS (agg);
2098
2099 if (klass->stop)
2100 result = klass->stop (agg);
2101 else
2102 result = TRUE;
2103
2104 agg->priv->has_peer_latency = FALSE;
2105 agg->priv->peer_latency_live = FALSE;
2106 agg->priv->peer_latency_min = agg->priv->peer_latency_max = 0;
2107 agg->priv->posted_latency_msg = FALSE;
2108 agg->priv->blocked = FALSE;
2109
2110 gst_aggregator_set_allocation (agg, NULL, NULL, NULL, NULL);
2111
2112 if (agg->priv->running) {
2113 /* As sinkpads get deactivated after the src pad, we
2114 * may have restarted the source pad task after receiving
2115 * flush events on one of our sinkpads. Stop our src pad
2116 * task again if that is the case */
2117 gst_aggregator_stop_srcpad_task (agg, NULL);
2118 }
2119
2120 return result;
2121 }
2122
2123 /* GstElement vmethods implementations */
2124 static GstStateChangeReturn
2125 gst_aggregator_change_state (GstElement * element, GstStateChange transition)
2126 {
2127 GstStateChangeReturn ret;
2128 GstAggregator *self = GST_AGGREGATOR (element);
2129
2130 switch (transition) {
2131 case GST_STATE_CHANGE_READY_TO_PAUSED:
2132 if (!gst_aggregator_start (self))
2133 goto error_start;
2134 break;
2135 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2136 /* Wake up any waiting as now we have a clock and can do
2137 * proper waiting on the clock if necessary */
2138 SRC_LOCK (self);
2139 self->priv->blocked = FALSE;
2140 SRC_BROADCAST (self);
2141 SRC_UNLOCK (self);
2142 break;
2143 default:
2144 break;
2145 }
2146
2147 if ((ret =
2148 GST_ELEMENT_CLASS (aggregator_parent_class)->change_state (element,
2149 transition)) == GST_STATE_CHANGE_FAILURE)
2150 goto failure;
2151
2152
2153 switch (transition) {
2154 case GST_STATE_CHANGE_PAUSED_TO_READY:
2155 if (!gst_aggregator_stop (self)) {
2156 /* What to do in this case? Error out? */
2157 GST_ERROR_OBJECT (self, "Subclass failed to stop.");
2158 }
2159 break;
2160 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2161 /* Wake up any waiting as now clock might be gone and we might
2162 * need to wait on the condition variable again */
2163 SRC_LOCK (self);
2164 self->priv->blocked = TRUE;
2165 SRC_BROADCAST (self);
2166 SRC_UNLOCK (self);
2167 if (self->priv->force_live) {
2168 ret = GST_STATE_CHANGE_NO_PREROLL;
2169 }
2170 break;
2171 case GST_STATE_CHANGE_READY_TO_PAUSED:
2172 if (self->priv->force_live) {
2173 ret = GST_STATE_CHANGE_NO_PREROLL;
2174 }
2175 break;
2176 default:
2177 break;
2178 }
2179
2180 return ret;
2181
2182 /* ERRORS */
2183 failure:
2184 {
2185 GST_ERROR_OBJECT (element, "parent failed state change");
2186 return ret;
2187 }
2188 error_start:
2189 {
2190 GST_ERROR_OBJECT (element, "Subclass failed to start");
2191 return GST_STATE_CHANGE_FAILURE;
2192 }
2193 }
2194
2195 static void
2196 gst_aggregator_release_pad (GstElement * element, GstPad * pad)
2197 {
2198 GstAggregator *self = GST_AGGREGATOR (element);
2199 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2200
2201 GST_INFO_OBJECT (pad, "Removing pad");
2202
2203 SRC_LOCK (self);
2204 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
2205 PAD_LOCK (aggpad);
2206 gst_buffer_replace (&aggpad->priv->peeked_buffer, NULL);
2207 gst_buffer_replace (&aggpad->priv->clipped_buffer, NULL);
2208 PAD_UNLOCK (aggpad);
2209 gst_element_remove_pad (element, pad);
2210
2211 self->priv->has_peer_latency = FALSE;
2212 SRC_BROADCAST (self);
2213 SRC_UNLOCK (self);
2214 }
2215
2216 static GstAggregatorPad *
2217 gst_aggregator_default_create_new_pad (GstAggregator * self,
2218 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
2219 {
2220 GstAggregatorPad *agg_pad;
2221 GstAggregatorPrivate *priv = self->priv;
2222 gint serial = 0;
2223 gchar *name = NULL;
2224 gchar *percent_str;
2225
2226 GType pad_type =
2227 GST_PAD_TEMPLATE_GTYPE (templ) ==
2228 G_TYPE_NONE ? GST_TYPE_AGGREGATOR_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
2229
2230 if (templ->direction != GST_PAD_SINK)
2231 goto not_sink;
2232
2233 if (templ->presence != GST_PAD_REQUEST)
2234 goto not_request;
2235
2236 percent_str = strrchr (templ->name_template, '%');
2237
2238 if (percent_str == NULL) {
2239 if (req_name)
2240 name = g_strdup (req_name);
2241 else
2242 name = g_strdup (templ->name_template);
2243 } else if (g_strcmp0 (percent_str, "%u") == 0
2244 || g_strcmp0 (percent_str, "%d") == 0) {
2245 guint percent_index = percent_str - templ->name_template;
2246 gchar *template_str = g_strndup (templ->name_template, percent_index);
2247
2248 GST_OBJECT_LOCK (self);
2249 if (req_name == NULL || g_strcmp0 (templ->name_template, req_name) == 0) {
2250 /* no name given when requesting the pad, use next available int */
2251 serial = ++priv->max_padserial;
2252 } else if (g_str_has_prefix (req_name, template_str)) {
2253 gchar *endptr = NULL;
2254
2255 /* parse serial number from requested padname */
2256 serial = g_ascii_strtoull (&req_name[percent_index], &endptr, 10);
2257
2258 if (endptr != NULL && *endptr == '\0') {
2259 if (serial > priv->max_padserial) {
2260 priv->max_padserial = serial;
2261 }
2262 } else {
2263 g_free (template_str);
2264 GST_OBJECT_UNLOCK (self);
2265 goto invalid_request_name;
2266 }
2267 } else {
2268 g_free (template_str);
2269 GST_OBJECT_UNLOCK (self);
2270 goto invalid_request_name;
2271 }
2272
2273 name = g_strdup_printf ("%s%u", template_str, serial);
2274 g_free (template_str);
2275
2276 GST_OBJECT_UNLOCK (self);
2277 } else {
2278 goto invalid_template_name;
2279 }
2280
2281 g_assert (g_type_is_a (pad_type, GST_TYPE_AGGREGATOR_PAD));
2282 agg_pad = g_object_new (pad_type,
2283 "name", name, "direction", GST_PAD_SINK, "template", templ, NULL);
2284 g_free (name);
2285
2286 return agg_pad;
2287
2288 /* errors */
2289 not_sink:
2290 {
2291 GST_WARNING_OBJECT (self, "request new pad that is not a SINK pad");
2292 return NULL;
2293 }
2294 not_request:
2295 {
2296 GST_WARNING_OBJECT (self, "request new pad that is not a REQUEST pad");
2297 return NULL;
2298 }
2299 invalid_template_name:
2300 {
2301 GST_WARNING_OBJECT (self,
2302 "template name %s is invalid, must be in the form name_%%u (%s)",
2303 templ->name_template, percent_str);
2304 return NULL;
2305 }
2306 invalid_request_name:
2307 {
2308 GST_WARNING_OBJECT (self,
2309 "requested name %s is invalid, must be in the form %s", req_name,
2310 templ->name_template);
2311 return NULL;
2312 }
2313 }
2314
2315 static GstPad *
2316 gst_aggregator_request_new_pad (GstElement * element,
2317 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
2318 {
2319 GstAggregator *self;
2320 GstAggregatorPad *agg_pad;
2321 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (element);
2322
2323 self = GST_AGGREGATOR (element);
2324
2325 agg_pad = klass->create_new_pad (self, templ, req_name, caps);
2326 if (!agg_pad) {
2327 GST_ERROR_OBJECT (element, "Couldn't create new pad");
2328 return NULL;
2329 }
2330
2331 GST_DEBUG_OBJECT (element, "Adding pad %s", GST_PAD_NAME (agg_pad));
2332
2333 /* add the pad to the element */
2334 gst_element_add_pad (element, GST_PAD (agg_pad));
2335
2336 return GST_PAD (agg_pad);
2337 }
2338
2339 /* Must be called with SRC_LOCK held, temporarily releases it! */
2340
2341 static gboolean
2342 gst_aggregator_query_latency_unlocked (GstAggregator * self, GstQuery * query)
2343 {
2344 gboolean query_ret, upstream_live;
2345 GstClockTime our_latency, min, max;
2346
2347 GST_TRACE_OBJECT (self, "querying latency");
2348
2349 /* Temporarily release the lock to do the query. */
2350 SRC_UNLOCK (self);
2351 query_ret = gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
2352 SRC_LOCK (self);
2353
2354 if (!query_ret) {
2355 GST_WARNING_OBJECT (self, "Latency query failed");
2356 return FALSE;
2357 }
2358
2359 gst_query_parse_latency (query, &upstream_live, &min, &max);
2360
2361 GST_LOG_OBJECT (self,
2362 "queried upstream latency, live: %s min: %" GST_TIME_FORMAT " max: %"
2363 GST_TIME_FORMAT, upstream_live ? "true" : "false", GST_TIME_ARGS (min),
2364 GST_TIME_ARGS (max));
2365
2366 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (min))) {
2367 GST_ERROR_OBJECT (self, "Invalid minimum latency %" GST_TIME_FORMAT
2368 ". Please file a bug at " PACKAGE_BUGREPORT ".", GST_TIME_ARGS (min));
2369 return FALSE;
2370 }
2371
2372 if (self->priv->upstream_latency_min > min) {
2373 GstClockTimeDiff diff =
2374 GST_CLOCK_DIFF (min, self->priv->upstream_latency_min);
2375
2376 min += diff;
2377 if (GST_CLOCK_TIME_IS_VALID (max)) {
2378 max += diff;
2379 }
2380 }
2381
2382 if (min > max && GST_CLOCK_TIME_IS_VALID (max)) {
2383 SRC_UNLOCK (self);
2384 GST_ELEMENT_WARNING (self, CORE, CLOCK, (NULL),
2385 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2386 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2387 GST_TIME_ARGS (max), GST_TIME_ARGS (min)));
2388 SRC_LOCK (self);
2389 return FALSE;
2390 }
2391
2392 our_latency = self->priv->latency;
2393
2394 self->priv->peer_latency_live = upstream_live;
2395 self->priv->peer_latency_min = min;
2396 self->priv->peer_latency_max = max;
2397 self->priv->has_peer_latency = TRUE;
2398
2399 /* add our own */
2400 min += our_latency;
2401 min += self->priv->sub_latency_min;
2402 if (GST_CLOCK_TIME_IS_VALID (self->priv->sub_latency_max)
2403 && GST_CLOCK_TIME_IS_VALID (max))
2404 max += self->priv->sub_latency_max + our_latency;
2405 else
2406 max = GST_CLOCK_TIME_NONE;
2407
2408 SRC_BROADCAST (self);
2409
2410 GST_DEBUG_OBJECT (self, "configured latency live:%s min:%" G_GINT64_FORMAT
2411 " max:%" G_GINT64_FORMAT,
2412 is_live_unlocked (self) ? "true" : "false", min, max);
2413
2414 gst_query_set_latency (query, is_live_unlocked (self), min, max);
2415
2416 return query_ret;
2417 }
2418
2419 /*
2420 * MUST be called with the src_lock held. Temporarily releases the lock inside
2421 * gst_aggregator_query_latency_unlocked() to do the actual query!
2422 *
2423 * See gst_aggregator_get_latency() for doc
2424 */
2425 static GstClockTime
2426 gst_aggregator_get_latency_unlocked (GstAggregator * self)
2427 {
2428 GstClockTime latency;
2429
2430 g_return_val_if_fail (GST_IS_AGGREGATOR (self), 0);
2431
2432 if (!self->priv->has_peer_latency) {
2433 GstQuery *query = gst_query_new_latency ();
2434 gboolean ret;
2435
2436 ret = gst_aggregator_query_latency_unlocked (self, query);
2437 gst_query_unref (query);
2438 /* If we've been set to live, we don't wait for a peer latency, we will
2439 * simply query it again next time around */
2440 if (!ret && !self->priv->force_live)
2441 return GST_CLOCK_TIME_NONE;
2442 }
2443
2444 /* If we've been set to live, we don't wait for a peer latency, we will
2445 * simply query it again next time around */
2446 if (!self->priv->force_live) {
2447 if (!self->priv->has_peer_latency || !self->priv->peer_latency_live)
2448 return GST_CLOCK_TIME_NONE;
2449 }
2450
2451 /* latency_min is never GST_CLOCK_TIME_NONE by construction */
2452 latency = self->priv->peer_latency_min;
2453
2454 /* add our own */
2455 latency += self->priv->latency;
2456 latency += self->priv->sub_latency_min;
2457
2458 return latency;
2459 }
2460
2461 /**
2462 * gst_aggregator_get_latency:
2463 * @self: a #GstAggregator
2464 *
2465 * Retrieves the latency values reported by @self in response to the latency
2466 * query, or %GST_CLOCK_TIME_NONE if there is not live source connected and the element
2467 * will not wait for the clock.
2468 *
2469 * Typically only called by subclasses.
2470 *
2471 * Returns: The latency or %GST_CLOCK_TIME_NONE if the element does not sync
2472 */
2473 GstClockTime
2474 gst_aggregator_get_latency (GstAggregator * self)
2475 {
2476 GstClockTime ret;
2477
2478 SRC_LOCK (self);
2479 ret = gst_aggregator_get_latency_unlocked (self);
2480 SRC_UNLOCK (self);
2481
2482 return ret;
2483 }
2484
2485 static gboolean
2486 gst_aggregator_send_event (GstElement * element, GstEvent * event)
2487 {
2488 GstAggregator *self = GST_AGGREGATOR (element);
2489
2490 GST_STATE_LOCK (element);
2491 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK &&
2492 GST_STATE (element) < GST_STATE_PAUSED) {
2493 gdouble rate;
2494 GstFormat fmt;
2495 GstSeekFlags flags;
2496 GstSeekType start_type, stop_type;
2497 gint64 start, stop;
2498
2499 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
2500 &start, &stop_type, &stop);
2501
2502 GST_OBJECT_LOCK (self);
2503 gst_segment_do_seek (&GST_AGGREGATOR_PAD (self->srcpad)->segment, rate, fmt,
2504 flags, start_type, start, stop_type, stop, NULL);
2505 self->priv->next_seqnum = gst_event_get_seqnum (event);
2506 self->priv->first_buffer = FALSE;
2507 GST_OBJECT_UNLOCK (self);
2508
2509 GST_DEBUG_OBJECT (element, "Storing segment %" GST_PTR_FORMAT, event);
2510 }
2511
2512 GST_STATE_UNLOCK (element);
2513
2514
2515 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2516 SRC_LOCK (self);
2517 self->priv->got_eos_event = TRUE;
2518 SRC_BROADCAST (self);
2519 SRC_UNLOCK (self);
2520 }
2521
2522 return GST_ELEMENT_CLASS (aggregator_parent_class)->send_event (element,
2523 event);
2524 }
2525
2526 static gboolean
2527 gst_aggregator_default_src_query (GstAggregator * self, GstQuery * query)
2528 {
2529 gboolean res = TRUE;
2530
2531 switch (GST_QUERY_TYPE (query)) {
2532 case GST_QUERY_SEEKING:
2533 {
2534 GstFormat format;
2535
2536 /* don't pass it along as some (file)sink might claim it does
2537 * whereas with a collectpads in between that will not likely work */
2538 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2539 gst_query_set_seeking (query, format, FALSE, 0, -1);
2540 res = TRUE;
2541
2542 break;
2543 }
2544 case GST_QUERY_LATENCY:
2545 SRC_LOCK (self);
2546 res = gst_aggregator_query_latency_unlocked (self, query);
2547 SRC_UNLOCK (self);
2548 break;
2549 default:
2550 return gst_pad_query_default (self->srcpad, GST_OBJECT (self), query);
2551 }
2552
2553 return res;
2554 }
2555
2556 static gboolean
2557 gst_aggregator_event_forward_func (GstPad * pad, gpointer user_data)
2558 {
2559 EventData *evdata = user_data;
2560 gboolean ret = TRUE;
2561 GstPad *peer = gst_pad_get_peer (pad);
2562 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
2563
2564 if (peer) {
2565 if (evdata->only_to_active_pads && aggpad->priv->first_buffer) {
2566 GST_DEBUG_OBJECT (pad, "not sending event to inactive pad");
2567 ret = TRUE;
2568 } else {
2569 ret = gst_pad_send_event (peer, gst_event_ref (evdata->event));
2570 GST_DEBUG_OBJECT (pad, "return of event push is %d", ret);
2571 }
2572 }
2573
2574 if (ret == FALSE) {
2575 if (GST_EVENT_TYPE (evdata->event) == GST_EVENT_SEEK) {
2576 GstQuery *seeking = gst_query_new_seeking (GST_FORMAT_TIME);
2577
2578 GST_DEBUG_OBJECT (pad, "Event %" GST_PTR_FORMAT " failed", evdata->event);
2579
2580 if (gst_pad_query (peer, seeking)) {
2581 gboolean seekable;
2582
2583 gst_query_parse_seeking (seeking, NULL, &seekable, NULL, NULL);
2584
2585 if (seekable == FALSE) {
2586 GST_INFO_OBJECT (pad,
2587 "Source not seekable, We failed but it does not matter!");
2588
2589 ret = TRUE;
2590 }
2591 } else {
2592 GST_ERROR_OBJECT (pad, "Query seeking FAILED");
2593 }
2594
2595 gst_query_unref (seeking);
2596 }
2597 } else {
2598 evdata->one_actually_seeked = TRUE;
2599 }
2600
2601 evdata->result &= ret;
2602
2603 if (peer)
2604 gst_object_unref (peer);
2605
2606 /* Always send to all pads */
2607 return FALSE;
2608 }
2609
2610 static void
2611 gst_aggregator_forward_event_to_all_sinkpads (GstAggregator * self,
2612 EventData * evdata)
2613 {
2614 evdata->result = TRUE;
2615 evdata->one_actually_seeked = FALSE;
2616
2617 gst_pad_forward (self->srcpad, gst_aggregator_event_forward_func, evdata);
2618
2619 gst_event_unref (evdata->event);
2620 }
2621
2622 static gboolean
2623 gst_aggregator_do_seek (GstAggregator * self, GstEvent * event)
2624 {
2625 gdouble rate;
2626 GstFormat fmt;
2627 GstSeekFlags flags;
2628 GstSeekType start_type, stop_type;
2629 gint64 start, stop;
2630 gboolean flush;
2631 EventData evdata = { 0, };
2632 GstAggregatorPrivate *priv = self->priv;
2633
2634 gst_event_parse_seek (event, &rate, &fmt, &flags, &start_type,
2635 &start, &stop_type, &stop);
2636
2637 GST_INFO_OBJECT (self, "starting SEEK");
2638
2639 flush = flags & GST_SEEK_FLAG_FLUSH;
2640
2641 GST_OBJECT_LOCK (self);
2642
2643 if (gst_event_get_seqnum (event) == self->priv->next_seqnum) {
2644 evdata.result = TRUE;
2645 GST_DEBUG_OBJECT (self, "Dropping duplicated seek event with seqnum %d",
2646 self->priv->next_seqnum);
2647 GST_OBJECT_UNLOCK (self);
2648 goto done;
2649 }
2650
2651 self->priv->next_seqnum = gst_event_get_seqnum (event);
2652
2653 gst_segment_do_seek (&GST_AGGREGATOR_PAD (self->srcpad)->segment, rate, fmt,
2654 flags, start_type, start, stop_type, stop, NULL);
2655
2656 /* Seeking sets a position */
2657 self->priv->first_buffer = FALSE;
2658
2659 if (flush)
2660 priv->flushing = TRUE;
2661
2662 GST_OBJECT_UNLOCK (self);
2663
2664 if (flush) {
2665 GstEvent *event = gst_event_new_flush_start ();
2666
2667 gst_event_set_seqnum (event, self->priv->next_seqnum);
2668 gst_aggregator_stop_srcpad_task (self, event);
2669 }
2670
2671 /* forward the seek upstream */
2672 evdata.event = event;
2673 evdata.flush = flush;
2674 evdata.only_to_active_pads = FALSE;
2675 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2676 event = NULL;
2677
2678 if (!evdata.result || !evdata.one_actually_seeked) {
2679 GST_OBJECT_LOCK (self);
2680 priv->flushing = FALSE;
2681 GST_OBJECT_UNLOCK (self);
2682
2683 /* No flush stop is inbound for us to forward */
2684 if (flush) {
2685 GstEvent *event = gst_event_new_flush_stop (TRUE);
2686
2687 gst_event_set_seqnum (event, self->priv->next_seqnum);
2688 gst_pad_push_event (self->srcpad, event);
2689 }
2690 }
2691
2692 done:
2693 GST_INFO_OBJECT (self, "seek done, result: %d", evdata.result);
2694
2695 return evdata.result;
2696 }
2697
2698 static gboolean
2699 gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
2700 {
2701 EventData evdata = { 0, };
2702
2703 switch (GST_EVENT_TYPE (event)) {
2704 case GST_EVENT_SEEK:
2705 /* _do_seek() unrefs the event. */
2706 return gst_aggregator_do_seek (self, event);
2707 case GST_EVENT_NAVIGATION:
2708 /* specific handling has to be implemented in subclasses */
2709 gst_event_unref (event);
2710 return FALSE;
2711 case GST_EVENT_RECONFIGURE:
2712 /* We will renegotiate with downstream, we don't
2713 * need to forward this further */
2714 gst_event_unref (event);
2715 return TRUE;
2716 default:
2717 break;
2718 }
2719
2720 /* Don't forward QOS events to pads that had no active buffer yet. Otherwise
2721 * they will receive a QOS event that has earliest_time=0 (because we can't
2722 * have negative timestamps), and consider their buffer as too late */
2723 evdata.event = event;
2724 evdata.flush = FALSE;
2725 evdata.only_to_active_pads = GST_EVENT_TYPE (event) == GST_EVENT_QOS;
2726 gst_aggregator_forward_event_to_all_sinkpads (self, &evdata);
2727 return evdata.result;
2728 }
2729
2730 static gboolean
2731 gst_aggregator_src_pad_event_func (GstPad * pad, GstObject * parent,
2732 GstEvent * event)
2733 {
2734 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2735
2736 return klass->src_event (GST_AGGREGATOR (parent), event);
2737 }
2738
2739 static gboolean
2740 gst_aggregator_src_pad_query_func (GstPad * pad, GstObject * parent,
2741 GstQuery * query)
2742 {
2743 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2744
2745 return klass->src_query (GST_AGGREGATOR (parent), query);
2746 }
2747
2748 static gboolean
2749 gst_aggregator_src_pad_activate_mode_func (GstPad * pad,
2750 GstObject * parent, GstPadMode mode, gboolean active)
2751 {
2752 GstAggregator *self = GST_AGGREGATOR (parent);
2753 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (parent);
2754
2755 if (klass->src_activate) {
2756 if (klass->src_activate (self, mode, active) == FALSE) {
2757 return FALSE;
2758 }
2759 }
2760
2761 if (active == TRUE) {
2762 switch (mode) {
2763 case GST_PAD_MODE_PUSH:
2764 {
2765 GST_INFO_OBJECT (pad, "Activating pad!");
2766 gst_aggregator_start_srcpad_task (self);
2767 return TRUE;
2768 }
2769 default:
2770 {
2771 GST_ERROR_OBJECT (pad, "Only supported mode is PUSH");
2772 return FALSE;
2773 }
2774 }
2775 }
2776
2777 /* deactivating */
2778 GST_INFO_OBJECT (self, "Deactivating srcpad");
2779
2780 gst_aggregator_stop_srcpad_task (self, FALSE);
2781
2782 return TRUE;
2783 }
2784
2785 static gboolean
2786 gst_aggregator_default_sink_query (GstAggregator * self,
2787 GstAggregatorPad * aggpad, GstQuery * query)
2788 {
2789 GstPad *pad = GST_PAD (aggpad);
2790
2791 if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
2792 GstQuery *decide_query = NULL;
2793 GstAggregatorClass *agg_class;
2794 gboolean ret;
2795
2796 GST_OBJECT_LOCK (self);
2797 PAD_LOCK (aggpad);
2798 if (G_UNLIKELY (!aggpad->priv->negotiated)) {
2799 GST_DEBUG_OBJECT (self,
2800 "not negotiated yet, can't answer ALLOCATION query");
2801 PAD_UNLOCK (aggpad);
2802 GST_OBJECT_UNLOCK (self);
2803
2804 return FALSE;
2805 }
2806
2807 if ((decide_query = self->priv->allocation_query))
2808 gst_query_ref (decide_query);
2809 PAD_UNLOCK (aggpad);
2810 GST_OBJECT_UNLOCK (self);
2811
2812 GST_DEBUG_OBJECT (self,
2813 "calling propose allocation with query %" GST_PTR_FORMAT, decide_query);
2814
2815 agg_class = GST_AGGREGATOR_GET_CLASS (self);
2816
2817 /* pass the query to the propose_allocation vmethod if any */
2818 if (agg_class->propose_allocation)
2819 ret = agg_class->propose_allocation (self, aggpad, decide_query, query);
2820 else
2821 ret = FALSE;
2822
2823 if (decide_query)
2824 gst_query_unref (decide_query);
2825
2826 GST_DEBUG_OBJECT (self, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
2827 return ret;
2828 }
2829
2830 return gst_pad_query_default (pad, GST_OBJECT (self), query);
2831 }
2832
2833 static gboolean
2834 gst_aggregator_default_sink_query_pre_queue (GstAggregator * self,
2835 GstAggregatorPad * aggpad, GstQuery * query)
2836 {
2837 if (GST_QUERY_IS_SERIALIZED (query)) {
2838 GstStructure *s;
2839 gboolean ret = FALSE;
2840
2841 SRC_LOCK (self);
2842 PAD_LOCK (aggpad);
2843
2844 if (aggpad->priv->flow_return != GST_FLOW_OK) {
2845 SRC_UNLOCK (self);
2846 goto flushing;
2847 }
2848
2849 g_queue_push_head (&aggpad->priv->data, query);
2850 SRC_BROADCAST (self);
2851 SRC_UNLOCK (self);
2852
2853 /* Sanity check: aggregator's sink pad can only proccess one serialized
2854 * query at a time. */
2855 g_warn_if_fail (!aggpad->priv->query_in_proccess);
2856
2857 while ((!gst_aggregator_pad_queue_is_empty (aggpad)
2858 && aggpad->priv->flow_return == GST_FLOW_OK) ||
2859 aggpad->priv->query_in_proccess) {
2860 GST_DEBUG_OBJECT (aggpad, "Waiting for query to be consumed");
2861 PAD_WAIT_EVENT (aggpad);
2862 }
2863
2864 s = gst_query_writable_structure (query);
2865 if (gst_structure_get_boolean (s, "gst-aggregator-retval", &ret))
2866 gst_structure_remove_field (s, "gst-aggregator-retval");
2867 else
2868 g_queue_remove (&aggpad->priv->data, query);
2869
2870 if (aggpad->priv->flow_return != GST_FLOW_OK)
2871 goto flushing;
2872
2873 PAD_UNLOCK (aggpad);
2874
2875 return ret;
2876 } else {
2877 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
2878
2879 return klass->sink_query (self, aggpad, query);
2880 }
2881
2882 flushing:
2883 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping query",
2884 gst_flow_get_name (aggpad->priv->flow_return));
2885 PAD_UNLOCK (aggpad);
2886
2887 return FALSE;
2888 }
2889
2890 static void
2891 gst_aggregator_constructed (GObject * object)
2892 {
2893 GstAggregator *agg = GST_AGGREGATOR (object);
2894
2895 G_OBJECT_CLASS (aggregator_parent_class)->constructed (object);
2896
2897 if (agg->priv->force_live) {
2898 GST_OBJECT_FLAG_SET (agg, GST_ELEMENT_FLAG_SOURCE);
2899 }
2900 }
2901
2902 static void
2903 gst_aggregator_finalize (GObject * object)
2904 {
2905 GstAggregator *self = (GstAggregator *) object;
2906
2907 g_mutex_clear (&self->priv->src_lock);
2908 g_cond_clear (&self->priv->src_cond);
2909
2910 G_OBJECT_CLASS (aggregator_parent_class)->finalize (object);
2911 }
2912
2913 /*
2914 * gst_aggregator_set_latency_property:
2915 * @agg: a #GstAggregator
2916 * @latency: the new latency value (in nanoseconds).
2917 *
2918 * Sets the new latency value to @latency. This value is used to limit the
2919 * amount of time a pad waits for data to appear before considering the pad
2920 * as unresponsive.
2921 */
2922 static void
2923 gst_aggregator_set_latency_property (GstAggregator * self, GstClockTime latency)
2924 {
2925 gboolean changed;
2926
2927 g_return_if_fail (GST_IS_AGGREGATOR (self));
2928 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (latency));
2929
2930 SRC_LOCK (self);
2931 changed = (self->priv->latency != latency);
2932
2933 if (changed) {
2934 GList *item;
2935
2936 GST_OBJECT_LOCK (self);
2937 /* First lock all the pads */
2938 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2939 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2940 PAD_LOCK (aggpad);
2941 }
2942
2943 self->priv->latency = latency;
2944
2945 SRC_BROADCAST (self);
2946
2947 /* Now wake up the pads */
2948 for (item = GST_ELEMENT_CAST (self)->sinkpads; item; item = item->next) {
2949 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (item->data);
2950 PAD_BROADCAST_EVENT (aggpad);
2951 PAD_UNLOCK (aggpad);
2952 }
2953 GST_OBJECT_UNLOCK (self);
2954 }
2955
2956 SRC_UNLOCK (self);
2957
2958 if (changed)
2959 gst_element_post_message (GST_ELEMENT_CAST (self),
2960 gst_message_new_latency (GST_OBJECT_CAST (self)));
2961 }
2962
2963 /*
2964 * gst_aggregator_get_latency_property:
2965 * @agg: a #GstAggregator
2966 *
2967 * Gets the latency value. See gst_aggregator_set_latency for
2968 * more details.
2969 *
2970 * Returns: The time in nanoseconds to wait for data to arrive on a sink pad
2971 * before a pad is deemed unresponsive. A value of -1 means an
2972 * unlimited time.
2973 */
2974 static GstClockTime
2975 gst_aggregator_get_latency_property (GstAggregator * agg)
2976 {
2977 GstClockTime res;
2978
2979 g_return_val_if_fail (GST_IS_AGGREGATOR (agg), GST_CLOCK_TIME_NONE);
2980
2981 GST_OBJECT_LOCK (agg);
2982 res = agg->priv->latency;
2983 GST_OBJECT_UNLOCK (agg);
2984
2985 return res;
2986 }
2987
2988 static void
2989 gst_aggregator_set_property (GObject * object, guint prop_id,
2990 const GValue * value, GParamSpec * pspec)
2991 {
2992 GstAggregator *agg = GST_AGGREGATOR (object);
2993
2994 switch (prop_id) {
2995 case PROP_LATENCY:
2996 gst_aggregator_set_latency_property (agg, g_value_get_uint64 (value));
2997 break;
2998 case PROP_MIN_UPSTREAM_LATENCY:
2999 SRC_LOCK (agg);
3000 agg->priv->upstream_latency_min = g_value_get_uint64 (value);
3001 SRC_UNLOCK (agg);
3002 break;
3003 case PROP_START_TIME_SELECTION:
3004 agg->priv->start_time_selection = g_value_get_enum (value);
3005 break;
3006 case PROP_START_TIME:
3007 agg->priv->start_time = g_value_get_uint64 (value);
3008 break;
3009 case PROP_EMIT_SIGNALS:
3010 agg->priv->emit_signals = g_value_get_boolean (value);
3011 break;
3012 default:
3013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3014 break;
3015 }
3016 }
3017
3018 static void
3019 gst_aggregator_get_property (GObject * object, guint prop_id,
3020 GValue * value, GParamSpec * pspec)
3021 {
3022 GstAggregator *agg = GST_AGGREGATOR (object);
3023
3024 switch (prop_id) {
3025 case PROP_LATENCY:
3026 g_value_set_uint64 (value, gst_aggregator_get_latency_property (agg));
3027 break;
3028 case PROP_MIN_UPSTREAM_LATENCY:
3029 SRC_LOCK (agg);
3030 g_value_set_uint64 (value, agg->priv->upstream_latency_min);
3031 SRC_UNLOCK (agg);
3032 break;
3033 case PROP_START_TIME_SELECTION:
3034 g_value_set_enum (value, agg->priv->start_time_selection);
3035 break;
3036 case PROP_START_TIME:
3037 g_value_set_uint64 (value, agg->priv->start_time);
3038 break;
3039 case PROP_EMIT_SIGNALS:
3040 g_value_set_boolean (value, agg->priv->emit_signals);
3041 break;
3042 default:
3043 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3044 break;
3045 }
3046 }
3047
3048 /* GObject vmethods implementations */
3049 static void
3050 gst_aggregator_class_init (GstAggregatorClass * klass)
3051 {
3052 GObjectClass *gobject_class = (GObjectClass *) klass;
3053 GstElementClass *gstelement_class = (GstElementClass *) klass;
3054
3055 aggregator_parent_class = g_type_class_peek_parent (klass);
3056
3057 GST_DEBUG_CATEGORY_INIT (aggregator_debug, "aggregator",
3058 GST_DEBUG_FG_MAGENTA, "GstAggregator");
3059
3060 if (aggregator_private_offset != 0)
3061 g_type_class_adjust_private_offset (klass, &aggregator_private_offset);
3062
3063 klass->finish_buffer = gst_aggregator_default_finish_buffer;
3064 klass->finish_buffer_list = gst_aggregator_default_finish_buffer_list;
3065
3066 klass->sink_event = gst_aggregator_default_sink_event;
3067 klass->sink_query = gst_aggregator_default_sink_query;
3068
3069 klass->src_event = gst_aggregator_default_src_event;
3070 klass->src_query = gst_aggregator_default_src_query;
3071
3072 klass->create_new_pad = gst_aggregator_default_create_new_pad;
3073 klass->update_src_caps = gst_aggregator_default_update_src_caps;
3074 klass->fixate_src_caps = gst_aggregator_default_fixate_src_caps;
3075 klass->negotiated_src_caps = gst_aggregator_default_negotiated_src_caps;
3076
3077 klass->negotiate = gst_aggregator_default_negotiate;
3078
3079 klass->sink_event_pre_queue = gst_aggregator_default_sink_event_pre_queue;
3080 klass->sink_query_pre_queue = gst_aggregator_default_sink_query_pre_queue;
3081
3082 gstelement_class->request_new_pad =
3083 GST_DEBUG_FUNCPTR (gst_aggregator_request_new_pad);
3084 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_aggregator_send_event);
3085 gstelement_class->release_pad =
3086 GST_DEBUG_FUNCPTR (gst_aggregator_release_pad);
3087 gstelement_class->change_state =
3088 GST_DEBUG_FUNCPTR (gst_aggregator_change_state);
3089
3090 gobject_class->set_property = gst_aggregator_set_property;
3091 gobject_class->get_property = gst_aggregator_get_property;
3092 gobject_class->constructed = gst_aggregator_constructed;
3093 gobject_class->finalize = gst_aggregator_finalize;
3094
3095 g_object_class_install_property (gobject_class, PROP_LATENCY,
3096 g_param_spec_uint64 ("latency", "Buffer latency",
3097 "Additional latency in live mode to allow upstream "
3098 "to take longer to produce buffers for the current "
3099 "position (in nanoseconds)", 0, G_MAXUINT64,
3100 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
3101
3102 /**
3103 * GstAggregator:min-upstream-latency:
3104 *
3105 * Force minimum upstream latency (in nanoseconds). When sources with a
3106 * higher latency are expected to be plugged in dynamically after the
3107 * aggregator has started playing, this allows overriding the minimum
3108 * latency reported by the initial source(s). This is only taken into
3109 * account when larger than the actually reported minimum latency.
3110 *
3111 * Since: 1.16
3112 */
3113 g_object_class_install_property (gobject_class, PROP_MIN_UPSTREAM_LATENCY,
3114 g_param_spec_uint64 ("min-upstream-latency", "Buffer latency",
3115 "When sources with a higher latency are expected to be plugged "
3116 "in dynamically after the aggregator has started playing, "
3117 "this allows overriding the minimum latency reported by the "
3118 "initial source(s). This is only taken into account when larger "
3119 "than the actually reported minimum latency. (nanoseconds)",
3120 0, G_MAXUINT64,
3121 DEFAULT_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
3122
3123 g_object_class_install_property (gobject_class, PROP_START_TIME_SELECTION,
3124 g_param_spec_enum ("start-time-selection", "Start Time Selection",
3125 "Decides which start time is output",
3126 gst_aggregator_start_time_selection_get_type (),
3127 DEFAULT_START_TIME_SELECTION,
3128 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
3129
3130 g_object_class_install_property (gobject_class, PROP_START_TIME,
3131 g_param_spec_uint64 ("start-time", "Start Time",
3132 "Start time to use if start-time-selection=set", 0,
3133 G_MAXUINT64,
3134 DEFAULT_START_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
3135
3136 /**
3137 * GstAggregator:emit-signals:
3138 *
3139 * Enables the emission of signals such as #GstAggregator::samples-selected
3140 *
3141 * Since: 1.18
3142 */
3143 g_object_class_install_property (gobject_class, PROP_EMIT_SIGNALS,
3144 g_param_spec_boolean ("emit-signals", "Emit signals",
3145 "Send signals", DEFAULT_EMIT_SIGNALS,
3146 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
3147
3148 /**
3149 * GstAggregator::samples-selected:
3150 * @aggregator: The #GstAggregator that emitted the signal
3151 * @segment: The #GstSegment the next output buffer is part of
3152 * @pts: The presentation timestamp of the next output buffer
3153 * @dts: The decoding timestamp of the next output buffer
3154 * @duration: The duration of the next output buffer
3155 * @info: (nullable): a #GstStructure containing additional information
3156 *
3157 * Signals that the #GstAggregator subclass has selected the next set
3158 * of input samples it will aggregate. Handlers may call
3159 * gst_aggregator_peek_next_sample() at that point.
3160 *
3161 * Since: 1.18
3162 */
3163 gst_aggregator_signals[SIGNAL_SAMPLES_SELECTED] =
3164 g_signal_new ("samples-selected", G_TYPE_FROM_CLASS (klass),
3165 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 5,
3166 GST_TYPE_SEGMENT | G_SIGNAL_TYPE_STATIC_SCOPE, GST_TYPE_CLOCK_TIME,
3167 GST_TYPE_CLOCK_TIME, GST_TYPE_CLOCK_TIME,
3168 GST_TYPE_STRUCTURE | G_SIGNAL_TYPE_STATIC_SCOPE);
3169
3170 gst_meta_register_custom_simple ("GstAggregatorMissingDataMeta");
3171 }
3172
3173 static inline gpointer
3174 gst_aggregator_get_instance_private (GstAggregator * self)
3175 {
3176 return (G_STRUCT_MEMBER_P (self, aggregator_private_offset));
3177 }
3178
3179 static void
3180 gst_aggregator_init (GstAggregator * self, GstAggregatorClass * klass)
3181 {
3182 GstPadTemplate *pad_template;
3183 GstAggregatorPrivate *priv;
3184 GType pad_type;
3185
3186 g_return_if_fail (klass->aggregate != NULL);
3187
3188 self->priv = gst_aggregator_get_instance_private (self);
3189
3190 priv = self->priv;
3191
3192 pad_template =
3193 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
3194 g_return_if_fail (pad_template != NULL);
3195
3196 priv->max_padserial = -1;
3197 priv->ignore_inactive_pads = FALSE;
3198
3199 self->priv->peer_latency_live = FALSE;
3200 self->priv->peer_latency_min = self->priv->sub_latency_min = 0;
3201 self->priv->peer_latency_max = self->priv->sub_latency_max = 0;
3202 self->priv->has_peer_latency = FALSE;
3203
3204 pad_type =
3205 GST_PAD_TEMPLATE_GTYPE (pad_template) ==
3206 G_TYPE_NONE ? GST_TYPE_AGGREGATOR_PAD :
3207 GST_PAD_TEMPLATE_GTYPE (pad_template);
3208 g_assert (g_type_is_a (pad_type, GST_TYPE_AGGREGATOR_PAD));
3209 self->srcpad =
3210 g_object_new (pad_type, "name", "src", "direction", GST_PAD_SRC,
3211 "template", pad_template, NULL);
3212
3213 gst_aggregator_reset_flow_values (self);
3214
3215 gst_pad_set_event_function (self->srcpad,
3216 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_event_func));
3217 gst_pad_set_query_function (self->srcpad,
3218 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_query_func));
3219 gst_pad_set_activatemode_function (self->srcpad,
3220 GST_DEBUG_FUNCPTR (gst_aggregator_src_pad_activate_mode_func));
3221
3222 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
3223
3224 self->priv->upstream_latency_min = DEFAULT_MIN_UPSTREAM_LATENCY;
3225 self->priv->latency = DEFAULT_LATENCY;
3226 self->priv->start_time_selection = DEFAULT_START_TIME_SELECTION;
3227 self->priv->start_time = DEFAULT_START_TIME;
3228 self->priv->force_live = DEFAULT_FORCE_LIVE;
3229
3230 g_mutex_init (&self->priv->src_lock);
3231 g_cond_init (&self->priv->src_cond);
3232 }
3233
3234 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
3235 * method to get to the padtemplates */
3236 GType
3237 gst_aggregator_get_type (void)
3238 {
3239 static gsize type = 0;
3240
3241 if (g_once_init_enter (&type)) {
3242 GType _type;
3243 static const GTypeInfo info = {
3244 sizeof (GstAggregatorClass),
3245 NULL,
3246 NULL,
3247 (GClassInitFunc) gst_aggregator_class_init,
3248 NULL,
3249 NULL,
3250 sizeof (GstAggregator),
3251 0,
3252 (GInstanceInitFunc) gst_aggregator_init,
3253 };
3254
3255 _type = g_type_register_static (GST_TYPE_ELEMENT,
3256 "GstAggregator", &info, G_TYPE_FLAG_ABSTRACT);
3257
3258 aggregator_private_offset =
3259 g_type_add_instance_private (_type, sizeof (GstAggregatorPrivate));
3260
3261 g_once_init_leave (&type, _type);
3262 }
3263 return type;
3264 }
3265
3266 /* Must be called with SRC lock and PAD lock held */
3267 static gboolean
3268 gst_aggregator_pad_has_space (GstAggregator * self, GstAggregatorPad * aggpad)
3269 {
3270 guint64 max_time_level;
3271
3272 GST_TRACE_OBJECT (aggpad,
3273 "Have %" GST_TIME_FORMAT " queued in %u buffers (%" G_GUINT64_FORMAT
3274 " bytes)", GST_TIME_ARGS (aggpad->priv->time_level),
3275 aggpad->priv->num_buffers, aggpad->priv->num_bytes);
3276
3277 /* Empty queue always has space */
3278 if (aggpad->priv->num_buffers == 0 && aggpad->priv->clipped_buffer == NULL)
3279 return TRUE;
3280
3281 /* We also want at least two buffers, one is being processed and one is ready
3282 * for the next iteration when we operate in live mode. */
3283 if (is_live_unlocked (self) && aggpad->priv->num_buffers < 2)
3284 return TRUE;
3285
3286 /* On top of our latency, we also want to allow buffering up to the
3287 * minimum upstream latency to allow queue free sources with lower then
3288 * upstream latency. */
3289 max_time_level =
3290 self->priv->latency + self->priv->sub_latency_min +
3291 self->priv->peer_latency_min;
3292
3293 /* zero latency, if there is a buffer, it's full */
3294 if (max_time_level == 0)
3295 return FALSE;
3296
3297 GST_TRACE_OBJECT (aggpad, "Maximum queue level %" GST_TIME_FORMAT,
3298 GST_TIME_ARGS (max_time_level));
3299
3300 /* Allow no more buffers than the latency */
3301 return (aggpad->priv->time_level <= max_time_level);
3302 }
3303
3304 /* Must be called with the PAD_LOCK held */
3305 static void
3306 apply_buffer (GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
3307 {
3308 GstClockTime timestamp;
3309
3310 if (GST_BUFFER_DTS_IS_VALID (buffer))
3311 timestamp = GST_BUFFER_DTS (buffer);
3312 else
3313 timestamp = GST_BUFFER_PTS (buffer);
3314
3315 if (timestamp == GST_CLOCK_TIME_NONE) {
3316 if (head)
3317 timestamp = aggpad->priv->head_position;
3318 else
3319 timestamp = aggpad->priv->tail_position;
3320 }
3321
3322 /* add duration */
3323 if (GST_BUFFER_DURATION_IS_VALID (buffer))
3324 timestamp += GST_BUFFER_DURATION (buffer);
3325
3326 if (head)
3327 aggpad->priv->head_position = timestamp;
3328 else
3329 aggpad->priv->tail_position = timestamp;
3330
3331 GST_OBJECT_LOCK (aggpad);
3332 update_time_level (aggpad, head);
3333 GST_OBJECT_UNLOCK (aggpad);
3334 }
3335
3336 /*
3337 * Can be called either from the sinkpad's chain function or from the srcpad's
3338 * thread in the case of a buffer synthetized from a GAP event.
3339 * Because of this second case, FLUSH_LOCK can't be used here.
3340 */
3341
3342 static GstFlowReturn
3343 gst_aggregator_pad_chain_internal (GstAggregator * self,
3344 GstAggregatorPad * aggpad, GstBuffer * buffer, gboolean head)
3345 {
3346 GstFlowReturn flow_return;
3347 GstClockTime buf_pts;
3348 GstClockTime buf_duration;
3349
3350 GST_TRACE_OBJECT (aggpad,
3351 "entering chain internal with %" GST_PTR_FORMAT, buffer);
3352
3353 PAD_LOCK (aggpad);
3354 flow_return = aggpad->priv->flow_return;
3355 if (flow_return != GST_FLOW_OK)
3356 goto flushing;
3357
3358 PAD_UNLOCK (aggpad);
3359
3360 buf_pts = GST_BUFFER_PTS (buffer);
3361 buf_duration = GST_BUFFER_DURATION (buffer);
3362
3363 for (;;) {
3364 SRC_LOCK (self);
3365 GST_OBJECT_LOCK (self);
3366 PAD_LOCK (aggpad);
3367
3368 if (aggpad->priv->first_buffer) {
3369 self->priv->has_peer_latency = FALSE;
3370 aggpad->priv->first_buffer = FALSE;
3371 }
3372
3373 if ((gst_aggregator_pad_has_space (self, aggpad) || !head)
3374 && aggpad->priv->flow_return == GST_FLOW_OK) {
3375 if (head) {
3376 GST_DEBUG_OBJECT (aggpad, "Enqueuing %" GST_PTR_FORMAT, buffer);
3377 g_queue_push_head (&aggpad->priv->data, buffer);
3378 } else {
3379 g_queue_push_tail (&aggpad->priv->data, buffer);
3380 }
3381 apply_buffer (aggpad, buffer, head);
3382 aggpad->priv->num_buffers++;
3383 aggpad->priv->num_bytes += gst_buffer_get_size (buffer);
3384 buffer = NULL;
3385 SRC_BROADCAST (self);
3386 break;
3387 }
3388
3389 flow_return = aggpad->priv->flow_return;
3390 if (flow_return != GST_FLOW_OK) {
3391 GST_OBJECT_UNLOCK (self);
3392 SRC_UNLOCK (self);
3393 goto flushing;
3394 }
3395 GST_DEBUG_OBJECT (aggpad,
3396 "Waiting for buffer to be consumed (chain) before enqueueing %"
3397 GST_PTR_FORMAT, buffer);
3398 GST_OBJECT_UNLOCK (self);
3399 SRC_UNLOCK (self);
3400 PAD_WAIT_EVENT (aggpad);
3401
3402 PAD_UNLOCK (aggpad);
3403 }
3404
3405 if (self->priv->first_buffer) {
3406 GstClockTime start_time;
3407 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (self->srcpad);
3408
3409 switch (self->priv->start_time_selection) {
3410 case GST_AGGREGATOR_START_TIME_SELECTION_ZERO:
3411 default:
3412 start_time = 0;
3413 break;
3414 case GST_AGGREGATOR_START_TIME_SELECTION_FIRST:
3415 GST_OBJECT_LOCK (aggpad);
3416 if (aggpad->priv->head_segment.format == GST_FORMAT_TIME) {
3417 start_time = buf_pts;
3418 if (start_time != -1) {
3419 if (aggpad->priv->head_segment.rate < 0.0 && buf_duration != -1) {
3420 start_time += buf_duration;
3421 }
3422 start_time = MAX (start_time, aggpad->priv->head_segment.start);
3423 start_time =
3424 gst_segment_to_running_time (&aggpad->priv->head_segment,
3425 GST_FORMAT_TIME, start_time);
3426 }
3427 } else {
3428 start_time = 0;
3429 GST_WARNING_OBJECT (aggpad,
3430 "Ignoring request of selecting the first start time "
3431 "as the segment is a %s segment instead of a time segment",
3432 gst_format_get_name (aggpad->priv->head_segment.format));
3433 }
3434 GST_OBJECT_UNLOCK (aggpad);
3435 break;
3436 case GST_AGGREGATOR_START_TIME_SELECTION_SET:
3437 start_time = self->priv->start_time;
3438 if (start_time == -1)
3439 start_time = 0;
3440 break;
3441 case GST_AGGREGATOR_START_TIME_SELECTION_NOW:
3442 start_time = gst_element_get_current_running_time (GST_ELEMENT (self));
3443 break;
3444 }
3445
3446 if (start_time != -1) {
3447 if (srcpad->segment.position == -1)
3448 srcpad->segment.position = start_time;
3449 else
3450 srcpad->segment.position = MIN (start_time, srcpad->segment.position);
3451
3452 GST_DEBUG_OBJECT (self, "Selecting start time %" GST_TIME_FORMAT,
3453 GST_TIME_ARGS (start_time));
3454 }
3455 }
3456
3457 PAD_UNLOCK (aggpad);
3458 GST_OBJECT_UNLOCK (self);
3459 SRC_UNLOCK (self);
3460
3461 GST_TRACE_OBJECT (aggpad, "Done chaining");
3462
3463 return flow_return;
3464
3465 flushing:
3466 PAD_UNLOCK (aggpad);
3467
3468 GST_DEBUG_OBJECT (aggpad, "Pad is %s, dropping buffer",
3469 gst_flow_get_name (flow_return));
3470 if (buffer)
3471 gst_buffer_unref (buffer);
3472
3473 return flow_return;
3474 }
3475
3476 static GstFlowReturn
3477 gst_aggregator_pad_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
3478 {
3479 GstFlowReturn ret;
3480 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
3481
3482 GST_TRACE_OBJECT (aggpad, "entering chain");
3483
3484 PAD_FLUSH_LOCK (aggpad);
3485
3486 ret = gst_aggregator_pad_chain_internal (GST_AGGREGATOR_CAST (object),
3487 aggpad, buffer, TRUE);
3488
3489 PAD_FLUSH_UNLOCK (aggpad);
3490
3491 return ret;
3492 }
3493
3494 static gboolean
3495 gst_aggregator_pad_query_func (GstPad * pad, GstObject * parent,
3496 GstQuery * query)
3497 {
3498 GstAggregator *self = GST_AGGREGATOR (parent);
3499 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
3500 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
3501
3502 g_assert (klass->sink_query_pre_queue);
3503 return klass->sink_query_pre_queue (self, aggpad, query);
3504 }
3505
3506 static GstFlowReturn
3507 gst_aggregator_pad_event_func (GstPad * pad, GstObject * parent,
3508 GstEvent * event)
3509 {
3510 GstAggregator *self = GST_AGGREGATOR (parent);
3511 GstAggregatorClass *klass = GST_AGGREGATOR_GET_CLASS (self);
3512 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
3513
3514 g_assert (klass->sink_event_pre_queue);
3515 return klass->sink_event_pre_queue (self, aggpad, event);
3516 }
3517
3518 static gboolean
3519 gst_aggregator_pad_activate_mode_func (GstPad * pad,
3520 GstObject * parent, GstPadMode mode, gboolean active)
3521 {
3522 GstAggregator *self = GST_AGGREGATOR (parent);
3523 GstAggregatorPad *aggpad = GST_AGGREGATOR_PAD (pad);
3524
3525 if (active == FALSE) {
3526 SRC_LOCK (self);
3527 gst_aggregator_pad_set_flushing (aggpad, GST_FLOW_FLUSHING, TRUE);
3528 SRC_BROADCAST (self);
3529 SRC_UNLOCK (self);
3530 } else {
3531 PAD_LOCK (aggpad);
3532 aggpad->priv->flow_return = GST_FLOW_OK;
3533 PAD_BROADCAST_EVENT (aggpad);
3534 PAD_UNLOCK (aggpad);
3535 }
3536
3537 return TRUE;
3538 }
3539
3540 /***********************************
3541 * GstAggregatorPad implementation *
3542 ************************************/
3543 G_DEFINE_TYPE_WITH_PRIVATE (GstAggregatorPad, gst_aggregator_pad, GST_TYPE_PAD);
3544
3545 #define DEFAULT_PAD_EMIT_SIGNALS FALSE
3546 #define DEFAULT_PAD_CURRENT_LEVEL_TIME 0
3547 #define DEFAULT_PAD_CURRENT_LEVEL_BUFFERS 0
3548 #define DEFAULT_PAD_CURRENT_LEVEL_BYTES 0
3549
3550 enum
3551 {
3552 PAD_PROP_0,
3553 PAD_PROP_EMIT_SIGNALS,
3554 PAD_PROP_CURRENT_LEVEL_TIME,
3555 PAD_PROP_CURRENT_LEVEL_BUFFERS,
3556 PAD_PROP_CURRENT_LEVEL_BYTES,
3557 };
3558
3559 enum
3560 {
3561 PAD_SIGNAL_BUFFER_CONSUMED,
3562 PAD_LAST_SIGNAL,
3563 };
3564
3565 static guint gst_aggregator_pad_signals[PAD_LAST_SIGNAL] = { 0 };
3566
3567 static void
3568 gst_aggregator_pad_constructed (GObject * object)
3569 {
3570 GstPad *pad = GST_PAD (object);
3571
3572 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->constructed (object);
3573
3574 if (GST_PAD_IS_SINK (pad)) {
3575 gst_pad_set_chain_function (pad,
3576 GST_DEBUG_FUNCPTR (gst_aggregator_pad_chain));
3577 gst_pad_set_event_full_function_full (pad,
3578 GST_DEBUG_FUNCPTR (gst_aggregator_pad_event_func), NULL, NULL);
3579 gst_pad_set_query_function (pad,
3580 GST_DEBUG_FUNCPTR (gst_aggregator_pad_query_func));
3581 gst_pad_set_activatemode_function (pad,
3582 GST_DEBUG_FUNCPTR (gst_aggregator_pad_activate_mode_func));
3583 }
3584 }
3585
3586 static void
3587 gst_aggregator_pad_finalize (GObject * object)
3588 {
3589 GstAggregatorPad *pad = (GstAggregatorPad *) object;
3590
3591 gst_buffer_replace (&pad->priv->peeked_buffer, NULL);
3592 g_cond_clear (&pad->priv->event_cond);
3593 g_mutex_clear (&pad->priv->flush_lock);
3594 g_mutex_clear (&pad->priv->lock);
3595
3596 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->finalize (object);
3597 }
3598
3599 static void
3600 gst_aggregator_pad_dispose (GObject * object)
3601 {
3602 GstAggregatorPad *pad = (GstAggregatorPad *) object;
3603
3604 gst_aggregator_pad_set_flushing (pad, GST_FLOW_FLUSHING, TRUE);
3605
3606 G_OBJECT_CLASS (gst_aggregator_pad_parent_class)->dispose (object);
3607 }
3608
3609 static void
3610 gst_aggregator_pad_set_property (GObject * object, guint prop_id,
3611 const GValue * value, GParamSpec * pspec)
3612 {
3613 GstAggregatorPad *pad = GST_AGGREGATOR_PAD (object);
3614
3615 switch (prop_id) {
3616 case PAD_PROP_EMIT_SIGNALS:
3617 pad->priv->emit_signals = g_value_get_boolean (value);
3618 break;
3619 default:
3620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3621 break;
3622 }
3623 }
3624
3625 static void
3626 gst_aggregator_pad_get_property (GObject * object, guint prop_id,
3627 GValue * value, GParamSpec * pspec)
3628 {
3629 GstAggregatorPad *pad = GST_AGGREGATOR_PAD (object);
3630
3631 switch (prop_id) {
3632 case PAD_PROP_EMIT_SIGNALS:
3633 g_value_set_boolean (value, pad->priv->emit_signals);
3634 break;
3635 case PAD_PROP_CURRENT_LEVEL_TIME:
3636 PAD_LOCK (pad);
3637 GST_OBJECT_LOCK (pad);
3638 g_value_set_uint64 (value, pad->priv->time_level);
3639 GST_OBJECT_UNLOCK (pad);
3640 PAD_UNLOCK (pad);
3641 break;
3642 case PAD_PROP_CURRENT_LEVEL_BUFFERS:
3643 PAD_LOCK (pad);
3644 GST_OBJECT_LOCK (pad);
3645 g_value_set_uint64 (value, pad->priv->num_buffers);
3646 GST_OBJECT_UNLOCK (pad);
3647 PAD_UNLOCK (pad);
3648 break;
3649 case PAD_PROP_CURRENT_LEVEL_BYTES:
3650 PAD_LOCK (pad);
3651 GST_OBJECT_LOCK (pad);
3652 g_value_set_uint64 (value, pad->priv->num_bytes);
3653 GST_OBJECT_UNLOCK (pad);
3654 PAD_UNLOCK (pad);
3655 break;
3656 default:
3657 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3658 break;
3659 }
3660 }
3661
3662 static void
3663 gst_aggregator_pad_class_init (GstAggregatorPadClass * klass)
3664 {
3665 GObjectClass *gobject_class = (GObjectClass *) klass;
3666
3667 gobject_class->constructed = gst_aggregator_pad_constructed;
3668 gobject_class->finalize = gst_aggregator_pad_finalize;
3669 gobject_class->dispose = gst_aggregator_pad_dispose;
3670 gobject_class->set_property = gst_aggregator_pad_set_property;
3671 gobject_class->get_property = gst_aggregator_pad_get_property;
3672
3673 /**
3674 * GstAggregatorPad:buffer-consumed:
3675 * @aggregator: The #GstAggregator that emitted the signal
3676 * @buffer: The buffer that was consumed
3677 *
3678 * Signals that a buffer was consumed. As aggregator pads store buffers
3679 * in an internal queue, there is no direct match between input and output
3680 * buffers at any given time. This signal can be useful to forward metas
3681 * such as #GstVideoTimeCodeMeta or #GstVideoCaptionMeta at the right time.
3682 *
3683 * Since: 1.16
3684 */
3685 gst_aggregator_pad_signals[PAD_SIGNAL_BUFFER_CONSUMED] =
3686 g_signal_new ("buffer-consumed", G_TYPE_FROM_CLASS (klass),
3687 G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_BUFFER);
3688
3689 /**
3690 * GstAggregatorPad:emit-signals:
3691 *
3692 * Enables the emission of signals such as #GstAggregatorPad::buffer-consumed
3693 *
3694 * Since: 1.16
3695 */
3696 g_object_class_install_property (gobject_class, PAD_PROP_EMIT_SIGNALS,
3697 g_param_spec_boolean ("emit-signals", "Emit signals",
3698 "Send signals to signal data consumption", DEFAULT_PAD_EMIT_SIGNALS,
3699 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
3700
3701 /**
3702 * GstAggregatorPad:current-level-time:
3703 *
3704 * The amount of currently queued time inside this pad
3705 *
3706 * Since: 1.28
3707 */
3708 g_object_class_install_property (gobject_class, PAD_PROP_CURRENT_LEVEL_TIME,
3709 g_param_spec_uint64 ("current-level-time", "Current Level Time",
3710 "The amount of currently queued time", 0, G_MAXUINT64,
3711 DEFAULT_PAD_CURRENT_LEVEL_TIME,
3712 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3713
3714 /**
3715 * GstAggregatorPad:current-level-buffers:
3716 *
3717 * The number of currently queued buffers inside this pad
3718 *
3719 * Since: 1.28
3720 */
3721 g_object_class_install_property (gobject_class,
3722 PAD_PROP_CURRENT_LEVEL_BUFFERS,
3723 g_param_spec_uint64 ("current-level-buffers", "Current Level Buffers",
3724 "The number of currently queued buffers", 0, G_MAXUINT64,
3725 DEFAULT_PAD_CURRENT_LEVEL_BUFFERS,
3726 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3727
3728 /**
3729 * GstAggregatorPad:current-level-bytes:
3730 *
3731 * The number of currently queued bytes inside this pad
3732 *
3733 * Since: 1.28
3734 */
3735 g_object_class_install_property (gobject_class, PAD_PROP_CURRENT_LEVEL_BYTES,
3736 g_param_spec_uint64 ("current-level-bytes", "Current Level Bytes",
3737 "The number of currently queued bytes", 0, G_MAXUINT64,
3738 DEFAULT_PAD_CURRENT_LEVEL_BYTES,
3739 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3740 }
3741
3742 static void
3743 gst_aggregator_pad_init (GstAggregatorPad * pad)
3744 {
3745 pad->priv = gst_aggregator_pad_get_instance_private (pad);
3746
3747 g_queue_init (&pad->priv->data);
3748 g_cond_init (&pad->priv->event_cond);
3749
3750 g_mutex_init (&pad->priv->flush_lock);
3751 g_mutex_init (&pad->priv->lock);
3752
3753 gst_aggregator_pad_reset_unlocked (pad);
3754 pad->priv->negotiated = FALSE;
3755 pad->priv->emit_signals = DEFAULT_PAD_EMIT_SIGNALS;
3756 }
3757
3758 /* Must be called with the PAD_LOCK held */
3759 static void
3760 gst_aggregator_pad_buffer_consumed (GstAggregatorPad * pad, GstBuffer * buffer,
3761 gboolean dequeued)
3762 {
3763 if (dequeued) {
3764 pad->priv->num_buffers--;
3765 pad->priv->num_bytes -= gst_buffer_get_size (buffer);
3766 }
3767
3768 if (buffer && pad->priv->emit_signals) {
3769 g_signal_emit (pad, gst_aggregator_pad_signals[PAD_SIGNAL_BUFFER_CONSUMED],
3770 0, buffer);
3771 }
3772 PAD_BROADCAST_EVENT (pad);
3773 }
3774
3775 /* Must be called with the PAD_LOCK held */
3776 static void
3777 gst_aggregator_pad_clip_buffer_unlocked (GstAggregatorPad * pad)
3778 {
3779 GstAggregator *self = NULL;
3780 GstAggregatorClass *aggclass = NULL;
3781 GstBuffer *buffer = NULL;
3782
3783 while (pad->priv->clipped_buffer == NULL &&
3784 GST_IS_BUFFER (g_queue_peek_tail (&pad->priv->data))) {
3785 buffer = g_queue_pop_tail (&pad->priv->data);
3786
3787 apply_buffer (pad, buffer, FALSE);
3788
3789 /* We only take the parent here so that it's not taken if the buffer is
3790 * already clipped or if the queue is empty.
3791 */
3792 if (self == NULL) {
3793 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
3794 if (self == NULL) {
3795 gst_buffer_unref (buffer);
3796 return;
3797 }
3798
3799 aggclass = GST_AGGREGATOR_GET_CLASS (self);
3800 }
3801
3802 if (aggclass->clip) {
3803 GstBuffer *buffer_ref = gst_buffer_ref (buffer);
3804
3805 GST_TRACE_OBJECT (pad, "Clipping: %" GST_PTR_FORMAT, buffer);
3806
3807 buffer = aggclass->clip (self, pad, buffer);
3808
3809 if (buffer == NULL) {
3810 gst_aggregator_pad_buffer_consumed (pad, buffer_ref, TRUE);
3811 GST_TRACE_OBJECT (pad, "Clipping consumed the buffer");
3812 } else {
3813 g_assert (gst_buffer_get_size (buffer) <=
3814 gst_buffer_get_size (buffer_ref));
3815
3816 pad->priv->num_bytes -=
3817 gst_buffer_get_size (buffer_ref) - gst_buffer_get_size (buffer);
3818 }
3819
3820 gst_buffer_unref (buffer_ref);
3821 }
3822
3823 pad->priv->clipped_buffer = buffer;
3824 }
3825
3826 if (self)
3827 gst_object_unref (self);
3828 }
3829
3830 /**
3831 * gst_aggregator_pad_pop_buffer:
3832 * @pad: the pad to get buffer from
3833 *
3834 * Steal the ref to the buffer currently queued in @pad.
3835 *
3836 * Returns: (nullable) (transfer full): The buffer in @pad or NULL if no buffer was
3837 * queued. You should unref the buffer after usage.
3838 */
3839 GstBuffer *
3840 gst_aggregator_pad_pop_buffer (GstAggregatorPad * pad)
3841 {
3842 GstBuffer *buffer = NULL;
3843
3844 PAD_LOCK (pad);
3845
3846 /* If the subclass has already peeked a buffer, we guarantee
3847 * that it receives the same buffer, no matter if the pad has
3848 * errored out / been flushed in the meantime.
3849 */
3850 if (pad->priv->peeked_buffer) {
3851 buffer = pad->priv->peeked_buffer;
3852 goto done;
3853 }
3854
3855 if (pad->priv->flow_return != GST_FLOW_OK)
3856 goto done;
3857
3858 gst_aggregator_pad_clip_buffer_unlocked (pad);
3859 buffer = pad->priv->clipped_buffer;
3860
3861 done:
3862 if (buffer) {
3863 if (pad->priv->clipped_buffer != NULL) {
3864 /* Here we still hold a reference to both the clipped buffer
3865 * and possibly the peeked buffer, we transfer the first and
3866 * potentially release the second
3867 */
3868 gst_aggregator_pad_buffer_consumed (pad, buffer, TRUE);
3869 pad->priv->clipped_buffer = NULL;
3870 gst_buffer_replace (&pad->priv->peeked_buffer, NULL);
3871 } else {
3872 /* Here our clipped buffer has already been released, for
3873 * example because of a flush. We thus transfer the reference
3874 * to the peeked buffer to the caller, and we don't decrement
3875 * pad.num_buffers as it has already been done elsewhere
3876 */
3877 gst_aggregator_pad_buffer_consumed (pad, buffer, FALSE);
3878 pad->priv->peeked_buffer = NULL;
3879 }
3880 GST_DEBUG_OBJECT (pad, "Consumed: %" GST_PTR_FORMAT, buffer);
3881 }
3882
3883 PAD_UNLOCK (pad);
3884
3885 return buffer;
3886 }
3887
3888 /**
3889 * gst_aggregator_pad_drop_buffer:
3890 * @pad: the pad where to drop any pending buffer
3891 *
3892 * Drop the buffer currently queued in @pad.
3893 *
3894 * Returns: TRUE if there was a buffer queued in @pad, or FALSE if not.
3895 */
3896 gboolean
3897 gst_aggregator_pad_drop_buffer (GstAggregatorPad * pad)
3898 {
3899 GstBuffer *buf;
3900
3901 buf = gst_aggregator_pad_pop_buffer (pad);
3902
3903 if (buf == NULL)
3904 return FALSE;
3905
3906 gst_buffer_unref (buf);
3907 return TRUE;
3908 }
3909
3910 /**
3911 * gst_aggregator_pad_peek_buffer:
3912 * @pad: the pad to get buffer from
3913 *
3914 * Returns: (nullable) (transfer full): A reference to the buffer in @pad or
3915 * NULL if no buffer was queued. You should unref the buffer after
3916 * usage.
3917 */
3918 GstBuffer *
3919 gst_aggregator_pad_peek_buffer (GstAggregatorPad * pad)
3920 {
3921 GstBuffer *buffer = NULL;
3922
3923 PAD_LOCK (pad);
3924
3925 if (pad->priv->peeked_buffer) {
3926 buffer = gst_buffer_ref (pad->priv->peeked_buffer);
3927 goto done;
3928 }
3929
3930 if (pad->priv->flow_return != GST_FLOW_OK)
3931 goto done;
3932
3933 gst_aggregator_pad_clip_buffer_unlocked (pad);
3934
3935 if (pad->priv->clipped_buffer) {
3936 buffer = gst_buffer_ref (pad->priv->clipped_buffer);
3937 pad->priv->peeked_buffer = gst_buffer_ref (buffer);
3938 } else {
3939 buffer = NULL;
3940 }
3941
3942 done:
3943 PAD_UNLOCK (pad);
3944 return buffer;
3945 }
3946
3947 /**
3948 * gst_aggregator_pad_has_buffer:
3949 * @pad: the pad to check the buffer on
3950 *
3951 * This checks if a pad has a buffer available that will be returned by
3952 * a call to gst_aggregator_pad_peek_buffer() or
3953 * gst_aggregator_pad_pop_buffer().
3954 *
3955 * Returns: %TRUE if the pad has a buffer available as the next thing.
3956 *
3957 * Since: 1.14.1
3958 */
3959 gboolean
3960 gst_aggregator_pad_has_buffer (GstAggregatorPad * pad)
3961 {
3962 gboolean has_buffer;
3963
3964 PAD_LOCK (pad);
3965
3966 if (pad->priv->peeked_buffer) {
3967 has_buffer = TRUE;
3968 } else {
3969 gst_aggregator_pad_clip_buffer_unlocked (pad);
3970 has_buffer = (pad->priv->clipped_buffer != NULL);
3971 if (has_buffer)
3972 pad->priv->peeked_buffer = gst_buffer_ref (pad->priv->clipped_buffer);
3973 }
3974 PAD_UNLOCK (pad);
3975
3976 return has_buffer;
3977 }
3978
3979 /**
3980 * gst_aggregator_pad_is_eos:
3981 * @pad: an aggregator pad
3982 *
3983 * Returns: %TRUE if the pad is EOS, otherwise %FALSE.
3984 */
3985 gboolean
3986 gst_aggregator_pad_is_eos (GstAggregatorPad * pad)
3987 {
3988 gboolean is_eos;
3989
3990 PAD_LOCK (pad);
3991 is_eos = pad->priv->eos;
3992 PAD_UNLOCK (pad);
3993
3994 return is_eos;
3995 }
3996
3997 /**
3998 * gst_aggregator_pad_is_inactive:
3999 * @pad: an aggregator pad
4000 *
4001 * It is only valid to call this method from #GstAggregatorClass::aggregate()
4002 *
4003 * Returns: %TRUE if the pad is inactive, %FALSE otherwise.
4004 * See gst_aggregator_ignore_inactive_pads() for more info.
4005 * Since: 1.20
4006 */
4007 gboolean
4008 gst_aggregator_pad_is_inactive (GstAggregatorPad * pad)
4009 {
4010 GstAggregator *self;
4011 gboolean inactive;
4012
4013 self = GST_AGGREGATOR (gst_pad_get_parent_element (GST_PAD (pad)));
4014 if (!self)
4015 return FALSE;
4016
4017 PAD_LOCK (pad);
4018 inactive = self->priv->ignore_inactive_pads && is_live_unlocked (self)
4019 && pad->priv->first_buffer;
4020 PAD_UNLOCK (pad);
4021
4022 gst_object_unref (self);
4023
4024 return inactive;
4025 }
4026
4027 /**
4028 * gst_aggregator_set_latency:
4029 * @self: a #GstAggregator
4030 * @min_latency: minimum latency
4031 * @max_latency: maximum latency
4032 *
4033 * Lets #GstAggregator sub-classes tell the baseclass what their internal
4034 * latency is. Will also post a LATENCY message on the bus so the pipeline
4035 * can reconfigure its global latency if the values changed.
4036 */
4037 void
4038 gst_aggregator_set_latency (GstAggregator * self,
4039 GstClockTime min_latency, GstClockTime max_latency)
4040 {
4041 gboolean changed = FALSE;
4042
4043 g_return_if_fail (GST_IS_AGGREGATOR (self));
4044 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
4045 g_return_if_fail (max_latency >= min_latency);
4046
4047 SRC_LOCK (self);
4048 if (self->priv->sub_latency_min != min_latency) {
4049 self->priv->sub_latency_min = min_latency;
4050 changed = TRUE;
4051 }
4052 if (self->priv->sub_latency_max != max_latency) {
4053 self->priv->sub_latency_max = max_latency;
4054 changed = TRUE;
4055 }
4056 if (!self->priv->posted_latency_msg) {
4057 self->priv->posted_latency_msg = TRUE;
4058 changed = TRUE;
4059 }
4060
4061 if (changed)
4062 SRC_BROADCAST (self);
4063 SRC_UNLOCK (self);
4064
4065 if (changed) {
4066 gst_element_post_message (GST_ELEMENT_CAST (self),
4067 gst_message_new_latency (GST_OBJECT_CAST (self)));
4068 }
4069 }
4070
4071 /**
4072 * gst_aggregator_get_buffer_pool:
4073 * @self: a #GstAggregator
4074 *
4075 * Returns: (transfer full) (nullable): the instance of the #GstBufferPool used
4076 * by @trans; free it after use it
4077 */
4078 GstBufferPool *
4079 gst_aggregator_get_buffer_pool (GstAggregator * self)
4080 {
4081 GstBufferPool *pool;
4082
4083 g_return_val_if_fail (GST_IS_AGGREGATOR (self), NULL);
4084
4085 GST_OBJECT_LOCK (self);
4086 pool = self->priv->pool;
4087 if (pool)
4088 gst_object_ref (pool);
4089 GST_OBJECT_UNLOCK (self);
4090
4091 return pool;
4092 }
4093
4094 /**
4095 * gst_aggregator_get_allocator:
4096 * @self: a #GstAggregator
4097 * @allocator: (out) (optional) (nullable) (transfer full): the #GstAllocator
4098 * used
4099 * @params: (out caller-allocates) (optional): the
4100 * #GstAllocationParams of @allocator
4101 *
4102 * Lets #GstAggregator sub-classes get the memory @allocator
4103 * acquired by the base class and its @params.
4104 *
4105 * Unref the @allocator after use it.
4106 */
4107 void
4108 gst_aggregator_get_allocator (GstAggregator * self,
4109 GstAllocator ** allocator, GstAllocationParams * params)
4110 {
4111 g_return_if_fail (GST_IS_AGGREGATOR (self));
4112
4113 if (allocator)
4114 *allocator = self->priv->allocator ?
4115 gst_object_ref (self->priv->allocator) : NULL;
4116
4117 if (params)
4118 *params = self->priv->allocation_params;
4119 }
4120
4121 /**
4122 * gst_aggregator_simple_get_next_time:
4123 * @self: A #GstAggregator
4124 *
4125 * This is a simple #GstAggregatorClass::get_next_time implementation that
4126 * just looks at the #GstSegment on the srcpad of the aggregator and bases
4127 * the next time on the running time there.
4128 *
4129 * This is the desired behaviour in most cases where you have a live source
4130 * and you have a dead line based aggregator subclass.
4131 *
4132 * Returns: The running time based on the position
4133 *
4134 * Since: 1.16
4135 */
4136 GstClockTime
4137 gst_aggregator_simple_get_next_time (GstAggregator * self)
4138 {
4139 GstClockTime next_time;
4140 GstAggregatorPad *srcpad = GST_AGGREGATOR_PAD (self->srcpad);
4141 GstSegment *segment = &srcpad->segment;
4142
4143 GST_OBJECT_LOCK (self);
4144 if (segment->format != GST_FORMAT_TIME) {
4145 GST_OBJECT_UNLOCK (self);
4146 return GST_CLOCK_TIME_NONE;
4147 }
4148
4149 if (segment->position == -1 || segment->position < segment->start)
4150 next_time = segment->start;
4151 else
4152 next_time = segment->position;
4153
4154 if (segment->stop != -1 && next_time > segment->stop)
4155 next_time = segment->stop;
4156
4157 next_time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, next_time);
4158 GST_OBJECT_UNLOCK (self);
4159
4160 return next_time;
4161 }
4162
4163 /**
4164 * gst_aggregator_update_segment:
4165 * @self: A #GstAggregator
4166 * @segment: The new #GstSegment
4167 *
4168 * Subclasses should use this to update the segment on their
4169 * source pad, instead of directly pushing new segment events
4170 * downstream.
4171 *
4172 * Subclasses MUST call this before gst_aggregator_selected_samples(),
4173 * if it is used at all.
4174 *
4175 * Since: 1.18
4176 */
4177 void
4178 gst_aggregator_update_segment (GstAggregator * self, const GstSegment * segment)
4179 {
4180 g_return_if_fail (GST_IS_AGGREGATOR (self));
4181 g_return_if_fail (segment != NULL);
4182
4183 GST_INFO_OBJECT (self, "Updating srcpad segment: %" GST_SEGMENT_FORMAT,
4184 segment);
4185
4186 GST_OBJECT_LOCK (self);
4187 GST_AGGREGATOR_PAD (self->srcpad)->segment = *segment;
4188 self->priv->send_segment = TRUE;
4189 /* we have a segment from the subclass now and really shouldn't override
4190 * anything in that segment anymore, like the segment.position */
4191 self->priv->first_buffer = FALSE;
4192 GST_OBJECT_UNLOCK (self);
4193 }
4194
4195 /**
4196 * gst_aggregator_selected_samples:
4197 * @self: A #GstAggregator
4198 * @pts: The presentation timestamp of the next output buffer
4199 * @dts: The decoding timestamp of the next output buffer
4200 * @duration: The duration of the next output buffer
4201 * @info: (nullable): a #GstStructure containing additional information
4202 *
4203 * Subclasses should call this when they have prepared the
4204 * buffers they will aggregate for each of their sink pads, but
4205 * before using any of the properties of the pads that govern
4206 * *how* aggregation should be performed, for example z-index
4207 * for video aggregators.
4208 *
4209 * If gst_aggregator_update_segment() is used by the subclass,
4210 * it MUST be called before gst_aggregator_selected_samples().
4211 *
4212 * This function MUST only be called from the #GstAggregatorClass::aggregate()
4213 * function.
4214 *
4215 * Since: 1.18
4216 */
4217 void
4218 gst_aggregator_selected_samples (GstAggregator * self,
4219 GstClockTime pts, GstClockTime dts, GstClockTime duration,
4220 GstStructure * info)
4221 {
4222 g_return_if_fail (GST_IS_AGGREGATOR (self));
4223
4224 if (self->priv->emit_signals) {
4225 g_signal_emit (self, gst_aggregator_signals[SIGNAL_SAMPLES_SELECTED], 0,
4226 &GST_AGGREGATOR_PAD (self->srcpad)->segment, pts, dts, duration, info);
4227 }
4228
4229 self->priv->selected_samples_called_or_warned = TRUE;
4230 }
4231
4232 /**
4233 * gst_aggregator_set_ignore_inactive_pads:
4234 * @self: A #GstAggregator
4235 * @ignore: whether inactive pads should not be waited on
4236 *
4237 * Subclasses should call this when they don't want to time out
4238 * waiting for a pad that hasn't yet received any buffers in live
4239 * mode.
4240 *
4241 * #GstAggregator will still wait once on each newly-added pad, making
4242 * sure upstream has had a fair chance to start up.
4243 *
4244 * Since: 1.20
4245 */
4246 void
4247 gst_aggregator_set_ignore_inactive_pads (GstAggregator * self, gboolean ignore)
4248 {
4249 g_return_if_fail (GST_IS_AGGREGATOR (self));
4250
4251 GST_OBJECT_LOCK (self);
4252 self->priv->ignore_inactive_pads = ignore;
4253 GST_OBJECT_UNLOCK (self);
4254 }
4255
4256 /**
4257 * gst_aggregator_get_ignore_inactive_pads:
4258 * @self: A #GstAggregator
4259 *
4260 * Returns: whether inactive pads will not be waited on
4261 * Since: 1.20
4262 */
4263 gboolean
4264 gst_aggregator_get_ignore_inactive_pads (GstAggregator * self)
4265 {
4266 gboolean ret;
4267
4268 g_return_val_if_fail (GST_IS_AGGREGATOR (self), FALSE);
4269
4270 GST_OBJECT_LOCK (self);
4271 ret = self->priv->ignore_inactive_pads;
4272 GST_OBJECT_UNLOCK (self);
4273
4274 return ret;
4275 }
4276
4277 /**
4278 * gst_aggregator_get_force_live:
4279 * @self: A #GstAggregator
4280 *
4281 * Subclasses may use the return value to inform whether they should return
4282 * %GST_FLOW_EOS from their aggregate implementation.
4283 *
4284 * Returns: whether live status was forced on @self.
4285 *
4286 * Since: 1.22
4287 */
4288 gboolean
4289 gst_aggregator_get_force_live (GstAggregator * self)
4290 {
4291 return self->priv->force_live;
4292 }
4293
4294 /**
4295 * gst_aggregator_set_force_live:
4296 * @self: A #GstAggregator
4297 * @force_live: The new value
4298 *
4299 * Subclasses should call this at construction time in order for @self to
4300 * aggregate on a timeout even when no live source is connected.
4301 *
4302 * Since: 1.22
4303 */
4304 void
4305 gst_aggregator_set_force_live (GstAggregator * self, gboolean force_live)
4306 {
4307 self->priv->force_live = force_live;
4308 }