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-rtpjitterbuffer
29 * This element reorders and removes duplicate RTP packets as they are received
30 * from a network source.
32 * The element needs the clock-rate of the RTP payload in order to estimate the
33 * delay. This information is obtained either from the caps on the sink pad or,
34 * when no caps are present, from the #GstRtpJitterBuffer::request-pt-map signal.
35 * To clear the previous pt-map use the #GstRtpJitterBuffer::clear-pt-map signal.
37 * The rtpjitterbuffer will wait for missing packets up to a configurable time
38 * limit using the #GstRtpJitterBuffer:latency property. Packets arriving too
39 * late are considered to be lost packets. If the #GstRtpJitterBuffer:do-lost
40 * property is set, lost packets will result in a custom serialized downstream
41 * event of name GstRTPPacketLost. The lost packet events are usually used by a
42 * depayloader or other element to create concealment data or some other logic
43 * to gracefully handle the missing packets.
45 * The jitterbuffer will use the DTS (or PTS if no DTS is set) of the incomming
46 * buffer and the rtptime inside the RTP packet to create a PTS on the outgoing
49 * The jitterbuffer can also be configured to send early retransmission events
50 * upstream by setting the #GstRtpJitterBuffer:do-retransmission property. In
51 * this mode, the jitterbuffer tries to estimate when a packet should arrive and
52 * sends a custom upstream event named GstRTPRetransmissionRequest when the
53 * packet is considered late. The initial expected packet arrival time is
54 * calculated as follows:
56 * - If seqnum N arrived at time T, seqnum N+1 is expected to arrive at
57 * T + packet-spacing + #GstRtpJitterBuffer:rtx-delay. The packet spacing is
58 * calculated from the DTS (or PTS is no DTS) of two consecutive RTP
59 * packets with different rtptime.
61 * - If seqnum N0 arrived at time T0 and seqnum Nm arrived at time Tm,
62 * seqnum Ni is expected at time Ti = T0 + i*(Tm - T0)/(Nm - N0). Any
63 * previously scheduled timeout is overwritten.
65 * - If seqnum N arrived, all seqnum older than
66 * N - #GstRtpJitterBuffer:rtx-delay-reorder are considered late
67 * immediately. This is to request fast feedback for abonormally reorder
68 * packets before any of the previous timeouts is triggered.
70 * A late packet triggers the GstRTPRetransmissionRequest custom upstream
71 * event. After the initial timeout expires and the retransmission event is
72 * sent, the timeout is scheduled for
73 * T + #GstRtpJitterBuffer:rtx-retry-timeout. If the missing packet did not
74 * arrive after #GstRtpJitterBuffer:rtx-retry-timeout, a new
75 * GstRTPRetransmissionRequest is sent upstream and the timeout is rescheduled
76 * again for T + #GstRtpJitterBuffer:rtx-retry-timeout. This repeats until
77 * #GstRtpJitterBuffer:rtx-retry-period elapsed, at which point no further
78 * retransmission requests are sent and the regular logic is performed to
79 * schedule a lost packet as discussed above.
81 * This element acts as a live element and so adds #GstRtpJitterBuffer:latency
84 * This element will automatically be used inside rtpbin.
87 * <title>Example pipelines</title>
89 * gst-launch-1.0 rtspsrc location=rtsp://192.168.1.133:8554/mpeg1or2AudioVideoTest ! rtpjitterbuffer ! rtpmpvdepay ! mpeg2dec ! xvimagesink
90 * ]| Connect to a streaming server and decode the MPEG video. The jitterbuffer is
91 * inserted into the pipeline to smooth out network jitter and to reorder the
92 * out-of-order RTP packets.
102 #include <gst/rtp/gstrtpbuffer.h>
104 #include "gstrtpjitterbuffer.h"
105 #include "rtpjitterbuffer.h"
106 #include "rtpstats.h"
108 #include <gst/glib-compat-private.h>
110 GST_DEBUG_CATEGORY (rtpjitterbuffer_debug);
111 #define GST_CAT_DEFAULT (rtpjitterbuffer_debug)
113 /* RTPJitterBuffer signals and args */
116 SIGNAL_REQUEST_PT_MAP,
124 #define DEFAULT_LATENCY_MS 200
125 #define DEFAULT_DROP_ON_LATENCY FALSE
126 #define DEFAULT_TS_OFFSET 0
127 #define DEFAULT_DO_LOST FALSE
128 #define DEFAULT_MODE RTP_JITTER_BUFFER_MODE_SLAVE
129 #define DEFAULT_PERCENT 0
130 #define DEFAULT_DO_RETRANSMISSION FALSE
131 #define DEFAULT_RTX_NEXT_SEQNUM TRUE
132 #define DEFAULT_RTX_DELAY -1
133 #define DEFAULT_RTX_MIN_DELAY 0
134 #define DEFAULT_RTX_DELAY_REORDER 3
135 #define DEFAULT_RTX_RETRY_TIMEOUT -1
136 #define DEFAULT_RTX_MIN_RETRY_TIMEOUT -1
137 #define DEFAULT_RTX_RETRY_PERIOD -1
138 #define DEFAULT_RTX_MAX_RETRIES -1
140 #define DEFAULT_AUTO_RTX_DELAY (20 * GST_MSECOND)
141 #define DEFAULT_AUTO_RTX_TIMEOUT (40 * GST_MSECOND)
147 PROP_DROP_ON_LATENCY,
152 PROP_DO_RETRANSMISSION,
153 PROP_RTX_NEXT_SEQNUM,
156 PROP_RTX_DELAY_REORDER,
157 PROP_RTX_RETRY_TIMEOUT,
158 PROP_RTX_MIN_RETRY_TIMEOUT,
159 PROP_RTX_RETRY_PERIOD,
160 PROP_RTX_MAX_RETRIES,
165 #define JBUF_LOCK(priv) (g_mutex_lock (&(priv)->jbuf_lock))
167 #define JBUF_LOCK_CHECK(priv,label) G_STMT_START { \
169 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
172 #define JBUF_UNLOCK(priv) (g_mutex_unlock (&(priv)->jbuf_lock))
174 #define JBUF_WAIT_TIMER(priv) G_STMT_START { \
175 GST_DEBUG ("waiting timer"); \
176 (priv)->waiting_timer = TRUE; \
177 g_cond_wait (&(priv)->jbuf_timer, &(priv)->jbuf_lock); \
178 (priv)->waiting_timer = FALSE; \
179 GST_DEBUG ("waiting timer done"); \
181 #define JBUF_SIGNAL_TIMER(priv) G_STMT_START { \
182 if (G_UNLIKELY ((priv)->waiting_timer)) { \
183 GST_DEBUG ("signal timer"); \
184 g_cond_signal (&(priv)->jbuf_timer); \
188 #define JBUF_WAIT_EVENT(priv,label) G_STMT_START { \
189 GST_DEBUG ("waiting event"); \
190 (priv)->waiting_event = TRUE; \
191 g_cond_wait (&(priv)->jbuf_event, &(priv)->jbuf_lock); \
192 (priv)->waiting_event = FALSE; \
193 GST_DEBUG ("waiting event done"); \
194 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
197 #define JBUF_SIGNAL_EVENT(priv) G_STMT_START { \
198 if (G_UNLIKELY ((priv)->waiting_event)) { \
199 GST_DEBUG ("signal event"); \
200 g_cond_signal (&(priv)->jbuf_event); \
204 #define JBUF_WAIT_QUERY(priv,label) G_STMT_START { \
205 GST_DEBUG ("waiting query"); \
206 (priv)->waiting_query = TRUE; \
207 g_cond_wait (&(priv)->jbuf_query, &(priv)->jbuf_lock); \
208 (priv)->waiting_query = FALSE; \
209 GST_DEBUG ("waiting query done"); \
210 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
213 #define JBUF_SIGNAL_QUERY(priv,res) G_STMT_START { \
214 (priv)->last_query = res; \
215 if (G_UNLIKELY ((priv)->waiting_query)) { \
216 GST_DEBUG ("signal query"); \
217 g_cond_signal (&(priv)->jbuf_query); \
222 struct _GstRtpJitterBufferPrivate
224 GstPad *sinkpad, *srcpad;
227 RTPJitterBuffer *jbuf;
229 gboolean waiting_timer;
231 gboolean waiting_event;
233 gboolean waiting_query;
241 gboolean timer_running;
242 GThread *timer_thread;
247 gboolean drop_on_latency;
250 gboolean do_retransmission;
251 gboolean rtx_next_seqnum;
254 gint rtx_delay_reorder;
255 gint rtx_retry_timeout;
256 gint rtx_min_retry_timeout;
257 gint rtx_retry_period;
258 gint rtx_max_retries;
260 /* the last seqnum we pushed out */
261 guint32 last_popped_seqnum;
262 /* the next expected seqnum we push */
264 /* seqnum-base, if known */
266 /* last output time */
267 GstClockTime last_out_time;
268 /* last valid input timestamp and rtptime pair */
269 GstClockTime ips_dts;
271 GstClockTime packet_spacing;
273 /* the next expected seqnum we receive */
274 GstClockTime last_in_dts;
275 guint32 last_in_seqnum;
276 guint32 next_in_seqnum;
280 /* start and stop ranges */
281 GstClockTime npt_start;
282 GstClockTime npt_stop;
283 guint64 ext_timestamp;
284 guint64 last_elapsed;
285 guint64 estimated_eos;
292 /* clock rate and rtp timestamp offset */
296 gint64 prev_ts_offset;
298 /* when we are shutting down */
299 GstFlowReturn srcresult;
305 GstClockTime timer_timeout;
306 guint16 timer_seqnum;
307 /* the latency of the upstream peer, we have to take this into account when
308 * synchronizing the buffers. */
309 GstClockTime peer_latency;
313 /* some accounting */
315 guint64 num_duplicates;
316 guint64 num_rtx_requests;
317 guint64 num_rtx_success;
318 guint64 num_rtx_failed;
323 GstClockTime last_dts;
324 guint64 last_rtptime;
325 GstClockTime avg_jitter;
342 GstClockTime timeout;
343 GstClockTime duration;
344 GstClockTime rtx_base;
345 GstClockTime rtx_delay;
346 GstClockTime rtx_retry;
347 GstClockTime rtx_last;
351 #define GST_RTP_JITTER_BUFFER_GET_PRIVATE(o) \
352 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_RTP_JITTER_BUFFER, \
353 GstRtpJitterBufferPrivate))
355 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_template =
356 GST_STATIC_PAD_TEMPLATE ("sink",
359 GST_STATIC_CAPS ("application/x-rtp"
360 /* "clock-rate = (int) [ 1, 2147483647 ], "
361 * "payload = (int) , "
362 * "encoding-name = (string) "
366 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_rtcp_template =
367 GST_STATIC_PAD_TEMPLATE ("sink_rtcp",
370 GST_STATIC_CAPS ("application/x-rtcp")
373 static GstStaticPadTemplate gst_rtp_jitter_buffer_src_template =
374 GST_STATIC_PAD_TEMPLATE ("src",
377 GST_STATIC_CAPS ("application/x-rtp"
378 /* "payload = (int) , "
379 * "clock-rate = (int) , "
380 * "encoding-name = (string) "
384 static guint gst_rtp_jitter_buffer_signals[LAST_SIGNAL] = { 0 };
386 #define gst_rtp_jitter_buffer_parent_class parent_class
387 G_DEFINE_TYPE (GstRtpJitterBuffer, gst_rtp_jitter_buffer, GST_TYPE_ELEMENT);
389 /* object overrides */
390 static void gst_rtp_jitter_buffer_set_property (GObject * object,
391 guint prop_id, const GValue * value, GParamSpec * pspec);
392 static void gst_rtp_jitter_buffer_get_property (GObject * object,
393 guint prop_id, GValue * value, GParamSpec * pspec);
394 static void gst_rtp_jitter_buffer_finalize (GObject * object);
396 /* element overrides */
397 static GstStateChangeReturn gst_rtp_jitter_buffer_change_state (GstElement
398 * element, GstStateChange transition);
399 static GstPad *gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
400 GstPadTemplate * templ, const gchar * name, const GstCaps * filter);
401 static void gst_rtp_jitter_buffer_release_pad (GstElement * element,
403 static GstClock *gst_rtp_jitter_buffer_provide_clock (GstElement * element);
406 static GstCaps *gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter);
407 static GstIterator *gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad,
410 /* sinkpad overrides */
411 static gboolean gst_rtp_jitter_buffer_sink_event (GstPad * pad,
412 GstObject * parent, GstEvent * event);
413 static GstFlowReturn gst_rtp_jitter_buffer_chain (GstPad * pad,
414 GstObject * parent, GstBuffer * buffer);
416 static gboolean gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad,
417 GstObject * parent, GstEvent * event);
418 static GstFlowReturn gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad,
419 GstObject * parent, GstBuffer * buffer);
421 static gboolean gst_rtp_jitter_buffer_sink_query (GstPad * pad,
422 GstObject * parent, GstQuery * query);
424 /* srcpad overrides */
425 static gboolean gst_rtp_jitter_buffer_src_event (GstPad * pad,
426 GstObject * parent, GstEvent * event);
427 static gboolean gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad,
428 GstObject * parent, GstPadMode mode, gboolean active);
429 static void gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer);
430 static gboolean gst_rtp_jitter_buffer_src_query (GstPad * pad,
431 GstObject * parent, GstQuery * query);
434 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer);
436 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jitterbuffer,
437 gboolean active, guint64 base_time);
438 static void do_handle_sync (GstRtpJitterBuffer * jitterbuffer);
440 static void unschedule_current_timer (GstRtpJitterBuffer * jitterbuffer);
441 static void remove_all_timers (GstRtpJitterBuffer * jitterbuffer);
443 static void wait_next_timeout (GstRtpJitterBuffer * jitterbuffer);
445 static GstStructure *gst_rtp_jitter_buffer_create_stats (GstRtpJitterBuffer *
449 gst_rtp_jitter_buffer_class_init (GstRtpJitterBufferClass * klass)
451 GObjectClass *gobject_class;
452 GstElementClass *gstelement_class;
454 gobject_class = (GObjectClass *) klass;
455 gstelement_class = (GstElementClass *) klass;
457 g_type_class_add_private (klass, sizeof (GstRtpJitterBufferPrivate));
459 gobject_class->finalize = gst_rtp_jitter_buffer_finalize;
461 gobject_class->set_property = gst_rtp_jitter_buffer_set_property;
462 gobject_class->get_property = gst_rtp_jitter_buffer_get_property;
465 * GstRtpJitterBuffer:latency:
467 * The maximum latency of the jitterbuffer. Packets will be kept in the buffer
468 * for at most this time.
470 g_object_class_install_property (gobject_class, PROP_LATENCY,
471 g_param_spec_uint ("latency", "Buffer latency in ms",
472 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
473 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 * GstRtpJitterBuffer:drop-on-latency:
477 * Drop oldest buffers when the queue is completely filled.
479 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
480 g_param_spec_boolean ("drop-on-latency",
481 "Drop buffers when maximum latency is reached",
482 "Tells the jitterbuffer to never exceed the given latency in size",
483 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 * GstRtpJitterBuffer:ts-offset:
487 * Adjust GStreamer output buffer timestamps in the jitterbuffer with offset.
488 * This is mainly used to ensure interstream synchronisation.
490 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
491 g_param_spec_int64 ("ts-offset", "Timestamp Offset",
492 "Adjust buffer timestamps with offset in nanoseconds", G_MININT64,
493 G_MAXINT64, DEFAULT_TS_OFFSET,
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
497 * GstRtpJitterBuffer:do-lost:
499 * Send out a GstRTPPacketLost event downstream when a packet is considered
502 g_object_class_install_property (gobject_class, PROP_DO_LOST,
503 g_param_spec_boolean ("do-lost", "Do Lost",
504 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
505 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
508 * GstRtpJitterBuffer:mode:
510 * Control the buffering and timestamping mode used by the jitterbuffer.
512 g_object_class_install_property (gobject_class, PROP_MODE,
513 g_param_spec_enum ("mode", "Mode",
514 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
515 DEFAULT_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
517 * GstRtpJitterBuffer:percent:
519 * The percent of the jitterbuffer that is filled.
521 g_object_class_install_property (gobject_class, PROP_PERCENT,
522 g_param_spec_int ("percent", "percent",
523 "The buffer filled percent", 0, 100,
524 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
526 * GstRtpJitterBuffer:do-retransmission:
528 * Send out a GstRTPRetransmission event upstream when a packet is considered
529 * late and should be retransmitted.
533 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
534 g_param_spec_boolean ("do-retransmission", "Do Retransmission",
535 "Send retransmission events upstream when a packet is late",
536 DEFAULT_DO_RETRANSMISSION,
537 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 * GstRtpJitterBuffer:rtx-next-seqnum
542 * Estimate when the next packet should arrive and schedule a retransmission
544 * This is, when packet N arrives, a GstRTPRetransmission event is schedule
545 * for packet N+1. So it will be requested if it does not arrive at the expected time.
546 * The expected time is calculated using the dts of N and the packet spacing.
550 g_object_class_install_property (gobject_class, PROP_RTX_NEXT_SEQNUM,
551 g_param_spec_boolean ("rtx-next-seqnum", "RTX next seqnum",
552 "Estimate when the next packet should arrive and schedule a "
553 "retransmission request for it.",
554 DEFAULT_RTX_NEXT_SEQNUM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 * GstRtpJitterBuffer:rtx-delay:
559 * When a packet did not arrive at the expected time, wait this extra amount
560 * of time before sending a retransmission event.
562 * When -1 is used, the max jitter will be used as extra delay.
566 g_object_class_install_property (gobject_class, PROP_RTX_DELAY,
567 g_param_spec_int ("rtx-delay", "RTX Delay",
568 "Extra time in ms to wait before sending retransmission "
569 "event (-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_DELAY,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 * GstRtpJitterBuffer:rtx-min-delay:
575 * When a packet did not arrive at the expected time, wait at least this extra amount
576 * of time before sending a retransmission event.
580 g_object_class_install_property (gobject_class, PROP_RTX_MIN_DELAY,
581 g_param_spec_uint ("rtx-min-delay", "Minimum RTX Delay",
582 "Minimum time in ms to wait before sending retransmission "
583 "event", 0, G_MAXUINT, DEFAULT_RTX_MIN_DELAY,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
586 * GstRtpJitterBuffer:rtx-delay-reorder:
588 * Assume that a retransmission event should be sent when we see
589 * this much packet reordering.
591 * When -1 is used, the value will be estimated based on observed packet
596 g_object_class_install_property (gobject_class, PROP_RTX_DELAY_REORDER,
597 g_param_spec_int ("rtx-delay-reorder", "RTX Delay Reorder",
598 "Sending retransmission event when this much reordering (-1 automatic)",
599 -1, G_MAXINT, DEFAULT_RTX_DELAY_REORDER,
600 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
602 * GstRtpJitterBuffer::rtx-retry-timeout:
604 * When no packet has been received after sending a retransmission event
605 * for this time, retry sending a retransmission event.
607 * When -1 is used, the value will be estimated based on observed round
612 g_object_class_install_property (gobject_class, PROP_RTX_RETRY_TIMEOUT,
613 g_param_spec_int ("rtx-retry-timeout", "RTX Retry Timeout",
614 "Retry sending a transmission event after this timeout in "
615 "ms (-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_RETRY_TIMEOUT,
616 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
618 * GstRtpJitterBuffer::rtx-min-retry-timeout:
620 * The minimum amount of time between retry timeouts. When
621 * GstRtpJitterBuffer::rtx-retry-timeout is -1, this value ensures a
622 * minimum interval between retry timeouts.
624 * When -1 is used, the value will be estimated based on the
629 g_object_class_install_property (gobject_class, PROP_RTX_MIN_RETRY_TIMEOUT,
630 g_param_spec_int ("rtx-min-retry-timeout", "RTX Min Retry Timeout",
631 "Minimum timeout between sending a transmission event in "
632 "ms (-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_MIN_RETRY_TIMEOUT,
633 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
635 * GstRtpJitterBuffer:rtx-retry-period:
637 * The amount of time to try to get a retransmission.
639 * When -1 is used, the value will be estimated based on the jitterbuffer
640 * latency and the observed round trip time.
644 g_object_class_install_property (gobject_class, PROP_RTX_RETRY_PERIOD,
645 g_param_spec_int ("rtx-retry-period", "RTX Retry Period",
646 "Try to get a retransmission for this many ms "
647 "(-1 automatic)", -1, G_MAXINT, DEFAULT_RTX_RETRY_PERIOD,
648 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
650 * GstRtpJitterBuffer:rtx-max-retries:
652 * The maximum number of retries to request a retransmission.
654 * This implies that as maximum (rtx-max-retries + 1) retransmissions will be requested.
655 * When -1 is used, the number of retransmission request will not be limited.
659 g_object_class_install_property (gobject_class, PROP_RTX_MAX_RETRIES,
660 g_param_spec_int ("rtx-max-retries", "RTX Max Retries",
661 "The maximum number of retries to request a retransmission. "
662 "(-1 not limited)", -1, G_MAXINT, DEFAULT_RTX_MAX_RETRIES,
663 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
665 * GstRtpJitterBuffer:stats:
667 * Various jitterbuffer statistics. This property returns a GstStructure
668 * with name application/x-rtp-jitterbuffer-stats with the following fields:
670 * "rtx-count" G_TYPE_UINT64 The number of retransmissions requested
671 * "rtx-success-count" G_TYPE_UINT64 The number of successful retransmissions
672 * "rtx-per-packet" G_TYPE_DOUBLE Average number of RTX per packet
673 * "rtx-rtt" G_TYPE_UINT64 Average round trip time per RTX
677 g_object_class_install_property (gobject_class, PROP_STATS,
678 g_param_spec_boxed ("stats", "Statistics",
679 "Various statistics", GST_TYPE_STRUCTURE,
680 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
683 * GstRtpJitterBuffer::request-pt-map:
684 * @buffer: the object which received the signal
687 * Request the payload type as #GstCaps for @pt.
689 gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP] =
690 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
691 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
692 request_pt_map), NULL, NULL, g_cclosure_marshal_generic,
693 GST_TYPE_CAPS, 1, G_TYPE_UINT);
695 * GstRtpJitterBuffer::handle-sync:
696 * @buffer: the object which received the signal
697 * @struct: a GstStructure containing sync values.
699 * Be notified of new sync values.
701 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC] =
702 g_signal_new ("handle-sync", G_TYPE_FROM_CLASS (klass),
703 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
704 handle_sync), NULL, NULL, g_cclosure_marshal_VOID__BOXED,
705 G_TYPE_NONE, 1, GST_TYPE_STRUCTURE | G_SIGNAL_TYPE_STATIC_SCOPE);
708 * GstRtpJitterBuffer::on-npt-stop:
709 * @buffer: the object which received the signal
711 * Signal that the jitterbufer has pushed the RTP packet that corresponds to
712 * the npt-stop position.
714 gst_rtp_jitter_buffer_signals[SIGNAL_ON_NPT_STOP] =
715 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
716 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
717 on_npt_stop), NULL, NULL, g_cclosure_marshal_VOID__VOID,
718 G_TYPE_NONE, 0, G_TYPE_NONE);
721 * GstRtpJitterBuffer::clear-pt-map:
722 * @buffer: the object which received the signal
724 * Invalidate the clock-rate as obtained with the
725 * #GstRtpJitterBuffer::request-pt-map signal.
727 gst_rtp_jitter_buffer_signals[SIGNAL_CLEAR_PT_MAP] =
728 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
729 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
730 G_STRUCT_OFFSET (GstRtpJitterBufferClass, clear_pt_map), NULL, NULL,
731 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
734 * GstRtpJitterBuffer::set-active:
735 * @buffer: the object which received the signal
737 * Start pushing out packets with the given base time. This signal is only
738 * useful in buffering mode.
740 * Returns: the time of the last pushed packet.
742 gst_rtp_jitter_buffer_signals[SIGNAL_SET_ACTIVE] =
743 g_signal_new ("set-active", G_TYPE_FROM_CLASS (klass),
744 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
745 G_STRUCT_OFFSET (GstRtpJitterBufferClass, set_active), NULL, NULL,
746 g_cclosure_marshal_generic, G_TYPE_UINT64, 2, G_TYPE_BOOLEAN,
749 gstelement_class->change_state =
750 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_change_state);
751 gstelement_class->request_new_pad =
752 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_request_new_pad);
753 gstelement_class->release_pad =
754 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_release_pad);
755 gstelement_class->provide_clock =
756 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_provide_clock);
758 gst_element_class_add_pad_template (gstelement_class,
759 gst_static_pad_template_get (&gst_rtp_jitter_buffer_src_template));
760 gst_element_class_add_pad_template (gstelement_class,
761 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_template));
762 gst_element_class_add_pad_template (gstelement_class,
763 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_rtcp_template));
765 gst_element_class_set_static_metadata (gstelement_class,
766 "RTP packet jitter-buffer", "Filter/Network/RTP",
767 "A buffer that deals with network jitter and other transmission faults",
768 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
769 "Wim Taymans <wim.taymans@gmail.com>");
771 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_clear_pt_map);
772 klass->set_active = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_set_active);
774 GST_DEBUG_CATEGORY_INIT
775 (rtpjitterbuffer_debug, "rtpjitterbuffer", 0, "RTP Jitter Buffer");
779 gst_rtp_jitter_buffer_init (GstRtpJitterBuffer * jitterbuffer)
781 GstRtpJitterBufferPrivate *priv;
783 priv = GST_RTP_JITTER_BUFFER_GET_PRIVATE (jitterbuffer);
784 jitterbuffer->priv = priv;
786 priv->latency_ms = DEFAULT_LATENCY_MS;
787 priv->latency_ns = priv->latency_ms * GST_MSECOND;
788 priv->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
789 priv->do_lost = DEFAULT_DO_LOST;
790 priv->do_retransmission = DEFAULT_DO_RETRANSMISSION;
791 priv->rtx_next_seqnum = DEFAULT_RTX_NEXT_SEQNUM;
792 priv->rtx_delay = DEFAULT_RTX_DELAY;
793 priv->rtx_min_delay = DEFAULT_RTX_MIN_DELAY;
794 priv->rtx_delay_reorder = DEFAULT_RTX_DELAY_REORDER;
795 priv->rtx_retry_timeout = DEFAULT_RTX_RETRY_TIMEOUT;
796 priv->rtx_min_retry_timeout = DEFAULT_RTX_MIN_RETRY_TIMEOUT;
797 priv->rtx_retry_period = DEFAULT_RTX_RETRY_PERIOD;
798 priv->rtx_max_retries = DEFAULT_RTX_MAX_RETRIES;
801 priv->last_rtptime = -1;
802 priv->avg_jitter = 0;
803 priv->timers = g_array_new (FALSE, TRUE, sizeof (TimerData));
804 priv->jbuf = rtp_jitter_buffer_new ();
805 g_mutex_init (&priv->jbuf_lock);
806 g_cond_init (&priv->jbuf_timer);
807 g_cond_init (&priv->jbuf_event);
808 g_cond_init (&priv->jbuf_query);
810 /* reset skew detection initialy */
811 rtp_jitter_buffer_reset_skew (priv->jbuf);
812 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
813 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
817 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_src_template,
820 gst_pad_set_activatemode_function (priv->srcpad,
821 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_activate_mode));
822 gst_pad_set_query_function (priv->srcpad,
823 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_query));
824 gst_pad_set_event_function (priv->srcpad,
825 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_event));
828 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_sink_template,
831 gst_pad_set_chain_function (priv->sinkpad,
832 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_chain));
833 gst_pad_set_event_function (priv->sinkpad,
834 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_event));
835 gst_pad_set_query_function (priv->sinkpad,
836 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_query));
838 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->srcpad);
839 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->sinkpad);
841 GST_OBJECT_FLAG_SET (jitterbuffer, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
844 #define IS_DROPABLE(it) (((it)->type == ITEM_TYPE_BUFFER) || ((it)->type == ITEM_TYPE_LOST))
846 #define ITEM_TYPE_BUFFER 0
847 #define ITEM_TYPE_LOST 1
848 #define ITEM_TYPE_EVENT 2
849 #define ITEM_TYPE_QUERY 3
851 static RTPJitterBufferItem *
852 alloc_item (gpointer data, guint type, GstClockTime dts, GstClockTime pts,
853 guint seqnum, guint count, guint rtptime)
855 RTPJitterBufferItem *item;
857 item = g_slice_new (RTPJitterBufferItem);
864 item->seqnum = seqnum;
866 item->rtptime = rtptime;
872 free_item (RTPJitterBufferItem * item)
874 if (item->data && item->type != ITEM_TYPE_QUERY)
875 gst_mini_object_unref (item->data);
876 g_slice_free (RTPJitterBufferItem, item);
880 free_item_and_retain_events (RTPJitterBufferItem * item, gpointer user_data)
882 GList **l = user_data;
884 if (item->data && item->type == ITEM_TYPE_EVENT
885 && GST_EVENT_IS_STICKY (item->data)) {
886 *l = g_list_prepend (*l, item->data);
887 } else if (item->data && item->type != ITEM_TYPE_QUERY) {
888 gst_mini_object_unref (item->data);
890 g_slice_free (RTPJitterBufferItem, item);
894 gst_rtp_jitter_buffer_finalize (GObject * object)
896 GstRtpJitterBuffer *jitterbuffer;
897 GstRtpJitterBufferPrivate *priv;
899 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
900 priv = jitterbuffer->priv;
902 g_array_free (priv->timers, TRUE);
903 g_mutex_clear (&priv->jbuf_lock);
904 g_cond_clear (&priv->jbuf_timer);
905 g_cond_clear (&priv->jbuf_event);
906 g_cond_clear (&priv->jbuf_query);
908 rtp_jitter_buffer_flush (priv->jbuf, (GFunc) free_item, NULL);
909 g_object_unref (priv->jbuf);
911 G_OBJECT_CLASS (parent_class)->finalize (object);
915 gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad, GstObject * parent)
917 GstRtpJitterBuffer *jitterbuffer;
918 GstPad *otherpad = NULL;
919 GstIterator *it = NULL;
922 jitterbuffer = GST_RTP_JITTER_BUFFER_CAST (parent);
924 if (pad == jitterbuffer->priv->sinkpad) {
925 otherpad = jitterbuffer->priv->srcpad;
926 } else if (pad == jitterbuffer->priv->srcpad) {
927 otherpad = jitterbuffer->priv->sinkpad;
928 } else if (pad == jitterbuffer->priv->rtcpsinkpad) {
929 it = gst_iterator_new_single (GST_TYPE_PAD, NULL);
933 g_value_init (&val, GST_TYPE_PAD);
934 g_value_set_object (&val, otherpad);
935 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
936 g_value_unset (&val);
943 create_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
945 GstRtpJitterBufferPrivate *priv;
947 priv = jitterbuffer->priv;
949 GST_DEBUG_OBJECT (jitterbuffer, "creating RTCP sink pad");
952 gst_pad_new_from_static_template
953 (&gst_rtp_jitter_buffer_sink_rtcp_template, "sink_rtcp");
954 gst_pad_set_chain_function (priv->rtcpsinkpad,
955 gst_rtp_jitter_buffer_chain_rtcp);
956 gst_pad_set_event_function (priv->rtcpsinkpad,
957 (GstPadEventFunction) gst_rtp_jitter_buffer_sink_rtcp_event);
958 gst_pad_set_iterate_internal_links_function (priv->rtcpsinkpad,
959 gst_rtp_jitter_buffer_iterate_internal_links);
960 gst_pad_set_active (priv->rtcpsinkpad, TRUE);
961 gst_element_add_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
963 return priv->rtcpsinkpad;
967 remove_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
969 GstRtpJitterBufferPrivate *priv;
971 priv = jitterbuffer->priv;
973 GST_DEBUG_OBJECT (jitterbuffer, "removing RTCP sink pad");
975 gst_pad_set_active (priv->rtcpsinkpad, FALSE);
977 gst_element_remove_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
978 priv->rtcpsinkpad = NULL;
982 gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
983 GstPadTemplate * templ, const gchar * name, const GstCaps * filter)
985 GstRtpJitterBuffer *jitterbuffer;
986 GstElementClass *klass;
988 GstRtpJitterBufferPrivate *priv;
990 g_return_val_if_fail (templ != NULL, NULL);
991 g_return_val_if_fail (GST_IS_RTP_JITTER_BUFFER (element), NULL);
993 jitterbuffer = GST_RTP_JITTER_BUFFER_CAST (element);
994 priv = jitterbuffer->priv;
995 klass = GST_ELEMENT_GET_CLASS (element);
997 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
999 /* figure out the template */
1000 if (templ == gst_element_class_get_pad_template (klass, "sink_rtcp")) {
1001 if (priv->rtcpsinkpad != NULL)
1004 result = create_rtcp_sink (jitterbuffer);
1006 goto wrong_template;
1013 g_warning ("rtpjitterbuffer: this is not our template");
1018 g_warning ("rtpjitterbuffer: pad already requested");
1024 gst_rtp_jitter_buffer_release_pad (GstElement * element, GstPad * pad)
1026 GstRtpJitterBuffer *jitterbuffer;
1027 GstRtpJitterBufferPrivate *priv;
1029 g_return_if_fail (GST_IS_RTP_JITTER_BUFFER (element));
1030 g_return_if_fail (GST_IS_PAD (pad));
1032 jitterbuffer = GST_RTP_JITTER_BUFFER_CAST (element);
1033 priv = jitterbuffer->priv;
1035 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1037 if (priv->rtcpsinkpad == pad) {
1038 remove_rtcp_sink (jitterbuffer);
1047 g_warning ("gstjitterbuffer: asked to release an unknown pad");
1053 gst_rtp_jitter_buffer_provide_clock (GstElement * element)
1055 return gst_system_clock_obtain ();
1059 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer)
1061 GstRtpJitterBufferPrivate *priv;
1063 priv = jitterbuffer->priv;
1065 /* this will trigger a new pt-map request signal, FIXME, do something better. */
1068 priv->clock_rate = -1;
1069 /* do not clear current content, but refresh state for new arrival */
1070 GST_DEBUG_OBJECT (jitterbuffer, "reset jitterbuffer");
1071 rtp_jitter_buffer_reset_skew (priv->jbuf);
1076 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jbuf, gboolean active,
1079 GstRtpJitterBufferPrivate *priv;
1080 GstClockTime last_out;
1081 RTPJitterBufferItem *item;
1086 GST_DEBUG_OBJECT (jbuf, "setting active %d with offset %" GST_TIME_FORMAT,
1087 active, GST_TIME_ARGS (offset));
1089 if (active != priv->active) {
1090 /* add the amount of time spent in paused to the output offset. All
1091 * outgoing buffers will have this offset applied to their timestamps in
1092 * order to make them arrive in time in the sink. */
1093 priv->out_offset = offset;
1094 GST_DEBUG_OBJECT (jbuf, "out offset %" GST_TIME_FORMAT,
1095 GST_TIME_ARGS (priv->out_offset));
1096 priv->active = active;
1097 JBUF_SIGNAL_EVENT (priv);
1100 rtp_jitter_buffer_set_buffering (priv->jbuf, TRUE);
1102 if ((item = rtp_jitter_buffer_peek (priv->jbuf))) {
1103 /* head buffer timestamp and offset gives our output time */
1104 last_out = item->dts + priv->ts_offset;
1106 /* use last known time when the buffer is empty */
1107 last_out = priv->last_out_time;
1115 gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter)
1117 GstRtpJitterBuffer *jitterbuffer;
1118 GstRtpJitterBufferPrivate *priv;
1123 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
1124 priv = jitterbuffer->priv;
1126 other = (pad == priv->srcpad ? priv->sinkpad : priv->srcpad);
1128 caps = gst_pad_peer_query_caps (other, filter);
1130 templ = gst_pad_get_pad_template_caps (pad);
1132 GST_DEBUG_OBJECT (jitterbuffer, "use template");
1137 GST_DEBUG_OBJECT (jitterbuffer, "intersect with template");
1139 intersect = gst_caps_intersect (caps, templ);
1140 gst_caps_unref (caps);
1141 gst_caps_unref (templ);
1145 gst_object_unref (jitterbuffer);
1151 * Must be called with JBUF_LOCK held
1155 gst_jitter_buffer_sink_parse_caps (GstRtpJitterBuffer * jitterbuffer,
1158 GstRtpJitterBufferPrivate *priv;
1159 GstStructure *caps_struct;
1163 priv = jitterbuffer->priv;
1165 /* first parse the caps */
1166 caps_struct = gst_caps_get_structure (caps, 0);
1168 GST_DEBUG_OBJECT (jitterbuffer, "got caps");
1170 /* we need a clock-rate to convert the rtp timestamps to GStreamer time and to
1171 * measure the amount of data in the buffer */
1172 if (!gst_structure_get_int (caps_struct, "clock-rate", &priv->clock_rate))
1175 if (priv->clock_rate <= 0)
1178 GST_DEBUG_OBJECT (jitterbuffer, "got clock-rate %d", priv->clock_rate);
1180 rtp_jitter_buffer_set_clock_rate (priv->jbuf, priv->clock_rate);
1182 /* The clock base is the RTP timestamp corrsponding to the npt-start value. We
1183 * can use this to track the amount of time elapsed on the sender. */
1184 if (gst_structure_get_uint (caps_struct, "clock-base", &val))
1185 priv->clock_base = val;
1187 priv->clock_base = -1;
1189 priv->ext_timestamp = priv->clock_base;
1191 GST_DEBUG_OBJECT (jitterbuffer, "got clock-base %" G_GINT64_FORMAT,
1194 if (gst_structure_get_uint (caps_struct, "seqnum-base", &val)) {
1195 /* first expected seqnum, only update when we didn't have a previous base. */
1196 if (priv->next_in_seqnum == -1)
1197 priv->next_in_seqnum = val;
1198 if (priv->next_seqnum == -1) {
1199 priv->next_seqnum = val;
1200 JBUF_SIGNAL_EVENT (priv);
1202 priv->seqnum_base = val;
1204 priv->seqnum_base = -1;
1207 GST_DEBUG_OBJECT (jitterbuffer, "got seqnum-base %d", priv->next_in_seqnum);
1209 /* the start and stop times. The seqnum-base corresponds to the start time. We
1210 * will keep track of the seqnums on the output and when we reach the one
1211 * corresponding to npt-stop, we emit the npt-stop-reached signal */
1212 if (gst_structure_get_clock_time (caps_struct, "npt-start", &tval))
1213 priv->npt_start = tval;
1215 priv->npt_start = 0;
1217 if (gst_structure_get_clock_time (caps_struct, "npt-stop", &tval))
1218 priv->npt_stop = tval;
1220 priv->npt_stop = -1;
1222 GST_DEBUG_OBJECT (jitterbuffer,
1223 "npt start/stop: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1224 GST_TIME_ARGS (priv->npt_start), GST_TIME_ARGS (priv->npt_stop));
1231 GST_DEBUG_OBJECT (jitterbuffer, "No clock-rate in caps!");
1236 GST_DEBUG_OBJECT (jitterbuffer, "Invalid clock-rate %d", priv->clock_rate);
1242 gst_rtp_jitter_buffer_flush_start (GstRtpJitterBuffer * jitterbuffer)
1244 GstRtpJitterBufferPrivate *priv;
1246 priv = jitterbuffer->priv;
1249 /* mark ourselves as flushing */
1250 priv->srcresult = GST_FLOW_FLUSHING;
1251 GST_DEBUG_OBJECT (jitterbuffer, "Disabling pop on queue");
1252 /* this unblocks any waiting pops on the src pad task */
1253 JBUF_SIGNAL_EVENT (priv);
1254 JBUF_SIGNAL_QUERY (priv, FALSE);
1259 gst_rtp_jitter_buffer_flush_stop (GstRtpJitterBuffer * jitterbuffer)
1261 GstRtpJitterBufferPrivate *priv;
1263 priv = jitterbuffer->priv;
1266 GST_DEBUG_OBJECT (jitterbuffer, "Enabling pop on queue");
1267 /* Mark as non flushing */
1268 priv->srcresult = GST_FLOW_OK;
1269 gst_segment_init (&priv->segment, GST_FORMAT_TIME);
1270 priv->last_popped_seqnum = -1;
1271 priv->last_out_time = -1;
1272 priv->next_seqnum = -1;
1273 priv->seqnum_base = -1;
1274 priv->ips_rtptime = -1;
1275 priv->ips_dts = GST_CLOCK_TIME_NONE;
1276 priv->packet_spacing = 0;
1277 priv->next_in_seqnum = -1;
1278 priv->clock_rate = -1;
1281 priv->estimated_eos = -1;
1282 priv->last_elapsed = 0;
1283 priv->ext_timestamp = -1;
1284 priv->avg_jitter = 0;
1285 priv->last_dts = -1;
1286 priv->last_rtptime = -1;
1287 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
1288 rtp_jitter_buffer_flush (priv->jbuf, (GFunc) free_item, NULL);
1289 rtp_jitter_buffer_disable_buffering (priv->jbuf, FALSE);
1290 rtp_jitter_buffer_reset_skew (priv->jbuf);
1291 remove_all_timers (jitterbuffer);
1296 gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad, GstObject * parent,
1297 GstPadMode mode, gboolean active)
1300 GstRtpJitterBuffer *jitterbuffer = NULL;
1302 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1305 case GST_PAD_MODE_PUSH:
1307 /* allow data processing */
1308 gst_rtp_jitter_buffer_flush_stop (jitterbuffer);
1310 /* start pushing out buffers */
1311 GST_DEBUG_OBJECT (jitterbuffer, "Starting task on srcpad");
1312 result = gst_pad_start_task (jitterbuffer->priv->srcpad,
1313 (GstTaskFunction) gst_rtp_jitter_buffer_loop, jitterbuffer, NULL);
1315 /* make sure all data processing stops ASAP */
1316 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
1318 /* NOTE this will hardlock if the state change is called from the src pad
1319 * task thread because we will _join() the thread. */
1320 GST_DEBUG_OBJECT (jitterbuffer, "Stopping task on srcpad");
1321 result = gst_pad_stop_task (pad);
1331 static GstStateChangeReturn
1332 gst_rtp_jitter_buffer_change_state (GstElement * element,
1333 GstStateChange transition)
1335 GstRtpJitterBuffer *jitterbuffer;
1336 GstRtpJitterBufferPrivate *priv;
1337 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1339 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
1340 priv = jitterbuffer->priv;
1342 switch (transition) {
1343 case GST_STATE_CHANGE_NULL_TO_READY:
1345 case GST_STATE_CHANGE_READY_TO_PAUSED:
1347 /* reset negotiated values */
1348 priv->clock_rate = -1;
1349 priv->clock_base = -1;
1350 priv->peer_latency = 0;
1352 /* block until we go to PLAYING */
1353 priv->blocked = TRUE;
1354 priv->timer_running = TRUE;
1355 priv->timer_thread =
1356 g_thread_new ("timer", (GThreadFunc) wait_next_timeout, jitterbuffer);
1359 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1361 /* unblock to allow streaming in PLAYING */
1362 priv->blocked = FALSE;
1363 JBUF_SIGNAL_EVENT (priv);
1364 JBUF_SIGNAL_TIMER (priv);
1371 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1373 switch (transition) {
1374 case GST_STATE_CHANGE_READY_TO_PAUSED:
1375 /* we are a live element because we sync to the clock, which we can only
1376 * do in the PLAYING state */
1377 if (ret != GST_STATE_CHANGE_FAILURE)
1378 ret = GST_STATE_CHANGE_NO_PREROLL;
1380 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1382 /* block to stop streaming when PAUSED */
1383 priv->blocked = TRUE;
1384 unschedule_current_timer (jitterbuffer);
1386 if (ret != GST_STATE_CHANGE_FAILURE)
1387 ret = GST_STATE_CHANGE_NO_PREROLL;
1389 case GST_STATE_CHANGE_PAUSED_TO_READY:
1391 gst_buffer_replace (&priv->last_sr, NULL);
1392 priv->timer_running = FALSE;
1393 unschedule_current_timer (jitterbuffer);
1394 JBUF_SIGNAL_TIMER (priv);
1395 JBUF_SIGNAL_QUERY (priv, FALSE);
1397 g_thread_join (priv->timer_thread);
1398 priv->timer_thread = NULL;
1400 case GST_STATE_CHANGE_READY_TO_NULL:
1410 gst_rtp_jitter_buffer_src_event (GstPad * pad, GstObject * parent,
1413 gboolean ret = TRUE;
1414 GstRtpJitterBuffer *jitterbuffer;
1415 GstRtpJitterBufferPrivate *priv;
1417 jitterbuffer = GST_RTP_JITTER_BUFFER_CAST (parent);
1418 priv = jitterbuffer->priv;
1420 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1422 switch (GST_EVENT_TYPE (event)) {
1423 case GST_EVENT_LATENCY:
1425 GstClockTime latency;
1427 gst_event_parse_latency (event, &latency);
1429 GST_DEBUG_OBJECT (jitterbuffer,
1430 "configuring latency of %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1433 /* adjust the overall buffer delay to the total pipeline latency in
1434 * buffering mode because if downstream consumes too fast (because of
1435 * large latency or queues, we would start rebuffering again. */
1436 if (rtp_jitter_buffer_get_mode (priv->jbuf) ==
1437 RTP_JITTER_BUFFER_MODE_BUFFER) {
1438 rtp_jitter_buffer_set_delay (priv->jbuf, latency);
1442 ret = gst_pad_push_event (priv->sinkpad, event);
1446 ret = gst_pad_push_event (priv->sinkpad, event);
1453 /* handles and stores the event in the jitterbuffer, must be called with
1456 queue_event (GstRtpJitterBuffer * jitterbuffer, GstEvent * event)
1458 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1459 RTPJitterBufferItem *item;
1462 switch (GST_EVENT_TYPE (event)) {
1463 case GST_EVENT_CAPS:
1467 gst_event_parse_caps (event, &caps);
1468 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1471 case GST_EVENT_SEGMENT:
1472 gst_event_copy_segment (event, &priv->segment);
1474 /* we need time for now */
1475 if (priv->segment.format != GST_FORMAT_TIME)
1476 goto newseg_wrong_format;
1478 GST_DEBUG_OBJECT (jitterbuffer,
1479 "segment: %" GST_SEGMENT_FORMAT, &priv->segment);
1483 rtp_jitter_buffer_disable_buffering (priv->jbuf, TRUE);
1490 GST_DEBUG_OBJECT (jitterbuffer, "adding event");
1491 item = alloc_item (event, ITEM_TYPE_EVENT, -1, -1, -1, 0, -1);
1492 rtp_jitter_buffer_insert (priv->jbuf, item, &head, NULL);
1494 JBUF_SIGNAL_EVENT (priv);
1499 newseg_wrong_format:
1501 GST_DEBUG_OBJECT (jitterbuffer, "received non TIME newsegment");
1502 gst_event_unref (event);
1508 gst_rtp_jitter_buffer_sink_event (GstPad * pad, GstObject * parent,
1511 gboolean ret = TRUE;
1512 GstRtpJitterBuffer *jitterbuffer;
1513 GstRtpJitterBufferPrivate *priv;
1515 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1516 priv = jitterbuffer->priv;
1518 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1520 switch (GST_EVENT_TYPE (event)) {
1521 case GST_EVENT_FLUSH_START:
1522 ret = gst_pad_push_event (priv->srcpad, event);
1523 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
1524 /* wait for the loop to go into PAUSED */
1525 gst_pad_pause_task (priv->srcpad);
1527 case GST_EVENT_FLUSH_STOP:
1528 ret = gst_pad_push_event (priv->srcpad, event);
1530 gst_rtp_jitter_buffer_src_activate_mode (priv->srcpad, parent,
1531 GST_PAD_MODE_PUSH, TRUE);
1534 if (GST_EVENT_IS_SERIALIZED (event)) {
1535 /* serialized events go in the queue */
1537 if (priv->srcresult != GST_FLOW_OK) {
1538 /* Errors in sticky event pushing are no problem and ignored here
1539 * as they will cause more meaningful errors during data flow.
1540 * For EOS events, that are not followed by data flow, we still
1541 * return FALSE here though.
1543 if (!GST_EVENT_IS_STICKY (event) ||
1544 GST_EVENT_TYPE (event) == GST_EVENT_EOS)
1545 goto out_flow_error;
1547 /* refuse more events on EOS */
1550 ret = queue_event (jitterbuffer, event);
1553 /* non-serialized events are forwarded downstream immediately */
1554 ret = gst_pad_push_event (priv->srcpad, event);
1563 GST_DEBUG_OBJECT (jitterbuffer,
1564 "refusing event, we have a downstream flow error: %s",
1565 gst_flow_get_name (priv->srcresult));
1567 gst_event_unref (event);
1572 GST_DEBUG_OBJECT (jitterbuffer, "refusing event, we are EOS");
1574 gst_event_unref (event);
1580 gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad, GstObject * parent,
1583 gboolean ret = TRUE;
1584 GstRtpJitterBuffer *jitterbuffer;
1586 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1588 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1590 switch (GST_EVENT_TYPE (event)) {
1591 case GST_EVENT_FLUSH_START:
1592 gst_event_unref (event);
1594 case GST_EVENT_FLUSH_STOP:
1595 gst_event_unref (event);
1598 ret = gst_pad_event_default (pad, parent, event);
1606 * Must be called with JBUF_LOCK held, will release the LOCK when emiting the
1607 * signal. The function returns GST_FLOW_ERROR when a parsing error happened and
1608 * GST_FLOW_FLUSHING when the element is shutting down. On success
1609 * GST_FLOW_OK is returned.
1611 static GstFlowReturn
1612 gst_rtp_jitter_buffer_get_clock_rate (GstRtpJitterBuffer * jitterbuffer,
1616 GValue args[2] = { {0}, {0} };
1620 g_value_init (&args[0], GST_TYPE_ELEMENT);
1621 g_value_set_object (&args[0], jitterbuffer);
1622 g_value_init (&args[1], G_TYPE_UINT);
1623 g_value_set_uint (&args[1], pt);
1625 g_value_init (&ret, GST_TYPE_CAPS);
1626 g_value_set_boxed (&ret, NULL);
1628 JBUF_UNLOCK (jitterbuffer->priv);
1629 g_signal_emitv (args, gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP], 0,
1631 JBUF_LOCK_CHECK (jitterbuffer->priv, out_flushing);
1633 g_value_unset (&args[0]);
1634 g_value_unset (&args[1]);
1635 caps = (GstCaps *) g_value_dup_boxed (&ret);
1636 g_value_unset (&ret);
1640 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1641 gst_caps_unref (caps);
1643 if (G_UNLIKELY (!res))
1651 GST_DEBUG_OBJECT (jitterbuffer, "could not get caps");
1652 return GST_FLOW_ERROR;
1656 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1657 return GST_FLOW_FLUSHING;
1661 GST_DEBUG_OBJECT (jitterbuffer, "parse failed");
1662 return GST_FLOW_ERROR;
1666 /* call with jbuf lock held */
1668 check_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint percent)
1670 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1671 GstMessage *message = NULL;
1676 /* Post a buffering message */
1677 if (priv->last_percent != percent) {
1678 priv->last_percent = percent;
1680 gst_message_new_buffering (GST_OBJECT_CAST (jitterbuffer), percent);
1681 gst_message_set_buffering_stats (message, GST_BUFFERING_LIVE, -1, -1, -1);
1688 apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
1690 GstRtpJitterBufferPrivate *priv;
1692 priv = jitterbuffer->priv;
1694 if (timestamp == -1)
1697 /* apply the timestamp offset, this is used for inter stream sync */
1698 timestamp += priv->ts_offset;
1699 /* add the offset, this is used when buffering */
1700 timestamp += priv->out_offset;
1706 find_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type, guint16 seqnum)
1708 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1709 TimerData *timer = NULL;
1712 len = priv->timers->len;
1713 for (i = 0; i < len; i++) {
1714 TimerData *test = &g_array_index (priv->timers, TimerData, i);
1715 if (test->seqnum == seqnum && test->type == type) {
1724 unschedule_current_timer (GstRtpJitterBuffer * jitterbuffer)
1726 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1728 if (priv->clock_id) {
1729 GST_DEBUG_OBJECT (jitterbuffer, "unschedule current timer");
1730 gst_clock_id_unschedule (priv->clock_id);
1731 priv->clock_id = NULL;
1736 get_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1738 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1739 GstClockTime test_timeout;
1741 if ((test_timeout = timer->timeout) == -1)
1744 if (timer->type != TIMER_TYPE_EXPECTED) {
1745 /* add our latency and offset to get output times. */
1746 test_timeout = apply_offset (jitterbuffer, test_timeout);
1747 test_timeout += priv->latency_ns;
1749 return test_timeout;
1753 recalculate_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1755 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1757 if (priv->clock_id) {
1758 GstClockTime timeout = get_timeout (jitterbuffer, timer);
1760 GST_DEBUG ("%" GST_TIME_FORMAT " <> %" GST_TIME_FORMAT,
1761 GST_TIME_ARGS (timeout), GST_TIME_ARGS (priv->timer_timeout));
1763 if (timeout == -1 || timeout < priv->timer_timeout)
1764 unschedule_current_timer (jitterbuffer);
1769 add_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
1770 guint16 seqnum, guint num, GstClockTime timeout, GstClockTime delay,
1771 GstClockTime duration)
1773 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1777 GST_DEBUG_OBJECT (jitterbuffer,
1778 "add timer %d for seqnum %d to %" GST_TIME_FORMAT ", delay %"
1779 GST_TIME_FORMAT, type, seqnum, GST_TIME_ARGS (timeout),
1780 GST_TIME_ARGS (delay));
1782 len = priv->timers->len;
1783 g_array_set_size (priv->timers, len + 1);
1784 timer = &g_array_index (priv->timers, TimerData, len);
1787 timer->seqnum = seqnum;
1789 timer->timeout = timeout + delay;
1790 timer->duration = duration;
1791 if (type == TIMER_TYPE_EXPECTED) {
1792 timer->rtx_base = timeout;
1793 timer->rtx_delay = delay;
1794 timer->rtx_retry = 0;
1796 timer->num_rtx_retry = 0;
1797 recalculate_timer (jitterbuffer, timer);
1798 JBUF_SIGNAL_TIMER (priv);
1804 reschedule_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
1805 guint16 seqnum, GstClockTime timeout, GstClockTime delay, gboolean reset)
1807 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1808 gboolean seqchange, timechange;
1811 seqchange = timer->seqnum != seqnum;
1812 timechange = timer->timeout != timeout;
1814 if (!seqchange && !timechange)
1817 oldseq = timer->seqnum;
1819 GST_DEBUG_OBJECT (jitterbuffer,
1820 "replace timer for seqnum %d->%d to %" GST_TIME_FORMAT,
1821 oldseq, seqnum, GST_TIME_ARGS (timeout + delay));
1823 timer->timeout = timeout + delay;
1824 timer->seqnum = seqnum;
1826 timer->rtx_base = timeout;
1827 timer->rtx_delay = delay;
1828 timer->rtx_retry = 0;
1831 timer->num_rtx_retry = 0;
1833 if (priv->clock_id) {
1834 /* we changed the seqnum and there is a timer currently waiting with this
1835 * seqnum, unschedule it */
1836 if (seqchange && priv->timer_seqnum == oldseq)
1837 unschedule_current_timer (jitterbuffer);
1838 /* we changed the time, check if it is earlier than what we are waiting
1839 * for and unschedule if so */
1840 else if (timechange)
1841 recalculate_timer (jitterbuffer, timer);
1846 set_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
1847 guint16 seqnum, GstClockTime timeout)
1851 /* find the seqnum timer */
1852 timer = find_timer (jitterbuffer, type, seqnum);
1853 if (timer == NULL) {
1854 timer = add_timer (jitterbuffer, type, seqnum, 0, timeout, 0, -1);
1856 reschedule_timer (jitterbuffer, timer, seqnum, timeout, 0, FALSE);
1862 remove_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1864 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1867 if (priv->clock_id && priv->timer_seqnum == timer->seqnum)
1868 unschedule_current_timer (jitterbuffer);
1871 GST_DEBUG_OBJECT (jitterbuffer, "removed index %d", idx);
1872 g_array_remove_index_fast (priv->timers, idx);
1877 remove_all_timers (GstRtpJitterBuffer * jitterbuffer)
1879 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1880 GST_DEBUG_OBJECT (jitterbuffer, "removed all timers");
1881 g_array_set_size (priv->timers, 0);
1882 unschedule_current_timer (jitterbuffer);
1885 /* get the extra delay to wait before sending RTX */
1887 get_rtx_delay (GstRtpJitterBufferPrivate * priv)
1891 if (priv->rtx_delay == -1) {
1892 if (priv->avg_jitter == 0)
1893 delay = DEFAULT_AUTO_RTX_DELAY;
1895 /* jitter is in nanoseconds, 2x jitter is a good margin */
1896 delay = priv->avg_jitter * 2;
1898 delay = priv->rtx_delay * GST_MSECOND;
1900 if (priv->rtx_min_delay > 0)
1901 delay = MAX (delay, priv->rtx_min_delay * GST_MSECOND);
1906 /* we just received a packet with seqnum and dts.
1908 * First check for old seqnum that we are still expecting. If the gap with the
1909 * current seqnum is too big, unschedule the timeouts.
1911 * If we have a valid packet spacing estimate we can set a timer for when we
1912 * should receive the next packet.
1913 * If we don't have a valid estimate, we remove any timer we might have
1914 * had for this packet.
1917 update_timers (GstRtpJitterBuffer * jitterbuffer, guint16 seqnum,
1918 GstClockTime dts, gboolean do_next_seqnum)
1920 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1921 TimerData *timer = NULL;
1924 /* go through all timers and unschedule the ones with a large gap, also find
1925 * the timer for the seqnum */
1926 len = priv->timers->len;
1927 for (i = 0; i < len; i++) {
1928 TimerData *test = &g_array_index (priv->timers, TimerData, i);
1931 gap = gst_rtp_buffer_compare_seqnum (test->seqnum, seqnum);
1933 GST_DEBUG_OBJECT (jitterbuffer, "%d, %d, #%d<->#%d gap %d", i,
1934 test->type, test->seqnum, seqnum, gap);
1937 GST_DEBUG ("found timer for current seqnum");
1938 /* the timer for the current seqnum */
1940 /* when no retransmission, we can stop now, we only need to find the
1941 * timer for the current seqnum */
1942 if (!priv->do_retransmission)
1944 } else if (gap > priv->rtx_delay_reorder) {
1945 /* max gap, we exceeded the max reorder distance and we don't expect the
1946 * missing packet to be this reordered */
1947 if (test->num_rtx_retry == 0 && test->type == TIMER_TYPE_EXPECTED)
1948 reschedule_timer (jitterbuffer, test, test->seqnum, -1, 0, FALSE);
1952 do_next_seqnum = do_next_seqnum && priv->packet_spacing > 0
1953 && priv->do_retransmission && priv->rtx_next_seqnum;
1955 if (timer && timer->type != TIMER_TYPE_DEADLINE) {
1956 if (timer->num_rtx_retry > 0) {
1957 GstClockTime rtx_last, delay;
1959 /* we scheduled a retry for this packet and now we have it */
1960 priv->num_rtx_success++;
1961 /* all the previous retry attempts failed */
1962 priv->num_rtx_failed += timer->num_rtx_retry - 1;
1963 /* number of retries before receiving the packet */
1964 if (priv->avg_rtx_num == 0.0)
1965 priv->avg_rtx_num = timer->num_rtx_retry;
1967 priv->avg_rtx_num = (timer->num_rtx_retry + 7 * priv->avg_rtx_num) / 8;
1968 /* calculate the delay between retransmission request and receiving this
1969 * packet, start with when we scheduled this timeout last */
1970 rtx_last = timer->rtx_last;
1971 if (dts != GST_CLOCK_TIME_NONE && dts > rtx_last) {
1972 /* we have a valid delay if this packet arrived after we scheduled the
1974 delay = dts - rtx_last;
1975 if (priv->avg_rtx_rtt == 0)
1976 priv->avg_rtx_rtt = delay;
1978 priv->avg_rtx_rtt = (delay + 7 * priv->avg_rtx_rtt) / 8;
1982 GST_LOG_OBJECT (jitterbuffer,
1983 "RTX success %" G_GUINT64_FORMAT ", failed %" G_GUINT64_FORMAT
1984 ", requests %" G_GUINT64_FORMAT ", dups %" G_GUINT64_FORMAT
1985 ", avg-num %g, delay %" GST_TIME_FORMAT ", avg-rtt %" GST_TIME_FORMAT,
1986 priv->num_rtx_success, priv->num_rtx_failed, priv->num_rtx_requests,
1987 priv->num_duplicates, priv->avg_rtx_num, GST_TIME_ARGS (delay),
1988 GST_TIME_ARGS (priv->avg_rtx_rtt));
1990 /* don't try to estimate the next seqnum because this is a retransmitted
1991 * packet and it probably did not arrive with the expected packet
1993 do_next_seqnum = FALSE;
1997 if (do_next_seqnum) {
1998 GstClockTime expected, delay;
2000 /* calculate expected arrival time of the next seqnum */
2001 expected = dts + priv->packet_spacing;
2003 delay = get_rtx_delay (priv);
2005 /* and update/install timer for next seqnum */
2007 reschedule_timer (jitterbuffer, timer, priv->next_in_seqnum, expected,
2010 add_timer (jitterbuffer, TIMER_TYPE_EXPECTED, priv->next_in_seqnum, 0,
2011 expected, delay, priv->packet_spacing);
2012 } else if (timer && timer->type != TIMER_TYPE_DEADLINE) {
2013 /* if we had a timer, remove it, we don't know when to expect the next
2015 remove_timer (jitterbuffer, timer);
2020 calculate_packet_spacing (GstRtpJitterBuffer * jitterbuffer, guint32 rtptime,
2023 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2025 /* we need consecutive seqnums with a different
2026 * rtptime to estimate the packet spacing. */
2027 if (priv->ips_rtptime != rtptime) {
2028 /* rtptime changed, check dts diff */
2029 if (priv->ips_dts != -1 && dts != -1 && dts > priv->ips_dts) {
2030 GstClockTime new_packet_spacing = dts - priv->ips_dts;
2031 GstClockTime old_packet_spacing = priv->packet_spacing;
2033 /* Biased towards bigger packet spacings to prevent
2034 * too many unneeded retransmission requests for next
2035 * packets that just arrive a little later than we would
2037 if (old_packet_spacing > new_packet_spacing)
2038 priv->packet_spacing =
2039 (new_packet_spacing + 3 * old_packet_spacing) / 4;
2040 else if (old_packet_spacing > 0)
2041 priv->packet_spacing =
2042 (3 * new_packet_spacing + old_packet_spacing) / 4;
2044 priv->packet_spacing = new_packet_spacing;
2046 GST_DEBUG_OBJECT (jitterbuffer,
2047 "new packet spacing %" GST_TIME_FORMAT
2048 " old packet spacing %" GST_TIME_FORMAT
2049 " combined to %" GST_TIME_FORMAT,
2050 GST_TIME_ARGS (new_packet_spacing),
2051 GST_TIME_ARGS (old_packet_spacing),
2052 GST_TIME_ARGS (priv->packet_spacing));
2054 priv->ips_rtptime = rtptime;
2055 priv->ips_dts = dts;
2060 calculate_expected (GstRtpJitterBuffer * jitterbuffer, guint32 expected,
2061 guint16 seqnum, GstClockTime dts, gint gap)
2063 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2064 GstClockTime total_duration, duration, expected_dts;
2066 guint lost_packets = 0;
2068 GST_DEBUG_OBJECT (jitterbuffer,
2069 "dts %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT,
2070 GST_TIME_ARGS (dts), GST_TIME_ARGS (priv->last_in_dts));
2072 /* the total duration spanned by the missing packets */
2073 if (dts >= priv->last_in_dts)
2074 total_duration = dts - priv->last_in_dts;
2078 /* interpolate between the current time and the last time based on
2079 * number of packets we are missing, this is the estimated duration
2080 * for the missing packet based on equidistant packet spacing. */
2081 duration = total_duration / (gap + 1);
2083 GST_DEBUG_OBJECT (jitterbuffer, "duration %" GST_TIME_FORMAT,
2084 GST_TIME_ARGS (duration));
2086 if (total_duration > priv->latency_ns) {
2087 GstClockTime gap_time;
2089 gap_time = total_duration - priv->latency_ns;
2092 lost_packets = gap_time / duration;
2093 gap_time = lost_packets * duration;
2098 /* too many lost packets, some of the missing packets are already
2099 * too late and we can generate lost packet events for them. */
2100 GST_DEBUG_OBJECT (jitterbuffer, "too many lost packets %" GST_TIME_FORMAT
2101 " > %" GST_TIME_FORMAT ", consider %u lost",
2102 GST_TIME_ARGS (total_duration), GST_TIME_ARGS (priv->latency_ns),
2105 /* this timer will fire immediately and the lost event will be pushed from
2106 * the timer thread */
2107 add_timer (jitterbuffer, TIMER_TYPE_LOST, expected, lost_packets,
2108 priv->last_in_dts + duration, 0, gap_time);
2110 expected += lost_packets;
2111 priv->last_in_dts += gap_time;
2114 expected_dts = priv->last_in_dts + (lost_packets + 1) * duration;
2116 if (priv->do_retransmission) {
2119 type = TIMER_TYPE_EXPECTED;
2120 /* if we had a timer for the first missing packet, update it. */
2121 if ((timer = find_timer (jitterbuffer, type, expected))) {
2122 GstClockTime timeout = timer->timeout;
2124 timer->duration = duration;
2125 if (timeout > (expected_dts + timer->rtx_retry)) {
2126 GstClockTime delay = timeout - expected_dts - timer->rtx_retry;
2127 reschedule_timer (jitterbuffer, timer, timer->seqnum, expected_dts,
2131 expected_dts += duration;
2134 type = TIMER_TYPE_LOST;
2137 while (gst_rtp_buffer_compare_seqnum (expected, seqnum) > 0) {
2138 add_timer (jitterbuffer, type, expected, 0, expected_dts, 0, duration);
2139 expected_dts += duration;
2145 calculate_jitter (GstRtpJitterBuffer * jitterbuffer, GstClockTime dts,
2149 GstClockTimeDiff dtsdiff, rtpdiffns, diff;
2150 GstRtpJitterBufferPrivate *priv;
2152 priv = jitterbuffer->priv;
2154 if (G_UNLIKELY (dts == GST_CLOCK_TIME_NONE) || priv->clock_rate <= 0)
2157 if (priv->last_dts != -1)
2158 dtsdiff = dts - priv->last_dts;
2162 if (priv->last_rtptime != -1)
2163 rtpdiff = rtptime - (guint32) priv->last_rtptime;
2167 priv->last_dts = dts;
2168 priv->last_rtptime = rtptime;
2172 gst_util_uint64_scale_int (rtpdiff, GST_SECOND, priv->clock_rate);
2175 -gst_util_uint64_scale_int (-rtpdiff, GST_SECOND, priv->clock_rate);
2177 diff = ABS (dtsdiff - rtpdiffns);
2179 /* jitter is stored in nanoseconds */
2180 priv->avg_jitter = (diff + (15 * priv->avg_jitter)) >> 4;
2182 GST_LOG_OBJECT (jitterbuffer,
2183 "dtsdiff %" GST_TIME_FORMAT " rtptime %" GST_TIME_FORMAT
2184 ", clock-rate %d, diff %" GST_TIME_FORMAT ", jitter: %" GST_TIME_FORMAT,
2185 GST_TIME_ARGS (dtsdiff), GST_TIME_ARGS (rtpdiffns), priv->clock_rate,
2186 GST_TIME_ARGS (diff), GST_TIME_ARGS (priv->avg_jitter));
2193 GST_DEBUG_OBJECT (jitterbuffer,
2194 "no dts or no clock-rate, can't calculate jitter");
2199 static GstFlowReturn
2200 gst_rtp_jitter_buffer_chain (GstPad * pad, GstObject * parent,
2203 GstRtpJitterBuffer *jitterbuffer;
2204 GstRtpJitterBufferPrivate *priv;
2206 guint32 expected, rtptime;
2207 GstFlowReturn ret = GST_FLOW_OK;
2208 GstClockTime dts, pts;
2213 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
2214 gboolean do_next_seqnum = FALSE;
2215 RTPJitterBufferItem *item;
2216 GstMessage *msg = NULL;
2218 jitterbuffer = GST_RTP_JITTER_BUFFER_CAST (parent);
2220 priv = jitterbuffer->priv;
2222 if (G_UNLIKELY (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)))
2223 goto invalid_buffer;
2225 pt = gst_rtp_buffer_get_payload_type (&rtp);
2226 seqnum = gst_rtp_buffer_get_seq (&rtp);
2227 rtptime = gst_rtp_buffer_get_timestamp (&rtp);
2228 gst_rtp_buffer_unmap (&rtp);
2230 /* make sure we have PTS and DTS set */
2231 pts = GST_BUFFER_PTS (buffer);
2232 dts = GST_BUFFER_DTS (buffer);
2238 /* take the DTS of the buffer. This is the time when the packet was
2239 * received and is used to calculate jitter and clock skew. We will adjust
2240 * this DTS with the smoothed value after processing it in the
2241 * jitterbuffer and assign it as the PTS. */
2242 /* bring to running time */
2243 dts = gst_segment_to_running_time (&priv->segment, GST_FORMAT_TIME, dts);
2245 GST_DEBUG_OBJECT (jitterbuffer,
2246 "Received packet #%d at time %" GST_TIME_FORMAT ", discont %d", seqnum,
2247 GST_TIME_ARGS (dts), GST_BUFFER_IS_DISCONT (buffer));
2249 JBUF_LOCK_CHECK (priv, out_flushing);
2251 if (G_UNLIKELY (priv->last_pt != pt)) {
2254 GST_DEBUG_OBJECT (jitterbuffer, "pt changed from %u to %u", priv->last_pt,
2258 /* reset clock-rate so that we get a new one */
2259 priv->clock_rate = -1;
2261 /* Try to get the clock-rate from the caps first if we can. If there are no
2262 * caps we must fire the signal to get the clock-rate. */
2263 if ((caps = gst_pad_get_current_caps (pad))) {
2264 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
2265 gst_caps_unref (caps);
2269 if (G_UNLIKELY (priv->clock_rate == -1)) {
2270 /* no clock rate given on the caps, try to get one with the signal */
2271 if (gst_rtp_jitter_buffer_get_clock_rate (jitterbuffer,
2272 pt) == GST_FLOW_FLUSHING)
2275 if (G_UNLIKELY (priv->clock_rate == -1))
2279 /* don't accept more data on EOS */
2280 if (G_UNLIKELY (priv->eos))
2283 calculate_jitter (jitterbuffer, dts, rtptime);
2285 if (priv->seqnum_base != -1) {
2288 gap = gst_rtp_buffer_compare_seqnum (priv->seqnum_base, seqnum);
2291 GST_DEBUG_OBJECT (jitterbuffer,
2292 "packet seqnum #%d before seqnum-base #%d", seqnum,
2294 gst_buffer_unref (buffer);
2297 } else if (gap > 16384) {
2298 /* From now on don't compare against the seqnum base anymore as
2299 * at some point in the future we will wrap around and also that
2300 * much reordering is very unlikely */
2301 priv->seqnum_base = -1;
2305 expected = priv->next_in_seqnum;
2307 /* now check against our expected seqnum */
2308 if (G_LIKELY (expected != -1)) {
2311 /* now calculate gap */
2312 gap = gst_rtp_buffer_compare_seqnum (expected, seqnum);
2314 GST_DEBUG_OBJECT (jitterbuffer, "expected #%d, got #%d, gap of %d",
2315 expected, seqnum, gap);
2317 if (G_LIKELY (gap == 0)) {
2318 /* packet is expected */
2319 calculate_packet_spacing (jitterbuffer, rtptime, dts);
2320 do_next_seqnum = TRUE;
2322 gboolean reset = FALSE;
2324 if (!GST_CLOCK_TIME_IS_VALID (dts)) {
2325 /* We would run into calculations with GST_CLOCK_TIME_NONE below
2326 * and can't compensate for anything without DTS on RTP packets
2328 goto gap_but_no_dts;
2329 } else if (gap < 0) {
2330 /* we received an old packet */
2331 if (G_UNLIKELY (gap < -RTP_MAX_MISORDER)) {
2332 /* too old packet, reset */
2333 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too old %d < %d", gap,
2337 GST_DEBUG_OBJECT (jitterbuffer, "old packet received");
2340 /* new packet, we are missing some packets */
2341 if (G_UNLIKELY (gap > RTP_MAX_DROPOUT)) {
2342 /* packet too far in future, reset */
2343 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too new %d > %d", gap,
2347 GST_DEBUG_OBJECT (jitterbuffer, "%d missing packets", gap);
2348 /* fill in the gap with EXPECTED timers */
2349 calculate_expected (jitterbuffer, expected, seqnum, dts, gap);
2351 do_next_seqnum = TRUE;
2354 if (G_UNLIKELY (reset)) {
2355 GList *events = NULL, *l;
2357 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
2358 rtp_jitter_buffer_flush (priv->jbuf,
2359 (GFunc) free_item_and_retain_events, &events);
2360 rtp_jitter_buffer_reset_skew (priv->jbuf);
2361 remove_all_timers (jitterbuffer);
2362 priv->discont = TRUE;
2363 priv->last_popped_seqnum = -1;
2364 priv->next_seqnum = seqnum;
2365 do_next_seqnum = TRUE;
2367 /* Insert all sticky events again in order, otherwise we would
2368 * potentially loose STREAM_START, CAPS or SEGMENT events
2370 events = g_list_reverse (events);
2371 for (l = events; l; l = l->next) {
2372 RTPJitterBufferItem *item;
2374 item = alloc_item (l->data, ITEM_TYPE_EVENT, -1, -1, -1, 0, -1);
2375 rtp_jitter_buffer_insert (priv->jbuf, item, &head, NULL);
2377 g_list_free (events);
2379 JBUF_SIGNAL_EVENT (priv);
2381 /* reset spacing estimation when gap */
2382 priv->ips_rtptime = -1;
2383 priv->ips_dts = GST_CLOCK_TIME_NONE;
2386 GST_DEBUG_OBJECT (jitterbuffer, "First buffer #%d", seqnum);
2387 /* we don't know what the next_in_seqnum should be, wait for the last
2388 * possible moment to push this buffer, maybe we get an earlier seqnum
2390 set_timer (jitterbuffer, TIMER_TYPE_DEADLINE, seqnum, dts);
2391 do_next_seqnum = TRUE;
2392 /* take rtptime and dts to calculate packet spacing */
2393 priv->ips_rtptime = rtptime;
2394 priv->ips_dts = dts;
2396 if (do_next_seqnum) {
2397 priv->last_in_seqnum = seqnum;
2398 priv->last_in_dts = dts;
2399 priv->next_in_seqnum = (seqnum + 1) & 0xffff;
2402 /* let's check if this buffer is too late, we can only accept packets with
2403 * bigger seqnum than the one we last pushed. */
2404 if (G_LIKELY (priv->last_popped_seqnum != -1)) {
2407 gap = gst_rtp_buffer_compare_seqnum (priv->last_popped_seqnum, seqnum);
2409 /* priv->last_popped_seqnum >= seqnum, we're too late. */
2410 if (G_UNLIKELY (gap <= 0))
2414 /* let's drop oldest packet if the queue is already full and drop-on-latency
2415 * is set. We can only do this when there actually is a latency. When no
2416 * latency is set, we just pump it in the queue and let the other end push it
2417 * out as fast as possible. */
2418 if (priv->latency_ms && priv->drop_on_latency) {
2420 gst_util_uint64_scale_int (priv->latency_ms, priv->clock_rate, 1000);
2422 if (G_UNLIKELY (rtp_jitter_buffer_get_ts_diff (priv->jbuf) >= latency_ts)) {
2423 RTPJitterBufferItem *old_item;
2425 old_item = rtp_jitter_buffer_peek (priv->jbuf);
2427 if (IS_DROPABLE (old_item)) {
2428 old_item = rtp_jitter_buffer_pop (priv->jbuf, &percent);
2429 GST_DEBUG_OBJECT (jitterbuffer, "Queue full, dropping old packet %p",
2431 priv->next_seqnum = (old_item->seqnum + 1) & 0xffff;
2432 free_item (old_item);
2434 /* we might have removed some head buffers, signal the pushing thread to
2435 * see if it can push now */
2436 JBUF_SIGNAL_EVENT (priv);
2440 item = alloc_item (buffer, ITEM_TYPE_BUFFER, dts, pts, seqnum, 1, rtptime);
2442 /* now insert the packet into the queue in sorted order. This function returns
2443 * FALSE if a packet with the same seqnum was already in the queue, meaning we
2444 * have a duplicate. */
2445 if (G_UNLIKELY (!rtp_jitter_buffer_insert (priv->jbuf, item,
2450 update_timers (jitterbuffer, seqnum, dts, do_next_seqnum);
2452 /* we had an unhandled SR, handle it now */
2454 do_handle_sync (jitterbuffer);
2456 if (G_UNLIKELY (head)) {
2457 /* signal addition of new buffer when the _loop is waiting. */
2458 if (G_LIKELY (priv->active))
2459 JBUF_SIGNAL_EVENT (priv);
2461 /* let's unschedule and unblock any waiting buffers. We only want to do this
2462 * when the head buffer changed */
2463 if (G_UNLIKELY (priv->clock_id)) {
2464 GST_DEBUG_OBJECT (jitterbuffer, "Unscheduling waiting new buffer");
2465 unschedule_current_timer (jitterbuffer);
2469 GST_DEBUG_OBJECT (jitterbuffer,
2470 "Pushed packet #%d, now %d packets, head: %d, " "percent %d", seqnum,
2471 rtp_jitter_buffer_num_packets (priv->jbuf), head, percent);
2473 msg = check_buffering_percent (jitterbuffer, percent);
2479 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer), msg);
2486 /* this is not fatal but should be filtered earlier */
2487 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2488 ("Received invalid RTP payload, dropping"));
2489 gst_buffer_unref (buffer);
2494 GST_WARNING_OBJECT (jitterbuffer,
2495 "No clock-rate in caps!, dropping buffer");
2496 gst_buffer_unref (buffer);
2501 ret = priv->srcresult;
2502 GST_DEBUG_OBJECT (jitterbuffer, "flushing %s", gst_flow_get_name (ret));
2503 gst_buffer_unref (buffer);
2509 GST_WARNING_OBJECT (jitterbuffer, "we are EOS, refusing buffer");
2510 gst_buffer_unref (buffer);
2515 GST_WARNING_OBJECT (jitterbuffer, "Packet #%d too late as #%d was already"
2516 " popped, dropping", seqnum, priv->last_popped_seqnum);
2518 gst_buffer_unref (buffer);
2523 GST_WARNING_OBJECT (jitterbuffer, "Duplicate packet #%d detected, dropping",
2525 priv->num_duplicates++;
2531 /* this is fatal as we can't compensate for gaps without DTS */
2532 GST_ELEMENT_ERROR (jitterbuffer, STREAM, DECODE, (NULL),
2533 ("Received packet without DTS after a gap"));
2534 gst_buffer_unref (buffer);
2535 ret = GST_FLOW_ERROR;
2541 compute_elapsed (GstRtpJitterBuffer * jitterbuffer, RTPJitterBufferItem * item)
2543 guint64 ext_time, elapsed;
2545 GstRtpJitterBufferPrivate *priv;
2547 priv = jitterbuffer->priv;
2548 rtp_time = item->rtptime;
2550 GST_LOG_OBJECT (jitterbuffer, "rtp %" G_GUINT32_FORMAT ", ext %"
2551 G_GUINT64_FORMAT, rtp_time, priv->ext_timestamp);
2553 if (rtp_time < priv->ext_timestamp) {
2554 ext_time = priv->ext_timestamp;
2556 ext_time = gst_rtp_buffer_ext_timestamp (&priv->ext_timestamp, rtp_time);
2559 if (ext_time > priv->clock_base)
2560 elapsed = ext_time - priv->clock_base;
2564 elapsed = gst_util_uint64_scale_int (elapsed, GST_SECOND, priv->clock_rate);
2569 update_estimated_eos (GstRtpJitterBuffer * jitterbuffer,
2570 RTPJitterBufferItem * item)
2572 guint64 total, elapsed, left, estimated;
2573 GstClockTime out_time;
2574 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2576 if (priv->npt_stop == -1 || priv->ext_timestamp == -1
2577 || priv->clock_base == -1 || priv->clock_rate <= 0)
2580 /* compute the elapsed time */
2581 elapsed = compute_elapsed (jitterbuffer, item);
2583 /* do nothing if elapsed time doesn't increment */
2584 if (priv->last_elapsed && elapsed <= priv->last_elapsed)
2587 priv->last_elapsed = elapsed;
2589 /* this is the total time we need to play */
2590 total = priv->npt_stop - priv->npt_start;
2591 GST_LOG_OBJECT (jitterbuffer, "total %" GST_TIME_FORMAT,
2592 GST_TIME_ARGS (total));
2594 /* this is how much time there is left */
2595 if (total > elapsed)
2596 left = total - elapsed;
2600 /* if we have less time left that the size of the buffer, we will not
2601 * be able to keep it filled, disabled buffering then */
2602 if (left < rtp_jitter_buffer_get_delay (priv->jbuf)) {
2603 GST_DEBUG_OBJECT (jitterbuffer, "left %" GST_TIME_FORMAT
2604 ", disable buffering close to EOS", GST_TIME_ARGS (left));
2605 rtp_jitter_buffer_disable_buffering (priv->jbuf, TRUE);
2608 /* this is the current time as running-time */
2609 out_time = item->dts;
2612 estimated = gst_util_uint64_scale (out_time, total, elapsed);
2614 /* if there is almost nothing left,
2615 * we may never advance enough to end up in the above case */
2616 if (total < GST_SECOND)
2617 estimated = GST_SECOND;
2621 GST_LOG_OBJECT (jitterbuffer, "elapsed %" GST_TIME_FORMAT ", estimated %"
2622 GST_TIME_FORMAT, GST_TIME_ARGS (elapsed), GST_TIME_ARGS (estimated));
2624 if (estimated != -1 && priv->estimated_eos != estimated) {
2625 set_timer (jitterbuffer, TIMER_TYPE_EOS, -1, estimated);
2626 priv->estimated_eos = estimated;
2630 /* take a buffer from the queue and push it */
2631 static GstFlowReturn
2632 pop_and_push_next (GstRtpJitterBuffer * jitterbuffer, guint seqnum)
2634 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2635 GstFlowReturn result = GST_FLOW_OK;
2636 RTPJitterBufferItem *item;
2637 GstBuffer *outbuf = NULL;
2638 GstEvent *outevent = NULL;
2639 GstQuery *outquery = NULL;
2640 GstClockTime dts, pts;
2642 gboolean do_push = TRUE;
2646 /* when we get here we are ready to pop and push the buffer */
2647 item = rtp_jitter_buffer_pop (priv->jbuf, &percent);
2651 case ITEM_TYPE_BUFFER:
2653 /* we need to make writable to change the flags and timestamps */
2654 outbuf = gst_buffer_make_writable (item->data);
2656 if (G_UNLIKELY (priv->discont)) {
2657 /* set DISCONT flag when we missed a packet. We pushed the buffer writable
2658 * into the jitterbuffer so we can modify now. */
2659 GST_DEBUG_OBJECT (jitterbuffer, "mark output buffer discont");
2660 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2661 priv->discont = FALSE;
2663 if (G_UNLIKELY (priv->ts_discont)) {
2664 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
2665 priv->ts_discont = FALSE;
2669 gst_segment_to_position (&priv->segment, GST_FORMAT_TIME, item->dts);
2671 gst_segment_to_position (&priv->segment, GST_FORMAT_TIME, item->pts);
2673 /* apply timestamp with offset to buffer now */
2674 GST_BUFFER_DTS (outbuf) = apply_offset (jitterbuffer, dts);
2675 GST_BUFFER_PTS (outbuf) = apply_offset (jitterbuffer, pts);
2677 /* update the elapsed time when we need to check against the npt stop time. */
2678 update_estimated_eos (jitterbuffer, item);
2680 priv->last_out_time = GST_BUFFER_PTS (outbuf);
2682 case ITEM_TYPE_LOST:
2683 priv->discont = TRUE;
2687 case ITEM_TYPE_EVENT:
2688 outevent = item->data;
2690 case ITEM_TYPE_QUERY:
2691 outquery = item->data;
2695 /* now we are ready to push the buffer. Save the seqnum and release the lock
2696 * so the other end can push stuff in the queue again. */
2698 priv->last_popped_seqnum = seqnum;
2699 priv->next_seqnum = (seqnum + item->count) & 0xffff;
2701 msg = check_buffering_percent (jitterbuffer, percent);
2708 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer), msg);
2711 case ITEM_TYPE_BUFFER:
2713 GST_DEBUG_OBJECT (jitterbuffer,
2714 "Pushing buffer %d, dts %" GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT,
2715 seqnum, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)),
2716 GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
2717 result = gst_pad_push (priv->srcpad, outbuf);
2719 JBUF_LOCK_CHECK (priv, out_flushing);
2721 case ITEM_TYPE_LOST:
2722 case ITEM_TYPE_EVENT:
2723 GST_DEBUG_OBJECT (jitterbuffer, "%sPushing event %" GST_PTR_FORMAT
2724 ", seqnum %d", do_push ? "" : "NOT ", outevent, seqnum);
2727 gst_pad_push_event (priv->srcpad, outevent);
2729 gst_event_unref (outevent);
2731 result = GST_FLOW_OK;
2733 JBUF_LOCK_CHECK (priv, out_flushing);
2735 case ITEM_TYPE_QUERY:
2739 res = gst_pad_peer_query (priv->srcpad, outquery);
2741 JBUF_LOCK_CHECK (priv, out_flushing);
2742 result = GST_FLOW_OK;
2743 GST_LOG_OBJECT (jitterbuffer, "did query %p, return %d", outquery, res);
2744 JBUF_SIGNAL_QUERY (priv, res);
2753 return priv->srcresult;
2757 #define GST_FLOW_WAIT GST_FLOW_CUSTOM_SUCCESS
2759 /* Peek a buffer and compare the seqnum to the expected seqnum.
2760 * If all is fine, the buffer is pushed.
2761 * If something is wrong, we wait for some event
2763 static GstFlowReturn
2764 handle_next_buffer (GstRtpJitterBuffer * jitterbuffer)
2766 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2767 GstFlowReturn result = GST_FLOW_OK;
2768 RTPJitterBufferItem *item;
2770 guint32 next_seqnum;
2773 /* only push buffers when PLAYING and active and not buffering */
2774 if (priv->blocked || !priv->active ||
2775 rtp_jitter_buffer_is_buffering (priv->jbuf))
2776 return GST_FLOW_WAIT;
2779 /* peek a buffer, we're just looking at the sequence number.
2780 * If all is fine, we'll pop and push it. If the sequence number is wrong we
2781 * wait for a timeout or something to change.
2782 * The peeked buffer is valid for as long as we hold the jitterbuffer lock. */
2783 item = rtp_jitter_buffer_peek (priv->jbuf);
2787 /* get the seqnum and the next expected seqnum */
2788 seqnum = item->seqnum;
2792 next_seqnum = priv->next_seqnum;
2794 /* get the gap between this and the previous packet. If we don't know the
2795 * previous packet seqnum assume no gap. */
2796 if (G_UNLIKELY (next_seqnum == -1)) {
2797 GST_DEBUG_OBJECT (jitterbuffer, "First buffer #%d", seqnum);
2798 /* we don't know what the next_seqnum should be, the chain function should
2799 * have scheduled a DEADLINE timer that will increment next_seqnum when it
2800 * fires, so wait for that */
2801 result = GST_FLOW_WAIT;
2803 /* else calculate GAP */
2804 gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum);
2806 if (G_LIKELY (gap == 0)) {
2808 /* no missing packet, pop and push */
2809 result = pop_and_push_next (jitterbuffer, seqnum);
2810 } else if (G_UNLIKELY (gap < 0)) {
2811 RTPJitterBufferItem *item;
2812 /* if we have a packet that we already pushed or considered dropped, pop it
2813 * off and get the next packet */
2814 GST_DEBUG_OBJECT (jitterbuffer, "Old packet #%d, next #%d dropping",
2815 seqnum, next_seqnum);
2816 item = rtp_jitter_buffer_pop (priv->jbuf, NULL);
2820 /* the chain function has scheduled timers to request retransmission or
2821 * when to consider the packet lost, wait for that */
2822 GST_DEBUG_OBJECT (jitterbuffer,
2823 "Sequence number GAP detected: expected %d instead of %d (%d missing)",
2824 next_seqnum, seqnum, gap);
2825 result = GST_FLOW_WAIT;
2832 GST_DEBUG_OBJECT (jitterbuffer, "no buffer, going to wait");
2834 result = GST_FLOW_EOS;
2836 result = GST_FLOW_WAIT;
2842 get_rtx_retry_timeout (GstRtpJitterBufferPrivate * priv)
2844 GstClockTime rtx_retry_timeout;
2845 GstClockTime rtx_min_retry_timeout;
2847 if (priv->rtx_retry_timeout == -1) {
2848 if (priv->avg_rtx_rtt == 0)
2849 rtx_retry_timeout = DEFAULT_AUTO_RTX_TIMEOUT;
2851 /* we want to ask for a retransmission after we waited for a
2852 * complete RTT and the additional jitter */
2853 rtx_retry_timeout = priv->avg_rtx_rtt + priv->avg_jitter * 2;
2855 rtx_retry_timeout = priv->rtx_retry_timeout * GST_MSECOND;
2857 /* make sure we don't retry too often. On very low latency networks,
2858 * the RTT and jitter can be very low. */
2859 if (priv->rtx_min_retry_timeout == -1) {
2860 rtx_min_retry_timeout = priv->packet_spacing;
2862 rtx_min_retry_timeout = priv->rtx_min_retry_timeout * GST_MSECOND;
2864 rtx_retry_timeout = MAX (rtx_retry_timeout, rtx_min_retry_timeout);
2866 return rtx_retry_timeout;
2870 get_rtx_retry_period (GstRtpJitterBufferPrivate * priv,
2871 GstClockTime rtx_retry_timeout)
2873 GstClockTime rtx_retry_period;
2875 if (priv->rtx_retry_period == -1) {
2876 /* we retry up to the configured jitterbuffer size but leaving some
2877 * room for the retransmission to arrive in time */
2878 if (rtx_retry_timeout > priv->latency_ns) {
2879 rtx_retry_period = 0;
2881 rtx_retry_period = priv->latency_ns - rtx_retry_timeout;
2884 rtx_retry_period = priv->rtx_retry_period * GST_MSECOND;
2886 return rtx_retry_period;
2889 /* the timeout for when we expected a packet expired */
2891 do_expected_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
2894 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2896 guint delay, delay_ms, avg_rtx_rtt_ms;
2897 guint rtx_retry_timeout_ms, rtx_retry_period_ms;
2898 GstClockTime rtx_retry_period;
2899 GstClockTime rtx_retry_timeout;
2902 GST_DEBUG_OBJECT (jitterbuffer, "expected %d didn't arrive, now %"
2903 GST_TIME_FORMAT, timer->seqnum, GST_TIME_ARGS (now));
2905 rtx_retry_timeout = get_rtx_retry_timeout (priv);
2906 rtx_retry_period = get_rtx_retry_period (priv, rtx_retry_timeout);
2908 GST_DEBUG_OBJECT (jitterbuffer, "timeout %" GST_TIME_FORMAT ", period %"
2909 GST_TIME_FORMAT, GST_TIME_ARGS (rtx_retry_timeout),
2910 GST_TIME_ARGS (rtx_retry_period));
2912 delay = timer->rtx_delay + timer->rtx_retry;
2914 delay_ms = GST_TIME_AS_MSECONDS (delay);
2915 rtx_retry_timeout_ms = GST_TIME_AS_MSECONDS (rtx_retry_timeout);
2916 rtx_retry_period_ms = GST_TIME_AS_MSECONDS (rtx_retry_period);
2917 avg_rtx_rtt_ms = GST_TIME_AS_MSECONDS (priv->avg_rtx_rtt);
2919 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
2920 gst_structure_new ("GstRTPRetransmissionRequest",
2921 "seqnum", G_TYPE_UINT, (guint) timer->seqnum,
2922 "running-time", G_TYPE_UINT64, timer->rtx_base,
2923 "delay", G_TYPE_UINT, delay_ms,
2924 "retry", G_TYPE_UINT, timer->num_rtx_retry,
2925 "frequency", G_TYPE_UINT, rtx_retry_timeout_ms,
2926 "period", G_TYPE_UINT, rtx_retry_period_ms,
2927 "deadline", G_TYPE_UINT, priv->latency_ms,
2928 "packet-spacing", G_TYPE_UINT64, priv->packet_spacing,
2929 "avg-rtt", G_TYPE_UINT, avg_rtx_rtt_ms, NULL));
2931 priv->num_rtx_requests++;
2932 timer->num_rtx_retry++;
2934 GST_OBJECT_LOCK (jitterbuffer);
2935 if ((clock = GST_ELEMENT_CLOCK (jitterbuffer))) {
2936 timer->rtx_last = gst_clock_get_time (clock);
2937 timer->rtx_last -= GST_ELEMENT_CAST (jitterbuffer)->base_time;
2939 timer->rtx_last = now;
2941 GST_OBJECT_UNLOCK (jitterbuffer);
2943 /* calculate the timeout for the next retransmission attempt */
2944 timer->rtx_retry += rtx_retry_timeout;
2945 GST_DEBUG_OBJECT (jitterbuffer, "base %" GST_TIME_FORMAT ", delay %"
2946 GST_TIME_FORMAT ", retry %" GST_TIME_FORMAT ", num_retry %u",
2947 GST_TIME_ARGS (timer->rtx_base), GST_TIME_ARGS (timer->rtx_delay),
2948 GST_TIME_ARGS (timer->rtx_retry), timer->num_rtx_retry);
2949 if ((priv->rtx_max_retries != -1
2950 && timer->num_rtx_retry >= priv->rtx_max_retries)
2951 || (timer->rtx_retry + timer->rtx_delay > rtx_retry_period)) {
2952 GST_DEBUG_OBJECT (jitterbuffer, "reschedule as LOST timer");
2953 /* too many retransmission request, we now convert the timer
2954 * to a lost timer, leave the num_rtx_retry as it is for stats */
2955 timer->type = TIMER_TYPE_LOST;
2956 timer->rtx_delay = 0;
2957 timer->rtx_retry = 0;
2959 reschedule_timer (jitterbuffer, timer, timer->seqnum,
2960 timer->rtx_base + timer->rtx_retry, timer->rtx_delay, FALSE);
2963 gst_pad_push_event (priv->sinkpad, event);
2969 /* a packet is lost */
2971 do_lost_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
2974 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
2975 GstClockTime duration, timestamp;
2976 guint seqnum, lost_packets, num_rtx_retry, next_in_seqnum;
2977 gboolean late, head;
2979 RTPJitterBufferItem *item;
2981 seqnum = timer->seqnum;
2982 timestamp = apply_offset (jitterbuffer, timer->timeout);
2983 duration = timer->duration;
2984 if (duration == GST_CLOCK_TIME_NONE && priv->packet_spacing > 0)
2985 duration = priv->packet_spacing;
2986 lost_packets = MAX (timer->num, 1);
2987 late = timer->num > 0;
2988 num_rtx_retry = timer->num_rtx_retry;
2990 /* we had a gap and thus we lost some packets. Create an event for this. */
2991 if (lost_packets > 1)
2992 GST_DEBUG_OBJECT (jitterbuffer, "Packets #%d -> #%d lost", seqnum,
2993 seqnum + lost_packets - 1);
2995 GST_DEBUG_OBJECT (jitterbuffer, "Packet #%d lost", seqnum);
2997 priv->num_late += lost_packets;
2998 priv->num_rtx_failed += num_rtx_retry;
3000 next_in_seqnum = (seqnum + lost_packets) & 0xffff;
3002 /* we now only accept seqnum bigger than this */
3003 if (gst_rtp_buffer_compare_seqnum (priv->next_in_seqnum, next_in_seqnum) > 0)
3004 priv->next_in_seqnum = next_in_seqnum;
3006 /* create paket lost event */
3007 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
3008 gst_structure_new ("GstRTPPacketLost",
3009 "seqnum", G_TYPE_UINT, (guint) seqnum,
3010 "timestamp", G_TYPE_UINT64, timestamp,
3011 "duration", G_TYPE_UINT64, duration,
3012 "late", G_TYPE_BOOLEAN, late,
3013 "retry", G_TYPE_UINT, num_rtx_retry, NULL));
3015 item = alloc_item (event, ITEM_TYPE_LOST, -1, -1, seqnum, lost_packets, -1);
3016 rtp_jitter_buffer_insert (priv->jbuf, item, &head, NULL);
3018 /* remove timer now */
3019 remove_timer (jitterbuffer, timer);
3021 JBUF_SIGNAL_EVENT (priv);
3027 do_eos_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
3030 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
3032 GST_INFO_OBJECT (jitterbuffer, "got the NPT timeout");
3033 remove_timer (jitterbuffer, timer);
3035 /* there was no EOS in the buffer, put one in there now */
3036 queue_event (jitterbuffer, gst_event_new_eos ());
3038 JBUF_SIGNAL_EVENT (priv);
3044 do_deadline_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
3047 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
3049 GST_INFO_OBJECT (jitterbuffer, "got deadline timeout");
3051 /* timer seqnum might have been obsoleted by caps seqnum-base,
3052 * only mess with current ongoing seqnum if still unknown */
3053 if (priv->next_seqnum == -1)
3054 priv->next_seqnum = timer->seqnum;
3055 remove_timer (jitterbuffer, timer);
3056 JBUF_SIGNAL_EVENT (priv);
3062 do_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
3065 gboolean removed = FALSE;
3067 switch (timer->type) {
3068 case TIMER_TYPE_EXPECTED:
3069 removed = do_expected_timeout (jitterbuffer, timer, now);
3071 case TIMER_TYPE_LOST:
3072 removed = do_lost_timeout (jitterbuffer, timer, now);
3074 case TIMER_TYPE_DEADLINE:
3075 removed = do_deadline_timeout (jitterbuffer, timer, now);
3077 case TIMER_TYPE_EOS:
3078 removed = do_eos_timeout (jitterbuffer, timer, now);
3084 /* called when we need to wait for the next timeout.
3086 * We loop over the array of recorded timeouts and wait for the earliest one.
3087 * When it timed out, do the logic associated with the timer.
3089 * If there are no timers, we wait on a gcond until something new happens.
3092 wait_next_timeout (GstRtpJitterBuffer * jitterbuffer)
3094 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
3095 GstClockTime now = 0;
3098 while (priv->timer_running) {
3099 TimerData *timer = NULL;
3100 GstClockTime timer_timeout = -1;
3103 GST_DEBUG_OBJECT (jitterbuffer, "now %" GST_TIME_FORMAT,
3104 GST_TIME_ARGS (now));
3106 len = priv->timers->len;
3107 for (i = 0; i < len; i++) {
3108 TimerData *test = &g_array_index (priv->timers, TimerData, i);
3109 GstClockTime test_timeout = get_timeout (jitterbuffer, test);
3110 gboolean save_best = FALSE;
3112 GST_DEBUG_OBJECT (jitterbuffer, "%d, %d, %d, %" GST_TIME_FORMAT,
3113 i, test->type, test->seqnum, GST_TIME_ARGS (test_timeout));
3115 /* find the smallest timeout */
3116 if (timer == NULL) {
3118 } else if (timer_timeout == -1) {
3119 /* we already have an immediate timeout, the new timer must be an
3120 * immediate timer with smaller seqnum to become the best */
3121 if (test_timeout == -1
3122 && (gst_rtp_buffer_compare_seqnum (test->seqnum,
3123 timer->seqnum) > 0))
3125 } else if (test_timeout == -1) {
3126 /* first immediate timer */
3128 } else if (test_timeout < timer_timeout) {
3131 } else if (test_timeout == timer_timeout
3132 && (gst_rtp_buffer_compare_seqnum (test->seqnum,
3133 timer->seqnum) > 0)) {
3134 /* same timer, smaller seqnum */
3138 GST_DEBUG_OBJECT (jitterbuffer, "new best %d", i);
3140 timer_timeout = test_timeout;
3143 if (timer && !priv->blocked) {
3145 GstClockTime sync_time;
3148 GstClockTimeDiff clock_jitter;
3150 if (timer_timeout == -1 || timer_timeout <= now) {
3151 do_timeout (jitterbuffer, timer, now);
3152 /* check here, do_timeout could have released the lock */
3153 if (!priv->timer_running)
3158 GST_OBJECT_LOCK (jitterbuffer);
3159 clock = GST_ELEMENT_CLOCK (jitterbuffer);
3161 GST_OBJECT_UNLOCK (jitterbuffer);
3162 /* let's just push if there is no clock */
3163 GST_DEBUG_OBJECT (jitterbuffer, "No clock, timeout right away");
3164 now = timer_timeout;
3168 /* prepare for sync against clock */
3169 sync_time = timer_timeout + GST_ELEMENT_CAST (jitterbuffer)->base_time;
3170 /* add latency of peer to get input time */
3171 sync_time += priv->peer_latency;
3173 GST_DEBUG_OBJECT (jitterbuffer, "sync to timestamp %" GST_TIME_FORMAT
3174 " with sync time %" GST_TIME_FORMAT,
3175 GST_TIME_ARGS (timer_timeout), GST_TIME_ARGS (sync_time));
3177 /* create an entry for the clock */
3178 id = priv->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
3179 priv->timer_timeout = timer_timeout;
3180 priv->timer_seqnum = timer->seqnum;
3181 GST_OBJECT_UNLOCK (jitterbuffer);
3183 /* release the lock so that the other end can push stuff or unlock */
3186 ret = gst_clock_id_wait (id, &clock_jitter);
3189 if (!priv->timer_running) {
3190 gst_clock_id_unref (id);
3191 priv->clock_id = NULL;
3195 if (ret != GST_CLOCK_UNSCHEDULED) {
3196 now = timer_timeout + MAX (clock_jitter, 0);
3197 GST_DEBUG_OBJECT (jitterbuffer, "sync done, %d, #%d, %" G_GINT64_FORMAT,
3198 ret, priv->timer_seqnum, clock_jitter);
3200 GST_DEBUG_OBJECT (jitterbuffer, "sync unscheduled");
3202 /* and free the entry */
3203 gst_clock_id_unref (id);
3204 priv->clock_id = NULL;
3206 /* no timers, wait for activity */
3207 JBUF_WAIT_TIMER (priv);
3212 GST_DEBUG_OBJECT (jitterbuffer, "we are stopping");
3217 * This funcion implements the main pushing loop on the source pad.
3219 * It first tries to push as many buffers as possible. If there is a seqnum
3220 * mismatch, we wait for the next timeouts.
3223 gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer)
3225 GstRtpJitterBufferPrivate *priv;
3226 GstFlowReturn result = GST_FLOW_OK;
3228 priv = jitterbuffer->priv;
3230 JBUF_LOCK_CHECK (priv, flushing);
3232 result = handle_next_buffer (jitterbuffer);
3233 if (G_LIKELY (result == GST_FLOW_WAIT)) {
3234 /* now wait for the next event */
3235 JBUF_WAIT_EVENT (priv, flushing);
3236 result = GST_FLOW_OK;
3239 while (result == GST_FLOW_OK);
3240 /* store result for upstream */
3241 priv->srcresult = result;
3242 /* if we get here we need to pause */
3248 result = priv->srcresult;
3255 JBUF_SIGNAL_QUERY (priv, FALSE);
3258 GST_DEBUG_OBJECT (jitterbuffer, "pausing task, reason %s",
3259 gst_flow_get_name (result));
3260 gst_pad_pause_task (priv->srcpad);
3261 if (result == GST_FLOW_EOS) {
3262 event = gst_event_new_eos ();
3263 gst_pad_push_event (priv->srcpad, event);
3269 /* collect the info from the lastest RTCP packet and the jitterbuffer sync, do
3270 * some sanity checks and then emit the handle-sync signal with the parameters.
3271 * This function must be called with the LOCK */
3273 do_handle_sync (GstRtpJitterBuffer * jitterbuffer)
3275 GstRtpJitterBufferPrivate *priv;
3276 guint64 base_rtptime, base_time;
3278 guint64 last_rtptime;
3280 guint64 ext_rtptime, diff;
3281 gboolean valid = TRUE, keep = FALSE;
3283 priv = jitterbuffer->priv;
3285 /* get the last values from the jitterbuffer */
3286 rtp_jitter_buffer_get_sync (priv->jbuf, &base_rtptime, &base_time,
3287 &clock_rate, &last_rtptime);
3289 clock_base = priv->clock_base;
3290 ext_rtptime = priv->ext_rtptime;
3292 GST_DEBUG_OBJECT (jitterbuffer, "ext SR %" G_GUINT64_FORMAT ", base %"
3293 G_GUINT64_FORMAT ", clock-rate %" G_GUINT32_FORMAT
3294 ", clock-base %" G_GUINT64_FORMAT ", last-rtptime %" G_GUINT64_FORMAT,
3295 ext_rtptime, base_rtptime, clock_rate, clock_base, last_rtptime);
3297 if (base_rtptime == -1 || clock_rate == -1 || base_time == -1) {
3298 /* we keep this SR packet for later. When we get a valid RTP packet the
3299 * above values will be set and we can try to use the SR packet */
3300 GST_DEBUG_OBJECT (jitterbuffer, "keeping for later, no RTP values");
3303 /* we can't accept anything that happened before we did the last resync */
3304 if (base_rtptime > ext_rtptime) {
3305 GST_DEBUG_OBJECT (jitterbuffer, "dropping, older than base time");
3308 /* the SR RTP timestamp must be something close to what we last observed
3309 * in the jitterbuffer */
3310 if (ext_rtptime > last_rtptime) {
3311 /* check how far ahead it is to our RTP timestamps */
3312 diff = ext_rtptime - last_rtptime;
3313 /* if bigger than 1 second, we drop it */
3314 if (diff > clock_rate) {
3315 GST_DEBUG_OBJECT (jitterbuffer, "too far ahead");
3316 /* should drop this, but some RTSP servers end up with bogus
3317 * way too ahead RTCP packet when repeated PAUSE/PLAY,
3318 * so still trigger rptbin sync but invalidate RTCP data
3319 * (sync might use other methods) */
3322 GST_DEBUG_OBJECT (jitterbuffer, "ext last %" G_GUINT64_FORMAT ", diff %"
3323 G_GUINT64_FORMAT, last_rtptime, diff);
3329 GST_DEBUG_OBJECT (jitterbuffer, "keeping RTCP packet for later");
3333 s = gst_structure_new ("application/x-rtp-sync",
3334 "base-rtptime", G_TYPE_UINT64, base_rtptime,
3335 "base-time", G_TYPE_UINT64, base_time,
3336 "clock-rate", G_TYPE_UINT, clock_rate,
3337 "clock-base", G_TYPE_UINT64, clock_base,
3338 "sr-ext-rtptime", G_TYPE_UINT64, ext_rtptime,
3339 "sr-buffer", GST_TYPE_BUFFER, priv->last_sr, NULL);
3341 GST_DEBUG_OBJECT (jitterbuffer, "signaling sync");
3342 gst_buffer_replace (&priv->last_sr, NULL);
3344 g_signal_emit (jitterbuffer,
3345 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC], 0, s);
3347 gst_structure_free (s);
3349 GST_DEBUG_OBJECT (jitterbuffer, "dropping RTCP packet");
3350 gst_buffer_replace (&priv->last_sr, NULL);
3354 static GstFlowReturn
3355 gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad, GstObject * parent,
3358 GstRtpJitterBuffer *jitterbuffer;
3359 GstRtpJitterBufferPrivate *priv;
3360 GstFlowReturn ret = GST_FLOW_OK;
3362 GstRTCPPacket packet;
3363 guint64 ext_rtptime;
3365 GstRTCPBuffer rtcp = { NULL, };
3367 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
3369 if (G_UNLIKELY (!gst_rtcp_buffer_validate (buffer)))
3370 goto invalid_buffer;
3372 priv = jitterbuffer->priv;
3374 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
3376 if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet))
3379 /* first packet must be SR or RR or else the validate would have failed */
3380 switch (gst_rtcp_packet_get_type (&packet)) {
3381 case GST_RTCP_TYPE_SR:
3382 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, &rtptime,
3388 gst_rtcp_buffer_unmap (&rtcp);
3390 GST_DEBUG_OBJECT (jitterbuffer, "received RTCP of SSRC %08x", ssrc);
3393 /* convert the RTP timestamp to our extended timestamp, using the same offset
3394 * we used in the jitterbuffer */
3395 ext_rtptime = priv->jbuf->ext_rtptime;
3396 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
3398 priv->ext_rtptime = ext_rtptime;
3399 gst_buffer_replace (&priv->last_sr, buffer);
3401 do_handle_sync (jitterbuffer);
3405 gst_buffer_unref (buffer);
3411 /* this is not fatal but should be filtered earlier */
3412 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
3413 ("Received invalid RTCP payload, dropping"));
3419 /* this is not fatal but should be filtered earlier */
3420 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
3421 ("Received empty RTCP payload, dropping"));
3422 gst_rtcp_buffer_unmap (&rtcp);
3428 GST_DEBUG_OBJECT (jitterbuffer, "ignoring RTCP packet");
3429 gst_rtcp_buffer_unmap (&rtcp);
3436 gst_rtp_jitter_buffer_sink_query (GstPad * pad, GstObject * parent,
3439 gboolean res = FALSE;
3440 GstRtpJitterBuffer *jitterbuffer;
3441 GstRtpJitterBufferPrivate *priv;
3443 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
3444 priv = jitterbuffer->priv;
3446 switch (GST_QUERY_TYPE (query)) {
3447 case GST_QUERY_CAPS:
3449 GstCaps *filter, *caps;
3451 gst_query_parse_caps (query, &filter);
3452 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
3453 gst_query_set_caps_result (query, caps);
3454 gst_caps_unref (caps);
3459 if (GST_QUERY_IS_SERIALIZED (query)) {
3460 RTPJitterBufferItem *item;
3463 JBUF_LOCK_CHECK (priv, out_flushing);
3464 if (rtp_jitter_buffer_get_mode (priv->jbuf) !=
3465 RTP_JITTER_BUFFER_MODE_BUFFER) {
3466 GST_DEBUG_OBJECT (jitterbuffer, "adding serialized query");
3467 item = alloc_item (query, ITEM_TYPE_QUERY, -1, -1, -1, 0, -1);
3468 rtp_jitter_buffer_insert (priv->jbuf, item, &head, NULL);
3470 JBUF_SIGNAL_EVENT (priv);
3471 JBUF_WAIT_QUERY (priv, out_flushing);
3472 res = priv->last_query;
3474 GST_DEBUG_OBJECT (jitterbuffer, "refusing query, we are buffering");
3479 res = gst_pad_query_default (pad, parent, query);
3487 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
3495 gst_rtp_jitter_buffer_src_query (GstPad * pad, GstObject * parent,
3498 GstRtpJitterBuffer *jitterbuffer;
3499 GstRtpJitterBufferPrivate *priv;
3500 gboolean res = FALSE;
3502 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
3503 priv = jitterbuffer->priv;
3505 switch (GST_QUERY_TYPE (query)) {
3506 case GST_QUERY_LATENCY:
3508 /* We need to send the query upstream and add the returned latency to our
3510 GstClockTime min_latency, max_latency;
3512 GstClockTime our_latency;
3514 if ((res = gst_pad_peer_query (priv->sinkpad, query))) {
3515 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
3517 GST_DEBUG_OBJECT (jitterbuffer, "Peer latency: min %"
3518 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
3519 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
3521 /* store this so that we can safely sync on the peer buffers. */
3523 priv->peer_latency = min_latency;
3524 our_latency = priv->latency_ns;
3527 GST_DEBUG_OBJECT (jitterbuffer, "Our latency: %" GST_TIME_FORMAT,
3528 GST_TIME_ARGS (our_latency));
3530 /* we add some latency but can buffer an infinite amount of time */
3531 min_latency += our_latency;
3534 GST_DEBUG_OBJECT (jitterbuffer, "Calculated total latency : min %"
3535 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
3536 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
3538 gst_query_set_latency (query, TRUE, min_latency, max_latency);
3542 case GST_QUERY_POSITION:
3544 GstClockTime start, last_out;
3547 gst_query_parse_position (query, &fmt, NULL);
3548 if (fmt != GST_FORMAT_TIME) {
3549 res = gst_pad_query_default (pad, parent, query);
3554 start = priv->npt_start;
3555 last_out = priv->last_out_time;
3558 GST_DEBUG_OBJECT (jitterbuffer, "npt start %" GST_TIME_FORMAT
3559 ", last out %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
3560 GST_TIME_ARGS (last_out));
3562 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (last_out)) {
3563 /* bring 0-based outgoing time to stream time */
3564 gst_query_set_position (query, GST_FORMAT_TIME, start + last_out);
3567 res = gst_pad_query_default (pad, parent, query);
3571 case GST_QUERY_CAPS:
3573 GstCaps *filter, *caps;
3575 gst_query_parse_caps (query, &filter);
3576 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
3577 gst_query_set_caps_result (query, caps);
3578 gst_caps_unref (caps);
3583 res = gst_pad_query_default (pad, parent, query);
3591 gst_rtp_jitter_buffer_set_property (GObject * object,
3592 guint prop_id, const GValue * value, GParamSpec * pspec)
3594 GstRtpJitterBuffer *jitterbuffer;
3595 GstRtpJitterBufferPrivate *priv;
3597 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
3598 priv = jitterbuffer->priv;
3603 guint new_latency, old_latency;
3605 new_latency = g_value_get_uint (value);
3608 old_latency = priv->latency_ms;
3609 priv->latency_ms = new_latency;
3610 priv->latency_ns = priv->latency_ms * GST_MSECOND;
3611 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
3614 /* post message if latency changed, this will inform the parent pipeline
3615 * that a latency reconfiguration is possible/needed. */
3616 if (new_latency != old_latency) {
3617 GST_DEBUG_OBJECT (jitterbuffer, "latency changed to: %" GST_TIME_FORMAT,
3618 GST_TIME_ARGS (new_latency * GST_MSECOND));
3620 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer),
3621 gst_message_new_latency (GST_OBJECT_CAST (jitterbuffer)));
3625 case PROP_DROP_ON_LATENCY:
3627 priv->drop_on_latency = g_value_get_boolean (value);
3630 case PROP_TS_OFFSET:
3632 priv->ts_offset = g_value_get_int64 (value);
3633 priv->ts_discont = TRUE;
3638 priv->do_lost = g_value_get_boolean (value);
3643 rtp_jitter_buffer_set_mode (priv->jbuf, g_value_get_enum (value));
3646 case PROP_DO_RETRANSMISSION:
3648 priv->do_retransmission = g_value_get_boolean (value);
3651 case PROP_RTX_NEXT_SEQNUM:
3653 priv->rtx_next_seqnum = g_value_get_boolean (value);
3656 case PROP_RTX_DELAY:
3658 priv->rtx_delay = g_value_get_int (value);
3661 case PROP_RTX_MIN_DELAY:
3663 priv->rtx_min_delay = g_value_get_uint (value);
3666 case PROP_RTX_DELAY_REORDER:
3668 priv->rtx_delay_reorder = g_value_get_int (value);
3671 case PROP_RTX_RETRY_TIMEOUT:
3673 priv->rtx_retry_timeout = g_value_get_int (value);
3676 case PROP_RTX_MIN_RETRY_TIMEOUT:
3678 priv->rtx_min_retry_timeout = g_value_get_int (value);
3681 case PROP_RTX_RETRY_PERIOD:
3683 priv->rtx_retry_period = g_value_get_int (value);
3686 case PROP_RTX_MAX_RETRIES:
3688 priv->rtx_max_retries = g_value_get_int (value);
3692 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3698 gst_rtp_jitter_buffer_get_property (GObject * object,
3699 guint prop_id, GValue * value, GParamSpec * pspec)
3701 GstRtpJitterBuffer *jitterbuffer;
3702 GstRtpJitterBufferPrivate *priv;
3704 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
3705 priv = jitterbuffer->priv;
3710 g_value_set_uint (value, priv->latency_ms);
3713 case PROP_DROP_ON_LATENCY:
3715 g_value_set_boolean (value, priv->drop_on_latency);
3718 case PROP_TS_OFFSET:
3720 g_value_set_int64 (value, priv->ts_offset);
3725 g_value_set_boolean (value, priv->do_lost);
3730 g_value_set_enum (value, rtp_jitter_buffer_get_mode (priv->jbuf));
3738 if (priv->srcresult != GST_FLOW_OK)
3741 percent = rtp_jitter_buffer_get_percent (priv->jbuf);
3743 g_value_set_int (value, percent);
3747 case PROP_DO_RETRANSMISSION:
3749 g_value_set_boolean (value, priv->do_retransmission);
3752 case PROP_RTX_NEXT_SEQNUM:
3754 g_value_set_boolean (value, priv->rtx_next_seqnum);
3757 case PROP_RTX_DELAY:
3759 g_value_set_int (value, priv->rtx_delay);
3762 case PROP_RTX_MIN_DELAY:
3764 g_value_set_uint (value, priv->rtx_min_delay);
3767 case PROP_RTX_DELAY_REORDER:
3769 g_value_set_int (value, priv->rtx_delay_reorder);
3772 case PROP_RTX_RETRY_TIMEOUT:
3774 g_value_set_int (value, priv->rtx_retry_timeout);
3777 case PROP_RTX_MIN_RETRY_TIMEOUT:
3779 g_value_set_int (value, priv->rtx_min_retry_timeout);
3782 case PROP_RTX_RETRY_PERIOD:
3784 g_value_set_int (value, priv->rtx_retry_period);
3787 case PROP_RTX_MAX_RETRIES:
3789 g_value_set_int (value, priv->rtx_max_retries);
3793 g_value_take_boxed (value,
3794 gst_rtp_jitter_buffer_create_stats (jitterbuffer));
3797 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3802 static GstStructure *
3803 gst_rtp_jitter_buffer_create_stats (GstRtpJitterBuffer * jbuf)
3807 JBUF_LOCK (jbuf->priv);
3808 s = gst_structure_new ("application/x-rtp-jitterbuffer-stats",
3809 "rtx-count", G_TYPE_UINT64, jbuf->priv->num_rtx_requests,
3810 "rtx-success-count", G_TYPE_UINT64, jbuf->priv->num_rtx_success,
3811 "rtx-per-packet", G_TYPE_DOUBLE, jbuf->priv->avg_rtx_num,
3812 "rtx-rtt", G_TYPE_UINT64, jbuf->priv->avg_rtx_rtt, NULL);
3813 JBUF_UNLOCK (jbuf->priv);