gst/rtpmanager/gstrtpbin.*: Add signal to notify listeners when a sender becomes...
[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->packets = g_queue_new ();
174   src->seqnum_base = -1;
175   src->last_rtptime = -1;
176
177   rtp_source_reset (src);
178 }
179
180 static void
181 rtp_source_finalize (GObject * object)
182 {
183   RTPSource *src;
184   GstBuffer *buffer;
185   gint i;
186
187   src = RTP_SOURCE_CAST (object);
188
189   while ((buffer = g_queue_pop_head (src->packets)))
190     gst_buffer_unref (buffer);
191   g_queue_free (src->packets);
192
193   for (i = 0; i < 9; i++)
194     g_free (src->sdes[i]);
195
196   g_free (src->bye_reason);
197
198   gst_caps_replace (&src->caps, NULL);
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, "seqnum-base", &val))
529     src->seqnum_base = val;
530   GST_DEBUG ("got seqnum-base %" G_GINT32_FORMAT, src->seqnum_base);
531
532   gst_caps_replace (&src->caps, caps);
533 }
534
535 /**
536  * rtp_source_set_sdes:
537  * @src: an #RTPSource
538  * @type: the type of the SDES item
539  * @data: the SDES data
540  * @len: the SDES length
541  *
542  * Store an SDES item of @type in @src. 
543  *
544  * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
545  */
546 gboolean
547 rtp_source_set_sdes (RTPSource * src, GstRTCPSDESType type,
548     const guint8 * data, guint len)
549 {
550   guint8 *old;
551
552   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
553
554   if (type < 0 || type > GST_RTCP_SDES_PRIV)
555     return FALSE;
556
557   old = src->sdes[type];
558
559   /* lengths are the same, check if the data is the same */
560   if ((src->sdes_len[type] == len))
561     if (data != NULL && old != NULL && (memcmp (old, data, len) == 0))
562       return FALSE;
563
564   /* NULL data, make sure we store 0 length or if no length is given,
565    * take strlen */
566   if (data == NULL)
567     len = 0;
568
569   g_free (src->sdes[type]);
570   src->sdes[type] = g_memdup (data, len);
571   src->sdes_len[type] = len;
572
573   return TRUE;
574 }
575
576 /**
577  * rtp_source_set_sdes_string:
578  * @src: an #RTPSource
579  * @type: the type of the SDES item
580  * @data: the SDES data
581  *
582  * Store an SDES item of @type in @src. This function is similar to
583  * rtp_source_set_sdes() but takes a null-terminated string for convenience.
584  *
585  * Returns: %FALSE if the SDES item was unchanged or @type is unknown.
586  */
587 gboolean
588 rtp_source_set_sdes_string (RTPSource * src, GstRTCPSDESType type,
589     const gchar * data)
590 {
591   guint len;
592   gboolean result;
593
594   if (data)
595     len = strlen (data);
596   else
597     len = 0;
598
599   result = rtp_source_set_sdes (src, type, (guint8 *) data, len);
600
601   return result;
602 }
603
604 /**
605  * rtp_source_get_sdes:
606  * @src: an #RTPSource
607  * @type: the type of the SDES item
608  * @data: location to store the SDES data or NULL
609  * @len: location to store the SDES length or NULL
610  *
611  * Get the SDES item of @type from @src. Note that @data does not always point
612  * to a null-terminated string, use rtp_source_get_sdes_string() to retrieve a
613  * null-terminated string instead.
614  *
615  * @data remains valid until the next call to rtp_source_set_sdes().
616  *
617  * Returns: %TRUE if @type was valid and @data and @len contain valid
618  * data. @data can be NULL when the item was unset.
619  */
620 gboolean
621 rtp_source_get_sdes (RTPSource * src, GstRTCPSDESType type, guint8 ** data,
622     guint * len)
623 {
624   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
625
626   if (type < 0 || type > GST_RTCP_SDES_PRIV)
627     return FALSE;
628
629   if (data)
630     *data = src->sdes[type];
631   if (len)
632     *len = src->sdes_len[type];
633
634   return TRUE;
635 }
636
637 /**
638  * rtp_source_get_sdes_string:
639  * @src: an #RTPSource
640  * @type: the type of the SDES item
641  *
642  * Get the SDES item of @type from @src. 
643  *
644  * Returns: a null-terminated copy of the SDES item or NULL when @type was not
645  * valid or the SDES item was unset. g_free() after usage.
646  */
647 gchar *
648 rtp_source_get_sdes_string (RTPSource * src, GstRTCPSDESType type)
649 {
650   gchar *result;
651
652   g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
653
654   if (type < 0 || type > GST_RTCP_SDES_PRIV)
655     return NULL;
656
657   result = g_strndup ((const gchar *) src->sdes[type], src->sdes_len[type]);
658
659   return result;
660 }
661
662 /**
663  * rtp_source_set_rtp_from:
664  * @src: an #RTPSource
665  * @address: the RTP address to set
666  *
667  * Set that @src is receiving RTP packets from @address. This is used for
668  * collistion checking.
669  */
670 void
671 rtp_source_set_rtp_from (RTPSource * src, GstNetAddress * address)
672 {
673   g_return_if_fail (RTP_IS_SOURCE (src));
674
675   src->have_rtp_from = TRUE;
676   memcpy (&src->rtp_from, address, sizeof (GstNetAddress));
677 }
678
679 /**
680  * rtp_source_set_rtcp_from:
681  * @src: an #RTPSource
682  * @address: the RTCP address to set
683  *
684  * Set that @src is receiving RTCP packets from @address. This is used for
685  * collistion checking.
686  */
687 void
688 rtp_source_set_rtcp_from (RTPSource * src, GstNetAddress * address)
689 {
690   g_return_if_fail (RTP_IS_SOURCE (src));
691
692   src->have_rtcp_from = TRUE;
693   memcpy (&src->rtcp_from, address, sizeof (GstNetAddress));
694 }
695
696 static GstFlowReturn
697 push_packet (RTPSource * src, GstBuffer * buffer)
698 {
699   GstFlowReturn ret = GST_FLOW_OK;
700
701   /* push queued packets first if any */
702   while (!g_queue_is_empty (src->packets)) {
703     GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
704
705     GST_LOG ("pushing queued packet");
706     if (src->callbacks.push_rtp)
707       src->callbacks.push_rtp (src, buffer, src->user_data);
708     else
709       gst_buffer_unref (buffer);
710   }
711   GST_LOG ("pushing new packet");
712   /* push packet */
713   if (src->callbacks.push_rtp)
714     ret = src->callbacks.push_rtp (src, buffer, src->user_data);
715   else
716     gst_buffer_unref (buffer);
717
718   return ret;
719 }
720
721 static gint
722 get_clock_rate (RTPSource * src, guint8 payload)
723 {
724   if (src->clock_rate == -1) {
725     gint clock_rate = -1;
726
727     if (src->callbacks.clock_rate)
728       clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
729
730     GST_DEBUG ("new payload %d, got clock-rate %d", payload, clock_rate);
731
732     src->clock_rate = clock_rate;
733   }
734   src->payload = payload;
735
736   return src->clock_rate;
737 }
738
739 /* Jitter is the variation in the delay of received packets in a flow. It is
740  * measured by comparing the interval when RTP packets were sent to the interval
741  * at which they were received. For instance, if packet #1 and packet #2 leave
742  * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
743  * milliseconds. */
744 static void
745 calculate_jitter (RTPSource * src, GstBuffer * buffer,
746     RTPArrivalStats * arrival)
747 {
748   guint64 ntpnstime;
749   guint32 rtparrival, transit, rtptime;
750   gint32 diff;
751   gint clock_rate;
752   guint8 pt;
753
754   /* get arrival time */
755   if ((ntpnstime = arrival->ntpnstime) == GST_CLOCK_TIME_NONE)
756     goto no_time;
757
758   pt = gst_rtp_buffer_get_payload_type (buffer);
759
760   GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
761
762   /* get clockrate */
763   if ((clock_rate = get_clock_rate (src, pt)) == -1)
764     goto no_clock_rate;
765
766   rtptime = gst_rtp_buffer_get_timestamp (buffer);
767
768   /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
769    * care about the absolute value, just the difference. */
770   rtparrival = gst_util_uint64_scale_int (ntpnstime, clock_rate, GST_SECOND);
771
772   /* transit time is difference with RTP timestamp */
773   transit = rtparrival - rtptime;
774
775   /* get ABS diff with previous transit time */
776   if (src->stats.transit != -1) {
777     if (transit > src->stats.transit)
778       diff = transit - src->stats.transit;
779     else
780       diff = src->stats.transit - transit;
781   } else
782     diff = 0;
783
784   src->stats.transit = transit;
785
786   /* update jitter, the value we store is scaled up so we can keep precision. */
787   src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
788
789   src->stats.prev_rtptime = src->stats.last_rtptime;
790   src->stats.last_rtptime = rtparrival;
791
792   GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
793       rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
794
795   return;
796
797   /* ERRORS */
798 no_time:
799   {
800     GST_WARNING ("cannot get current time");
801     return;
802   }
803 no_clock_rate:
804   {
805     GST_WARNING ("cannot get clock-rate for pt %d", pt);
806     return;
807   }
808 }
809
810 static void
811 init_seq (RTPSource * src, guint16 seq)
812 {
813   src->stats.base_seq = seq;
814   src->stats.max_seq = seq;
815   src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
816   src->stats.cycles = 0;
817   src->stats.packets_received = 0;
818   src->stats.octets_received = 0;
819   src->stats.bytes_received = 0;
820   src->stats.prev_received = 0;
821   src->stats.prev_expected = 0;
822
823   GST_DEBUG ("base_seq %d", seq);
824 }
825
826 /**
827  * rtp_source_process_rtp:
828  * @src: an #RTPSource
829  * @buffer: an RTP buffer
830  *
831  * Let @src handle the incomming RTP @buffer.
832  *
833  * Returns: a #GstFlowReturn.
834  */
835 GstFlowReturn
836 rtp_source_process_rtp (RTPSource * src, GstBuffer * buffer,
837     RTPArrivalStats * arrival)
838 {
839   GstFlowReturn result = GST_FLOW_OK;
840   guint16 seqnr, udelta;
841   RTPSourceStats *stats;
842
843   g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
844   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
845
846   stats = &src->stats;
847
848   seqnr = gst_rtp_buffer_get_seq (buffer);
849
850   rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
851
852   if (stats->cycles == -1) {
853     GST_DEBUG ("received first buffer");
854     /* first time we heard of this source */
855     init_seq (src, seqnr);
856     src->stats.max_seq = seqnr - 1;
857     src->probation = RTP_DEFAULT_PROBATION;
858   }
859
860   udelta = seqnr - stats->max_seq;
861
862   /* if we are still on probation, check seqnum */
863   if (src->probation) {
864     guint16 expected;
865
866     expected = src->stats.max_seq + 1;
867
868     /* when in probation, we require consecutive seqnums */
869     if (seqnr == expected) {
870       /* expected packet */
871       GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
872       src->probation--;
873       src->stats.max_seq = seqnr;
874       if (src->probation == 0) {
875         GST_DEBUG ("probation done!");
876         init_seq (src, seqnr);
877       } else {
878         GstBuffer *q;
879
880         GST_DEBUG ("probation %d: queue buffer", src->probation);
881         /* when still in probation, keep packets in a list. */
882         g_queue_push_tail (src->packets, buffer);
883         /* remove packets from queue if there are too many */
884         while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
885           q = g_queue_pop_head (src->packets);
886           gst_buffer_unref (q);
887         }
888         goto done;
889       }
890     } else {
891       GST_DEBUG ("probation: seqnr %d != expected %d", seqnr, expected);
892       src->probation = RTP_DEFAULT_PROBATION;
893       src->stats.max_seq = seqnr;
894       goto done;
895     }
896   } else if (udelta < RTP_MAX_DROPOUT) {
897     /* in order, with permissible gap */
898     if (seqnr < stats->max_seq) {
899       /* sequence number wrapped - count another 64K cycle. */
900       stats->cycles += RTP_SEQ_MOD;
901     }
902     stats->max_seq = seqnr;
903   } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
904     /* the sequence number made a very large jump */
905     if (seqnr == stats->bad_seq) {
906       /* two sequential packets -- assume that the other side
907        * restarted without telling us so just re-sync
908        * (i.e., pretend this was the first packet).  */
909       init_seq (src, seqnr);
910     } else {
911       /* unacceptable jump */
912       stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
913       goto bad_sequence;
914     }
915   } else {
916     /* duplicate or reordered packet, will be filtered by jitterbuffer. */
917     GST_WARNING ("duplicate or reordered packet");
918   }
919
920   src->stats.octets_received += arrival->payload_len;
921   src->stats.bytes_received += arrival->bytes;
922   src->stats.packets_received++;
923   /* the source that sent the packet must be a sender */
924   src->is_sender = TRUE;
925   src->validated = TRUE;
926
927   GST_LOG ("seq %d, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
928       seqnr, src->stats.packets_received, src->stats.octets_received);
929
930   /* calculate jitter for the stats */
931   calculate_jitter (src, buffer, arrival);
932
933   /* we're ready to push the RTP packet now */
934   result = push_packet (src, buffer);
935
936 done:
937   return result;
938
939   /* ERRORS */
940 bad_sequence:
941   {
942     GST_WARNING ("unacceptable seqnum received");
943     return GST_FLOW_OK;
944   }
945 }
946
947 /**
948  * rtp_source_process_bye:
949  * @src: an #RTPSource
950  * @reason: the reason for leaving
951  *
952  * Notify @src that a BYE packet has been received. This will make the source
953  * inactive.
954  */
955 void
956 rtp_source_process_bye (RTPSource * src, const gchar * reason)
957 {
958   g_return_if_fail (RTP_IS_SOURCE (src));
959
960   GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
961       GST_STR_NULL (reason));
962
963   /* copy the reason and mark as received_bye */
964   g_free (src->bye_reason);
965   src->bye_reason = g_strdup (reason);
966   src->received_bye = TRUE;
967 }
968
969 /**
970  * rtp_source_send_rtp:
971  * @src: an #RTPSource
972  * @buffer: an RTP buffer
973  * @ntpnstime: the NTP time when this buffer was captured in nanoseconds
974  *
975  * Send an RTP @buffer originating from @src. This will make @src a sender.
976  * This function takes ownership of @buffer and modifies the SSRC in the RTP
977  * packet to that of @src when needed.
978  *
979  * Returns: a #GstFlowReturn.
980  */
981 GstFlowReturn
982 rtp_source_send_rtp (RTPSource * src, GstBuffer * buffer, guint64 ntpnstime)
983 {
984   GstFlowReturn result = GST_FLOW_OK;
985   guint len;
986   guint32 rtptime;
987   guint64 ext_rtptime;
988   guint64 ntp_diff, rtp_diff;
989
990   g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
991   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
992
993   len = gst_rtp_buffer_get_payload_len (buffer);
994
995   rtp_source_update_caps (src, GST_BUFFER_CAPS (buffer));
996
997   /* we are a sender now */
998   src->is_sender = TRUE;
999
1000   /* update stats for the SR */
1001   src->stats.packets_sent++;
1002   src->stats.octets_sent += len;
1003
1004   rtptime = gst_rtp_buffer_get_timestamp (buffer);
1005   ext_rtptime = src->last_rtptime;
1006   ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1007
1008   GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", NTP %" GST_TIME_FORMAT,
1009       src->ssrc, ext_rtptime, GST_TIME_ARGS (ntpnstime));
1010
1011   if (ext_rtptime > src->last_rtptime) {
1012     rtp_diff = ext_rtptime - src->last_rtptime;
1013     ntp_diff = ntpnstime - src->last_ntpnstime;
1014
1015     /* calc the diff so we can detect drift at the sender. This can also be used
1016      * to guestimate the clock rate if the NTP time is locked to the RTP
1017      * timestamps (as is the case when the capture device is providing the clock). */
1018     GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff NTP %"
1019         GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (ntp_diff));
1020   }
1021
1022   /* we keep track of the last received RTP timestamp and the corresponding
1023    * NTP timestamp so that we can use this info when constructing SR reports */
1024   src->last_rtptime = ext_rtptime;
1025   src->last_ntpnstime = ntpnstime;
1026
1027   /* push packet */
1028   if (src->callbacks.push_rtp) {
1029     guint32 ssrc;
1030
1031     ssrc = gst_rtp_buffer_get_ssrc (buffer);
1032     if (ssrc != src->ssrc) {
1033       /* the SSRC of the packet is not correct, make a writable buffer and
1034        * update the SSRC. This could involve a complete copy of the packet when
1035        * it is not writable. Usually the payloader will use caps negotiation to
1036        * get the correct SSRC from the session manager before pushing anything. */
1037       buffer = gst_buffer_make_writable (buffer);
1038
1039       GST_WARNING ("updating SSRC from %08x to %08x, fix the payloader", ssrc,
1040           src->ssrc);
1041       gst_rtp_buffer_set_ssrc (buffer, src->ssrc);
1042     }
1043     GST_LOG ("pushing RTP packet %" G_GUINT64_FORMAT, src->stats.packets_sent);
1044     result = src->callbacks.push_rtp (src, buffer, src->user_data);
1045   } else {
1046     GST_WARNING ("no callback installed, dropping packet");
1047     gst_buffer_unref (buffer);
1048   }
1049
1050   return result;
1051 }
1052
1053 /**
1054  * rtp_source_process_sr:
1055  * @src: an #RTPSource
1056  * @time: time of packet arrival
1057  * @ntptime: the NTP time in 32.32 fixed point
1058  * @rtptime: the RTP time
1059  * @packet_count: the packet count
1060  * @octet_count: the octect count
1061  *
1062  * Update the sender report in @src.
1063  */
1064 void
1065 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1066     guint32 rtptime, guint32 packet_count, guint32 octet_count)
1067 {
1068   RTPSenderReport *curr;
1069   gint curridx;
1070
1071   g_return_if_fail (RTP_IS_SOURCE (src));
1072
1073   GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1074       ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1075       (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1076       packet_count, octet_count);
1077
1078   curridx = src->stats.curr_sr ^ 1;
1079   curr = &src->stats.sr[curridx];
1080
1081   /* this is a sender now */
1082   src->is_sender = TRUE;
1083
1084   /* update current */
1085   curr->is_valid = TRUE;
1086   curr->ntptime = ntptime;
1087   curr->rtptime = rtptime;
1088   curr->packet_count = packet_count;
1089   curr->octet_count = octet_count;
1090   curr->time = time;
1091
1092   /* make current */
1093   src->stats.curr_sr = curridx;
1094 }
1095
1096 /**
1097  * rtp_source_process_rb:
1098  * @src: an #RTPSource
1099  * @time: the current time in nanoseconds since 1970
1100  * @fractionlost: fraction lost since last SR/RR
1101  * @packetslost: the cumululative number of packets lost
1102  * @exthighestseq: the extended last sequence number received
1103  * @jitter: the interarrival jitter
1104  * @lsr: the last SR packet from this source
1105  * @dlsr: the delay since last SR packet
1106  *
1107  * Update the report block in @src.
1108  */
1109 void
1110 rtp_source_process_rb (RTPSource * src, GstClockTime time, guint8 fractionlost,
1111     gint32 packetslost, guint32 exthighestseq, guint32 jitter, guint32 lsr,
1112     guint32 dlsr)
1113 {
1114   RTPReceiverReport *curr;
1115   gint curridx;
1116   guint32 ntp, A;
1117
1118   g_return_if_fail (RTP_IS_SOURCE (src));
1119
1120   GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1121       ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1122       src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1123       lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1124
1125   curridx = src->stats.curr_rr ^ 1;
1126   curr = &src->stats.rr[curridx];
1127
1128   /* update current */
1129   curr->is_valid = TRUE;
1130   curr->fractionlost = fractionlost;
1131   curr->packetslost = packetslost;
1132   curr->exthighestseq = exthighestseq;
1133   curr->jitter = jitter;
1134   curr->lsr = lsr;
1135   curr->dlsr = dlsr;
1136
1137   /* calculate round trip */
1138   ntp = (gst_rtcp_unix_to_ntp (time) >> 16) & 0xffffffff;
1139   A = ntp - dlsr;
1140   A -= lsr;
1141   curr->round_trip = A;
1142
1143   GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1144       A >> 16, A & 0xffff);
1145
1146   /* make current */
1147   src->stats.curr_rr = curridx;
1148 }
1149
1150 /**
1151  * rtp_source_get_new_sr:
1152  * @src: an #RTPSource
1153  * @ntpnstime: the current time in nanoseconds since 1970
1154  * @ntptime: the NTP time in 32.32 fixed point
1155  * @rtptime: the RTP time corresponding to @ntptime
1156  * @packet_count: the packet count
1157  * @octet_count: the octect count
1158  *
1159  * Get new values to put into a new SR report from this source.
1160  *
1161  * Returns: %TRUE on success.
1162  */
1163 gboolean
1164 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1165     guint64 * ntptime, guint32 * rtptime, guint32 * packet_count,
1166     guint32 * octet_count)
1167 {
1168   guint64 t_rtp;
1169   guint64 t_current_ntp;
1170   GstClockTimeDiff diff;
1171
1172   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1173
1174   /* use the sync params to interpolate the date->time member to rtptime. We
1175    * use the last sent timestamp and rtptime as reference points. We assume
1176    * that the slope of the rtptime vs timestamp curve is 1, which is certainly
1177    * sufficient for the frequency at which we report SR and the rate we send
1178    * out RTP packets. */
1179   t_rtp = src->last_rtptime;
1180
1181   GST_DEBUG ("last_ntpnstime %" GST_TIME_FORMAT ", last_rtptime %"
1182       G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_ntpnstime), t_rtp);
1183
1184   if (src->clock_rate != -1) {
1185     /* get the diff with the SR time */
1186     diff = GST_CLOCK_DIFF (src->last_ntpnstime, ntpnstime);
1187
1188     /* now translate the diff to RTP time, handle positive and negative cases.
1189      * If there is no diff, we already set rtptime correctly above. */
1190     if (diff > 0) {
1191       GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff %" GST_TIME_FORMAT,
1192           GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1193       t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1194     } else {
1195       diff = -diff;
1196       GST_DEBUG ("ntpnstime %" GST_TIME_FORMAT ", diff -%" GST_TIME_FORMAT,
1197           GST_TIME_ARGS (ntpnstime), GST_TIME_ARGS (diff));
1198       t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1199     }
1200   } else {
1201     GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1202   }
1203
1204   /* convert the NTP time in nanoseconds to 32.32 fixed point */
1205   t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1206
1207   GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1208       (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1209       (guint32) t_rtp);
1210
1211   if (ntptime)
1212     *ntptime = t_current_ntp;
1213   if (rtptime)
1214     *rtptime = t_rtp;
1215   if (packet_count)
1216     *packet_count = src->stats.packets_sent;
1217   if (octet_count)
1218     *octet_count = src->stats.octets_sent;
1219
1220   return TRUE;
1221 }
1222
1223 /**
1224  * rtp_source_get_new_rb:
1225  * @src: an #RTPSource
1226  * @ntpnstime: the current time in nanoseconds since 1970
1227  * @fractionlost: fraction lost since last SR/RR
1228  * @packetslost: the cumululative number of packets lost
1229  * @exthighestseq: the extended last sequence number received
1230  * @jitter: the interarrival jitter
1231  * @lsr: the last SR packet from this source
1232  * @dlsr: the delay since last SR packet
1233  *
1234  * Get new values to put into a new report block from this source.
1235  *
1236  * Returns: %TRUE on success.
1237  */
1238 gboolean
1239 rtp_source_get_new_rb (RTPSource * src, guint64 ntpnstime,
1240     guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1241     guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1242 {
1243   RTPSourceStats *stats;
1244   guint64 extended_max, expected;
1245   guint64 expected_interval, received_interval, ntptime;
1246   gint64 lost, lost_interval;
1247   guint32 fraction, LSR, DLSR;
1248   GstClockTime sr_time;
1249
1250   stats = &src->stats;
1251
1252   extended_max = stats->cycles + stats->max_seq;
1253   expected = extended_max - stats->base_seq + 1;
1254
1255   GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1256       ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1257       extended_max, expected, stats->packets_received, stats->base_seq);
1258
1259   lost = expected - stats->packets_received;
1260   lost = CLAMP (lost, -0x800000, 0x7fffff);
1261
1262   expected_interval = expected - stats->prev_expected;
1263   stats->prev_expected = expected;
1264   received_interval = stats->packets_received - stats->prev_received;
1265   stats->prev_received = stats->packets_received;
1266
1267   lost_interval = expected_interval - received_interval;
1268
1269   if (expected_interval == 0 || lost_interval <= 0)
1270     fraction = 0;
1271   else
1272     fraction = (lost_interval << 8) / expected_interval;
1273
1274   GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1275   /* we scaled the jitter up for additional precision */
1276   GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1277       ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1278       extended_max, stats->jitter >> 4);
1279
1280   if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1281     GstClockTime diff;
1282
1283     /* LSR is middle 32 bits of the last ntptime */
1284     LSR = (ntptime >> 16) & 0xffffffff;
1285     diff = ntpnstime - sr_time;
1286     GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1287     /* DLSR, delay since last SR is expressed in 1/65536 second units */
1288     DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1289   } else {
1290     /* No valid SR received, LSR/DLSR are set to 0 then */
1291     GST_DEBUG ("no valid SR received");
1292     LSR = 0;
1293     DLSR = 0;
1294   }
1295   GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1296       DLSR >> 16, DLSR & 0xffff);
1297
1298   if (fractionlost)
1299     *fractionlost = fraction;
1300   if (packetslost)
1301     *packetslost = lost;
1302   if (exthighestseq)
1303     *exthighestseq = extended_max;
1304   if (jitter)
1305     *jitter = stats->jitter >> 4;
1306   if (lsr)
1307     *lsr = LSR;
1308   if (dlsr)
1309     *dlsr = DLSR;
1310
1311   return TRUE;
1312 }
1313
1314 /**
1315  * rtp_source_get_last_sr:
1316  * @src: an #RTPSource
1317  * @time: time of packet arrival
1318  * @ntptime: the NTP time in 32.32 fixed point
1319  * @rtptime: the RTP time
1320  * @packet_count: the packet count
1321  * @octet_count: the octect count
1322  *
1323  * Get the values of the last sender report as set with rtp_source_process_sr().
1324  *
1325  * Returns: %TRUE if there was a valid SR report.
1326  */
1327 gboolean
1328 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1329     guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1330 {
1331   RTPSenderReport *curr;
1332
1333   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1334
1335   curr = &src->stats.sr[src->stats.curr_sr];
1336   if (!curr->is_valid)
1337     return FALSE;
1338
1339   if (ntptime)
1340     *ntptime = curr->ntptime;
1341   if (rtptime)
1342     *rtptime = curr->rtptime;
1343   if (packet_count)
1344     *packet_count = curr->packet_count;
1345   if (octet_count)
1346     *octet_count = curr->octet_count;
1347   if (time)
1348     *time = curr->time;
1349
1350   return TRUE;
1351 }
1352
1353 /**
1354  * rtp_source_get_last_rb:
1355  * @src: an #RTPSource
1356  * @fractionlost: fraction lost since last SR/RR
1357  * @packetslost: the cumululative number of packets lost
1358  * @exthighestseq: the extended last sequence number received
1359  * @jitter: the interarrival jitter
1360  * @lsr: the last SR packet from this source
1361  * @dlsr: the delay since last SR packet
1362  *
1363  * Get the values of the last RB report set with rtp_source_process_rb().
1364  *
1365  * Returns: %TRUE if there was a valid SB report.
1366  */
1367 gboolean
1368 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1369     gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1370     guint32 * lsr, guint32 * dlsr)
1371 {
1372   RTPReceiverReport *curr;
1373
1374   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1375
1376   curr = &src->stats.rr[src->stats.curr_rr];
1377   if (!curr->is_valid)
1378     return FALSE;
1379
1380   if (fractionlost)
1381     *fractionlost = curr->fractionlost;
1382   if (packetslost)
1383     *packetslost = curr->packetslost;
1384   if (exthighestseq)
1385     *exthighestseq = curr->exthighestseq;
1386   if (jitter)
1387     *jitter = curr->jitter;
1388   if (lsr)
1389     *lsr = curr->lsr;
1390   if (dlsr)
1391     *dlsr = curr->dlsr;
1392
1393   return TRUE;
1394 }