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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 * SECTION:element-gstrtpjitterbuffer
28 * @short_description: buffer, reorder and remove duplicate RTP packets to
29 * compensate for network oddities.
33 * This element reorders and removes duplicate RTP packets as they are received
34 * from a network source. It will also wait for missing packets up to a
35 * configurable time limit using the ::latency property. Packets arriving too
36 * late are considered to be lost packets.
39 * This element acts as a live element and so adds ::latency to the pipeline.
42 * The element needs the clock-rate of the RTP payload in order to estimate the
43 * delay. This information is obtained either from the caps on the sink pad or,
44 * when no caps are present, from the ::request-pt-map signal. To clear the
45 * previous pt-map use the ::clear-pt-map signal.
48 * This element will automatically be used inside gstrtpbin.
50 * <title>Example pipelines</title>
53 * gst-launch rtspsrc location=rtsp://192.168.1.133:8554/mpeg1or2AudioVideoTest ! gstrtpjitterbuffer ! rtpmpvdepay ! mpeg2dec ! xvimagesink
55 * Connect to a streaming server and decode the MPEG video. The jitterbuffer is
56 * inserted into the pipeline to smooth out network jitter and to reorder the
57 * out-of-order RTP packets.
61 * Last reviewed on 2007-05-28 (0.10.5)
70 #include <gst/rtp/gstrtpbuffer.h>
72 #include "gstrtpbin-marshal.h"
74 #include "gstrtpjitterbuffer.h"
75 #include "rtpjitterbuffer.h"
77 GST_DEBUG_CATEGORY (rtpjitterbuffer_debug);
78 #define GST_CAT_DEFAULT (rtpjitterbuffer_debug)
80 /* low and high threshold tell the queue when to start and stop buffering */
81 #define LOW_THRESHOLD 0.2
82 #define HIGH_THRESHOLD 0.8
84 /* elementfactory information */
85 static const GstElementDetails gst_rtp_jitter_buffer_details =
86 GST_ELEMENT_DETAILS ("RTP packet jitter-buffer",
88 "A buffer that deals with network jitter and other transmission faults",
89 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
90 "Wim Taymans <wim.taymans@gmail.com>");
92 /* RTPJitterBuffer signals and args */
95 SIGNAL_REQUEST_PT_MAP,
100 #define DEFAULT_LATENCY_MS 200
101 #define DEFAULT_DROP_ON_LATENCY FALSE
102 #define DEFAULT_TS_OFFSET 0
108 PROP_DROP_ON_LATENCY,
112 #define JBUF_LOCK(priv) (g_mutex_lock ((priv)->jbuf_lock))
114 #define JBUF_LOCK_CHECK(priv,label) G_STMT_START { \
116 if (priv->srcresult != GST_FLOW_OK) \
120 #define JBUF_UNLOCK(priv) (g_mutex_unlock ((priv)->jbuf_lock))
121 #define JBUF_WAIT(priv) (g_cond_wait ((priv)->jbuf_cond, (priv)->jbuf_lock))
123 #define JBUF_WAIT_CHECK(priv,label) G_STMT_START { \
125 if (priv->srcresult != GST_FLOW_OK) \
129 #define JBUF_SIGNAL(priv) (g_cond_signal ((priv)->jbuf_cond))
131 struct _GstRtpJitterBufferPrivate
133 GstPad *sinkpad, *srcpad;
135 RTPJitterBuffer *jbuf;
142 gboolean drop_on_latency;
145 /* the last seqnum we pushed out */
146 guint32 last_popped_seqnum;
147 /* the next expected seqnum */
153 /* clock rate and rtp timestamp offset */
157 gint64 prev_ts_offset;
159 /* when we are shutting down */
160 GstFlowReturn srcresult;
166 /* the latency of the upstream peer, we have to take this into account when
167 * synchronizing the buffers. */
168 GstClockTime peer_latency;
170 /* some accounting */
172 guint64 num_duplicates;
175 #define GST_RTP_JITTER_BUFFER_GET_PRIVATE(o) \
176 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_RTP_JITTER_BUFFER, \
177 GstRtpJitterBufferPrivate))
179 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_template =
180 GST_STATIC_PAD_TEMPLATE ("sink",
183 GST_STATIC_CAPS ("application/x-rtp, "
184 "clock-rate = (int) [ 1, 2147483647 ]"
185 /* "payload = (int) , "
186 * "encoding-name = (string) "
190 static GstStaticPadTemplate gst_rtp_jitter_buffer_src_template =
191 GST_STATIC_PAD_TEMPLATE ("src",
194 GST_STATIC_CAPS ("application/x-rtp"
195 /* "payload = (int) , "
196 * "clock-rate = (int) , "
197 * "encoding-name = (string) "
201 static guint gst_rtp_jitter_buffer_signals[LAST_SIGNAL] = { 0 };
203 GST_BOILERPLATE (GstRtpJitterBuffer, gst_rtp_jitter_buffer, GstElement,
206 /* object overrides */
207 static void gst_rtp_jitter_buffer_set_property (GObject * object,
208 guint prop_id, const GValue * value, GParamSpec * pspec);
209 static void gst_rtp_jitter_buffer_get_property (GObject * object,
210 guint prop_id, GValue * value, GParamSpec * pspec);
211 static void gst_rtp_jitter_buffer_finalize (GObject * object);
213 /* element overrides */
214 static GstStateChangeReturn gst_rtp_jitter_buffer_change_state (GstElement
215 * element, GstStateChange transition);
218 static GstCaps *gst_rtp_jitter_buffer_getcaps (GstPad * pad);
220 /* sinkpad overrides */
221 static gboolean gst_jitter_buffer_sink_setcaps (GstPad * pad, GstCaps * caps);
222 static gboolean gst_rtp_jitter_buffer_sink_event (GstPad * pad,
224 static GstFlowReturn gst_rtp_jitter_buffer_chain (GstPad * pad,
227 /* srcpad overrides */
229 gst_rtp_jitter_buffer_src_activate_push (GstPad * pad, gboolean active);
230 static void gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer);
231 static gboolean gst_rtp_jitter_buffer_query (GstPad * pad, GstQuery * query);
234 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer);
237 gst_rtp_jitter_buffer_base_init (gpointer klass)
239 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
241 gst_element_class_add_pad_template (element_class,
242 gst_static_pad_template_get (&gst_rtp_jitter_buffer_src_template));
243 gst_element_class_add_pad_template (element_class,
244 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_template));
245 gst_element_class_set_details (element_class, &gst_rtp_jitter_buffer_details);
249 gst_rtp_jitter_buffer_class_init (GstRtpJitterBufferClass * klass)
251 GObjectClass *gobject_class;
252 GstElementClass *gstelement_class;
254 gobject_class = (GObjectClass *) klass;
255 gstelement_class = (GstElementClass *) klass;
257 g_type_class_add_private (klass, sizeof (GstRtpJitterBufferPrivate));
259 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_finalize);
261 gobject_class->set_property = gst_rtp_jitter_buffer_set_property;
262 gobject_class->get_property = gst_rtp_jitter_buffer_get_property;
265 * GstRtpJitterBuffer::latency:
267 * The maximum latency of the jitterbuffer. Packets will be kept in the buffer
268 * for at most this time.
270 g_object_class_install_property (gobject_class, PROP_LATENCY,
271 g_param_spec_uint ("latency", "Buffer latency in ms",
272 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
275 * GstRtpJitterBuffer::drop-on-latency:
277 * Drop oldest buffers when the queue is completely filled.
279 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
280 g_param_spec_boolean ("drop-on-latency",
281 "Drop buffers when maximum latency is reached",
282 "Tells the jitterbuffer to never exceed the given latency in size",
283 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE));
285 * GstRtpJitterBuffer::ts-offset:
287 * Adjust RTP timestamps in the jitterbuffer with offset.
289 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
290 g_param_spec_int64 ("ts-offset",
292 "Adjust buffer RTP timestamps with offset in nanoseconds", G_MININT64,
293 G_MAXINT64, DEFAULT_TS_OFFSET, G_PARAM_READWRITE));
295 * GstRtpJitterBuffer::request-pt-map:
296 * @buffer: the object which received the signal
299 * Request the payload type as #GstCaps for @pt.
301 gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP] =
302 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
303 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
304 request_pt_map), NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT,
305 GST_TYPE_CAPS, 1, G_TYPE_UINT);
307 * GstRtpJitterBuffer::clear-pt-map:
308 * @buffer: the object which received the signal
310 * Invalidate the clock-rate as obtained with the ::request-pt-map signal.
312 gst_rtp_jitter_buffer_signals[SIGNAL_CLEAR_PT_MAP] =
313 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
314 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
315 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
316 G_TYPE_NONE, 0, G_TYPE_NONE);
318 gstelement_class->change_state = gst_rtp_jitter_buffer_change_state;
320 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_clear_pt_map);
322 GST_DEBUG_CATEGORY_INIT
323 (rtpjitterbuffer_debug, "gstrtpjitterbuffer", 0, "RTP Jitter Buffer");
327 gst_rtp_jitter_buffer_init (GstRtpJitterBuffer * jitterbuffer,
328 GstRtpJitterBufferClass * klass)
330 GstRtpJitterBufferPrivate *priv;
332 priv = GST_RTP_JITTER_BUFFER_GET_PRIVATE (jitterbuffer);
333 jitterbuffer->priv = priv;
335 priv->latency_ms = DEFAULT_LATENCY_MS;
336 priv->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
338 priv->jbuf = rtp_jitter_buffer_new ();
339 priv->jbuf_lock = g_mutex_new ();
340 priv->jbuf_cond = g_cond_new ();
343 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_src_template,
346 gst_pad_set_activatepush_function (priv->srcpad,
347 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_activate_push));
348 gst_pad_set_query_function (priv->srcpad,
349 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_query));
350 gst_pad_set_getcaps_function (priv->srcpad,
351 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_getcaps));
354 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_sink_template,
357 gst_pad_set_chain_function (priv->sinkpad,
358 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_chain));
359 gst_pad_set_event_function (priv->sinkpad,
360 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_event));
361 gst_pad_set_setcaps_function (priv->sinkpad,
362 GST_DEBUG_FUNCPTR (gst_jitter_buffer_sink_setcaps));
363 gst_pad_set_getcaps_function (priv->sinkpad,
364 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_getcaps));
366 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->srcpad);
367 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->sinkpad);
371 gst_rtp_jitter_buffer_finalize (GObject * object)
373 GstRtpJitterBuffer *jitterbuffer;
375 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
377 g_mutex_free (jitterbuffer->priv->jbuf_lock);
378 g_cond_free (jitterbuffer->priv->jbuf_cond);
380 g_object_unref (jitterbuffer->priv->jbuf);
382 G_OBJECT_CLASS (parent_class)->finalize (object);
386 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer)
388 GstRtpJitterBufferPrivate *priv;
390 priv = jitterbuffer->priv;
392 /* this will trigger a new pt-map request signal, FIXME, do something better. */
393 priv->clock_rate = -1;
397 gst_rtp_jitter_buffer_getcaps (GstPad * pad)
399 GstRtpJitterBuffer *jitterbuffer;
400 GstRtpJitterBufferPrivate *priv;
403 const GstCaps *templ;
405 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
406 priv = jitterbuffer->priv;
408 other = (pad == priv->srcpad ? priv->sinkpad : priv->srcpad);
410 caps = gst_pad_peer_get_caps (other);
412 templ = gst_pad_get_pad_template_caps (pad);
414 GST_DEBUG_OBJECT (jitterbuffer, "copy template");
415 caps = gst_caps_copy (templ);
419 GST_DEBUG_OBJECT (jitterbuffer, "intersect with template");
421 intersect = gst_caps_intersect (caps, templ);
422 gst_caps_unref (caps);
426 gst_object_unref (jitterbuffer);
432 gst_jitter_buffer_sink_parse_caps (GstRtpJitterBuffer * jitterbuffer,
435 GstRtpJitterBufferPrivate *priv;
436 GstStructure *caps_struct;
439 priv = jitterbuffer->priv;
441 /* first parse the caps */
442 caps_struct = gst_caps_get_structure (caps, 0);
444 GST_DEBUG_OBJECT (jitterbuffer, "got caps");
446 /* we need a clock-rate to convert the rtp timestamps to GStreamer time and to
447 * measure the amount of data in the buffer */
448 if (!gst_structure_get_int (caps_struct, "clock-rate", &priv->clock_rate))
451 if (priv->clock_rate <= 0)
454 GST_DEBUG_OBJECT (jitterbuffer, "got clock-rate %d", priv->clock_rate);
456 /* gah, clock-base is uint. If we don't have a base, we will use the first
457 * buffer timestamp as the base time. This will screw up sync but it's better
459 if (gst_structure_get_uint (caps_struct, "clock-base", &val))
460 priv->clock_base = val;
462 priv->clock_base = -1;
464 GST_DEBUG_OBJECT (jitterbuffer, "got clock-base %" G_GINT64_FORMAT,
467 /* first expected seqnum */
468 if (gst_structure_get_uint (caps_struct, "seqnum-base", &val))
469 priv->next_seqnum = val;
471 priv->next_seqnum = -1;
473 GST_DEBUG_OBJECT (jitterbuffer, "got seqnum-base %d", priv->next_seqnum);
480 GST_DEBUG_OBJECT (jitterbuffer, "No clock-rate in caps!");
485 GST_DEBUG_OBJECT (jitterbuffer, "Invalid clock-rate %d", priv->clock_rate);
491 gst_jitter_buffer_sink_setcaps (GstPad * pad, GstCaps * caps)
493 GstRtpJitterBuffer *jitterbuffer;
494 GstRtpJitterBufferPrivate *priv;
497 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
498 priv = jitterbuffer->priv;
500 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
502 /* set same caps on srcpad on success */
504 gst_pad_set_caps (priv->srcpad, caps);
506 gst_object_unref (jitterbuffer);
512 gst_rtp_jitter_buffer_flush_start (GstRtpJitterBuffer * jitterbuffer)
514 GstRtpJitterBufferPrivate *priv;
516 priv = jitterbuffer->priv;
519 /* mark ourselves as flushing */
520 priv->srcresult = GST_FLOW_WRONG_STATE;
521 GST_DEBUG_OBJECT (jitterbuffer, "Disabling pop on queue");
522 /* this unblocks any waiting pops on the src pad task */
524 /* unlock clock, we just unschedule, the entry will be released by the
525 * locking streaming thread. */
527 gst_clock_id_unschedule (priv->clock_id);
532 gst_rtp_jitter_buffer_flush_stop (GstRtpJitterBuffer * jitterbuffer)
534 GstRtpJitterBufferPrivate *priv;
536 priv = jitterbuffer->priv;
539 GST_DEBUG_OBJECT (jitterbuffer, "Enabling pop on queue");
540 /* Mark as non flushing */
541 priv->srcresult = GST_FLOW_OK;
542 gst_segment_init (&priv->segment, GST_FORMAT_TIME);
543 priv->last_popped_seqnum = -1;
544 priv->next_seqnum = -1;
545 priv->clock_rate = -1;
547 rtp_jitter_buffer_flush (priv->jbuf);
548 rtp_jitter_buffer_reset_skew (priv->jbuf);
553 gst_rtp_jitter_buffer_src_activate_push (GstPad * pad, gboolean active)
555 gboolean result = TRUE;
556 GstRtpJitterBuffer *jitterbuffer = NULL;
558 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
561 /* allow data processing */
562 gst_rtp_jitter_buffer_flush_stop (jitterbuffer);
564 /* start pushing out buffers */
565 GST_DEBUG_OBJECT (jitterbuffer, "Starting task on srcpad");
566 gst_pad_start_task (jitterbuffer->priv->srcpad,
567 (GstTaskFunction) gst_rtp_jitter_buffer_loop, jitterbuffer);
569 /* make sure all data processing stops ASAP */
570 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
572 /* NOTE this will hardlock if the state change is called from the src pad
573 * task thread because we will _join() the thread. */
574 GST_DEBUG_OBJECT (jitterbuffer, "Stopping task on srcpad");
575 result = gst_pad_stop_task (pad);
578 gst_object_unref (jitterbuffer);
583 static GstStateChangeReturn
584 gst_rtp_jitter_buffer_change_state (GstElement * element,
585 GstStateChange transition)
587 GstRtpJitterBuffer *jitterbuffer;
588 GstRtpJitterBufferPrivate *priv;
589 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
591 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
592 priv = jitterbuffer->priv;
594 switch (transition) {
595 case GST_STATE_CHANGE_NULL_TO_READY:
597 case GST_STATE_CHANGE_READY_TO_PAUSED:
599 /* reset negotiated values */
600 priv->clock_rate = -1;
601 priv->clock_base = -1;
602 priv->peer_latency = 0;
604 /* block until we go to PLAYING */
605 priv->blocked = TRUE;
606 /* reset skew detection initialy */
607 rtp_jitter_buffer_reset_skew (priv->jbuf);
610 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
612 /* unblock to allow streaming in PLAYING */
613 priv->blocked = FALSE;
621 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
623 switch (transition) {
624 case GST_STATE_CHANGE_READY_TO_PAUSED:
625 /* we are a live element because we sync to the clock, which we can only
626 * do in the PLAYING state */
627 if (ret != GST_STATE_CHANGE_FAILURE)
628 ret = GST_STATE_CHANGE_NO_PREROLL;
630 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
632 /* block to stop streaming when PAUSED */
633 priv->blocked = TRUE;
635 if (ret != GST_STATE_CHANGE_FAILURE)
636 ret = GST_STATE_CHANGE_NO_PREROLL;
638 case GST_STATE_CHANGE_PAUSED_TO_READY:
640 case GST_STATE_CHANGE_READY_TO_NULL:
650 * Performs comparison 'b - a' with check for overflows.
653 priv_compare_rtp_seq_lt (guint16 a, guint16 b)
655 /* check if diff more than half of the 16bit range */
656 if (abs (b - a) > (1 << 15)) {
657 /* one of a/b has wrapped */
665 gst_rtp_jitter_buffer_sink_event (GstPad * pad, GstEvent * event)
668 GstRtpJitterBuffer *jitterbuffer;
669 GstRtpJitterBufferPrivate *priv;
671 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
672 priv = jitterbuffer->priv;
674 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
676 switch (GST_EVENT_TYPE (event)) {
677 case GST_EVENT_NEWSEGMENT:
681 gint64 start, stop, time;
684 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
685 &start, &stop, &time);
687 /* we need time for now */
688 if (format != GST_FORMAT_TIME)
689 goto newseg_wrong_format;
691 GST_DEBUG_OBJECT (jitterbuffer,
692 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
693 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
694 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
695 GST_TIME_ARGS (time));
697 /* now configure the values, we need these to time the release of the
698 * buffers on the srcpad. */
699 gst_segment_set_newsegment_full (&priv->segment, update,
700 rate, arate, format, start, stop, time);
702 /* FIXME, push SEGMENT in the queue. Sorting order might be difficult. */
703 ret = gst_pad_push_event (priv->srcpad, event);
706 case GST_EVENT_FLUSH_START:
707 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
708 ret = gst_pad_push_event (priv->srcpad, event);
710 case GST_EVENT_FLUSH_STOP:
711 ret = gst_pad_push_event (priv->srcpad, event);
712 ret = gst_rtp_jitter_buffer_src_activate_push (priv->srcpad, TRUE);
716 /* push EOS in queue. We always push it at the head */
718 /* check for flushing, we need to discard the event and return FALSE when
720 ret = priv->srcresult == GST_FLOW_OK;
721 if (ret && !priv->eos) {
722 GST_DEBUG_OBJECT (jitterbuffer, "queuing EOS");
725 } else if (priv->eos) {
726 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, we are already EOS");
728 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, reason %s",
729 gst_flow_get_name (priv->srcresult));
732 gst_event_unref (event);
736 ret = gst_pad_push_event (priv->srcpad, event);
741 gst_object_unref (jitterbuffer);
748 GST_DEBUG_OBJECT (jitterbuffer, "received non TIME newsegment");
755 gst_rtp_jitter_buffer_get_clock_rate (GstRtpJitterBuffer * jitterbuffer,
759 GValue args[2] = { {0}, {0} };
763 g_value_init (&args[0], GST_TYPE_ELEMENT);
764 g_value_set_object (&args[0], jitterbuffer);
765 g_value_init (&args[1], G_TYPE_UINT);
766 g_value_set_uint (&args[1], pt);
768 g_value_init (&ret, GST_TYPE_CAPS);
769 g_value_set_boxed (&ret, NULL);
771 g_signal_emitv (args, gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP], 0,
774 g_value_unset (&args[0]);
775 g_value_unset (&args[1]);
776 caps = (GstCaps *) g_value_dup_boxed (&ret);
777 g_value_unset (&ret);
781 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
783 gst_caps_unref (caps);
790 GST_DEBUG_OBJECT (jitterbuffer, "could not get caps");
796 gst_rtp_jitter_buffer_chain (GstPad * pad, GstBuffer * buffer)
798 GstRtpJitterBuffer *jitterbuffer;
799 GstRtpJitterBufferPrivate *priv;
801 GstFlowReturn ret = GST_FLOW_OK;
802 GstClockTime timestamp;
806 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
808 if (!gst_rtp_buffer_validate (buffer))
811 priv = jitterbuffer->priv;
813 if (priv->last_pt != gst_rtp_buffer_get_payload_type (buffer)) {
816 priv->last_pt = gst_rtp_buffer_get_payload_type (buffer);
817 /* reset clock-rate so that we get a new one */
818 priv->clock_rate = -1;
819 /* Try to get the clock-rate from the caps first if we can. If there are no
820 * caps we must fire the signal to get the clock-rate. */
821 if ((caps = GST_BUFFER_CAPS (buffer))) {
822 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
826 if (priv->clock_rate == -1) {
829 /* no clock rate given on the caps, try to get one with the signal */
830 pt = gst_rtp_buffer_get_payload_type (buffer);
832 gst_rtp_jitter_buffer_get_clock_rate (jitterbuffer, pt);
833 if (priv->clock_rate == -1)
837 /* take the timestamp of the buffer. This is the time when the packet was
838 * received and is used to calculate jitter and clock skew. We will adjust
839 * this timestamp with the smoothed value after processing it in the
841 timestamp = GST_BUFFER_TIMESTAMP (buffer);
842 /* bring to running time */
843 timestamp = gst_segment_to_running_time (&priv->segment, GST_FORMAT_TIME,
846 seqnum = gst_rtp_buffer_get_seq (buffer);
847 GST_DEBUG_OBJECT (jitterbuffer,
848 "Received packet #%d at time %" GST_TIME_FORMAT, seqnum,
849 GST_TIME_ARGS (timestamp));
851 JBUF_LOCK_CHECK (priv, out_flushing);
852 /* don't accept more data on EOS */
856 /* let's check if this buffer is too late, we cannot accept packets with
857 * bigger seqnum than the one we already pushed. */
858 if (priv->last_popped_seqnum != -1) {
859 if (priv_compare_rtp_seq_lt (priv->last_popped_seqnum, seqnum) < 0)
863 /* let's drop oldest packet if the queue is already full and drop-on-latency
864 * is set. We can only do this when there actually is a latency. When no
865 * latency is set, we just pump it in the queue and let the other end push it
866 * out as fast as possible. */
867 if (priv->latency_ms && priv->drop_on_latency) {
870 gst_util_uint64_scale_int (priv->latency_ms, priv->clock_rate, 1000);
872 if (rtp_jitter_buffer_get_ts_diff (priv->jbuf) >= latency_ts) {
875 GST_DEBUG_OBJECT (jitterbuffer, "Queue full, dropping old packet #%d",
878 old_buf = rtp_jitter_buffer_pop (priv->jbuf);
879 gst_buffer_unref (old_buf);
883 /* now insert the packet into the queue in sorted order. This function returns
884 * FALSE if a packet with the same seqnum was already in the queue, meaning we
885 * have a duplicate. */
886 if (!rtp_jitter_buffer_insert (priv->jbuf, buffer, timestamp,
887 priv->clock_rate, &tail))
890 /* signal addition of new buffer when the _loop is waiting. */
894 /* let's unschedule and unblock any waiting buffers. We only want to do this
895 * when the tail buffer changed */
896 if (priv->clock_id && tail) {
897 GST_DEBUG_OBJECT (jitterbuffer,
898 "Unscheduling waiting buffer, new tail buffer");
899 gst_clock_id_unschedule (priv->clock_id);
902 GST_DEBUG_OBJECT (jitterbuffer, "Pushed packet #%d, now %d packets",
903 seqnum, rtp_jitter_buffer_num_packets (priv->jbuf));
908 gst_object_unref (jitterbuffer);
915 /* this is fatal and should be filtered earlier */
916 GST_ELEMENT_ERROR (jitterbuffer, STREAM, DECODE, (NULL),
917 ("Received invalid RTP payload"));
918 gst_buffer_unref (buffer);
919 gst_object_unref (jitterbuffer);
920 return GST_FLOW_ERROR;
924 GST_WARNING_OBJECT (jitterbuffer, "No clock-rate in caps!");
925 gst_buffer_unref (buffer);
926 gst_object_unref (jitterbuffer);
931 ret = priv->srcresult;
932 GST_DEBUG_OBJECT (jitterbuffer, "flushing %s", gst_flow_get_name (ret));
933 gst_buffer_unref (buffer);
938 ret = GST_FLOW_UNEXPECTED;
939 GST_WARNING_OBJECT (jitterbuffer, "we are EOS, refusing buffer");
940 gst_buffer_unref (buffer);
945 GST_WARNING_OBJECT (jitterbuffer, "Packet #%d too late as #%d was already"
946 " popped, dropping", seqnum, priv->last_popped_seqnum);
948 gst_buffer_unref (buffer);
953 GST_WARNING_OBJECT (jitterbuffer, "Duplicate packet #%d detected, dropping",
955 priv->num_duplicates++;
956 gst_buffer_unref (buffer);
962 apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
964 GstRtpJitterBufferPrivate *priv;
966 priv = jitterbuffer->priv;
971 /* apply the timestamp offset */
972 timestamp += priv->ts_offset;
978 * This funcion will push out buffers on the source pad.
980 * For each pushed buffer, the seqnum is recorded, if the next buffer B has a
981 * different seqnum (missing packets before B), this function will wait for the
982 * missing packet to arrive up to the timestamp of buffer B.
985 gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer)
987 GstRtpJitterBufferPrivate *priv;
989 GstFlowReturn result;
991 GstClockTime timestamp, out_time;
993 priv = jitterbuffer->priv;
995 JBUF_LOCK_CHECK (priv, flushing);
997 GST_DEBUG_OBJECT (jitterbuffer, "Peeking item");
1000 /* always wait if we are blocked */
1001 if (!priv->blocked) {
1002 /* if we have a packet, we can grab it */
1003 if (rtp_jitter_buffer_num_packets (priv->jbuf) > 0)
1005 /* no packets but we are EOS, do eos logic */
1009 /* wait for packets or flushing now */
1010 priv->waiting = TRUE;
1011 JBUF_WAIT_CHECK (priv, flushing);
1012 priv->waiting = FALSE;
1015 /* peek a buffer, we're just looking at the timestamp and the sequence number.
1016 * If all is fine, we'll pop and push it. If the sequence number is wrong we
1017 * wait on the timestamp. In the chain function we will unlock the wait when a
1018 * new buffer is available. The peeked buffer is valid for as long as we hold
1019 * the jitterbuffer lock. */
1020 outbuf = rtp_jitter_buffer_peek (priv->jbuf);
1021 seqnum = gst_rtp_buffer_get_seq (outbuf);
1023 /* get the timestamp, this is already corrected for clock skew by the
1025 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
1027 GST_DEBUG_OBJECT (jitterbuffer,
1028 "Peeked buffer #%d, timestamp %" GST_TIME_FORMAT ", now %d left",
1029 seqnum, GST_TIME_ARGS (timestamp),
1030 rtp_jitter_buffer_num_packets (priv->jbuf));
1032 /* apply our timestamp offset to the incomming buffer, this will be our output
1034 out_time = apply_offset (jitterbuffer, timestamp);
1036 /* If we don't know what the next seqnum should be (== -1) we have to wait
1037 * because it might be possible that we are not receiving this buffer in-order,
1038 * a buffer with a lower seqnum could arrive later and we want to push that
1039 * earlier buffer before this buffer then.
1040 * If we know the expected seqnum, we can compare it to the current seqnum to
1041 * determine if we have missing a packet. If we have a missing packet (which
1042 * must be before this packet) we can wait for it until the deadline for this
1043 * packet expires. */
1044 if ((priv->next_seqnum == -1 || priv->next_seqnum != seqnum)
1045 && out_time != -1) {
1047 GstClockTime sync_time;
1051 if (priv->next_seqnum != -1) {
1052 /* we expected next_seqnum but received something else, that's a gap */
1053 GST_WARNING_OBJECT (jitterbuffer,
1054 "Sequence number GAP detected: expected %d instead of %d",
1055 priv->next_seqnum, seqnum);
1057 /* we don't know what the next_seqnum should be, wait for the last
1058 * possible moment to push this buffer, maybe we get an earlier seqnum
1060 GST_DEBUG_OBJECT (jitterbuffer, "First buffer %d, do sync", seqnum);
1063 GST_OBJECT_LOCK (jitterbuffer);
1064 clock = GST_ELEMENT_CLOCK (jitterbuffer);
1066 GST_OBJECT_UNLOCK (jitterbuffer);
1067 /* let's just push if there is no clock */
1071 GST_DEBUG_OBJECT (jitterbuffer, "sync to timestamp %" GST_TIME_FORMAT,
1072 GST_TIME_ARGS (out_time));
1074 /* prepare for sync against clock */
1075 sync_time = out_time + GST_ELEMENT_CAST (jitterbuffer)->base_time;
1076 /* add latency, this includes our own latency and the peer latency. */
1077 sync_time += (priv->latency_ms * GST_MSECOND);
1078 sync_time += priv->peer_latency;
1080 /* create an entry for the clock */
1081 id = priv->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
1082 GST_OBJECT_UNLOCK (jitterbuffer);
1084 /* release the lock so that the other end can push stuff or unlock */
1087 ret = gst_clock_id_wait (id, NULL);
1090 /* and free the entry */
1091 gst_clock_id_unref (id);
1092 priv->clock_id = NULL;
1094 /* at this point, the clock could have been unlocked by a timeout, a new
1095 * tail element was added to the queue or because we are shutting down. Check
1096 * for shutdown first. */
1097 if (priv->srcresult != GST_FLOW_OK)
1100 /* if we got unscheduled and we are not flushing, it's because a new tail
1101 * element became available in the queue. Grab it and try to push or sync. */
1102 if (ret == GST_CLOCK_UNSCHEDULED) {
1103 GST_DEBUG_OBJECT (jitterbuffer,
1104 "Wait got unscheduled, will retry to push with new buffer");
1107 /* Get new timestamp, latency might have changed */
1108 out_time = apply_offset (jitterbuffer, timestamp);
1111 /* when we get here we are ready to pop and push the buffer */
1112 outbuf = rtp_jitter_buffer_pop (priv->jbuf);
1114 /* check if we are pushing something unexpected */
1115 if (priv->next_seqnum != -1 && priv->next_seqnum != seqnum) {
1118 /* calc number of missing packets, careful for wraparounds */
1119 dropped = priv_compare_rtp_seq_lt (priv->next_seqnum, seqnum);
1121 GST_DEBUG_OBJECT (jitterbuffer,
1122 "Pushing DISCONT after dropping %d (%d to %d)", dropped,
1123 priv->next_seqnum, seqnum);
1126 priv->num_late += dropped;
1128 /* set DISCONT flag when we missed a packet. */
1129 outbuf = gst_buffer_make_metadata_writable (outbuf);
1130 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1133 /* apply timestamp with offset to buffer now */
1134 GST_BUFFER_TIMESTAMP (outbuf) = out_time;
1136 /* now we are ready to push the buffer. Save the seqnum and release the lock
1137 * so the other end can push stuff in the queue again. */
1138 priv->last_popped_seqnum = seqnum;
1139 priv->next_seqnum = (seqnum + 1) & 0xffff;
1143 GST_DEBUG_OBJECT (jitterbuffer,
1144 "Pushing buffer %d, timestamp %" GST_TIME_FORMAT, seqnum,
1145 GST_TIME_ARGS (out_time));
1146 result = gst_pad_push (priv->srcpad, outbuf);
1147 if (result != GST_FLOW_OK)
1155 /* store result, we are flushing now */
1156 GST_DEBUG_OBJECT (jitterbuffer, "We are EOS, pushing EOS downstream");
1157 priv->srcresult = GST_FLOW_UNEXPECTED;
1158 gst_pad_pause_task (priv->srcpad);
1159 gst_pad_push_event (priv->srcpad, gst_event_new_eos ());
1165 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1166 gst_pad_pause_task (priv->srcpad);
1172 const gchar *reason = gst_flow_get_name (result);
1174 GST_DEBUG_OBJECT (jitterbuffer, "pausing task, reason %s", reason);
1178 priv->srcresult = result;
1179 /* we don't post errors or anything because upstream will do that for us
1180 * when we pass the return value upstream. */
1181 gst_pad_pause_task (priv->srcpad);
1188 gst_rtp_jitter_buffer_query (GstPad * pad, GstQuery * query)
1190 GstRtpJitterBuffer *jitterbuffer;
1191 GstRtpJitterBufferPrivate *priv;
1192 gboolean res = FALSE;
1194 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
1195 priv = jitterbuffer->priv;
1197 switch (GST_QUERY_TYPE (query)) {
1198 case GST_QUERY_LATENCY:
1200 /* We need to send the query upstream and add the returned latency to our
1202 GstClockTime min_latency, max_latency;
1204 GstClockTime our_latency;
1206 if ((res = gst_pad_peer_query (priv->sinkpad, query))) {
1207 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1209 GST_DEBUG_OBJECT (jitterbuffer, "Peer latency: min %"
1210 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1211 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1213 /* store this so that we can safely sync on the peer buffers. */
1215 priv->peer_latency = min_latency;
1216 our_latency = ((guint64) priv->latency_ms) * GST_MSECOND;
1219 GST_DEBUG_OBJECT (jitterbuffer, "Our latency: %" GST_TIME_FORMAT,
1220 GST_TIME_ARGS (our_latency));
1222 /* we add some latency but can buffer an infinite amount of time */
1223 min_latency += our_latency;
1226 GST_DEBUG_OBJECT (jitterbuffer, "Calculated total latency : min %"
1227 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1228 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1230 gst_query_set_latency (query, TRUE, min_latency, max_latency);
1235 res = gst_pad_query_default (pad, query);
1239 gst_object_unref (jitterbuffer);
1245 gst_rtp_jitter_buffer_set_property (GObject * object,
1246 guint prop_id, const GValue * value, GParamSpec * pspec)
1248 GstRtpJitterBuffer *jitterbuffer;
1249 GstRtpJitterBufferPrivate *priv;
1251 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
1252 priv = jitterbuffer->priv;
1257 guint new_latency, old_latency;
1259 new_latency = g_value_get_uint (value);
1262 old_latency = priv->latency_ms;
1263 priv->latency_ms = new_latency;
1266 /* post message if latency changed, this will inform the parent pipeline
1267 * that a latency reconfiguration is possible/needed. */
1268 if (new_latency != old_latency) {
1269 GST_DEBUG_OBJECT (jitterbuffer, "latency changed to: %" GST_TIME_FORMAT,
1270 GST_TIME_ARGS (new_latency * GST_MSECOND));
1272 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer),
1273 gst_message_new_latency (GST_OBJECT_CAST (jitterbuffer)));
1277 case PROP_DROP_ON_LATENCY:
1278 priv->drop_on_latency = g_value_get_boolean (value);
1280 case PROP_TS_OFFSET:
1282 priv->ts_offset = g_value_get_int64 (value);
1286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1292 gst_rtp_jitter_buffer_get_property (GObject * object,
1293 guint prop_id, GValue * value, GParamSpec * pspec)
1295 GstRtpJitterBuffer *jitterbuffer;
1296 GstRtpJitterBufferPrivate *priv;
1298 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
1299 priv = jitterbuffer->priv;
1304 g_value_set_uint (value, priv->latency_ms);
1307 case PROP_DROP_ON_LATENCY:
1308 g_value_set_boolean (value, priv->drop_on_latency);
1310 case PROP_TS_OFFSET:
1312 g_value_set_int64 (value, priv->ts_offset);
1316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);