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