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