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
29 * This element reorders and removes duplicate RTP packets as they are received
30 * from a network source. It will also wait for missing packets up to a
31 * configurable time limit using the #GstRtpJitterBuffer:latency property.
32 * Packets arriving too late are considered to be lost packets.
34 * This element acts as a live element and so adds #GstRtpJitterBuffer:latency
37 * The element needs the clock-rate of the RTP payload in order to estimate the
38 * delay. This information is obtained either from the caps on the sink pad or,
39 * when no caps are present, from the #GstRtpJitterBuffer::request-pt-map signal.
40 * To clear the previous pt-map use the #GstRtpJitterBuffer::clear-pt-map signal.
42 * This element will automatically be used inside gstrtpbin.
45 * <title>Example pipelines</title>
47 * gst-launch rtspsrc location=rtsp://192.168.1.133:8554/mpeg1or2AudioVideoTest ! gstrtpjitterbuffer ! rtpmpvdepay ! mpeg2dec ! xvimagesink
48 * ]| Connect to a streaming server and decode the MPEG video. The jitterbuffer is
49 * inserted into the pipeline to smooth out network jitter and to reorder the
50 * out-of-order RTP packets.
53 * Last reviewed on 2007-05-28 (0.10.5)
62 #include <gst/rtp/gstrtpbuffer.h>
64 #include "gstrtpbin-marshal.h"
66 #include "gstrtpjitterbuffer.h"
67 #include "rtpjitterbuffer.h"
69 GST_DEBUG_CATEGORY (rtpjitterbuffer_debug);
70 #define GST_CAT_DEFAULT (rtpjitterbuffer_debug)
72 /* low and high threshold tell the queue when to start and stop buffering */
73 #define LOW_THRESHOLD 0.2
74 #define HIGH_THRESHOLD 0.8
76 /* elementfactory information */
77 static const GstElementDetails gst_rtp_jitter_buffer_details =
78 GST_ELEMENT_DETAILS ("RTP packet jitter-buffer",
80 "A buffer that deals with network jitter and other transmission faults",
81 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
82 "Wim Taymans <wim.taymans@gmail.com>");
84 /* RTPJitterBuffer signals and args */
87 SIGNAL_REQUEST_PT_MAP,
92 #define DEFAULT_LATENCY_MS 200
93 #define DEFAULT_DROP_ON_LATENCY FALSE
94 #define DEFAULT_TS_OFFSET 0
95 #define DEFAULT_DO_LOST FALSE
101 PROP_DROP_ON_LATENCY,
107 #define JBUF_LOCK(priv) (g_mutex_lock ((priv)->jbuf_lock))
109 #define JBUF_LOCK_CHECK(priv,label) G_STMT_START { \
111 if (priv->srcresult != GST_FLOW_OK) \
115 #define JBUF_UNLOCK(priv) (g_mutex_unlock ((priv)->jbuf_lock))
116 #define JBUF_WAIT(priv) (g_cond_wait ((priv)->jbuf_cond, (priv)->jbuf_lock))
118 #define JBUF_WAIT_CHECK(priv,label) G_STMT_START { \
120 if (priv->srcresult != GST_FLOW_OK) \
124 #define JBUF_SIGNAL(priv) (g_cond_signal ((priv)->jbuf_cond))
126 struct _GstRtpJitterBufferPrivate
128 GstPad *sinkpad, *srcpad;
130 RTPJitterBuffer *jbuf;
138 gboolean drop_on_latency;
142 /* the last seqnum we pushed out */
143 guint32 last_popped_seqnum;
144 /* the next expected seqnum */
146 /* last output time */
147 GstClockTime last_out_time;
152 /* clock rate and rtp timestamp offset */
156 gint64 prev_ts_offset;
158 /* when we are shutting down */
159 GstFlowReturn srcresult;
165 /* the latency of the upstream peer, we have to take this into account when
166 * synchronizing the buffers. */
167 GstClockTime peer_latency;
169 /* some accounting */
171 guint64 num_duplicates;
174 #define GST_RTP_JITTER_BUFFER_GET_PRIVATE(o) \
175 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_RTP_JITTER_BUFFER, \
176 GstRtpJitterBufferPrivate))
178 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_template =
179 GST_STATIC_PAD_TEMPLATE ("sink",
182 GST_STATIC_CAPS ("application/x-rtp, "
183 "clock-rate = (int) [ 1, 2147483647 ]"
184 /* "payload = (int) , "
185 * "encoding-name = (string) "
189 static GstStaticPadTemplate gst_rtp_jitter_buffer_src_template =
190 GST_STATIC_PAD_TEMPLATE ("src",
193 GST_STATIC_CAPS ("application/x-rtp"
194 /* "payload = (int) , "
195 * "clock-rate = (int) , "
196 * "encoding-name = (string) "
200 static guint gst_rtp_jitter_buffer_signals[LAST_SIGNAL] = { 0 };
202 GST_BOILERPLATE (GstRtpJitterBuffer, gst_rtp_jitter_buffer, GstElement,
205 /* object overrides */
206 static void gst_rtp_jitter_buffer_set_property (GObject * object,
207 guint prop_id, const GValue * value, GParamSpec * pspec);
208 static void gst_rtp_jitter_buffer_get_property (GObject * object,
209 guint prop_id, GValue * value, GParamSpec * pspec);
210 static void gst_rtp_jitter_buffer_finalize (GObject * object);
212 /* element overrides */
213 static GstStateChangeReturn gst_rtp_jitter_buffer_change_state (GstElement
214 * element, GstStateChange transition);
217 static GstCaps *gst_rtp_jitter_buffer_getcaps (GstPad * pad);
219 /* sinkpad overrides */
220 static gboolean gst_jitter_buffer_sink_setcaps (GstPad * pad, GstCaps * caps);
221 static gboolean gst_rtp_jitter_buffer_src_event (GstPad * pad,
223 static gboolean gst_rtp_jitter_buffer_sink_event (GstPad * pad,
225 static GstFlowReturn gst_rtp_jitter_buffer_chain (GstPad * pad,
228 /* srcpad overrides */
230 gst_rtp_jitter_buffer_src_activate_push (GstPad * pad, gboolean active);
231 static void gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer);
232 static gboolean gst_rtp_jitter_buffer_query (GstPad * pad, GstQuery * query);
235 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer);
238 gst_rtp_jitter_buffer_base_init (gpointer klass)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
242 gst_element_class_add_pad_template (element_class,
243 gst_static_pad_template_get (&gst_rtp_jitter_buffer_src_template));
244 gst_element_class_add_pad_template (element_class,
245 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_template));
246 gst_element_class_set_details (element_class, &gst_rtp_jitter_buffer_details);
250 gst_rtp_jitter_buffer_class_init (GstRtpJitterBufferClass * klass)
252 GObjectClass *gobject_class;
253 GstElementClass *gstelement_class;
255 gobject_class = (GObjectClass *) klass;
256 gstelement_class = (GstElementClass *) klass;
258 g_type_class_add_private (klass, sizeof (GstRtpJitterBufferPrivate));
260 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_finalize);
262 gobject_class->set_property = gst_rtp_jitter_buffer_set_property;
263 gobject_class->get_property = gst_rtp_jitter_buffer_get_property;
266 * GstRtpJitterBuffer::latency:
268 * The maximum latency of the jitterbuffer. Packets will be kept in the buffer
269 * for at most this time.
271 g_object_class_install_property (gobject_class, PROP_LATENCY,
272 g_param_spec_uint ("latency", "Buffer latency in ms",
273 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
276 * GstRtpJitterBuffer::drop-on-latency:
278 * Drop oldest buffers when the queue is completely filled.
280 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
281 g_param_spec_boolean ("drop-on-latency",
282 "Drop buffers when maximum latency is reached",
283 "Tells the jitterbuffer to never exceed the given latency in size",
284 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE));
286 * GstRtpJitterBuffer::ts-offset:
288 * Adjust GStreamer output buffer timestamps in the jitterbuffer with offset.
289 * This is mainly used to ensure interstream synchronisation.
291 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
292 g_param_spec_int64 ("ts-offset", "Timestamp Offset",
293 "Adjust buffer timestamps with offset in nanoseconds", G_MININT64,
294 G_MAXINT64, DEFAULT_TS_OFFSET,
295 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 * GstRtpJitterBuffer::do-lost:
300 * Send out a GstRTPPacketLost event downstream when a packet is considered
303 g_object_class_install_property (gobject_class, PROP_DO_LOST,
304 g_param_spec_boolean ("do-lost", "Do Lost",
305 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
306 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 * GstRtpJitterBuffer::request-pt-map:
309 * @buffer: the object which received the signal
312 * Request the payload type as #GstCaps for @pt.
314 gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP] =
315 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
316 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
317 request_pt_map), NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT,
318 GST_TYPE_CAPS, 1, G_TYPE_UINT);
320 * GstRtpJitterBuffer::clear-pt-map:
321 * @buffer: the object which received the signal
323 * Invalidate the clock-rate as obtained with the
324 * #GstRtpJitterBuffer::request-pt-map signal.
326 gst_rtp_jitter_buffer_signals[SIGNAL_CLEAR_PT_MAP] =
327 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
328 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
329 clear_pt_map), NULL, NULL, g_cclosure_marshal_VOID__VOID,
330 G_TYPE_NONE, 0, G_TYPE_NONE);
332 gstelement_class->change_state = gst_rtp_jitter_buffer_change_state;
334 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_clear_pt_map);
336 GST_DEBUG_CATEGORY_INIT
337 (rtpjitterbuffer_debug, "gstrtpjitterbuffer", 0, "RTP Jitter Buffer");
341 gst_rtp_jitter_buffer_init (GstRtpJitterBuffer * jitterbuffer,
342 GstRtpJitterBufferClass * klass)
344 GstRtpJitterBufferPrivate *priv;
346 priv = GST_RTP_JITTER_BUFFER_GET_PRIVATE (jitterbuffer);
347 jitterbuffer->priv = priv;
349 priv->latency_ms = DEFAULT_LATENCY_MS;
350 priv->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
351 priv->do_lost = DEFAULT_DO_LOST;
353 priv->jbuf = rtp_jitter_buffer_new ();
354 priv->jbuf_lock = g_mutex_new ();
355 priv->jbuf_cond = g_cond_new ();
358 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_src_template,
361 gst_pad_set_activatepush_function (priv->srcpad,
362 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_activate_push));
363 gst_pad_set_query_function (priv->srcpad,
364 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_query));
365 gst_pad_set_getcaps_function (priv->srcpad,
366 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_getcaps));
367 gst_pad_set_event_function (priv->srcpad,
368 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_event));
371 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_sink_template,
374 gst_pad_set_chain_function (priv->sinkpad,
375 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_chain));
376 gst_pad_set_event_function (priv->sinkpad,
377 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_event));
378 gst_pad_set_setcaps_function (priv->sinkpad,
379 GST_DEBUG_FUNCPTR (gst_jitter_buffer_sink_setcaps));
380 gst_pad_set_getcaps_function (priv->sinkpad,
381 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_getcaps));
383 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->srcpad);
384 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->sinkpad);
388 gst_rtp_jitter_buffer_finalize (GObject * object)
390 GstRtpJitterBuffer *jitterbuffer;
392 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
394 g_mutex_free (jitterbuffer->priv->jbuf_lock);
395 g_cond_free (jitterbuffer->priv->jbuf_cond);
397 g_object_unref (jitterbuffer->priv->jbuf);
399 G_OBJECT_CLASS (parent_class)->finalize (object);
403 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer)
405 GstRtpJitterBufferPrivate *priv;
407 priv = jitterbuffer->priv;
409 /* this will trigger a new pt-map request signal, FIXME, do something better. */
410 priv->clock_rate = -1;
414 gst_rtp_jitter_buffer_getcaps (GstPad * pad)
416 GstRtpJitterBuffer *jitterbuffer;
417 GstRtpJitterBufferPrivate *priv;
420 const GstCaps *templ;
422 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
423 priv = jitterbuffer->priv;
425 other = (pad == priv->srcpad ? priv->sinkpad : priv->srcpad);
427 caps = gst_pad_peer_get_caps (other);
429 templ = gst_pad_get_pad_template_caps (pad);
431 GST_DEBUG_OBJECT (jitterbuffer, "copy template");
432 caps = gst_caps_copy (templ);
436 GST_DEBUG_OBJECT (jitterbuffer, "intersect with template");
438 intersect = gst_caps_intersect (caps, templ);
439 gst_caps_unref (caps);
443 gst_object_unref (jitterbuffer);
449 gst_jitter_buffer_sink_parse_caps (GstRtpJitterBuffer * jitterbuffer,
452 GstRtpJitterBufferPrivate *priv;
453 GstStructure *caps_struct;
456 priv = jitterbuffer->priv;
458 /* first parse the caps */
459 caps_struct = gst_caps_get_structure (caps, 0);
461 GST_DEBUG_OBJECT (jitterbuffer, "got caps");
463 /* we need a clock-rate to convert the rtp timestamps to GStreamer time and to
464 * measure the amount of data in the buffer */
465 if (!gst_structure_get_int (caps_struct, "clock-rate", &priv->clock_rate))
468 if (priv->clock_rate <= 0)
471 GST_DEBUG_OBJECT (jitterbuffer, "got clock-rate %d", priv->clock_rate);
473 /* gah, clock-base is uint. If we don't have a base, we will use the first
474 * buffer timestamp as the base time. This will screw up sync but it's better
476 if (gst_structure_get_uint (caps_struct, "clock-base", &val))
477 priv->clock_base = val;
479 priv->clock_base = -1;
481 GST_DEBUG_OBJECT (jitterbuffer, "got clock-base %" G_GINT64_FORMAT,
484 /* first expected seqnum */
485 if (gst_structure_get_uint (caps_struct, "seqnum-base", &val))
486 priv->next_seqnum = val;
488 priv->next_seqnum = -1;
490 GST_DEBUG_OBJECT (jitterbuffer, "got seqnum-base %d", priv->next_seqnum);
497 GST_DEBUG_OBJECT (jitterbuffer, "No clock-rate in caps!");
502 GST_DEBUG_OBJECT (jitterbuffer, "Invalid clock-rate %d", priv->clock_rate);
508 gst_jitter_buffer_sink_setcaps (GstPad * pad, GstCaps * caps)
510 GstRtpJitterBuffer *jitterbuffer;
511 GstRtpJitterBufferPrivate *priv;
514 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
515 priv = jitterbuffer->priv;
517 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
519 /* set same caps on srcpad on success */
521 gst_pad_set_caps (priv->srcpad, caps);
523 gst_object_unref (jitterbuffer);
529 gst_rtp_jitter_buffer_flush_start (GstRtpJitterBuffer * jitterbuffer)
531 GstRtpJitterBufferPrivate *priv;
533 priv = jitterbuffer->priv;
536 /* mark ourselves as flushing */
537 priv->srcresult = GST_FLOW_WRONG_STATE;
538 GST_DEBUG_OBJECT (jitterbuffer, "Disabling pop on queue");
539 /* this unblocks any waiting pops on the src pad task */
541 /* unlock clock, we just unschedule, the entry will be released by the
542 * locking streaming thread. */
544 gst_clock_id_unschedule (priv->clock_id);
549 gst_rtp_jitter_buffer_flush_stop (GstRtpJitterBuffer * jitterbuffer)
551 GstRtpJitterBufferPrivate *priv;
553 priv = jitterbuffer->priv;
556 GST_DEBUG_OBJECT (jitterbuffer, "Enabling pop on queue");
557 /* Mark as non flushing */
558 priv->srcresult = GST_FLOW_OK;
559 gst_segment_init (&priv->segment, GST_FORMAT_TIME);
560 priv->last_popped_seqnum = -1;
561 priv->last_out_time = -1;
562 priv->next_seqnum = -1;
563 priv->clock_rate = -1;
565 rtp_jitter_buffer_flush (priv->jbuf);
566 rtp_jitter_buffer_reset_skew (priv->jbuf);
571 gst_rtp_jitter_buffer_src_activate_push (GstPad * pad, gboolean active)
573 gboolean result = TRUE;
574 GstRtpJitterBuffer *jitterbuffer = NULL;
576 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
579 /* allow data processing */
580 gst_rtp_jitter_buffer_flush_stop (jitterbuffer);
582 /* start pushing out buffers */
583 GST_DEBUG_OBJECT (jitterbuffer, "Starting task on srcpad");
584 gst_pad_start_task (jitterbuffer->priv->srcpad,
585 (GstTaskFunction) gst_rtp_jitter_buffer_loop, jitterbuffer);
587 /* make sure all data processing stops ASAP */
588 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
590 /* NOTE this will hardlock if the state change is called from the src pad
591 * task thread because we will _join() the thread. */
592 GST_DEBUG_OBJECT (jitterbuffer, "Stopping task on srcpad");
593 result = gst_pad_stop_task (pad);
596 gst_object_unref (jitterbuffer);
601 static GstStateChangeReturn
602 gst_rtp_jitter_buffer_change_state (GstElement * element,
603 GstStateChange transition)
605 GstRtpJitterBuffer *jitterbuffer;
606 GstRtpJitterBufferPrivate *priv;
607 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
609 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
610 priv = jitterbuffer->priv;
612 switch (transition) {
613 case GST_STATE_CHANGE_NULL_TO_READY:
615 case GST_STATE_CHANGE_READY_TO_PAUSED:
617 /* reset negotiated values */
618 priv->clock_rate = -1;
619 priv->clock_base = -1;
620 priv->peer_latency = 0;
622 /* block until we go to PLAYING */
623 priv->blocked = TRUE;
624 /* reset skew detection initialy */
625 rtp_jitter_buffer_reset_skew (priv->jbuf);
628 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
630 /* unblock to allow streaming in PLAYING */
631 priv->blocked = FALSE;
639 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
641 switch (transition) {
642 case GST_STATE_CHANGE_READY_TO_PAUSED:
643 /* we are a live element because we sync to the clock, which we can only
644 * do in the PLAYING state */
645 if (ret != GST_STATE_CHANGE_FAILURE)
646 ret = GST_STATE_CHANGE_NO_PREROLL;
648 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
650 /* block to stop streaming when PAUSED */
651 priv->blocked = TRUE;
653 if (ret != GST_STATE_CHANGE_FAILURE)
654 ret = GST_STATE_CHANGE_NO_PREROLL;
656 case GST_STATE_CHANGE_PAUSED_TO_READY:
658 case GST_STATE_CHANGE_READY_TO_NULL:
668 gst_rtp_jitter_buffer_src_event (GstPad * pad, GstEvent * event)
671 GstRtpJitterBuffer *jitterbuffer;
672 GstRtpJitterBufferPrivate *priv;
674 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
675 priv = jitterbuffer->priv;
677 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
679 switch (GST_EVENT_TYPE (event)) {
681 ret = gst_pad_push_event (priv->sinkpad, event);
684 gst_object_unref (jitterbuffer);
690 gst_rtp_jitter_buffer_sink_event (GstPad * pad, GstEvent * event)
693 GstRtpJitterBuffer *jitterbuffer;
694 GstRtpJitterBufferPrivate *priv;
696 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
697 priv = jitterbuffer->priv;
699 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
701 switch (GST_EVENT_TYPE (event)) {
702 case GST_EVENT_NEWSEGMENT:
706 gint64 start, stop, time;
709 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
710 &start, &stop, &time);
712 /* we need time for now */
713 if (format != GST_FORMAT_TIME)
714 goto newseg_wrong_format;
716 GST_DEBUG_OBJECT (jitterbuffer,
717 "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
718 ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
719 update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
720 GST_TIME_ARGS (time));
722 /* now configure the values, we need these to time the release of the
723 * buffers on the srcpad. */
724 gst_segment_set_newsegment_full (&priv->segment, update,
725 rate, arate, format, start, stop, time);
727 /* FIXME, push SEGMENT in the queue. Sorting order might be difficult. */
728 ret = gst_pad_push_event (priv->srcpad, event);
731 case GST_EVENT_FLUSH_START:
732 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
733 ret = gst_pad_push_event (priv->srcpad, event);
735 case GST_EVENT_FLUSH_STOP:
736 ret = gst_pad_push_event (priv->srcpad, event);
737 ret = gst_rtp_jitter_buffer_src_activate_push (priv->srcpad, TRUE);
741 /* push EOS in queue. We always push it at the head */
743 /* check for flushing, we need to discard the event and return FALSE when
745 ret = priv->srcresult == GST_FLOW_OK;
746 if (ret && !priv->eos) {
747 GST_DEBUG_OBJECT (jitterbuffer, "queuing EOS");
750 } else if (priv->eos) {
751 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, we are already EOS");
753 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, reason %s",
754 gst_flow_get_name (priv->srcresult));
757 gst_event_unref (event);
761 ret = gst_pad_push_event (priv->srcpad, event);
766 gst_object_unref (jitterbuffer);
773 GST_DEBUG_OBJECT (jitterbuffer, "received non TIME newsegment");
780 gst_rtp_jitter_buffer_get_clock_rate (GstRtpJitterBuffer * jitterbuffer,
784 GValue args[2] = { {0}, {0} };
788 g_value_init (&args[0], GST_TYPE_ELEMENT);
789 g_value_set_object (&args[0], jitterbuffer);
790 g_value_init (&args[1], G_TYPE_UINT);
791 g_value_set_uint (&args[1], pt);
793 g_value_init (&ret, GST_TYPE_CAPS);
794 g_value_set_boxed (&ret, NULL);
796 g_signal_emitv (args, gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP], 0,
799 g_value_unset (&args[0]);
800 g_value_unset (&args[1]);
801 caps = (GstCaps *) g_value_dup_boxed (&ret);
802 g_value_unset (&ret);
806 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
808 gst_caps_unref (caps);
815 GST_DEBUG_OBJECT (jitterbuffer, "could not get caps");
821 gst_rtp_jitter_buffer_chain (GstPad * pad, GstBuffer * buffer)
823 GstRtpJitterBuffer *jitterbuffer;
824 GstRtpJitterBufferPrivate *priv;
826 GstFlowReturn ret = GST_FLOW_OK;
827 GstClockTime timestamp;
831 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
833 if (!gst_rtp_buffer_validate (buffer))
836 priv = jitterbuffer->priv;
838 if (priv->last_pt != gst_rtp_buffer_get_payload_type (buffer)) {
841 priv->last_pt = gst_rtp_buffer_get_payload_type (buffer);
842 /* reset clock-rate so that we get a new one */
843 priv->clock_rate = -1;
844 /* Try to get the clock-rate from the caps first if we can. If there are no
845 * caps we must fire the signal to get the clock-rate. */
846 if ((caps = GST_BUFFER_CAPS (buffer))) {
847 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
851 if (priv->clock_rate == -1) {
854 /* no clock rate given on the caps, try to get one with the signal */
855 pt = gst_rtp_buffer_get_payload_type (buffer);
857 gst_rtp_jitter_buffer_get_clock_rate (jitterbuffer, pt);
858 if (priv->clock_rate == -1)
862 /* take the timestamp of the buffer. This is the time when the packet was
863 * received and is used to calculate jitter and clock skew. We will adjust
864 * this timestamp with the smoothed value after processing it in the
866 timestamp = GST_BUFFER_TIMESTAMP (buffer);
867 /* bring to running time */
868 timestamp = gst_segment_to_running_time (&priv->segment, GST_FORMAT_TIME,
871 seqnum = gst_rtp_buffer_get_seq (buffer);
872 GST_DEBUG_OBJECT (jitterbuffer,
873 "Received packet #%d at time %" GST_TIME_FORMAT, seqnum,
874 GST_TIME_ARGS (timestamp));
876 JBUF_LOCK_CHECK (priv, out_flushing);
877 /* don't accept more data on EOS */
881 /* let's check if this buffer is too late, we can only accept packets with
882 * bigger seqnum than the one we last pushed. */
883 if (priv->last_popped_seqnum != -1) {
886 gap = gst_rtp_buffer_compare_seqnum (priv->last_popped_seqnum, seqnum);
889 /* priv->last_popped_seqnum >= seqnum, this packet is too late or the
890 * sender might have been restarted with different seqnum. */
892 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too old %d", gap);
893 priv->last_popped_seqnum = -1;
894 priv->next_seqnum = -1;
899 /* priv->last_popped_seqnum < seqnum, this is a new packet */
901 GST_DEBUG_OBJECT (jitterbuffer, "reset: too many dropped packets %d",
903 priv->last_popped_seqnum = -1;
904 priv->next_seqnum = -1;
909 /* let's drop oldest packet if the queue is already full and drop-on-latency
910 * is set. We can only do this when there actually is a latency. When no
911 * latency is set, we just pump it in the queue and let the other end push it
912 * out as fast as possible. */
913 if (priv->latency_ms && priv->drop_on_latency) {
916 gst_util_uint64_scale_int (priv->latency_ms, priv->clock_rate, 1000);
918 if (rtp_jitter_buffer_get_ts_diff (priv->jbuf) >= latency_ts) {
921 old_buf = rtp_jitter_buffer_pop (priv->jbuf);
923 GST_DEBUG_OBJECT (jitterbuffer, "Queue full, dropping old packet #%d",
924 gst_rtp_buffer_get_seq (old_buf));
926 gst_buffer_unref (old_buf);
930 /* we need to make the metadata writable before pushing it in the jitterbuffer
931 * because the jitterbuffer will update the timestamp */
932 buffer = gst_buffer_make_metadata_writable (buffer);
934 /* now insert the packet into the queue in sorted order. This function returns
935 * FALSE if a packet with the same seqnum was already in the queue, meaning we
936 * have a duplicate. */
937 if (!rtp_jitter_buffer_insert (priv->jbuf, buffer, timestamp,
938 priv->clock_rate, &tail))
941 /* signal addition of new buffer when the _loop is waiting. */
945 /* let's unschedule and unblock any waiting buffers. We only want to do this
946 * when the tail buffer changed */
947 if (priv->clock_id && tail) {
948 GST_DEBUG_OBJECT (jitterbuffer,
949 "Unscheduling waiting buffer, new tail buffer");
950 gst_clock_id_unschedule (priv->clock_id);
953 GST_DEBUG_OBJECT (jitterbuffer, "Pushed packet #%d, now %d packets",
954 seqnum, rtp_jitter_buffer_num_packets (priv->jbuf));
959 gst_object_unref (jitterbuffer);
966 /* this is not fatal but should be filtered earlier */
967 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
968 ("Received invalid RTP payload, dropping"));
969 gst_buffer_unref (buffer);
970 gst_object_unref (jitterbuffer);
975 GST_WARNING_OBJECT (jitterbuffer, "No clock-rate in caps!");
976 gst_buffer_unref (buffer);
977 gst_object_unref (jitterbuffer);
982 ret = priv->srcresult;
983 GST_DEBUG_OBJECT (jitterbuffer, "flushing %s", gst_flow_get_name (ret));
984 gst_buffer_unref (buffer);
989 ret = GST_FLOW_UNEXPECTED;
990 GST_WARNING_OBJECT (jitterbuffer, "we are EOS, refusing buffer");
991 gst_buffer_unref (buffer);
996 GST_WARNING_OBJECT (jitterbuffer, "Packet #%d too late as #%d was already"
997 " popped, dropping", seqnum, priv->last_popped_seqnum);
999 gst_buffer_unref (buffer);
1004 GST_WARNING_OBJECT (jitterbuffer, "Duplicate packet #%d detected, dropping",
1006 priv->num_duplicates++;
1007 gst_buffer_unref (buffer);
1013 apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
1015 GstRtpJitterBufferPrivate *priv;
1017 priv = jitterbuffer->priv;
1019 if (timestamp == -1)
1022 /* apply the timestamp offset */
1023 timestamp += priv->ts_offset;
1029 * This funcion will push out buffers on the source pad.
1031 * For each pushed buffer, the seqnum is recorded, if the next buffer B has a
1032 * different seqnum (missing packets before B), this function will wait for the
1033 * missing packet to arrive up to the timestamp of buffer B.
1036 gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer)
1038 GstRtpJitterBufferPrivate *priv;
1040 GstFlowReturn result;
1042 guint32 next_seqnum;
1043 GstClockTime timestamp, out_time;
1044 gboolean discont = FALSE;
1047 priv = jitterbuffer->priv;
1049 JBUF_LOCK_CHECK (priv, flushing);
1051 GST_DEBUG_OBJECT (jitterbuffer, "Peeking item");
1053 /* always wait if we are blocked */
1054 if (!priv->blocked) {
1055 /* if we have a packet, we can exit the loop and grab it */
1056 if (rtp_jitter_buffer_num_packets (priv->jbuf) > 0)
1058 /* no packets but we are EOS, do eos logic */
1062 /* underrun, wait for packets or flushing now */
1063 priv->waiting = TRUE;
1064 JBUF_WAIT_CHECK (priv, flushing);
1065 priv->waiting = FALSE;
1068 /* peek a buffer, we're just looking at the timestamp and the sequence number.
1069 * If all is fine, we'll pop and push it. If the sequence number is wrong we
1070 * wait on the timestamp. In the chain function we will unlock the wait when a
1071 * new buffer is available. The peeked buffer is valid for as long as we hold
1072 * the jitterbuffer lock. */
1073 outbuf = rtp_jitter_buffer_peek (priv->jbuf);
1075 /* get the seqnum and the next expected seqnum */
1076 seqnum = gst_rtp_buffer_get_seq (outbuf);
1077 next_seqnum = priv->next_seqnum;
1079 /* get the timestamp, this is already corrected for clock skew by the
1081 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
1083 GST_DEBUG_OBJECT (jitterbuffer,
1084 "Peeked buffer #%d, expect #%d, timestamp %" GST_TIME_FORMAT
1085 ", now %d left", seqnum, next_seqnum, GST_TIME_ARGS (timestamp),
1086 rtp_jitter_buffer_num_packets (priv->jbuf));
1088 /* apply our timestamp offset to the incomming buffer, this will be our output
1090 out_time = apply_offset (jitterbuffer, timestamp);
1092 /* get the gap between this and the previous packet. If we don't know the
1093 * previous packet seqnum assume no gap. */
1094 if (next_seqnum != -1) {
1095 gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum);
1097 /* if we have a packet that we already pushed or considered dropped, pop it
1098 * off and get the next packet */
1100 GST_DEBUG_OBJECT (jitterbuffer, "Old packet #%d, next #%d dropping",
1101 seqnum, next_seqnum);
1102 outbuf = rtp_jitter_buffer_pop (priv->jbuf);
1103 gst_buffer_unref (outbuf);
1107 GST_DEBUG_OBJECT (jitterbuffer, "no next seqnum known, first packet");
1111 /* If we don't know what the next seqnum should be (== -1) we have to wait
1112 * because it might be possible that we are not receiving this buffer in-order,
1113 * a buffer with a lower seqnum could arrive later and we want to push that
1114 * earlier buffer before this buffer then.
1115 * If we know the expected seqnum, we can compare it to the current seqnum to
1116 * determine if we have missing a packet. If we have a missing packet (which
1117 * must be before this packet) we can wait for it until the deadline for this
1118 * packet expires. */
1119 if (gap != 0 && out_time != -1) {
1121 GstClockTime sync_time;
1124 GstClockTime duration = GST_CLOCK_TIME_NONE;
1128 GST_WARNING_OBJECT (jitterbuffer,
1129 "Sequence number GAP detected: expected %d instead of %d (%d missing)",
1130 next_seqnum, seqnum, gap);
1132 if (priv->last_out_time != -1) {
1133 GST_DEBUG_OBJECT (jitterbuffer,
1134 "out_time %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT,
1135 GST_TIME_ARGS (out_time), GST_TIME_ARGS (priv->last_out_time));
1136 /* interpolate between the current time and the last time based on
1137 * number of packets we are missing, this is the estimated duration
1138 * for the missing packet based on equidistant packet spacing. Also make
1139 * sure we never go negative. */
1140 if (out_time > priv->last_out_time)
1141 duration = (out_time - priv->last_out_time) / (gap + 1);
1145 GST_DEBUG_OBJECT (jitterbuffer, "duration %" GST_TIME_FORMAT,
1146 GST_TIME_ARGS (duration));
1147 /* add this duration to the timestamp of the last packet we pushed */
1148 out_time = (priv->last_out_time + duration);
1151 /* we don't know what the next_seqnum should be, wait for the last
1152 * possible moment to push this buffer, maybe we get an earlier seqnum
1154 GST_DEBUG_OBJECT (jitterbuffer, "First buffer %d, do sync", seqnum);
1157 GST_OBJECT_LOCK (jitterbuffer);
1158 clock = GST_ELEMENT_CLOCK (jitterbuffer);
1160 GST_OBJECT_UNLOCK (jitterbuffer);
1161 /* let's just push if there is no clock */
1165 GST_DEBUG_OBJECT (jitterbuffer, "sync to timestamp %" GST_TIME_FORMAT,
1166 GST_TIME_ARGS (out_time));
1168 /* prepare for sync against clock */
1169 sync_time = out_time + GST_ELEMENT_CAST (jitterbuffer)->base_time;
1170 /* add latency, this includes our own latency and the peer latency. */
1171 sync_time += (priv->latency_ms * GST_MSECOND);
1172 sync_time += priv->peer_latency;
1174 /* create an entry for the clock */
1175 id = priv->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
1176 GST_OBJECT_UNLOCK (jitterbuffer);
1178 /* release the lock so that the other end can push stuff or unlock */
1181 ret = gst_clock_id_wait (id, NULL);
1184 /* and free the entry */
1185 gst_clock_id_unref (id);
1186 priv->clock_id = NULL;
1188 /* at this point, the clock could have been unlocked by a timeout, a new
1189 * tail element was added to the queue or because we are shutting down. Check
1190 * for shutdown first. */
1191 if (priv->srcresult != GST_FLOW_OK)
1194 /* if we got unscheduled and we are not flushing, it's because a new tail
1195 * element became available in the queue. Grab it and try to push or sync. */
1196 if (ret == GST_CLOCK_UNSCHEDULED) {
1197 GST_DEBUG_OBJECT (jitterbuffer,
1198 "Wait got unscheduled, will retry to push with new buffer");
1203 /* we now timed out, this means we lost a packet or finished synchronizing
1204 * on the first buffer. */
1208 /* we had a gap and thus we lost a packet. Create an event for this. */
1209 GST_DEBUG_OBJECT (jitterbuffer, "Packet #%d lost", next_seqnum);
1213 if (priv->do_lost) {
1214 /* create paket lost event */
1215 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1216 gst_structure_new ("GstRTPPacketLost",
1217 "seqnum", G_TYPE_UINT, (guint) next_seqnum,
1218 "timestamp", G_TYPE_UINT64, out_time,
1219 "duration", G_TYPE_UINT64, duration, NULL));
1220 gst_pad_push_event (priv->srcpad, event);
1223 /* update our expected next packet */
1224 priv->last_popped_seqnum = next_seqnum;
1225 priv->last_out_time = out_time;
1226 priv->next_seqnum = (next_seqnum + 1) & 0xffff;
1227 /* look for next packet */
1231 /* there was no known gap,just the first packet, exit the loop and push */
1232 GST_DEBUG_OBJECT (jitterbuffer, "First packet #%d synced", seqnum);
1234 /* get new timestamp, latency might have changed */
1235 out_time = apply_offset (jitterbuffer, timestamp);
1239 /* when we get here we are ready to pop and push the buffer */
1240 outbuf = rtp_jitter_buffer_pop (priv->jbuf);
1242 if (discont || priv->discont) {
1243 /* set DISCONT flag when we missed a packet. We pushed the buffer writable
1244 * into the jitterbuffer so we can modify now. */
1245 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1246 priv->discont = FALSE;
1249 /* apply timestamp with offset to buffer now */
1250 GST_BUFFER_TIMESTAMP (outbuf) = out_time;
1252 /* now we are ready to push the buffer. Save the seqnum and release the lock
1253 * so the other end can push stuff in the queue again. */
1254 priv->last_popped_seqnum = seqnum;
1255 priv->last_out_time = out_time;
1256 priv->next_seqnum = (seqnum + 1) & 0xffff;
1260 GST_DEBUG_OBJECT (jitterbuffer,
1261 "Pushing buffer %d, timestamp %" GST_TIME_FORMAT, seqnum,
1262 GST_TIME_ARGS (out_time));
1263 result = gst_pad_push (priv->srcpad, outbuf);
1264 if (result != GST_FLOW_OK)
1272 /* store result, we are flushing now */
1273 GST_DEBUG_OBJECT (jitterbuffer, "We are EOS, pushing EOS downstream");
1274 priv->srcresult = GST_FLOW_UNEXPECTED;
1275 gst_pad_pause_task (priv->srcpad);
1276 gst_pad_push_event (priv->srcpad, gst_event_new_eos ());
1282 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1283 gst_pad_pause_task (priv->srcpad);
1289 const gchar *reason = gst_flow_get_name (result);
1291 GST_DEBUG_OBJECT (jitterbuffer, "pausing task, reason %s", reason);
1295 priv->srcresult = result;
1296 /* we don't post errors or anything because upstream will do that for us
1297 * when we pass the return value upstream. */
1298 gst_pad_pause_task (priv->srcpad);
1305 gst_rtp_jitter_buffer_query (GstPad * pad, GstQuery * query)
1307 GstRtpJitterBuffer *jitterbuffer;
1308 GstRtpJitterBufferPrivate *priv;
1309 gboolean res = FALSE;
1311 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
1312 priv = jitterbuffer->priv;
1314 switch (GST_QUERY_TYPE (query)) {
1315 case GST_QUERY_LATENCY:
1317 /* We need to send the query upstream and add the returned latency to our
1319 GstClockTime min_latency, max_latency;
1321 GstClockTime our_latency;
1323 if ((res = gst_pad_peer_query (priv->sinkpad, query))) {
1324 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1326 GST_DEBUG_OBJECT (jitterbuffer, "Peer latency: min %"
1327 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1328 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1330 /* store this so that we can safely sync on the peer buffers. */
1332 priv->peer_latency = min_latency;
1333 our_latency = ((guint64) priv->latency_ms) * GST_MSECOND;
1336 GST_DEBUG_OBJECT (jitterbuffer, "Our latency: %" GST_TIME_FORMAT,
1337 GST_TIME_ARGS (our_latency));
1339 /* we add some latency but can buffer an infinite amount of time */
1340 min_latency += our_latency;
1343 GST_DEBUG_OBJECT (jitterbuffer, "Calculated total latency : min %"
1344 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1345 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1347 gst_query_set_latency (query, TRUE, min_latency, max_latency);
1352 res = gst_pad_query_default (pad, query);
1356 gst_object_unref (jitterbuffer);
1362 gst_rtp_jitter_buffer_set_property (GObject * object,
1363 guint prop_id, const GValue * value, GParamSpec * pspec)
1365 GstRtpJitterBuffer *jitterbuffer;
1366 GstRtpJitterBufferPrivate *priv;
1368 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
1369 priv = jitterbuffer->priv;
1374 guint new_latency, old_latency;
1376 new_latency = g_value_get_uint (value);
1379 old_latency = priv->latency_ms;
1380 priv->latency_ms = new_latency;
1383 /* post message if latency changed, this will inform the parent pipeline
1384 * that a latency reconfiguration is possible/needed. */
1385 if (new_latency != old_latency) {
1386 GST_DEBUG_OBJECT (jitterbuffer, "latency changed to: %" GST_TIME_FORMAT,
1387 GST_TIME_ARGS (new_latency * GST_MSECOND));
1389 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer),
1390 gst_message_new_latency (GST_OBJECT_CAST (jitterbuffer)));
1394 case PROP_DROP_ON_LATENCY:
1396 priv->drop_on_latency = g_value_get_boolean (value);
1399 case PROP_TS_OFFSET:
1401 priv->ts_offset = g_value_get_int64 (value);
1402 /* FIXME, we don't really have a method for signaling a timestamp
1403 * DISCONT without also making this a data discont. */
1404 /* priv->discont = TRUE; */
1409 priv->do_lost = g_value_get_boolean (value);
1413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1419 gst_rtp_jitter_buffer_get_property (GObject * object,
1420 guint prop_id, GValue * value, GParamSpec * pspec)
1422 GstRtpJitterBuffer *jitterbuffer;
1423 GstRtpJitterBufferPrivate *priv;
1425 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
1426 priv = jitterbuffer->priv;
1431 g_value_set_uint (value, priv->latency_ms);
1434 case PROP_DROP_ON_LATENCY:
1436 g_value_set_boolean (value, priv->drop_on_latency);
1439 case PROP_TS_OFFSET:
1441 g_value_set_int64 (value, priv->ts_offset);
1446 g_value_set_boolean (value, priv->do_lost);
1450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);