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 /* the next expected seqnum we receive */
152 guint32 next_in_seqnum;
154 /* start and stop ranges */
155 GstClockTime npt_start;
156 GstClockTime npt_stop;
157 guint64 ext_timestamp;
158 guint64 last_elapsed;
159 guint64 estimated_eos;
161 gboolean reached_npt_stop;
166 /* clock rate and rtp timestamp offset */
170 gint64 prev_ts_offset;
172 /* when we are shutting down */
173 GstFlowReturn srcresult;
179 gboolean unscheduled;
180 /* the latency of the upstream peer, we have to take this into account when
181 * synchronizing the buffers. */
182 GstClockTime peer_latency;
186 /* some accounting */
188 guint64 num_duplicates;
191 #define GST_RTP_JITTER_BUFFER_GET_PRIVATE(o) \
192 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_RTP_JITTER_BUFFER, \
193 GstRtpJitterBufferPrivate))
195 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_template =
196 GST_STATIC_PAD_TEMPLATE ("sink",
199 GST_STATIC_CAPS ("application/x-rtp, "
200 "clock-rate = (int) [ 1, 2147483647 ]"
201 /* "payload = (int) , "
202 * "encoding-name = (string) "
206 static GstStaticPadTemplate gst_rtp_jitter_buffer_sink_rtcp_template =
207 GST_STATIC_PAD_TEMPLATE ("sink_rtcp",
210 GST_STATIC_CAPS ("application/x-rtcp")
213 static GstStaticPadTemplate gst_rtp_jitter_buffer_src_template =
214 GST_STATIC_PAD_TEMPLATE ("src",
217 GST_STATIC_CAPS ("application/x-rtp"
218 /* "payload = (int) , "
219 * "clock-rate = (int) , "
220 * "encoding-name = (string) "
224 static guint gst_rtp_jitter_buffer_signals[LAST_SIGNAL] = { 0 };
226 #define gst_rtp_jitter_buffer_parent_class parent_class
227 G_DEFINE_TYPE (GstRtpJitterBuffer, gst_rtp_jitter_buffer, GST_TYPE_ELEMENT);
229 /* object overrides */
230 static void gst_rtp_jitter_buffer_set_property (GObject * object,
231 guint prop_id, const GValue * value, GParamSpec * pspec);
232 static void gst_rtp_jitter_buffer_get_property (GObject * object,
233 guint prop_id, GValue * value, GParamSpec * pspec);
234 static void gst_rtp_jitter_buffer_finalize (GObject * object);
236 /* element overrides */
237 static GstStateChangeReturn gst_rtp_jitter_buffer_change_state (GstElement
238 * element, GstStateChange transition);
239 static GstPad *gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
240 GstPadTemplate * templ, const gchar * name, const GstCaps * filter);
241 static void gst_rtp_jitter_buffer_release_pad (GstElement * element,
243 static GstClock *gst_rtp_jitter_buffer_provide_clock (GstElement * element);
246 static GstCaps *gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter);
247 static GstIterator *gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad,
250 /* sinkpad overrides */
251 static gboolean gst_rtp_jitter_buffer_sink_event (GstPad * pad,
252 GstObject * parent, GstEvent * event);
253 static GstFlowReturn gst_rtp_jitter_buffer_chain (GstPad * pad,
254 GstObject * parent, GstBuffer * buffer);
256 static gboolean gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad,
257 GstObject * parent, GstEvent * event);
258 static GstFlowReturn gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad,
259 GstObject * parent, GstBuffer * buffer);
261 static gboolean gst_rtp_jitter_buffer_sink_query (GstPad * pad,
262 GstObject * parent, GstQuery * query);
264 /* srcpad overrides */
265 static gboolean gst_rtp_jitter_buffer_src_event (GstPad * pad,
266 GstObject * parent, GstEvent * event);
267 static gboolean gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad,
268 GstObject * parent, GstPadMode mode, gboolean active);
269 static void gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer);
270 static gboolean gst_rtp_jitter_buffer_src_query (GstPad * pad,
271 GstObject * parent, GstQuery * query);
274 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer);
276 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jitterbuffer,
277 gboolean active, guint64 base_time);
278 static void do_handle_sync (GstRtpJitterBuffer * jitterbuffer);
281 gst_rtp_jitter_buffer_class_init (GstRtpJitterBufferClass * klass)
283 GObjectClass *gobject_class;
284 GstElementClass *gstelement_class;
286 gobject_class = (GObjectClass *) klass;
287 gstelement_class = (GstElementClass *) klass;
289 g_type_class_add_private (klass, sizeof (GstRtpJitterBufferPrivate));
291 gobject_class->finalize = gst_rtp_jitter_buffer_finalize;
293 gobject_class->set_property = gst_rtp_jitter_buffer_set_property;
294 gobject_class->get_property = gst_rtp_jitter_buffer_get_property;
297 * GstRtpJitterBuffer::latency:
299 * The maximum latency of the jitterbuffer. Packets will be kept in the buffer
300 * for at most this time.
302 g_object_class_install_property (gobject_class, PROP_LATENCY,
303 g_param_spec_uint ("latency", "Buffer latency in ms",
304 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
305 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 * GstRtpJitterBuffer::drop-on-latency:
309 * Drop oldest buffers when the queue is completely filled.
311 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
312 g_param_spec_boolean ("drop-on-latency",
313 "Drop buffers when maximum latency is reached",
314 "Tells the jitterbuffer to never exceed the given latency in size",
315 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 * GstRtpJitterBuffer::ts-offset:
319 * Adjust GStreamer output buffer timestamps in the jitterbuffer with offset.
320 * This is mainly used to ensure interstream synchronisation.
322 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
323 g_param_spec_int64 ("ts-offset", "Timestamp Offset",
324 "Adjust buffer timestamps with offset in nanoseconds", G_MININT64,
325 G_MAXINT64, DEFAULT_TS_OFFSET,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 * GstRtpJitterBuffer::do-lost:
331 * Send out a GstRTPPacketLost event downstream when a packet is considered
334 g_object_class_install_property (gobject_class, PROP_DO_LOST,
335 g_param_spec_boolean ("do-lost", "Do Lost",
336 "Send an event downstream when a packet is lost", DEFAULT_DO_LOST,
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 * GstRtpJitterBuffer::mode:
342 * Control the buffering and timestamping mode used by the jitterbuffer.
344 g_object_class_install_property (gobject_class, PROP_MODE,
345 g_param_spec_enum ("mode", "Mode",
346 "Control the buffering algorithm in use", RTP_TYPE_JITTER_BUFFER_MODE,
347 DEFAULT_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 * GstRtpJitterBuffer::percent:
351 * The percent of the jitterbuffer that is filled.
355 g_object_class_install_property (gobject_class, PROP_PERCENT,
356 g_param_spec_int ("percent", "percent",
357 "The buffer filled percent", 0, 100,
358 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
360 * GstRtpJitterBuffer::request-pt-map:
361 * @buffer: the object which received the signal
364 * Request the payload type as #GstCaps for @pt.
366 gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP] =
367 g_signal_new ("request-pt-map", G_TYPE_FROM_CLASS (klass),
368 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
369 request_pt_map), NULL, NULL, gst_rtp_bin_marshal_BOXED__UINT,
370 GST_TYPE_CAPS, 1, G_TYPE_UINT);
372 * GstRtpJitterBuffer::handle-sync:
373 * @buffer: the object which received the signal
374 * @struct: a GstStructure containing sync values.
376 * Be notified of new sync values.
378 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC] =
379 g_signal_new ("handle-sync", G_TYPE_FROM_CLASS (klass),
380 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
381 handle_sync), NULL, NULL, g_cclosure_marshal_VOID__BOXED,
382 G_TYPE_NONE, 1, GST_TYPE_STRUCTURE | G_SIGNAL_TYPE_STATIC_SCOPE);
385 * GstRtpJitterBuffer::on-npt-stop
386 * @buffer: the object which received the signal
388 * Signal that the jitterbufer has pushed the RTP packet that corresponds to
389 * the npt-stop position.
391 gst_rtp_jitter_buffer_signals[SIGNAL_ON_NPT_STOP] =
392 g_signal_new ("on-npt-stop", G_TYPE_FROM_CLASS (klass),
393 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRtpJitterBufferClass,
394 on_npt_stop), NULL, NULL, g_cclosure_marshal_VOID__VOID,
395 G_TYPE_NONE, 0, G_TYPE_NONE);
398 * GstRtpJitterBuffer::clear-pt-map:
399 * @buffer: the object which received the signal
401 * Invalidate the clock-rate as obtained with the
402 * #GstRtpJitterBuffer::request-pt-map signal.
404 gst_rtp_jitter_buffer_signals[SIGNAL_CLEAR_PT_MAP] =
405 g_signal_new ("clear-pt-map", G_TYPE_FROM_CLASS (klass),
406 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
407 G_STRUCT_OFFSET (GstRtpJitterBufferClass, clear_pt_map), NULL, NULL,
408 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
411 * GstRtpJitterBuffer::set-active:
412 * @buffer: the object which received the signal
414 * Start pushing out packets with the given base time. This signal is only
415 * useful in buffering mode.
417 * Returns: the time of the last pushed packet.
421 gst_rtp_jitter_buffer_signals[SIGNAL_SET_ACTIVE] =
422 g_signal_new ("set-active", G_TYPE_FROM_CLASS (klass),
423 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
424 G_STRUCT_OFFSET (GstRtpJitterBufferClass, set_active), NULL, NULL,
425 gst_rtp_bin_marshal_UINT64__BOOL_UINT64, G_TYPE_UINT64, 2, G_TYPE_BOOLEAN,
428 gstelement_class->change_state =
429 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_change_state);
430 gstelement_class->request_new_pad =
431 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_request_new_pad);
432 gstelement_class->release_pad =
433 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_release_pad);
434 gstelement_class->provide_clock =
435 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_provide_clock);
437 gst_element_class_add_pad_template (gstelement_class,
438 gst_static_pad_template_get (&gst_rtp_jitter_buffer_src_template));
439 gst_element_class_add_pad_template (gstelement_class,
440 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_template));
441 gst_element_class_add_pad_template (gstelement_class,
442 gst_static_pad_template_get (&gst_rtp_jitter_buffer_sink_rtcp_template));
444 gst_element_class_set_static_metadata (gstelement_class,
445 "RTP packet jitter-buffer", "Filter/Network/RTP",
446 "A buffer that deals with network jitter and other transmission faults",
447 "Philippe Kalaf <philippe.kalaf@collabora.co.uk>, "
448 "Wim Taymans <wim.taymans@gmail.com>");
450 klass->clear_pt_map = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_clear_pt_map);
451 klass->set_active = GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_set_active);
453 GST_DEBUG_CATEGORY_INIT
454 (rtpjitterbuffer_debug, "gstrtpjitterbuffer", 0, "RTP Jitter Buffer");
458 gst_rtp_jitter_buffer_init (GstRtpJitterBuffer * jitterbuffer)
460 GstRtpJitterBufferPrivate *priv;
462 priv = GST_RTP_JITTER_BUFFER_GET_PRIVATE (jitterbuffer);
463 jitterbuffer->priv = priv;
465 priv->latency_ms = DEFAULT_LATENCY_MS;
466 priv->latency_ns = priv->latency_ms * GST_MSECOND;
467 priv->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
468 priv->do_lost = DEFAULT_DO_LOST;
470 priv->jbuf = rtp_jitter_buffer_new ();
471 g_mutex_init (&priv->jbuf_lock);
472 g_cond_init (&priv->jbuf_cond);
474 /* reset skew detection initialy */
475 rtp_jitter_buffer_reset_skew (priv->jbuf);
476 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
477 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
481 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_src_template,
484 gst_pad_set_activatemode_function (priv->srcpad,
485 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_activate_mode));
486 gst_pad_set_query_function (priv->srcpad,
487 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_query));
488 gst_pad_set_event_function (priv->srcpad,
489 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_src_event));
492 gst_pad_new_from_static_template (&gst_rtp_jitter_buffer_sink_template,
495 gst_pad_set_chain_function (priv->sinkpad,
496 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_chain));
497 gst_pad_set_event_function (priv->sinkpad,
498 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_event));
499 gst_pad_set_query_function (priv->sinkpad,
500 GST_DEBUG_FUNCPTR (gst_rtp_jitter_buffer_sink_query));
502 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->srcpad);
503 gst_element_add_pad (GST_ELEMENT (jitterbuffer), priv->sinkpad);
505 GST_OBJECT_FLAG_SET (jitterbuffer, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
509 gst_rtp_jitter_buffer_finalize (GObject * object)
511 GstRtpJitterBuffer *jitterbuffer;
513 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
515 g_mutex_clear (&jitterbuffer->priv->jbuf_lock);
516 g_cond_clear (&jitterbuffer->priv->jbuf_cond);
518 g_object_unref (jitterbuffer->priv->jbuf);
520 G_OBJECT_CLASS (parent_class)->finalize (object);
524 gst_rtp_jitter_buffer_iterate_internal_links (GstPad * pad, GstObject * parent)
526 GstRtpJitterBuffer *jitterbuffer;
527 GstPad *otherpad = NULL;
531 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
533 if (pad == jitterbuffer->priv->sinkpad) {
534 otherpad = jitterbuffer->priv->srcpad;
535 } else if (pad == jitterbuffer->priv->srcpad) {
536 otherpad = jitterbuffer->priv->sinkpad;
537 } else if (pad == jitterbuffer->priv->rtcpsinkpad) {
541 g_value_init (&val, GST_TYPE_PAD);
542 g_value_set_object (&val, otherpad);
543 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
544 g_value_unset (&val);
550 create_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
552 GstRtpJitterBufferPrivate *priv;
554 priv = jitterbuffer->priv;
556 GST_DEBUG_OBJECT (jitterbuffer, "creating RTCP sink pad");
559 gst_pad_new_from_static_template
560 (&gst_rtp_jitter_buffer_sink_rtcp_template, "sink_rtcp");
561 gst_pad_set_chain_function (priv->rtcpsinkpad,
562 gst_rtp_jitter_buffer_chain_rtcp);
563 gst_pad_set_event_function (priv->rtcpsinkpad,
564 (GstPadEventFunction) gst_rtp_jitter_buffer_sink_rtcp_event);
565 gst_pad_set_iterate_internal_links_function (priv->rtcpsinkpad,
566 gst_rtp_jitter_buffer_iterate_internal_links);
567 gst_pad_set_active (priv->rtcpsinkpad, TRUE);
568 gst_element_add_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
570 return priv->rtcpsinkpad;
574 remove_rtcp_sink (GstRtpJitterBuffer * jitterbuffer)
576 GstRtpJitterBufferPrivate *priv;
578 priv = jitterbuffer->priv;
580 GST_DEBUG_OBJECT (jitterbuffer, "removing RTCP sink pad");
582 gst_pad_set_active (priv->rtcpsinkpad, FALSE);
584 gst_element_remove_pad (GST_ELEMENT_CAST (jitterbuffer), priv->rtcpsinkpad);
585 priv->rtcpsinkpad = NULL;
589 gst_rtp_jitter_buffer_request_new_pad (GstElement * element,
590 GstPadTemplate * templ, const gchar * name, const GstCaps * filter)
592 GstRtpJitterBuffer *jitterbuffer;
593 GstElementClass *klass;
595 GstRtpJitterBufferPrivate *priv;
597 g_return_val_if_fail (templ != NULL, NULL);
598 g_return_val_if_fail (GST_IS_RTP_JITTER_BUFFER (element), NULL);
600 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
601 priv = jitterbuffer->priv;
602 klass = GST_ELEMENT_GET_CLASS (element);
604 GST_DEBUG_OBJECT (element, "requesting pad %s", GST_STR_NULL (name));
606 /* figure out the template */
607 if (templ == gst_element_class_get_pad_template (klass, "sink_rtcp")) {
608 if (priv->rtcpsinkpad != NULL)
611 result = create_rtcp_sink (jitterbuffer);
620 g_warning ("gstrtpjitterbuffer: this is not our template");
625 g_warning ("gstrtpjitterbuffer: pad already requested");
631 gst_rtp_jitter_buffer_release_pad (GstElement * element, GstPad * pad)
633 GstRtpJitterBuffer *jitterbuffer;
634 GstRtpJitterBufferPrivate *priv;
636 g_return_if_fail (GST_IS_RTP_JITTER_BUFFER (element));
637 g_return_if_fail (GST_IS_PAD (pad));
639 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
640 priv = jitterbuffer->priv;
642 GST_DEBUG_OBJECT (element, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
644 if (priv->rtcpsinkpad == pad) {
645 remove_rtcp_sink (jitterbuffer);
654 g_warning ("gstjitterbuffer: asked to release an unknown pad");
660 gst_rtp_jitter_buffer_provide_clock (GstElement * element)
662 return gst_system_clock_obtain ();
666 gst_rtp_jitter_buffer_clear_pt_map (GstRtpJitterBuffer * jitterbuffer)
668 GstRtpJitterBufferPrivate *priv;
670 priv = jitterbuffer->priv;
672 /* this will trigger a new pt-map request signal, FIXME, do something better. */
675 priv->clock_rate = -1;
676 /* do not clear current content, but refresh state for new arrival */
677 GST_DEBUG_OBJECT (jitterbuffer, "reset jitterbuffer");
678 rtp_jitter_buffer_reset_skew (priv->jbuf);
679 priv->last_popped_seqnum = -1;
680 priv->next_seqnum = -1;
685 gst_rtp_jitter_buffer_set_active (GstRtpJitterBuffer * jbuf, gboolean active,
688 GstRtpJitterBufferPrivate *priv;
689 GstClockTime last_out;
695 GST_DEBUG_OBJECT (jbuf, "setting active %d with offset %" GST_TIME_FORMAT,
696 active, GST_TIME_ARGS (offset));
698 if (active != priv->active) {
699 /* add the amount of time spent in paused to the output offset. All
700 * outgoing buffers will have this offset applied to their timestamps in
701 * order to make them arrive in time in the sink. */
702 priv->out_offset = offset;
703 GST_DEBUG_OBJECT (jbuf, "out offset %" GST_TIME_FORMAT,
704 GST_TIME_ARGS (priv->out_offset));
705 priv->active = active;
709 rtp_jitter_buffer_set_buffering (priv->jbuf, TRUE);
711 if ((head = rtp_jitter_buffer_peek (priv->jbuf))) {
712 /* head buffer timestamp and offset gives our output time */
713 last_out = GST_BUFFER_TIMESTAMP (head) + priv->ts_offset;
715 /* use last known time when the buffer is empty */
716 last_out = priv->last_out_time;
724 gst_rtp_jitter_buffer_getcaps (GstPad * pad, GstCaps * filter)
726 GstRtpJitterBuffer *jitterbuffer;
727 GstRtpJitterBufferPrivate *priv;
732 jitterbuffer = GST_RTP_JITTER_BUFFER (gst_pad_get_parent (pad));
733 priv = jitterbuffer->priv;
735 other = (pad == priv->srcpad ? priv->sinkpad : priv->srcpad);
737 caps = gst_pad_peer_query_caps (other, filter);
739 templ = gst_pad_get_pad_template_caps (pad);
741 GST_DEBUG_OBJECT (jitterbuffer, "use template");
746 GST_DEBUG_OBJECT (jitterbuffer, "intersect with template");
748 intersect = gst_caps_intersect (caps, templ);
749 gst_caps_unref (caps);
750 gst_caps_unref (templ);
754 gst_object_unref (jitterbuffer);
760 * Must be called with JBUF_LOCK held
764 gst_jitter_buffer_sink_parse_caps (GstRtpJitterBuffer * jitterbuffer,
767 GstRtpJitterBufferPrivate *priv;
768 GstStructure *caps_struct;
772 priv = jitterbuffer->priv;
774 /* first parse the caps */
775 caps_struct = gst_caps_get_structure (caps, 0);
777 GST_DEBUG_OBJECT (jitterbuffer, "got caps");
779 /* we need a clock-rate to convert the rtp timestamps to GStreamer time and to
780 * measure the amount of data in the buffer */
781 if (!gst_structure_get_int (caps_struct, "clock-rate", &priv->clock_rate))
784 if (priv->clock_rate <= 0)
787 GST_DEBUG_OBJECT (jitterbuffer, "got clock-rate %d", priv->clock_rate);
789 /* The clock base is the RTP timestamp corrsponding to the npt-start value. We
790 * can use this to track the amount of time elapsed on the sender. */
791 if (gst_structure_get_uint (caps_struct, "clock-base", &val))
792 priv->clock_base = val;
794 priv->clock_base = -1;
796 priv->ext_timestamp = priv->clock_base;
798 GST_DEBUG_OBJECT (jitterbuffer, "got clock-base %" G_GINT64_FORMAT,
801 if (gst_structure_get_uint (caps_struct, "seqnum-base", &val)) {
802 /* first expected seqnum, only update when we didn't have a previous base. */
803 if (priv->next_in_seqnum == -1)
804 priv->next_in_seqnum = val;
805 if (priv->next_seqnum == -1)
806 priv->next_seqnum = val;
809 GST_DEBUG_OBJECT (jitterbuffer, "got seqnum-base %d", priv->next_in_seqnum);
811 /* the start and stop times. The seqnum-base corresponds to the start time. We
812 * will keep track of the seqnums on the output and when we reach the one
813 * corresponding to npt-stop, we emit the npt-stop-reached signal */
814 if (gst_structure_get_clock_time (caps_struct, "npt-start", &tval))
815 priv->npt_start = tval;
819 if (gst_structure_get_clock_time (caps_struct, "npt-stop", &tval))
820 priv->npt_stop = tval;
824 GST_DEBUG_OBJECT (jitterbuffer,
825 "npt start/stop: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
826 GST_TIME_ARGS (priv->npt_start), GST_TIME_ARGS (priv->npt_stop));
833 GST_DEBUG_OBJECT (jitterbuffer, "No clock-rate in caps!");
838 GST_DEBUG_OBJECT (jitterbuffer, "Invalid clock-rate %d", priv->clock_rate);
844 gst_rtp_jitter_buffer_flush_start (GstRtpJitterBuffer * jitterbuffer)
846 GstRtpJitterBufferPrivate *priv;
848 priv = jitterbuffer->priv;
851 /* mark ourselves as flushing */
852 priv->srcresult = GST_FLOW_FLUSHING;
853 GST_DEBUG_OBJECT (jitterbuffer, "Disabling pop on queue");
854 /* this unblocks any waiting pops on the src pad task */
856 /* unlock clock, we just unschedule, the entry will be released by the
857 * locking streaming thread. */
858 if (priv->clock_id) {
859 gst_clock_id_unschedule (priv->clock_id);
860 priv->unscheduled = TRUE;
866 gst_rtp_jitter_buffer_flush_stop (GstRtpJitterBuffer * jitterbuffer)
868 GstRtpJitterBufferPrivate *priv;
870 priv = jitterbuffer->priv;
873 GST_DEBUG_OBJECT (jitterbuffer, "Enabling pop on queue");
874 /* Mark as non flushing */
875 priv->srcresult = GST_FLOW_OK;
876 gst_segment_init (&priv->segment, GST_FORMAT_TIME);
877 priv->last_popped_seqnum = -1;
878 priv->last_out_time = -1;
879 priv->next_seqnum = -1;
880 priv->next_in_seqnum = -1;
881 priv->clock_rate = -1;
883 priv->estimated_eos = -1;
884 priv->last_elapsed = 0;
885 priv->reached_npt_stop = FALSE;
886 priv->ext_timestamp = -1;
887 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
888 rtp_jitter_buffer_flush (priv->jbuf);
889 rtp_jitter_buffer_reset_skew (priv->jbuf);
894 gst_rtp_jitter_buffer_src_activate_mode (GstPad * pad, GstObject * parent,
895 GstPadMode mode, gboolean active)
898 GstRtpJitterBuffer *jitterbuffer = NULL;
900 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
903 case GST_PAD_MODE_PUSH:
905 /* allow data processing */
906 gst_rtp_jitter_buffer_flush_stop (jitterbuffer);
908 /* start pushing out buffers */
909 GST_DEBUG_OBJECT (jitterbuffer, "Starting task on srcpad");
910 result = gst_pad_start_task (jitterbuffer->priv->srcpad,
911 (GstTaskFunction) gst_rtp_jitter_buffer_loop, jitterbuffer, NULL);
913 /* make sure all data processing stops ASAP */
914 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
916 /* NOTE this will hardlock if the state change is called from the src pad
917 * task thread because we will _join() the thread. */
918 GST_DEBUG_OBJECT (jitterbuffer, "Stopping task on srcpad");
919 result = gst_pad_stop_task (pad);
929 static GstStateChangeReturn
930 gst_rtp_jitter_buffer_change_state (GstElement * element,
931 GstStateChange transition)
933 GstRtpJitterBuffer *jitterbuffer;
934 GstRtpJitterBufferPrivate *priv;
935 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
937 jitterbuffer = GST_RTP_JITTER_BUFFER (element);
938 priv = jitterbuffer->priv;
940 switch (transition) {
941 case GST_STATE_CHANGE_NULL_TO_READY:
943 case GST_STATE_CHANGE_READY_TO_PAUSED:
945 /* reset negotiated values */
946 priv->clock_rate = -1;
947 priv->clock_base = -1;
948 priv->peer_latency = 0;
950 /* block until we go to PLAYING */
951 priv->blocked = TRUE;
954 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
956 /* unblock to allow streaming in PLAYING */
957 priv->blocked = FALSE;
965 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
967 switch (transition) {
968 case GST_STATE_CHANGE_READY_TO_PAUSED:
969 /* we are a live element because we sync to the clock, which we can only
970 * do in the PLAYING state */
971 if (ret != GST_STATE_CHANGE_FAILURE)
972 ret = GST_STATE_CHANGE_NO_PREROLL;
974 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
976 /* block to stop streaming when PAUSED */
977 priv->blocked = TRUE;
979 if (ret != GST_STATE_CHANGE_FAILURE)
980 ret = GST_STATE_CHANGE_NO_PREROLL;
982 case GST_STATE_CHANGE_PAUSED_TO_READY:
983 gst_buffer_replace (&priv->last_sr, NULL);
985 case GST_STATE_CHANGE_READY_TO_NULL:
995 gst_rtp_jitter_buffer_src_event (GstPad * pad, GstObject * parent,
999 GstRtpJitterBuffer *jitterbuffer;
1000 GstRtpJitterBufferPrivate *priv;
1002 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1003 priv = jitterbuffer->priv;
1005 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1007 switch (GST_EVENT_TYPE (event)) {
1008 case GST_EVENT_LATENCY:
1010 GstClockTime latency;
1012 gst_event_parse_latency (event, &latency);
1014 GST_DEBUG_OBJECT (jitterbuffer,
1015 "configuring latency of %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
1018 /* adjust the overall buffer delay to the total pipeline latency in
1019 * buffering mode because if downstream consumes too fast (because of
1020 * large latency or queues, we would start rebuffering again. */
1021 if (rtp_jitter_buffer_get_mode (priv->jbuf) ==
1022 RTP_JITTER_BUFFER_MODE_BUFFER) {
1023 rtp_jitter_buffer_set_delay (priv->jbuf, latency);
1027 ret = gst_pad_push_event (priv->sinkpad, event);
1031 ret = gst_pad_push_event (priv->sinkpad, event);
1039 gst_rtp_jitter_buffer_sink_event (GstPad * pad, GstObject * parent,
1042 gboolean ret = TRUE;
1043 GstRtpJitterBuffer *jitterbuffer;
1044 GstRtpJitterBufferPrivate *priv;
1046 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1047 priv = jitterbuffer->priv;
1049 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1051 switch (GST_EVENT_TYPE (event)) {
1052 case GST_EVENT_CAPS:
1056 gst_event_parse_caps (event, &caps);
1059 ret = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1062 /* set same caps on srcpad on success */
1064 ret = gst_pad_push_event (priv->srcpad, event);
1066 gst_event_unref (event);
1069 case GST_EVENT_SEGMENT:
1071 gst_event_copy_segment (event, &priv->segment);
1073 /* we need time for now */
1074 if (priv->segment.format != GST_FORMAT_TIME)
1075 goto newseg_wrong_format;
1077 GST_DEBUG_OBJECT (jitterbuffer,
1078 "newsegment: %" GST_SEGMENT_FORMAT, &priv->segment);
1080 /* FIXME, push SEGMENT in the queue. Sorting order might be difficult. */
1081 ret = gst_pad_push_event (priv->srcpad, event);
1084 case GST_EVENT_FLUSH_START:
1085 gst_rtp_jitter_buffer_flush_start (jitterbuffer);
1086 ret = gst_pad_push_event (priv->srcpad, event);
1088 case GST_EVENT_FLUSH_STOP:
1089 ret = gst_pad_push_event (priv->srcpad, event);
1091 gst_rtp_jitter_buffer_src_activate_mode (priv->srcpad, parent,
1092 GST_PAD_MODE_PUSH, TRUE);
1096 /* push EOS in queue. We always push it at the head */
1098 /* check for flushing, we need to discard the event and return FALSE when
1099 * we are flushing */
1100 ret = priv->srcresult == GST_FLOW_OK;
1101 if (ret && !priv->eos) {
1102 GST_INFO_OBJECT (jitterbuffer, "queuing EOS");
1105 } else if (priv->eos) {
1106 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, we are already EOS");
1108 GST_DEBUG_OBJECT (jitterbuffer, "dropping EOS, reason %s",
1109 gst_flow_get_name (priv->srcresult));
1112 gst_event_unref (event);
1116 ret = gst_pad_push_event (priv->srcpad, event);
1125 newseg_wrong_format:
1127 GST_DEBUG_OBJECT (jitterbuffer, "received non TIME newsegment");
1129 gst_event_unref (event);
1135 gst_rtp_jitter_buffer_sink_rtcp_event (GstPad * pad, GstObject * parent,
1138 gboolean ret = TRUE;
1139 GstRtpJitterBuffer *jitterbuffer;
1141 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1143 GST_DEBUG_OBJECT (jitterbuffer, "received %s", GST_EVENT_TYPE_NAME (event));
1145 switch (GST_EVENT_TYPE (event)) {
1146 case GST_EVENT_FLUSH_START:
1147 gst_event_unref (event);
1149 case GST_EVENT_FLUSH_STOP:
1150 gst_event_unref (event);
1153 ret = gst_pad_event_default (pad, parent, event);
1161 * Must be called with JBUF_LOCK held, will release the LOCK when emiting the
1162 * signal. The function returns GST_FLOW_ERROR when a parsing error happened and
1163 * GST_FLOW_FLUSHING when the element is shutting down. On success
1164 * GST_FLOW_OK is returned.
1166 static GstFlowReturn
1167 gst_rtp_jitter_buffer_get_clock_rate (GstRtpJitterBuffer * jitterbuffer,
1171 GValue args[2] = { {0}, {0} };
1175 g_value_init (&args[0], GST_TYPE_ELEMENT);
1176 g_value_set_object (&args[0], jitterbuffer);
1177 g_value_init (&args[1], G_TYPE_UINT);
1178 g_value_set_uint (&args[1], pt);
1180 g_value_init (&ret, GST_TYPE_CAPS);
1181 g_value_set_boxed (&ret, NULL);
1183 JBUF_UNLOCK (jitterbuffer->priv);
1184 g_signal_emitv (args, gst_rtp_jitter_buffer_signals[SIGNAL_REQUEST_PT_MAP], 0,
1186 JBUF_LOCK_CHECK (jitterbuffer->priv, out_flushing);
1188 g_value_unset (&args[0]);
1189 g_value_unset (&args[1]);
1190 caps = (GstCaps *) g_value_dup_boxed (&ret);
1191 g_value_unset (&ret);
1195 res = gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1196 gst_caps_unref (caps);
1198 if (G_UNLIKELY (!res))
1206 GST_DEBUG_OBJECT (jitterbuffer, "could not get caps");
1207 return GST_FLOW_ERROR;
1211 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1212 return GST_FLOW_FLUSHING;
1216 GST_DEBUG_OBJECT (jitterbuffer, "parse failed");
1217 return GST_FLOW_ERROR;
1221 /* call with jbuf lock held */
1223 check_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint * percent)
1225 GstRtpJitterBufferPrivate *priv = jitterbuffer->priv;
1227 /* too short a stream, or too close to EOS will never really fill buffer */
1228 if (*percent != -1 && priv->npt_stop != -1 &&
1229 priv->npt_stop - priv->npt_start <=
1230 rtp_jitter_buffer_get_delay (priv->jbuf)) {
1231 GST_DEBUG_OBJECT (jitterbuffer, "short stream; faking full buffer");
1232 rtp_jitter_buffer_set_buffering (priv->jbuf, FALSE);
1238 post_buffering_percent (GstRtpJitterBuffer * jitterbuffer, gint percent)
1240 GstMessage *message;
1242 /* Post a buffering message */
1243 message = gst_message_new_buffering (GST_OBJECT_CAST (jitterbuffer), percent);
1244 gst_message_set_buffering_stats (message, GST_BUFFERING_LIVE, -1, -1, -1);
1246 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer), message);
1249 static GstFlowReturn
1250 gst_rtp_jitter_buffer_chain (GstPad * pad, GstObject * parent,
1253 GstRtpJitterBuffer *jitterbuffer;
1254 GstRtpJitterBufferPrivate *priv;
1256 GstFlowReturn ret = GST_FLOW_OK;
1257 GstClockTime timestamp;
1262 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
1264 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
1266 priv = jitterbuffer->priv;
1268 if (G_UNLIKELY (!gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)))
1269 goto invalid_buffer;
1271 pt = gst_rtp_buffer_get_payload_type (&rtp);
1272 seqnum = gst_rtp_buffer_get_seq (&rtp);
1273 gst_rtp_buffer_unmap (&rtp);
1275 /* take the timestamp of the buffer. This is the time when the packet was
1276 * received and is used to calculate jitter and clock skew. We will adjust
1277 * this timestamp with the smoothed value after processing it in the
1279 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1280 /* bring to running time */
1281 timestamp = gst_segment_to_running_time (&priv->segment, GST_FORMAT_TIME,
1284 GST_DEBUG_OBJECT (jitterbuffer,
1285 "Received packet #%d at time %" GST_TIME_FORMAT, seqnum,
1286 GST_TIME_ARGS (timestamp));
1288 JBUF_LOCK_CHECK (priv, out_flushing);
1290 if (G_UNLIKELY (priv->last_pt != pt)) {
1293 GST_DEBUG_OBJECT (jitterbuffer, "pt changed from %u to %u", priv->last_pt,
1297 /* reset clock-rate so that we get a new one */
1298 priv->clock_rate = -1;
1300 /* Try to get the clock-rate from the caps first if we can. If there are no
1301 * caps we must fire the signal to get the clock-rate. */
1302 if ((caps = gst_pad_get_current_caps (pad))) {
1303 gst_jitter_buffer_sink_parse_caps (jitterbuffer, caps);
1304 gst_caps_unref (caps);
1308 if (G_UNLIKELY (priv->clock_rate == -1)) {
1309 /* no clock rate given on the caps, try to get one with the signal */
1310 if (gst_rtp_jitter_buffer_get_clock_rate (jitterbuffer,
1311 pt) == GST_FLOW_FLUSHING)
1314 if (G_UNLIKELY (priv->clock_rate == -1))
1318 /* don't accept more data on EOS */
1319 if (G_UNLIKELY (priv->eos))
1322 /* now check against our expected seqnum */
1323 if (G_LIKELY (priv->next_in_seqnum != -1)) {
1325 gboolean reset = FALSE;
1327 gap = gst_rtp_buffer_compare_seqnum (priv->next_in_seqnum, seqnum);
1328 if (G_UNLIKELY (gap != 0)) {
1329 GST_DEBUG_OBJECT (jitterbuffer, "expected #%d, got #%d, gap of %d",
1330 priv->next_in_seqnum, seqnum, gap);
1331 /* priv->next_in_seqnum >= seqnum, this packet is too late or the
1332 * sender might have been restarted with different seqnum. */
1333 if (gap < -RTP_MAX_MISORDER) {
1334 GST_DEBUG_OBJECT (jitterbuffer, "reset: buffer too old %d", gap);
1337 /* priv->next_in_seqnum < seqnum, this is a new packet */
1338 else if (G_UNLIKELY (gap > RTP_MAX_DROPOUT)) {
1339 GST_DEBUG_OBJECT (jitterbuffer, "reset: too many dropped packets %d",
1343 GST_DEBUG_OBJECT (jitterbuffer, "tolerable gap");
1346 if (G_UNLIKELY (reset)) {
1347 GST_DEBUG_OBJECT (jitterbuffer, "flush and reset jitterbuffer");
1348 rtp_jitter_buffer_flush (priv->jbuf);
1349 rtp_jitter_buffer_reset_skew (priv->jbuf);
1350 priv->last_popped_seqnum = -1;
1351 priv->next_seqnum = seqnum;
1354 priv->next_in_seqnum = (seqnum + 1) & 0xffff;
1356 /* let's check if this buffer is too late, we can only accept packets with
1357 * bigger seqnum than the one we last pushed. */
1358 if (G_LIKELY (priv->last_popped_seqnum != -1)) {
1361 gap = gst_rtp_buffer_compare_seqnum (priv->last_popped_seqnum, seqnum);
1363 /* priv->last_popped_seqnum >= seqnum, we're too late. */
1364 if (G_UNLIKELY (gap <= 0))
1368 /* let's drop oldest packet if the queue is already full and drop-on-latency
1369 * is set. We can only do this when there actually is a latency. When no
1370 * latency is set, we just pump it in the queue and let the other end push it
1371 * out as fast as possible. */
1372 if (priv->latency_ms && priv->drop_on_latency) {
1374 gst_util_uint64_scale_int (priv->latency_ms, priv->clock_rate, 1000);
1376 if (G_UNLIKELY (rtp_jitter_buffer_get_ts_diff (priv->jbuf) >= latency_ts)) {
1379 old_buf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
1381 GST_DEBUG_OBJECT (jitterbuffer, "Queue full, dropping old packet %p",
1384 gst_buffer_unref (old_buf);
1388 /* we need to make the metadata writable before pushing it in the jitterbuffer
1389 * because the jitterbuffer will update the timestamp */
1390 buffer = gst_buffer_make_writable (buffer);
1392 /* now insert the packet into the queue in sorted order. This function returns
1393 * FALSE if a packet with the same seqnum was already in the queue, meaning we
1394 * have a duplicate. */
1395 if (G_UNLIKELY (!rtp_jitter_buffer_insert (priv->jbuf, buffer, timestamp,
1396 priv->clock_rate, &tail, &percent)))
1399 /* we had an unhandled SR, handle it now */
1401 do_handle_sync (jitterbuffer);
1403 /* signal addition of new buffer when the _loop is waiting. */
1404 if (priv->waiting && priv->active)
1407 /* let's unschedule and unblock any waiting buffers. We only want to do this
1408 * when the tail buffer changed */
1409 if (G_UNLIKELY (priv->clock_id && tail)) {
1410 GST_DEBUG_OBJECT (jitterbuffer,
1411 "Unscheduling waiting buffer, new tail buffer");
1412 gst_clock_id_unschedule (priv->clock_id);
1413 priv->unscheduled = TRUE;
1416 GST_DEBUG_OBJECT (jitterbuffer, "Pushed packet #%d, now %d packets, tail: %d",
1417 seqnum, rtp_jitter_buffer_num_packets (priv->jbuf), tail);
1419 check_buffering_percent (jitterbuffer, &percent);
1425 post_buffering_percent (jitterbuffer, percent);
1432 /* this is not fatal but should be filtered earlier */
1433 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
1434 ("Received invalid RTP payload, dropping"));
1435 gst_buffer_unref (buffer);
1440 GST_WARNING_OBJECT (jitterbuffer,
1441 "No clock-rate in caps!, dropping buffer");
1442 gst_buffer_unref (buffer);
1447 ret = priv->srcresult;
1448 GST_DEBUG_OBJECT (jitterbuffer, "flushing %s", gst_flow_get_name (ret));
1449 gst_buffer_unref (buffer);
1455 GST_WARNING_OBJECT (jitterbuffer, "we are EOS, refusing buffer");
1456 gst_buffer_unref (buffer);
1461 GST_WARNING_OBJECT (jitterbuffer, "Packet #%d too late as #%d was already"
1462 " popped, dropping", seqnum, priv->last_popped_seqnum);
1464 gst_buffer_unref (buffer);
1469 GST_WARNING_OBJECT (jitterbuffer, "Duplicate packet #%d detected, dropping",
1471 priv->num_duplicates++;
1472 gst_buffer_unref (buffer);
1478 apply_offset (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
1480 GstRtpJitterBufferPrivate *priv;
1482 priv = jitterbuffer->priv;
1484 if (timestamp == -1)
1487 /* apply the timestamp offset, this is used for inter stream sync */
1488 timestamp += priv->ts_offset;
1489 /* add the offset, this is used when buffering */
1490 timestamp += priv->out_offset;
1496 get_sync_time (GstRtpJitterBuffer * jitterbuffer, GstClockTime timestamp)
1498 GstClockTime result;
1499 GstRtpJitterBufferPrivate *priv;
1501 priv = jitterbuffer->priv;
1503 result = timestamp + GST_ELEMENT_CAST (jitterbuffer)->base_time;
1504 /* add latency, this includes our own latency and the peer latency. */
1505 result += priv->latency_ns;
1506 result += priv->peer_latency;
1512 eos_reached (GstClock * clock, GstClockTime time, GstClockID id,
1513 GstRtpJitterBuffer * jitterbuffer)
1515 GstRtpJitterBufferPrivate *priv;
1517 priv = jitterbuffer->priv;
1519 JBUF_LOCK_CHECK (priv, flushing);
1520 if (priv->waiting) {
1521 GST_INFO_OBJECT (jitterbuffer, "got the NPT timeout");
1522 priv->reached_npt_stop = TRUE;
1538 compute_elapsed (GstRtpJitterBuffer * jitterbuffer, GstBuffer * outbuf)
1540 guint64 ext_time, elapsed;
1542 GstRtpJitterBufferPrivate *priv;
1543 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
1545 priv = jitterbuffer->priv;
1546 gst_rtp_buffer_map (outbuf, GST_MAP_READ, &rtp);
1547 rtp_time = gst_rtp_buffer_get_timestamp (&rtp);
1548 gst_rtp_buffer_unmap (&rtp);
1550 GST_LOG_OBJECT (jitterbuffer, "rtp %" G_GUINT32_FORMAT ", ext %"
1551 G_GUINT64_FORMAT, rtp_time, priv->ext_timestamp);
1553 if (rtp_time < priv->ext_timestamp) {
1554 ext_time = priv->ext_timestamp;
1556 ext_time = gst_rtp_buffer_ext_timestamp (&priv->ext_timestamp, rtp_time);
1559 if (ext_time > priv->clock_base)
1560 elapsed = ext_time - priv->clock_base;
1564 elapsed = gst_util_uint64_scale_int (elapsed, GST_SECOND, priv->clock_rate);
1569 * This funcion will push out buffers on the source pad.
1571 * For each pushed buffer, the seqnum is recorded, if the next buffer B has a
1572 * different seqnum (missing packets before B), this function will wait for the
1573 * missing packet to arrive up to the timestamp of buffer B.
1576 gst_rtp_jitter_buffer_loop (GstRtpJitterBuffer * jitterbuffer)
1578 GstRtpJitterBufferPrivate *priv;
1580 GstFlowReturn result;
1582 guint32 next_seqnum;
1583 GstClockTime timestamp, out_time;
1584 gboolean discont = FALSE;
1588 GstClockTime sync_time;
1590 GstRTPBuffer rtp = { NULL, };
1592 priv = jitterbuffer->priv;
1594 JBUF_LOCK_CHECK (priv, flushing);
1596 GST_DEBUG_OBJECT (jitterbuffer, "Peeking item");
1599 /* always wait if we are blocked */
1600 if (G_LIKELY (!priv->blocked)) {
1601 /* we're buffering but not EOS, wait. */
1602 if (!priv->eos && (!priv->active
1603 || rtp_jitter_buffer_is_buffering (priv->jbuf))) {
1604 GstClockTime elapsed, delay, left;
1606 if (priv->estimated_eos == -1)
1609 outbuf = rtp_jitter_buffer_peek (priv->jbuf);
1610 if (outbuf != NULL) {
1611 elapsed = compute_elapsed (jitterbuffer, outbuf);
1612 if (GST_BUFFER_DURATION_IS_VALID (outbuf))
1613 elapsed += GST_BUFFER_DURATION (outbuf);
1615 GST_INFO_OBJECT (jitterbuffer, "no buffer, using last_elapsed");
1616 elapsed = priv->last_elapsed;
1619 delay = rtp_jitter_buffer_get_delay (priv->jbuf);
1621 if (priv->estimated_eos > elapsed)
1622 left = priv->estimated_eos - elapsed;
1626 GST_INFO_OBJECT (jitterbuffer, "buffering, elapsed %" GST_TIME_FORMAT
1627 " estimated_eos %" GST_TIME_FORMAT " left %" GST_TIME_FORMAT
1628 " delay %" GST_TIME_FORMAT,
1629 GST_TIME_ARGS (elapsed), GST_TIME_ARGS (priv->estimated_eos),
1630 GST_TIME_ARGS (left), GST_TIME_ARGS (delay));
1634 /* if we have a packet, we can exit the loop and grab it */
1635 if (rtp_jitter_buffer_num_packets (priv->jbuf) > 0)
1637 /* no packets but we are EOS, do eos logic */
1638 if (G_UNLIKELY (priv->eos))
1640 /* underrun, wait for packets or flushing now if we are expecting an EOS
1641 * timeout, set the async timer for it too */
1642 if (priv->estimated_eos != -1 && !priv->reached_npt_stop) {
1643 sync_time = get_sync_time (jitterbuffer, priv->estimated_eos);
1645 GST_OBJECT_LOCK (jitterbuffer);
1646 clock = GST_ELEMENT_CLOCK (jitterbuffer);
1648 GST_INFO_OBJECT (jitterbuffer, "scheduling timeout");
1649 id = gst_clock_new_single_shot_id (clock, sync_time);
1650 gst_clock_id_wait_async (id, (GstClockCallback) eos_reached,
1651 jitterbuffer, NULL);
1653 GST_OBJECT_UNLOCK (jitterbuffer);
1658 GST_DEBUG_OBJECT (jitterbuffer, "waiting");
1659 priv->waiting = TRUE;
1661 priv->waiting = FALSE;
1662 GST_DEBUG_OBJECT (jitterbuffer, "waiting done");
1665 /* unschedule any pending async notifications we might have */
1666 gst_clock_id_unschedule (id);
1667 gst_clock_id_unref (id);
1669 if (G_UNLIKELY (priv->srcresult != GST_FLOW_OK))
1672 if (id && priv->reached_npt_stop) {
1677 /* peek a buffer, we're just looking at the timestamp and the sequence number.
1678 * If all is fine, we'll pop and push it. If the sequence number is wrong we
1679 * wait on the timestamp. In the chain function we will unlock the wait when a
1680 * new buffer is available. The peeked buffer is valid for as long as we hold
1681 * the jitterbuffer lock. */
1682 outbuf = rtp_jitter_buffer_peek (priv->jbuf);
1684 /* get the seqnum and the next expected seqnum */
1685 gst_rtp_buffer_map (outbuf, GST_MAP_READ, &rtp);
1686 seqnum = gst_rtp_buffer_get_seq (&rtp);
1687 gst_rtp_buffer_unmap (&rtp);
1688 next_seqnum = priv->next_seqnum;
1690 /* get the timestamp, this is already corrected for clock skew by the
1692 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
1694 GST_DEBUG_OBJECT (jitterbuffer,
1695 "Peeked buffer #%d, expect #%d, timestamp %" GST_TIME_FORMAT
1696 ", now %d left", seqnum, next_seqnum, GST_TIME_ARGS (timestamp),
1697 rtp_jitter_buffer_num_packets (priv->jbuf));
1699 /* apply our timestamp offset to the incomming buffer, this will be our output
1701 out_time = apply_offset (jitterbuffer, timestamp);
1703 /* get the gap between this and the previous packet. If we don't know the
1704 * previous packet seqnum assume no gap. */
1705 if (G_LIKELY (next_seqnum != -1)) {
1706 gap = gst_rtp_buffer_compare_seqnum (next_seqnum, seqnum);
1708 /* if we have a packet that we already pushed or considered dropped, pop it
1709 * off and get the next packet */
1710 if (G_UNLIKELY (gap < 0)) {
1711 GST_DEBUG_OBJECT (jitterbuffer, "Old packet #%d, next #%d dropping",
1712 seqnum, next_seqnum);
1713 outbuf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
1714 gst_buffer_unref (outbuf);
1718 GST_DEBUG_OBJECT (jitterbuffer, "no next seqnum known, first packet");
1722 /* If we don't know what the next seqnum should be (== -1) we have to wait
1723 * because it might be possible that we are not receiving this buffer in-order,
1724 * a buffer with a lower seqnum could arrive later and we want to push that
1725 * earlier buffer before this buffer then.
1726 * If we know the expected seqnum, we can compare it to the current seqnum to
1727 * determine if we have missing a packet. If we have a missing packet (which
1728 * must be before this packet) we can wait for it until the deadline for this
1729 * packet expires. */
1730 if (G_UNLIKELY (gap != 0 && out_time != -1)) {
1732 GstClockTime duration = GST_CLOCK_TIME_NONE;
1733 GstClockTimeDiff clock_jitter;
1734 guint32 lost_packets = 1;
1735 gboolean lost_packets_late = FALSE;
1739 GST_DEBUG_OBJECT (jitterbuffer,
1740 "Sequence number GAP detected: expected %d instead of %d (%d missing)",
1741 next_seqnum, seqnum, gap);
1743 if (priv->last_out_time != -1) {
1744 GST_DEBUG_OBJECT (jitterbuffer,
1745 "out_time %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT,
1746 GST_TIME_ARGS (out_time), GST_TIME_ARGS (priv->last_out_time));
1747 /* interpolate between the current time and the last time based on
1748 * number of packets we are missing, this is the estimated duration
1749 * for the missing packet based on equidistant packet spacing. Also make
1750 * sure we never go negative. */
1751 if (out_time >= priv->last_out_time)
1752 duration = (out_time - priv->last_out_time) / (gap + 1);
1756 GST_DEBUG_OBJECT (jitterbuffer, "duration %" GST_TIME_FORMAT,
1757 GST_TIME_ARGS (duration));
1758 /* add this duration to the timestamp of the last packet we pushed */
1759 out_time = (priv->last_out_time + duration);
1762 /* we don't know what the next_seqnum should be, wait for the last
1763 * possible moment to push this buffer, maybe we get an earlier seqnum
1765 GST_DEBUG_OBJECT (jitterbuffer, "First buffer %d, do sync", seqnum);
1768 GST_OBJECT_LOCK (jitterbuffer);
1769 clock = GST_ELEMENT_CLOCK (jitterbuffer);
1771 GST_OBJECT_UNLOCK (jitterbuffer);
1772 /* let's just push if there is no clock */
1773 GST_DEBUG_OBJECT (jitterbuffer, "No clock, push right away");
1777 /* prepare for sync against clock */
1778 sync_time = get_sync_time (jitterbuffer, out_time);
1780 GST_DEBUG_OBJECT (jitterbuffer, "sync to timestamp %" GST_TIME_FORMAT
1781 " with sync time %" GST_TIME_FORMAT,
1782 GST_TIME_ARGS (out_time), GST_TIME_ARGS (sync_time));
1784 /* create an entry for the clock */
1785 id = priv->clock_id = gst_clock_new_single_shot_id (clock, sync_time);
1786 priv->unscheduled = FALSE;
1787 GST_OBJECT_UNLOCK (jitterbuffer);
1789 /* release the lock so that the other end can push stuff or unlock */
1792 ret = gst_clock_id_wait (id, &clock_jitter);
1795 GST_DEBUG_OBJECT (jitterbuffer, "sync done");
1796 /* and free the entry */
1797 gst_clock_id_unref (id);
1798 priv->clock_id = NULL;
1800 if (ret == GST_CLOCK_EARLY && gap > 0
1801 && clock_jitter > (priv->latency_ns + priv->peer_latency)) {
1802 GstClockTimeDiff total_duration;
1803 GstClockTime out_time_diff;
1805 out_time_diff = apply_offset (jitterbuffer, timestamp) - out_time;
1806 total_duration = MIN (out_time_diff, clock_jitter);
1809 lost_packets = total_duration / duration;
1812 total_duration = lost_packets * duration;
1814 GST_DEBUG_OBJECT (jitterbuffer,
1815 "Current sync_time has expired a long time ago (+%" GST_TIME_FORMAT
1816 ") Cover up %d lost packets with duration %" GST_TIME_FORMAT,
1817 GST_TIME_ARGS (clock_jitter),
1818 lost_packets, GST_TIME_ARGS (total_duration));
1820 duration = total_duration;
1821 lost_packets_late = TRUE;
1824 /* at this point, the clock could have been unlocked by a timeout, a new
1825 * tail element was added to the queue or because we are shutting down. Check
1826 * for shutdown first. */
1828 ((priv->srcresult != GST_FLOW_OK))
1831 /* if we got unscheduled and we are not flushing, it's because a new tail
1832 * element became available in the queue or we flushed the queue.
1833 * Grab it and try to push or sync. */
1834 if (ret == GST_CLOCK_UNSCHEDULED || priv->unscheduled) {
1835 GST_DEBUG_OBJECT (jitterbuffer,
1836 "Wait got unscheduled, will retry to push with new buffer");
1841 /* we now timed out, this means we lost a packet or finished synchronizing
1842 * on the first buffer. */
1846 /* we had a gap and thus we lost some packets. Create an event for this. */
1847 if (lost_packets > 1)
1848 GST_DEBUG_OBJECT (jitterbuffer, "Packets #%d -> #%d lost", next_seqnum,
1849 next_seqnum + lost_packets - 1);
1851 GST_DEBUG_OBJECT (jitterbuffer, "Packet #%d lost", next_seqnum);
1853 priv->num_late += lost_packets;
1856 /* update our expected next packet */
1857 priv->last_popped_seqnum = next_seqnum;
1858 priv->last_out_time += duration;
1859 priv->next_seqnum = (next_seqnum + lost_packets) & 0xffff;
1861 if (priv->do_lost) {
1862 /* create paket lost event */
1863 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1864 gst_structure_new ("GstRTPPacketLost",
1865 "seqnum", G_TYPE_UINT, (guint) next_seqnum,
1866 "timestamp", G_TYPE_UINT64, out_time,
1867 "duration", G_TYPE_UINT64, duration,
1868 "late", G_TYPE_BOOLEAN, lost_packets_late, NULL));
1870 gst_pad_push_event (priv->srcpad, event);
1871 JBUF_LOCK_CHECK (priv, flushing);
1873 /* look for next packet */
1877 /* there was no known gap,just the first packet, exit the loop and push */
1878 GST_DEBUG_OBJECT (jitterbuffer, "First packet #%d synced", seqnum);
1880 /* get new timestamp, latency might have changed */
1881 out_time = apply_offset (jitterbuffer, timestamp);
1885 /* when we get here we are ready to pop and push the buffer */
1886 outbuf = rtp_jitter_buffer_pop (priv->jbuf, &percent);
1888 check_buffering_percent (jitterbuffer, &percent);
1890 if (G_UNLIKELY (discont || priv->discont)) {
1891 /* set DISCONT flag when we missed a packet. We pushed the buffer writable
1892 * into the jitterbuffer so we can modify now. */
1893 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1894 priv->discont = FALSE;
1896 if (G_UNLIKELY (priv->ts_discont)) {
1897 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
1898 priv->ts_discont = FALSE;
1901 /* apply timestamp with offset to buffer now */
1902 GST_BUFFER_PTS (outbuf) = out_time;
1903 GST_BUFFER_DTS (outbuf) = out_time;
1905 /* update the elapsed time when we need to check against the npt stop time. */
1906 if (priv->npt_stop != -1 && priv->ext_timestamp != -1
1907 && priv->clock_base != -1 && priv->clock_rate > 0) {
1908 guint64 elapsed, estimated;
1910 elapsed = compute_elapsed (jitterbuffer, outbuf);
1912 if (elapsed > priv->last_elapsed || !priv->last_elapsed) {
1915 priv->last_elapsed = elapsed;
1917 left = priv->npt_stop - priv->npt_start;
1918 GST_LOG_OBJECT (jitterbuffer, "left %" GST_TIME_FORMAT,
1919 GST_TIME_ARGS (left));
1922 estimated = gst_util_uint64_scale (out_time, left, elapsed);
1924 /* if there is almost nothing left,
1925 * we may never advance enough to end up in the above case */
1926 if (left < GST_SECOND)
1927 estimated = GST_SECOND;
1932 GST_LOG_OBJECT (jitterbuffer, "elapsed %" GST_TIME_FORMAT ", estimated %"
1933 GST_TIME_FORMAT, GST_TIME_ARGS (elapsed), GST_TIME_ARGS (estimated));
1935 priv->estimated_eos = estimated;
1939 /* now we are ready to push the buffer. Save the seqnum and release the lock
1940 * so the other end can push stuff in the queue again. */
1941 priv->last_popped_seqnum = seqnum;
1942 priv->last_out_time = out_time;
1943 priv->next_seqnum = (seqnum + 1) & 0xffff;
1947 post_buffering_percent (jitterbuffer, percent);
1950 GST_DEBUG_OBJECT (jitterbuffer,
1951 "Pushing buffer %d, timestamp %" GST_TIME_FORMAT, seqnum,
1952 GST_TIME_ARGS (out_time));
1953 result = gst_pad_push (priv->srcpad, outbuf);
1954 if (G_UNLIKELY (result != GST_FLOW_OK))
1962 /* store result, we are flushing now */
1963 GST_DEBUG_OBJECT (jitterbuffer, "We are EOS, pushing EOS downstream");
1964 priv->srcresult = GST_FLOW_EOS;
1965 gst_pad_pause_task (priv->srcpad);
1967 gst_pad_push_event (priv->srcpad, gst_event_new_eos ());
1972 /* store result, we are flushing now */
1973 GST_DEBUG_OBJECT (jitterbuffer, "We reached the NPT stop");
1976 g_signal_emit (jitterbuffer,
1977 gst_rtp_jitter_buffer_signals[SIGNAL_ON_NPT_STOP], 0, NULL);
1982 GST_DEBUG_OBJECT (jitterbuffer, "we are flushing");
1983 gst_pad_pause_task (priv->srcpad);
1989 GST_DEBUG_OBJECT (jitterbuffer, "pausing task, reason %s",
1990 gst_flow_get_name (result));
1994 priv->srcresult = result;
1995 /* we don't post errors or anything because upstream will do that for us
1996 * when we pass the return value upstream. */
1997 gst_pad_pause_task (priv->srcpad);
2003 /* collect the info form the lastest RTCP packet and the jittebuffer sync, do
2004 * some sanity checks and then emit the handle-sync signal with the parameters.
2005 * This function must be called with the LOCK */
2007 do_handle_sync (GstRtpJitterBuffer * jitterbuffer)
2009 GstRtpJitterBufferPrivate *priv;
2010 guint64 base_rtptime, base_time;
2012 guint64 last_rtptime;
2014 guint64 ext_rtptime, diff;
2015 gboolean drop = FALSE;
2017 priv = jitterbuffer->priv;
2019 /* get the last values from the jitterbuffer */
2020 rtp_jitter_buffer_get_sync (priv->jbuf, &base_rtptime, &base_time,
2021 &clock_rate, &last_rtptime);
2023 clock_base = priv->clock_base;
2024 ext_rtptime = priv->ext_rtptime;
2026 GST_DEBUG_OBJECT (jitterbuffer, "ext SR %" G_GUINT64_FORMAT ", base %"
2027 G_GUINT64_FORMAT ", clock-rate %" G_GUINT32_FORMAT
2028 ", clock-base %" G_GUINT64_FORMAT ", last-rtptime %" G_GUINT64_FORMAT,
2029 ext_rtptime, base_rtptime, clock_rate, clock_base, last_rtptime);
2031 if (base_rtptime == -1 || clock_rate == -1 || base_time == -1) {
2032 GST_DEBUG_OBJECT (jitterbuffer, "dropping, no RTP values");
2035 /* we can't accept anything that happened before we did the last resync */
2036 if (base_rtptime > ext_rtptime) {
2037 GST_DEBUG_OBJECT (jitterbuffer, "dropping, older than base time");
2040 /* the SR RTP timestamp must be something close to what we last observed
2041 * in the jitterbuffer */
2042 if (ext_rtptime > last_rtptime) {
2043 /* check how far ahead it is to our RTP timestamps */
2044 diff = ext_rtptime - last_rtptime;
2045 /* if bigger than 1 second, we drop it */
2046 if (diff > clock_rate) {
2047 GST_DEBUG_OBJECT (jitterbuffer, "too far ahead");
2048 /* should drop this, but some RTSP servers end up with bogus
2049 * way too ahead RTCP packet when repeated PAUSE/PLAY,
2050 * so still trigger rptbin sync but invalidate RTCP data
2051 * (sync might use other methods) */
2054 GST_DEBUG_OBJECT (jitterbuffer, "ext last %" G_GUINT64_FORMAT ", diff %"
2055 G_GUINT64_FORMAT, last_rtptime, diff);
2063 s = gst_structure_new ("application/x-rtp-sync",
2064 "base-rtptime", G_TYPE_UINT64, base_rtptime,
2065 "base-time", G_TYPE_UINT64, base_time,
2066 "clock-rate", G_TYPE_UINT, clock_rate,
2067 "clock-base", G_TYPE_UINT64, clock_base,
2068 "sr-ext-rtptime", G_TYPE_UINT64, ext_rtptime,
2069 "sr-buffer", GST_TYPE_BUFFER, priv->last_sr, NULL);
2071 GST_DEBUG_OBJECT (jitterbuffer, "signaling sync");
2072 gst_buffer_replace (&priv->last_sr, NULL);
2074 g_signal_emit (jitterbuffer,
2075 gst_rtp_jitter_buffer_signals[SIGNAL_HANDLE_SYNC], 0, s);
2077 gst_structure_free (s);
2079 GST_DEBUG_OBJECT (jitterbuffer, "dropping RTCP packet");
2083 static GstFlowReturn
2084 gst_rtp_jitter_buffer_chain_rtcp (GstPad * pad, GstObject * parent,
2087 GstRtpJitterBuffer *jitterbuffer;
2088 GstRtpJitterBufferPrivate *priv;
2089 GstFlowReturn ret = GST_FLOW_OK;
2091 GstRTCPPacket packet;
2092 guint64 ext_rtptime;
2094 GstRTCPBuffer rtcp = { NULL, };
2096 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
2098 if (G_UNLIKELY (!gst_rtcp_buffer_validate (buffer)))
2099 goto invalid_buffer;
2101 priv = jitterbuffer->priv;
2103 gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcp);
2105 if (!gst_rtcp_buffer_get_first_packet (&rtcp, &packet))
2108 /* first packet must be SR or RR or else the validate would have failed */
2109 switch (gst_rtcp_packet_get_type (&packet)) {
2110 case GST_RTCP_TYPE_SR:
2111 gst_rtcp_packet_sr_get_sender_info (&packet, &ssrc, NULL, &rtptime,
2117 gst_rtcp_buffer_unmap (&rtcp);
2119 GST_DEBUG_OBJECT (jitterbuffer, "received RTCP of SSRC %08x", ssrc);
2122 /* convert the RTP timestamp to our extended timestamp, using the same offset
2123 * we used in the jitterbuffer */
2124 ext_rtptime = priv->jbuf->ext_rtptime;
2125 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
2127 priv->ext_rtptime = ext_rtptime;
2128 gst_buffer_replace (&priv->last_sr, buffer);
2130 do_handle_sync (jitterbuffer);
2134 gst_buffer_unref (buffer);
2140 /* this is not fatal but should be filtered earlier */
2141 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2142 ("Received invalid RTCP payload, dropping"));
2148 /* this is not fatal but should be filtered earlier */
2149 GST_ELEMENT_WARNING (jitterbuffer, STREAM, DECODE, (NULL),
2150 ("Received empty RTCP payload, dropping"));
2151 gst_rtcp_buffer_unmap (&rtcp);
2157 GST_DEBUG_OBJECT (jitterbuffer, "ignoring RTCP packet");
2158 gst_rtcp_buffer_unmap (&rtcp);
2165 gst_rtp_jitter_buffer_sink_query (GstPad * pad, GstObject * parent,
2168 gboolean res = FALSE;
2170 switch (GST_QUERY_TYPE (query)) {
2171 case GST_QUERY_CAPS:
2173 GstCaps *filter, *caps;
2175 gst_query_parse_caps (query, &filter);
2176 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
2177 gst_query_set_caps_result (query, caps);
2178 gst_caps_unref (caps);
2183 if (GST_QUERY_IS_SERIALIZED (query)) {
2184 GST_WARNING_OBJECT (pad, "unhandled serialized query");
2187 res = gst_pad_query_default (pad, parent, query);
2195 gst_rtp_jitter_buffer_src_query (GstPad * pad, GstObject * parent,
2198 GstRtpJitterBuffer *jitterbuffer;
2199 GstRtpJitterBufferPrivate *priv;
2200 gboolean res = FALSE;
2202 jitterbuffer = GST_RTP_JITTER_BUFFER (parent);
2203 priv = jitterbuffer->priv;
2205 switch (GST_QUERY_TYPE (query)) {
2206 case GST_QUERY_LATENCY:
2208 /* We need to send the query upstream and add the returned latency to our
2210 GstClockTime min_latency, max_latency;
2212 GstClockTime our_latency;
2214 if ((res = gst_pad_peer_query (priv->sinkpad, query))) {
2215 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
2217 GST_DEBUG_OBJECT (jitterbuffer, "Peer latency: min %"
2218 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
2219 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2221 /* store this so that we can safely sync on the peer buffers. */
2223 priv->peer_latency = min_latency;
2224 our_latency = priv->latency_ns;
2227 GST_DEBUG_OBJECT (jitterbuffer, "Our latency: %" GST_TIME_FORMAT,
2228 GST_TIME_ARGS (our_latency));
2230 /* we add some latency but can buffer an infinite amount of time */
2231 min_latency += our_latency;
2234 GST_DEBUG_OBJECT (jitterbuffer, "Calculated total latency : min %"
2235 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
2236 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
2238 gst_query_set_latency (query, TRUE, min_latency, max_latency);
2242 case GST_QUERY_POSITION:
2244 GstClockTime start, last_out;
2247 gst_query_parse_position (query, &fmt, NULL);
2248 if (fmt != GST_FORMAT_TIME) {
2249 res = gst_pad_query_default (pad, parent, query);
2254 start = priv->npt_start;
2255 last_out = priv->last_out_time;
2258 GST_DEBUG_OBJECT (jitterbuffer, "npt start %" GST_TIME_FORMAT
2259 ", last out %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
2260 GST_TIME_ARGS (last_out));
2262 if (GST_CLOCK_TIME_IS_VALID (start) && GST_CLOCK_TIME_IS_VALID (last_out)) {
2263 /* bring 0-based outgoing time to stream time */
2264 gst_query_set_position (query, GST_FORMAT_TIME, start + last_out);
2267 res = gst_pad_query_default (pad, parent, query);
2271 case GST_QUERY_CAPS:
2273 GstCaps *filter, *caps;
2275 gst_query_parse_caps (query, &filter);
2276 caps = gst_rtp_jitter_buffer_getcaps (pad, filter);
2277 gst_query_set_caps_result (query, caps);
2278 gst_caps_unref (caps);
2283 res = gst_pad_query_default (pad, parent, query);
2291 gst_rtp_jitter_buffer_set_property (GObject * object,
2292 guint prop_id, const GValue * value, GParamSpec * pspec)
2294 GstRtpJitterBuffer *jitterbuffer;
2295 GstRtpJitterBufferPrivate *priv;
2297 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
2298 priv = jitterbuffer->priv;
2303 guint new_latency, old_latency;
2305 new_latency = g_value_get_uint (value);
2308 old_latency = priv->latency_ms;
2309 priv->latency_ms = new_latency;
2310 priv->latency_ns = priv->latency_ms * GST_MSECOND;
2311 rtp_jitter_buffer_set_delay (priv->jbuf, priv->latency_ns);
2314 /* post message if latency changed, this will inform the parent pipeline
2315 * that a latency reconfiguration is possible/needed. */
2316 if (new_latency != old_latency) {
2317 GST_DEBUG_OBJECT (jitterbuffer, "latency changed to: %" GST_TIME_FORMAT,
2318 GST_TIME_ARGS (new_latency * GST_MSECOND));
2320 gst_element_post_message (GST_ELEMENT_CAST (jitterbuffer),
2321 gst_message_new_latency (GST_OBJECT_CAST (jitterbuffer)));
2325 case PROP_DROP_ON_LATENCY:
2327 priv->drop_on_latency = g_value_get_boolean (value);
2330 case PROP_TS_OFFSET:
2332 priv->ts_offset = g_value_get_int64 (value);
2333 priv->ts_discont = TRUE;
2338 priv->do_lost = g_value_get_boolean (value);
2343 rtp_jitter_buffer_set_mode (priv->jbuf, g_value_get_enum (value));
2347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2353 gst_rtp_jitter_buffer_get_property (GObject * object,
2354 guint prop_id, GValue * value, GParamSpec * pspec)
2356 GstRtpJitterBuffer *jitterbuffer;
2357 GstRtpJitterBufferPrivate *priv;
2359 jitterbuffer = GST_RTP_JITTER_BUFFER (object);
2360 priv = jitterbuffer->priv;
2365 g_value_set_uint (value, priv->latency_ms);
2368 case PROP_DROP_ON_LATENCY:
2370 g_value_set_boolean (value, priv->drop_on_latency);
2373 case PROP_TS_OFFSET:
2375 g_value_set_int64 (value, priv->ts_offset);
2380 g_value_set_boolean (value, priv->do_lost);
2385 g_value_set_enum (value, rtp_jitter_buffer_get_mode (priv->jbuf));
2393 if (priv->srcresult != GST_FLOW_OK)
2396 percent = rtp_jitter_buffer_get_percent (priv->jbuf);
2398 g_value_set_int (value, percent);
2403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);