2 * Farsight Voice+Video library
4 * Copyright 2007 Collabora Ltd,
5 * Copyright 2007 Nokia Corporation
6 * @author: Philippe Kalaf <philippe.kalaf@collabora.co.uk>.
7 * Copyright 2007 Wim Taymans <wim.taymans@gmail.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * SECTION:element-gstrtpjitterbuffer
29 * This element reorders and removes duplicate RTP packets as they are received
30 * from a network source. It will also wait for missing packets up to a
31 * configurable time limit using the #GstRtpJitterBuffer:latency property.
32 * Packets arriving too late are considered to be lost packets.
34 * This element acts as a live element and so adds #GstRtpJitterBuffer:latency
37 * The element needs the clock-rate of the RTP payload in order to estimate the
38 * delay. This information is obtained either from the caps on the sink pad or,
39 * when no caps are present, from the #GstRtpJitterBuffer::request-pt-map signal.
40 * To clear the previous pt-map use the #GstRtpJitterBuffer::clear-pt-map signal.
42 * This element will automatically be used inside gstrtpbin.
45 * <title>Example pipelines</title>
47 * gst-launch-1.0 rtspsrc location=rtsp://192.168.1.133:8554/mpeg1or2AudioVideoTest ! gstrtpjitterbuffer ! rtpmpvdepay ! mpeg2dec ! xvimagesink
48 * ]| Connect to a streaming server and decode the MPEG video. The jitterbuffer is
49 * inserted into the pipeline to smooth out network jitter and to reorder the
50 * out-of-order RTP packets.
53 * Last reviewed on 2007-05-28 (0.10.5)
62 #include <gst/rtp/gstrtpbuffer.h>
64 #include "gstrtpjitterbuffer.h"
65 #include "rtpjitterbuffer.h"
68 #include <gst/glib-compat-private.h>
70 GST_DEBUG_CATEGORY (rtpjitterbuffer_debug);
71 #define GST_CAT_DEFAULT (rtpjitterbuffer_debug)
73 /* RTPJitterBuffer signals and args */
76 SIGNAL_REQUEST_PT_MAP,
84 #define DEFAULT_LATENCY_MS 200
85 #define DEFAULT_DROP_ON_LATENCY FALSE
86 #define DEFAULT_TS_OFFSET 0
87 #define DEFAULT_DO_LOST FALSE
88 #define DEFAULT_MODE RTP_JITTER_BUFFER_MODE_SLAVE
89 #define DEFAULT_PERCENT 0
90 #define DEFAULT_DO_RETRANSMISSION FALSE
91 #define DEFAULT_RTX_DELAY 20
92 #define DEFAULT_RTX_DELAY_REORDER 3
93 #define DEFAULT_RTX_RETRY_TIMEOUT 40
94 #define DEFAULT_RTX_RETRY_PERIOD 160
100 PROP_DROP_ON_LATENCY,
105 PROP_DO_RETRANSMISSION,
107 PROP_RTX_DELAY_REORDER,
108 PROP_RTX_RETRY_TIMEOUT,
109 PROP_RTX_RETRY_PERIOD,
113 #define JBUF_LOCK(priv) (g_mutex_lock (&(priv)->jbuf_lock))
115 #define JBUF_LOCK_CHECK(priv,label) G_STMT_START { \
117 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
120 #define JBUF_UNLOCK(priv) (g_mutex_unlock (&(priv)->jbuf_lock))
122 #define JBUF_WAIT_TIMER(priv) G_STMT_START { \
123 (priv)->waiting_timer = TRUE; \
124 g_cond_wait (&(priv)->jbuf_timer, &(priv)->jbuf_lock); \
125 (priv)->waiting_timer = FALSE; \
127 #define JBUF_SIGNAL_TIMER(priv) G_STMT_START { \
128 if (G_UNLIKELY ((priv)->waiting_timer)) \
129 g_cond_signal (&(priv)->jbuf_timer); \
132 #define JBUF_WAIT_EVENT(priv,label) G_STMT_START { \
133 (priv)->waiting_event = TRUE; \
134 g_cond_wait (&(priv)->jbuf_event, &(priv)->jbuf_lock); \
135 (priv)->waiting_event = FALSE; \
136 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
139 #define JBUF_SIGNAL_EVENT(priv) G_STMT_START { \
140 if (G_UNLIKELY ((priv)->waiting_event)) \
141 g_cond_signal (&(priv)->jbuf_event); \
144 struct _GstRtpJitterBufferPrivate
146 GstPad *sinkpad, *srcpad;
149 RTPJitterBuffer *jbuf;
151 gboolean waiting_timer;
153 gboolean waiting_event;
160 gboolean timer_running;
161 GThread *timer_thread;
166 gboolean drop_on_latency;
169 gboolean do_retransmission;
171 gint rtx_delay_reorder;
172 gint rtx_retry_timeout;
173 gint rtx_retry_period;
175 /* the last seqnum we pushed out */
176 guint32 last_popped_seqnum;
177 /* the next expected seqnum we push */
179 /* last output time */
180 GstClockTime last_out_time;
181 /* last valid input timestamp and rtptime pair */
182 GstClockTime ips_dts;
184 GstClockTime packet_spacing;
186 /* the next expected seqnum we receive */
187 GstClockTime last_in_dts;
188 guint32 last_in_seqnum;
189 guint32 next_in_seqnum;
193 /* start and stop ranges */
194 GstClockTime npt_start;
195 GstClockTime npt_stop;
196 guint64 ext_timestamp;
197 guint64 last_elapsed;
198 guint64 estimated_eos;
204 /* clock rate and rtp timestamp offset */
208 gint64 prev_ts_offset;
210 /* when we are shutting down */
211 GstFlowReturn srcresult;
217 GstClockTime timer_timeout;
218 guint16 timer_seqnum;
219 gboolean unscheduled;
220 /* the latency of the upstream peer, we have to take this into account when
221 * synchronizing the buffers. */
222 GstClockTime peer_latency;
226 /* some accounting */
228 guint64 num_duplicates;
244 GstClockTime timeout;
245 GstClockTime rtx_base;
246 GstClockTime rtx_retry;
249 #define GST_RTP_JITTER_BUFFER_GET_PRIVATE(o) \
250 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_RTP_JITTER_BUFFER, \
251 GstRtpJitterBufferPrivate))
253 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_template =
254 GST_STATIC_PAD_TEMPLATE ("sink",
257 GST_STATIC_CAPS ("application/x-rtp, "
258 "clock-rate = (int) [ 1, 2147483647 ]"
259 /* "payload = (int) , "
260 * "encoding-name = (string) "
264 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_rtcp_template =
265 GST_STATIC_PAD_TEMPLATE ("sink_rtcp",
268 GST_STATIC_CAPS ("application/x-rtcp")
271 static GstStaticPadTemplate gst_rtp_jitter_buffer_src_template =
272 GST_STATIC_PAD_TEMPLATE ("src",
275 GST_STATIC_CAPS ("application/x-rtp"
276 /* "payload = (int) , "
277 * "clock-rate = (int) , "
278 * "encoding-name = (string) "
282 static guint gst_rtp_jitter_buffer_signals[LAST_SIGNAL] = { 0 };
284 #define gst_rtp_jitter_buffer_parent_class parent_class
285 G_DEFINE_TYPE (GstRtpJitterBuffer, gst_rtp_jitter_buffer, GST_TYPE_ELEMENT);
287 /* object overrides */
288 static void gst_rtp_jitter_buffer_set_property (GObject * object,
289 guint prop_id, const GValue * value, GParamSpec * pspec);
290 static void gst_rtp_jitter_buffer_get_property (GObject * object,
291 guint prop_id, GValue * value, GParamSpec * pspec);
292 static void gst_rtp_jitter_buffer_finalize (GObject * object);
294 /* element overrides */
295 static GstStateChangeReturn gst_rtp_jitter_buffer_change_state (GstElement
296 * element, GstStateChange transition);
297 static GstPad *gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
298 GstPadTemplate * templ, const gchar * name, const GstCaps * filter);
299 static void gst_rtp_jitter_buffer_release_pad (GstElement * element,
301 static GstClock *gst_rtp_jitter_buffer_provide_clock (GstElement * element);
304 static GstCaps *gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter);
305 static GstIterator *gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad,
308 /* sinkpad overrides */
309 static gboolean gst_rtp_jitter_buffer_sink_event (GstPad * pad,
310 GstObject * parent, GstEvent * event);
311 static GstFlowReturn gst_rtp_jitter_buffer_chain (GstPad * pad,
312 GstObject * parent, GstBuffer * buffer);
314 static gboolean gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad,
315 GstObject * parent, GstEvent * event);
316 static GstFlowReturn gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad,
317 GstObject * parent, GstBuffer * buffer);
319 static gboolean gst_rtp_jitter_buffer_sink_query (GstPad * pad,
320 GstObject * parent, GstQuery * query);
322 /* srcpad overrides */
323 static gboolean gst_rtp_jitter_buffer_src_event (GstPad * pad,
324 GstObject * parent, GstEvent * event);
325 static gboolean gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad,
326 GstObject * parent, GstPadMode mode, gboolean active);
327 static void gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer);
328 static gboolean gst_rtp_jitter_buffer_src_query (GstPad * pad,
329 GstObject * parent, GstQuery * query);
332 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer);
334 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jitterbuffer,
335 gboolean active, guint64 base_time);
336 static void do_handle_sync (GstRtpJitterBuffer * jitterbuffer);
338 static void unschedule_current_timer (GstRtpJitterBuffer * jitterbuffer);
339 static void remove_all_timers (GstRtpJitterBuffer * jitterbuffer);
341 static void wait_next_timeout (GstRtpJitterBuffer * jitterbuffer);
344 gst_rtp_jitter_buffer_class_init (GstRtpJitterBufferClass * klass)
346 GObjectClass *gobject_class;
347 GstElementClass *gstelement_class;
349 gobject_class = (GObjectClass *) klass;
350 gstelement_class = (GstElementClass *) klass;
352 g_type_class_add_private (klass, sizeof (GstRtpJitterBufferPrivate));
354 gobject_class->finalize = gst_rtp_jitter_buffer_finalize;
356 gobject_class->set_property = gst_rtp_jitter_buffer_set_property;
357 gobject_class->get_property = gst_rtp_jitter_buffer_get_property;
360 * GstRtpJitterBuffer::latency:
362 * The maximum latency of the jitterbuffer. Packets will be kept in the buffer
363 * for at most this time.
365 g_object_class_install_property (gobject_class, PROP_LATENCY,
366 g_param_spec_uint ("latency", "Buffer latency in ms",
367 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 * GstRtpJitterBuffer::drop-on-latency:
372 * Drop oldest buffers when the queue is completely filled.
374 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
375 g_param_spec_boolean ("drop-on-latency",
376 "Drop buffers when maximum latency is reached",
377 "Tells the jitterbuffer to never exceed the given latency in size",
378 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 * GstRtpJitterBuffer::ts-offset:
382 * Adjust GStreamer output buffer timestamps in the jitterbuffer with offset.
383 * This is mainly used to ensure interstream synchronisation.
385 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
386 g_param_spec_int64 ("ts-offset", "Timestamp Offset",
387 "Adjust buffer timestamps with offset in nanoseconds", G_MININT64,
388 G_MAXINT64, DEFAULT_TS_OFFSET,
389 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 * GstRtpJitterBuffer::do-lost:
394 * Send out a GstRTPPacketLost event downstream when a packet is considered
397 g_object_class_install_property (gobject_class, PROP_DO_LOST,
398 g_param_spec_boolean ("do-lost", "Do Lost",
399 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 * GstRtpJitterBuffer::mode:
405 * Control the buffering and timestamping mode used by the jitterbuffer.
407 g_object_class_install_property (gobject_class, PROP_MODE,
408 g_param_spec_enum ("mode", "Mode",
409 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
410 DEFAULT_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 * GstRtpJitterBuffer::percent:
414 * The percent of the jitterbuffer that is filled.
418 g_object_class_install_property (gobject_class, PROP_PERCENT,
419 g_param_spec_int ("percent", "percent",
420 "The buffer filled percent", 0, 100,
421 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
423 * GstRtpJitterBuffer::do-retransmission:
425 * Send out a GstRTPRetransmission event upstream when a packet is considered
426 * late and should be retransmitted.
430 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
431 g_param_spec_boolean ("do-retransmission", "Do Retransmission",
432 "Send retransmission events upstream when a packet is late",
433 DEFAULT_DO_RETRANSMISSION,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 * GstRtpJitterBuffer::rtx-delay:
439 * When a packet did not arrive at the expected time, wait this extra amount
440 * of time before sending a retransmission event.
442 * When -1 is used, the max jitter will be used as extra delay.
446 g_object_class_install_property (gobject_class, PROP_RTX_DELAY,
447 g_param_spec_int ("rtx-delay", "RTX Delay",
448 "Extra time in ms to wait before sending retransmission "
449 "event (-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_DELAY,
450 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
452 * GstRtpJitterBuffer::rtx-delay-reorder:
454 * Assume that a retransmission event should be sent when we see
455 * this much packet reordering.
457 * When -1 is used, the value will be estimated based on observed packet
462 g_object_class_install_property (gobject_class, PROP_RTX_DELAY_REORDER,
463 g_param_spec_int ("rtx-delay-reorder", "RTX Delay Reorder",
464 "Sending retransmission event when this much reordering (-1 automatic)",
465 -1, G_MAXINT, DEFAULT_RTX_DELAY_REORDER,
466 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
468 * GstRtpJitterBuffer::rtx-retry-timeout:
470 * When no packet has been received after sending a retransmission event
471 * for this time, retry sending a retransmission event.
473 * When -1 is used, the value will be estimated based on observed round
478 g_object_class_install_property (gobject_class, PROP_RTX_RETRY_TIMEOUT,
479 g_param_spec_int ("rtx-retry-timeout", "RTX Retry Timeout",
480 "Retry sending a transmission event after this timeout in "
481 "ms (-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_RETRY_TIMEOUT,
482 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
484 * GstRtpJitterBuffer::rtx-retry-period:
486 * The amount of time to try to get a retransmission.
488 * When -1 is used, the value will be estimated based on the jitterbuffer
489 * latency and the observed round trip time.
493 g_object_class_install_property (gobject_class, PROP_RTX_RETRY_PERIOD,
494 g_param_spec_int ("rtx-retry-period", "RTX Retry Period",
495 "Try to get a retransmission for this many ms "
496 "(-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_RETRY_PERIOD,
497 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
500 * GstRtpJitterBuffer::request-pt-map:
501 * @buffer: the object which received the signal
504 * Request the payload type as #GstCaps for @pt.
506 gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP] =
507 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
508 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
509 request_pt_map), NULL, NULL, g_cclosure_marshal_generic,
510 GST_TYPE_CAPS, 1, G_TYPE_UINT);
512 * GstRtpJitterBuffer::handle-sync:
513 * @buffer: the object which received the signal
514 * @struct: a GstStructure containing sync values.
516 * Be notified of new sync values.
518 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC] =
519 g_signal_new ("handle-sync", G_TYPE_FROM_CLASS (klass),
520 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
521 handle_sync), NULL, NULL, g_cclosure_marshal_VOID__BOXED,
522 G_TYPE_NONE, 1, GST_TYPE_STRUCTURE | G_SIGNAL_TYPE_STATIC_SCOPE);
525 * GstRtpJitterBuffer::on-npt-stop
526 * @buffer: the object which received the signal
528 * Signal that the jitterbufer has pushed the RTP packet that corresponds to
529 * the npt-stop position.
531 gst_rtp_jitter_buffer_signals[SIGNAL_ON_NPT_STOP] =
532 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
533 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
534 on_npt_stop), NULL, NULL, g_cclosure_marshal_VOID__VOID,
535 G_TYPE_NONE, 0, G_TYPE_NONE);
538 * GstRtpJitterBuffer::clear-pt-map:
539 * @buffer: the object which received the signal
541 * Invalidate the clock-rate as obtained with the
542 * #GstRtpJitterBuffer::request-pt-map signal.
544 gst_rtp_jitter_buffer_signals[SIGNAL_CLEAR_PT_MAP] =
545 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
546 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
547 G_STRUCT_OFFSET (GstRtpJitterBufferClass, clear_pt_map), NULL, NULL,
548 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
551 * GstRtpJitterBuffer::set-active:
552 * @buffer: the object which received the signal
554 * Start pushing out packets with the given base time. This signal is only
555 * useful in buffering mode.
557 * Returns: the time of the last pushed packet.
561 gst_rtp_jitter_buffer_signals[SIGNAL_SET_ACTIVE] =
562 g_signal_new ("set-active", G_TYPE_FROM_CLASS (klass),
563 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
564 G_STRUCT_OFFSET (GstRtpJitterBufferClass, set_active), NULL, NULL,
565 g_cclosure_marshal_generic, G_TYPE_UINT64, 2, G_TYPE_BOOLEAN,
568 gstelement_class->change_state =
569 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_change_state);
570 gstelement_class->request_new_pad =
571 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_request_new_pad);
572 gstelement_class->release_pad =
573 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_release_pad);
574 gstelement_class->provide_clock =
575 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_provide_clock);
577 gst_element_class_add_pad_template (gstelement_class,
578 gst_static_pad_template_get (&gst_rtp_jitter_buffer_src_template));
579 gst_element_class_add_pad_template (gstelement_class,
580 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_template));
581 gst_element_class_add_pad_template (gstelement_class,
582 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_rtcp_template));
584 gst_element_class_set_static_metadata (gstelement_class,
585 "RTP packet jitter-buffer", "Filter/Network/RTP",
586 "A buffer that deals with network jitter and other transmission faults",
587 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
588 "Wim Taymans <wim.taymans@gmail.com>");
590 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_clear_pt_map);
591 klass->set_active = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_set_active);
593 GST_DEBUG_CATEGORY_INIT
594 (rtpjitterbuffer_debug, "gstrtpjitterbuffer", 0, "RTP Jitter Buffer");
598 gst_rtp_jitter_buffer_init (GstRtpJitterBuffer * jitterbuffer)
600 GstRtpJitterBufferPrivate *priv;
602 priv = GST_RTP_JITTER_BUFFER_GET_PRIVATE (jitterbuffer);
603 jitterbuffer->priv = priv;
605 priv->latency_ms = DEFAULT_LATENCY_MS;
606 priv->latency_ns = priv->latency_ms * GST_MSECOND;
607 priv->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
608 priv->do_lost = DEFAULT_DO_LOST;
609 priv->do_retransmission = DEFAULT_DO_RETRANSMISSION;
610 priv->rtx_delay = DEFAULT_RTX_DELAY;
611 priv->rtx_delay_reorder = DEFAULT_RTX_DELAY_REORDER;
612 priv->rtx_retry_timeout = DEFAULT_RTX_RETRY_TIMEOUT;
613 priv->rtx_retry_period = DEFAULT_RTX_RETRY_PERIOD;
615 priv->timers = g_array_new (FALSE, TRUE, sizeof (TimerData));
616 priv->jbuf = rtp_jitter_buffer_new ();
617 g_mutex_init (&priv->jbuf_lock);
618 g_cond_init (&priv->jbuf_timer);
619 g_cond_init (&priv->jbuf_event);
621 /* reset skew detection initialy */
622 rtp_jitter_buffer_reset_skew (priv->jbuf);
623 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
624 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
628 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_src_template,
631 gst_pad_set_activatemode_function (priv->srcpad,
632 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_activate_mode));
633 gst_pad_set_query_function (priv->srcpad,
634 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_query));
635 gst_pad_set_event_function (priv->srcpad,
636 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_event));
639 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_sink_template,
642 gst_pad_set_chain_function (priv->sinkpad,
643 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_chain));
644 gst_pad_set_event_function (priv->sinkpad,
645 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_event));
646 gst_pad_set_query_function (priv->sinkpad,
647 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_query));
649 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->srcpad);
650 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->sinkpad);
652 GST_OBJECT_FLAG_SET (jitterbuffer, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
656 gst_rtp_jitter_buffer_finalize (GObject * object)
658 GstRtpJitterBuffer *jitterbuffer;
660 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
662 g_array_free (jitterbuffer->priv->timers, TRUE);
663 g_mutex_clear (&jitterbuffer->priv->jbuf_lock);
664 g_cond_clear (&jitterbuffer->priv->jbuf_timer);
665 g_cond_clear (&jitterbuffer->priv->jbuf_event);
667 g_object_unref (jitterbuffer->priv->jbuf);
669 G_OBJECT_CLASS (parent_class)->finalize (object);
673 gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad, GstObject * parent)
675 GstRtpJitterBuffer *jitterbuffer;
676 GstPad *otherpad = NULL;
680 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
682 if (pad == jitterbuffer->priv->sinkpad) {
683 otherpad = jitterbuffer->priv->srcpad;
684 } else if (pad == jitterbuffer->priv->srcpad) {
685 otherpad = jitterbuffer->priv->sinkpad;
686 } else if (pad == jitterbuffer->priv->rtcpsinkpad) {
690 g_value_init (&val, GST_TYPE_PAD);
691 g_value_set_object (&val, otherpad);
692 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
693 g_value_unset (&val);
699 create_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
701 GstRtpJitterBufferPrivate *priv;
703 priv = jitterbuffer->priv;
705 GST_DEBUG_OBJECT (jitterbuffer, "creating RTCP sink pad");
708 gst_pad_new_from_static_template
709 (&gst_rtp_jitter_buffer_sink_rtcp_template, "sink_rtcp");
710 gst_pad_set_chain_function (priv->rtcpsinkpad,
711 gst_rtp_jitter_buffer_chain_rtcp);
712 gst_pad_set_event_function (priv->rtcpsinkpad,
713 (GstPadEventFunction) gst_rtp_jitter_buffer_sink_rtcp_event);
714 gst_pad_set_iterate_internal_links_function (priv->rtcpsinkpad,
715 gst_rtp_jitter_buffer_iterate_internal_links);
716 gst_pad_set_active (priv->rtcpsinkpad, TRUE);
717 gst_element_add_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
719 return priv->rtcpsinkpad;
723 remove_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
725 GstRtpJitterBufferPrivate *priv;
727 priv = jitterbuffer->priv;
729 GST_DEBUG_OBJECT (jitterbuffer, "removing RTCP sink pad");
731 gst_pad_set_active (priv->rtcpsinkpad, FALSE);
733 gst_element_remove_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
734 priv->rtcpsinkpad = NULL;
738 gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
739 GstPadTemplate * templ, const gchar * name, const GstCaps * filter)
741 GstRtpJitterBuffer *jitterbuffer;
742 GstElementClass *klass;
744 GstRtpJitterBufferPrivate *priv;
746 g_return_val_if_fail (templ != NULL, NULL);
747 g_return_val_if_fail (GST_IS_RTP_JITTER_BUFFER (element), NULL);
749 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
750 priv = jitterbuffer->priv;
751 klass = GST_ELEMENT_GET_CLASS (element);
753 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
755 /* figure out the template */
756 if (templ == gst_element_class_get_pad_template (klass, "sink_rtcp")) {
757 if (priv->rtcpsinkpad != NULL)
760 result = create_rtcp_sink (jitterbuffer);
769 g_warning ("gstrtpjitterbuffer: this is not our template");
774 g_warning ("gstrtpjitterbuffer: pad already requested");
780 gst_rtp_jitter_buffer_release_pad (GstElement * element, GstPad * pad)
782 GstRtpJitterBuffer *jitterbuffer;
783 GstRtpJitterBufferPrivate *priv;
785 g_return_if_fail (GST_IS_RTP_JITTER_BUFFER (element));
786 g_return_if_fail (GST_IS_PAD (pad));
788 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
789 priv = jitterbuffer->priv;
791 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
793 if (priv->rtcpsinkpad == pad) {
794 remove_rtcp_sink (jitterbuffer);
803 g_warning ("gstjitterbuffer: asked to release an unknown pad");
809 gst_rtp_jitter_buffer_provide_clock (GstElement * element)
811 return gst_system_clock_obtain ();
815 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer)
817 GstRtpJitterBufferPrivate *priv;
819 priv = jitterbuffer->priv;
821 /* this will trigger a new pt-map request signal, FIXME, do something better. */
824 priv->clock_rate = -1;
825 /* do not clear current content, but refresh state for new arrival */
826 GST_DEBUG_OBJECT (jitterbuffer, "reset jitterbuffer");
827 rtp_jitter_buffer_reset_skew (priv->jbuf);
828 priv->last_popped_seqnum = -1;
829 priv->next_seqnum = -1;
834 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jbuf, gboolean active,
837 GstRtpJitterBufferPrivate *priv;
838 GstClockTime last_out;
844 GST_DEBUG_OBJECT (jbuf, "setting active %d with offset %" GST_TIME_FORMAT,
845 active, GST_TIME_ARGS (offset));
847 if (active != priv->active) {
848 /* add the amount of time spent in paused to the output offset. All
849 * outgoing buffers will have this offset applied to their timestamps in
850 * order to make them arrive in time in the sink. */
851 priv->out_offset = offset;
852 GST_DEBUG_OBJECT (jbuf, "out offset %" GST_TIME_FORMAT,
853 GST_TIME_ARGS (priv->out_offset));
854 priv->active = active;
855 JBUF_SIGNAL_EVENT (priv);
858 rtp_jitter_buffer_set_buffering (priv->jbuf, TRUE);
860 if ((head = rtp_jitter_buffer_peek (priv->jbuf))) {
861 /* head buffer timestamp and offset gives our output time */
862 last_out = GST_BUFFER_DTS (head) + priv->ts_offset;
864 /* use last known time when the buffer is empty */
865 last_out = priv->last_out_time;
873 gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter)
875 GstRtpJitterBuffer *jitterbuffer;
876 GstRtpJitterBufferPrivate *priv;
881 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
882 priv = jitterbuffer->priv;
884 other = (pad == priv->srcpad ? priv->sinkpad : priv->srcpad);
886 caps = gst_pad_peer_query_caps (other, filter);
888 templ = gst_pad_get_pad_template_caps (pad);
890 GST_DEBUG_OBJECT (jitterbuffer, "use template");
895 GST_DEBUG_OBJECT (jitterbuffer, "intersect with template");
897 intersect = gst_caps_intersect (caps, templ);
898 gst_caps_unref (caps);
899 gst_caps_unref (templ);
903 gst_object_unref (jitterbuffer);
909 * Must be called with JBUF_LOCK held
913 gst_jitter_buffer_sink_parse_caps (GstRtpJitterBuffer * jitterbuffer,
916 GstRtpJitterBufferPrivate *priv;
917 GstStructure *caps_struct;
921 priv = jitterbuffer->priv;
923 /* first parse the caps */
924 caps_struct = gst_caps_get_structure (caps, 0);
926 GST_DEBUG_OBJECT (jitterbuffer, "got caps");
928 /* we need a clock-rate to convert the rtp timestamps to GStreamer time and to
929 * measure the amount of data in the buffer */
930 if (!gst_structure_get_int (caps_struct, "clock-rate", &priv->clock_rate))
933 if (priv->clock_rate <= 0)
936 GST_DEBUG_OBJECT (jitterbuffer, "got clock-rate %d", priv->clock_rate);
938 /* The clock base is the RTP timestamp corrsponding to the npt-start value. We
939 * can use this to track the amount of time elapsed on the sender. */
940 if (gst_structure_get_uint (caps_struct, "clock-base", &val))
941 priv->clock_base = val;
943 priv->clock_base = -1;
945 priv->ext_timestamp = priv->clock_base;
947 GST_DEBUG_OBJECT (jitterbuffer, "got clock-base %" G_GINT64_FORMAT,
950 if (gst_structure_get_uint (caps_struct, "seqnum-base", &val)) {
951 /* first expected seqnum, only update when we didn't have a previous base. */
952 if (priv->next_in_seqnum == -1)
953 priv->next_in_seqnum = val;
954 if (priv->next_seqnum == -1)
955 priv->next_seqnum = val;
958 GST_DEBUG_OBJECT (jitterbuffer, "got seqnum-base %d", priv->next_in_seqnum);
960 /* the start and stop times. The seqnum-base corresponds to the start time. We
961 * will keep track of the seqnums on the output and when we reach the one
962 * corresponding to npt-stop, we emit the npt-stop-reached signal */
963 if (gst_structure_get_clock_time (caps_struct, "npt-start", &tval))
964 priv->npt_start = tval;
968 if (gst_structure_get_clock_time (caps_struct, "npt-stop", &tval))
969 priv->npt_stop = tval;
973 GST_DEBUG_OBJECT (jitterbuffer,
974 "npt start/stop: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
975 GST_TIME_ARGS (priv->npt_start), GST_TIME_ARGS (priv->npt_stop));
982 GST_DEBUG_OBJECT (jitterbuffer, "No clock-rate in caps!");
987 GST_DEBUG_OBJECT (jitterbuffer, "Invalid clock-rate %d", priv->clock_rate);
993 gst_rtp_jitter_buffer_flush_start (GstRtpJitterBuffer * jitterbuffer)
995 GstRtpJitterBufferPrivate *priv;
997 priv = jitterbuffer->priv;
1000 /* mark ourselves as flushing */
1001 priv->srcresult = GST_FLOW_FLUSHING;
1002 GST_DEBUG_OBJECT (jitterbuffer, "Disabling pop on queue");
1003 /* this unblocks any waiting pops on the src pad task */
1004 JBUF_SIGNAL_EVENT (priv);
1005 /* unlock clock, we just unschedule, the entry will be released by the
1006 * locking streaming thread. */
1007 unschedule_current_timer (jitterbuffer);
1012 gst_rtp_jitter_buffer_flush_stop (GstRtpJitterBuffer * jitterbuffer)
1014 GstRtpJitterBufferPrivate *priv;
1016 priv = jitterbuffer->priv;
1019 GST_DEBUG_OBJECT (jitterbuffer, "Enabling pop on queue");
1020 /* Mark as non flushing */
1021 priv->srcresult = GST_FLOW_OK;
1022 gst_segment_init (&priv->segment, GST_FORMAT_TIME);
1023 priv->last_popped_seqnum = -1;
1024 priv->last_out_time = -1;
1025 priv->next_seqnum = -1;
1026 priv->ips_rtptime = -1;
1027 priv->ips_dts = GST_CLOCK_TIME_NONE;
1028 priv->packet_spacing = 0;
1029 priv->next_in_seqnum = -1;
1030 priv->clock_rate = -1;
1032 priv->estimated_eos = -1;
1033 priv->last_elapsed = 0;
1034 priv->ext_timestamp = -1;
1035 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
1036 rtp_jitter_buffer_flush (priv->jbuf);
1037 rtp_jitter_buffer_reset_skew (priv->jbuf);
1038 remove_all_timers (jitterbuffer);
1043 gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad, GstObject * parent,
1044 GstPadMode mode, gboolean active)
1047 GstRtpJitterBuffer *jitterbuffer = NULL;
1049 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1052 case GST_PAD_MODE_PUSH:
1054 /* allow data processing */
1055 gst_rtp_jitter_buffer_flush_stop (jitterbuffer);
1057 /* start pushing out buffers */
1058 GST_DEBUG_OBJECT (jitterbuffer, "Starting task on srcpad");
1059 result = gst_pad_start_task (jitterbuffer->priv->srcpad,
1060 (GstTaskFunction) gst_rtp_jitter_buffer_loop, jitterbuffer, NULL);
1062 /* make sure all data processing stops ASAP */
1063 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
1065 /* NOTE this will hardlock if the state change is called from the src pad
1066 * task thread because we will _join() the thread. */
1067 GST_DEBUG_OBJECT (jitterbuffer, "Stopping task on srcpad");
1068 result = gst_pad_stop_task (pad);
1078 static GstStateChangeReturn
1079 gst_rtp_jitter_buffer_change_state (GstElement * element,
1080 GstStateChange transition)
1082 GstRtpJitterBuffer *jitterbuffer;
1083 GstRtpJitterBufferPrivate *priv;
1084 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1086 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
1087 priv = jitterbuffer->priv;
1089 switch (transition) {
1090 case GST_STATE_CHANGE_NULL_TO_READY:
1092 case GST_STATE_CHANGE_READY_TO_PAUSED:
1094 /* reset negotiated values */
1095 priv->clock_rate = -1;
1096 priv->clock_base = -1;
1097 priv->peer_latency = 0;
1099 /* block until we go to PLAYING */
1100 priv->blocked = TRUE;
1101 priv->timer_running = TRUE;
1102 priv->timer_thread =
1103 g_thread_new ("timer", (GThreadFunc) wait_next_timeout, jitterbuffer);
1106 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1108 /* unblock to allow streaming in PLAYING */
1109 priv->blocked = FALSE;
1110 JBUF_SIGNAL_EVENT (priv);
1117 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1119 switch (transition) {
1120 case GST_STATE_CHANGE_READY_TO_PAUSED:
1121 /* we are a live element because we sync to the clock, which we can only
1122 * do in the PLAYING state */
1123 if (ret != GST_STATE_CHANGE_FAILURE)
1124 ret = GST_STATE_CHANGE_NO_PREROLL;
1126 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1128 /* block to stop streaming when PAUSED */
1129 priv->blocked = TRUE;
1131 if (ret != GST_STATE_CHANGE_FAILURE)
1132 ret = GST_STATE_CHANGE_NO_PREROLL;
1134 case GST_STATE_CHANGE_PAUSED_TO_READY:
1136 gst_buffer_replace (&priv->last_sr, NULL);
1137 priv->timer_running = FALSE;
1138 JBUF_SIGNAL_TIMER (priv);
1140 g_thread_join (priv->timer_thread);
1141 priv->timer_thread = NULL;
1143 case GST_STATE_CHANGE_READY_TO_NULL:
1153 gst_rtp_jitter_buffer_src_event (GstPad * pad, GstObject * parent,
1156 gboolean ret = TRUE;
1157 GstRtpJitterBuffer *jitterbuffer;
1158 GstRtpJitterBufferPrivate *priv;
1160 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1161 priv = jitterbuffer->priv;
1163 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1165 switch (GST_EVENT_TYPE (event)) {
1166 case GST_EVENT_LATENCY:
1168 GstClockTime latency;
1170 gst_event_parse_latency (event, &latency);
1172 GST_DEBUG_OBJECT (jitterbuffer,
1173 "configuring latency of %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1176 /* adjust the overall buffer delay to the total pipeline latency in
1177 * buffering mode because if downstream consumes too fast (because of
1178 * large latency or queues, we would start rebuffering again. */
1179 if (rtp_jitter_buffer_get_mode (priv->jbuf) ==
1180 RTP_JITTER_BUFFER_MODE_BUFFER) {
1181 rtp_jitter_buffer_set_delay (priv->jbuf, latency);
1185 ret = gst_pad_push_event (priv->sinkpad, event);
1189 ret = gst_pad_push_event (priv->sinkpad, event);
1197 gst_rtp_jitter_buffer_sink_event (GstPad * pad, GstObject * parent,
1200 gboolean ret = TRUE;
1201 GstRtpJitterBuffer *jitterbuffer;
1202 GstRtpJitterBufferPrivate *priv;
1204 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1205 priv = jitterbuffer->priv;
1207 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1209 switch (GST_EVENT_TYPE (event)) {
1210 case GST_EVENT_CAPS:
1214 gst_event_parse_caps (event, &caps);
1217 ret = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1220 /* set same caps on srcpad on success */
1222 ret = gst_pad_push_event (priv->srcpad, event);
1224 gst_event_unref (event);
1227 case GST_EVENT_SEGMENT:
1229 gst_event_copy_segment (event, &priv->segment);
1231 /* we need time for now */
1232 if (priv->segment.format != GST_FORMAT_TIME)
1233 goto newseg_wrong_format;
1235 GST_DEBUG_OBJECT (jitterbuffer,
1236 "newsegment: %" GST_SEGMENT_FORMAT, &priv->segment);
1238 /* FIXME, push SEGMENT in the queue. Sorting order might be difficult. */
1239 ret = gst_pad_push_event (priv->srcpad, event);
1242 case GST_EVENT_FLUSH_START:
1243 ret = gst_pad_push_event (priv->srcpad, event);
1244 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
1246 case GST_EVENT_FLUSH_STOP:
1247 ret = gst_pad_push_event (priv->srcpad, event);
1249 gst_rtp_jitter_buffer_src_activate_mode (priv->srcpad, parent,
1250 GST_PAD_MODE_PUSH, TRUE);
1254 /* push EOS in queue. We always push it at the head */
1256 /* check for flushing, we need to discard the event and return FALSE when
1257 * we are flushing */
1258 ret = priv->srcresult == GST_FLOW_OK;
1259 if (ret && !priv->eos) {
1260 GST_INFO_OBJECT (jitterbuffer, "queuing EOS");
1262 JBUF_SIGNAL_EVENT (priv);
1263 } else if (priv->eos) {
1264 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, we are already EOS");
1266 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, reason %s",
1267 gst_flow_get_name (priv->srcresult));
1270 gst_event_unref (event);
1274 ret = gst_pad_push_event (priv->srcpad, event);
1283 newseg_wrong_format:
1285 GST_DEBUG_OBJECT (jitterbuffer, "received non TIME newsegment");
1287 gst_event_unref (event);
1293 gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad, GstObject * parent,
1296 gboolean ret = TRUE;
1297 GstRtpJitterBuffer *jitterbuffer;
1299 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1301 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1303 switch (GST_EVENT_TYPE (event)) {
1304 case GST_EVENT_FLUSH_START:
1305 gst_event_unref (event);
1307 case GST_EVENT_FLUSH_STOP:
1308 gst_event_unref (event);
1311 ret = gst_pad_event_default (pad, parent, event);
1319 * Must be called with JBUF_LOCK held, will release the LOCK when emiting the
1320 * signal. The function returns GST_FLOW_ERROR when a parsing error happened and
1321 * GST_FLOW_FLUSHING when the element is shutting down. On success
1322 * GST_FLOW_OK is returned.
1324 static GstFlowReturn
1325 gst_rtp_jitter_buffer_get_clock_rate (GstRtpJitterBuffer * jitterbuffer,
1329 GValue args[2] = { {0}, {0} };
1333 g_value_init (&args[0], GST_TYPE_ELEMENT);
1334 g_value_set_object (&args[0], jitterbuffer);
1335 g_value_init (&args[1], G_TYPE_UINT);
1336 g_value_set_uint (&args[1], pt);
1338 g_value_init (&ret, GST_TYPE_CAPS);
1339 g_value_set_boxed (&ret, NULL);
1341 JBUF_UNLOCK (jitterbuffer->priv);
1342 g_signal_emitv (args, gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP], 0,
1344 JBUF_LOCK_CHECK (jitterbuffer->priv, out_flushing);
1346 g_value_unset (&args[0]);
1347 g_value_unset (&args[1]);
1348 caps = (GstCaps *) g_value_dup_boxed (&ret);
1349 g_value_unset (&ret);
1353 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1354 gst_caps_unref (caps);
1356 if (G_UNLIKELY (!res))
1364 GST_DEBUG_OBJECT (jitterbuffer, "could not get caps");
1365 return GST_FLOW_ERROR;
1369 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1370 return GST_FLOW_FLUSHING;
1374 GST_DEBUG_OBJECT (jitterbuffer, "parse failed");
1375 return GST_FLOW_ERROR;
1379 /* call with jbuf lock held */
1381 check_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint * percent)
1383 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1385 /* too short a stream, or too close to EOS will never really fill buffer */
1386 if (*percent != -1 && priv->npt_stop != -1 &&
1387 priv->npt_stop - priv->npt_start <=
1388 rtp_jitter_buffer_get_delay (priv->jbuf)) {
1389 GST_DEBUG_OBJECT (jitterbuffer, "short stream; faking full buffer");
1390 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
1396 post_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint percent)
1398 GstMessage *message;
1400 /* Post a buffering message */
1401 message = gst_message_new_buffering (GST_OBJECT_CAST (jitterbuffer), percent);
1402 gst_message_set_buffering_stats (message, GST_BUFFERING_LIVE, -1, -1, -1);
1404 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer), message);
1408 apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
1410 GstRtpJitterBufferPrivate *priv;
1412 priv = jitterbuffer->priv;
1414 if (timestamp == -1)
1417 /* apply the timestamp offset, this is used for inter stream sync */
1418 timestamp += priv->ts_offset;
1419 /* add the offset, this is used when buffering */
1420 timestamp += priv->out_offset;
1426 find_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type, guint16 seqnum)
1428 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1429 TimerData *timer = NULL;
1432 len = priv->timers->len;
1433 for (i = 0; i < len; i++) {
1434 TimerData *test = &g_array_index (priv->timers, TimerData, i);
1435 if (test->seqnum == seqnum && test->type == type) {
1444 unschedule_current_timer (GstRtpJitterBuffer * jitterbuffer)
1446 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1448 if (priv->clock_id) {
1449 GST_DEBUG_OBJECT (jitterbuffer, "unschedule current timer");
1450 gst_clock_id_unschedule (priv->clock_id);
1451 priv->unscheduled = TRUE;
1456 get_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1458 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1459 GstClockTime test_timeout;
1461 if ((test_timeout = timer->timeout) == -1)
1464 if (timer->type != TIMER_TYPE_EXPECTED) {
1465 /* add our latency and offset to get output times. */
1466 test_timeout = apply_offset (jitterbuffer, test_timeout);
1467 test_timeout += priv->latency_ns;
1469 return test_timeout;
1473 recalculate_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1475 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1477 if (priv->clock_id) {
1478 GstClockTime timeout = get_timeout (jitterbuffer, timer);
1480 GST_DEBUG ("%" GST_TIME_FORMAT " <> %" GST_TIME_FORMAT,
1481 GST_TIME_ARGS (timeout), GST_TIME_ARGS (priv->timer_timeout));
1483 if (timeout == -1 || timeout < priv->timer_timeout)
1484 unschedule_current_timer (jitterbuffer);
1489 add_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
1490 guint16 seqnum, GstClockTime timeout)
1492 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1496 GST_DEBUG_OBJECT (jitterbuffer,
1497 "add timer for seqnum %d to %" GST_TIME_FORMAT,
1498 seqnum, GST_TIME_ARGS (timeout));
1500 len = priv->timers->len;
1501 g_array_set_size (priv->timers, len + 1);
1502 timer = &g_array_index (priv->timers, TimerData, len);
1505 timer->seqnum = seqnum;
1506 timer->timeout = timeout;
1507 if (type == TIMER_TYPE_EXPECTED) {
1508 timer->rtx_base = timeout;
1509 timer->rtx_retry = 0;
1511 recalculate_timer (jitterbuffer, timer);
1512 JBUF_SIGNAL_TIMER (priv);
1518 reschedule_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
1519 guint16 seqnum, GstClockTime timeout)
1521 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1522 gboolean seqchange, timechange;
1525 seqchange = timer->seqnum != seqnum;
1526 timechange = timer->timeout != timeout;
1528 if (!seqchange && !timechange)
1531 oldseq = timer->seqnum;
1533 GST_DEBUG_OBJECT (jitterbuffer,
1534 "replace timer for seqnum %d->%d to %" GST_TIME_FORMAT,
1535 oldseq, seqnum, GST_TIME_ARGS (timeout));
1537 timer->timeout = timeout;
1538 timer->seqnum = seqnum;
1539 if (seqchange && timer->type == TIMER_TYPE_EXPECTED) {
1540 timer->rtx_base = timeout;
1541 timer->rtx_retry = 0;
1544 if (priv->clock_id) {
1545 /* we changed the seqnum and there is a timer currently waiting with this
1546 * seqnum, unschedule it */
1547 if (seqchange && priv->timer_seqnum == oldseq)
1548 unschedule_current_timer (jitterbuffer);
1549 /* we changed the time, check if it is earlier than what we are waiting
1550 * for and unschedule if so */
1551 else if (timechange)
1552 recalculate_timer (jitterbuffer, timer);
1557 set_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
1558 guint16 seqnum, GstClockTime timeout)
1562 /* find the seqnum timer */
1563 timer = find_timer (jitterbuffer, type, seqnum);
1564 if (timer == NULL) {
1565 timer = add_timer (jitterbuffer, type, seqnum, timeout);
1567 reschedule_timer (jitterbuffer, timer, seqnum, timeout);
1573 remove_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1575 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1578 if (priv->clock_id && priv->timer_seqnum == timer->seqnum)
1579 unschedule_current_timer (jitterbuffer);
1582 GST_DEBUG_OBJECT (jitterbuffer, "removed index %d", idx);
1583 g_array_remove_index_fast (priv->timers, idx);
1588 remove_all_timers (GstRtpJitterBuffer * jitterbuffer)
1590 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1591 GST_DEBUG_OBJECT (jitterbuffer, "removed all timers");
1592 g_array_set_size (priv->timers, 0);
1593 unschedule_current_timer (jitterbuffer);
1596 /* we just received a packet with seqnum and dts.
1598 * First check for old seqnum that we are still expecting. If the gap with the
1599 * current timestamp is too big, unschedule the timeouts.
1601 * If we have a valid packet spacing estimate we can set a timer for when we
1602 * should receive the next packet.
1603 * If we don't have a valid estimate, we remove any timer we might have
1604 * had for this packet.
1607 update_timers (GstRtpJitterBuffer * jitterbuffer, guint16 seqnum,
1608 GstClockTime dts, gboolean do_next_seqnum)
1610 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1611 TimerData *timer = NULL;
1614 /* go through all timers and unschedule the ones with a large gap, also find
1615 * the timer for the seqnum */
1616 len = priv->timers->len;
1617 for (i = 0; i < len; i++) {
1618 TimerData *test = &g_array_index (priv->timers, TimerData, i);
1621 gap = gst_rtp_buffer_compare_seqnum (test->seqnum, seqnum);
1623 GST_DEBUG_OBJECT (jitterbuffer, "%d, #%d<->#%d gap %d", i,
1624 test->seqnum, seqnum, gap);
1627 GST_DEBUG ("found timer for current seqnum");
1628 /* the timer for the current seqnum */
1630 } else if (gap > priv->rtx_delay_reorder) {
1631 /* max gap, we exceeded the max reorder distance and we don't expect the
1632 * missing packet to be this reordered */
1633 if (test->rtx_retry == 0 && test->type == TIMER_TYPE_EXPECTED)
1634 reschedule_timer (jitterbuffer, test, test->seqnum, -1);
1638 if (priv->packet_spacing > 0 && do_next_seqnum && priv->do_retransmission) {
1639 GstClockTime expected;
1641 /* calculate expected arrival time of the next seqnum */
1642 expected = dts + priv->packet_spacing + (priv->rtx_delay * GST_MSECOND);
1643 /* and update/install timer for next seqnum */
1645 reschedule_timer (jitterbuffer, timer, priv->next_in_seqnum, expected);
1647 add_timer (jitterbuffer, TIMER_TYPE_EXPECTED, priv->next_in_seqnum,
1649 } else if (timer && timer->type != TIMER_TYPE_DEADLINE) {
1650 /* if we had a timer, remove it, we don't know when to expect the next
1652 remove_timer (jitterbuffer, timer);
1653 JBUF_SIGNAL_EVENT (priv);
1658 calculate_packet_spacing (GstRtpJitterBuffer * jitterbuffer, guint32 rtptime,
1661 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1663 /* we need consecutive seqnums with a different
1664 * rtptime to estimate the packet spacing. */
1665 if (priv->ips_rtptime != rtptime) {
1666 /* rtptime changed, check dts diff */
1667 if (priv->ips_dts != -1 && dts != -1 && dts > priv->ips_dts) {
1668 priv->packet_spacing = dts - priv->ips_dts;
1669 GST_DEBUG_OBJECT (jitterbuffer,
1670 "new packet spacing %" GST_TIME_FORMAT,
1671 GST_TIME_ARGS (priv->packet_spacing));
1673 priv->ips_rtptime = rtptime;
1674 priv->ips_dts = dts;
1679 calculate_expected (GstRtpJitterBuffer * jitterbuffer, guint32 expected,
1680 guint16 seqnum, GstClockTime dts, gint gap)
1682 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1683 GstClockTime duration, expected_dts;
1686 GST_DEBUG_OBJECT (jitterbuffer,
1687 "dts %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT,
1688 GST_TIME_ARGS (dts), GST_TIME_ARGS (priv->last_in_dts));
1690 /* interpolate between the current time and the last time based on
1691 * number of packets we are missing, this is the estimated duration
1692 * for the missing packet based on equidistant packet spacing. Also make
1693 * sure we never go negative. */
1694 if (dts >= priv->last_in_dts)
1695 duration = (dts - priv->last_in_dts) / (gap + 1);
1697 /* packet already lost, timer will timeout quickly */
1700 GST_DEBUG_OBJECT (jitterbuffer, "duration %" GST_TIME_FORMAT,
1701 GST_TIME_ARGS (duration));
1703 expected_dts = priv->last_in_dts + duration;
1705 if (priv->do_retransmission) {
1706 type = TIMER_TYPE_EXPECTED;
1707 if (find_timer (jitterbuffer, type, expected))
1710 type = TIMER_TYPE_LOST;
1713 while (expected < seqnum) {
1714 add_timer (jitterbuffer, type, expected, expected_dts);
1715 expected_dts += duration;
1720 static GstFlowReturn
1721 gst_rtp_jitter_buffer_chain (GstPad * pad, GstObject * parent,
1724 GstRtpJitterBuffer *jitterbuffer;
1725 GstRtpJitterBufferPrivate *priv;
1727 guint32 expected, rtptime;
1728 GstFlowReturn ret = GST_FLOW_OK;
1729 GstClockTime dts, pts;
1734 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
1735 gboolean do_next_seqnum = FALSE;
1737 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1739 priv = jitterbuffer->priv;
1741 if (G_UNLIKELY (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)))
1742 goto invalid_buffer;
1744 pt = gst_rtp_buffer_get_payload_type (&rtp);
1745 seqnum = gst_rtp_buffer_get_seq (&rtp);
1746 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
1747 gst_rtp_buffer_unmap (&rtp);
1749 /* make sure we have PTS and DTS set */
1750 pts = GST_BUFFER_PTS (buffer);
1751 dts = GST_BUFFER_DTS (buffer);
1757 /* take the DTS of the buffer. This is the time when the packet was
1758 * received and is used to calculate jitter and clock skew. We will adjust
1759 * this DTS with the smoothed value after processing it in the
1760 * jitterbuffer and assign it as the PTS. */
1761 /* bring to running time */
1762 dts = gst_segment_to_running_time (&priv->segment, GST_FORMAT_TIME, dts);
1764 GST_DEBUG_OBJECT (jitterbuffer,
1765 "Received packet #%d at time %" GST_TIME_FORMAT ", discont %d", seqnum,
1766 GST_TIME_ARGS (dts), GST_BUFFER_IS_DISCONT (buffer));
1768 JBUF_LOCK_CHECK (priv, out_flushing);
1770 if (G_UNLIKELY (priv->last_pt != pt)) {
1773 GST_DEBUG_OBJECT (jitterbuffer, "pt changed from %u to %u", priv->last_pt,
1777 /* reset clock-rate so that we get a new one */
1778 priv->clock_rate = -1;
1780 /* Try to get the clock-rate from the caps first if we can. If there are no
1781 * caps we must fire the signal to get the clock-rate. */
1782 if ((caps = gst_pad_get_current_caps (pad))) {
1783 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1784 gst_caps_unref (caps);
1788 if (G_UNLIKELY (priv->clock_rate == -1)) {
1789 /* no clock rate given on the caps, try to get one with the signal */
1790 if (gst_rtp_jitter_buffer_get_clock_rate (jitterbuffer,
1791 pt) == GST_FLOW_FLUSHING)
1794 if (G_UNLIKELY (priv->clock_rate == -1))
1798 /* don't accept more data on EOS */
1799 if (G_UNLIKELY (priv->eos))
1802 expected = priv->next_in_seqnum;
1804 /* now check against our expected seqnum */
1805 if (G_LIKELY (expected != -1)) {
1808 /* now calculate gap */
1809 gap = gst_rtp_buffer_compare_seqnum (expected, seqnum);
1811 GST_DEBUG_OBJECT (jitterbuffer, "expected #%d, got #%d, gap of %d",
1812 expected, seqnum, gap);
1814 if (G_LIKELY (gap == 0)) {
1815 /* packet is expected */
1816 calculate_packet_spacing (jitterbuffer, rtptime, dts);
1817 do_next_seqnum = TRUE;
1819 gboolean reset = FALSE;
1822 /* we received an old packet */
1823 if (G_UNLIKELY (gap < -RTP_MAX_MISORDER)) {
1824 /* too old packet, reset */
1825 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too old %d < %d", gap,
1829 GST_DEBUG_OBJECT (jitterbuffer, "old packet received");
1832 /* new packet, we are missing some packets */
1833 if (G_UNLIKELY (gap > RTP_MAX_DROPOUT)) {
1834 /* packet too far in future, reset */
1835 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too new %d > %d", gap,
1839 GST_DEBUG_OBJECT (jitterbuffer, "%d missing packets", gap);
1840 /* fill in the gap with EXPECTED timers */
1841 calculate_expected (jitterbuffer, expected, seqnum, dts, gap);
1842 do_next_seqnum = TRUE;
1845 if (G_UNLIKELY (reset)) {
1846 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
1847 rtp_jitter_buffer_flush (priv->jbuf);
1848 rtp_jitter_buffer_reset_skew (priv->jbuf);
1849 remove_all_timers (jitterbuffer);
1850 priv->last_popped_seqnum = -1;
1851 priv->next_seqnum = seqnum;
1852 do_next_seqnum = TRUE;
1854 /* reset spacing estimation when gap */
1855 priv->ips_rtptime = -1;
1856 priv->ips_dts = GST_CLOCK_TIME_NONE;
1859 GST_DEBUG_OBJECT (jitterbuffer, "First buffer #%d", seqnum);
1860 /* we don't know what the next_in_seqnum should be, wait for the last
1861 * possible moment to push this buffer, maybe we get an earlier seqnum
1863 set_timer (jitterbuffer, TIMER_TYPE_DEADLINE, seqnum, dts);
1864 do_next_seqnum = TRUE;
1866 if (do_next_seqnum) {
1867 priv->last_in_seqnum = seqnum;
1868 priv->last_in_dts = dts;
1869 priv->next_in_seqnum = (seqnum + 1) & 0xffff;
1872 /* let's check if this buffer is too late, we can only accept packets with
1873 * bigger seqnum than the one we last pushed. */
1874 if (G_LIKELY (priv->last_popped_seqnum != -1)) {
1877 gap = gst_rtp_buffer_compare_seqnum (priv->last_popped_seqnum, seqnum);
1879 /* priv->last_popped_seqnum >= seqnum, we're too late. */
1880 if (G_UNLIKELY (gap <= 0))
1884 /* let's drop oldest packet if the queue is already full and drop-on-latency
1885 * is set. We can only do this when there actually is a latency. When no
1886 * latency is set, we just pump it in the queue and let the other end push it
1887 * out as fast as possible. */
1888 if (priv->latency_ms && priv->drop_on_latency) {
1890 gst_util_uint64_scale_int (priv->latency_ms, priv->clock_rate, 1000);
1892 if (G_UNLIKELY (rtp_jitter_buffer_get_ts_diff (priv->jbuf) >= latency_ts)) {
1895 old_buf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
1897 GST_DEBUG_OBJECT (jitterbuffer, "Queue full, dropping old packet %p",
1900 gst_buffer_unref (old_buf);
1904 /* we need to make the metadata writable before pushing it in the jitterbuffer
1905 * because the jitterbuffer will update the PTS */
1906 buffer = gst_buffer_make_writable (buffer);
1907 GST_BUFFER_DTS (buffer) = dts;
1908 GST_BUFFER_PTS (buffer) = pts;
1910 /* now insert the packet into the queue in sorted order. This function returns
1911 * FALSE if a packet with the same seqnum was already in the queue, meaning we
1912 * have a duplicate. */
1913 if (G_UNLIKELY (!rtp_jitter_buffer_insert (priv->jbuf, buffer, dts,
1914 priv->clock_rate, &tail, &percent)))
1918 update_timers (jitterbuffer, seqnum, dts, do_next_seqnum);
1920 /* we had an unhandled SR, handle it now */
1922 do_handle_sync (jitterbuffer);
1924 /* signal addition of new buffer when the _loop is waiting. */
1925 if (priv->active && priv->waiting_timer)
1926 JBUF_SIGNAL_EVENT (priv);
1928 /* let's unschedule and unblock any waiting buffers. We only want to do this
1929 * when the tail buffer changed */
1930 if (G_UNLIKELY (priv->clock_id && tail)) {
1931 GST_DEBUG_OBJECT (jitterbuffer, "Unscheduling waiting new buffer");
1932 unschedule_current_timer (jitterbuffer);
1935 GST_DEBUG_OBJECT (jitterbuffer, "Pushed packet #%d, now %d packets, tail: %d",
1936 seqnum, rtp_jitter_buffer_num_packets (priv->jbuf), tail);
1938 check_buffering_percent (jitterbuffer, &percent);
1944 post_buffering_percent (jitterbuffer, percent);
1951 /* this is not fatal but should be filtered earlier */
1952 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
1953 ("Received invalid RTP payload, dropping"));
1954 gst_buffer_unref (buffer);
1959 GST_WARNING_OBJECT (jitterbuffer,
1960 "No clock-rate in caps!, dropping buffer");
1961 gst_buffer_unref (buffer);
1966 ret = priv->srcresult;
1967 GST_DEBUG_OBJECT (jitterbuffer, "flushing %s", gst_flow_get_name (ret));
1968 gst_buffer_unref (buffer);
1974 GST_WARNING_OBJECT (jitterbuffer, "we are EOS, refusing buffer");
1975 gst_buffer_unref (buffer);
1980 GST_WARNING_OBJECT (jitterbuffer, "Packet #%d too late as #%d was already"
1981 " popped, dropping", seqnum, priv->last_popped_seqnum);
1983 gst_buffer_unref (buffer);
1988 GST_WARNING_OBJECT (jitterbuffer, "Duplicate packet #%d detected, dropping",
1990 priv->num_duplicates++;
1991 gst_buffer_unref (buffer);
1997 compute_elapsed (GstRtpJitterBuffer * jitterbuffer, GstBuffer * outbuf)
1999 guint64 ext_time, elapsed;
2001 GstRtpJitterBufferPrivate *priv;
2002 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
2004 priv = jitterbuffer->priv;
2005 gst_rtp_buffer_map (outbuf, GST_MAP_READ, &rtp);
2006 rtp_time = gst_rtp_buffer_get_timestamp (&rtp);
2007 gst_rtp_buffer_unmap (&rtp);
2009 GST_LOG_OBJECT (jitterbuffer, "rtp %" G_GUINT32_FORMAT ", ext %"
2010 G_GUINT64_FORMAT, rtp_time, priv->ext_timestamp);
2012 if (rtp_time < priv->ext_timestamp) {
2013 ext_time = priv->ext_timestamp;
2015 ext_time = gst_rtp_buffer_ext_timestamp (&priv->ext_timestamp, rtp_time);
2018 if (ext_time > priv->clock_base)
2019 elapsed = ext_time - priv->clock_base;
2023 elapsed = gst_util_uint64_scale_int (elapsed, GST_SECOND, priv->clock_rate);
2028 update_estimated_eos (GstRtpJitterBuffer * jitterbuffer, GstBuffer * outbuf)
2030 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2032 if (priv->npt_stop != -1 && priv->ext_timestamp != -1
2033 && priv->clock_base != -1 && priv->clock_rate > 0) {
2034 guint64 elapsed, estimated;
2036 elapsed = compute_elapsed (jitterbuffer, outbuf);
2038 if (elapsed > priv->last_elapsed || !priv->last_elapsed) {
2040 GstClockTime out_time;
2042 priv->last_elapsed = elapsed;
2044 left = priv->npt_stop - priv->npt_start;
2045 GST_LOG_OBJECT (jitterbuffer, "left %" GST_TIME_FORMAT,
2046 GST_TIME_ARGS (left));
2048 out_time = GST_BUFFER_DTS (outbuf);
2051 estimated = gst_util_uint64_scale (out_time, left, elapsed);
2053 /* if there is almost nothing left,
2054 * we may never advance enough to end up in the above case */
2055 if (left < GST_SECOND)
2056 estimated = GST_SECOND;
2061 GST_LOG_OBJECT (jitterbuffer, "elapsed %" GST_TIME_FORMAT ", estimated %"
2062 GST_TIME_FORMAT, GST_TIME_ARGS (elapsed), GST_TIME_ARGS (estimated));
2064 if (estimated != -1 && priv->estimated_eos != estimated) {
2065 set_timer (jitterbuffer, TIMER_TYPE_EOS, -1, estimated);
2066 priv->estimated_eos = estimated;
2072 /* take a buffer from the queue and push it */
2073 static GstFlowReturn
2074 pop_and_push_next (GstRtpJitterBuffer * jitterbuffer, guint16 seqnum)
2076 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2077 GstFlowReturn result;
2079 GstClockTime dts, pts;
2082 /* when we get here we are ready to pop and push the buffer */
2083 outbuf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
2085 check_buffering_percent (jitterbuffer, &percent);
2087 if (G_UNLIKELY (priv->discont)) {
2088 /* set DISCONT flag when we missed a packet. We pushed the buffer writable
2089 * into the jitterbuffer so we can modify now. */
2090 GST_DEBUG_OBJECT (jitterbuffer, "mark output buffer discont");
2091 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2092 priv->discont = FALSE;
2094 if (G_UNLIKELY (priv->ts_discont)) {
2095 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
2096 priv->ts_discont = FALSE;
2099 dts = GST_BUFFER_DTS (outbuf);
2100 pts = GST_BUFFER_PTS (outbuf);
2102 dts = gst_segment_to_position (&priv->segment, GST_FORMAT_TIME, dts);
2103 pts = gst_segment_to_position (&priv->segment, GST_FORMAT_TIME, pts);
2105 /* apply timestamp with offset to buffer now */
2106 GST_BUFFER_DTS (outbuf) = apply_offset (jitterbuffer, dts);
2107 GST_BUFFER_PTS (outbuf) = apply_offset (jitterbuffer, pts);
2109 /* update the elapsed time when we need to check against the npt stop time. */
2110 update_estimated_eos (jitterbuffer, outbuf);
2112 /* now we are ready to push the buffer. Save the seqnum and release the lock
2113 * so the other end can push stuff in the queue again. */
2114 priv->last_popped_seqnum = seqnum;
2115 priv->last_out_time = GST_BUFFER_PTS (outbuf);
2116 priv->next_seqnum = (seqnum + 1) & 0xffff;
2120 post_buffering_percent (jitterbuffer, percent);
2123 GST_DEBUG_OBJECT (jitterbuffer,
2124 "Pushing buffer %d, dts %" GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT,
2125 seqnum, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
2126 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
2128 result = gst_pad_push (priv->srcpad, outbuf);
2130 JBUF_LOCK_CHECK (priv, out_flushing);
2137 return priv->srcresult;
2141 #define GST_FLOW_WAIT GST_FLOW_CUSTOM_SUCCESS
2143 /* Peek a buffer and compare the seqnum to the expected seqnum.
2144 * If all is fine, the buffer is pushed.
2145 * If something is wrong, we wait for some event
2147 static GstFlowReturn
2148 handle_next_buffer (GstRtpJitterBuffer * jitterbuffer)
2150 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2151 GstFlowReturn result = GST_FLOW_OK;
2154 guint32 next_seqnum;
2156 GstRTPBuffer rtp = { NULL, };
2158 /* only push buffers when PLAYING and active and not buffering */
2159 if (priv->blocked || !priv->active ||
2160 rtp_jitter_buffer_is_buffering (priv->jbuf))
2161 return GST_FLOW_WAIT;
2164 /* peek a buffer, we're just looking at the sequence number.
2165 * If all is fine, we'll pop and push it. If the sequence number is wrong we
2166 * wait for a timeout or something to change.
2167 * The peeked buffer is valid for as long as we hold the jitterbuffer lock. */
2168 outbuf = rtp_jitter_buffer_peek (priv->jbuf);
2172 /* get the seqnum and the next expected seqnum */
2173 gst_rtp_buffer_map (outbuf, GST_MAP_READ, &rtp);
2174 seqnum = gst_rtp_buffer_get_seq (&rtp);
2175 gst_rtp_buffer_unmap (&rtp);
2177 next_seqnum = priv->next_seqnum;
2179 /* get the gap between this and the previous packet. If we don't know the
2180 * previous packet seqnum assume no gap. */
2181 if (G_UNLIKELY (next_seqnum == -1)) {
2182 GST_DEBUG_OBJECT (jitterbuffer, "First buffer #%d", seqnum);
2183 /* we don't know what the next_seqnum should be, the chain function should
2184 * have scheduled a DEADLINE timer that will increment next_seqnum when it
2185 * fires, so wait for that */
2186 result = GST_FLOW_WAIT;
2188 /* else calculate GAP */
2189 gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum);
2191 if (G_LIKELY (gap == 0)) {
2192 /* no missing packet, pop and push */
2193 result = pop_and_push_next (jitterbuffer, seqnum);
2194 } else if (G_UNLIKELY (gap < 0)) {
2195 /* if we have a packet that we already pushed or considered dropped, pop it
2196 * off and get the next packet */
2197 GST_DEBUG_OBJECT (jitterbuffer, "Old packet #%d, next #%d dropping",
2198 seqnum, next_seqnum);
2199 outbuf = rtp_jitter_buffer_pop (priv->jbuf, NULL);
2200 gst_buffer_unref (outbuf);
2203 /* the chain function has scheduled timers to request retransmission or
2204 * when to consider the packet lost, wait for that */
2205 GST_DEBUG_OBJECT (jitterbuffer,
2206 "Sequence number GAP detected: expected %d instead of %d (%d missing)",
2207 next_seqnum, seqnum, gap);
2208 result = GST_FLOW_WAIT;
2215 GST_DEBUG_OBJECT (jitterbuffer, "no buffer, going to wait");
2217 result = GST_FLOW_EOS;
2219 result = GST_FLOW_WAIT;
2224 /* the timeout for when we expected a packet expired */
2226 do_expected_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
2227 GstClockTimeDiff clock_jitter)
2229 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2232 GST_DEBUG_OBJECT (jitterbuffer, "expected %d didn't arrive", timer->seqnum);
2234 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2235 gst_structure_new ("GstRTPRetransmissionRequest",
2236 "seqnum", G_TYPE_UINT, (guint) timer->seqnum,
2237 "running-time", G_TYPE_UINT64, timer->rtx_base,
2238 "delay", G_TYPE_UINT, GST_TIME_AS_MSECONDS (timer->rtx_retry),
2239 "frequency", G_TYPE_UINT, priv->rtx_retry_timeout,
2240 "period", G_TYPE_UINT, priv->rtx_retry_period,
2241 "deadline", G_TYPE_UINT, priv->latency_ms,
2242 "packet-spacing", G_TYPE_UINT64, priv->packet_spacing, NULL));
2245 gst_pad_push_event (priv->sinkpad, event);
2248 /* calculate the timeout for the next retransmission attempt */
2249 timer->rtx_retry += (priv->rtx_retry_timeout * GST_MSECOND);
2250 if (timer->rtx_retry > (priv->rtx_retry_period * GST_MSECOND)) {
2251 GST_DEBUG_OBJECT (jitterbuffer, "reschedule as LOST timer");
2252 /* too many retransmission request, we now convert the timer
2253 * to a lost timer */
2254 timer->type = TIMER_TYPE_LOST;
2255 timer->rtx_retry = 0;
2257 reschedule_timer (jitterbuffer, timer, timer->seqnum,
2258 timer->rtx_base + timer->rtx_retry);
2261 /* a packet is lost */
2263 do_lost_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
2264 GstClockTimeDiff clock_jitter)
2266 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2267 GstClockTime duration = GST_CLOCK_TIME_NONE;
2268 guint32 lost_packets = 1;
2269 gboolean lost_packets_late = FALSE;
2272 if (clock_jitter > 0
2273 && clock_jitter > (priv->latency_ns + priv->peer_latency)) {
2274 GstClockTimeDiff total_duration;
2275 GstClockTime out_time_diff;
2278 apply_offset (jitterbuffer, timer->timeout) - timer->timeout;
2279 total_duration = MIN (out_time_diff, clock_jitter);
2282 lost_packets = total_duration / duration;
2285 total_duration = lost_packets * duration;
2287 GST_DEBUG_OBJECT (jitterbuffer,
2288 "Current sync_time has expired a long time ago (+%" GST_TIME_FORMAT
2289 ") Cover up %d lost packets with duration %" GST_TIME_FORMAT,
2290 GST_TIME_ARGS (clock_jitter),
2291 lost_packets, GST_TIME_ARGS (total_duration));
2293 duration = total_duration;
2294 lost_packets_late = TRUE;
2298 /* we had a gap and thus we lost some packets. Create an event for this. */
2299 if (lost_packets > 1)
2300 GST_DEBUG_OBJECT (jitterbuffer, "Packets #%d -> #%d lost", timer->seqnum,
2301 timer->seqnum + lost_packets - 1);
2303 GST_DEBUG_OBJECT (jitterbuffer, "Packet #%d lost", timer->seqnum);
2305 priv->num_late += lost_packets;
2306 priv->discont = TRUE;
2308 /* update our expected next packet */
2309 priv->last_popped_seqnum = timer->seqnum;
2310 priv->last_out_time = apply_offset (jitterbuffer, timer->timeout);
2311 if (timer->seqnum + lost_packets > priv->next_seqnum)
2312 priv->next_seqnum = (timer->seqnum + lost_packets) & 0xffff;
2313 /* remove timer now */
2314 remove_timer (jitterbuffer, timer);
2315 JBUF_SIGNAL_EVENT (priv);
2317 if (priv->do_lost) {
2320 /* create paket lost event */
2321 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
2322 gst_structure_new ("GstRTPPacketLost",
2323 "seqnum", G_TYPE_UINT, (guint) priv->last_popped_seqnum,
2324 "timestamp", G_TYPE_UINT64, priv->last_out_time,
2325 "duration", G_TYPE_UINT64, duration,
2326 "late", G_TYPE_BOOLEAN, lost_packets_late, NULL));
2328 gst_pad_push_event (priv->srcpad, event);
2334 do_eos_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
2336 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2338 GST_INFO_OBJECT (jitterbuffer, "got the NPT timeout");
2339 remove_timer (jitterbuffer, timer);
2340 JBUF_SIGNAL_EVENT (priv);
2344 do_deadline_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
2345 GstClockTimeDiff clock_jitter)
2347 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2349 GST_INFO_OBJECT (jitterbuffer, "got deadline timeout");
2351 priv->next_seqnum = timer->seqnum;
2352 remove_timer (jitterbuffer, timer);
2353 JBUF_SIGNAL_EVENT (priv);
2356 /* called when we need to wait for the next timeout.
2358 * We loop over the array of recorded timeouts and wait for the earliest one.
2359 * When it timed out, do the logic associated with the timer.
2361 * If there are no timers, we wait on a gcond until something new happens.
2364 wait_next_timeout (GstRtpJitterBuffer * jitterbuffer)
2366 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2369 while (priv->timer_running) {
2370 TimerData *timer = NULL;
2371 GstClockTime timer_timeout = -1;
2375 len = priv->timers->len;
2376 for (i = 0; i < len; i++) {
2377 TimerData *test = &g_array_index (priv->timers, TimerData, i);
2378 GstClockTime test_timeout = get_timeout (jitterbuffer, test);
2380 GST_DEBUG_OBJECT (jitterbuffer, "%d, %d, %d, %" GST_TIME_FORMAT,
2381 i, test->type, test->seqnum, GST_TIME_ARGS (test_timeout));
2383 /* find the smallest timeout */
2384 if (timer == NULL || test_timeout == -1 || test_timeout < timer_timeout) {
2386 timer_timeout = test_timeout;
2387 if (timer_timeout == -1)
2393 GstClockTime sync_time;
2396 GstClockTimeDiff clock_jitter;
2398 /* no timestamp, timeout immeditately */
2399 if (timer_timeout == -1)
2402 GST_OBJECT_LOCK (jitterbuffer);
2403 clock = GST_ELEMENT_CLOCK (jitterbuffer);
2405 GST_OBJECT_UNLOCK (jitterbuffer);
2406 /* let's just push if there is no clock */
2407 GST_DEBUG_OBJECT (jitterbuffer, "No clock, timeout right away");
2411 /* prepare for sync against clock */
2412 sync_time = timer_timeout + GST_ELEMENT_CAST (jitterbuffer)->base_time;
2413 /* add latency of peer to get input time */
2414 sync_time += priv->peer_latency;
2416 GST_DEBUG_OBJECT (jitterbuffer, "sync to timestamp %" GST_TIME_FORMAT
2417 " with sync time %" GST_TIME_FORMAT,
2418 GST_TIME_ARGS (timer_timeout), GST_TIME_ARGS (sync_time));
2420 /* create an entry for the clock */
2421 id = priv->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
2422 priv->unscheduled = FALSE;
2423 priv->timer_timeout = timer_timeout;
2424 priv->timer_seqnum = timer->seqnum;
2425 timer_idx = timer->idx;
2426 GST_OBJECT_UNLOCK (jitterbuffer);
2428 /* release the lock so that the other end can push stuff or unlock */
2431 ret = gst_clock_id_wait (id, &clock_jitter);
2434 if (!priv->timer_running)
2437 GST_DEBUG_OBJECT (jitterbuffer, "sync done, %d, #%d, %" G_GINT64_FORMAT,
2438 ret, priv->timer_seqnum, clock_jitter);
2439 /* and free the entry */
2440 gst_clock_id_unref (id);
2441 priv->clock_id = NULL;
2443 if (priv->timers->len <= timer_idx)
2446 /* we released the lock, the array might have changed */
2447 timer = &g_array_index (priv->timers, TimerData, timer_idx);
2448 /* if changed to timeout immediately, do so */
2449 if (timer->timeout == -1)
2452 /* if we got unscheduled and we are not flushing, it's because a new tail
2453 * element became available in the queue or we flushed the queue.
2454 * Grab it and try to push or sync. */
2455 if (ret == GST_CLOCK_UNSCHEDULED || priv->unscheduled) {
2456 GST_DEBUG_OBJECT (jitterbuffer, "Wait got unscheduled");
2461 switch (timer->type) {
2462 case TIMER_TYPE_EXPECTED:
2463 do_expected_timeout (jitterbuffer, timer, clock_jitter);
2465 case TIMER_TYPE_LOST:
2466 do_lost_timeout (jitterbuffer, timer, clock_jitter);
2468 case TIMER_TYPE_DEADLINE:
2469 do_deadline_timeout (jitterbuffer, timer, clock_jitter);
2471 case TIMER_TYPE_EOS:
2472 do_eos_timeout (jitterbuffer, timer);
2476 /* no timers, wait for activity */
2477 GST_DEBUG_OBJECT (jitterbuffer, "waiting");
2478 JBUF_WAIT_TIMER (priv);
2479 GST_DEBUG_OBJECT (jitterbuffer, "waiting done");
2484 GST_DEBUG_OBJECT (jitterbuffer, "we are stopping");
2489 * This funcion implements the main pushing loop on the source pad.
2491 * It first tries to push as many buffers as possible. If there is a seqnum
2492 * mismatch, we wait for the next timeouts.
2495 gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer)
2497 GstRtpJitterBufferPrivate *priv;
2498 GstFlowReturn result;
2500 priv = jitterbuffer->priv;
2502 JBUF_LOCK_CHECK (priv, flushing);
2504 result = handle_next_buffer (jitterbuffer);
2505 if (G_LIKELY (result == GST_FLOW_WAIT)) {
2506 GST_DEBUG_OBJECT (jitterbuffer, "waiting for event");
2507 /* now wait for the next event */
2508 JBUF_WAIT_EVENT (priv, flushing);
2509 GST_DEBUG_OBJECT (jitterbuffer, "waiting for event done");
2510 result = GST_FLOW_OK;
2513 while (result == GST_FLOW_OK);
2516 /* if we get here we need to pause */
2522 result = priv->srcresult;
2528 const gchar *reason = gst_flow_get_name (result);
2531 GST_DEBUG_OBJECT (jitterbuffer, "pausing task, reason %s", reason);
2532 gst_pad_pause_task (priv->srcpad);
2533 if (result == GST_FLOW_EOS) {
2534 event = gst_event_new_eos ();
2535 gst_pad_push_event (priv->srcpad, event);
2541 /* collect the info from the lastest RTCP packet and the jitterbuffer sync, do
2542 * some sanity checks and then emit the handle-sync signal with the parameters.
2543 * This function must be called with the LOCK */
2545 do_handle_sync (GstRtpJitterBuffer * jitterbuffer)
2547 GstRtpJitterBufferPrivate *priv;
2548 guint64 base_rtptime, base_time;
2550 guint64 last_rtptime;
2552 guint64 ext_rtptime, diff;
2553 gboolean drop = FALSE;
2555 priv = jitterbuffer->priv;
2557 /* get the last values from the jitterbuffer */
2558 rtp_jitter_buffer_get_sync (priv->jbuf, &base_rtptime, &base_time,
2559 &clock_rate, &last_rtptime);
2561 clock_base = priv->clock_base;
2562 ext_rtptime = priv->ext_rtptime;
2564 GST_DEBUG_OBJECT (jitterbuffer, "ext SR %" G_GUINT64_FORMAT ", base %"
2565 G_GUINT64_FORMAT ", clock-rate %" G_GUINT32_FORMAT
2566 ", clock-base %" G_GUINT64_FORMAT ", last-rtptime %" G_GUINT64_FORMAT,
2567 ext_rtptime, base_rtptime, clock_rate, clock_base, last_rtptime);
2569 if (base_rtptime == -1 || clock_rate == -1 || base_time == -1) {
2570 GST_DEBUG_OBJECT (jitterbuffer, "dropping, no RTP values");
2573 /* we can't accept anything that happened before we did the last resync */
2574 if (base_rtptime > ext_rtptime) {
2575 GST_DEBUG_OBJECT (jitterbuffer, "dropping, older than base time");
2578 /* the SR RTP timestamp must be something close to what we last observed
2579 * in the jitterbuffer */
2580 if (ext_rtptime > last_rtptime) {
2581 /* check how far ahead it is to our RTP timestamps */
2582 diff = ext_rtptime - last_rtptime;
2583 /* if bigger than 1 second, we drop it */
2584 if (diff > clock_rate) {
2585 GST_DEBUG_OBJECT (jitterbuffer, "too far ahead");
2586 /* should drop this, but some RTSP servers end up with bogus
2587 * way too ahead RTCP packet when repeated PAUSE/PLAY,
2588 * so still trigger rptbin sync but invalidate RTCP data
2589 * (sync might use other methods) */
2592 GST_DEBUG_OBJECT (jitterbuffer, "ext last %" G_GUINT64_FORMAT ", diff %"
2593 G_GUINT64_FORMAT, last_rtptime, diff);
2601 s = gst_structure_new ("application/x-rtp-sync",
2602 "base-rtptime", G_TYPE_UINT64, base_rtptime,
2603 "base-time", G_TYPE_UINT64, base_time,
2604 "clock-rate", G_TYPE_UINT, clock_rate,
2605 "clock-base", G_TYPE_UINT64, clock_base,
2606 "sr-ext-rtptime", G_TYPE_UINT64, ext_rtptime,
2607 "sr-buffer", GST_TYPE_BUFFER, priv->last_sr, NULL);
2609 GST_DEBUG_OBJECT (jitterbuffer, "signaling sync");
2610 gst_buffer_replace (&priv->last_sr, NULL);
2612 g_signal_emit (jitterbuffer,
2613 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC], 0, s);
2615 gst_structure_free (s);
2617 GST_DEBUG_OBJECT (jitterbuffer, "dropping RTCP packet");
2621 static GstFlowReturn
2622 gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad, GstObject * parent,
2625 GstRtpJitterBuffer *jitterbuffer;
2626 GstRtpJitterBufferPrivate *priv;
2627 GstFlowReturn ret = GST_FLOW_OK;
2629 GstRTCPPacket packet;
2630 guint64 ext_rtptime;
2632 GstRTCPBuffer rtcp = { NULL, };
2634 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
2636 if (G_UNLIKELY (!gst_rtcp_buffer_validate (buffer)))
2637 goto invalid_buffer;
2639 priv = jitterbuffer->priv;
2641 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2643 if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet))
2646 /* first packet must be SR or RR or else the validate would have failed */
2647 switch (gst_rtcp_packet_get_type (&packet)) {
2648 case GST_RTCP_TYPE_SR:
2649 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, &rtptime,
2655 gst_rtcp_buffer_unmap (&rtcp);
2657 GST_DEBUG_OBJECT (jitterbuffer, "received RTCP of SSRC %08x", ssrc);
2660 /* convert the RTP timestamp to our extended timestamp, using the same offset
2661 * we used in the jitterbuffer */
2662 ext_rtptime = priv->jbuf->ext_rtptime;
2663 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
2665 priv->ext_rtptime = ext_rtptime;
2666 gst_buffer_replace (&priv->last_sr, buffer);
2668 do_handle_sync (jitterbuffer);
2672 gst_buffer_unref (buffer);
2678 /* this is not fatal but should be filtered earlier */
2679 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2680 ("Received invalid RTCP payload, dropping"));
2686 /* this is not fatal but should be filtered earlier */
2687 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2688 ("Received empty RTCP payload, dropping"));
2689 gst_rtcp_buffer_unmap (&rtcp);
2695 GST_DEBUG_OBJECT (jitterbuffer, "ignoring RTCP packet");
2696 gst_rtcp_buffer_unmap (&rtcp);
2703 gst_rtp_jitter_buffer_sink_query (GstPad * pad, GstObject * parent,
2706 gboolean res = FALSE;
2708 switch (GST_QUERY_TYPE (query)) {
2709 case GST_QUERY_CAPS:
2711 GstCaps *filter, *caps;
2713 gst_query_parse_caps (query, &filter);
2714 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
2715 gst_query_set_caps_result (query, caps);
2716 gst_caps_unref (caps);
2721 if (GST_QUERY_IS_SERIALIZED (query)) {
2722 GST_WARNING_OBJECT (pad, "unhandled serialized query");
2725 res = gst_pad_query_default (pad, parent, query);
2733 gst_rtp_jitter_buffer_src_query (GstPad * pad, GstObject * parent,
2736 GstRtpJitterBuffer *jitterbuffer;
2737 GstRtpJitterBufferPrivate *priv;
2738 gboolean res = FALSE;
2740 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
2741 priv = jitterbuffer->priv;
2743 switch (GST_QUERY_TYPE (query)) {
2744 case GST_QUERY_LATENCY:
2746 /* We need to send the query upstream and add the returned latency to our
2748 GstClockTime min_latency, max_latency;
2750 GstClockTime our_latency;
2752 if ((res = gst_pad_peer_query (priv->sinkpad, query))) {
2753 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
2755 GST_DEBUG_OBJECT (jitterbuffer, "Peer latency: min %"
2756 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
2757 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2759 /* store this so that we can safely sync on the peer buffers. */
2761 priv->peer_latency = min_latency;
2762 our_latency = priv->latency_ns;
2765 GST_DEBUG_OBJECT (jitterbuffer, "Our latency: %" GST_TIME_FORMAT,
2766 GST_TIME_ARGS (our_latency));
2768 /* we add some latency but can buffer an infinite amount of time */
2769 min_latency += our_latency;
2772 GST_DEBUG_OBJECT (jitterbuffer, "Calculated total latency : min %"
2773 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
2774 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2776 gst_query_set_latency (query, TRUE, min_latency, max_latency);
2780 case GST_QUERY_POSITION:
2782 GstClockTime start, last_out;
2785 gst_query_parse_position (query, &fmt, NULL);
2786 if (fmt != GST_FORMAT_TIME) {
2787 res = gst_pad_query_default (pad, parent, query);
2792 start = priv->npt_start;
2793 last_out = priv->last_out_time;
2796 GST_DEBUG_OBJECT (jitterbuffer, "npt start %" GST_TIME_FORMAT
2797 ", last out %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
2798 GST_TIME_ARGS (last_out));
2800 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (last_out)) {
2801 /* bring 0-based outgoing time to stream time */
2802 gst_query_set_position (query, GST_FORMAT_TIME, start + last_out);
2805 res = gst_pad_query_default (pad, parent, query);
2809 case GST_QUERY_CAPS:
2811 GstCaps *filter, *caps;
2813 gst_query_parse_caps (query, &filter);
2814 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
2815 gst_query_set_caps_result (query, caps);
2816 gst_caps_unref (caps);
2821 res = gst_pad_query_default (pad, parent, query);
2829 gst_rtp_jitter_buffer_set_property (GObject * object,
2830 guint prop_id, const GValue * value, GParamSpec * pspec)
2832 GstRtpJitterBuffer *jitterbuffer;
2833 GstRtpJitterBufferPrivate *priv;
2835 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
2836 priv = jitterbuffer->priv;
2841 guint new_latency, old_latency;
2843 new_latency = g_value_get_uint (value);
2846 old_latency = priv->latency_ms;
2847 priv->latency_ms = new_latency;
2848 priv->latency_ns = priv->latency_ms * GST_MSECOND;
2849 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
2852 /* post message if latency changed, this will inform the parent pipeline
2853 * that a latency reconfiguration is possible/needed. */
2854 if (new_latency != old_latency) {
2855 GST_DEBUG_OBJECT (jitterbuffer, "latency changed to: %" GST_TIME_FORMAT,
2856 GST_TIME_ARGS (new_latency * GST_MSECOND));
2858 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer),
2859 gst_message_new_latency (GST_OBJECT_CAST (jitterbuffer)));
2863 case PROP_DROP_ON_LATENCY:
2865 priv->drop_on_latency = g_value_get_boolean (value);
2868 case PROP_TS_OFFSET:
2870 priv->ts_offset = g_value_get_int64 (value);
2871 priv->ts_discont = TRUE;
2876 priv->do_lost = g_value_get_boolean (value);
2881 rtp_jitter_buffer_set_mode (priv->jbuf, g_value_get_enum (value));
2884 case PROP_DO_RETRANSMISSION:
2886 priv->do_retransmission = g_value_get_boolean (value);
2889 case PROP_RTX_DELAY:
2891 priv->rtx_delay = g_value_get_int (value);
2894 case PROP_RTX_DELAY_REORDER:
2896 priv->rtx_delay_reorder = g_value_get_int (value);
2899 case PROP_RTX_RETRY_TIMEOUT:
2901 priv->rtx_retry_timeout = g_value_get_int (value);
2904 case PROP_RTX_RETRY_PERIOD:
2906 priv->rtx_retry_period = g_value_get_int (value);
2910 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2916 gst_rtp_jitter_buffer_get_property (GObject * object,
2917 guint prop_id, GValue * value, GParamSpec * pspec)
2919 GstRtpJitterBuffer *jitterbuffer;
2920 GstRtpJitterBufferPrivate *priv;
2922 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
2923 priv = jitterbuffer->priv;
2928 g_value_set_uint (value, priv->latency_ms);
2931 case PROP_DROP_ON_LATENCY:
2933 g_value_set_boolean (value, priv->drop_on_latency);
2936 case PROP_TS_OFFSET:
2938 g_value_set_int64 (value, priv->ts_offset);
2943 g_value_set_boolean (value, priv->do_lost);
2948 g_value_set_enum (value, rtp_jitter_buffer_get_mode (priv->jbuf));
2956 if (priv->srcresult != GST_FLOW_OK)
2959 percent = rtp_jitter_buffer_get_percent (priv->jbuf);
2961 g_value_set_int (value, percent);
2965 case PROP_DO_RETRANSMISSION:
2967 g_value_set_boolean (value, priv->do_retransmission);
2970 case PROP_RTX_DELAY:
2972 g_value_set_int (value, priv->rtx_delay);
2975 case PROP_RTX_DELAY_REORDER:
2977 g_value_set_int (value, priv->rtx_delay_reorder);
2980 case PROP_RTX_RETRY_TIMEOUT:
2982 g_value_set_int (value, priv->rtx_retry_timeout);
2985 case PROP_RTX_RETRY_PERIOD:
2987 g_value_set_int (value, priv->rtx_retry_period);
2991 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);