ddbf733be5b23015f575307ab0b4c13959aa790c
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpsource.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <string.h>
20
21 #include <gst/rtp/gstrtpbuffer.h>
22 #include <gst/rtp/gstrtcpbuffer.h>
23
24 #include "rtpsource.h"
25
26 GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
27 #define GST_CAT_DEFAULT rtp_source_debug
28
29 #define RTP_MAX_PROBATION_LEN   32
30
31 /* signals and args */
32 enum
33 {
34   LAST_SIGNAL
35 };
36
37 #define DEFAULT_SSRC                 0
38 #define DEFAULT_IS_CSRC              FALSE
39 #define DEFAULT_IS_VALIDATED         FALSE
40 #define DEFAULT_IS_SENDER            FALSE
41 #define DEFAULT_SDES_CNAME           NULL
42 #define DEFAULT_SDES_NAME            NULL
43 #define DEFAULT_SDES_EMAIL           NULL
44 #define DEFAULT_SDES_PHONE           NULL
45 #define DEFAULT_SDES_LOCATION        NULL
46 #define DEFAULT_SDES_TOOL            NULL
47 #define DEFAULT_SDES_NOTE            NULL
48
49 enum
50 {
51   PROP_0,
52   PROP_SSRC,
53   PROP_IS_CSRC,
54   PROP_IS_VALIDATED,
55   PROP_IS_SENDER,
56   PROP_SDES_CNAME,
57   PROP_SDES_NAME,
58   PROP_SDES_EMAIL,
59   PROP_SDES_PHONE,
60   PROP_SDES_LOCATION,
61   PROP_SDES_TOOL,
62   PROP_SDES_NOTE,
63   PROP_LAST
64 };
65
66 /* GObject vmethods */
67 static void rtp_source_finalize (GObject * object);
68 static void rtp_source_set_property (GObject * object, guint prop_id,
69     const GValue * value, GParamSpec * pspec);
70 static void rtp_source_get_property (GObject * object, guint prop_id,
71     GValue * value, GParamSpec * pspec);
72
73 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
74
75 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
76
77 static void
78 rtp_source_class_init (RTPSourceClass * klass)
79 {
80   GObjectClass *gobject_class;
81
82   gobject_class = (GObjectClass *) klass;
83
84   gobject_class->finalize = rtp_source_finalize;
85
86   gobject_class->set_property = rtp_source_set_property;
87   gobject_class->get_property = rtp_source_get_property;
88
89   g_object_class_install_property (gobject_class, PROP_SSRC,
90       g_param_spec_uint ("ssrc", "SSRC",
91           "The SSRC of this source", 0, G_MAXUINT,
92           DEFAULT_SSRC, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
93
94   g_object_class_install_property (gobject_class, PROP_IS_CSRC,
95       g_param_spec_boolean ("is-csrc", "Is CSRC",
96           "If this SSRC is acting as a contributing source",
97           DEFAULT_IS_CSRC, G_PARAM_READABLE));
98
99   g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
100       g_param_spec_boolean ("is-validated", "Is Validated",
101           "If this SSRC is validated", DEFAULT_IS_VALIDATED, G_PARAM_READABLE));
102
103   g_object_class_install_property (gobject_class, PROP_IS_SENDER,
104       g_param_spec_boolean ("is-sender", "Is Sender",
105           "If this SSRC is a sender", DEFAULT_IS_SENDER, G_PARAM_READABLE));
106
107   g_object_class_install_property (gobject_class, PROP_SDES_CNAME,
108       g_param_spec_string ("sdes-cname", "SDES CNAME",
109           "The CNAME to put in SDES messages of this source",
110           DEFAULT_SDES_CNAME, G_PARAM_READWRITE));
111
112   g_object_class_install_property (gobject_class, PROP_SDES_NAME,
113       g_param_spec_string ("sdes-name", "SDES NAME",
114           "The NAME to put in SDES messages of this source",
115           DEFAULT_SDES_NAME, G_PARAM_READWRITE));
116
117   g_object_class_install_property (gobject_class, PROP_SDES_EMAIL,
118       g_param_spec_string ("sdes-email", "SDES EMAIL",
119           "The EMAIL to put in SDES messages of this source",
120           DEFAULT_SDES_EMAIL, G_PARAM_READWRITE));
121
122   g_object_class_install_property (gobject_class, PROP_SDES_PHONE,
123       g_param_spec_string ("sdes-phone", "SDES PHONE",
124           "The PHONE to put in SDES messages of this source",
125           DEFAULT_SDES_PHONE, G_PARAM_READWRITE));
126
127   g_object_class_install_property (gobject_class, PROP_SDES_LOCATION,
128       g_param_spec_string ("sdes-location", "SDES LOCATION",
129           "The LOCATION to put in SDES messages of this source",
130           DEFAULT_SDES_LOCATION, G_PARAM_READWRITE));
131
132   g_object_class_install_property (gobject_class, PROP_SDES_TOOL,
133       g_param_spec_string ("sdes-tool", "SDES TOOL",
134           "The TOOL to put in SDES messages of this source",
135           DEFAULT_SDES_TOOL, G_PARAM_READWRITE));
136
137   g_object_class_install_property (gobject_class, PROP_SDES_NOTE,
138       g_param_spec_string ("sdes-note", "SDES NOTE",
139           "The NOTE to put in SDES messages of this source",
140           DEFAULT_SDES_NOTE, G_PARAM_READWRITE));
141
142   GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
143 }
144
145 /**
146  * rtp_source_reset:
147  * @src: an #RTPSource
148  *
149  * Reset the stats of @src.
150  */
151 void
152 rtp_source_reset (RTPSource * src)
153 {
154   src->received_bye = FALSE;
155
156   src->stats.cycles = -1;
157   src->stats.jitter = 0;
158   src->stats.transit = -1;
159   src->stats.curr_sr = 0;
160   src->stats.curr_rr = 0;
161 }
162
163 static void
164 rtp_source_init (RTPSource * src)
165 {
166   /* sources are initialy on probation until we receive enough valid RTP
167    * packets or a valid RTCP packet */
168   src->validated = FALSE;
169   src->probation = RTP_DEFAULT_PROBATION;
170
171   src->payload = 0;
172   src->clock_rate = -1;
173   src->clock_base = -1;
174   src->clock_base_time = -1;
175   src->packets = g_queue_new ();
176   src->seqnum_base = -1;
177   src->last_rtptime = -1;
178
179   rtp_source_reset (src);
180 }
181
182 static void
183 rtp_source_finalize (GObject * object)
184 {
185   RTPSource *src;
186   GstBuffer *buffer;
187   gint i;
188
189   src = RTP_SOURCE_CAST (object);
190
191   while ((buffer = g_queue_pop_head (src->packets)))
192     gst_buffer_unref (buffer);
193   g_queue_free (src->packets);
194
195   for (i = 0; i < 9; i++)
196     g_free (src->sdes[i]);
197
198   g_free (src->bye_reason);
199
200   gst_caps_replace (&src->caps, NULL);
201
202   G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
203 }
204
205 static void
206 rtp_source_set_property (GObject * object, guint prop_id,
207     const GValue * value, GParamSpec * pspec)
208 {
209   RTPSource *src;
210
211   src = RTP_SOURCE (object);
212
213   switch (prop_id) {
214     case PROP_SSRC:
215       src->ssrc = g_value_get_uint (value);
216       break;
217     case PROP_SDES_CNAME:
218       rtp_source_set_sdes_string (src, GST_RTCP_SDES_CNAME,
219           g_value_get_string (value));
220       break;
221     case PROP_SDES_NAME:
222       rtp_source_set_sdes_string (src, GST_RTCP_SDES_NAME,
223           g_value_get_string (value));
224       break;
225     case PROP_SDES_EMAIL:
226       rtp_source_set_sdes_string (src, GST_RTCP_SDES_EMAIL,
227           g_value_get_string (value));
228       break;
229     case PROP_SDES_PHONE:
230       rtp_source_set_sdes_string (src, GST_RTCP_SDES_PHONE,
231           g_value_get_string (value));
232       break;
233     case PROP_SDES_LOCATION:
234       rtp_source_set_sdes_string (src, GST_RTCP_SDES_LOC,
235           g_value_get_string (value));
236       break;
237     case PROP_SDES_TOOL:
238       rtp_source_set_sdes_string (src, GST_RTCP_SDES_TOOL,
239           g_value_get_string (value));
240       break;
241     case PROP_SDES_NOTE:
242       rtp_source_set_sdes_string (src, GST_RTCP_SDES_NOTE,
243           g_value_get_string (value));
244       break;
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247       break;
248   }
249 }
250
251 static void
252 rtp_source_get_property (GObject * object, guint prop_id,
253     GValue * value, GParamSpec * pspec)
254 {
255   RTPSource *src;
256
257   src = RTP_SOURCE (object);
258
259   switch (prop_id) {
260     case PROP_SSRC:
261       g_value_set_uint (value, rtp_source_get_ssrc (src));
262       break;
263     case PROP_IS_CSRC:
264       g_value_set_boolean (value, rtp_source_is_as_csrc (src));
265       break;
266     case PROP_IS_VALIDATED:
267       g_value_set_boolean (value, rtp_source_is_validated (src));
268       break;
269     case PROP_IS_SENDER:
270       g_value_set_boolean (value, rtp_source_is_sender (src));
271       break;
272     case PROP_SDES_CNAME:
273       g_value_take_string (value, rtp_source_get_sdes_string (src,
274               GST_RTCP_SDES_CNAME));
275       break;
276     case PROP_SDES_NAME:
277       g_value_take_string (value, rtp_source_get_sdes_string (src,
278               GST_RTCP_SDES_NAME));
279       break;
280     case PROP_SDES_EMAIL:
281       g_value_take_string (value, rtp_source_get_sdes_string (src,
282               GST_RTCP_SDES_EMAIL));
283       break;
284     case PROP_SDES_PHONE:
285       g_value_take_string (value, rtp_source_get_sdes_string (src,
286               GST_RTCP_SDES_PHONE));
287       break;
288     case PROP_SDES_LOCATION:
289       g_value_take_string (value, rtp_source_get_sdes_string (src,
290               GST_RTCP_SDES_LOC));
291       break;
292     case PROP_SDES_TOOL:
293       g_value_take_string (value, rtp_source_get_sdes_string (src,
294               GST_RTCP_SDES_TOOL));
295       break;
296     case PROP_SDES_NOTE:
297       g_value_take_string (value, rtp_source_get_sdes_string (src,
298               GST_RTCP_SDES_NOTE));
299       break;
300     default:
301       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302       break;
303   }
304 }
305
306 /**
307  * rtp_source_new:
308  * @ssrc: an SSRC
309  *
310  * Create a #RTPSource with @ssrc.
311  *
312  * Returns: a new #RTPSource. Use g_object_unref() after usage.
313  */
314 RTPSource *
315 rtp_source_new (guint32 ssrc)
316 {
317   RTPSource *src;
318
319   src = g_object_new (RTP_TYPE_SOURCE, NULL);
320   src->ssrc = ssrc;
321
322   return src;
323 }
324
325 /**
326  * rtp_source_set_callbacks:
327  * @src: an #RTPSource
328  * @cb: callback functions
329  * @user_data: user data
330  *
331  * Set the callbacks for the source.
332  */
333 void
334 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
335     gpointer user_data)
336 {
337   g_return_if_fail (RTP_IS_SOURCE (src));
338
339   src->callbacks.push_rtp = cb->push_rtp;
340   src->callbacks.clock_rate = cb->clock_rate;
341   src->user_data = user_data;
342 }
343
344 /**
345  * rtp_source_get_ssrc:
346  * @src: an #RTPSource
347  *
348  * Get the SSRC of @source.
349  *
350  * Returns: the SSRC of src.
351  */
352 guint32
353 rtp_source_get_ssrc (RTPSource * src)
354 {
355   guint32 result;
356
357   g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
358
359   result = src->ssrc;
360
361   return result;
362 }
363
364 /**
365  * rtp_source_set_as_csrc:
366  * @src: an #RTPSource
367  *
368  * Configure @src as a CSRC, this will also validate @src.
369  */
370 void
371 rtp_source_set_as_csrc (RTPSource * src)
372 {
373   g_return_if_fail (RTP_IS_SOURCE (src));
374
375   src->validated = TRUE;
376   src->is_csrc = TRUE;
377 }
378
379 /**
380  * rtp_source_is_as_csrc:
381  * @src: an #RTPSource
382  *
383  * Check if @src is a contributing source.
384  *
385  * Returns: %TRUE if @src is acting as a contributing source.
386  */
387 gboolean
388 rtp_source_is_as_csrc (RTPSource * src)
389 {
390   gboolean result;
391
392   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
393
394   result = src->is_csrc;
395
396   return result;
397 }
398
399 /**
400  * rtp_source_is_active:
401  * @src: an #RTPSource
402  *
403  * Check if @src is an active source. A source is active if it has been
404  * validated and has not yet received a BYE packet
405  *
406  * Returns: %TRUE if @src is an qactive source.
407  */
408 gboolean
409 rtp_source_is_active (RTPSource * src)
410 {
411   gboolean result;
412
413   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
414
415   result = RTP_SOURCE_IS_ACTIVE (src);
416
417   return result;
418 }
419
420 /**
421  * rtp_source_is_validated:
422  * @src: an #RTPSource
423  *
424  * Check if @src is a validated source.
425  *
426  * Returns: %TRUE if @src is a validated source.
427  */
428 gboolean
429 rtp_source_is_validated (RTPSource * src)
430 {
431   gboolean result;
432
433   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
434
435   result = src->validated;
436
437   return result;
438 }
439
440 /**
441  * rtp_source_is_sender:
442  * @src: an #RTPSource
443  *
444  * Check if @src is a sending source.
445  *
446  * Returns: %TRUE if @src is a sending source.
447  */
448 gboolean
449 rtp_source_is_sender (RTPSource * src)
450 {
451   gboolean result;
452
453   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
454
455   result = RTP_SOURCE_IS_SENDER (src);
456
457   return result;
458 }
459
460 /**
461  * rtp_source_received_bye:
462  * @src: an #RTPSource
463  *
464  * Check if @src has receoved a BYE packet.
465  *
466  * Returns: %TRUE if @src has received a BYE packet.
467  */
468 gboolean
469 rtp_source_received_bye (RTPSource * src)
470 {
471   gboolean result;
472
473   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
474
475   result = src->received_bye;
476
477   return result;
478 }
479
480
481 /**
482  * rtp_source_get_bye_reason:
483  * @src: an #RTPSource
484  *
485  * Get the BYE reason for @src. Check if the source receoved a BYE message first
486  * with rtp_source_received_bye().
487  *
488  * Returns: The BYE reason or NULL when no reason was given or the source did
489  * not receive a BYE message yet. g_fee() after usage.
490  */
491 gchar *
492 rtp_source_get_bye_reason (RTPSource * src)
493 {
494   gchar *result;
495
496   g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
497
498   result = g_strdup (src->bye_reason);
499
500   return result;
501 }
502
503 /**
504  * rtp_source_update_caps:
505  * @src: an #RTPSource
506  * @caps: a #GstCaps
507  *
508  * Parse @caps and store all relevant information in @source.
509  */
510 void
511 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
512 {
513   GstStructure *s;
514   guint val;
515   gint ival;
516
517   /* nothing changed, return */
518   if (src->caps == caps)
519     return;
520
521   s = gst_caps_get_structure (caps, 0);
522
523   if (gst_structure_get_int (s, "payload", &ival))
524     src->payload = ival;
525   GST_DEBUG ("got payload %d", src->payload);
526
527   gst_structure_get_int (s, "clock-rate", &src->clock_rate);
528   GST_DEBUG ("got clock-rate %d", src->clock_rate);
529
530   if (gst_structure_get_uint (s, "clock-base", &val))
531     src->clock_base = val;
532   GST_DEBUG ("got clock-base %" G_GINT64_FORMAT, src->clock_base);
533
534   if (gst_structure_get_uint (s, "seqnum-base", &val))
535     src->seqnum_base = val;
536   GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
537
538   gst_caps_replace (&src->caps, caps);
539 }
540
541 /**
542  * rtp_source_set_sdes:
543  * @src: an #RTPSource
544  * @type: the type of the SDES item
545  * @data: the SDES data
546  * @len: the SDES length
547  *
548  * Store an SDES item of @type in @src. 
549  *
550  * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
551  */
552 gboolean
553 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
554     const guint8 * data, guint len)
555 {
556   guint8 *old;
557
558   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
559
560   if (type < 0 || type > GST_RTCP_SDES_PRIV)
561     return FALSE;
562
563   old = src->sdes[type];
564
565   /* lengths are the same, check if the data is the same */
566   if ((src->sdes_len[type] == len))
567     if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
568       return FALSE;
569
570   /* NULL data, make sure we store 0 length or if no length is given,
571    * take strlen */
572   if (data == NULL)
573     len = 0;
574
575   g_free (src->sdes[type]);
576   src->sdes[type] = g_memdup (data, len);
577   src->sdes_len[type] = len;
578
579   return TRUE;
580 }
581
582 /**
583  * rtp_source_set_sdes_string:
584  * @src: an #RTPSource
585  * @type: the type of the SDES item
586  * @data: the SDES data
587  *
588  * Store an SDES item of @type in @src. This function is similar to
589  * rtp_source_set_sdes() but takes a null-terminated string for convenience.
590  *
591  * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
592  */
593 gboolean
594 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
595     const gchar * data)
596 {
597   guint len;
598   gboolean result;
599
600   if (data)
601     len = strlen (data);
602   else
603     len = 0;
604
605   result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
606
607   return result;
608 }
609
610 /**
611  * rtp_source_get_sdes:
612  * @src: an #RTPSource
613  * @type: the type of the SDES item
614  * @data: location to store the SDES data or NULL
615  * @len: location to store the SDES length or NULL
616  *
617  * Get the SDES item of @type from @src. Note that @data does not always point
618  * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
619  * null-terminated string instead.
620  *
621  * @data remains valid until the next call to rtp_source_set_sdes().
622  *
623  * Returns: %TRUE if @type was valid and @data and @len contain valid
624  * data. @data can be NULL when the item was unset.
625  */
626 gboolean
627 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
628     guint * len)
629 {
630   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
631
632   if (type < 0 || type > GST_RTCP_SDES_PRIV)
633     return FALSE;
634
635   if (data)
636     *data = src->sdes[type];
637   if (len)
638     *len = src->sdes_len[type];
639
640   return TRUE;
641 }
642
643 /**
644  * rtp_source_get_sdes_string:
645  * @src: an #RTPSource
646  * @type: the type of the SDES item
647  *
648  * Get the SDES item of @type from @src. 
649  *
650  * Returns: a null-terminated copy of the SDES item or NULL when @type was not
651  * valid or the SDES item was unset. g_free() after usage.
652  */
653 gchar *
654 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
655 {
656   gchar *result;
657
658   g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
659
660   if (type < 0 || type > GST_RTCP_SDES_PRIV)
661     return NULL;
662
663   result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
664
665   return result;
666 }
667
668 /**
669  * rtp_source_set_rtp_from:
670  * @src: an #RTPSource
671  * @address: the RTP address to set
672  *
673  * Set that @src is receiving RTP packets from @address. This is used for
674  * collistion checking.
675  */
676 void
677 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
678 {
679   g_return_if_fail (RTP_IS_SOURCE (src));
680
681   src->have_rtp_from = TRUE;
682   memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
683 }
684
685 /**
686  * rtp_source_set_rtcp_from:
687  * @src: an #RTPSource
688  * @address: the RTCP address to set
689  *
690  * Set that @src is receiving RTCP packets from @address. This is used for
691  * collistion checking.
692  */
693 void
694 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
695 {
696   g_return_if_fail (RTP_IS_SOURCE (src));
697
698   src->have_rtcp_from = TRUE;
699   memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
700 }
701
702 static GstFlowReturn
703 push_packet (RTPSource * src, GstBuffer * buffer)
704 {
705   GstFlowReturn ret = GST_FLOW_OK;
706
707   /* push queued packets first if any */
708   while (!g_queue_is_empty (src->packets)) {
709     GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
710
711     GST_LOG ("pushing queued packet");
712     if (src->callbacks.push_rtp)
713       src->callbacks.push_rtp (src, buffer, src->user_data);
714     else
715       gst_buffer_unref (buffer);
716   }
717   GST_LOG ("pushing new packet");
718   /* push packet */
719   if (src->callbacks.push_rtp)
720     ret = src->callbacks.push_rtp (src, buffer, src->user_data);
721   else
722     gst_buffer_unref (buffer);
723
724   return ret;
725 }
726
727 static gint
728 get_clock_rate (RTPSource * src, guint8 payload)
729 {
730   if (src->clock_rate == -1) {
731     gint clock_rate = -1;
732
733     if (src->callbacks.clock_rate)
734       clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
735
736     GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
737
738     src->clock_rate = clock_rate;
739   }
740   src->payload = payload;
741
742   return src->clock_rate;
743 }
744
745 /* Jitter is the variation in the delay of received packets in a flow. It is
746  * measured by comparing the interval when RTP packets were sent to the interval
747  * at which they were received. For instance, if packet #1 and packet #2 leave
748  * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
749  * milliseconds. */
750 static void
751 calculate_jitter (RTPSource * src, GstBuffer * buffer,
752     RTPArrivalStats * arrival)
753 {
754   guint64 ntpnstime;
755   guint32 rtparrival, transit, rtptime;
756   gint32 diff;
757   gint clock_rate;
758   guint8 pt;
759
760   /* get arrival time */
761   if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
762     goto no_time;
763
764   pt = gst_rtp_buffer_get_payload_type (buffer);
765
766   GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
767
768   /* get clockrate */
769   if ((clock_rate = get_clock_rate (src, pt)) == -1)
770     goto no_clock_rate;
771
772   rtptime = gst_rtp_buffer_get_timestamp (buffer);
773
774   /* no clock-base, take first rtptime as base */
775   if (src->clock_base == -1) {
776     GST_DEBUG ("using clock-base of %" G_GUINT32_FORMAT, rtptime);
777     src->clock_base = rtptime;
778     src->clock_base_time = arrival->timestamp;
779   }
780
781   /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
782    * care about the absolute value, just the difference. */
783   rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
784
785   /* transit time is difference with RTP timestamp */
786   transit = rtparrival - rtptime;
787
788   /* get ABS diff with previous transit time */
789   if (src->stats.transit != -1) {
790     if (transit > src->stats.transit)
791       diff = transit - src->stats.transit;
792     else
793       diff = src->stats.transit - transit;
794   } else
795     diff = 0;
796
797   src->stats.transit = transit;
798
799   /* update jitter, the value we store is scaled up so we can keep precision. */
800   src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
801
802   src->stats.prev_rtptime = src->stats.last_rtptime;
803   src->stats.last_rtptime = rtparrival;
804
805   GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
806       rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
807
808   return;
809
810   /* ERRORS */
811 no_time:
812   {
813     GST_WARNING ("cannot get current time");
814     return;
815   }
816 no_clock_rate:
817   {
818     GST_WARNING ("cannot get clock-rate for pt %d", pt);
819     return;
820   }
821 }
822
823 static void
824 init_seq (RTPSource * src, guint16 seq)
825 {
826   src->stats.base_seq = seq;
827   src->stats.max_seq = seq;
828   src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
829   src->stats.cycles = 0;
830   src->stats.packets_received = 0;
831   src->stats.octets_received = 0;
832   src->stats.bytes_received = 0;
833   src->stats.prev_received = 0;
834   src->stats.prev_expected = 0;
835
836   GST_DEBUG ("base_seq %d", seq);
837 }
838
839 /**
840  * rtp_source_process_rtp:
841  * @src: an #RTPSource
842  * @buffer: an RTP buffer
843  *
844  * Let @src handle the incomming RTP @buffer.
845  *
846  * Returns: a #GstFlowReturn.
847  */
848 GstFlowReturn
849 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
850     RTPArrivalStats * arrival)
851 {
852   GstFlowReturn result = GST_FLOW_OK;
853   guint16 seqnr, udelta;
854   RTPSourceStats *stats;
855
856   g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
857   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
858
859   stats = &src->stats;
860
861   seqnr = gst_rtp_buffer_get_seq (buffer);
862
863   rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
864
865   if (stats->cycles == -1) {
866     GST_DEBUG ("received first buffer");
867     /* first time we heard of this source */
868     init_seq (src, seqnr);
869     src->stats.max_seq = seqnr - 1;
870     src->probation = RTP_DEFAULT_PROBATION;
871   }
872
873   udelta = seqnr - stats->max_seq;
874
875   /* if we are still on probation, check seqnum */
876   if (src->probation) {
877     guint16 expected;
878
879     expected = src->stats.max_seq + 1;
880
881     /* when in probation, we require consecutive seqnums */
882     if (seqnr == expected) {
883       /* expected packet */
884       GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
885       src->probation--;
886       src->stats.max_seq = seqnr;
887       if (src->probation == 0) {
888         GST_DEBUG ("probation done!");
889         init_seq (src, seqnr);
890       } else {
891         GstBuffer *q;
892
893         GST_DEBUG ("probation %d: queue buffer", src->probation);
894         /* when still in probation, keep packets in a list. */
895         g_queue_push_tail (src->packets, buffer);
896         /* remove packets from queue if there are too many */
897         while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
898           q = g_queue_pop_head (src->packets);
899           gst_buffer_unref (q);
900         }
901         goto done;
902       }
903     } else {
904       GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
905       src->probation = RTP_DEFAULT_PROBATION;
906       src->stats.max_seq = seqnr;
907       goto done;
908     }
909   } else if (udelta < RTP_MAX_DROPOUT) {
910     /* in order, with permissible gap */
911     if (seqnr < stats->max_seq) {
912       /* sequence number wrapped - count another 64K cycle. */
913       stats->cycles += RTP_SEQ_MOD;
914     }
915     stats->max_seq = seqnr;
916   } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
917     /* the sequence number made a very large jump */
918     if (seqnr == stats->bad_seq) {
919       /* two sequential packets -- assume that the other side
920        * restarted without telling us so just re-sync
921        * (i.e., pretend this was the first packet).  */
922       init_seq (src, seqnr);
923     } else {
924       /* unacceptable jump */
925       stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
926       src->clock_base = -1;
927       goto bad_sequence;
928     }
929   } else {
930     /* duplicate or reordered packet, will be filtered by jitterbuffer. */
931     GST_WARNING ("duplicate or reordered packet");
932     src->clock_base = -1;
933   }
934
935   src->stats.octets_received += arrival->payload_len;
936   src->stats.bytes_received += arrival->bytes;
937   src->stats.packets_received++;
938   /* the source that sent the packet must be a sender */
939   src->is_sender = TRUE;
940   src->validated = TRUE;
941
942   GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
943       seqnr, src->stats.packets_received, src->stats.octets_received);
944
945   /* calculate jitter for the stats */
946   calculate_jitter (src, buffer, arrival);
947
948   /* we're ready to push the RTP packet now */
949   result = push_packet (src, buffer);
950
951 done:
952   return result;
953
954   /* ERRORS */
955 bad_sequence:
956   {
957     GST_WARNING ("unacceptable seqnum received");
958     return GST_FLOW_OK;
959   }
960 }
961
962 /**
963  * rtp_source_process_bye:
964  * @src: an #RTPSource
965  * @reason: the reason for leaving
966  *
967  * Notify @src that a BYE packet has been received. This will make the source
968  * inactive.
969  */
970 void
971 rtp_source_process_bye (RTPSource * src, const gchar * reason)
972 {
973   g_return_if_fail (RTP_IS_SOURCE (src));
974
975   GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
976       GST_STR_NULL (reason));
977
978   /* copy the reason and mark as received_bye */
979   g_free (src->bye_reason);
980   src->bye_reason = g_strdup (reason);
981   src->received_bye = TRUE;
982 }
983
984 /**
985  * rtp_source_send_rtp:
986  * @src: an #RTPSource
987  * @buffer: an RTP buffer
988  * @ntpnstime: the NTP time when this buffer was captured in nanoseconds
989  *
990  * Send an RTP @buffer originating from @src. This will make @src a sender.
991  * This function takes ownership of @buffer and modifies the SSRC in the RTP
992  * packet to that of @src when needed.
993  *
994  * Returns: a #GstFlowReturn.
995  */
996 GstFlowReturn
997 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
998 {
999   GstFlowReturn result = GST_FLOW_OK;
1000   guint len;
1001   guint32 rtptime;
1002   guint64 ext_rtptime;
1003   guint64 ntp_diff, rtp_diff;
1004
1005   g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1006   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1007
1008   len = gst_rtp_buffer_get_payload_len (buffer);
1009
1010   rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
1011
1012   /* we are a sender now */
1013   src->is_sender = TRUE;
1014
1015   /* update stats for the SR */
1016   src->stats.packets_sent++;
1017   src->stats.octets_sent += len;
1018
1019   rtptime = gst_rtp_buffer_get_timestamp (buffer);
1020   ext_rtptime = src->last_rtptime;
1021   ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1022
1023   GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1024       src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1025
1026   if (ext_rtptime > src->last_rtptime) {
1027     rtp_diff = ext_rtptime - src->last_rtptime;
1028     ntp_diff = ntpnstime - src->last_ntpnstime;
1029
1030     /* calc the diff so we can detect drift at the sender. This can also be used
1031      * to guestimate the clock rate if the NTP time is locked to the RTP
1032      * timestamps (as is the case when the capture device is providing the clock). */
1033     GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1034         GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1035   }
1036
1037   /* we keep track of the last received RTP timestamp and the corresponding
1038    * NTP timestamp so that we can use this info when constructing SR reports */
1039   src->last_rtptime = ext_rtptime;
1040   src->last_ntpnstime = ntpnstime;
1041
1042   /* push packet */
1043   if (src->callbacks.push_rtp) {
1044     guint32 ssrc;
1045
1046     ssrc = gst_rtp_buffer_get_ssrc (buffer);
1047     if (ssrc != src->ssrc) {
1048       /* the SSRC of the packet is not correct, make a writable buffer and
1049        * update the SSRC. This could involve a complete copy of the packet when
1050        * it is not writable. Usually the payloader will use caps negotiation to
1051        * get the correct SSRC from the session manager before pushing anything. */
1052       buffer = gst_buffer_make_writable (buffer);
1053
1054       GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1055           src->ssrc);
1056       gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1057     }
1058     GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1059     result = src->callbacks.push_rtp (src, buffer, src->user_data);
1060   } else {
1061     GST_WARNING ("no callback installed, dropping packet");
1062     gst_buffer_unref (buffer);
1063   }
1064
1065   return result;
1066 }
1067
1068 /**
1069  * rtp_source_process_sr:
1070  * @src: an #RTPSource
1071  * @time: time of packet arrival
1072  * @ntptime: the NTP time in 32.32 fixed point
1073  * @rtptime: the RTP time
1074  * @packet_count: the packet count
1075  * @octet_count: the octect count
1076  *
1077  * Update the sender report in @src.
1078  */
1079 void
1080 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1081     guint32 rtptime, guint32 packet_count, guint32 octet_count)
1082 {
1083   RTPSenderReport *curr;
1084   gint curridx;
1085
1086   g_return_if_fail (RTP_IS_SOURCE (src));
1087
1088   GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1089       ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1090       (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1091       packet_count, octet_count);
1092
1093   curridx = src->stats.curr_sr ^ 1;
1094   curr = &src->stats.sr[curridx];
1095
1096   /* this is a sender now */
1097   src->is_sender = TRUE;
1098
1099   /* update current */
1100   curr->is_valid = TRUE;
1101   curr->ntptime = ntptime;
1102   curr->rtptime = rtptime;
1103   curr->packet_count = packet_count;
1104   curr->octet_count = octet_count;
1105   curr->time = time;
1106
1107   /* make current */
1108   src->stats.curr_sr = curridx;
1109 }
1110
1111 /**
1112  * rtp_source_process_rb:
1113  * @src: an #RTPSource
1114  * @time: the current time in nanoseconds since 1970
1115  * @fractionlost: fraction lost since last SR/RR
1116  * @packetslost: the cumululative number of packets lost
1117  * @exthighestseq: the extended last sequence number received
1118  * @jitter: the interarrival jitter
1119  * @lsr: the last SR packet from this source
1120  * @dlsr: the delay since last SR packet
1121  *
1122  * Update the report block in @src.
1123  */
1124 void
1125 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1126     gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1127     guint32 dlsr)
1128 {
1129   RTPReceiverReport *curr;
1130   gint curridx;
1131   guint32 ntp, A;
1132
1133   g_return_if_fail (RTP_IS_SOURCE (src));
1134
1135   GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1136       ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1137       src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1138       lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1139
1140   curridx = src->stats.curr_rr ^ 1;
1141   curr = &src->stats.rr[curridx];
1142
1143   /* update current */
1144   curr->is_valid = TRUE;
1145   curr->fractionlost = fractionlost;
1146   curr->packetslost = packetslost;
1147   curr->exthighestseq = exthighestseq;
1148   curr->jitter = jitter;
1149   curr->lsr = lsr;
1150   curr->dlsr = dlsr;
1151
1152   /* calculate round trip */
1153   ntp = (gst_rtcp_unix_to_ntp (time) >> 16) & 0xffffffff;
1154   A = ntp - dlsr;
1155   A -= lsr;
1156   curr->round_trip = A;
1157
1158   GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1159       A >> 16, A & 0xffff);
1160
1161   /* make current */
1162   src->stats.curr_rr = curridx;
1163 }
1164
1165 /**
1166  * rtp_source_get_new_sr:
1167  * @src: an #RTPSource
1168  * @ntpnstime: the current time in nanoseconds since 1970
1169  * @ntptime: the NTP time in 32.32 fixed point
1170  * @rtptime: the RTP time corresponding to @ntptime
1171  * @packet_count: the packet count
1172  * @octet_count: the octect count
1173  *
1174  * Get new values to put into a new SR report from this source.
1175  *
1176  * Returns: %TRUE on success.
1177  */
1178 gboolean
1179 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1180     guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1181     guint32 * octet_count)
1182 {
1183   guint64 t_rtp;
1184   guint64 t_current_ntp;
1185   GstClockTimeDiff diff;
1186
1187   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1188
1189   /* use the sync params to interpolate the date->time member to rtptime. We
1190    * use the last sent timestamp and rtptime as reference points. We assume
1191    * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1192    * sufficient for the frequency at which we report SR and the rate we send
1193    * out RTP packets. */
1194   t_rtp = src->last_rtptime;
1195
1196   GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1197       G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1198
1199   if (src->clock_rate != -1) {
1200     /* get the diff with the SR time */
1201     diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1202
1203     /* now translate the diff to RTP time, handle positive and negative cases.
1204      * If there is no diff, we already set rtptime correctly above. */
1205     if (diff > 0) {
1206       GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1207           GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1208       t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1209     } else {
1210       diff = -diff;
1211       GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1212           GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1213       t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1214     }
1215   } else {
1216     GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1217   }
1218
1219   /* convert the NTP time in nanoseconds to 32.32 fixed point */
1220   t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1221
1222   GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1223       (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1224       (guint32) t_rtp);
1225
1226   if (ntptime)
1227     *ntptime = t_current_ntp;
1228   if (rtptime)
1229     *rtptime = t_rtp;
1230   if (packet_count)
1231     *packet_count = src->stats.packets_sent;
1232   if (octet_count)
1233     *octet_count = src->stats.octets_sent;
1234
1235   return TRUE;
1236 }
1237
1238 /**
1239  * rtp_source_get_new_rb:
1240  * @src: an #RTPSource
1241  * @ntpnstime: the current time in nanoseconds since 1970
1242  * @fractionlost: fraction lost since last SR/RR
1243  * @packetslost: the cumululative number of packets lost
1244  * @exthighestseq: the extended last sequence number received
1245  * @jitter: the interarrival jitter
1246  * @lsr: the last SR packet from this source
1247  * @dlsr: the delay since last SR packet
1248  *
1249  * Get new values to put into a new report block from this source.
1250  *
1251  * Returns: %TRUE on success.
1252  */
1253 gboolean
1254 rtp_source_get_new_rb (RTPSource * src, guint64 ntpnstime,
1255     guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1256     guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1257 {
1258   RTPSourceStats *stats;
1259   guint64 extended_max, expected;
1260   guint64 expected_interval, received_interval, ntptime;
1261   gint64 lost, lost_interval;
1262   guint32 fraction, LSR, DLSR;
1263   GstClockTime sr_time;
1264
1265   stats = &src->stats;
1266
1267   extended_max = stats->cycles + stats->max_seq;
1268   expected = extended_max - stats->base_seq + 1;
1269
1270   GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1271       ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1272       extended_max, expected, stats->packets_received, stats->base_seq);
1273
1274   lost = expected - stats->packets_received;
1275   lost = CLAMP (lost, -0x800000, 0x7fffff);
1276
1277   expected_interval = expected - stats->prev_expected;
1278   stats->prev_expected = expected;
1279   received_interval = stats->packets_received - stats->prev_received;
1280   stats->prev_received = stats->packets_received;
1281
1282   lost_interval = expected_interval - received_interval;
1283
1284   if (expected_interval == 0 || lost_interval <= 0)
1285     fraction = 0;
1286   else
1287     fraction = (lost_interval << 8) / expected_interval;
1288
1289   GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1290   /* we scaled the jitter up for additional precision */
1291   GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1292       ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1293       extended_max, stats->jitter >> 4);
1294
1295   if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1296     GstClockTime diff;
1297
1298     /* LSR is middle 32 bits of the last ntptime */
1299     LSR = (ntptime >> 16) & 0xffffffff;
1300     diff = ntpnstime - sr_time;
1301     GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1302     /* DLSR, delay since last SR is expressed in 1/65536 second units */
1303     DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1304   } else {
1305     /* No valid SR received, LSR/DLSR are set to 0 then */
1306     GST_DEBUG ("no valid SR received");
1307     LSR = 0;
1308     DLSR = 0;
1309   }
1310   GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1311       DLSR >> 16, DLSR & 0xffff);
1312
1313   if (fractionlost)
1314     *fractionlost = fraction;
1315   if (packetslost)
1316     *packetslost = lost;
1317   if (exthighestseq)
1318     *exthighestseq = extended_max;
1319   if (jitter)
1320     *jitter = stats->jitter >> 4;
1321   if (lsr)
1322     *lsr = LSR;
1323   if (dlsr)
1324     *dlsr = DLSR;
1325
1326   return TRUE;
1327 }
1328
1329 /**
1330  * rtp_source_get_last_sr:
1331  * @src: an #RTPSource
1332  * @time: time of packet arrival
1333  * @ntptime: the NTP time in 32.32 fixed point
1334  * @rtptime: the RTP time
1335  * @packet_count: the packet count
1336  * @octet_count: the octect count
1337  *
1338  * Get the values of the last sender report as set with rtp_source_process_sr().
1339  *
1340  * Returns: %TRUE if there was a valid SR report.
1341  */
1342 gboolean
1343 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1344     guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1345 {
1346   RTPSenderReport *curr;
1347
1348   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1349
1350   curr = &src->stats.sr[src->stats.curr_sr];
1351   if (!curr->is_valid)
1352     return FALSE;
1353
1354   if (ntptime)
1355     *ntptime = curr->ntptime;
1356   if (rtptime)
1357     *rtptime = curr->rtptime;
1358   if (packet_count)
1359     *packet_count = curr->packet_count;
1360   if (octet_count)
1361     *octet_count = curr->octet_count;
1362   if (time)
1363     *time = curr->time;
1364
1365   return TRUE;
1366 }
1367
1368 /**
1369  * rtp_source_get_last_rb:
1370  * @src: an #RTPSource
1371  * @fractionlost: fraction lost since last SR/RR
1372  * @packetslost: the cumululative number of packets lost
1373  * @exthighestseq: the extended last sequence number received
1374  * @jitter: the interarrival jitter
1375  * @lsr: the last SR packet from this source
1376  * @dlsr: the delay since last SR packet
1377  *
1378  * Get the values of the last RB report set with rtp_source_process_rb().
1379  *
1380  * Returns: %TRUE if there was a valid SB report.
1381  */
1382 gboolean
1383 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1384     gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1385     guint32 * lsr, guint32 * dlsr)
1386 {
1387   RTPReceiverReport *curr;
1388
1389   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1390
1391   curr = &src->stats.rr[src->stats.curr_rr];
1392   if (!curr->is_valid)
1393     return FALSE;
1394
1395   if (fractionlost)
1396     *fractionlost = curr->fractionlost;
1397   if (packetslost)
1398     *packetslost = curr->packetslost;
1399   if (exthighestseq)
1400     *exthighestseq = curr->exthighestseq;
1401   if (jitter)
1402     *jitter = curr->jitter;
1403   if (lsr)
1404     *lsr = curr->lsr;
1405   if (dlsr)
1406     *dlsr = curr->dlsr;
1407
1408   return TRUE;
1409 }