blob: d726408c69499614934ae56d55b7c956fb71eb15 [file] [log] [blame]
Wim Taymans54b3dec2007-04-18 18:58:53 +00001/* GStreamer
Wim Taymans49e501a2007-12-10 15:34:19 +00002 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
Miguel París Díazf321bfe2015-10-07 13:03:02 +02003 * Copyright (C) 2015 Kurento (http://kurento.org/)
4 * @author: Miguel París <mparisdiaz@gmail.com>
Wim Taymans54b3dec2007-04-18 18:58:53 +00005 *
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
Tim-Philipp Müller230cf412012-11-04 00:07:18 +000018 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
Wim Taymans54b3dec2007-04-18 18:58:53 +000020 */
21#include <string.h>
22
23#include <gst/rtp/gstrtpbuffer.h>
24#include <gst/rtp/gstrtcpbuffer.h>
25
26#include "rtpsource.h"
27
28GST_DEBUG_CATEGORY_STATIC (rtp_source_debug);
29#define GST_CAT_DEFAULT rtp_source_debug
30
Branko Subasic779f67a2009-06-19 19:09:19 +020031#define RTP_MAX_PROBATION_LEN 32
Wim Taymans54b3dec2007-04-18 18:58:53 +000032
33/* signals and args */
34enum
35{
36 LAST_SIGNAL
37};
38
Wim Taymans1971ae02007-12-10 11:08:11 +000039#define DEFAULT_SSRC 0
40#define DEFAULT_IS_CSRC FALSE
41#define DEFAULT_IS_VALIDATED FALSE
42#define DEFAULT_IS_SENDER FALSE
Wim Taymans55bb4d52008-11-22 15:24:47 +000043#define DEFAULT_SDES NULL
Aleix Conchillo Flaque4a200b62012-08-22 16:36:21 -070044#define DEFAULT_PROBATION RTP_DEFAULT_PROBATION
Miguel París Díaz4c960942015-10-07 13:02:12 +020045#define DEFAULT_MAX_DROPOUT_TIME 60000
46#define DEFAULT_MAX_MISORDER_TIME 2000
Wim Taymans1971ae02007-12-10 11:08:11 +000047
Wim Taymans54b3dec2007-04-18 18:58:53 +000048enum
49{
Wim Taymans1971ae02007-12-10 11:08:11 +000050 PROP_0,
51 PROP_SSRC,
52 PROP_IS_CSRC,
53 PROP_IS_VALIDATED,
54 PROP_IS_SENDER,
Wim Taymans55bb4d52008-11-22 15:24:47 +000055 PROP_SDES,
Wim Taymans2f5b1302008-11-20 18:41:34 +000056 PROP_STATS,
Miguel París Díaz4c960942015-10-07 13:02:12 +020057 PROP_PROBATION,
58 PROP_MAX_DROPOUT_TIME,
59 PROP_MAX_MISORDER_TIME
Wim Taymans54b3dec2007-04-18 18:58:53 +000060};
61
62/* GObject vmethods */
63static void rtp_source_finalize (GObject * object);
Wim Taymans1971ae02007-12-10 11:08:11 +000064static void rtp_source_set_property (GObject * object, guint prop_id,
65 const GValue * value, GParamSpec * pspec);
66static void rtp_source_get_property (GObject * object, guint prop_id,
67 GValue * value, GParamSpec * pspec);
Wim Taymans54b3dec2007-04-18 18:58:53 +000068
69/* static guint rtp_source_signals[LAST_SIGNAL] = { 0 }; */
70
71G_DEFINE_TYPE (RTPSource, rtp_source, G_TYPE_OBJECT);
72
73static void
74rtp_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
Wim Taymans1971ae02007-12-10 11:08:11 +000082 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",
Wim Taymans55bb4d52008-11-22 15:24:47 +000087 "The SSRC of this source", 0, G_MAXUINT, DEFAULT_SSRC,
Wim Taymans2f5b1302008-11-20 18:41:34 +000088 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Wim Taymans1971ae02007-12-10 11:08:11 +000089
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",
Wim Taymans2f5b1302008-11-20 18:41:34 +000093 DEFAULT_IS_CSRC, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Wim Taymans1971ae02007-12-10 11:08:11 +000094
95 g_object_class_install_property (gobject_class, PROP_IS_VALIDATED,
96 g_param_spec_boolean ("is-validated", "Is Validated",
Wim Taymans2f5b1302008-11-20 18:41:34 +000097 "If this SSRC is validated", DEFAULT_IS_VALIDATED,
98 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Wim Taymans1971ae02007-12-10 11:08:11 +000099
100 g_object_class_install_property (gobject_class, PROP_IS_SENDER,
101 g_param_spec_boolean ("is-sender", "Is Sender",
Wim Taymans2f5b1302008-11-20 18:41:34 +0000102 "If this SSRC is a sender", DEFAULT_IS_SENDER,
103 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Wim Taymans1971ae02007-12-10 11:08:11 +0000104
Wim Taymans55bb4d52008-11-22 15:24:47 +0000105 /**
106 * RTPSource::sdes
107 *
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200108 * The current SDES items of the source. Returns a structure with name
109 * application/x-rtp-source-sdes and may contain the following fields:
Wim Taymans55bb4d52008-11-22 15:24:47 +0000110 *
Nirbheek Chauhan78847d02016-03-15 03:26:14 +0530111 * 'cname' G_TYPE_STRING : The canonical name in the form user@host
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200112 * '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 *
Ilya Konstantinovc7e168e2015-06-10 14:49:50 +0300119 * Other fields may be present and these represent private items in
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200120 * the SDES where the field name is the prefix.
Wim Taymans55bb4d52008-11-22 15:24:47 +0000121 */
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));
Wim Taymans2f5b1302008-11-20 18:41:34 +0000126
127 /**
128 * RTPSource::stats
129 *
Ilya Konstantinovc7e168e2015-06-10 14:49:50 +0300130 * 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.
Wim Taymans3fcde442009-09-03 19:12:39 +0200144 *
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300145 * "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
Wim Taymans6035ee02010-12-23 13:00:49 +0100151 * "seqnum-base" G_TYPE_INT first seqnum if known
152 * "clock-rate" G_TYPE_INT the clock rate of the media
153 *
Ilya Konstantinovc7e168e2015-06-10 14:49:50 +0300154 * The following fields are only present when known.
Wim Taymans6035ee02010-12-23 13:00:49 +0100155 *
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
Ilya Konstantinovc7e168e2015-06-10 14:49:50 +0300160 * when "is-sender" is TRUE.
Wim Taymans6035ee02010-12-23 13:00:49 +0100161 *
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
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300174 * "jitter" G_TYPE_UINT estimated jitter (in clock rate units)
Wim Taymans6035ee02010-12-23 13:00:49 +0100175 * "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
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300181 * "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)
Wim Taymans6035ee02010-12-23 13:00:49 +0100183 * "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 *
Wim Taymans7caad212010-12-23 13:55:31 +0100186 * 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
Nirbheek Chauhan78847d02016-03-15 03:26:14 +0530191 * "sent-rb-fractionlost" G_TYPE_UINT calculated lost 8-bit fraction
Wim Taymans7caad212010-12-23 13:55:31 +0100192 * "sent-rb-packetslost" G_TYPE_INT lost packets
193 * "sent-rb-exthighestseq" G_TYPE_UINT last seen seqnum
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300194 * "sent-rb-jitter" G_TYPE_UINT jitter (in clock rate units)
Nirbheek Chauhan78847d02016-03-15 03:26:14 +0530195 * "sent-rb-lsr" G_TYPE_UINT last SR time (seconds in NTP Short Format, 16.16 fixed point)
196 * "sent-rb-dlsr" G_TYPE_UINT delay since last SR (seconds in NTP Short Format, 16.16 fixed point)
Wim Taymans7caad212010-12-23 13:55:31 +0100197 *
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.
Wim Taymans6035ee02010-12-23 13:00:49 +0100201 *
202 * "have-rb" G_TYPE_BOOLEAN the source has sent RB
Nirbheek Chauhan78847d02016-03-15 03:26:14 +0530203 * "rb-fractionlost" G_TYPE_UINT lost 8-bit fraction
Wim Taymans6035ee02010-12-23 13:00:49 +0100204 * "rb-packetslost" G_TYPE_INT lost packets
205 * "rb-exthighestseq" G_TYPE_UINT highest received seqnum
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300206 * "rb-jitter" G_TYPE_UINT reception jitter (in clock rate units)
Nirbheek Chauhan78847d02016-03-15 03:26:14 +0530207 * "rb-lsr" G_TYPE_UINT last SR time (seconds in NTP Short Format, 16.16 fixed point)
208 * "rb-dlsr" G_TYPE_UINT delay since last SR (seconds in NTP Short Format, 16.16 fixed point)
Wim Taymans6035ee02010-12-23 13:00:49 +0100209 *
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300210 * 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
Wim Taymans7caad212010-12-23 13:55:31 +0100212 * non-internal sources.
Wim Taymans6035ee02010-12-23 13:00:49 +0100213 *
Nirbheek Chauhan78847d02016-03-15 03:26:14 +0530214 * "rb-round-trip" G_TYPE_UINT the round-trip time (seconds in NTP Short Format, 16.16 fixed point)
Ilya Konstantinov0a578c22015-06-09 19:02:55 +0300215 *
Wim Taymans2f5b1302008-11-20 18:41:34 +0000216 */
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));
Wim Taymans1971ae02007-12-10 11:08:11 +0000221
Aleix Conchillo Flaque4a200b62012-08-22 16:36:21 -0700222 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
Miguel París Díaz4c960942015-10-07 13:02:12 +0200228 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
Wim Taymans54b3dec2007-04-18 18:58:53 +0000240 GST_DEBUG_CATEGORY_INIT (rtp_source_debug, "rtpsource", 0, "RTP Source");
241}
242
Olivier Crete3f588472008-03-11 12:40:58 +0000243/**
244 * rtp_source_reset:
245 * @src: an #RTPSource
246 *
247 * Reset the stats of @src.
248 */
249void
250rtp_source_reset (RTPSource * src)
251{
Wim Taymans1d024962013-07-25 16:49:41 +0200252 src->marked_bye = FALSE;
253 if (src->bye_reason)
254 g_free (src->bye_reason);
255 src->bye_reason = NULL;
Wim Taymans93d07292013-07-25 17:15:37 +0200256 src->sent_bye = FALSE;
George Kiagiadakis6a2de912013-11-14 16:19:29 +0200257 g_hash_table_remove_all (src->reported_in_sr_of);
Olivier Crete3f588472008-03-11 12:40:58 +0000258
259 src->stats.cycles = -1;
260 src->stats.jitter = 0;
261 src->stats.transit = -1;
262 src->stats.curr_sr = 0;
Wim Taymans0c9c1432013-07-25 17:06:22 +0200263 src->stats.sr[0].is_valid = FALSE;
Olivier Crete3f588472008-03-11 12:40:58 +0000264 src->stats.curr_rr = 0;
Wim Taymans0c9c1432013-07-25 17:06:22 +0200265 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;
Wim Taymans4379ed12013-08-05 23:22:16 +0200270 g_array_set_size (src->nacks, 0);
Santiago Carot-Nemesio22791412015-03-03 16:01:53 +0100271
272 src->stats.sent_pli_count = 0;
Santiago Carot-Nemesioe05378e2015-03-03 16:23:15 +0100273 src->stats.sent_fir_count = 0;
Santiago Carot-Nemesioa1e42492017-01-02 13:42:04 +0100274 src->stats.sent_nack_count = 0;
275 src->stats.recv_nack_count = 0;
Olivier Crete3f588472008-03-11 12:40:58 +0000276}
277
Wim Taymans54b3dec2007-04-18 18:58:53 +0000278static void
279rtp_source_init (RTPSource * src)
280{
281 /* sources are initialy on probation until we receive enough valid RTP
282 * packets or a valid RTCP packet */
283 src->validated = FALSE;
Wim Taymans2f5b1302008-11-20 18:41:34 +0000284 src->internal = FALSE;
Aleix Conchillo Flaque4a200b62012-08-22 16:36:21 -0700285 src->probation = DEFAULT_PROBATION;
286 src->curr_probation = src->probation;
Pascal Buhlerca6a5122010-09-24 15:33:40 +0200287 src->closing = FALSE;
Miguel París Díaz4c960942015-10-07 13:02:12 +0200288 src->max_dropout_time = DEFAULT_MAX_DROPOUT_TIME;
289 src->max_misorder_time = DEFAULT_MAX_MISORDER_TIME;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000290
Wim Taymans9a8a8e72011-10-29 09:09:45 +0200291 src->sdes = gst_structure_new_empty ("application/x-rtp-source-sdes");
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200292
Wim Taymans3fe87f72008-12-29 14:21:47 +0000293 src->payload = -1;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000294 src->clock_rate = -1;
295 src->packets = g_queue_new ();
Olivier Crête51a8bed2014-10-10 18:30:07 -0400296 src->seqnum_offset = -1;
Wim Taymans919deb42007-09-12 18:04:32 +0000297 src->last_rtptime = -1;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000298
Olivier Crêtedb5150a2010-06-22 13:33:32 -0400299 src->retained_feedback = g_queue_new ();
Wim Taymans4379ed12013-08-05 23:22:16 +0200300 src->nacks = g_array_new (FALSE, FALSE, sizeof (guint32));
Olivier Crêtedb5150a2010-06-22 13:33:32 -0400301
George Kiagiadakis6a2de912013-11-14 16:19:29 +0200302 src->reported_in_sr_of = g_hash_table_new (g_direct_hash, g_direct_equal);
303
Miguel París Díaz3aa69ca2017-02-02 12:55:25 +0100304 src->last_keyframe_request = GST_CLOCK_TIME_NONE;
305
Olivier Crete3f588472008-03-11 12:40:58 +0000306 rtp_source_reset (src);
Miguel París Díaz54a2f332017-03-15 18:58:55 +0100307
308 src->pt_set = FALSE;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000309}
310
Olivier Crête2e54d382014-05-03 18:30:20 -0400311void
Sebastian Drögecb789e32012-01-17 13:08:42 +0100312rtp_conflicting_address_free (RTPConflictingAddress * addr)
313{
314 g_object_unref (addr->address);
Olivier Crête2e54d382014-05-03 18:30:20 -0400315 g_slice_free (RTPConflictingAddress, addr);
Sebastian Drögecb789e32012-01-17 13:08:42 +0100316}
317
318static void
Wim Taymans54b3dec2007-04-18 18:58:53 +0000319rtp_source_finalize (GObject * object)
320{
321 RTPSource *src;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000322
323 src = RTP_SOURCE_CAST (object);
324
Sebastian Dröge9f18a272015-05-18 17:38:14 +0300325 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
Wim Taymans54b3dec2007-04-18 18:58:53 +0000326 g_queue_free (src->packets);
327
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200328 gst_structure_free (src->sdes);
Wim Taymansdf55cf22007-12-12 12:11:53 +0000329
Wim Taymans1971ae02007-12-10 11:08:11 +0000330 g_free (src->bye_reason);
331
Peter Kjellerstedtb1ef0392008-05-09 07:41:58 +0000332 gst_caps_replace (&src->caps, NULL);
333
Olivier Crête2e54d382014-05-03 18:30:20 -0400334 g_list_free_full (src->conflicting_addresses,
335 (GDestroyNotify) rtp_conflicting_address_free);
Sebastian Dröge9f18a272015-05-18 17:38:14 +0300336 g_queue_foreach (src->retained_feedback, (GFunc) gst_buffer_unref, NULL);
Olivier Crêtedb5150a2010-06-22 13:33:32 -0400337 g_queue_free (src->retained_feedback);
338
Wim Taymans4379ed12013-08-05 23:22:16 +0200339 g_array_free (src->nacks, TRUE);
340
Sebastian Drögecb789e32012-01-17 13:08:42 +0100341 if (src->rtp_from)
342 g_object_unref (src->rtp_from);
343 if (src->rtcp_from)
344 g_object_unref (src->rtcp_from);
345
George Kiagiadakis6a2de912013-11-14 16:19:29 +0200346 g_hash_table_unref (src->reported_in_sr_of);
347
Wim Taymans54b3dec2007-04-18 18:58:53 +0000348 G_OBJECT_CLASS (rtp_source_parent_class)->finalize (object);
349}
350
Wim Taymans2f5b1302008-11-20 18:41:34 +0000351static GstStructure *
352rtp_source_create_stats (RTPSource * src)
353{
354 GstStructure *s;
355 gboolean is_sender = src->is_sender;
356 gboolean internal = src->internal;
Sebastian Drögecb789e32012-01-17 13:08:42 +0100357 gchar *address_str;
Wim Taymans6035ee02010-12-23 13:00:49 +0100358 gboolean have_rb;
359 guint8 fractionlost = 0;
360 gint32 packetslost = 0;
361 guint32 exthighestseq = 0;
362 guint32 jitter = 0;
363 guint32 lsr = 0;
364 guint32 dlsr = 0;
365 guint32 round_trip = 0;
366 gboolean have_sr;
367 GstClockTime time = 0;
368 guint64 ntptime = 0;
369 guint32 rtptime = 0;
370 guint32 packet_count = 0;
371 guint32 octet_count = 0;
372
Wim Taymans2f5b1302008-11-20 18:41:34 +0000373
374 /* common data for all types of sources */
375 s = gst_structure_new ("application/x-rtp-source-stats",
376 "ssrc", G_TYPE_UINT, (guint) src->ssrc,
377 "internal", G_TYPE_BOOLEAN, internal,
378 "validated", G_TYPE_BOOLEAN, src->validated,
Wim Taymans1d024962013-07-25 16:49:41 +0200379 "received-bye", G_TYPE_BOOLEAN, src->marked_bye,
Wim Taymans2f5b1302008-11-20 18:41:34 +0000380 "is-csrc", G_TYPE_BOOLEAN, src->is_csrc,
Havard Graff0fa589a2009-08-31 18:34:08 +0200381 "is-sender", G_TYPE_BOOLEAN, is_sender,
Olivier Crête51a8bed2014-10-10 18:30:07 -0400382 "seqnum-base", G_TYPE_INT, src->seqnum_offset,
Havard Graff0fa589a2009-08-31 18:34:08 +0200383 "clock-rate", G_TYPE_INT, src->clock_rate, NULL);
Wim Taymans2f5b1302008-11-20 18:41:34 +0000384
Wim Taymans0ae6e362009-05-22 13:47:30 +0200385 /* add address and port */
Sebastian Drögecb789e32012-01-17 13:08:42 +0100386 if (src->rtp_from) {
387 address_str = __g_socket_address_to_string (src->rtp_from);
Wim Taymans0ae6e362009-05-22 13:47:30 +0200388 gst_structure_set (s, "rtp-from", G_TYPE_STRING, address_str, NULL);
Sebastian Drögecb789e32012-01-17 13:08:42 +0100389 g_free (address_str);
Wim Taymans0ae6e362009-05-22 13:47:30 +0200390 }
Sebastian Drögecb789e32012-01-17 13:08:42 +0100391 if (src->rtcp_from) {
392 address_str = __g_socket_address_to_string (src->rtcp_from);
Wim Taymans0ae6e362009-05-22 13:47:30 +0200393 gst_structure_set (s, "rtcp-from", G_TYPE_STRING, address_str, NULL);
Sebastian Drögecb789e32012-01-17 13:08:42 +0100394 g_free (address_str);
Wim Taymans0ae6e362009-05-22 13:47:30 +0200395 }
396
Wim Taymans6035ee02010-12-23 13:00:49 +0100397 gst_structure_set (s,
398 "octets-sent", G_TYPE_UINT64, src->stats.octets_sent,
399 "packets-sent", G_TYPE_UINT64, src->stats.packets_sent,
400 "octets-received", G_TYPE_UINT64, src->stats.octets_received,
401 "packets-received", G_TYPE_UINT64, src->stats.packets_received,
402 "bitrate", G_TYPE_UINT64, src->bitrate,
403 "packets-lost", G_TYPE_INT,
404 (gint) rtp_stats_get_packets_lost (&src->stats), "jitter", G_TYPE_UINT,
Santiago Carot-Nemesio22791412015-03-03 16:01:53 +0100405 (guint) (src->stats.jitter >> 4),
406 "sent-pli-count", G_TYPE_UINT, src->stats.sent_pli_count,
Santiago Carot-Nemesioe05378e2015-03-03 16:23:15 +0100407 "recv-pli-count", G_TYPE_UINT, src->stats.recv_pli_count,
408 "sent-fir-count", G_TYPE_UINT, src->stats.sent_fir_count,
Santiago Carot-Nemesioa1e42492017-01-02 13:42:04 +0100409 "recv-fir-count", G_TYPE_UINT, src->stats.recv_fir_count,
410 "sent-nack-count", G_TYPE_UINT, src->stats.sent_nack_count,
411 "recv-nack-count", G_TYPE_UINT, src->stats.recv_nack_count, NULL);
Havard Graff0fa589a2009-08-31 18:34:08 +0200412
Wim Taymans6035ee02010-12-23 13:00:49 +0100413 /* get the last SR. */
414 have_sr = rtp_source_get_last_sr (src, &time, &ntptime, &rtptime,
415 &packet_count, &octet_count);
416 gst_structure_set (s,
417 "have-sr", G_TYPE_BOOLEAN, have_sr,
418 "sr-ntptime", G_TYPE_UINT64, ntptime,
419 "sr-rtptime", G_TYPE_UINT, (guint) rtptime,
420 "sr-octet-count", G_TYPE_UINT, (guint) octet_count,
421 "sr-packet-count", G_TYPE_UINT, (guint) packet_count, NULL);
Havard Graff0fa589a2009-08-31 18:34:08 +0200422
Wim Taymans7caad212010-12-23 13:55:31 +0100423 if (!internal) {
424 /* get the last RB we sent */
425 gst_structure_set (s,
Wim Taymansb5647682010-12-27 13:11:59 +0100426 "sent-rb", G_TYPE_BOOLEAN, src->last_rr.is_valid,
Wim Taymans7caad212010-12-23 13:55:31 +0100427 "sent-rb-fractionlost", G_TYPE_UINT, (guint) src->last_rr.fractionlost,
428 "sent-rb-packetslost", G_TYPE_INT, (gint) src->last_rr.packetslost,
429 "sent-rb-exthighestseq", G_TYPE_UINT,
430 (guint) src->last_rr.exthighestseq, "sent-rb-jitter", G_TYPE_UINT,
431 (guint) src->last_rr.jitter, "sent-rb-lsr", G_TYPE_UINT,
432 (guint) src->last_rr.lsr, "sent-rb-dlsr", G_TYPE_UINT,
433 (guint) src->last_rr.dlsr, NULL);
Havard Graff0fa589a2009-08-31 18:34:08 +0200434
Wim Taymans7caad212010-12-23 13:55:31 +0100435 /* get the last RB */
436 have_rb = rtp_source_get_last_rb (src, &fractionlost, &packetslost,
437 &exthighestseq, &jitter, &lsr, &dlsr, &round_trip);
438
439 gst_structure_set (s,
440 "have-rb", G_TYPE_BOOLEAN, have_rb,
441 "rb-fractionlost", G_TYPE_UINT, (guint) fractionlost,
442 "rb-packetslost", G_TYPE_INT, (gint) packetslost,
443 "rb-exthighestseq", G_TYPE_UINT, (guint) exthighestseq,
444 "rb-jitter", G_TYPE_UINT, (guint) jitter,
445 "rb-lsr", G_TYPE_UINT, (guint) lsr,
446 "rb-dlsr", G_TYPE_UINT, (guint) dlsr,
447 "rb-round-trip", G_TYPE_UINT, (guint) round_trip, NULL);
448 }
Wim Taymans2f5b1302008-11-20 18:41:34 +0000449
450 return s;
451}
452
Wim Taymans9f330992009-06-29 16:21:05 +0200453/**
454 * rtp_source_get_sdes_struct:
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200455 * @src: an #RTPSource
Wim Taymans9f330992009-06-29 16:21:05 +0200456 *
Wim Taymans05418f12009-12-22 22:27:21 +0100457 * Get the SDES from @src. See the SDES property for more details.
Wim Taymans9f330992009-06-29 16:21:05 +0200458 *
Wim Taymans05418f12009-12-22 22:27:21 +0100459 * Returns: %GstStructure of type "application/x-rtp-source-sdes". The result is
460 * valid until the SDES items of @src are modified.
Wim Taymans9f330992009-06-29 16:21:05 +0200461 */
Wim Taymans05418f12009-12-22 22:27:21 +0100462const GstStructure *
Wim Taymans9f330992009-06-29 16:21:05 +0200463rtp_source_get_sdes_struct (RTPSource * src)
Wim Taymans55bb4d52008-11-22 15:24:47 +0000464{
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200465 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
Wim Taymans55bb4d52008-11-22 15:24:47 +0000466
Wim Taymans05418f12009-12-22 22:27:21 +0100467 return src->sdes;
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200468}
Wim Taymans55bb4d52008-11-22 15:24:47 +0000469
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200470static gboolean
471sdes_struct_compare_func (GQuark field_id, const GValue * value,
472 gpointer user_data)
473{
Wim Taymans05418f12009-12-22 22:27:21 +0100474 GstStructure *old;
475 const gchar *field;
476
477 old = GST_STRUCTURE (user_data);
478 field = g_quark_to_string (field_id);
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200479
480 if (!gst_structure_has_field (old, field))
481 return FALSE;
482
483 g_assert (G_VALUE_HOLDS_STRING (value));
Wim Taymans05418f12009-12-22 22:27:21 +0100484
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200485 return strcmp (g_value_get_string (value), gst_structure_get_string (old,
486 field)) == 0;
Wim Taymans55bb4d52008-11-22 15:24:47 +0000487}
488
Wim Taymans9f330992009-06-29 16:21:05 +0200489/**
Wim Taymans0c9c1432013-07-25 17:06:22 +0200490 * rtp_source_set_sdes_struct:
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200491 * @src: an #RTPSource
492 * @sdes: the SDES structure
Wim Taymans9f330992009-06-29 16:21:05 +0200493 *
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200494 * Store the @sdes in @src. @sdes must be a structure of type
495 * "application/x-rtp-source-sdes", see the SDES property for more details.
496 *
Wim Taymans05418f12009-12-22 22:27:21 +0100497 * This function takes ownership of @sdes.
498 *
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200499 * Returns: %FALSE if the SDES was unchanged.
Wim Taymans9f330992009-06-29 16:21:05 +0200500 */
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200501gboolean
Wim Taymans05418f12009-12-22 22:27:21 +0100502rtp_source_set_sdes_struct (RTPSource * src, GstStructure * sdes)
Wim Taymans9f330992009-06-29 16:21:05 +0200503{
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200504 gboolean changed;
Wim Taymans9f330992009-06-29 16:21:05 +0200505
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200506 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200507 g_return_val_if_fail (strcmp (gst_structure_get_name (sdes),
508 "application/x-rtp-source-sdes") == 0, FALSE);
509
510 changed = !gst_structure_foreach (sdes, sdes_struct_compare_func, src->sdes);
511
512 if (changed) {
513 gst_structure_free (src->sdes);
Wim Taymans05418f12009-12-22 22:27:21 +0100514 src->sdes = sdes;
515 } else {
516 gst_structure_free (sdes);
Wim Taymans9f330992009-06-29 16:21:05 +0200517 }
Pascal Buhlerc3448f92009-08-31 18:42:25 +0200518 return changed;
Wim Taymans9f330992009-06-29 16:21:05 +0200519}
520
Wim Taymans1971ae02007-12-10 11:08:11 +0000521static void
522rtp_source_set_property (GObject * object, guint prop_id,
523 const GValue * value, GParamSpec * pspec)
524{
525 RTPSource *src;
526
527 src = RTP_SOURCE (object);
528
529 switch (prop_id) {
Wim Taymans95d1f622007-12-10 14:03:32 +0000530 case PROP_SSRC:
531 src->ssrc = g_value_get_uint (value);
532 break;
Aleix Conchillo Flaque4a200b62012-08-22 16:36:21 -0700533 case PROP_PROBATION:
534 src->probation = g_value_get_uint (value);
535 break;
Miguel París Díaz4c960942015-10-07 13:02:12 +0200536 case PROP_MAX_DROPOUT_TIME:
537 src->max_dropout_time = g_value_get_uint (value);
538 break;
539 case PROP_MAX_MISORDER_TIME:
540 src->max_misorder_time = g_value_get_uint (value);
541 break;
Wim Taymans1971ae02007-12-10 11:08:11 +0000542 default:
543 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
544 break;
545 }
546}
547
548static void
549rtp_source_get_property (GObject * object, guint prop_id,
550 GValue * value, GParamSpec * pspec)
551{
552 RTPSource *src;
553
554 src = RTP_SOURCE (object);
555
556 switch (prop_id) {
557 case PROP_SSRC:
558 g_value_set_uint (value, rtp_source_get_ssrc (src));
559 break;
560 case PROP_IS_CSRC:
561 g_value_set_boolean (value, rtp_source_is_as_csrc (src));
562 break;
563 case PROP_IS_VALIDATED:
564 g_value_set_boolean (value, rtp_source_is_validated (src));
565 break;
566 case PROP_IS_SENDER:
567 g_value_set_boolean (value, rtp_source_is_sender (src));
568 break;
Wim Taymans55bb4d52008-11-22 15:24:47 +0000569 case PROP_SDES:
Wim Taymans05418f12009-12-22 22:27:21 +0100570 g_value_set_boxed (value, rtp_source_get_sdes_struct (src));
Wim Taymans1971ae02007-12-10 11:08:11 +0000571 break;
Wim Taymans2f5b1302008-11-20 18:41:34 +0000572 case PROP_STATS:
573 g_value_take_boxed (value, rtp_source_create_stats (src));
574 break;
Aleix Conchillo Flaque4a200b62012-08-22 16:36:21 -0700575 case PROP_PROBATION:
576 g_value_set_uint (value, src->probation);
577 break;
Miguel París Díaz4c960942015-10-07 13:02:12 +0200578 case PROP_MAX_DROPOUT_TIME:
579 g_value_set_uint (value, src->max_dropout_time);
580 break;
581 case PROP_MAX_MISORDER_TIME:
582 g_value_set_uint (value, src->max_misorder_time);
583 break;
Wim Taymans1971ae02007-12-10 11:08:11 +0000584 default:
585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586 break;
587 }
588}
589
Wim Taymans54b3dec2007-04-18 18:58:53 +0000590/**
591 * rtp_source_new:
592 * @ssrc: an SSRC
593 *
594 * Create a #RTPSource with @ssrc.
595 *
596 * Returns: a new #RTPSource. Use g_object_unref() after usage.
597 */
598RTPSource *
599rtp_source_new (guint32 ssrc)
600{
601 RTPSource *src;
602
603 src = g_object_new (RTP_TYPE_SOURCE, NULL);
604 src->ssrc = ssrc;
605
606 return src;
607}
608
609/**
Wim Taymans1971ae02007-12-10 11:08:11 +0000610 * rtp_source_set_callbacks:
611 * @src: an #RTPSource
612 * @cb: callback functions
613 * @user_data: user data
614 *
615 * Set the callbacks for the source.
616 */
617void
618rtp_source_set_callbacks (RTPSource * src, RTPSourceCallbacks * cb,
619 gpointer user_data)
620{
621 g_return_if_fail (RTP_IS_SOURCE (src));
622
623 src->callbacks.push_rtp = cb->push_rtp;
624 src->callbacks.clock_rate = cb->clock_rate;
625 src->user_data = user_data;
626}
627
628/**
629 * rtp_source_get_ssrc:
630 * @src: an #RTPSource
631 *
632 * Get the SSRC of @source.
633 *
634 * Returns: the SSRC of src.
635 */
636guint32
637rtp_source_get_ssrc (RTPSource * src)
638{
639 guint32 result;
640
641 g_return_val_if_fail (RTP_IS_SOURCE (src), 0);
642
643 result = src->ssrc;
644
645 return result;
646}
647
648/**
649 * rtp_source_set_as_csrc:
650 * @src: an #RTPSource
651 *
652 * Configure @src as a CSRC, this will also validate @src.
653 */
654void
655rtp_source_set_as_csrc (RTPSource * src)
656{
657 g_return_if_fail (RTP_IS_SOURCE (src));
658
659 src->validated = TRUE;
660 src->is_csrc = TRUE;
661}
662
663/**
664 * rtp_source_is_as_csrc:
665 * @src: an #RTPSource
666 *
667 * Check if @src is a contributing source.
668 *
669 * Returns: %TRUE if @src is acting as a contributing source.
670 */
671gboolean
672rtp_source_is_as_csrc (RTPSource * src)
673{
674 gboolean result;
675
676 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
677
678 result = src->is_csrc;
679
680 return result;
681}
682
683/**
684 * rtp_source_is_active:
685 * @src: an #RTPSource
686 *
687 * Check if @src is an active source. A source is active if it has been
688 * validated and has not yet received a BYE packet
689 *
690 * Returns: %TRUE if @src is an qactive source.
691 */
692gboolean
693rtp_source_is_active (RTPSource * src)
694{
695 gboolean result;
696
697 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
698
699 result = RTP_SOURCE_IS_ACTIVE (src);
700
701 return result;
702}
703
704/**
705 * rtp_source_is_validated:
706 * @src: an #RTPSource
707 *
708 * Check if @src is a validated source.
709 *
710 * Returns: %TRUE if @src is a validated source.
711 */
712gboolean
713rtp_source_is_validated (RTPSource * src)
714{
715 gboolean result;
716
717 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
718
719 result = src->validated;
720
721 return result;
722}
723
724/**
725 * rtp_source_is_sender:
726 * @src: an #RTPSource
727 *
728 * Check if @src is a sending source.
729 *
730 * Returns: %TRUE if @src is a sending source.
731 */
732gboolean
733rtp_source_is_sender (RTPSource * src)
734{
735 gboolean result;
736
737 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
738
739 result = RTP_SOURCE_IS_SENDER (src);
740
741 return result;
742}
743
744/**
Wim Taymans1d024962013-07-25 16:49:41 +0200745 * rtp_source_is_marked_bye:
Wim Taymans1971ae02007-12-10 11:08:11 +0000746 * @src: an #RTPSource
747 *
Wim Taymans1d024962013-07-25 16:49:41 +0200748 * Check if @src is marked as leaving the session with a BYE packet.
Wim Taymans1971ae02007-12-10 11:08:11 +0000749 *
Wim Taymans1d024962013-07-25 16:49:41 +0200750 * Returns: %TRUE if @src has been marked BYE.
Wim Taymans1971ae02007-12-10 11:08:11 +0000751 */
752gboolean
Wim Taymans1d024962013-07-25 16:49:41 +0200753rtp_source_is_marked_bye (RTPSource * src)
Wim Taymans1971ae02007-12-10 11:08:11 +0000754{
755 gboolean result;
756
757 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
758
Wim Taymans1d024962013-07-25 16:49:41 +0200759 result = RTP_SOURCE_IS_MARKED_BYE (src);
Wim Taymans1971ae02007-12-10 11:08:11 +0000760
761 return result;
762}
763
764
765/**
766 * rtp_source_get_bye_reason:
767 * @src: an #RTPSource
768 *
Wim Taymans1d024962013-07-25 16:49:41 +0200769 * Get the BYE reason for @src. Check if the source is marked as leaving the
770 * session with a BYE message first with rtp_source_is_marked_bye().
Wim Taymans1971ae02007-12-10 11:08:11 +0000771 *
Wim Taymans1d024962013-07-25 16:49:41 +0200772 * Returns: The BYE reason or NULL when no reason was given or the source was
773 * not marked BYE yet. g_free() after usage.
Wim Taymans1971ae02007-12-10 11:08:11 +0000774 */
775gchar *
776rtp_source_get_bye_reason (RTPSource * src)
777{
778 gchar *result;
779
780 g_return_val_if_fail (RTP_IS_SOURCE (src), NULL);
781
782 result = g_strdup (src->bye_reason);
783
784 return result;
785}
786
787/**
Wim Taymanse7b62122007-09-03 21:19:34 +0000788 * rtp_source_update_caps:
789 * @src: an #RTPSource
790 * @caps: a #GstCaps
791 *
792 * Parse @caps and store all relevant information in @source.
793 */
794void
795rtp_source_update_caps (RTPSource * src, GstCaps * caps)
796{
797 GstStructure *s;
798 guint val;
799 gint ival;
Sebastian Dröge80268e72015-04-16 15:31:25 +0200800 gboolean rtx;
Wim Taymanse7b62122007-09-03 21:19:34 +0000801
802 /* nothing changed, return */
Wim Taymans9f683032009-07-28 18:18:20 +0200803 if (caps == NULL || src->caps == caps)
Wim Taymanse7b62122007-09-03 21:19:34 +0000804 return;
805
806 s = gst_caps_get_structure (caps, 0);
807
Sebastian Dröge80268e72015-04-16 15:31:25 +0200808 rtx = (gst_structure_get_uint (s, "rtx-ssrc", &val) && val == src->ssrc);
809
810 if (gst_structure_get_int (s, rtx ? "rtx-payload" : "payload", &ival))
Wim Taymanse7b62122007-09-03 21:19:34 +0000811 src->payload = ival;
Wim Taymans3fe87f72008-12-29 14:21:47 +0000812 else
813 src->payload = -1;
Sebastian Dröge80268e72015-04-16 15:31:25 +0200814
815 GST_DEBUG ("got %spayload %d", rtx ? "rtx " : "", src->payload);
Wim Taymanse7b62122007-09-03 21:19:34 +0000816
Wim Taymans3fe87f72008-12-29 14:21:47 +0000817 if (gst_structure_get_int (s, "clock-rate", &ival))
818 src->clock_rate = ival;
819 else
820 src->clock_rate = -1;
821
Wim Taymanse7b62122007-09-03 21:19:34 +0000822 GST_DEBUG ("got clock-rate %d", src->clock_rate);
823
Sebastian Dröge80268e72015-04-16 15:31:25 +0200824 if (gst_structure_get_uint (s, rtx ? "rtx-seqnum-offset" : "seqnum-offset",
825 &val))
Olivier Crête51a8bed2014-10-10 18:30:07 -0400826 src->seqnum_offset = val;
Wim Taymans3fe87f72008-12-29 14:21:47 +0000827 else
Olivier Crête51a8bed2014-10-10 18:30:07 -0400828 src->seqnum_offset = -1;
Wim Taymans3fe87f72008-12-29 14:21:47 +0000829
Sebastian Dröge80268e72015-04-16 15:31:25 +0200830 GST_DEBUG ("got %sseqnum-offset %" G_GINT32_FORMAT, rtx ? "rtx " : "",
831 src->seqnum_offset);
Wim Taymanse7b62122007-09-03 21:19:34 +0000832
833 gst_caps_replace (&src->caps, caps);
834}
835
836/**
Wim Taymans54b3dec2007-04-18 18:58:53 +0000837 * rtp_source_set_rtp_from:
838 * @src: an #RTPSource
839 * @address: the RTP address to set
840 *
841 * Set that @src is receiving RTP packets from @address. This is used for
842 * collistion checking.
843 */
844void
Sebastian Drögecb789e32012-01-17 13:08:42 +0100845rtp_source_set_rtp_from (RTPSource * src, GSocketAddress * address)
Wim Taymans54b3dec2007-04-18 18:58:53 +0000846{
847 g_return_if_fail (RTP_IS_SOURCE (src));
848
Sebastian Drögecb789e32012-01-17 13:08:42 +0100849 if (src->rtp_from)
850 g_object_unref (src->rtp_from);
851 src->rtp_from = G_SOCKET_ADDRESS (g_object_ref (address));
Wim Taymans54b3dec2007-04-18 18:58:53 +0000852}
853
854/**
855 * rtp_source_set_rtcp_from:
856 * @src: an #RTPSource
857 * @address: the RTCP address to set
858 *
859 * Set that @src is receiving RTCP packets from @address. This is used for
860 * collistion checking.
861 */
862void
Sebastian Drögecb789e32012-01-17 13:08:42 +0100863rtp_source_set_rtcp_from (RTPSource * src, GSocketAddress * address)
Wim Taymans54b3dec2007-04-18 18:58:53 +0000864{
865 g_return_if_fail (RTP_IS_SOURCE (src));
866
Sebastian Drögecb789e32012-01-17 13:08:42 +0100867 if (src->rtcp_from)
868 g_object_unref (src->rtcp_from);
869 src->rtcp_from = G_SOCKET_ADDRESS (g_object_ref (address));
Wim Taymans54b3dec2007-04-18 18:58:53 +0000870}
871
872static GstFlowReturn
873push_packet (RTPSource * src, GstBuffer * buffer)
874{
875 GstFlowReturn ret = GST_FLOW_OK;
876
877 /* push queued packets first if any */
878 while (!g_queue_is_empty (src->packets)) {
879 GstBuffer *buffer = GST_BUFFER_CAST (g_queue_pop_head (src->packets));
880
Peter Kjellerstedte2f49d92008-07-03 14:31:10 +0000881 GST_LOG ("pushing queued packet");
Wim Taymans54b3dec2007-04-18 18:58:53 +0000882 if (src->callbacks.push_rtp)
883 src->callbacks.push_rtp (src, buffer, src->user_data);
884 else
885 gst_buffer_unref (buffer);
886 }
Peter Kjellerstedte2f49d92008-07-03 14:31:10 +0000887 GST_LOG ("pushing new packet");
Wim Taymans54b3dec2007-04-18 18:58:53 +0000888 /* push packet */
889 if (src->callbacks.push_rtp)
890 ret = src->callbacks.push_rtp (src, buffer, src->user_data);
891 else
892 gst_buffer_unref (buffer);
893
894 return ret;
895}
896
897static gint
898get_clock_rate (RTPSource * src, guint8 payload)
899{
Wim Taymans3fe87f72008-12-29 14:21:47 +0000900 if (src->payload == -1) {
901 /* first payload received, nothing was in the caps, lock on to this payload */
902 src->payload = payload;
903 GST_DEBUG ("first payload %d", payload);
904 } else if (payload != src->payload) {
905 /* we have a different payload than before, reset the clock-rate */
Wim Taymans5ab3e102008-11-17 15:17:52 +0000906 GST_DEBUG ("new payload %d", payload);
907 src->payload = payload;
908 src->clock_rate = -1;
909 src->stats.transit = -1;
910 }
911
Wim Taymanse7b62122007-09-03 21:19:34 +0000912 if (src->clock_rate == -1) {
Wim Taymans54b3dec2007-04-18 18:58:53 +0000913 gint clock_rate = -1;
914
915 if (src->callbacks.clock_rate)
916 clock_rate = src->callbacks.clock_rate (src, payload, src->user_data);
917
Sebastian Drögec3645232008-11-17 19:47:32 +0000918 GST_DEBUG ("got clock-rate %d", clock_rate);
Wim Taymans54b3dec2007-04-18 18:58:53 +0000919
920 src->clock_rate = clock_rate;
Miguel París Díazf321bfe2015-10-07 13:03:02 +0200921 gst_rtp_packet_rate_ctx_reset (&src->packet_rate_ctx, clock_rate);
Wim Taymans54b3dec2007-04-18 18:58:53 +0000922 }
923 return src->clock_rate;
924}
925
Wim Taymansb2aa36c2007-09-16 19:40:31 +0000926/* Jitter is the variation in the delay of received packets in a flow. It is
927 * measured by comparing the interval when RTP packets were sent to the interval
928 * at which they were received. For instance, if packet #1 and packet #2 leave
929 * 50 milliseconds apart and arrive 60 milliseconds apart, then the jitter is 10
930 * milliseconds. */
Wim Taymans54b3dec2007-04-18 18:58:53 +0000931static void
Wim Taymansa02c9472013-09-13 12:22:36 +0200932calculate_jitter (RTPSource * src, RTPPacketInfo * pinfo)
Wim Taymans54b3dec2007-04-18 18:58:53 +0000933{
Wim Taymans74241e52010-01-20 18:19:34 +0100934 GstClockTime running_time;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000935 guint32 rtparrival, transit, rtptime;
936 gint32 diff;
937 gint clock_rate;
938 guint8 pt;
939
940 /* get arrival time */
Wim Taymans47662f92013-09-13 11:08:55 +0200941 if ((running_time = pinfo->running_time) == GST_CLOCK_TIME_NONE)
Wim Taymans54b3dec2007-04-18 18:58:53 +0000942 goto no_time;
943
Wim Taymansa02c9472013-09-13 12:22:36 +0200944 pt = pinfo->pt;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000945
Peter Kjellerstedte2f49d92008-07-03 14:31:10 +0000946 GST_LOG ("SSRC %08x got payload %d", src->ssrc, pt);
Wim Taymans56d58322007-09-17 02:01:41 +0000947
Wim Taymans54b3dec2007-04-18 18:58:53 +0000948 /* get clockrate */
Wim Taymansa02c9472013-09-13 12:22:36 +0200949 if ((clock_rate = get_clock_rate (src, pt)) == -1)
Wim Taymans54b3dec2007-04-18 18:58:53 +0000950 goto no_clock_rate;
951
Wim Taymansa02c9472013-09-13 12:22:36 +0200952 rtptime = pinfo->rtptime;
Wim Taymans54b3dec2007-04-18 18:58:53 +0000953
Wim Taymanse7b62122007-09-03 21:19:34 +0000954 /* convert arrival time to RTP timestamp units, truncate to 32 bits, we don't
955 * care about the absolute value, just the difference. */
Wim Taymans74241e52010-01-20 18:19:34 +0100956 rtparrival = gst_util_uint64_scale_int (running_time, clock_rate, GST_SECOND);
Wim Taymans54b3dec2007-04-18 18:58:53 +0000957
958 /* transit time is difference with RTP timestamp */
959 transit = rtparrival - rtptime;
Wim Taymansae536e02007-04-25 13:19:36 +0000960
961 /* get ABS diff with previous transit time */
962 if (src->stats.transit != -1) {
963 if (transit > src->stats.transit)
964 diff = transit - src->stats.transit;
965 else
966 diff = src->stats.transit - transit;
967 } else
Wim Taymans54b3dec2007-04-18 18:58:53 +0000968 diff = 0;
Wim Taymansae536e02007-04-25 13:19:36 +0000969
Wim Taymans54b3dec2007-04-18 18:58:53 +0000970 src->stats.transit = transit;
Wim Taymansae536e02007-04-25 13:19:36 +0000971
972 /* update jitter, the value we store is scaled up so we can keep precision. */
Wim Taymans54b3dec2007-04-18 18:58:53 +0000973 src->stats.jitter += diff - ((src->stats.jitter + 8) >> 4);
974
975 src->stats.prev_rtptime = src->stats.last_rtptime;
976 src->stats.last_rtptime = rtparrival;
977
Peter Kjellerstedte2f49d92008-07-03 14:31:10 +0000978 GST_LOG ("rtparrival %u, rtptime %u, clock-rate %d, diff %d, jitter: %f",
Wim Taymanseb868652007-08-28 20:30:16 +0000979 rtparrival, rtptime, clock_rate, diff, (src->stats.jitter) / 16.0);
Wim Taymans54b3dec2007-04-18 18:58:53 +0000980
981 return;
982
983 /* ERRORS */
984no_time:
985 {
Wim Taymans74241e52010-01-20 18:19:34 +0100986 GST_WARNING ("cannot get current running_time");
Wim Taymans54b3dec2007-04-18 18:58:53 +0000987 return;
988 }
989no_clock_rate:
990 {
991 GST_WARNING ("cannot get clock-rate for pt %d", pt);
992 return;
993 }
994}
995
Wim Taymans23883be2007-04-25 08:30:48 +0000996static void
997init_seq (RTPSource * src, guint16 seq)
998{
999 src->stats.base_seq = seq;
1000 src->stats.max_seq = seq;
1001 src->stats.bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1002 src->stats.cycles = 0;
1003 src->stats.packets_received = 0;
1004 src->stats.octets_received = 0;
1005 src->stats.bytes_received = 0;
1006 src->stats.prev_received = 0;
1007 src->stats.prev_expected = 0;
Santiago Carot-Nemesio22791412015-03-03 16:01:53 +01001008 src->stats.recv_pli_count = 0;
Santiago Carot-Nemesioe05378e2015-03-03 16:23:15 +01001009 src->stats.recv_fir_count = 0;
Wim Taymansae536e02007-04-25 13:19:36 +00001010
1011 GST_DEBUG ("base_seq %d", seq);
Wim Taymans23883be2007-04-25 08:30:48 +00001012}
1013
Wim Taymansc5203a42010-09-24 13:48:50 +02001014#define BITRATE_INTERVAL (2 * GST_SECOND)
1015
Wim Taymansc971d1a2010-03-02 12:39:20 +01001016static void
1017do_bitrate_estimation (RTPSource * src, GstClockTime running_time,
1018 guint64 * bytes_handled)
1019{
1020 guint64 elapsed;
1021
1022 if (src->prev_rtime) {
1023 elapsed = running_time - src->prev_rtime;
1024
Wim Taymansc5203a42010-09-24 13:48:50 +02001025 if (elapsed > BITRATE_INTERVAL) {
Wim Taymansc971d1a2010-03-02 12:39:20 +01001026 guint64 rate;
1027
Wim Taymansc5203a42010-09-24 13:48:50 +02001028 rate = gst_util_uint64_scale (*bytes_handled, 8 * GST_SECOND, elapsed);
Wim Taymansc971d1a2010-03-02 12:39:20 +01001029
1030 GST_LOG ("Elapsed %" G_GUINT64_FORMAT ", bytes %" G_GUINT64_FORMAT
1031 ", rate %" G_GUINT64_FORMAT, elapsed, *bytes_handled, rate);
1032
1033 if (src->bitrate == 0)
1034 src->bitrate = rate;
1035 else
1036 src->bitrate = ((src->bitrate * 3) + rate) / 4;
1037
1038 src->prev_rtime = running_time;
1039 *bytes_handled = 0;
1040 }
1041 } else {
1042 GST_LOG ("Reset bitrate measurement");
1043 src->prev_rtime = running_time;
1044 src->bitrate = 0;
1045 }
1046}
1047
Wim Taymans268a75e2013-11-07 16:13:16 +01001048static gboolean
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001049update_receiver_stats (RTPSource * src, RTPPacketInfo * pinfo,
1050 gboolean is_receive)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001051{
Sebastian Dröge3386de72015-05-18 17:07:23 +03001052 guint16 seqnr, expected;
Wim Taymans23883be2007-04-25 08:30:48 +00001053 RTPSourceStats *stats;
Sebastian Dröge3386de72015-05-18 17:07:23 +03001054 gint16 delta;
Miguel París Díazf321bfe2015-10-07 13:03:02 +02001055 gint32 packet_rate, max_dropout, max_misorder;
Wim Taymans54b3dec2007-04-18 18:58:53 +00001056
Wim Taymans23883be2007-04-25 08:30:48 +00001057 stats = &src->stats;
1058
Wim Taymansa02c9472013-09-13 12:22:36 +02001059 seqnr = pinfo->seqnum;
Wim Taymans23883be2007-04-25 08:30:48 +00001060
Miguel París Díazf321bfe2015-10-07 13:03:02 +02001061 packet_rate =
1062 gst_rtp_packet_rate_ctx_update (&src->packet_rate_ctx, pinfo->seqnum,
1063 pinfo->rtptime);
1064 max_dropout =
1065 gst_rtp_packet_rate_ctx_get_max_dropout (&src->packet_rate_ctx,
1066 src->max_dropout_time);
1067 max_misorder =
1068 gst_rtp_packet_rate_ctx_get_max_misorder (&src->packet_rate_ctx,
1069 src->max_misorder_time);
1070 GST_TRACE ("SSRC %08x, packet_rate: %d, max_dropout: %d, max_misorder: %d",
1071 src->ssrc, packet_rate, max_dropout, max_misorder);
1072
Wim Taymans23883be2007-04-25 08:30:48 +00001073 if (stats->cycles == -1) {
Wim Taymansa02c9472013-09-13 12:22:36 +02001074 GST_DEBUG ("received first packet");
Wim Taymans23883be2007-04-25 08:30:48 +00001075 /* first time we heard of this source */
1076 init_seq (src, seqnr);
1077 src->stats.max_seq = seqnr - 1;
Aleix Conchillo Flaque4a200b62012-08-22 16:36:21 -07001078 src->curr_probation = src->probation;
Wim Taymans23883be2007-04-25 08:30:48 +00001079 }
1080
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001081 if (is_receive) {
1082 expected = src->stats.max_seq + 1;
1083 delta = gst_rtp_buffer_compare_seqnum (expected, seqnr);
Wim Taymans23883be2007-04-25 08:30:48 +00001084
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001085 /* if we are still on probation, check seqnum */
1086 if (src->curr_probation) {
1087 /* when in probation, we require consecutive seqnums */
1088 if (delta == 0) {
1089 /* expected packet */
1090 GST_DEBUG ("probation: seqnr %d == expected %d", seqnr, expected);
1091 src->curr_probation--;
1092 if (seqnr < stats->max_seq) {
1093 /* sequence number wrapped - count another 64K cycle. */
1094 stats->cycles += RTP_SEQ_MOD;
1095 }
1096 src->stats.max_seq = seqnr;
1097
1098 if (src->curr_probation == 0) {
1099 GST_DEBUG ("probation done!");
1100 init_seq (src, seqnr);
1101 } else {
1102 GstBuffer *q;
1103
1104 GST_DEBUG ("probation %d: queue packet", src->curr_probation);
1105 /* when still in probation, keep packets in a list. */
1106 g_queue_push_tail (src->packets, pinfo->data);
1107 pinfo->data = NULL;
1108 /* remove packets from queue if there are too many */
1109 while (g_queue_get_length (src->packets) > RTP_MAX_PROBATION_LEN) {
1110 q = g_queue_pop_head (src->packets);
1111 gst_buffer_unref (q);
1112 }
1113 goto done;
1114 }
1115 } else {
1116 /* unexpected seqnum in probation */
1117 goto probation_seqnum;
1118 }
Miguel París Díazf321bfe2015-10-07 13:03:02 +02001119 } else if (delta >= 0 && delta < max_dropout) {
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001120 /* Clear bad packets */
1121 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
1122 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1123 g_queue_clear (src->packets);
1124
1125 /* in order, with permissible gap */
Sebastian Dröge1974b242015-05-18 17:08:53 +03001126 if (seqnr < stats->max_seq) {
1127 /* sequence number wrapped - count another 64K cycle. */
1128 stats->cycles += RTP_SEQ_MOD;
1129 }
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001130 stats->max_seq = seqnr;
Miguel París Díazf321bfe2015-10-07 13:03:02 +02001131 } else if (delta < -max_misorder || delta >= max_dropout) {
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001132 /* the sequence number made a very large jump */
1133 if (seqnr == stats->bad_seq && src->packets->head) {
1134 /* two sequential packets -- assume that the other side
1135 * restarted without telling us so just re-sync
1136 * (i.e., pretend this was the first packet). */
Wim Taymans23883be2007-04-25 08:30:48 +00001137 init_seq (src, seqnr);
1138 } else {
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001139 /* unacceptable jump */
1140 stats->bad_seq = (seqnr + 1) & (RTP_SEQ_MOD - 1);
1141 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1142 g_queue_clear (src->packets);
Wim Taymansa02c9472013-09-13 12:22:36 +02001143 g_queue_push_tail (src->packets, pinfo->data);
1144 pinfo->data = NULL;
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001145 goto bad_sequence;
Wim Taymans23883be2007-04-25 08:30:48 +00001146 }
Miguel París Díazf321bfe2015-10-07 13:03:02 +02001147 } else { /* delta < 0 && delta >= -max_misorder */
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001148 /* Clear bad packets */
1149 stats->bad_seq = RTP_SEQ_MOD + 1; /* so seq == bad_seq is false */
Sebastian Drögec60038f2015-05-18 17:38:31 +03001150 g_queue_foreach (src->packets, (GFunc) gst_buffer_unref, NULL);
1151 g_queue_clear (src->packets);
Sebastian Drögec60038f2015-05-18 17:38:31 +03001152
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001153 /* duplicate or reordered packet, will be filtered by jitterbuffer. */
Havard Graffb33470f2016-03-31 00:10:49 +02001154 GST_INFO ("duplicate or reordered packet (seqnr %u, expected %u)",
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001155 seqnr, expected);
1156 }
Wim Taymans54b3dec2007-04-18 18:58:53 +00001157 }
Wim Taymans23883be2007-04-25 08:30:48 +00001158
Wim Taymans47662f92013-09-13 11:08:55 +02001159 src->stats.octets_received += pinfo->payload_len;
1160 src->stats.bytes_received += pinfo->bytes;
Wim Taymans23883be2007-04-25 08:30:48 +00001161 src->stats.packets_received++;
Wim Taymansc971d1a2010-03-02 12:39:20 +01001162 /* for the bitrate estimation */
Wim Taymans47662f92013-09-13 11:08:55 +02001163 src->bytes_received += pinfo->payload_len;
Wim Taymans268a75e2013-11-07 16:13:16 +01001164
Sebastian Dröge54e92432015-05-18 17:19:31 +03001165 GST_LOG ("seq %u, PC: %" G_GUINT64_FORMAT ", OC: %" G_GUINT64_FORMAT,
Wim Taymans268a75e2013-11-07 16:13:16 +01001166 seqnr, src->stats.packets_received, src->stats.octets_received);
1167
1168 return TRUE;
1169
1170 /* ERRORS */
1171done:
1172 {
1173 return FALSE;
1174 }
1175bad_sequence:
1176 {
Miguel París Díazf321bfe2015-10-07 13:03:02 +02001177 GST_WARNING
1178 ("unacceptable seqnum received (seqnr %u, delta %d, packet_rate: %d, max_dropout: %d, max_misorder: %d)",
1179 seqnr, delta, packet_rate, max_dropout, max_misorder);
Wim Taymans268a75e2013-11-07 16:13:16 +01001180 return FALSE;
1181 }
1182probation_seqnum:
1183 {
1184 GST_WARNING ("probation: seqnr %d != expected %d", seqnr, expected);
1185 src->curr_probation = src->probation;
1186 src->stats.max_seq = seqnr;
1187 return FALSE;
1188 }
1189}
1190
1191/**
1192 * rtp_source_process_rtp:
1193 * @src: an #RTPSource
1194 * @pinfo: an #RTPPacketInfo
1195 *
1196 * Let @src handle the incomming RTP packet described in @pinfo.
1197 *
1198 * Returns: a #GstFlowReturn.
1199 */
1200GstFlowReturn
1201rtp_source_process_rtp (RTPSource * src, RTPPacketInfo * pinfo)
1202{
1203 GstFlowReturn result;
1204
1205 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
1206 g_return_val_if_fail (pinfo != NULL, GST_FLOW_ERROR);
1207
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001208 if (!update_receiver_stats (src, pinfo, TRUE))
Wim Taymans268a75e2013-11-07 16:13:16 +01001209 return GST_FLOW_OK;
1210
Wim Taymans23883be2007-04-25 08:30:48 +00001211 /* the source that sent the packet must be a sender */
1212 src->is_sender = TRUE;
1213 src->validated = TRUE;
1214
Wim Taymans47662f92013-09-13 11:08:55 +02001215 do_bitrate_estimation (src, pinfo->running_time, &src->bytes_received);
Tristan Matthewsa0a6d4f2010-03-01 16:40:27 -05001216
Wim Taymansbd1e0eb2008-05-26 10:09:29 +00001217 /* calculate jitter for the stats */
Wim Taymansa02c9472013-09-13 12:22:36 +02001218 calculate_jitter (src, pinfo);
Wim Taymans23883be2007-04-25 08:30:48 +00001219
1220 /* we're ready to push the RTP packet now */
Wim Taymansa02c9472013-09-13 12:22:36 +02001221 result = push_packet (src, pinfo->data);
1222 pinfo->data = NULL;
Wim Taymans23883be2007-04-25 08:30:48 +00001223
Wim Taymans54b3dec2007-04-18 18:58:53 +00001224 return result;
1225}
1226
1227/**
Wim Taymans1d024962013-07-25 16:49:41 +02001228 * rtp_source_mark_bye:
Wim Taymans54b3dec2007-04-18 18:58:53 +00001229 * @src: an #RTPSource
1230 * @reason: the reason for leaving
1231 *
Wim Taymans1d024962013-07-25 16:49:41 +02001232 * Mark @src in the BYE state. This can happen when the source wants to
1233 * leave the sesssion or when a BYE packets has been received.
1234 *
1235 * This will make the source inactive.
Wim Taymans54b3dec2007-04-18 18:58:53 +00001236 */
1237void
Wim Taymans1d024962013-07-25 16:49:41 +02001238rtp_source_mark_bye (RTPSource * src, const gchar * reason)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001239{
1240 g_return_if_fail (RTP_IS_SOURCE (src));
1241
1242 GST_DEBUG ("marking SSRC %08x as BYE, reason: %s", src->ssrc,
1243 GST_STR_NULL (reason));
1244
Wim Taymans1d024962013-07-25 16:49:41 +02001245 /* copy the reason and mark as bye */
Wim Taymans54b3dec2007-04-18 18:58:53 +00001246 g_free (src->bye_reason);
1247 src->bye_reason = g_strdup (reason);
Wim Taymans1d024962013-07-25 16:49:41 +02001248 src->marked_bye = TRUE;
Wim Taymans54b3dec2007-04-18 18:58:53 +00001249}
1250
1251/**
1252 * rtp_source_send_rtp:
1253 * @src: an #RTPSource
Branko Subasic779f67a2009-06-19 19:09:19 +02001254 * @data: an RTP buffer or a list of RTP buffers
1255 * @is_list: if @data is a buffer or list
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001256 * @running_time: the running time of @data
Wim Taymans54b3dec2007-04-18 18:58:53 +00001257 *
Branko Subasic779f67a2009-06-19 19:09:19 +02001258 * Send @data (an RTP buffer or list of buffers) originating from @src.
1259 * This will make @src a sender. This function takes ownership of @data and
1260 * modifies the SSRC in the RTP packet to that of @src when needed.
Wim Taymans54b3dec2007-04-18 18:58:53 +00001261 *
1262 * Returns: a #GstFlowReturn.
1263 */
1264GstFlowReturn
Wim Taymans28e5f902013-09-13 12:40:41 +02001265rtp_source_send_rtp (RTPSource * src, RTPPacketInfo * pinfo)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001266{
Branko Subasic779f67a2009-06-19 19:09:19 +02001267 GstFlowReturn result;
Wim Taymans28e5f902013-09-13 12:40:41 +02001268 GstClockTime running_time;
Wim Taymans919deb42007-09-12 18:04:32 +00001269 guint32 rtptime;
1270 guint64 ext_rtptime;
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001271 guint64 rt_diff, rtp_diff;
Wim Taymans54b3dec2007-04-18 18:58:53 +00001272
1273 g_return_val_if_fail (RTP_IS_SOURCE (src), GST_FLOW_ERROR);
Wim Taymans54b3dec2007-04-18 18:58:53 +00001274
1275 /* we are a sender now */
1276 src->is_sender = TRUE;
1277
Jose Antonio Santos Cadenas11f298a2015-06-22 11:28:13 +02001278 /* we are also a receiver of our packets */
Hyunjun Kob814d7e2015-10-02 16:18:15 +09001279 if (!update_receiver_stats (src, pinfo, FALSE))
Jose Antonio Santos Cadenas11f298a2015-06-22 11:28:13 +02001280 return GST_FLOW_OK;
1281
Miguel París Díaz54a2f332017-03-15 18:58:55 +01001282 if (src->pt_set && src->pt != pinfo->pt) {
Patrick Radizi23f77392017-09-14 11:20:17 +02001283 GST_WARNING ("Changing pt from %u to %u for SSRC %u", src->pt, pinfo->pt,
1284 src->ssrc);
Miguel París Díaz54a2f332017-03-15 18:58:55 +01001285 }
1286
1287 src->pt = pinfo->pt;
1288 src->pt_set = TRUE;
1289
Wim Taymans600afaa2007-04-30 13:41:30 +00001290 /* update stats for the SR */
Wim Taymans28e5f902013-09-13 12:40:41 +02001291 src->stats.packets_sent += pinfo->packets;
1292 src->stats.octets_sent += pinfo->payload_len;
1293 src->bytes_sent += pinfo->payload_len;
1294
1295 running_time = pinfo->running_time;
Wim Taymans2f5b1302008-11-20 18:41:34 +00001296
Wim Taymansc971d1a2010-03-02 12:39:20 +01001297 do_bitrate_estimation (src, running_time, &src->bytes_sent);
Wim Taymans600afaa2007-04-30 13:41:30 +00001298
Wim Taymans28e5f902013-09-13 12:40:41 +02001299 rtptime = pinfo->rtptime;
Wim Taymansc795b722013-09-13 10:55:31 +02001300
Wim Taymans919deb42007-09-12 18:04:32 +00001301 ext_rtptime = src->last_rtptime;
1302 ext_rtptime = gst_rtp_buffer_ext_timestamp (&ext_rtptime, rtptime);
1303
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001304 GST_LOG ("SSRC %08x, RTP %" G_GUINT64_FORMAT ", running_time %"
1305 GST_TIME_FORMAT, src->ssrc, ext_rtptime, GST_TIME_ARGS (running_time));
Wim Taymans919deb42007-09-12 18:04:32 +00001306
1307 if (ext_rtptime > src->last_rtptime) {
1308 rtp_diff = ext_rtptime - src->last_rtptime;
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001309 rt_diff = running_time - src->last_rtime;
Wim Taymans919deb42007-09-12 18:04:32 +00001310
1311 /* calc the diff so we can detect drift at the sender. This can also be used
1312 * to guestimate the clock rate if the NTP time is locked to the RTP
1313 * timestamps (as is the case when the capture device is providing the clock). */
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001314 GST_LOG ("SSRC %08x, diff RTP %" G_GUINT64_FORMAT ", diff running_time %"
1315 GST_TIME_FORMAT, src->ssrc, rtp_diff, GST_TIME_ARGS (rt_diff));
Wim Taymans919deb42007-09-12 18:04:32 +00001316 }
1317
Wim Taymans325dac02007-08-29 01:22:43 +00001318 /* we keep track of the last received RTP timestamp and the corresponding
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001319 * buffer running_time so that we can use this info when constructing SR reports */
1320 src->last_rtime = running_time;
Wim Taymans919deb42007-09-12 18:04:32 +00001321 src->last_rtptime = ext_rtptime;
Wim Taymansc576bce2007-08-29 16:56:27 +00001322
Wim Taymans54b3dec2007-04-18 18:58:53 +00001323 /* push packet */
Wim Taymansc795b722013-09-13 10:55:31 +02001324 if (!src->callbacks.push_rtp)
Branko Subasic779f67a2009-06-19 19:09:19 +02001325 goto no_callback;
Mark Nauwelaertsef026342011-07-06 10:11:52 +02001326
Wim Taymans28e5f902013-09-13 12:40:41 +02001327 GST_LOG ("pushing RTP %s %" G_GUINT64_FORMAT,
1328 pinfo->is_list ? "list" : "packet", src->stats.packets_sent);
Branko Subasic779f67a2009-06-19 19:09:19 +02001329
Wim Taymans28e5f902013-09-13 12:40:41 +02001330 result = src->callbacks.push_rtp (src, pinfo->data, src->user_data);
1331 pinfo->data = NULL;
Branko Subasic779f67a2009-06-19 19:09:19 +02001332
Wim Taymans54b3dec2007-04-18 18:58:53 +00001333 return result;
Branko Subasic779f67a2009-06-19 19:09:19 +02001334
1335 /* ERRORS */
Branko Subasic779f67a2009-06-19 19:09:19 +02001336no_callback:
1337 {
1338 GST_WARNING ("no callback installed, dropping packet");
Branko Subasic779f67a2009-06-19 19:09:19 +02001339 return GST_FLOW_OK;
1340 }
Wim Taymans54b3dec2007-04-18 18:58:53 +00001341}
1342
1343/**
1344 * rtp_source_process_sr:
1345 * @src: an #RTPSource
Wim Taymanse7b62122007-09-03 21:19:34 +00001346 * @time: time of packet arrival
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001347 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1348 * @rtptime: the RTP time (in clock rate units)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001349 * @packet_count: the packet count
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001350 * @octet_count: the octet count
Wim Taymans54b3dec2007-04-18 18:58:53 +00001351 *
1352 * Update the sender report in @src.
1353 */
1354void
Wim Taymanse7b62122007-09-03 21:19:34 +00001355rtp_source_process_sr (RTPSource * src, GstClockTime time, guint64 ntptime,
1356 guint32 rtptime, guint32 packet_count, guint32 octet_count)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001357{
1358 RTPSenderReport *curr;
1359 gint curridx;
1360
1361 g_return_if_fail (RTP_IS_SOURCE (src));
1362
Stefan Kost15b54ec2007-05-10 14:02:07 +00001363 GST_DEBUG ("got SR packet: SSRC %08x, NTP %08x:%08x, RTP %" G_GUINT32_FORMAT
1364 ", PC %" G_GUINT32_FORMAT ", OC %" G_GUINT32_FORMAT, src->ssrc,
1365 (guint32) (ntptime >> 32), (guint32) (ntptime & 0xffffffff), rtptime,
1366 packet_count, octet_count);
Wim Taymans54b3dec2007-04-18 18:58:53 +00001367
1368 curridx = src->stats.curr_sr ^ 1;
1369 curr = &src->stats.sr[curridx];
1370
Wim Taymans23883be2007-04-25 08:30:48 +00001371 /* this is a sender now */
1372 src->is_sender = TRUE;
1373
Wim Taymans54b3dec2007-04-18 18:58:53 +00001374 /* update current */
1375 curr->is_valid = TRUE;
1376 curr->ntptime = ntptime;
1377 curr->rtptime = rtptime;
1378 curr->packet_count = packet_count;
1379 curr->octet_count = octet_count;
Wim Taymansae536e02007-04-25 13:19:36 +00001380 curr->time = time;
Wim Taymans54b3dec2007-04-18 18:58:53 +00001381
1382 /* make current */
1383 src->stats.curr_sr = curridx;
Mark Nauwelaertse2179cb2011-09-02 13:41:41 +02001384
1385 src->stats.prev_rtcptime = src->stats.last_rtcptime;
1386 src->stats.last_rtcptime = time;
Wim Taymans54b3dec2007-04-18 18:58:53 +00001387}
1388
1389/**
1390 * rtp_source_process_rb:
1391 * @src: an #RTPSource
Wim Taymans8598aaf2011-02-02 18:21:26 +01001392 * @ntpnstime: the current time in nanoseconds since 1970
Wim Taymans54b3dec2007-04-18 18:58:53 +00001393 * @fractionlost: fraction lost since last SR/RR
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001394 * @packetslost: the cumulative number of packets lost
Wim Taymans54b3dec2007-04-18 18:58:53 +00001395 * @exthighestseq: the extended last sequence number received
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001396 * @jitter: the interarrival jitter (in clock rate units)
1397 * @lsr: the time of the last SR packet on this source
1398 * (in NTP Short Format, 16.16 fixed point)
1399 * @dlsr: the delay since the last SR packet
1400 * (in NTP Short Format, 16.16 fixed point)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001401 *
1402 * Update the report block in @src.
1403 */
1404void
Wim Taymans61382aa2011-02-02 18:27:52 +01001405rtp_source_process_rb (RTPSource * src, guint64 ntpnstime,
Wim Taymans8598aaf2011-02-02 18:21:26 +01001406 guint8 fractionlost, gint32 packetslost, guint32 exthighestseq,
1407 guint32 jitter, guint32 lsr, guint32 dlsr)
Wim Taymans54b3dec2007-04-18 18:58:53 +00001408{
1409 RTPReceiverReport *curr;
1410 gint curridx;
Wim Taymanse7b62122007-09-03 21:19:34 +00001411 guint32 ntp, A;
Wim Taymans8598aaf2011-02-02 18:21:26 +01001412 guint64 f_ntp;
Wim Taymans54b3dec2007-04-18 18:58:53 +00001413
1414 g_return_if_fail (RTP_IS_SOURCE (src));
1415
Wim Taymanse7b62122007-09-03 21:19:34 +00001416 GST_DEBUG ("got RB packet: SSRC %08x, FL %2x, PL %d, HS %" G_GUINT32_FORMAT
1417 ", jitter %" G_GUINT32_FORMAT ", LSR %04x:%04x, DLSR %04x:%04x",
1418 src->ssrc, fractionlost, packetslost, exthighestseq, jitter, lsr >> 16,
1419 lsr & 0xffff, dlsr >> 16, dlsr & 0xffff);
Wim Taymans54b3dec2007-04-18 18:58:53 +00001420
1421 curridx = src->stats.curr_rr ^ 1;
1422 curr = &src->stats.rr[curridx];
1423
1424 /* update current */
1425 curr->is_valid = TRUE;
1426 curr->fractionlost = fractionlost;
1427 curr->packetslost = packetslost;
1428 curr->exthighestseq = exthighestseq;
1429 curr->jitter = jitter;
1430 curr->lsr = lsr;
1431 curr->dlsr = dlsr;
1432
Wim Taymans8598aaf2011-02-02 18:21:26 +01001433 /* convert the NTP time in nanoseconds to 32.32 fixed point */
1434 f_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
Wim Taymans2f5b1302008-11-20 18:41:34 +00001435 /* calculate round trip, round the time up */
Wim Taymans8598aaf2011-02-02 18:21:26 +01001436 ntp = ((f_ntp + 0xffff) >> 16) & 0xffffffff;
1437
Wim Taymans2f5b1302008-11-20 18:41:34 +00001438 A = dlsr + lsr;
1439 if (A > 0 && ntp > A)
1440 A = ntp - A;
1441 else
1442 A = 0;
Wim Taymanse7b62122007-09-03 21:19:34 +00001443 curr->round_trip = A;
1444
1445 GST_DEBUG ("NTP %04x:%04x, round trip %04x:%04x", ntp >> 16, ntp & 0xffff,
1446 A >> 16, A & 0xffff);
1447
Wim Taymans54b3dec2007-04-18 18:58:53 +00001448 /* make current */
1449 src->stats.curr_rr = curridx;
1450}
Wim Taymansae536e02007-04-25 13:19:36 +00001451
1452/**
Wim Taymanse7b62122007-09-03 21:19:34 +00001453 * rtp_source_get_new_sr:
Wim Taymansae536e02007-04-25 13:19:36 +00001454 * @src: an #RTPSource
Wim Taymans1971ae02007-12-10 11:08:11 +00001455 * @ntpnstime: the current time in nanoseconds since 1970
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001456 * @running_time: the current running_time of the pipeline
1457 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1458 * @rtptime: the RTP time corresponding to @ntptime (in clock rate units)
Wim Taymansae536e02007-04-25 13:19:36 +00001459 * @packet_count: the packet count
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001460 * @octet_count: the octet count
Wim Taymanse7b62122007-09-03 21:19:34 +00001461 *
1462 * Get new values to put into a new SR report from this source.
1463 *
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001464 * @running_time and @ntpnstime are captured at the same time and represent the
1465 * running time of the pipeline clock and the absolute current system time in
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001466 * nanoseconds respectively. Together with the last running_time and RTP timestamp
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001467 * we have observed in the source, we can generate @ntptime and @rtptime for an SR
1468 * packet. @ntptime is basically the fixed point representation of @ntpnstime
1469 * and @rtptime the associated RTP timestamp.
1470 *
Wim Taymanse7b62122007-09-03 21:19:34 +00001471 * Returns: %TRUE on success.
1472 */
1473gboolean
Wim Taymans1971ae02007-12-10 11:08:11 +00001474rtp_source_get_new_sr (RTPSource * src, guint64 ntpnstime,
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001475 GstClockTime running_time, guint64 * ntptime, guint32 * rtptime,
1476 guint32 * packet_count, guint32 * octet_count)
Wim Taymanse7b62122007-09-03 21:19:34 +00001477{
Wim Taymans919deb42007-09-12 18:04:32 +00001478 guint64 t_rtp;
Wim Taymanse7b62122007-09-03 21:19:34 +00001479 guint64 t_current_ntp;
1480 GstClockTimeDiff diff;
1481
1482 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1483
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001484 /* We last saw a buffer with last_rtptime at last_rtime. Given a running_time
1485 * and an NTP time, we can scale the RTP timestamps so that they match the
1486 * given NTP time. for scaling, we assume that the slope of the rtptime vs
1487 * running_time vs ntptime curve is close to 1, which is certainly
Wim Taymanse7b62122007-09-03 21:19:34 +00001488 * sufficient for the frequency at which we report SR and the rate we send
1489 * out RTP packets. */
1490 t_rtp = src->last_rtptime;
1491
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001492 GST_DEBUG ("last_rtime %" GST_TIME_FORMAT ", last_rtptime %"
1493 G_GUINT64_FORMAT, GST_TIME_ARGS (src->last_rtime), t_rtp);
Wim Taymanse7b62122007-09-03 21:19:34 +00001494
Miguel París Díaz54a2f332017-03-15 18:58:55 +01001495 if (src->clock_rate == -1 && src->pt_set) {
1496 GST_INFO ("no clock-rate, getting for pt %u and SSRC %u", src->pt,
1497 src->ssrc);
1498 get_clock_rate (src, src->pt);
1499 }
1500
Wim Taymanse7b62122007-09-03 21:19:34 +00001501 if (src->clock_rate != -1) {
Wim Taymans83cb1ae2010-01-20 17:04:03 +01001502 /* get the diff between the clock running_time and the buffer running_time.
1503 * This is the elapsed time, as measured against the pipeline clock, between
1504 * when the rtp timestamp was observed and the current running_time.
1505 *
1506 * We need to apply this diff to the RTP timestamp to get the RTP timestamp
1507 * for the given ntpnstime. */
1508 diff = GST_CLOCK_DIFF (src->last_rtime, running_time);
Luis de Bethencourtd4f094f2015-11-03 14:26:29 +00001509 GST_DEBUG ("running_time %" GST_TIME_FORMAT ", diff %" GST_STIME_FORMAT,
1510 GST_TIME_ARGS (running_time), GST_STIME_ARGS (diff));
Wim Taymanse7b62122007-09-03 21:19:34 +00001511
1512 /* now translate the diff to RTP time, handle positive and negative cases.
1513 * If there is no diff, we already set rtptime correctly above. */
1514 if (diff > 0) {
Wim Taymanse7b62122007-09-03 21:19:34 +00001515 t_rtp += gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1516 } else {
1517 diff = -diff;
Wim Taymanse7b62122007-09-03 21:19:34 +00001518 t_rtp -= gst_util_uint64_scale_int (diff, src->clock_rate, GST_SECOND);
1519 }
1520 } else {
Miguel París Díaz389e0ab2016-06-03 13:09:35 +02001521 GST_WARNING ("no clock-rate, cannot interpolate rtp time for SSRC %u",
1522 src->ssrc);
Wim Taymanse7b62122007-09-03 21:19:34 +00001523 }
1524
Wim Taymans919deb42007-09-12 18:04:32 +00001525 /* convert the NTP time in nanoseconds to 32.32 fixed point */
Wim Taymanse7b62122007-09-03 21:19:34 +00001526 t_current_ntp = gst_util_uint64_scale (ntpnstime, (1LL << 32), GST_SECOND);
1527
1528 GST_DEBUG ("NTP %08x:%08x, RTP %" G_GUINT32_FORMAT,
1529 (guint32) (t_current_ntp >> 32), (guint32) (t_current_ntp & 0xffffffff),
Wim Taymans919deb42007-09-12 18:04:32 +00001530 (guint32) t_rtp);
Wim Taymanse7b62122007-09-03 21:19:34 +00001531
1532 if (ntptime)
1533 *ntptime = t_current_ntp;
1534 if (rtptime)
1535 *rtptime = t_rtp;
1536 if (packet_count)
1537 *packet_count = src->stats.packets_sent;
1538 if (octet_count)
1539 *octet_count = src->stats.octets_sent;
1540
1541 return TRUE;
1542}
1543
1544/**
1545 * rtp_source_get_new_rb:
1546 * @src: an #RTPSource
Wim Taymans2f5b1302008-11-20 18:41:34 +00001547 * @time: the current time of the system clock
Wim Taymanse7b62122007-09-03 21:19:34 +00001548 * @fractionlost: fraction lost since last SR/RR
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001549 * @packetslost: the cumulative number of packets lost
Wim Taymanse7b62122007-09-03 21:19:34 +00001550 * @exthighestseq: the extended last sequence number received
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001551 * @jitter: the interarrival jitter (in clock rate units)
1552 * @lsr: the time of the last SR packet on this source
Ilya Konstantinovc7e168e2015-06-10 14:49:50 +03001553 * (in NTP Short Format, 16.16 fixed point)
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001554 * @dlsr: the delay since the last SR packet
Ilya Konstantinovc7e168e2015-06-10 14:49:50 +03001555 * (in NTP Short Format, 16.16 fixed point)
Wim Taymanse7b62122007-09-03 21:19:34 +00001556 *
Wim Taymans1971ae02007-12-10 11:08:11 +00001557 * Get new values to put into a new report block from this source.
Wim Taymanse7b62122007-09-03 21:19:34 +00001558 *
1559 * Returns: %TRUE on success.
1560 */
1561gboolean
Wim Taymans2f5b1302008-11-20 18:41:34 +00001562rtp_source_get_new_rb (RTPSource * src, GstClockTime time,
Wim Taymanse7b62122007-09-03 21:19:34 +00001563 guint8 * fractionlost, gint32 * packetslost, guint32 * exthighestseq,
1564 guint32 * jitter, guint32 * lsr, guint32 * dlsr)
1565{
1566 RTPSourceStats *stats;
1567 guint64 extended_max, expected;
1568 guint64 expected_interval, received_interval, ntptime;
1569 gint64 lost, lost_interval;
1570 guint32 fraction, LSR, DLSR;
1571 GstClockTime sr_time;
1572
1573 stats = &src->stats;
1574
1575 extended_max = stats->cycles + stats->max_seq;
1576 expected = extended_max - stats->base_seq + 1;
1577
1578 GST_DEBUG ("ext_max %" G_GUINT64_FORMAT ", expected %" G_GUINT64_FORMAT
1579 ", received %" G_GUINT64_FORMAT ", base_seq %" G_GUINT32_FORMAT,
1580 extended_max, expected, stats->packets_received, stats->base_seq);
1581
1582 lost = expected - stats->packets_received;
1583 lost = CLAMP (lost, -0x800000, 0x7fffff);
1584
1585 expected_interval = expected - stats->prev_expected;
1586 stats->prev_expected = expected;
1587 received_interval = stats->packets_received - stats->prev_received;
1588 stats->prev_received = stats->packets_received;
1589
1590 lost_interval = expected_interval - received_interval;
1591
1592 if (expected_interval == 0 || lost_interval <= 0)
1593 fraction = 0;
1594 else
1595 fraction = (lost_interval << 8) / expected_interval;
1596
1597 GST_DEBUG ("add RR for SSRC %08x", src->ssrc);
1598 /* we scaled the jitter up for additional precision */
1599 GST_DEBUG ("fraction %" G_GUINT32_FORMAT ", lost %" G_GINT64_FORMAT
1600 ", extseq %" G_GUINT64_FORMAT ", jitter %d", fraction, lost,
1601 extended_max, stats->jitter >> 4);
1602
1603 if (rtp_source_get_last_sr (src, &sr_time, &ntptime, NULL, NULL, NULL)) {
1604 GstClockTime diff;
1605
1606 /* LSR is middle 32 bits of the last ntptime */
1607 LSR = (ntptime >> 16) & 0xffffffff;
Wim Taymans2f5b1302008-11-20 18:41:34 +00001608 diff = time - sr_time;
Wim Taymanse7b62122007-09-03 21:19:34 +00001609 GST_DEBUG ("last SR time diff %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
1610 /* DLSR, delay since last SR is expressed in 1/65536 second units */
1611 DLSR = gst_util_uint64_scale_int (diff, 65536, GST_SECOND);
1612 } else {
1613 /* No valid SR received, LSR/DLSR are set to 0 then */
1614 GST_DEBUG ("no valid SR received");
1615 LSR = 0;
1616 DLSR = 0;
1617 }
1618 GST_DEBUG ("LSR %04x:%04x, DLSR %04x:%04x", LSR >> 16, LSR & 0xffff,
1619 DLSR >> 16, DLSR & 0xffff);
1620
1621 if (fractionlost)
1622 *fractionlost = fraction;
1623 if (packetslost)
1624 *packetslost = lost;
1625 if (exthighestseq)
1626 *exthighestseq = extended_max;
1627 if (jitter)
1628 *jitter = stats->jitter >> 4;
1629 if (lsr)
1630 *lsr = LSR;
1631 if (dlsr)
1632 *dlsr = DLSR;
1633
1634 return TRUE;
1635}
1636
1637/**
1638 * rtp_source_get_last_sr:
1639 * @src: an #RTPSource
Wim Taymansae536e02007-04-25 13:19:36 +00001640 * @time: time of packet arrival
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001641 * @ntptime: the NTP time (in NTP Timestamp Format, 32.32 fixed point)
1642 * @rtptime: the RTP time (in clock rate units)
Wim Taymanse7b62122007-09-03 21:19:34 +00001643 * @packet_count: the packet count
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001644 * @octet_count: the octet count
Wim Taymansae536e02007-04-25 13:19:36 +00001645 *
1646 * Get the values of the last sender report as set with rtp_source_process_sr().
1647 *
1648 * Returns: %TRUE if there was a valid SR report.
1649 */
1650gboolean
Wim Taymanse7b62122007-09-03 21:19:34 +00001651rtp_source_get_last_sr (RTPSource * src, GstClockTime * time, guint64 * ntptime,
1652 guint32 * rtptime, guint32 * packet_count, guint32 * octet_count)
Wim Taymansae536e02007-04-25 13:19:36 +00001653{
1654 RTPSenderReport *curr;
1655
1656 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1657
1658 curr = &src->stats.sr[src->stats.curr_sr];
1659 if (!curr->is_valid)
1660 return FALSE;
1661
1662 if (ntptime)
1663 *ntptime = curr->ntptime;
1664 if (rtptime)
1665 *rtptime = curr->rtptime;
1666 if (packet_count)
1667 *packet_count = curr->packet_count;
1668 if (octet_count)
1669 *octet_count = curr->octet_count;
1670 if (time)
1671 *time = curr->time;
1672
1673 return TRUE;
1674}
1675
1676/**
1677 * rtp_source_get_last_rb:
1678 * @src: an #RTPSource
1679 * @fractionlost: fraction lost since last SR/RR
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001680 * @packetslost: the cumulative number of packets lost
Wim Taymansae536e02007-04-25 13:19:36 +00001681 * @exthighestseq: the extended last sequence number received
Ilya Konstantinov0a578c22015-06-09 19:02:55 +03001682 * @jitter: the interarrival jitter (in clock rate units)
1683 * @lsr: the time of the last SR packet on this source
1684 * (in NTP Short Format, 16.16 fixed point)
1685 * @dlsr: the delay since the last SR packet
1686 * (in NTP Short Format, 16.16 fixed point)
1687 * @round_trip: the round-trip time
1688 * (in NTP Short Format, 16.16 fixed point)
Wim Taymansae536e02007-04-25 13:19:36 +00001689 *
1690 * Get the values of the last RB report set with rtp_source_process_rb().
1691 *
1692 * Returns: %TRUE if there was a valid SB report.
1693 */
1694gboolean
1695rtp_source_get_last_rb (RTPSource * src, guint8 * fractionlost,
1696 gint32 * packetslost, guint32 * exthighestseq, guint32 * jitter,
Wim Taymans2f5b1302008-11-20 18:41:34 +00001697 guint32 * lsr, guint32 * dlsr, guint32 * round_trip)
Wim Taymansae536e02007-04-25 13:19:36 +00001698{
1699 RTPReceiverReport *curr;
1700
1701 g_return_val_if_fail (RTP_IS_SOURCE (src), FALSE);
1702
1703 curr = &src->stats.rr[src->stats.curr_rr];
1704 if (!curr->is_valid)
1705 return FALSE;
1706
1707 if (fractionlost)
1708 *fractionlost = curr->fractionlost;
1709 if (packetslost)
1710 *packetslost = curr->packetslost;
1711 if (exthighestseq)
1712 *exthighestseq = curr->exthighestseq;
1713 if (jitter)
1714 *jitter = curr->jitter;
1715 if (lsr)
1716 *lsr = curr->lsr;
1717 if (dlsr)
1718 *dlsr = curr->dlsr;
Wim Taymans2f5b1302008-11-20 18:41:34 +00001719 if (round_trip)
1720 *round_trip = curr->round_trip;
Wim Taymansae536e02007-04-25 13:19:36 +00001721
1722 return TRUE;
1723}
Olivier Crêtef336ea22010-03-05 15:46:48 +01001724
Olivier Crête2e54d382014-05-03 18:30:20 -04001725gboolean
1726find_conflicting_address (GList * conflicting_addresses,
1727 GSocketAddress * address, GstClockTime time)
1728{
1729 GList *item;
1730
1731 for (item = conflicting_addresses; item; item = g_list_next (item)) {
1732 RTPConflictingAddress *known_conflict = item->data;
1733
1734 if (__g_socket_address_equal (address, known_conflict->address)) {
1735 known_conflict->time = time;
1736 return TRUE;
1737 }
1738 }
1739
1740 return FALSE;
1741}
1742
1743GList *
1744add_conflicting_address (GList * conflicting_addresses,
1745 GSocketAddress * address, GstClockTime time)
1746{
1747 RTPConflictingAddress *new_conflict;
1748
1749 new_conflict = g_slice_new (RTPConflictingAddress);
1750
1751 new_conflict->address = G_SOCKET_ADDRESS (g_object_ref (address));
1752 new_conflict->time = time;
1753
1754 return g_list_prepend (conflicting_addresses, new_conflict);
1755}
1756
1757GList *
1758timeout_conflicting_addresses (GList * conflicting_addresses,
1759 GstClockTime current_time)
1760{
1761 GList *item;
1762 /* "a relatively long time" -- RFC 3550 section 8.2 */
1763 const GstClockTime collision_timeout =
1764 RTP_STATS_MIN_INTERVAL * GST_SECOND * 10;
1765
1766 item = g_list_first (conflicting_addresses);
1767 while (item) {
1768 RTPConflictingAddress *known_conflict = item->data;
1769 GList *next_item = g_list_next (item);
1770
1771 if (known_conflict->time < current_time - collision_timeout) {
1772 gchar *buf;
1773
1774 conflicting_addresses = g_list_delete_link (conflicting_addresses, item);
1775 buf = __g_socket_address_to_string (known_conflict->address);
1776 GST_DEBUG ("collision %p timed out: %s", known_conflict, buf);
1777 g_free (buf);
1778 rtp_conflicting_address_free (known_conflict);
1779 }
1780 item = next_item;
1781 }
1782
1783 return conflicting_addresses;
1784}
1785
Olivier Crêtef336ea22010-03-05 15:46:48 +01001786/**
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001787 * rtp_source_find_conflicting_address:
Olivier Crêtef336ea22010-03-05 15:46:48 +01001788 * @src: The source the packet came in
1789 * @address: address to check for
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001790 * @time: The time when the packet that is possibly in conflict arrived
Olivier Crêtef336ea22010-03-05 15:46:48 +01001791 *
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001792 * Checks if an address which has a conflict is already known. If it is
1793 * a known conflict, remember the time
Olivier Crêtef336ea22010-03-05 15:46:48 +01001794 *
1795 * Returns: TRUE if it was a known conflict, FALSE otherwise
1796 */
Olivier Crêtef336ea22010-03-05 15:46:48 +01001797gboolean
Sebastian Drögecb789e32012-01-17 13:08:42 +01001798rtp_source_find_conflicting_address (RTPSource * src, GSocketAddress * address,
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001799 GstClockTime time)
Olivier Crêtef336ea22010-03-05 15:46:48 +01001800{
Olivier Crête2e54d382014-05-03 18:30:20 -04001801 return find_conflicting_address (src->conflicting_addresses, address, time);
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001802}
1803
1804/**
1805 * rtp_source_add_conflicting_address:
1806 * @src: The source the packet came in
1807 * @address: address to remember
1808 * @time: The time when the packet that is in conflict arrived
1809 *
1810 * Adds a new conflict address
1811 */
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001812void
1813rtp_source_add_conflicting_address (RTPSource * src,
Sebastian Drögecb789e32012-01-17 13:08:42 +01001814 GSocketAddress * address, GstClockTime time)
Olivier Crêtea6dfe962010-03-05 16:08:45 +01001815{
Olivier Crête2e54d382014-05-03 18:30:20 -04001816 src->conflicting_addresses =
1817 add_conflicting_address (src->conflicting_addresses, address, time);
Olivier Crêtef336ea22010-03-05 15:46:48 +01001818}
1819
1820/**
1821 * rtp_source_timeout:
1822 * @src: The #RTPSource
1823 * @current_time: The current time
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001824 * @feedback_retention_window: The running time before which retained feedback
1825 * packets have to be discarded
Olivier Crêtef336ea22010-03-05 15:46:48 +01001826 *
1827 * This is processed on each RTCP interval. It times out old collisions.
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001828 * It also times out old retained feedback packets
Olivier Crêtef336ea22010-03-05 15:46:48 +01001829 */
Olivier Crêtef336ea22010-03-05 15:46:48 +01001830void
1831rtp_source_timeout (RTPSource * src, GstClockTime current_time,
Olivier Crête2e54d382014-05-03 18:30:20 -04001832 GstClockTime feedback_retention_window)
Olivier Crêtef336ea22010-03-05 15:46:48 +01001833{
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001834 GstRTCPPacket *pkt;
Olivier Crêtef336ea22010-03-05 15:46:48 +01001835
Olivier Crête2e54d382014-05-03 18:30:20 -04001836 src->conflicting_addresses =
1837 timeout_conflicting_addresses (src->conflicting_addresses, current_time);
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001838
1839 /* Time out AVPF packets that are older than the desired length */
1840 while ((pkt = g_queue_peek_tail (src->retained_feedback)) &&
Sebastian Drögedc059ef2015-06-10 14:33:50 +02001841 GST_BUFFER_PTS (pkt) < feedback_retention_window)
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001842 gst_buffer_unref (g_queue_pop_tail (src->retained_feedback));
1843}
1844
1845static gint
1846compare_buffers (gconstpointer a, gconstpointer b, gpointer user_data)
1847{
1848 const GstBuffer *bufa = a;
1849 const GstBuffer *bufb = b;
1850
Sebastian Drögedc059ef2015-06-10 14:33:50 +02001851 return GST_BUFFER_PTS (bufa) - GST_BUFFER_PTS (bufb);
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001852}
1853
1854void
1855rtp_source_retain_rtcp_packet (RTPSource * src, GstRTCPPacket * packet,
1856 GstClockTime running_time)
1857{
1858 GstBuffer *buffer;
1859
Mark Nauwelaertsef026342011-07-06 10:11:52 +02001860 buffer = gst_buffer_copy_region (packet->rtcp->buffer, GST_BUFFER_COPY_MEMORY,
1861 packet->offset, (gst_rtcp_packet_get_length (packet) + 1) * 4);
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001862
Sebastian Drögedc059ef2015-06-10 14:33:50 +02001863 GST_BUFFER_PTS (buffer) = running_time;
Olivier Crêtedb5150a2010-06-22 13:33:32 -04001864
1865 g_queue_insert_sorted (src->retained_feedback, buffer, compare_buffers, NULL);
1866}
1867
1868gboolean
1869rtp_source_has_retained (RTPSource * src, GCompareFunc func, gconstpointer data)
1870{
1871 if (g_queue_find_custom (src->retained_feedback, data, func))
1872 return TRUE;
1873 else
1874 return FALSE;
Olivier Crêtef336ea22010-03-05 15:46:48 +01001875}
Wim Taymans4379ed12013-08-05 23:22:16 +02001876
1877/**
Stefan Sauer12930c22015-07-07 16:59:20 +02001878 * rtp_source_register_nack:
Wim Taymans4379ed12013-08-05 23:22:16 +02001879 * @src: The #RTPSource
1880 * @seqnum: a seqnum
1881 *
1882 * Register that @seqnum has not been received from @src.
1883 */
1884void
1885rtp_source_register_nack (RTPSource * src, guint16 seqnum)
1886{
1887 guint i, len;
1888 guint32 dword = seqnum << 16;
1889 gint diff = 16;
1890
1891 len = src->nacks->len;
1892 for (i = 0; i < len; i++) {
1893 guint32 tdword;
1894 guint16 tseq;
1895
1896 tdword = g_array_index (src->nacks, guint32, i);
1897 tseq = tdword >> 16;
1898
1899 diff = gst_rtp_buffer_compare_seqnum (tseq, seqnum);
1900 if (diff < 16)
1901 break;
1902 }
1903 /* we already have this seqnum */
1904 if (diff == 0)
1905 return;
1906 /* it comes before the recorded seqnum, FIXME, we could merge it
1907 * if not to far away */
1908 if (diff < 0) {
1909 GST_DEBUG ("insert NACK #%u at %u", seqnum, i);
1910 g_array_insert_val (src->nacks, i, dword);
1911 } else if (diff < 16) {
1912 /* we can merge it */
1913 dword = g_array_index (src->nacks, guint32, i);
1914 dword |= 1 << (diff - 1);
1915 GST_DEBUG ("merge NACK #%u at %u with NACK #%u -> 0x%08x", seqnum, i,
1916 dword >> 16, dword);
1917 g_array_index (src->nacks, guint32, i) = dword;
1918 } else {
1919 GST_DEBUG ("append NACK #%u", seqnum);
1920 g_array_append_val (src->nacks, dword);
1921 }
1922 src->send_nack = TRUE;
1923}
1924
1925/**
Stefan Sauer12930c22015-07-07 16:59:20 +02001926 * rtp_source_get_nacks:
Wim Taymans4379ed12013-08-05 23:22:16 +02001927 * @src: The #RTPSource
1928 * @n_nacks: result number of nacks
1929 *
1930 * Get the registered NACKS since the last rtp_source_clear_nacks().
1931 *
1932 * Returns: an array of @n_nacks seqnum values.
1933 */
1934guint32 *
1935rtp_source_get_nacks (RTPSource * src, guint * n_nacks)
1936{
1937 if (n_nacks)
1938 *n_nacks = src->nacks->len;
1939
1940 return (guint32 *) src->nacks->data;
1941}
1942
1943void
1944rtp_source_clear_nacks (RTPSource * src)
1945{
1946 g_array_set_size (src->nacks, 0);
1947 src->send_nack = FALSE;
1948}