2 * Farsight Voice+Video library
4 * Copyright 2007 Collabora Ltd,
5 * Copyright 2007 Nokia Corporation
6 * @author: Philippe Kalaf <philippe.kalaf@collabora.co.uk>.
7 * Copyright 2007 Wim Taymans <wim.taymans@gmail.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * SECTION:element-gstrtpjitterbuffer
29 * This element reorders and removes duplicate RTP packets as they are received
30 * from a network source. It will also wait for missing packets up to a
31 * configurable time limit using the #GstRtpJitterBuffer:latency property.
32 * Packets arriving too late are considered to be lost packets.
34 * This element acts as a live element and so adds #GstRtpJitterBuffer:latency
37 * The element needs the clock-rate of the RTP payload in order to estimate the
38 * delay. This information is obtained either from the caps on the sink pad or,
39 * when no caps are present, from the #GstRtpJitterBuffer::request-pt-map signal.
40 * To clear the previous pt-map use the #GstRtpJitterBuffer::clear-pt-map signal.
42 * This element will automatically be used inside gstrtpbin.
45 * <title>Example pipelines</title>
47 * gst-launch-1.0 rtspsrc location=rtsp://192.168.1.133:8554/mpeg1or2AudioVideoTest ! gstrtpjitterbuffer ! rtpmpvdepay ! mpeg2dec ! xvimagesink
48 * ]| Connect to a streaming server and decode the MPEG video. The jitterbuffer is
49 * inserted into the pipeline to smooth out network jitter and to reorder the
50 * out-of-order RTP packets.
53 * Last reviewed on 2007-05-28 (0.10.5)
62 #include <gst/rtp/gstrtpbuffer.h>
64 #include "gstrtpbin-marshal.h"
66 #include "gstrtpjitterbuffer.h"
67 #include "rtpjitterbuffer.h"
70 #include <gst/glib-compat-private.h>
72 GST_DEBUG_CATEGORY (rtpjitterbuffer_debug);
73 #define GST_CAT_DEFAULT (rtpjitterbuffer_debug)
75 /* RTPJitterBuffer signals and args */
78 SIGNAL_REQUEST_PT_MAP,
86 #define DEFAULT_LATENCY_MS 200
87 #define DEFAULT_DROP_ON_LATENCY FALSE
88 #define DEFAULT_TS_OFFSET 0
89 #define DEFAULT_DO_LOST FALSE
90 #define DEFAULT_MODE RTP_JITTER_BUFFER_MODE_SLAVE
91 #define DEFAULT_PERCENT 0
105 #define JBUF_LOCK(priv) (g_mutex_lock (&(priv)->jbuf_lock))
107 #define JBUF_LOCK_CHECK(priv,label) G_STMT_START { \
109 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
113 #define JBUF_UNLOCK(priv) (g_mutex_unlock (&(priv)->jbuf_lock))
114 #define JBUF_WAIT(priv) (g_cond_wait (&(priv)->jbuf_cond, &(priv)->jbuf_lock))
116 #define JBUF_WAIT_CHECK(priv,label) G_STMT_START { \
118 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK)) \
122 #define JBUF_SIGNAL(priv) (g_cond_signal (&(priv)->jbuf_cond))
124 struct _GstRtpJitterBufferPrivate
126 GstPad *sinkpad, *srcpad;
129 RTPJitterBuffer *jbuf;
141 gboolean drop_on_latency;
145 /* the last seqnum we pushed out */
146 guint32 last_popped_seqnum;
147 /* the next expected seqnum we push */
149 /* last output time */
150 GstClockTime last_out_time;
151 GstClockTime last_out_pts;
152 /* the next expected seqnum we receive */
153 guint32 next_in_seqnum;
157 /* start and stop ranges */
158 GstClockTime npt_start;
159 GstClockTime npt_stop;
160 guint64 ext_timestamp;
161 guint64 last_elapsed;
162 guint64 estimated_eos;
168 /* clock rate and rtp timestamp offset */
172 gint64 prev_ts_offset;
174 /* when we are shutting down */
175 GstFlowReturn srcresult;
181 gboolean unscheduled;
182 /* the latency of the upstream peer, we have to take this into account when
183 * synchronizing the buffers. */
184 GstClockTime peer_latency;
188 /* some accounting */
190 guint64 num_duplicates;
206 GstClockTime timeout;
209 #define GST_RTP_JITTER_BUFFER_GET_PRIVATE(o) \
210 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_RTP_JITTER_BUFFER, \
211 GstRtpJitterBufferPrivate))
213 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_template =
214 GST_STATIC_PAD_TEMPLATE ("sink",
217 GST_STATIC_CAPS ("application/x-rtp, "
218 "clock-rate = (int) [ 1, 2147483647 ]"
219 /* "payload = (int) , "
220 * "encoding-name = (string) "
224 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_rtcp_template =
225 GST_STATIC_PAD_TEMPLATE ("sink_rtcp",
228 GST_STATIC_CAPS ("application/x-rtcp")
231 static GstStaticPadTemplate gst_rtp_jitter_buffer_src_template =
232 GST_STATIC_PAD_TEMPLATE ("src",
235 GST_STATIC_CAPS ("application/x-rtp"
236 /* "payload = (int) , "
237 * "clock-rate = (int) , "
238 * "encoding-name = (string) "
242 static guint gst_rtp_jitter_buffer_signals[LAST_SIGNAL] = { 0 };
244 #define gst_rtp_jitter_buffer_parent_class parent_class
245 G_DEFINE_TYPE (GstRtpJitterBuffer, gst_rtp_jitter_buffer, GST_TYPE_ELEMENT);
247 /* object overrides */
248 static void gst_rtp_jitter_buffer_set_property (GObject * object,
249 guint prop_id, const GValue * value, GParamSpec * pspec);
250 static void gst_rtp_jitter_buffer_get_property (GObject * object,
251 guint prop_id, GValue * value, GParamSpec * pspec);
252 static void gst_rtp_jitter_buffer_finalize (GObject * object);
254 /* element overrides */
255 static GstStateChangeReturn gst_rtp_jitter_buffer_change_state (GstElement
256 * element, GstStateChange transition);
257 static GstPad *gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
258 GstPadTemplate * templ, const gchar * name, const GstCaps * filter);
259 static void gst_rtp_jitter_buffer_release_pad (GstElement * element,
261 static GstClock *gst_rtp_jitter_buffer_provide_clock (GstElement * element);
264 static GstCaps *gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter);
265 static GstIterator *gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad,
268 /* sinkpad overrides */
269 static gboolean gst_rtp_jitter_buffer_sink_event (GstPad * pad,
270 GstObject * parent, GstEvent * event);
271 static GstFlowReturn gst_rtp_jitter_buffer_chain (GstPad * pad,
272 GstObject * parent, GstBuffer * buffer);
274 static gboolean gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad,
275 GstObject * parent, GstEvent * event);
276 static GstFlowReturn gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad,
277 GstObject * parent, GstBuffer * buffer);
279 static gboolean gst_rtp_jitter_buffer_sink_query (GstPad * pad,
280 GstObject * parent, GstQuery * query);
282 /* srcpad overrides */
283 static gboolean gst_rtp_jitter_buffer_src_event (GstPad * pad,
284 GstObject * parent, GstEvent * event);
285 static gboolean gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad,
286 GstObject * parent, GstPadMode mode, gboolean active);
287 static void gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer);
288 static gboolean gst_rtp_jitter_buffer_src_query (GstPad * pad,
289 GstObject * parent, GstQuery * query);
292 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer);
294 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jitterbuffer,
295 gboolean active, guint64 base_time);
296 static void do_handle_sync (GstRtpJitterBuffer * jitterbuffer);
297 static void remove_all_timers (GstRtpJitterBuffer * jitterbuffer);
300 gst_rtp_jitter_buffer_class_init (GstRtpJitterBufferClass * klass)
302 GObjectClass *gobject_class;
303 GstElementClass *gstelement_class;
305 gobject_class = (GObjectClass *) klass;
306 gstelement_class = (GstElementClass *) klass;
308 g_type_class_add_private (klass, sizeof (GstRtpJitterBufferPrivate));
310 gobject_class->finalize = gst_rtp_jitter_buffer_finalize;
312 gobject_class->set_property = gst_rtp_jitter_buffer_set_property;
313 gobject_class->get_property = gst_rtp_jitter_buffer_get_property;
316 * GstRtpJitterBuffer::latency:
318 * The maximum latency of the jitterbuffer. Packets will be kept in the buffer
319 * for at most this time.
321 g_object_class_install_property (gobject_class, PROP_LATENCY,
322 g_param_spec_uint ("latency", "Buffer latency in ms",
323 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
324 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 * GstRtpJitterBuffer::drop-on-latency:
328 * Drop oldest buffers when the queue is completely filled.
330 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
331 g_param_spec_boolean ("drop-on-latency",
332 "Drop buffers when maximum latency is reached",
333 "Tells the jitterbuffer to never exceed the given latency in size",
334 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
336 * GstRtpJitterBuffer::ts-offset:
338 * Adjust GStreamer output buffer timestamps in the jitterbuffer with offset.
339 * This is mainly used to ensure interstream synchronisation.
341 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
342 g_param_spec_int64 ("ts-offset", "Timestamp Offset",
343 "Adjust buffer timestamps with offset in nanoseconds", G_MININT64,
344 G_MAXINT64, DEFAULT_TS_OFFSET,
345 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 * GstRtpJitterBuffer::do-lost:
350 * Send out a GstRTPPacketLost event downstream when a packet is considered
353 g_object_class_install_property (gobject_class, PROP_DO_LOST,
354 g_param_spec_boolean ("do-lost", "Do Lost",
355 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 * GstRtpJitterBuffer::mode:
361 * Control the buffering and timestamping mode used by the jitterbuffer.
363 g_object_class_install_property (gobject_class, PROP_MODE,
364 g_param_spec_enum ("mode", "Mode",
365 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
366 DEFAULT_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 * GstRtpJitterBuffer::percent:
370 * The percent of the jitterbuffer that is filled.
374 g_object_class_install_property (gobject_class, PROP_PERCENT,
375 g_param_spec_int ("percent", "percent",
376 "The buffer filled percent", 0, 100,
377 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
379 * GstRtpJitterBuffer::request-pt-map:
380 * @buffer: the object which received the signal
383 * Request the payload type as #GstCaps for @pt.
385 gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP] =
386 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
387 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
388 request_pt_map), NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT,
389 GST_TYPE_CAPS, 1, G_TYPE_UINT);
391 * GstRtpJitterBuffer::handle-sync:
392 * @buffer: the object which received the signal
393 * @struct: a GstStructure containing sync values.
395 * Be notified of new sync values.
397 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC] =
398 g_signal_new ("handle-sync", G_TYPE_FROM_CLASS (klass),
399 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
400 handle_sync), NULL, NULL, g_cclosure_marshal_VOID__BOXED,
401 G_TYPE_NONE, 1, GST_TYPE_STRUCTURE | G_SIGNAL_TYPE_STATIC_SCOPE);
404 * GstRtpJitterBuffer::on-npt-stop
405 * @buffer: the object which received the signal
407 * Signal that the jitterbufer has pushed the RTP packet that corresponds to
408 * the npt-stop position.
410 gst_rtp_jitter_buffer_signals[SIGNAL_ON_NPT_STOP] =
411 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
412 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
413 on_npt_stop), NULL, NULL, g_cclosure_marshal_VOID__VOID,
414 G_TYPE_NONE, 0, G_TYPE_NONE);
417 * GstRtpJitterBuffer::clear-pt-map:
418 * @buffer: the object which received the signal
420 * Invalidate the clock-rate as obtained with the
421 * #GstRtpJitterBuffer::request-pt-map signal.
423 gst_rtp_jitter_buffer_signals[SIGNAL_CLEAR_PT_MAP] =
424 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
425 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
426 G_STRUCT_OFFSET (GstRtpJitterBufferClass, clear_pt_map), NULL, NULL,
427 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
430 * GstRtpJitterBuffer::set-active:
431 * @buffer: the object which received the signal
433 * Start pushing out packets with the given base time. This signal is only
434 * useful in buffering mode.
436 * Returns: the time of the last pushed packet.
440 gst_rtp_jitter_buffer_signals[SIGNAL_SET_ACTIVE] =
441 g_signal_new ("set-active", G_TYPE_FROM_CLASS (klass),
442 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
443 G_STRUCT_OFFSET (GstRtpJitterBufferClass, set_active), NULL, NULL,
444 gst_rtp_bin_marshal_UINT64__BOOL_UINT64, G_TYPE_UINT64, 2, G_TYPE_BOOLEAN,
447 gstelement_class->change_state =
448 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_change_state);
449 gstelement_class->request_new_pad =
450 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_request_new_pad);
451 gstelement_class->release_pad =
452 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_release_pad);
453 gstelement_class->provide_clock =
454 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_provide_clock);
456 gst_element_class_add_pad_template (gstelement_class,
457 gst_static_pad_template_get (&gst_rtp_jitter_buffer_src_template));
458 gst_element_class_add_pad_template (gstelement_class,
459 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_template));
460 gst_element_class_add_pad_template (gstelement_class,
461 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_rtcp_template));
463 gst_element_class_set_static_metadata (gstelement_class,
464 "RTP packet jitter-buffer", "Filter/Network/RTP",
465 "A buffer that deals with network jitter and other transmission faults",
466 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
467 "Wim Taymans <wim.taymans@gmail.com>");
469 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_clear_pt_map);
470 klass->set_active = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_set_active);
472 GST_DEBUG_CATEGORY_INIT
473 (rtpjitterbuffer_debug, "gstrtpjitterbuffer", 0, "RTP Jitter Buffer");
477 gst_rtp_jitter_buffer_init (GstRtpJitterBuffer * jitterbuffer)
479 GstRtpJitterBufferPrivate *priv;
481 priv = GST_RTP_JITTER_BUFFER_GET_PRIVATE (jitterbuffer);
482 jitterbuffer->priv = priv;
484 priv->latency_ms = DEFAULT_LATENCY_MS;
485 priv->latency_ns = priv->latency_ms * GST_MSECOND;
486 priv->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
487 priv->do_lost = DEFAULT_DO_LOST;
488 priv->timers = g_array_new (FALSE, TRUE, sizeof (TimerData));
490 priv->jbuf = rtp_jitter_buffer_new ();
491 g_mutex_init (&priv->jbuf_lock);
492 g_cond_init (&priv->jbuf_cond);
494 /* reset skew detection initialy */
495 rtp_jitter_buffer_reset_skew (priv->jbuf);
496 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
497 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
501 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_src_template,
504 gst_pad_set_activatemode_function (priv->srcpad,
505 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_activate_mode));
506 gst_pad_set_query_function (priv->srcpad,
507 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_query));
508 gst_pad_set_event_function (priv->srcpad,
509 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_event));
512 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_sink_template,
515 gst_pad_set_chain_function (priv->sinkpad,
516 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_chain));
517 gst_pad_set_event_function (priv->sinkpad,
518 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_event));
519 gst_pad_set_query_function (priv->sinkpad,
520 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_query));
522 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->srcpad);
523 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->sinkpad);
525 GST_OBJECT_FLAG_SET (jitterbuffer, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
529 gst_rtp_jitter_buffer_finalize (GObject * object)
531 GstRtpJitterBuffer *jitterbuffer;
533 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
535 g_array_free (jitterbuffer->priv->timers, TRUE);
536 g_mutex_clear (&jitterbuffer->priv->jbuf_lock);
537 g_cond_clear (&jitterbuffer->priv->jbuf_cond);
539 g_object_unref (jitterbuffer->priv->jbuf);
541 G_OBJECT_CLASS (parent_class)->finalize (object);
545 gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad, GstObject * parent)
547 GstRtpJitterBuffer *jitterbuffer;
548 GstPad *otherpad = NULL;
552 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
554 if (pad == jitterbuffer->priv->sinkpad) {
555 otherpad = jitterbuffer->priv->srcpad;
556 } else if (pad == jitterbuffer->priv->srcpad) {
557 otherpad = jitterbuffer->priv->sinkpad;
558 } else if (pad == jitterbuffer->priv->rtcpsinkpad) {
562 g_value_init (&val, GST_TYPE_PAD);
563 g_value_set_object (&val, otherpad);
564 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
565 g_value_unset (&val);
571 create_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
573 GstRtpJitterBufferPrivate *priv;
575 priv = jitterbuffer->priv;
577 GST_DEBUG_OBJECT (jitterbuffer, "creating RTCP sink pad");
580 gst_pad_new_from_static_template
581 (&gst_rtp_jitter_buffer_sink_rtcp_template, "sink_rtcp");
582 gst_pad_set_chain_function (priv->rtcpsinkpad,
583 gst_rtp_jitter_buffer_chain_rtcp);
584 gst_pad_set_event_function (priv->rtcpsinkpad,
585 (GstPadEventFunction) gst_rtp_jitter_buffer_sink_rtcp_event);
586 gst_pad_set_iterate_internal_links_function (priv->rtcpsinkpad,
587 gst_rtp_jitter_buffer_iterate_internal_links);
588 gst_pad_set_active (priv->rtcpsinkpad, TRUE);
589 gst_element_add_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
591 return priv->rtcpsinkpad;
595 remove_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
597 GstRtpJitterBufferPrivate *priv;
599 priv = jitterbuffer->priv;
601 GST_DEBUG_OBJECT (jitterbuffer, "removing RTCP sink pad");
603 gst_pad_set_active (priv->rtcpsinkpad, FALSE);
605 gst_element_remove_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
606 priv->rtcpsinkpad = NULL;
610 gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
611 GstPadTemplate * templ, const gchar * name, const GstCaps * filter)
613 GstRtpJitterBuffer *jitterbuffer;
614 GstElementClass *klass;
616 GstRtpJitterBufferPrivate *priv;
618 g_return_val_if_fail (templ != NULL, NULL);
619 g_return_val_if_fail (GST_IS_RTP_JITTER_BUFFER (element), NULL);
621 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
622 priv = jitterbuffer->priv;
623 klass = GST_ELEMENT_GET_CLASS (element);
625 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
627 /* figure out the template */
628 if (templ == gst_element_class_get_pad_template (klass, "sink_rtcp")) {
629 if (priv->rtcpsinkpad != NULL)
632 result = create_rtcp_sink (jitterbuffer);
641 g_warning ("gstrtpjitterbuffer: this is not our template");
646 g_warning ("gstrtpjitterbuffer: pad already requested");
652 gst_rtp_jitter_buffer_release_pad (GstElement * element, GstPad * pad)
654 GstRtpJitterBuffer *jitterbuffer;
655 GstRtpJitterBufferPrivate *priv;
657 g_return_if_fail (GST_IS_RTP_JITTER_BUFFER (element));
658 g_return_if_fail (GST_IS_PAD (pad));
660 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
661 priv = jitterbuffer->priv;
663 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
665 if (priv->rtcpsinkpad == pad) {
666 remove_rtcp_sink (jitterbuffer);
675 g_warning ("gstjitterbuffer: asked to release an unknown pad");
681 gst_rtp_jitter_buffer_provide_clock (GstElement * element)
683 return gst_system_clock_obtain ();
687 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer)
689 GstRtpJitterBufferPrivate *priv;
691 priv = jitterbuffer->priv;
693 /* this will trigger a new pt-map request signal, FIXME, do something better. */
696 priv->clock_rate = -1;
697 /* do not clear current content, but refresh state for new arrival */
698 GST_DEBUG_OBJECT (jitterbuffer, "reset jitterbuffer");
699 rtp_jitter_buffer_reset_skew (priv->jbuf);
700 priv->last_popped_seqnum = -1;
701 priv->next_seqnum = -1;
706 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jbuf, gboolean active,
709 GstRtpJitterBufferPrivate *priv;
710 GstClockTime last_out;
716 GST_DEBUG_OBJECT (jbuf, "setting active %d with offset %" GST_TIME_FORMAT,
717 active, GST_TIME_ARGS (offset));
719 if (active != priv->active) {
720 /* add the amount of time spent in paused to the output offset. All
721 * outgoing buffers will have this offset applied to their timestamps in
722 * order to make them arrive in time in the sink. */
723 priv->out_offset = offset;
724 GST_DEBUG_OBJECT (jbuf, "out offset %" GST_TIME_FORMAT,
725 GST_TIME_ARGS (priv->out_offset));
726 priv->active = active;
730 rtp_jitter_buffer_set_buffering (priv->jbuf, TRUE);
732 if ((head = rtp_jitter_buffer_peek (priv->jbuf))) {
733 /* head buffer timestamp and offset gives our output time */
734 last_out = GST_BUFFER_TIMESTAMP (head) + priv->ts_offset;
736 /* use last known time when the buffer is empty */
737 last_out = priv->last_out_time;
745 gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter)
747 GstRtpJitterBuffer *jitterbuffer;
748 GstRtpJitterBufferPrivate *priv;
753 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
754 priv = jitterbuffer->priv;
756 other = (pad == priv->srcpad ? priv->sinkpad : priv->srcpad);
758 caps = gst_pad_peer_query_caps (other, filter);
760 templ = gst_pad_get_pad_template_caps (pad);
762 GST_DEBUG_OBJECT (jitterbuffer, "use template");
767 GST_DEBUG_OBJECT (jitterbuffer, "intersect with template");
769 intersect = gst_caps_intersect (caps, templ);
770 gst_caps_unref (caps);
771 gst_caps_unref (templ);
775 gst_object_unref (jitterbuffer);
781 * Must be called with JBUF_LOCK held
785 gst_jitter_buffer_sink_parse_caps (GstRtpJitterBuffer * jitterbuffer,
788 GstRtpJitterBufferPrivate *priv;
789 GstStructure *caps_struct;
793 priv = jitterbuffer->priv;
795 /* first parse the caps */
796 caps_struct = gst_caps_get_structure (caps, 0);
798 GST_DEBUG_OBJECT (jitterbuffer, "got caps");
800 /* we need a clock-rate to convert the rtp timestamps to GStreamer time and to
801 * measure the amount of data in the buffer */
802 if (!gst_structure_get_int (caps_struct, "clock-rate", &priv->clock_rate))
805 if (priv->clock_rate <= 0)
808 GST_DEBUG_OBJECT (jitterbuffer, "got clock-rate %d", priv->clock_rate);
810 /* The clock base is the RTP timestamp corrsponding to the npt-start value. We
811 * can use this to track the amount of time elapsed on the sender. */
812 if (gst_structure_get_uint (caps_struct, "clock-base", &val))
813 priv->clock_base = val;
815 priv->clock_base = -1;
817 priv->ext_timestamp = priv->clock_base;
819 GST_DEBUG_OBJECT (jitterbuffer, "got clock-base %" G_GINT64_FORMAT,
822 if (gst_structure_get_uint (caps_struct, "seqnum-base", &val)) {
823 /* first expected seqnum, only update when we didn't have a previous base. */
824 if (priv->next_in_seqnum == -1)
825 priv->next_in_seqnum = val;
826 if (priv->next_seqnum == -1)
827 priv->next_seqnum = val;
830 GST_DEBUG_OBJECT (jitterbuffer, "got seqnum-base %d", priv->next_in_seqnum);
832 /* the start and stop times. The seqnum-base corresponds to the start time. We
833 * will keep track of the seqnums on the output and when we reach the one
834 * corresponding to npt-stop, we emit the npt-stop-reached signal */
835 if (gst_structure_get_clock_time (caps_struct, "npt-start", &tval))
836 priv->npt_start = tval;
840 if (gst_structure_get_clock_time (caps_struct, "npt-stop", &tval))
841 priv->npt_stop = tval;
845 GST_DEBUG_OBJECT (jitterbuffer,
846 "npt start/stop: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
847 GST_TIME_ARGS (priv->npt_start), GST_TIME_ARGS (priv->npt_stop));
854 GST_DEBUG_OBJECT (jitterbuffer, "No clock-rate in caps!");
859 GST_DEBUG_OBJECT (jitterbuffer, "Invalid clock-rate %d", priv->clock_rate);
865 gst_rtp_jitter_buffer_flush_start (GstRtpJitterBuffer * jitterbuffer)
867 GstRtpJitterBufferPrivate *priv;
869 priv = jitterbuffer->priv;
872 /* mark ourselves as flushing */
873 priv->srcresult = GST_FLOW_FLUSHING;
874 GST_DEBUG_OBJECT (jitterbuffer, "Disabling pop on queue");
875 /* this unblocks any waiting pops on the src pad task */
877 /* unlock clock, we just unschedule, the entry will be released by the
878 * locking streaming thread. */
879 if (priv->clock_id) {
880 gst_clock_id_unschedule (priv->clock_id);
881 priv->unscheduled = TRUE;
887 gst_rtp_jitter_buffer_flush_stop (GstRtpJitterBuffer * jitterbuffer)
889 GstRtpJitterBufferPrivate *priv;
891 priv = jitterbuffer->priv;
894 GST_DEBUG_OBJECT (jitterbuffer, "Enabling pop on queue");
895 /* Mark as non flushing */
896 priv->srcresult = GST_FLOW_OK;
897 gst_segment_init (&priv->segment, GST_FORMAT_TIME);
898 priv->last_popped_seqnum = -1;
899 priv->last_out_time = -1;
900 priv->last_out_pts = -1;
901 priv->next_seqnum = -1;
902 priv->next_in_seqnum = -1;
903 priv->clock_rate = -1;
905 priv->estimated_eos = -1;
906 priv->last_elapsed = 0;
907 priv->ext_timestamp = -1;
908 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
909 rtp_jitter_buffer_flush (priv->jbuf);
910 rtp_jitter_buffer_reset_skew (priv->jbuf);
911 remove_all_timers (jitterbuffer);
916 gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad, GstObject * parent,
917 GstPadMode mode, gboolean active)
920 GstRtpJitterBuffer *jitterbuffer = NULL;
922 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
925 case GST_PAD_MODE_PUSH:
927 /* allow data processing */
928 gst_rtp_jitter_buffer_flush_stop (jitterbuffer);
930 /* start pushing out buffers */
931 GST_DEBUG_OBJECT (jitterbuffer, "Starting task on srcpad");
932 result = gst_pad_start_task (jitterbuffer->priv->srcpad,
933 (GstTaskFunction) gst_rtp_jitter_buffer_loop, jitterbuffer, NULL);
935 /* make sure all data processing stops ASAP */
936 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
938 /* NOTE this will hardlock if the state change is called from the src pad
939 * task thread because we will _join() the thread. */
940 GST_DEBUG_OBJECT (jitterbuffer, "Stopping task on srcpad");
941 result = gst_pad_stop_task (pad);
951 static GstStateChangeReturn
952 gst_rtp_jitter_buffer_change_state (GstElement * element,
953 GstStateChange transition)
955 GstRtpJitterBuffer *jitterbuffer;
956 GstRtpJitterBufferPrivate *priv;
957 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
959 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
960 priv = jitterbuffer->priv;
962 switch (transition) {
963 case GST_STATE_CHANGE_NULL_TO_READY:
965 case GST_STATE_CHANGE_READY_TO_PAUSED:
967 /* reset negotiated values */
968 priv->clock_rate = -1;
969 priv->clock_base = -1;
970 priv->peer_latency = 0;
972 /* block until we go to PLAYING */
973 priv->blocked = TRUE;
976 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
978 /* unblock to allow streaming in PLAYING */
979 priv->blocked = FALSE;
987 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
989 switch (transition) {
990 case GST_STATE_CHANGE_READY_TO_PAUSED:
991 /* we are a live element because we sync to the clock, which we can only
992 * do in the PLAYING state */
993 if (ret != GST_STATE_CHANGE_FAILURE)
994 ret = GST_STATE_CHANGE_NO_PREROLL;
996 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
998 /* block to stop streaming when PAUSED */
999 priv->blocked = TRUE;
1001 if (ret != GST_STATE_CHANGE_FAILURE)
1002 ret = GST_STATE_CHANGE_NO_PREROLL;
1004 case GST_STATE_CHANGE_PAUSED_TO_READY:
1005 gst_buffer_replace (&priv->last_sr, NULL);
1007 case GST_STATE_CHANGE_READY_TO_NULL:
1017 gst_rtp_jitter_buffer_src_event (GstPad * pad, GstObject * parent,
1020 gboolean ret = TRUE;
1021 GstRtpJitterBuffer *jitterbuffer;
1022 GstRtpJitterBufferPrivate *priv;
1024 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1025 priv = jitterbuffer->priv;
1027 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1029 switch (GST_EVENT_TYPE (event)) {
1030 case GST_EVENT_LATENCY:
1032 GstClockTime latency;
1034 gst_event_parse_latency (event, &latency);
1036 GST_DEBUG_OBJECT (jitterbuffer,
1037 "configuring latency of %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1040 /* adjust the overall buffer delay to the total pipeline latency in
1041 * buffering mode because if downstream consumes too fast (because of
1042 * large latency or queues, we would start rebuffering again. */
1043 if (rtp_jitter_buffer_get_mode (priv->jbuf) ==
1044 RTP_JITTER_BUFFER_MODE_BUFFER) {
1045 rtp_jitter_buffer_set_delay (priv->jbuf, latency);
1049 ret = gst_pad_push_event (priv->sinkpad, event);
1053 ret = gst_pad_push_event (priv->sinkpad, event);
1061 gst_rtp_jitter_buffer_sink_event (GstPad * pad, GstObject * parent,
1064 gboolean ret = TRUE;
1065 GstRtpJitterBuffer *jitterbuffer;
1066 GstRtpJitterBufferPrivate *priv;
1068 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1069 priv = jitterbuffer->priv;
1071 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1073 switch (GST_EVENT_TYPE (event)) {
1074 case GST_EVENT_CAPS:
1078 gst_event_parse_caps (event, &caps);
1081 ret = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1084 /* set same caps on srcpad on success */
1086 ret = gst_pad_push_event (priv->srcpad, event);
1088 gst_event_unref (event);
1091 case GST_EVENT_SEGMENT:
1093 gst_event_copy_segment (event, &priv->segment);
1095 /* we need time for now */
1096 if (priv->segment.format != GST_FORMAT_TIME)
1097 goto newseg_wrong_format;
1099 GST_DEBUG_OBJECT (jitterbuffer,
1100 "newsegment: %" GST_SEGMENT_FORMAT, &priv->segment);
1102 /* FIXME, push SEGMENT in the queue. Sorting order might be difficult. */
1103 ret = gst_pad_push_event (priv->srcpad, event);
1106 case GST_EVENT_FLUSH_START:
1107 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
1108 ret = gst_pad_push_event (priv->srcpad, event);
1110 case GST_EVENT_FLUSH_STOP:
1111 ret = gst_pad_push_event (priv->srcpad, event);
1113 gst_rtp_jitter_buffer_src_activate_mode (priv->srcpad, parent,
1114 GST_PAD_MODE_PUSH, TRUE);
1118 /* push EOS in queue. We always push it at the head */
1120 /* check for flushing, we need to discard the event and return FALSE when
1121 * we are flushing */
1122 ret = priv->srcresult == GST_FLOW_OK;
1123 if (ret && !priv->eos) {
1124 GST_INFO_OBJECT (jitterbuffer, "queuing EOS");
1127 } else if (priv->eos) {
1128 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, we are already EOS");
1130 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, reason %s",
1131 gst_flow_get_name (priv->srcresult));
1134 gst_event_unref (event);
1138 ret = gst_pad_push_event (priv->srcpad, event);
1147 newseg_wrong_format:
1149 GST_DEBUG_OBJECT (jitterbuffer, "received non TIME newsegment");
1151 gst_event_unref (event);
1157 gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad, GstObject * parent,
1160 gboolean ret = TRUE;
1161 GstRtpJitterBuffer *jitterbuffer;
1163 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1165 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1167 switch (GST_EVENT_TYPE (event)) {
1168 case GST_EVENT_FLUSH_START:
1169 gst_event_unref (event);
1171 case GST_EVENT_FLUSH_STOP:
1172 gst_event_unref (event);
1175 ret = gst_pad_event_default (pad, parent, event);
1183 * Must be called with JBUF_LOCK held, will release the LOCK when emiting the
1184 * signal. The function returns GST_FLOW_ERROR when a parsing error happened and
1185 * GST_FLOW_FLUSHING when the element is shutting down. On success
1186 * GST_FLOW_OK is returned.
1188 static GstFlowReturn
1189 gst_rtp_jitter_buffer_get_clock_rate (GstRtpJitterBuffer * jitterbuffer,
1193 GValue args[2] = { {0}, {0} };
1197 g_value_init (&args[0], GST_TYPE_ELEMENT);
1198 g_value_set_object (&args[0], jitterbuffer);
1199 g_value_init (&args[1], G_TYPE_UINT);
1200 g_value_set_uint (&args[1], pt);
1202 g_value_init (&ret, GST_TYPE_CAPS);
1203 g_value_set_boxed (&ret, NULL);
1205 JBUF_UNLOCK (jitterbuffer->priv);
1206 g_signal_emitv (args, gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP], 0,
1208 JBUF_LOCK_CHECK (jitterbuffer->priv, out_flushing);
1210 g_value_unset (&args[0]);
1211 g_value_unset (&args[1]);
1212 caps = (GstCaps *) g_value_dup_boxed (&ret);
1213 g_value_unset (&ret);
1217 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1218 gst_caps_unref (caps);
1220 if (G_UNLIKELY (!res))
1228 GST_DEBUG_OBJECT (jitterbuffer, "could not get caps");
1229 return GST_FLOW_ERROR;
1233 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1234 return GST_FLOW_FLUSHING;
1238 GST_DEBUG_OBJECT (jitterbuffer, "parse failed");
1239 return GST_FLOW_ERROR;
1243 /* call with jbuf lock held */
1245 check_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint * percent)
1247 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1249 /* too short a stream, or too close to EOS will never really fill buffer */
1250 if (*percent != -1 && priv->npt_stop != -1 &&
1251 priv->npt_stop - priv->npt_start <=
1252 rtp_jitter_buffer_get_delay (priv->jbuf)) {
1253 GST_DEBUG_OBJECT (jitterbuffer, "short stream; faking full buffer");
1254 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
1260 post_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint percent)
1262 GstMessage *message;
1264 /* Post a buffering message */
1265 message = gst_message_new_buffering (GST_OBJECT_CAST (jitterbuffer), percent);
1266 gst_message_set_buffering_stats (message, GST_BUFFERING_LIVE, -1, -1, -1);
1268 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer), message);
1271 static GstFlowReturn
1272 gst_rtp_jitter_buffer_chain (GstPad * pad, GstObject * parent,
1275 GstRtpJitterBuffer *jitterbuffer;
1276 GstRtpJitterBufferPrivate *priv;
1278 GstFlowReturn ret = GST_FLOW_OK;
1279 GstClockTime timestamp;
1284 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
1286 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1288 priv = jitterbuffer->priv;
1290 if (G_UNLIKELY (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)))
1291 goto invalid_buffer;
1293 pt = gst_rtp_buffer_get_payload_type (&rtp);
1294 seqnum = gst_rtp_buffer_get_seq (&rtp);
1295 gst_rtp_buffer_unmap (&rtp);
1297 /* take the timestamp of the buffer. This is the time when the packet was
1298 * received and is used to calculate jitter and clock skew. We will adjust
1299 * this timestamp with the smoothed value after processing it in the
1301 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1302 /* bring to running time */
1303 timestamp = gst_segment_to_running_time (&priv->segment, GST_FORMAT_TIME,
1306 GST_DEBUG_OBJECT (jitterbuffer,
1307 "Received packet #%d at time %" GST_TIME_FORMAT, seqnum,
1308 GST_TIME_ARGS (timestamp));
1310 JBUF_LOCK_CHECK (priv, out_flushing);
1312 if (G_UNLIKELY (priv->last_pt != pt)) {
1315 GST_DEBUG_OBJECT (jitterbuffer, "pt changed from %u to %u", priv->last_pt,
1319 /* reset clock-rate so that we get a new one */
1320 priv->clock_rate = -1;
1322 /* Try to get the clock-rate from the caps first if we can. If there are no
1323 * caps we must fire the signal to get the clock-rate. */
1324 if ((caps = gst_pad_get_current_caps (pad))) {
1325 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1326 gst_caps_unref (caps);
1330 if (G_UNLIKELY (priv->clock_rate == -1)) {
1331 /* no clock rate given on the caps, try to get one with the signal */
1332 if (gst_rtp_jitter_buffer_get_clock_rate (jitterbuffer,
1333 pt) == GST_FLOW_FLUSHING)
1336 if (G_UNLIKELY (priv->clock_rate == -1))
1340 /* don't accept more data on EOS */
1341 if (G_UNLIKELY (priv->eos))
1344 /* now check against our expected seqnum */
1345 if (G_LIKELY (priv->next_in_seqnum != -1)) {
1347 gboolean reset = FALSE;
1349 gap = gst_rtp_buffer_compare_seqnum (priv->next_in_seqnum, seqnum);
1350 if (G_UNLIKELY (gap != 0)) {
1351 GST_DEBUG_OBJECT (jitterbuffer, "expected #%d, got #%d, gap of %d",
1352 priv->next_in_seqnum, seqnum, gap);
1353 /* priv->next_in_seqnum >= seqnum, this packet is too late or the
1354 * sender might have been restarted with different seqnum. */
1355 if (gap < -RTP_MAX_MISORDER) {
1356 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too old %d", gap);
1359 /* priv->next_in_seqnum < seqnum, this is a new packet */
1360 else if (G_UNLIKELY (gap > RTP_MAX_DROPOUT)) {
1361 GST_DEBUG_OBJECT (jitterbuffer, "reset: too many dropped packets %d",
1365 GST_DEBUG_OBJECT (jitterbuffer, "tolerable gap");
1368 if (G_UNLIKELY (reset)) {
1369 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
1370 rtp_jitter_buffer_flush (priv->jbuf);
1371 rtp_jitter_buffer_reset_skew (priv->jbuf);
1372 remove_all_timers (jitterbuffer);
1373 priv->last_popped_seqnum = -1;
1374 priv->next_seqnum = seqnum;
1377 priv->next_in_seqnum = (seqnum + 1) & 0xffff;
1379 /* let's check if this buffer is too late, we can only accept packets with
1380 * bigger seqnum than the one we last pushed. */
1381 if (G_LIKELY (priv->last_popped_seqnum != -1)) {
1384 gap = gst_rtp_buffer_compare_seqnum (priv->last_popped_seqnum, seqnum);
1386 /* priv->last_popped_seqnum >= seqnum, we're too late. */
1387 if (G_UNLIKELY (gap <= 0))
1391 /* let's drop oldest packet if the queue is already full and drop-on-latency
1392 * is set. We can only do this when there actually is a latency. When no
1393 * latency is set, we just pump it in the queue and let the other end push it
1394 * out as fast as possible. */
1395 if (priv->latency_ms && priv->drop_on_latency) {
1397 gst_util_uint64_scale_int (priv->latency_ms, priv->clock_rate, 1000);
1399 if (G_UNLIKELY (rtp_jitter_buffer_get_ts_diff (priv->jbuf) >= latency_ts)) {
1402 old_buf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
1404 GST_DEBUG_OBJECT (jitterbuffer, "Queue full, dropping old packet %p",
1407 gst_buffer_unref (old_buf);
1411 /* we need to make the metadata writable before pushing it in the jitterbuffer
1412 * because the jitterbuffer will update the timestamp */
1413 buffer = gst_buffer_make_writable (buffer);
1415 /* now insert the packet into the queue in sorted order. This function returns
1416 * FALSE if a packet with the same seqnum was already in the queue, meaning we
1417 * have a duplicate. */
1418 if (G_UNLIKELY (!rtp_jitter_buffer_insert (priv->jbuf, buffer, timestamp,
1419 priv->clock_rate, &tail, &percent)))
1422 /* we had an unhandled SR, handle it now */
1424 do_handle_sync (jitterbuffer);
1426 /* signal addition of new buffer when the _loop is waiting. */
1427 if (priv->waiting && priv->active)
1430 /* let's unschedule and unblock any waiting buffers. We only want to do this
1431 * when the tail buffer changed */
1432 if (G_UNLIKELY (priv->clock_id && tail)) {
1433 GST_DEBUG_OBJECT (jitterbuffer, "Unscheduling waiting new buffer");
1434 gst_clock_id_unschedule (priv->clock_id);
1435 priv->unscheduled = TRUE;
1438 GST_DEBUG_OBJECT (jitterbuffer, "Pushed packet #%d, now %d packets, tail: %d",
1439 seqnum, rtp_jitter_buffer_num_packets (priv->jbuf), tail);
1441 check_buffering_percent (jitterbuffer, &percent);
1447 post_buffering_percent (jitterbuffer, percent);
1454 /* this is not fatal but should be filtered earlier */
1455 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
1456 ("Received invalid RTP payload, dropping"));
1457 gst_buffer_unref (buffer);
1462 GST_WARNING_OBJECT (jitterbuffer,
1463 "No clock-rate in caps!, dropping buffer");
1464 gst_buffer_unref (buffer);
1469 ret = priv->srcresult;
1470 GST_DEBUG_OBJECT (jitterbuffer, "flushing %s", gst_flow_get_name (ret));
1471 gst_buffer_unref (buffer);
1477 GST_WARNING_OBJECT (jitterbuffer, "we are EOS, refusing buffer");
1478 gst_buffer_unref (buffer);
1483 GST_WARNING_OBJECT (jitterbuffer, "Packet #%d too late as #%d was already"
1484 " popped, dropping", seqnum, priv->last_popped_seqnum);
1486 gst_buffer_unref (buffer);
1491 GST_WARNING_OBJECT (jitterbuffer, "Duplicate packet #%d detected, dropping",
1493 priv->num_duplicates++;
1494 gst_buffer_unref (buffer);
1500 apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
1502 GstRtpJitterBufferPrivate *priv;
1504 priv = jitterbuffer->priv;
1506 if (timestamp == -1)
1509 /* apply the timestamp offset, this is used for inter stream sync */
1510 timestamp += priv->ts_offset;
1511 /* add the offset, this is used when buffering */
1512 timestamp += priv->out_offset;
1517 #define GST_FLOW_WAIT GST_FLOW_CUSTOM_SUCCESS
1520 find_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
1521 guint16 seqnum, gboolean * created)
1523 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1526 gboolean found = FALSE;
1528 len = priv->timers->len;
1529 for (i = 0; i < len; i++) {
1530 timer = &g_array_index (priv->timers, TimerData, i);
1531 if (timer->seqnum == seqnum && timer->type == type) {
1537 /* not found, create */
1538 g_array_set_size (priv->timers, len + 1);
1539 timer = &g_array_index (priv->timers, TimerData, len);
1542 timer->seqnum = seqnum;
1550 static GstFlowReturn
1551 set_timer (GstRtpJitterBuffer * jitterbuffer, TimerType type,
1552 guint16 seqnum, GstClockTime timeout)
1556 GST_DEBUG_OBJECT (jitterbuffer,
1557 "set timer for seqnum %d to %" GST_TIME_FORMAT, seqnum,
1558 GST_TIME_ARGS (timeout));
1560 /* find the seqnum timer */
1561 timer = find_timer (jitterbuffer, type, seqnum, NULL);
1562 timer->timeout = timeout;
1564 return GST_FLOW_WAIT;
1568 remove_timer (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1570 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1574 GST_DEBUG_OBJECT (jitterbuffer, "removed index %d", idx);
1575 g_array_remove_index_fast (priv->timers, idx);
1580 remove_all_timers (GstRtpJitterBuffer * jitterbuffer)
1582 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1583 GST_DEBUG_OBJECT (jitterbuffer, "removed all timers");
1584 g_array_set_size (priv->timers, 0);
1588 compute_elapsed (GstRtpJitterBuffer * jitterbuffer, GstBuffer * outbuf)
1590 guint64 ext_time, elapsed;
1592 GstRtpJitterBufferPrivate *priv;
1593 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
1595 priv = jitterbuffer->priv;
1596 gst_rtp_buffer_map (outbuf, GST_MAP_READ, &rtp);
1597 rtp_time = gst_rtp_buffer_get_timestamp (&rtp);
1598 gst_rtp_buffer_unmap (&rtp);
1600 GST_LOG_OBJECT (jitterbuffer, "rtp %" G_GUINT32_FORMAT ", ext %"
1601 G_GUINT64_FORMAT, rtp_time, priv->ext_timestamp);
1603 if (rtp_time < priv->ext_timestamp) {
1604 ext_time = priv->ext_timestamp;
1606 ext_time = gst_rtp_buffer_ext_timestamp (&priv->ext_timestamp, rtp_time);
1609 if (ext_time > priv->clock_base)
1610 elapsed = ext_time - priv->clock_base;
1614 elapsed = gst_util_uint64_scale_int (elapsed, GST_SECOND, priv->clock_rate);
1619 update_estimated_eos (GstRtpJitterBuffer * jitterbuffer, GstBuffer * outbuf)
1621 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1623 if (priv->npt_stop != -1 && priv->ext_timestamp != -1
1624 && priv->clock_base != -1 && priv->clock_rate > 0) {
1625 guint64 elapsed, estimated;
1627 elapsed = compute_elapsed (jitterbuffer, outbuf);
1629 if (elapsed > priv->last_elapsed || !priv->last_elapsed) {
1631 GstClockTime out_time;
1633 priv->last_elapsed = elapsed;
1635 left = priv->npt_stop - priv->npt_start;
1636 GST_LOG_OBJECT (jitterbuffer, "left %" GST_TIME_FORMAT,
1637 GST_TIME_ARGS (left));
1639 out_time = GST_BUFFER_PTS (outbuf);
1642 estimated = gst_util_uint64_scale (out_time, left, elapsed);
1644 /* if there is almost nothing left,
1645 * we may never advance enough to end up in the above case */
1646 if (left < GST_SECOND)
1647 estimated = GST_SECOND;
1652 GST_LOG_OBJECT (jitterbuffer, "elapsed %" GST_TIME_FORMAT ", estimated %"
1653 GST_TIME_FORMAT, GST_TIME_ARGS (elapsed), GST_TIME_ARGS (estimated));
1655 if (estimated != -1 && priv->estimated_eos != estimated) {
1656 set_timer (jitterbuffer, TIMER_TYPE_EOS, -1, estimated);
1657 priv->estimated_eos = estimated;
1663 /* take a buffer from the queue and push it */
1664 static GstFlowReturn
1665 pop_and_push_next (GstRtpJitterBuffer * jitterbuffer, guint16 seqnum,
1668 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1669 GstFlowReturn result;
1672 GstClockTime out_time;
1674 /* when we get here we are ready to pop and push the buffer */
1675 outbuf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
1677 check_buffering_percent (jitterbuffer, &percent);
1679 if (G_UNLIKELY (priv->discont)) {
1680 /* set DISCONT flag when we missed a packet. We pushed the buffer writable
1681 * into the jitterbuffer so we can modify now. */
1682 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1683 priv->discont = FALSE;
1685 if (G_UNLIKELY (priv->ts_discont)) {
1686 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
1687 priv->ts_discont = FALSE;
1690 /* apply timestamp with offset to buffer now */
1691 out_time = apply_offset (jitterbuffer, pts);
1692 GST_BUFFER_PTS (outbuf) = out_time;
1693 GST_BUFFER_DTS (outbuf) = out_time;
1695 /* update the elapsed time when we need to check against the npt stop time. */
1696 update_estimated_eos (jitterbuffer, outbuf);
1698 /* now we are ready to push the buffer. Save the seqnum and release the lock
1699 * so the other end can push stuff in the queue again. */
1700 priv->last_popped_seqnum = seqnum;
1701 priv->last_out_time = out_time;
1702 priv->last_out_pts = pts;
1703 priv->next_seqnum = (seqnum + 1) & 0xffff;
1707 post_buffering_percent (jitterbuffer, percent);
1710 GST_DEBUG_OBJECT (jitterbuffer,
1711 "Pushing buffer %d, timestamp %" GST_TIME_FORMAT, seqnum,
1712 GST_TIME_ARGS (out_time));
1713 result = gst_pad_push (priv->srcpad, outbuf);
1715 JBUF_LOCK_CHECK (priv, out_flushing);
1722 return priv->srcresult;
1727 estimate_pts (GstRtpJitterBuffer * jitterbuffer, GstClockTime pts, gint gap)
1729 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1730 GstClockTime duration;
1732 if (pts == -1 || priv->last_out_pts == -1)
1735 GST_DEBUG_OBJECT (jitterbuffer,
1736 "pts %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT,
1737 GST_TIME_ARGS (pts), GST_TIME_ARGS (priv->last_out_pts));
1739 /* interpolate between the current time and the last time based on
1740 * number of packets we are missing, this is the estimated duration
1741 * for the missing packet based on equidistant packet spacing. Also make
1742 * sure we never go negative. */
1743 if (pts >= priv->last_out_pts)
1744 duration = (pts - priv->last_out_pts) / (gap + 1);
1746 /* packet already lost, timer will timeout quickly */
1749 GST_DEBUG_OBJECT (jitterbuffer, "duration %" GST_TIME_FORMAT,
1750 GST_TIME_ARGS (duration));
1752 /* add this duration to the timestamp of the last packet we pushed */
1753 pts = (priv->last_out_pts + duration);
1758 /* Peek a buffer and compare the seqnum to the expected seqnum.
1759 * If all is fine, the buffer is pushed.
1760 * If something is wrong, a timeout is set. We set 2 kinds of timeouts:
1761 * * deadline: to the ultimate time we can still push the packet. We
1762 * do this for the first packet to make sure we have the previous
1764 * * lost: the ultimate time we can receive a packet before we have
1765 * to consider it lost. We estimate this based on the packet
1768 static GstFlowReturn
1769 handle_next_buffer (GstRtpJitterBuffer * jitterbuffer)
1771 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1772 GstFlowReturn result = GST_FLOW_OK;
1776 guint32 next_seqnum;
1778 GstRTPBuffer rtp = { NULL, };
1780 /* only push buffers when PLAYING and active and not buffering */
1781 if (priv->blocked || !priv->active ||
1782 rtp_jitter_buffer_is_buffering (priv->jbuf))
1783 return GST_FLOW_WAIT;
1786 /* peek a buffer, we're just looking at the sequence number.
1787 * If all is fine, we'll pop and push it. If the sequence number is wrong we
1788 * wait on the timestamp. In the chain function we will unlock the wait when a
1789 * new buffer is available. The peeked buffer is valid for as long as we hold
1790 * the jitterbuffer lock. */
1791 outbuf = rtp_jitter_buffer_peek (priv->jbuf);
1795 /* get the seqnum and the next expected seqnum */
1796 gst_rtp_buffer_map (outbuf, GST_MAP_READ, &rtp);
1797 seqnum = gst_rtp_buffer_get_seq (&rtp);
1798 gst_rtp_buffer_unmap (&rtp);
1800 next_seqnum = priv->next_seqnum;
1802 /* get the timestamp, this is already corrected for clock skew by the
1804 pts = GST_BUFFER_PTS (outbuf);
1806 /* get the gap between this and the previous packet. If we don't know the
1807 * previous packet seqnum assume no gap. */
1808 if (G_UNLIKELY (next_seqnum == -1)) {
1809 GST_DEBUG_OBJECT (jitterbuffer, "First buffer #%d", seqnum);
1810 /* we don't know what the next_seqnum should be, wait for the last
1811 * possible moment to push this buffer, maybe we get an earlier seqnum
1813 result = set_timer (jitterbuffer, TIMER_TYPE_DEADLINE, seqnum, pts);
1815 /* else calculate GAP */
1816 gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum);
1818 if (G_LIKELY (gap == 0)) {
1819 /* no missing packet, pop and push */
1820 result = pop_and_push_next (jitterbuffer, seqnum, pts);
1821 } else if (G_UNLIKELY (gap < 0)) {
1822 /* if we have a packet that we already pushed or considered dropped, pop it
1823 * off and get the next packet */
1824 GST_DEBUG_OBJECT (jitterbuffer, "Old packet #%d, next #%d dropping",
1825 seqnum, next_seqnum);
1826 outbuf = rtp_jitter_buffer_pop (priv->jbuf, NULL);
1827 gst_buffer_unref (outbuf);
1830 GST_DEBUG_OBJECT (jitterbuffer,
1831 "Sequence number GAP detected: expected %d instead of %d (%d missing)",
1832 next_seqnum, seqnum, gap);
1833 /* packet missing, estimate when we should ultimately push this packet */
1834 pts = estimate_pts (jitterbuffer, pts, gap);
1835 /* and set a timer for it */
1836 result = set_timer (jitterbuffer, TIMER_TYPE_LOST, next_seqnum, pts);
1843 GST_DEBUG_OBJECT (jitterbuffer, "no buffer, going to wait");
1844 return GST_FLOW_WAIT;
1848 /* a packet is lost */
1849 static GstFlowReturn
1850 do_lost_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer,
1851 GstClockTimeDiff clock_jitter)
1853 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1854 GstClockTime duration = GST_CLOCK_TIME_NONE;
1855 guint32 lost_packets = 1;
1856 gboolean lost_packets_late = FALSE;
1859 if (clock_jitter > 0
1860 && clock_jitter > (priv->latency_ns + priv->peer_latency)) {
1861 GstClockTimeDiff total_duration;
1862 GstClockTime out_time_diff;
1865 apply_offset (jitterbuffer, timer->timeout) - timer->timeout;
1866 total_duration = MIN (out_time_diff, clock_jitter);
1869 lost_packets = total_duration / duration;
1872 total_duration = lost_packets * duration;
1874 GST_DEBUG_OBJECT (jitterbuffer,
1875 "Current sync_time has expired a long time ago (+%" GST_TIME_FORMAT
1876 ") Cover up %d lost packets with duration %" GST_TIME_FORMAT,
1877 GST_TIME_ARGS (clock_jitter),
1878 lost_packets, GST_TIME_ARGS (total_duration));
1880 duration = total_duration;
1881 lost_packets_late = TRUE;
1885 /* we had a gap and thus we lost some packets. Create an event for this. */
1886 if (lost_packets > 1)
1887 GST_DEBUG_OBJECT (jitterbuffer, "Packets #%d -> #%d lost", timer->seqnum,
1888 timer->seqnum + lost_packets - 1);
1890 GST_DEBUG_OBJECT (jitterbuffer, "Packet #%d lost", timer->seqnum);
1892 priv->num_late += lost_packets;
1893 priv->discont = TRUE;
1895 /* update our expected next packet */
1896 priv->last_popped_seqnum = timer->seqnum;
1897 priv->last_out_time = apply_offset (jitterbuffer, timer->timeout);
1898 priv->last_out_pts = timer->timeout;
1899 priv->next_seqnum = (timer->seqnum + lost_packets) & 0xffff;
1900 /* remove timer now */
1901 remove_timer (jitterbuffer, timer);
1903 if (priv->do_lost) {
1906 /* create paket lost event */
1907 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1908 gst_structure_new ("GstRTPPacketLost",
1909 "seqnum", G_TYPE_UINT, (guint) priv->last_popped_seqnum,
1910 "timestamp", G_TYPE_UINT64, priv->last_out_time,
1911 "duration", G_TYPE_UINT64, duration,
1912 "late", G_TYPE_BOOLEAN, lost_packets_late, NULL));
1914 gst_pad_push_event (priv->srcpad, event);
1915 JBUF_LOCK_CHECK (priv, flushing);
1922 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1923 return priv->srcresult;
1927 static GstFlowReturn
1928 do_eos_timeout (GstRtpJitterBuffer * jitterbuffer, TimerData * timer)
1930 GST_INFO_OBJECT (jitterbuffer, "got the NPT timeout");
1931 remove_timer (jitterbuffer, timer);
1933 return GST_FLOW_EOS;
1936 /* called when we need to wait for the next timeout.
1938 * We loop over the array of recorded timeouts and wait for the earliest one.
1939 * When it timed out, do the logic associated with the timer.
1941 * If there are no timers, we wait on a gcond until something new happens.
1943 static GstFlowReturn
1944 wait_next_timeout (GstRtpJitterBuffer * jitterbuffer)
1946 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1947 GstFlowReturn result = GST_FLOW_OK;
1949 TimerData *timer = NULL;
1951 len = priv->timers->len;
1952 for (i = 0; i < len; i++) {
1953 TimerData *test = &g_array_index (priv->timers, TimerData, i);
1955 /* find the smallest timeout */
1956 if (timer == NULL || test->timeout == -1 || test->timeout < timer->timeout)
1961 GstClockTime sync_time;
1964 GstClockTimeDiff clock_jitter;
1966 /* no timestamp, timeout immeditately */
1967 if (timer->timeout == -1)
1970 GST_OBJECT_LOCK (jitterbuffer);
1971 clock = GST_ELEMENT_CLOCK (jitterbuffer);
1973 GST_OBJECT_UNLOCK (jitterbuffer);
1974 /* let's just push if there is no clock */
1975 GST_DEBUG_OBJECT (jitterbuffer, "No clock, timeout right away");
1979 /* prepare for sync against clock */
1980 sync_time = timer->timeout + GST_ELEMENT_CAST (jitterbuffer)->base_time;
1981 /* add latency of peer to get input time */
1982 sync_time += priv->peer_latency;
1984 /* add our latency and offset to get output times. */
1985 sync_time = apply_offset (jitterbuffer, sync_time);
1986 sync_time += priv->latency_ns;
1988 GST_DEBUG_OBJECT (jitterbuffer, "sync to timestamp %" GST_TIME_FORMAT
1989 " with sync time %" GST_TIME_FORMAT,
1990 GST_TIME_ARGS (timer->timeout), GST_TIME_ARGS (sync_time));
1992 /* create an entry for the clock */
1993 id = priv->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
1994 priv->unscheduled = FALSE;
1995 GST_OBJECT_UNLOCK (jitterbuffer);
1997 /* release the lock so that the other end can push stuff or unlock */
2000 ret = gst_clock_id_wait (id, &clock_jitter);
2003 GST_DEBUG_OBJECT (jitterbuffer, "sync done, %d, %" G_GINT64_FORMAT,
2005 /* and free the entry */
2006 gst_clock_id_unref (id);
2007 priv->clock_id = NULL;
2009 /* at this point, the clock could have been unlocked by a timeout, a new
2010 * tail element was added to the queue or because we are shutting down. Check
2011 * for shutdown first. */
2013 ((priv->srcresult != GST_FLOW_OK))
2016 /* if we got unscheduled and we are not flushing, it's because a new tail
2017 * element became available in the queue or we flushed the queue.
2018 * Grab it and try to push or sync. */
2019 if (ret == GST_CLOCK_UNSCHEDULED || priv->unscheduled) {
2020 GST_DEBUG_OBJECT (jitterbuffer, "Wait got unscheduled");
2024 switch (timer->type) {
2025 case TIMER_TYPE_EXPECTED:
2026 remove_timer (jitterbuffer, timer);
2028 case TIMER_TYPE_LOST:
2029 result = do_lost_timeout (jitterbuffer, timer, clock_jitter);
2031 case TIMER_TYPE_DEADLINE:
2032 priv->next_seqnum = timer->seqnum;
2033 remove_timer (jitterbuffer, timer);
2035 case TIMER_TYPE_EOS:
2036 result = do_eos_timeout (jitterbuffer, timer);
2040 /* no timers, wait for activity */
2041 GST_DEBUG_OBJECT (jitterbuffer, "waiting");
2042 priv->waiting = TRUE;
2043 JBUF_WAIT_CHECK (priv, flushing);
2044 priv->waiting = FALSE;
2045 GST_DEBUG_OBJECT (jitterbuffer, "waiting done");
2053 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
2054 return priv->srcresult;
2059 * This funcion implements the main pushing loop on the source pad.
2061 * It first tries to push as many buffers as possible. If there is a seqnum
2062 * mismatch, a timeout is created and this function goes waiting for the
2066 gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer)
2068 GstRtpJitterBufferPrivate *priv;
2069 GstFlowReturn result;
2071 priv = jitterbuffer->priv;
2073 JBUF_LOCK_CHECK (priv, flushing);
2075 result = handle_next_buffer (jitterbuffer);
2076 if (G_LIKELY (result == GST_FLOW_WAIT))
2077 /* now wait for the next event */
2078 result = wait_next_timeout (jitterbuffer);
2080 while (result == GST_FLOW_OK);
2083 /* if we get here we need to pause */
2089 result = priv->srcresult;
2095 const gchar *reason = gst_flow_get_name (result);
2098 GST_DEBUG_OBJECT (jitterbuffer, "pausing task, reason %s", reason);
2099 gst_pad_pause_task (priv->srcpad);
2100 if (result == GST_FLOW_EOS) {
2101 event = gst_event_new_eos ();
2102 gst_pad_push_event (priv->srcpad, event);
2108 /* collect the info from the lastest RTCP packet and the jitterbuffer sync, do
2109 * some sanity checks and then emit the handle-sync signal with the parameters.
2110 * This function must be called with the LOCK */
2112 do_handle_sync (GstRtpJitterBuffer * jitterbuffer)
2114 GstRtpJitterBufferPrivate *priv;
2115 guint64 base_rtptime, base_time;
2117 guint64 last_rtptime;
2119 guint64 ext_rtptime, diff;
2120 gboolean drop = FALSE;
2122 priv = jitterbuffer->priv;
2124 /* get the last values from the jitterbuffer */
2125 rtp_jitter_buffer_get_sync (priv->jbuf, &base_rtptime, &base_time,
2126 &clock_rate, &last_rtptime);
2128 clock_base = priv->clock_base;
2129 ext_rtptime = priv->ext_rtptime;
2131 GST_DEBUG_OBJECT (jitterbuffer, "ext SR %" G_GUINT64_FORMAT ", base %"
2132 G_GUINT64_FORMAT ", clock-rate %" G_GUINT32_FORMAT
2133 ", clock-base %" G_GUINT64_FORMAT ", last-rtptime %" G_GUINT64_FORMAT,
2134 ext_rtptime, base_rtptime, clock_rate, clock_base, last_rtptime);
2136 if (base_rtptime == -1 || clock_rate == -1 || base_time == -1) {
2137 GST_DEBUG_OBJECT (jitterbuffer, "dropping, no RTP values");
2140 /* we can't accept anything that happened before we did the last resync */
2141 if (base_rtptime > ext_rtptime) {
2142 GST_DEBUG_OBJECT (jitterbuffer, "dropping, older than base time");
2145 /* the SR RTP timestamp must be something close to what we last observed
2146 * in the jitterbuffer */
2147 if (ext_rtptime > last_rtptime) {
2148 /* check how far ahead it is to our RTP timestamps */
2149 diff = ext_rtptime - last_rtptime;
2150 /* if bigger than 1 second, we drop it */
2151 if (diff > clock_rate) {
2152 GST_DEBUG_OBJECT (jitterbuffer, "too far ahead");
2153 /* should drop this, but some RTSP servers end up with bogus
2154 * way too ahead RTCP packet when repeated PAUSE/PLAY,
2155 * so still trigger rptbin sync but invalidate RTCP data
2156 * (sync might use other methods) */
2159 GST_DEBUG_OBJECT (jitterbuffer, "ext last %" G_GUINT64_FORMAT ", diff %"
2160 G_GUINT64_FORMAT, last_rtptime, diff);
2168 s = gst_structure_new ("application/x-rtp-sync",
2169 "base-rtptime", G_TYPE_UINT64, base_rtptime,
2170 "base-time", G_TYPE_UINT64, base_time,
2171 "clock-rate", G_TYPE_UINT, clock_rate,
2172 "clock-base", G_TYPE_UINT64, clock_base,
2173 "sr-ext-rtptime", G_TYPE_UINT64, ext_rtptime,
2174 "sr-buffer", GST_TYPE_BUFFER, priv->last_sr, NULL);
2176 GST_DEBUG_OBJECT (jitterbuffer, "signaling sync");
2177 gst_buffer_replace (&priv->last_sr, NULL);
2179 g_signal_emit (jitterbuffer,
2180 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC], 0, s);
2182 gst_structure_free (s);
2184 GST_DEBUG_OBJECT (jitterbuffer, "dropping RTCP packet");
2188 static GstFlowReturn
2189 gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad, GstObject * parent,
2192 GstRtpJitterBuffer *jitterbuffer;
2193 GstRtpJitterBufferPrivate *priv;
2194 GstFlowReturn ret = GST_FLOW_OK;
2196 GstRTCPPacket packet;
2197 guint64 ext_rtptime;
2199 GstRTCPBuffer rtcp = { NULL, };
2201 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
2203 if (G_UNLIKELY (!gst_rtcp_buffer_validate (buffer)))
2204 goto invalid_buffer;
2206 priv = jitterbuffer->priv;
2208 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2210 if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet))
2213 /* first packet must be SR or RR or else the validate would have failed */
2214 switch (gst_rtcp_packet_get_type (&packet)) {
2215 case GST_RTCP_TYPE_SR:
2216 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, &rtptime,
2222 gst_rtcp_buffer_unmap (&rtcp);
2224 GST_DEBUG_OBJECT (jitterbuffer, "received RTCP of SSRC %08x", ssrc);
2227 /* convert the RTP timestamp to our extended timestamp, using the same offset
2228 * we used in the jitterbuffer */
2229 ext_rtptime = priv->jbuf->ext_rtptime;
2230 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
2232 priv->ext_rtptime = ext_rtptime;
2233 gst_buffer_replace (&priv->last_sr, buffer);
2235 do_handle_sync (jitterbuffer);
2239 gst_buffer_unref (buffer);
2245 /* this is not fatal but should be filtered earlier */
2246 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2247 ("Received invalid RTCP payload, dropping"));
2253 /* this is not fatal but should be filtered earlier */
2254 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2255 ("Received empty RTCP payload, dropping"));
2256 gst_rtcp_buffer_unmap (&rtcp);
2262 GST_DEBUG_OBJECT (jitterbuffer, "ignoring RTCP packet");
2263 gst_rtcp_buffer_unmap (&rtcp);
2270 gst_rtp_jitter_buffer_sink_query (GstPad * pad, GstObject * parent,
2273 gboolean res = FALSE;
2275 switch (GST_QUERY_TYPE (query)) {
2276 case GST_QUERY_CAPS:
2278 GstCaps *filter, *caps;
2280 gst_query_parse_caps (query, &filter);
2281 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
2282 gst_query_set_caps_result (query, caps);
2283 gst_caps_unref (caps);
2288 if (GST_QUERY_IS_SERIALIZED (query)) {
2289 GST_WARNING_OBJECT (pad, "unhandled serialized query");
2292 res = gst_pad_query_default (pad, parent, query);
2300 gst_rtp_jitter_buffer_src_query (GstPad * pad, GstObject * parent,
2303 GstRtpJitterBuffer *jitterbuffer;
2304 GstRtpJitterBufferPrivate *priv;
2305 gboolean res = FALSE;
2307 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
2308 priv = jitterbuffer->priv;
2310 switch (GST_QUERY_TYPE (query)) {
2311 case GST_QUERY_LATENCY:
2313 /* We need to send the query upstream and add the returned latency to our
2315 GstClockTime min_latency, max_latency;
2317 GstClockTime our_latency;
2319 if ((res = gst_pad_peer_query (priv->sinkpad, query))) {
2320 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
2322 GST_DEBUG_OBJECT (jitterbuffer, "Peer latency: min %"
2323 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
2324 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2326 /* store this so that we can safely sync on the peer buffers. */
2328 priv->peer_latency = min_latency;
2329 our_latency = priv->latency_ns;
2332 GST_DEBUG_OBJECT (jitterbuffer, "Our latency: %" GST_TIME_FORMAT,
2333 GST_TIME_ARGS (our_latency));
2335 /* we add some latency but can buffer an infinite amount of time */
2336 min_latency += our_latency;
2339 GST_DEBUG_OBJECT (jitterbuffer, "Calculated total latency : min %"
2340 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
2341 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2343 gst_query_set_latency (query, TRUE, min_latency, max_latency);
2347 case GST_QUERY_POSITION:
2349 GstClockTime start, last_out;
2352 gst_query_parse_position (query, &fmt, NULL);
2353 if (fmt != GST_FORMAT_TIME) {
2354 res = gst_pad_query_default (pad, parent, query);
2359 start = priv->npt_start;
2360 last_out = priv->last_out_time;
2363 GST_DEBUG_OBJECT (jitterbuffer, "npt start %" GST_TIME_FORMAT
2364 ", last out %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
2365 GST_TIME_ARGS (last_out));
2367 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (last_out)) {
2368 /* bring 0-based outgoing time to stream time */
2369 gst_query_set_position (query, GST_FORMAT_TIME, start + last_out);
2372 res = gst_pad_query_default (pad, parent, query);
2376 case GST_QUERY_CAPS:
2378 GstCaps *filter, *caps;
2380 gst_query_parse_caps (query, &filter);
2381 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
2382 gst_query_set_caps_result (query, caps);
2383 gst_caps_unref (caps);
2388 res = gst_pad_query_default (pad, parent, query);
2396 gst_rtp_jitter_buffer_set_property (GObject * object,
2397 guint prop_id, const GValue * value, GParamSpec * pspec)
2399 GstRtpJitterBuffer *jitterbuffer;
2400 GstRtpJitterBufferPrivate *priv;
2402 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
2403 priv = jitterbuffer->priv;
2408 guint new_latency, old_latency;
2410 new_latency = g_value_get_uint (value);
2413 old_latency = priv->latency_ms;
2414 priv->latency_ms = new_latency;
2415 priv->latency_ns = priv->latency_ms * GST_MSECOND;
2416 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
2419 /* post message if latency changed, this will inform the parent pipeline
2420 * that a latency reconfiguration is possible/needed. */
2421 if (new_latency != old_latency) {
2422 GST_DEBUG_OBJECT (jitterbuffer, "latency changed to: %" GST_TIME_FORMAT,
2423 GST_TIME_ARGS (new_latency * GST_MSECOND));
2425 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer),
2426 gst_message_new_latency (GST_OBJECT_CAST (jitterbuffer)));
2430 case PROP_DROP_ON_LATENCY:
2432 priv->drop_on_latency = g_value_get_boolean (value);
2435 case PROP_TS_OFFSET:
2437 priv->ts_offset = g_value_get_int64 (value);
2438 priv->ts_discont = TRUE;
2443 priv->do_lost = g_value_get_boolean (value);
2448 rtp_jitter_buffer_set_mode (priv->jbuf, g_value_get_enum (value));
2452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2458 gst_rtp_jitter_buffer_get_property (GObject * object,
2459 guint prop_id, GValue * value, GParamSpec * pspec)
2461 GstRtpJitterBuffer *jitterbuffer;
2462 GstRtpJitterBufferPrivate *priv;
2464 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
2465 priv = jitterbuffer->priv;
2470 g_value_set_uint (value, priv->latency_ms);
2473 case PROP_DROP_ON_LATENCY:
2475 g_value_set_boolean (value, priv->drop_on_latency);
2478 case PROP_TS_OFFSET:
2480 g_value_set_int64 (value, priv->ts_offset);
2485 g_value_set_boolean (value, priv->do_lost);
2490 g_value_set_enum (value, rtp_jitter_buffer_get_mode (priv->jbuf));
2498 if (priv->srcresult != GST_FLOW_OK)
2501 percent = rtp_jitter_buffer_get_percent (priv->jbuf);
2503 g_value_set_int (value, percent);
2508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);