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