Merge branch 'plugin-move-rtp-h265'
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / rtpsource.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C)  2015 Kurento (http://kurento.org/)
4  *   @author: Miguel ParĂ­s <mparisdiaz@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #include <string.h>
22
23 #include <gst/rtp/gstrtpbuffer.h>
24 #include <gst/rtp/gstrtcpbuffer.h>
25
26 #include "rtpsource.h"
27
28 GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
29 #define GST_CAT_DEFAULT rtp_source_debug
30
31 #define RTP_MAX_PROBATION_LEN  32
32
33 /* signals and args */
34 enum
35 {
36   LAST_SIGNAL
37 };
38
39 #define DEFAULT_SSRC                 0
40 #define DEFAULT_IS_CSRC              FALSE
41 #define DEFAULT_IS_VALIDATED         FALSE
42 #define DEFAULT_IS_SENDER            FALSE
43 #define DEFAULT_SDES                 NULL
44 #define DEFAULT_PROBATION            RTP_DEFAULT_PROBATION
45 #define DEFAULT_MAX_DROPOUT_TIME     60000
46 #define DEFAULT_MAX_MISORDER_TIME    2000
47
48 enum
49 {
50   PROP_0,
51   PROP_SSRC,
52   PROP_IS_CSRC,
53   PROP_IS_VALIDATED,
54   PROP_IS_SENDER,
55   PROP_SDES,
56   PROP_STATS,
57   PROP_PROBATION,
58   PROP_MAX_DROPOUT_TIME,
59   PROP_MAX_MISORDER_TIME
60 };
61
62 /* GObject vmethods */
63 static void rtp_source_finalize (GObject * object);
64 static void rtp_source_set_property (GObject * object, guint prop_id,
65     const GValue * value, GParamSpec * pspec);
66 static void rtp_source_get_property (GObject * object, guint prop_id,
67     GValue * value, GParamSpec * pspec);
68
69 /* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
70
71 G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
72
73 static void
74 rtp_source_class_init (RTPSourceClass * klass)
75 {
76   GObjectClass *gobject_class;
77
78   gobject_class = (GObjectClass *) klass;
79
80   gobject_class->finalize = rtp_source_finalize;
81
82   gobject_class->set_property = rtp_source_set_property;
83   gobject_class->get_property = rtp_source_get_property;
84
85   g_object_class_install_property (gobject_class, PROP_SSRC,
86       g_param_spec_uint ("ssrc", "SSRC",
87           "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
88           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
89
90   g_object_class_install_property (gobject_class, PROP_IS_CSRC,
91       g_param_spec_boolean ("is-csrc", "Is CSRC",
92           "If this SSRC is acting as a contributing source",
93           DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
94
95   g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
96       g_param_spec_boolean ("is-validated", "Is Validated",
97           "If this SSRC is validated", DEFAULT_IS_VALIDATED,
98           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
99
100   g_object_class_install_property (gobject_class, PROP_IS_SENDER,
101       g_param_spec_boolean ("is-sender", "Is Sender",
102           "If this SSRC is a sender", DEFAULT_IS_SENDER,
103           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
104
105   /**
106    * RTPSource::sdes
107    *
108    * The current SDES items of the source. Returns a structure with name
109    * application/x-rtp-source-sdes and may contain the following fields:
110    *
111    *  'cname'       G_TYPE_STRING  : The canonical name
112    *  'name'        G_TYPE_STRING  : The user name
113    *  'email'       G_TYPE_STRING  : The user's electronic mail address
114    *  'phone'       G_TYPE_STRING  : The user's phone number
115    *  'location'    G_TYPE_STRING  : The geographic user location
116    *  'tool'        G_TYPE_STRING  : The name of application or tool
117    *  'note'        G_TYPE_STRING  : A notice about the source
118    *
119    *  Other fields may be present and these represent private items in
120    *  the SDES where the field name is the prefix.
121    */
122   g_object_class_install_property (gobject_class, PROP_SDES,
123       g_param_spec_boxed ("sdes", "SDES",
124           "The SDES information for this source",
125           GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
126
127   /**
128    * RTPSource::stats
129    *
130    * This property returns a GstStructure named application/x-rtp-source-stats with
131    * fields useful for statistics and diagnostics.
132    *
133    * Take note of each respective field's units:
134    *
135    * - NTP times are in the appropriate 32-bit or 64-bit fixed-point format
136    *   starting from January 1, 1970 (except for timespans).
137    * - RTP times are in clock rate units (i.e. clock rate = 1 second)
138    *   starting at a random offset.
139    * - For fields indicating packet loss, note that late packets are not considered lost,
140    *   and duplicates are not taken into account. Hence, the loss may be negative
141    *   if there are duplicates.
142    *
143    * The following fields are always present.
144    *
145    *  "ssrc"         G_TYPE_UINT     the SSRC of this source
146    *  "internal"     G_TYPE_BOOLEAN  this source is a source of the session
147    *  "validated"    G_TYPE_BOOLEAN  the source is validated
148    *  "received-bye" G_TYPE_BOOLEAN  we received a BYE from this source
149    *  "is-csrc"      G_TYPE_BOOLEAN  this source was found as CSRC
150    *  "is-sender"    G_TYPE_BOOLEAN  this source is a sender
151    *  "seqnum-base"  G_TYPE_INT      first seqnum if known
152    *  "clock-rate"   G_TYPE_INT      the clock rate of the media
153    *
154    * The following fields are only present when known.
155    *
156    *  "rtp-from"     G_TYPE_STRING   where we received the last RTP packet from
157    *  "rtcp-from"    G_TYPE_STRING   where we received the last RTCP packet from
158    *
159    * The following fields make sense for internal sources and will only increase
160    * when "is-sender" is TRUE.
161    *
162    *  "octets-sent"  G_TYPE_UINT64   number of bytes we sent
163    *  "packets-sent" G_TYPE_UINT64   number of packets we sent
164    *
165    * The following fields make sense for non-internal sources and will only
166    * increase when "is-sender" is TRUE.
167    *
168    *  "octets-received"  G_TYPE_UINT64  total number of bytes received
169    *  "packets-received" G_TYPE_UINT64  total number of packets received
170    *
171    * Following fields are updated when "is-sender" is TRUE.
172    *
173    *  "bitrate"      G_TYPE_UINT64   bitrate in bits per second
174    *  "jitter"       G_TYPE_UINT     estimated jitter (in clock rate units)
175    *  "packets-lost" G_TYPE_INT      estimated amount of packets lost
176    *
177    * The last SR report this source sent. This only updates when "is-sender" is
178    * TRUE.
179    *
180    *  "have-sr"         G_TYPE_BOOLEAN  the source has sent SR
181    *  "sr-ntptime"      G_TYPE_UINT64   NTP time of SR (in NTP Timestamp Format, 32.32 fixed point)
182    *  "sr-rtptime"      G_TYPE_UINT     RTP time of SR (in clock rate units)
183    *  "sr-octet-count"  G_TYPE_UINT     the number of bytes in the SR
184    *  "sr-packet-count" G_TYPE_UINT     the number of packets in the SR
185    *
186    * The following fields are only present for non-internal sources and
187    * represent the content of the last RB packet that was sent to this source.
188    * These values are only updated when the source is sending.
189    *
190    *  "sent-rb"               G_TYPE_BOOLEAN  we have sent an RB
191    *  "sent-rb-fractionlost"  G_TYPE_UINT     calculated lost fraction
192    *  "sent-rb-packetslost"   G_TYPE_INT      lost packets
193    *  "sent-rb-exthighestseq" G_TYPE_UINT     last seen seqnum
194    *  "sent-rb-jitter"        G_TYPE_UINT     jitter (in clock rate units)
195    *  "sent-rb-lsr"           G_TYPE_UINT     last SR time (in NTP Short Format, 16.16 fixed point)
196    *  "sent-rb-dlsr"          G_TYPE_UINT     delay since last SR (in NTP Short Format, 16.16 fixed point)
197    *
198    * The following fields are only present for non-internal sources and
199    * represents the last RB that this source sent. This is only updated
200    * when the source is receiving data and sending RB blocks.
201    *
202    *  "have-rb"          G_TYPE_BOOLEAN  the source has sent RB
203    *  "rb-fractionlost"  G_TYPE_UINT     lost fraction
204    *  "rb-packetslost"   G_TYPE_INT      lost packets
205    *  "rb-exthighestseq" G_TYPE_UINT     highest received seqnum
206    *  "rb-jitter"        G_TYPE_UINT     reception jitter (in clock rate units)
207    *  "rb-lsr"           G_TYPE_UINT     last SR time (in NTP Short Format, 16.16 fixed point)
208    *  "rb-dlsr"          G_TYPE_UINT     delay since last SR (in NTP Short Format, 16.16 fixed point)
209    *
210    * The round trip of this source is calculated from the last RB
211    * values and the reception time of the last RB packet. It is only present for
212    * non-internal sources.
213    *
214    *  "rb-round-trip"    G_TYPE_UINT     the round-trip time (in NTP Short Format, 16.16 fixed point)
215    *
216    */
217   g_object_class_install_property (gobject_class, PROP_STATS,
218       g_param_spec_boxed ("stats", "Stats",
219           "The stats of this source", GST_TYPE_STRUCTURE,
220           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
221
222   g_object_class_install_property (gobject_class, PROP_PROBATION,
223       g_param_spec_uint ("probation", "Number of probations",
224           "Consecutive packet sequence numbers to accept the source",
225           0, G_MAXUINT, DEFAULT_PROBATION,
226           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227
228   g_object_class_install_property (gobject_class, PROP_MAX_DROPOUT_TIME,
229       g_param_spec_uint ("max-dropout-time", "Max dropout time",
230           "The maximum time (milliseconds) of missing packets tolerated.",
231           0, G_MAXUINT, DEFAULT_MAX_DROPOUT_TIME,
232           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
234   g_object_class_install_property (gobject_class, PROP_MAX_MISORDER_TIME,
235       g_param_spec_uint ("max-misorder-time", "Max misorder time",
236           "The maximum time (milliseconds) of misordered packets tolerated.",
237           0, G_MAXUINT, DEFAULT_MAX_MISORDER_TIME,
238           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239
240   GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
241 }
242
243 /**
244  * rtp_source_reset:
245  * @src: an #RTPSource
246  *
247  * Reset the stats of @src.
248  */
249 void
250 rtp_source_reset (RTPSource * src)
251 {
252   src->marked_bye = FALSE;
253   if (src->bye_reason)
254     g_free (src->bye_reason);
255   src->bye_reason = NULL;
256   src->sent_bye = FALSE;
257   g_hash_table_remove_all (src->reported_in_sr_of);
258
259   src->stats.cycles = -1;
260   src->stats.jitter = 0;
261   src->stats.transit = -1;
262   src->stats.curr_sr = 0;
263   src->stats.sr[0].is_valid = FALSE;
264   src->stats.curr_rr = 0;
265   src->stats.rr[0].is_valid = FALSE;
266   src->stats.prev_rtptime = GST_CLOCK_TIME_NONE;
267   src->stats.prev_rtcptime = GST_CLOCK_TIME_NONE;
268   src->stats.last_rtptime = GST_CLOCK_TIME_NONE;
269   src->stats.last_rtcptime = GST_CLOCK_TIME_NONE;
270   g_array_set_size (src->nacks, 0);
271
272   src->stats.sent_pli_count = 0;
273   src->stats.sent_fir_count = 0;
274 }
275
276 static void
277 rtp_source_init (RTPSource * src)
278 {
279   /* sources are initialy on probation until we receive enough valid RTP
280    * packets or a valid RTCP packet */
281   src->validated = FALSE;
282   src->internal = FALSE;
283   src->probation = DEFAULT_PROBATION;
284   src->curr_probation = src->probation;
285   src->closing = FALSE;
286   src->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
287   src->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
288
289   src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
290
291   src->payload = -1;
292   src->clock_rate = -1;
293   src->packets = g_queue_new ();
294   src->seqnum_offset = -1;
295   src->last_rtptime = -1;
296
297   src->retained_feedback = g_queue_new ();
298   src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
299
300   src->reported_in_sr_of = g_hash_table_new (g_direct_hash, g_direct_equal);
301
302   rtp_source_reset (src);
303 }
304
305 void
306 rtp_conflicting_address_free (RTPConflictingAddress * addr)
307 {
308   g_object_unref (addr->address);
309   g_slice_free (RTPConflictingAddress, addr);
310 }
311
312 static void
313 rtp_source_finalize (GObject * object)
314 {
315   RTPSource *src;
316
317   src = RTP_SOURCE_CAST (object);
318
319   g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
320   g_queue_free (src->packets);
321
322   gst_structure_free (src->sdes);
323
324   g_free (src->bye_reason);
325
326   gst_caps_replace (&src->caps, NULL);
327
328   g_list_free_full (src->conflicting_addresses,
329       (GDestroyNotify) rtp_conflicting_address_free);
330   g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
331   g_queue_free (src->retained_feedback);
332
333   g_array_free (src->nacks, TRUE);
334
335   if (src->rtp_from)
336     g_object_unref (src->rtp_from);
337   if (src->rtcp_from)
338     g_object_unref (src->rtcp_from);
339
340   g_hash_table_unref (src->reported_in_sr_of);
341
342   G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
343 }
344
345 static GstStructure *
346 rtp_source_create_stats (RTPSource * src)
347 {
348   GstStructure *s;
349   gboolean is_sender = src->is_sender;
350   gboolean internal = src->internal;
351   gchar *address_str;
352   gboolean have_rb;
353   guint8 fractionlost = 0;
354   gint32 packetslost = 0;
355   guint32 exthighestseq = 0;
356   guint32 jitter = 0;
357   guint32 lsr = 0;
358   guint32 dlsr = 0;
359   guint32 round_trip = 0;
360   gboolean have_sr;
361   GstClockTime time = 0;
362   guint64 ntptime = 0;
363   guint32 rtptime = 0;
364   guint32 packet_count = 0;
365   guint32 octet_count = 0;
366
367
368   /* common data for all types of sources */
369   s = gst_structure_new ("application/x-rtp-source-stats",
370       "ssrc", G_TYPE_UINT, (guint) src->ssrc,
371       "internal", G_TYPE_BOOLEAN, internal,
372       "validated", G_TYPE_BOOLEAN, src->validated,
373       "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
374       "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
375       "is-sender", G_TYPE_BOOLEAN, is_sender,
376       "seqnum-base", G_TYPE_INT, src->seqnum_offset,
377       "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
378
379   /* add address and port */
380   if (src->rtp_from) {
381     address_str = __g_socket_address_to_string (src->rtp_from);
382     gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
383     g_free (address_str);
384   }
385   if (src->rtcp_from) {
386     address_str = __g_socket_address_to_string (src->rtcp_from);
387     gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
388     g_free (address_str);
389   }
390
391   gst_structure_set (s,
392       "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
393       "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
394       "octets-received", G_TYPE_UINT64, src->stats.octets_received,
395       "packets-received", G_TYPE_UINT64, src->stats.packets_received,
396       "bitrate", G_TYPE_UINT64, src->bitrate,
397       "packets-lost", G_TYPE_INT,
398       (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
399       (guint) (src->stats.jitter >> 4),
400       "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
401       "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
402       "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
403       "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count, NULL);
404
405   /* get the last SR. */
406   have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
407       &packet_count, &octet_count);
408   gst_structure_set (s,
409       "have-sr", G_TYPE_BOOLEAN, have_sr,
410       "sr-ntptime", G_TYPE_UINT64, ntptime,
411       "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
412       "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
413       "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
414
415   if (!internal) {
416     /* get the last RB we sent */
417     gst_structure_set (s,
418         "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
419         "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
420         "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
421         "sent-rb-exthighestseq", G_TYPE_UINT,
422         (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
423         (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
424         (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
425         (guint) src->last_rr.dlsr, NULL);
426
427     /* get the last RB */
428     have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
429         &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
430
431     gst_structure_set (s,
432         "have-rb", G_TYPE_BOOLEAN, have_rb,
433         "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
434         "rb-packetslost", G_TYPE_INT, (gint) packetslost,
435         "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
436         "rb-jitter", G_TYPE_UINT, (guint) jitter,
437         "rb-lsr", G_TYPE_UINT, (guint) lsr,
438         "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
439         "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
440   }
441
442   return s;
443 }
444
445 /**
446  * rtp_source_get_sdes_struct:
447  * @src: an #RTPSource
448  *
449  * Get the SDES from @src. See the SDES property for more details.
450  *
451  * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
452  * valid until the SDES items of @src are modified.
453  */
454 const GstStructure *
455 rtp_source_get_sdes_struct (RTPSource * src)
456 {
457   g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
458
459   return src->sdes;
460 }
461
462 static gboolean
463 sdes_struct_compare_func (GQuark field_id, const GValue * value,
464     gpointer user_data)
465 {
466   GstStructure *old;
467   const gchar *field;
468
469   old = GST_STRUCTURE (user_data);
470   field = g_quark_to_string (field_id);
471
472   if (!gst_structure_has_field (old, field))
473     return FALSE;
474
475   g_assert (G_VALUE_HOLDS_STRING (value));
476
477   return strcmp (g_value_get_string (value), gst_structure_get_string (old,
478           field)) == 0;
479 }
480
481 /**
482  * rtp_source_set_sdes_struct:
483  * @src: an #RTPSource
484  * @sdes: the SDES structure
485  *
486  * Store the @sdes in @src. @sdes must be a structure of type
487  * "application/x-rtp-source-sdes", see the SDES property for more details.
488  *
489  * This function takes ownership of @sdes.
490  *
491  * Returns: %FALSE if the SDES was unchanged.
492  */
493 gboolean
494 rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
495 {
496   gboolean changed;
497
498   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
499   g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
500           "application/x-rtp-source-sdes") == 0, FALSE);
501
502   changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
503
504   if (changed) {
505     gst_structure_free (src->sdes);
506     src->sdes = sdes;
507   } else {
508     gst_structure_free (sdes);
509   }
510   return changed;
511 }
512
513 static void
514 rtp_source_set_property (GObject * object, guint prop_id,
515     const GValue * value, GParamSpec * pspec)
516 {
517   RTPSource *src;
518
519   src = RTP_SOURCE (object);
520
521   switch (prop_id) {
522     case PROP_SSRC:
523       src->ssrc = g_value_get_uint (value);
524       break;
525     case PROP_PROBATION:
526       src->probation = g_value_get_uint (value);
527       break;
528     case PROP_MAX_DROPOUT_TIME:
529       src->max_dropout_time = g_value_get_uint (value);
530       break;
531     case PROP_MAX_MISORDER_TIME:
532       src->max_misorder_time = g_value_get_uint (value);
533       break;
534     default:
535       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536       break;
537   }
538 }
539
540 static void
541 rtp_source_get_property (GObject * object, guint prop_id,
542     GValue * value, GParamSpec * pspec)
543 {
544   RTPSource *src;
545
546   src = RTP_SOURCE (object);
547
548   switch (prop_id) {
549     case PROP_SSRC:
550       g_value_set_uint (value, rtp_source_get_ssrc (src));
551       break;
552     case PROP_IS_CSRC:
553       g_value_set_boolean (value, rtp_source_is_as_csrc (src));
554       break;
555     case PROP_IS_VALIDATED:
556       g_value_set_boolean (value, rtp_source_is_validated (src));
557       break;
558     case PROP_IS_SENDER:
559       g_value_set_boolean (value, rtp_source_is_sender (src));
560       break;
561     case PROP_SDES:
562       g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
563       break;
564     case PROP_STATS:
565       g_value_take_boxed (value, rtp_source_create_stats (src));
566       break;
567     case PROP_PROBATION:
568       g_value_set_uint (value, src->probation);
569       break;
570     case PROP_MAX_DROPOUT_TIME:
571       g_value_set_uint (value, src->max_dropout_time);
572       break;
573     case PROP_MAX_MISORDER_TIME:
574       g_value_set_uint (value, src->max_misorder_time);
575       break;
576     default:
577       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
578       break;
579   }
580 }
581
582 /**
583  * rtp_source_new:
584  * @ssrc: an SSRC
585  *
586  * Create a #RTPSource with @ssrc.
587  *
588  * Returns: a new #RTPSource. Use g_object_unref() after usage.
589  */
590 RTPSource *
591 rtp_source_new (guint32 ssrc)
592 {
593   RTPSource *src;
594
595   src = g_object_new (RTP_TYPE_SOURCE, NULL);
596   src->ssrc = ssrc;
597
598   return src;
599 }
600
601 /**
602  * rtp_source_set_callbacks:
603  * @src: an #RTPSource
604  * @cb: callback functions
605  * @user_data: user data
606  *
607  * Set the callbacks for the source.
608  */
609 void
610 rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
611     gpointer user_data)
612 {
613   g_return_if_fail (RTP_IS_SOURCE (src));
614
615   src->callbacks.push_rtp = cb->push_rtp;
616   src->callbacks.clock_rate = cb->clock_rate;
617   src->user_data = user_data;
618 }
619
620 /**
621  * rtp_source_get_ssrc:
622  * @src: an #RTPSource
623  *
624  * Get the SSRC of @source.
625  *
626  * Returns: the SSRC of src.
627  */
628 guint32
629 rtp_source_get_ssrc (RTPSource * src)
630 {
631   guint32 result;
632
633   g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
634
635   result = src->ssrc;
636
637   return result;
638 }
639
640 /**
641  * rtp_source_set_as_csrc:
642  * @src: an #RTPSource
643  *
644  * Configure @src as a CSRC, this will also validate @src.
645  */
646 void
647 rtp_source_set_as_csrc (RTPSource * src)
648 {
649   g_return_if_fail (RTP_IS_SOURCE (src));
650
651   src->validated = TRUE;
652   src->is_csrc = TRUE;
653 }
654
655 /**
656  * rtp_source_is_as_csrc:
657  * @src: an #RTPSource
658  *
659  * Check if @src is a contributing source.
660  *
661  * Returns: %TRUE if @src is acting as a contributing source.
662  */
663 gboolean
664 rtp_source_is_as_csrc (RTPSource * src)
665 {
666   gboolean result;
667
668   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
669
670   result = src->is_csrc;
671
672   return result;
673 }
674
675 /**
676  * rtp_source_is_active:
677  * @src: an #RTPSource
678  *
679  * Check if @src is an active source. A source is active if it has been
680  * validated and has not yet received a BYE packet
681  *
682  * Returns: %TRUE if @src is an qactive source.
683  */
684 gboolean
685 rtp_source_is_active (RTPSource * src)
686 {
687   gboolean result;
688
689   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
690
691   result = RTP_SOURCE_IS_ACTIVE (src);
692
693   return result;
694 }
695
696 /**
697  * rtp_source_is_validated:
698  * @src: an #RTPSource
699  *
700  * Check if @src is a validated source.
701  *
702  * Returns: %TRUE if @src is a validated source.
703  */
704 gboolean
705 rtp_source_is_validated (RTPSource * src)
706 {
707   gboolean result;
708
709   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
710
711   result = src->validated;
712
713   return result;
714 }
715
716 /**
717  * rtp_source_is_sender:
718  * @src: an #RTPSource
719  *
720  * Check if @src is a sending source.
721  *
722  * Returns: %TRUE if @src is a sending source.
723  */
724 gboolean
725 rtp_source_is_sender (RTPSource * src)
726 {
727   gboolean result;
728
729   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
730
731   result = RTP_SOURCE_IS_SENDER (src);
732
733   return result;
734 }
735
736 /**
737  * rtp_source_is_marked_bye:
738  * @src: an #RTPSource
739  *
740  * Check if @src is marked as leaving the session with a BYE packet.
741  *
742  * Returns: %TRUE if @src has been marked BYE.
743  */
744 gboolean
745 rtp_source_is_marked_bye (RTPSource * src)
746 {
747   gboolean result;
748
749   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
750
751   result = RTP_SOURCE_IS_MARKED_BYE (src);
752
753   return result;
754 }
755
756
757 /**
758  * rtp_source_get_bye_reason:
759  * @src: an #RTPSource
760  *
761  * Get the BYE reason for @src. Check if the source is marked as leaving the
762  * session with a BYE message first with rtp_source_is_marked_bye().
763  *
764  * Returns: The BYE reason or NULL when no reason was given or the source was
765  * not marked BYE yet. g_free() after usage.
766  */
767 gchar *
768 rtp_source_get_bye_reason (RTPSource * src)
769 {
770   gchar *result;
771
772   g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
773
774   result = g_strdup (src->bye_reason);
775
776   return result;
777 }
778
779 /**
780  * rtp_source_update_caps:
781  * @src: an #RTPSource
782  * @caps: a #GstCaps
783  *
784  * Parse @caps and store all relevant information in @source.
785  */
786 void
787 rtp_source_update_caps (RTPSource * src, GstCaps * caps)
788 {
789   GstStructure *s;
790   guint val;
791   gint ival;
792   gboolean rtx;
793
794   /* nothing changed, return */
795   if (caps == NULL || src->caps == caps)
796     return;
797
798   s = gst_caps_get_structure (caps, 0);
799
800   rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
801
802   if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
803     src->payload = ival;
804   else
805     src->payload = -1;
806
807   GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
808
809   if (gst_structure_get_int (s, "clock-rate", &ival))
810     src->clock_rate = ival;
811   else
812     src->clock_rate = -1;
813
814   GST_DEBUG ("got clock-rate %d", src->clock_rate);
815
816   if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
817           &val))
818     src->seqnum_offset = val;
819   else
820     src->seqnum_offset = -1;
821
822   GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
823       src->seqnum_offset);
824
825   gst_caps_replace (&src->caps, caps);
826 }
827
828 /**
829  * rtp_source_set_rtp_from:
830  * @src: an #RTPSource
831  * @address: the RTP address to set
832  *
833  * Set that @src is receiving RTP packets from @address. This is used for
834  * collistion checking.
835  */
836 void
837 rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
838 {
839   g_return_if_fail (RTP_IS_SOURCE (src));
840
841   if (src->rtp_from)
842     g_object_unref (src->rtp_from);
843   src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
844 }
845
846 /**
847  * rtp_source_set_rtcp_from:
848  * @src: an #RTPSource
849  * @address: the RTCP address to set
850  *
851  * Set that @src is receiving RTCP packets from @address. This is used for
852  * collistion checking.
853  */
854 void
855 rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
856 {
857   g_return_if_fail (RTP_IS_SOURCE (src));
858
859   if (src->rtcp_from)
860     g_object_unref (src->rtcp_from);
861   src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
862 }
863
864 static GstFlowReturn
865 push_packet (RTPSource * src, GstBuffer * buffer)
866 {
867   GstFlowReturn ret = GST_FLOW_OK;
868
869   /* push queued packets first if any */
870   while (!g_queue_is_empty (src->packets)) {
871     GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
872
873     GST_LOG ("pushing queued packet");
874     if (src->callbacks.push_rtp)
875       src->callbacks.push_rtp (src, buffer, src->user_data);
876     else
877       gst_buffer_unref (buffer);
878   }
879   GST_LOG ("pushing new packet");
880   /* push packet */
881   if (src->callbacks.push_rtp)
882     ret = src->callbacks.push_rtp (src, buffer, src->user_data);
883   else
884     gst_buffer_unref (buffer);
885
886   return ret;
887 }
888
889 static gint
890 get_clock_rate (RTPSource * src, guint8 payload)
891 {
892   if (src->payload == -1) {
893     /* first payload received, nothing was in the caps, lock on to this payload */
894     src->payload = payload;
895     GST_DEBUG ("first payload %d", payload);
896   } else if (payload != src->payload) {
897     /* we have a different payload than before, reset the clock-rate */
898     GST_DEBUG ("new payload %d", payload);
899     src->payload = payload;
900     src->clock_rate = -1;
901     src->stats.transit = -1;
902   }
903
904   if (src->clock_rate == -1) {
905     gint clock_rate = -1;
906
907     if (src->callbacks.clock_rate)
908       clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
909
910     GST_DEBUG ("got clock-rate %d", clock_rate);
911
912     src->clock_rate = clock_rate;
913     gst_rtp_packet_rate_ctx_reset (&src->packet_rate_ctx, clock_rate);
914   }
915   return src->clock_rate;
916 }
917
918 /* Jitter is the variation in the delay of received packets in a flow. It is
919  * measured by comparing the interval when RTP packets were sent to the interval
920  * at which they were received. For instance, if packet #1 and packet #2 leave
921  * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
922  * milliseconds. */
923 static void
924 calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
925 {
926   GstClockTime running_time;
927   guint32 rtparrival, transit, rtptime;
928   gint32 diff;
929   gint clock_rate;
930   guint8 pt;
931
932   /* get arrival time */
933   if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
934     goto no_time;
935
936   pt = pinfo->pt;
937
938   GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
939
940   /* get clockrate */
941   if ((clock_rate = get_clock_rate (src, pt)) == -1)
942     goto no_clock_rate;
943
944   rtptime = pinfo->rtptime;
945
946   /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
947    * care about the absolute value, just the difference. */
948   rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
949
950   /* transit time is difference with RTP timestamp */
951   transit = rtparrival - rtptime;
952
953   /* get ABS diff with previous transit time */
954   if (src->stats.transit != -1) {
955     if (transit > src->stats.transit)
956       diff = transit - src->stats.transit;
957     else
958       diff = src->stats.transit - transit;
959   } else
960     diff = 0;
961
962   src->stats.transit = transit;
963
964   /* update jitter, the value we store is scaled up so we can keep precision. */
965   src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
966
967   src->stats.prev_rtptime = src->stats.last_rtptime;
968   src->stats.last_rtptime = rtparrival;
969
970   GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
971       rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
972
973   return;
974
975   /* ERRORS */
976 no_time:
977   {
978     GST_WARNING ("cannot get current running_time");
979     return;
980   }
981 no_clock_rate:
982   {
983     GST_WARNING ("cannot get clock-rate for pt %d", pt);
984     return;
985   }
986 }
987
988 static void
989 init_seq (RTPSource * src, guint16 seq)
990 {
991   src->stats.base_seq = seq;
992   src->stats.max_seq = seq;
993   src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
994   src->stats.cycles = 0;
995   src->stats.packets_received = 0;
996   src->stats.octets_received = 0;
997   src->stats.bytes_received = 0;
998   src->stats.prev_received = 0;
999   src->stats.prev_expected = 0;
1000   src->stats.recv_pli_count = 0;
1001   src->stats.recv_fir_count = 0;
1002
1003   GST_DEBUG ("base_seq %d", seq);
1004 }
1005
1006 #define BITRATE_INTERVAL (2 * GST_SECOND)
1007
1008 static void
1009 do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
1010     guint64 * bytes_handled)
1011 {
1012   guint64 elapsed;
1013
1014   if (src->prev_rtime) {
1015     elapsed = running_time - src->prev_rtime;
1016
1017     if (elapsed > BITRATE_INTERVAL) {
1018       guint64 rate;
1019
1020       rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
1021
1022       GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1023           ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1024
1025       if (src->bitrate == 0)
1026         src->bitrate = rate;
1027       else
1028         src->bitrate = ((src->bitrate * 3) + rate) / 4;
1029
1030       src->prev_rtime = running_time;
1031       *bytes_handled = 0;
1032     }
1033   } else {
1034     GST_LOG ("Reset bitrate measurement");
1035     src->prev_rtime = running_time;
1036     src->bitrate = 0;
1037   }
1038 }
1039
1040 static gboolean
1041 update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo,
1042     gboolean is_receive)
1043 {
1044   guint16 seqnr, expected;
1045   RTPSourceStats *stats;
1046   gint16 delta;
1047   gint32 packet_rate, max_dropout, max_misorder;
1048
1049   stats = &src->stats;
1050
1051   seqnr = pinfo->seqnum;
1052
1053   packet_rate =
1054       gst_rtp_packet_rate_ctx_update (&src->packet_rate_ctx, pinfo->seqnum,
1055       pinfo->rtptime);
1056   max_dropout =
1057       gst_rtp_packet_rate_ctx_get_max_dropout (&src->packet_rate_ctx,
1058       src->max_dropout_time);
1059   max_misorder =
1060       gst_rtp_packet_rate_ctx_get_max_misorder (&src->packet_rate_ctx,
1061       src->max_misorder_time);
1062   GST_TRACE ("SSRC %08x, packet_rate: %d, max_dropout: %d, max_misorder: %d",
1063       src->ssrc, packet_rate, max_dropout, max_misorder);
1064
1065   if (stats->cycles == -1) {
1066     GST_DEBUG ("received first packet");
1067     /* first time we heard of this source */
1068     init_seq (src, seqnr);
1069     src->stats.max_seq = seqnr - 1;
1070     src->curr_probation = src->probation;
1071   }
1072
1073   if (is_receive) {
1074     expected = src->stats.max_seq + 1;
1075     delta = gst_rtp_buffer_compare_seqnum (expected, seqnr);
1076
1077     /* if we are still on probation, check seqnum */
1078     if (src->curr_probation) {
1079       /* when in probation, we require consecutive seqnums */
1080       if (delta == 0) {
1081         /* expected packet */
1082         GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1083         src->curr_probation--;
1084         if (seqnr < stats->max_seq) {
1085           /* sequence number wrapped - count another 64K cycle. */
1086           stats->cycles += RTP_SEQ_MOD;
1087         }
1088         src->stats.max_seq = seqnr;
1089
1090         if (src->curr_probation == 0) {
1091           GST_DEBUG ("probation done!");
1092           init_seq (src, seqnr);
1093         } else {
1094           GstBuffer *q;
1095
1096           GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1097           /* when still in probation, keep packets in a list. */
1098           g_queue_push_tail (src->packets, pinfo->data);
1099           pinfo->data = NULL;
1100           /* remove packets from queue if there are too many */
1101           while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1102             q = g_queue_pop_head (src->packets);
1103             gst_buffer_unref (q);
1104           }
1105           goto done;
1106         }
1107       } else {
1108         /* unexpected seqnum in probation */
1109         goto probation_seqnum;
1110       }
1111     } else if (delta >= 0 && delta < max_dropout) {
1112       /* Clear bad packets */
1113       stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1114       g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1115       g_queue_clear (src->packets);
1116
1117       /* in order, with permissible gap */
1118       if (seqnr < stats->max_seq) {
1119         /* sequence number wrapped - count another 64K cycle. */
1120         stats->cycles += RTP_SEQ_MOD;
1121       }
1122       stats->max_seq = seqnr;
1123     } else if (delta < -max_misorder || delta >= max_dropout) {
1124       /* the sequence number made a very large jump */
1125       if (seqnr == stats->bad_seq && src->packets->head) {
1126         /* two sequential packets -- assume that the other side
1127          * restarted without telling us so just re-sync
1128          * (i.e., pretend this was the first packet).  */
1129         init_seq (src, seqnr);
1130       } else {
1131         /* unacceptable jump */
1132         stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1133         g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1134         g_queue_clear (src->packets);
1135         g_queue_push_tail (src->packets, pinfo->data);
1136         pinfo->data = NULL;
1137         goto bad_sequence;
1138       }
1139     } else {                    /* delta < 0 && delta >= -max_misorder */
1140       /* Clear bad packets */
1141       stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1142       g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1143       g_queue_clear (src->packets);
1144
1145       /* duplicate or reordered packet, will be filtered by jitterbuffer. */
1146       GST_WARNING ("duplicate or reordered packet (seqnr %u, expected %u)",
1147           seqnr, expected);
1148     }
1149   }
1150
1151   src->stats.octets_received += pinfo->payload_len;
1152   src->stats.bytes_received += pinfo->bytes;
1153   src->stats.packets_received++;
1154   /* for the bitrate estimation */
1155   src->bytes_received += pinfo->payload_len;
1156
1157   GST_LOG ("seq %u, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
1158       seqnr, src->stats.packets_received, src->stats.octets_received);
1159
1160   return TRUE;
1161
1162   /* ERRORS */
1163 done:
1164   {
1165     return FALSE;
1166   }
1167 bad_sequence:
1168   {
1169     GST_WARNING
1170         ("unacceptable seqnum received (seqnr %u, delta %d, packet_rate: %d, max_dropout: %d, max_misorder: %d)",
1171         seqnr, delta, packet_rate, max_dropout, max_misorder);
1172     return FALSE;
1173   }
1174 probation_seqnum:
1175   {
1176     GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1177     src->curr_probation = src->probation;
1178     src->stats.max_seq = seqnr;
1179     return FALSE;
1180   }
1181 }
1182
1183 /**
1184  * rtp_source_process_rtp:
1185  * @src: an #RTPSource
1186  * @pinfo: an #RTPPacketInfo
1187  *
1188  * Let @src handle the incomming RTP packet described in @pinfo.
1189  *
1190  * Returns: a #GstFlowReturn.
1191  */
1192 GstFlowReturn
1193 rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1194 {
1195   GstFlowReturn result;
1196
1197   g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1198   g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1199
1200   if (!update_receiver_stats (src, pinfo, TRUE))
1201     return GST_FLOW_OK;
1202
1203   /* the source that sent the packet must be a sender */
1204   src->is_sender = TRUE;
1205   src->validated = TRUE;
1206
1207   do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
1208
1209   /* calculate jitter for the stats */
1210   calculate_jitter (src, pinfo);
1211
1212   /* we're ready to push the RTP packet now */
1213   result = push_packet (src, pinfo->data);
1214   pinfo->data = NULL;
1215
1216   return result;
1217 }
1218
1219 /**
1220  * rtp_source_mark_bye:
1221  * @src: an #RTPSource
1222  * @reason: the reason for leaving
1223  *
1224  * Mark @src in the BYE state. This can happen when the source wants to
1225  * leave the sesssion or when a BYE packets has been received.
1226  *
1227  * This will make the source inactive.
1228  */
1229 void
1230 rtp_source_mark_bye (RTPSource * src, const gchar * reason)
1231 {
1232   g_return_if_fail (RTP_IS_SOURCE (src));
1233
1234   GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1235       GST_STR_NULL (reason));
1236
1237   /* copy the reason and mark as bye */
1238   g_free (src->bye_reason);
1239   src->bye_reason = g_strdup (reason);
1240   src->marked_bye = TRUE;
1241 }
1242
1243 /**
1244  * rtp_source_send_rtp:
1245  * @src: an #RTPSource
1246  * @data: an RTP buffer or a list of RTP buffers
1247  * @is_list: if @data is a buffer or list
1248  * @running_time: the running time of @data
1249  *
1250  * Send @data (an RTP buffer or list of buffers) originating from @src.
1251  * This will make @src a sender. This function takes ownership of @data and
1252  * modifies the SSRC in the RTP packet to that of @src when needed.
1253  *
1254  * Returns: a #GstFlowReturn.
1255  */
1256 GstFlowReturn
1257 rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1258 {
1259   GstFlowReturn result;
1260   GstClockTime running_time;
1261   guint32 rtptime;
1262   guint64 ext_rtptime;
1263   guint64 rt_diff, rtp_diff;
1264
1265   g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1266
1267   /* we are a sender now */
1268   src->is_sender = TRUE;
1269
1270   /* we are also a receiver of our packets */
1271   if (!update_receiver_stats (src, pinfo, FALSE))
1272     return GST_FLOW_OK;
1273
1274   /* update stats for the SR */
1275   src->stats.packets_sent += pinfo->packets;
1276   src->stats.octets_sent += pinfo->payload_len;
1277   src->bytes_sent += pinfo->payload_len;
1278
1279   running_time = pinfo->running_time;
1280
1281   do_bitrate_estimation (src, running_time, &src->bytes_sent);
1282
1283   rtptime = pinfo->rtptime;
1284
1285   ext_rtptime = src->last_rtptime;
1286   ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1287
1288   GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1289       GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
1290
1291   if (ext_rtptime > src->last_rtptime) {
1292     rtp_diff = ext_rtptime - src->last_rtptime;
1293     rt_diff = running_time - src->last_rtime;
1294
1295     /* calc the diff so we can detect drift at the sender. This can also be used
1296      * to guestimate the clock rate if the NTP time is locked to the RTP
1297      * timestamps (as is the case when the capture device is providing the clock). */
1298     GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1299         GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
1300   }
1301
1302   /* we keep track of the last received RTP timestamp and the corresponding
1303    * buffer running_time so that we can use this info when constructing SR reports */
1304   src->last_rtime = running_time;
1305   src->last_rtptime = ext_rtptime;
1306
1307   /* push packet */
1308   if (!src->callbacks.push_rtp)
1309     goto no_callback;
1310
1311   GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1312       pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
1313
1314   result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1315   pinfo->data = NULL;
1316
1317   return result;
1318
1319   /* ERRORS */
1320 no_callback:
1321   {
1322     GST_WARNING ("no callback installed, dropping packet");
1323     return GST_FLOW_OK;
1324   }
1325 }
1326
1327 /**
1328  * rtp_source_process_sr:
1329  * @src: an #RTPSource
1330  * @time: time of packet arrival
1331  * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1332  * @rtptime: the RTP time (in clock rate units)
1333  * @packet_count: the packet count
1334  * @octet_count: the octet count
1335  *
1336  * Update the sender report in @src.
1337  */
1338 void
1339 rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1340     guint32 rtptime, guint32 packet_count, guint32 octet_count)
1341 {
1342   RTPSenderReport *curr;
1343   gint curridx;
1344
1345   g_return_if_fail (RTP_IS_SOURCE (src));
1346
1347   GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1348       ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1349       (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1350       packet_count, octet_count);
1351
1352   curridx = src->stats.curr_sr ^ 1;
1353   curr = &src->stats.sr[curridx];
1354
1355   /* this is a sender now */
1356   src->is_sender = TRUE;
1357
1358   /* update current */
1359   curr->is_valid = TRUE;
1360   curr->ntptime = ntptime;
1361   curr->rtptime = rtptime;
1362   curr->packet_count = packet_count;
1363   curr->octet_count = octet_count;
1364   curr->time = time;
1365
1366   /* make current */
1367   src->stats.curr_sr = curridx;
1368
1369   src->stats.prev_rtcptime = src->stats.last_rtcptime;
1370   src->stats.last_rtcptime = time;
1371 }
1372
1373 /**
1374  * rtp_source_process_rb:
1375  * @src: an #RTPSource
1376  * @ntpnstime: the current time in nanoseconds since 1970
1377  * @fractionlost: fraction lost since last SR/RR
1378  * @packetslost: the cumulative number of packets lost
1379  * @exthighestseq: the extended last sequence number received
1380  * @jitter: the interarrival jitter (in clock rate units)
1381  * @lsr: the time of the last SR packet on this source
1382  *   (in NTP Short Format, 16.16 fixed point)
1383  * @dlsr: the delay since the last SR packet
1384  *   (in NTP Short Format, 16.16 fixed point)
1385  *
1386  * Update the report block in @src.
1387  */
1388 void
1389 rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
1390     guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1391     guint32 jitter, guint32 lsr, guint32 dlsr)
1392 {
1393   RTPReceiverReport *curr;
1394   gint curridx;
1395   guint32 ntp, A;
1396   guint64 f_ntp;
1397
1398   g_return_if_fail (RTP_IS_SOURCE (src));
1399
1400   GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1401       ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1402       src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1403       lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
1404
1405   curridx = src->stats.curr_rr ^ 1;
1406   curr = &src->stats.rr[curridx];
1407
1408   /* update current */
1409   curr->is_valid = TRUE;
1410   curr->fractionlost = fractionlost;
1411   curr->packetslost = packetslost;
1412   curr->exthighestseq = exthighestseq;
1413   curr->jitter = jitter;
1414   curr->lsr = lsr;
1415   curr->dlsr = dlsr;
1416
1417   /* convert the NTP time in nanoseconds to 32.32 fixed point */
1418   f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1419   /* calculate round trip, round the time up */
1420   ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1421
1422   A = dlsr + lsr;
1423   if (A > 0 && ntp > A)
1424     A = ntp - A;
1425   else
1426     A = 0;
1427   curr->round_trip = A;
1428
1429   GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1430       A >> 16, A & 0xffff);
1431
1432   /* make current */
1433   src->stats.curr_rr = curridx;
1434 }
1435
1436 /**
1437  * rtp_source_get_new_sr:
1438  * @src: an #RTPSource
1439  * @ntpnstime: the current time in nanoseconds since 1970
1440  * @running_time: the current running_time of the pipeline
1441  * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1442  * @rtptime: the RTP time corresponding to @ntptime (in clock rate units)
1443  * @packet_count: the packet count
1444  * @octet_count: the octet count
1445  *
1446  * Get new values to put into a new SR report from this source.
1447  *
1448  * @running_time and @ntpnstime are captured at the same time and represent the
1449  * running time of the pipeline clock and the absolute current system time in
1450  * nanoseconds respectively. Together with the last running_time and RTP timestamp
1451  * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1452  * packet. @ntptime is basically the fixed point representation of @ntpnstime
1453  * and @rtptime the associated RTP timestamp.
1454  *
1455  * Returns: %TRUE on success.
1456  */
1457 gboolean
1458 rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
1459     GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1460     guint32 * packet_count, guint32 * octet_count)
1461 {
1462   guint64 t_rtp;
1463   guint64 t_current_ntp;
1464   GstClockTimeDiff diff;
1465
1466   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1467
1468   /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1469    * and an NTP time, we can scale the RTP timestamps so that they match the
1470    * given NTP time.  for scaling, we assume that the slope of the rtptime vs
1471    * running_time vs ntptime curve is close to 1, which is certainly
1472    * sufficient for the frequency at which we report SR and the rate we send
1473    * out RTP packets. */
1474   t_rtp = src->last_rtptime;
1475
1476   GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1477       G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
1478
1479   if (src->clock_rate != -1) {
1480     /* get the diff between the clock running_time and the buffer running_time.
1481      * This is the elapsed time, as measured against the pipeline clock, between
1482      * when the rtp timestamp was observed and the current running_time.
1483      *
1484      * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1485      * for the given ntpnstime. */
1486     diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
1487     GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_STIME_FORMAT,
1488         GST_TIME_ARGS (running_time), GST_STIME_ARGS (diff));
1489
1490     /* now translate the diff to RTP time, handle positive and negative cases.
1491      * If there is no diff, we already set rtptime correctly above. */
1492     if (diff > 0) {
1493       t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1494     } else {
1495       diff = -diff;
1496       t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1497     }
1498   } else {
1499     GST_WARNING ("no clock-rate, cannot interpolate rtp time");
1500   }
1501
1502   /* convert the NTP time in nanoseconds to 32.32 fixed point */
1503   t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1504
1505   GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1506       (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
1507       (guint32) t_rtp);
1508
1509   if (ntptime)
1510     *ntptime = t_current_ntp;
1511   if (rtptime)
1512     *rtptime = t_rtp;
1513   if (packet_count)
1514     *packet_count = src->stats.packets_sent;
1515   if (octet_count)
1516     *octet_count = src->stats.octets_sent;
1517
1518   return TRUE;
1519 }
1520
1521 /**
1522  * rtp_source_get_new_rb:
1523  * @src: an #RTPSource
1524  * @time: the current time of the system clock
1525  * @fractionlost: fraction lost since last SR/RR
1526  * @packetslost: the cumulative number of packets lost
1527  * @exthighestseq: the extended last sequence number received
1528  * @jitter: the interarrival jitter (in clock rate units)
1529  * @lsr: the time of the last SR packet on this source
1530  *   (in NTP Short Format, 16.16 fixed point)
1531  * @dlsr: the delay since the last SR packet
1532  *   (in NTP Short Format, 16.16 fixed point)
1533  *
1534  * Get new values to put into a new report block from this source.
1535  *
1536  * Returns: %TRUE on success.
1537  */
1538 gboolean
1539 rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
1540     guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1541     guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1542 {
1543   RTPSourceStats *stats;
1544   guint64 extended_max, expected;
1545   guint64 expected_interval, received_interval, ntptime;
1546   gint64 lost, lost_interval;
1547   guint32 fraction, LSR, DLSR;
1548   GstClockTime sr_time;
1549
1550   stats = &src->stats;
1551
1552   extended_max = stats->cycles + stats->max_seq;
1553   expected = extended_max - stats->base_seq + 1;
1554
1555   GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1556       ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1557       extended_max, expected, stats->packets_received, stats->base_seq);
1558
1559   lost = expected - stats->packets_received;
1560   lost = CLAMP (lost, -0x800000, 0x7fffff);
1561
1562   expected_interval = expected - stats->prev_expected;
1563   stats->prev_expected = expected;
1564   received_interval = stats->packets_received - stats->prev_received;
1565   stats->prev_received = stats->packets_received;
1566
1567   lost_interval = expected_interval - received_interval;
1568
1569   if (expected_interval == 0 || lost_interval <= 0)
1570     fraction = 0;
1571   else
1572     fraction = (lost_interval << 8) / expected_interval;
1573
1574   GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1575   /* we scaled the jitter up for additional precision */
1576   GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1577       ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1578       extended_max, stats->jitter >> 4);
1579
1580   if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1581     GstClockTime diff;
1582
1583     /* LSR is middle 32 bits of the last ntptime */
1584     LSR = (ntptime >> 16) & 0xffffffff;
1585     diff = time - sr_time;
1586     GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1587     /* DLSR, delay since last SR is expressed in 1/65536 second units */
1588     DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1589   } else {
1590     /* No valid SR received, LSR/DLSR are set to 0 then */
1591     GST_DEBUG ("no valid SR received");
1592     LSR = 0;
1593     DLSR = 0;
1594   }
1595   GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1596       DLSR >> 16, DLSR & 0xffff);
1597
1598   if (fractionlost)
1599     *fractionlost = fraction;
1600   if (packetslost)
1601     *packetslost = lost;
1602   if (exthighestseq)
1603     *exthighestseq = extended_max;
1604   if (jitter)
1605     *jitter = stats->jitter >> 4;
1606   if (lsr)
1607     *lsr = LSR;
1608   if (dlsr)
1609     *dlsr = DLSR;
1610
1611   return TRUE;
1612 }
1613
1614 /**
1615  * rtp_source_get_last_sr:
1616  * @src: an #RTPSource
1617  * @time: time of packet arrival
1618  * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1619  * @rtptime: the RTP time (in clock rate units)
1620  * @packet_count: the packet count
1621  * @octet_count: the octet count
1622  *
1623  * Get the values of the last sender report as set with rtp_source_process_sr().
1624  *
1625  * Returns: %TRUE if there was a valid SR report.
1626  */
1627 gboolean
1628 rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1629     guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
1630 {
1631   RTPSenderReport *curr;
1632
1633   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1634
1635   curr = &src->stats.sr[src->stats.curr_sr];
1636   if (!curr->is_valid)
1637     return FALSE;
1638
1639   if (ntptime)
1640     *ntptime = curr->ntptime;
1641   if (rtptime)
1642     *rtptime = curr->rtptime;
1643   if (packet_count)
1644     *packet_count = curr->packet_count;
1645   if (octet_count)
1646     *octet_count = curr->octet_count;
1647   if (time)
1648     *time = curr->time;
1649
1650   return TRUE;
1651 }
1652
1653 /**
1654  * rtp_source_get_last_rb:
1655  * @src: an #RTPSource
1656  * @fractionlost: fraction lost since last SR/RR
1657  * @packetslost: the cumulative number of packets lost
1658  * @exthighestseq: the extended last sequence number received
1659  * @jitter: the interarrival jitter (in clock rate units)
1660  * @lsr: the time of the last SR packet on this source
1661  *   (in NTP Short Format, 16.16 fixed point)
1662  * @dlsr: the delay since the last SR packet
1663  *   (in NTP Short Format, 16.16 fixed point)
1664  * @round_trip: the round-trip time
1665  *   (in NTP Short Format, 16.16 fixed point)
1666  *
1667  * Get the values of the last RB report set with rtp_source_process_rb().
1668  *
1669  * Returns: %TRUE if there was a valid SB report.
1670  */
1671 gboolean
1672 rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1673     gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
1674     guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
1675 {
1676   RTPReceiverReport *curr;
1677
1678   g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1679
1680   curr = &src->stats.rr[src->stats.curr_rr];
1681   if (!curr->is_valid)
1682     return FALSE;
1683
1684   if (fractionlost)
1685     *fractionlost = curr->fractionlost;
1686   if (packetslost)
1687     *packetslost = curr->packetslost;
1688   if (exthighestseq)
1689     *exthighestseq = curr->exthighestseq;
1690   if (jitter)
1691     *jitter = curr->jitter;
1692   if (lsr)
1693     *lsr = curr->lsr;
1694   if (dlsr)
1695     *dlsr = curr->dlsr;
1696   if (round_trip)
1697     *round_trip = curr->round_trip;
1698
1699   return TRUE;
1700 }
1701
1702 gboolean
1703 find_conflicting_address (GList * conflicting_addresses,
1704     GSocketAddress * address, GstClockTime time)
1705 {
1706   GList *item;
1707
1708   for (item = conflicting_addresses; item; item = g_list_next (item)) {
1709     RTPConflictingAddress *known_conflict = item->data;
1710
1711     if (__g_socket_address_equal (address, known_conflict->address)) {
1712       known_conflict->time = time;
1713       return TRUE;
1714     }
1715   }
1716
1717   return FALSE;
1718 }
1719
1720 GList *
1721 add_conflicting_address (GList * conflicting_addresses,
1722     GSocketAddress * address, GstClockTime time)
1723 {
1724   RTPConflictingAddress *new_conflict;
1725
1726   new_conflict = g_slice_new (RTPConflictingAddress);
1727
1728   new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1729   new_conflict->time = time;
1730
1731   return g_list_prepend (conflicting_addresses, new_conflict);
1732 }
1733
1734 GList *
1735 timeout_conflicting_addresses (GList * conflicting_addresses,
1736     GstClockTime current_time)
1737 {
1738   GList *item;
1739   /* "a relatively long time" -- RFC 3550 section 8.2 */
1740   const GstClockTime collision_timeout =
1741       RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1742
1743   item = g_list_first (conflicting_addresses);
1744   while (item) {
1745     RTPConflictingAddress *known_conflict = item->data;
1746     GList *next_item = g_list_next (item);
1747
1748     if (known_conflict->time < current_time - collision_timeout) {
1749       gchar *buf;
1750
1751       conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1752       buf = __g_socket_address_to_string (known_conflict->address);
1753       GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1754       g_free (buf);
1755       rtp_conflicting_address_free (known_conflict);
1756     }
1757     item = next_item;
1758   }
1759
1760   return conflicting_addresses;
1761 }
1762
1763 /**
1764  * rtp_source_find_conflicting_address:
1765  * @src: The source the packet came in
1766  * @address: address to check for
1767  * @time: The time when the packet that is possibly in conflict arrived
1768  *
1769  * Checks if an address which has a conflict is already known. If it is
1770  * a known conflict, remember the time
1771  *
1772  * Returns: TRUE if it was a known conflict, FALSE otherwise
1773  */
1774 gboolean
1775 rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
1776     GstClockTime time)
1777 {
1778   return find_conflicting_address (src->conflicting_addresses, address, time);
1779 }
1780
1781 /**
1782  * rtp_source_add_conflicting_address:
1783  * @src: The source the packet came in
1784  * @address: address to remember
1785  * @time: The time when the packet that is in conflict arrived
1786  *
1787  * Adds a new conflict address
1788  */
1789 void
1790 rtp_source_add_conflicting_address (RTPSource * src,
1791     GSocketAddress * address, GstClockTime time)
1792 {
1793   src->conflicting_addresses =
1794       add_conflicting_address (src->conflicting_addresses, address, time);
1795 }
1796
1797 /**
1798  * rtp_source_timeout:
1799  * @src: The #RTPSource
1800  * @current_time: The current time
1801  * @feedback_retention_window: The running time before which retained feedback
1802  * packets have to be discarded
1803  *
1804  * This is processed on each RTCP interval. It times out old collisions.
1805  * It also times out old retained feedback packets
1806  */
1807 void
1808 rtp_source_timeout (RTPSource * src, GstClockTime current_time,
1809     GstClockTime feedback_retention_window)
1810 {
1811   GstRTCPPacket *pkt;
1812
1813   src->conflicting_addresses =
1814       timeout_conflicting_addresses (src->conflicting_addresses, current_time);
1815
1816   /* Time out AVPF packets that are older than the desired length */
1817   while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
1818       GST_BUFFER_PTS (pkt) < feedback_retention_window)
1819     gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1820 }
1821
1822 static gint
1823 compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1824 {
1825   const GstBuffer *bufa = a;
1826   const GstBuffer *bufb = b;
1827
1828   return GST_BUFFER_PTS (bufa) - GST_BUFFER_PTS (bufb);
1829 }
1830
1831 void
1832 rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1833     GstClockTime running_time)
1834 {
1835   GstBuffer *buffer;
1836
1837   buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1838       packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
1839
1840   GST_BUFFER_PTS (buffer) = running_time;
1841
1842   g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1843 }
1844
1845 gboolean
1846 rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1847 {
1848   if (g_queue_find_custom (src->retained_feedback, data, func))
1849     return TRUE;
1850   else
1851     return FALSE;
1852 }
1853
1854 /**
1855  * rtp_source_register_nack:
1856  * @src: The #RTPSource
1857  * @seqnum: a seqnum
1858  *
1859  * Register that @seqnum has not been received from @src.
1860  */
1861 void
1862 rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1863 {
1864   guint i, len;
1865   guint32 dword = seqnum << 16;
1866   gint diff = 16;
1867
1868   len = src->nacks->len;
1869   for (i = 0; i < len; i++) {
1870     guint32 tdword;
1871     guint16 tseq;
1872
1873     tdword = g_array_index (src->nacks, guint32, i);
1874     tseq = tdword >> 16;
1875
1876     diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1877     if (diff < 16)
1878       break;
1879   }
1880   /* we already have this seqnum */
1881   if (diff == 0)
1882     return;
1883   /* it comes before the recorded seqnum, FIXME, we could merge it
1884    * if not to far away */
1885   if (diff < 0) {
1886     GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1887     g_array_insert_val (src->nacks, i, dword);
1888   } else if (diff < 16) {
1889     /* we can merge it */
1890     dword = g_array_index (src->nacks, guint32, i);
1891     dword |= 1 << (diff - 1);
1892     GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1893         dword >> 16, dword);
1894     g_array_index (src->nacks, guint32, i) = dword;
1895   } else {
1896     GST_DEBUG ("append NACK #%u", seqnum);
1897     g_array_append_val (src->nacks, dword);
1898   }
1899   src->send_nack = TRUE;
1900 }
1901
1902 /**
1903  * rtp_source_get_nacks:
1904  * @src: The #RTPSource
1905  * @n_nacks: result number of nacks
1906  *
1907  * Get the registered NACKS since the last rtp_source_clear_nacks().
1908  *
1909  * Returns: an array of @n_nacks seqnum values.
1910  */
1911 guint32 *
1912 rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1913 {
1914   if (n_nacks)
1915     *n_nacks = src->nacks->len;
1916
1917   return (guint32 *) src->nacks->data;
1918 }
1919
1920 void
1921 rtp_source_clear_nacks (RTPSource * src)
1922 {
1923   g_array_set_size (src->nacks, 0);
1924   src->send_nack = FALSE;
1925 }