2 * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 /* for GValueArray... */
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 #include "gstwebrtcstats.h"
28 #include "gstwebrtcbin.h"
29 #include "transportstream.h"
30 #include "transportreceivebin.h"
32 #include "webrtctransceiver.h"
34 #define GST_CAT_DEFAULT gst_webrtc_stats_debug
35 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
40 static gsize _init = 0;
42 if (g_once_init_enter (&_init)) {
43 GST_DEBUG_CATEGORY_INIT (gst_webrtc_stats_debug, "webrtcstats", 0,
45 g_once_init_leave (&_init, 1);
50 monotonic_time_as_double_milliseconds (void)
52 return g_get_monotonic_time () / 1000.0;
56 _set_base_stats (GstStructure * s, GstWebRTCStatsType type, double ts,
59 gchar *name = _enum_value_to_string (GST_TYPE_WEBRTC_STATS_TYPE,
62 g_return_if_fail (name != NULL);
64 gst_structure_set_name (s, name);
65 gst_structure_set (s, "type", GST_TYPE_WEBRTC_STATS_TYPE, type, "timestamp",
66 G_TYPE_DOUBLE, ts, "id", G_TYPE_STRING, id, NULL);
72 _get_peer_connection_stats (GstWebRTCBin * webrtc)
74 GstStructure *s = gst_structure_new_empty ("unused");
76 /* FIXME: datachannel */
77 gst_structure_set (s, "data-channels-opened", G_TYPE_UINT, 0,
78 "data-channels-closed", G_TYPE_UINT, 0, "data-channels-requested",
79 G_TYPE_UINT, 0, "data-channels-accepted", G_TYPE_UINT, 0, NULL);
84 #define CLOCK_RATE_VALUE_TO_SECONDS(v,r) ((double) v / (double) clock_rate)
86 /* https://www.w3.org/TR/webrtc-stats/#inboundrtpstats-dict*
87 https://www.w3.org/TR/webrtc-stats/#outboundrtpstats-dict* */
89 _get_stats_from_rtp_source_stats (GstWebRTCBin * webrtc,
90 const GstStructure * source_stats, const gchar * codec_id,
91 const gchar * transport_id, GstStructure * s)
93 GstStructure *in, *out, *r_in, *r_out;
94 gchar *in_id, *out_id, *r_in_id, *r_out_id;
95 guint ssrc, fir, pli, nack, jitter;
97 guint64 packets, bytes;
98 gboolean have_rb = FALSE, sent_rb = FALSE;
101 gst_structure_get_double (s, "timestamp", &ts);
102 gst_structure_get_uint (source_stats, "ssrc", &ssrc);
103 gst_structure_get (source_stats, "have-rb", G_TYPE_BOOLEAN, &have_rb,
104 "sent_rb", G_TYPE_BOOLEAN, &sent_rb, "clock-rate", G_TYPE_INT,
107 in_id = g_strdup_printf ("rtp-inbound-stream-stats_%u", ssrc);
108 out_id = g_strdup_printf ("rtp-outbound-stream-stats_%u", ssrc);
109 r_in_id = g_strdup_printf ("rtp-remote-inbound-stream-stats_%u", ssrc);
110 r_out_id = g_strdup_printf ("rtp-remote-outbound-stream-stats_%u", ssrc);
112 in = gst_structure_new_empty (in_id);
113 _set_base_stats (in, GST_WEBRTC_STATS_INBOUND_RTP, ts, in_id);
116 gst_structure_set (in, "ssrc", G_TYPE_UINT, ssrc, NULL);
117 gst_structure_set (in, "codec-id", G_TYPE_STRING, codec_id, NULL);
118 gst_structure_set (in, "transport-id", G_TYPE_STRING, transport_id, NULL);
119 if (gst_structure_get_uint (source_stats, "recv-fir-count", &fir))
120 gst_structure_set (in, "fir-count", G_TYPE_UINT, fir, NULL);
121 if (gst_structure_get_uint (source_stats, "recv-pli-count", &pli))
122 gst_structure_set (in, "pli-count", G_TYPE_UINT, pli, NULL);
123 if (gst_structure_get_uint (source_stats, "recv-nack-count", &nack))
124 gst_structure_set (in, "nack-count", G_TYPE_UINT, nack, NULL);
125 /* XXX: mediaType, trackId, sliCount, qpSum */
127 /* RTCReceivedRTPStreamStats */
128 if (gst_structure_get_uint64 (source_stats, "packets-received", &packets))
129 gst_structure_set (in, "packets-received", G_TYPE_UINT64, packets, NULL);
130 if (gst_structure_get_uint64 (source_stats, "octets-received", &bytes))
131 gst_structure_set (in, "bytes-received", G_TYPE_UINT64, bytes, NULL);
132 if (gst_structure_get_int (source_stats, "packets-lost", &lost))
133 gst_structure_set (in, "packets-lost", G_TYPE_INT, lost, NULL);
134 if (gst_structure_get_uint (source_stats, "jitter", &jitter))
135 gst_structure_set (in, "jitter", G_TYPE_DOUBLE,
136 CLOCK_RATE_VALUE_TO_SECONDS (jitter, clock_rate), NULL);
138 RTCReceivedRTPStreamStats
140 unsigned long packetsDiscarded;
141 unsigned long packetsFailedDecryption;
142 unsigned long packetsRepaired;
143 unsigned long burstPacketsLost;
144 unsigned long burstPacketsDiscarded;
145 unsigned long burstLossCount;
146 unsigned long burstDiscardCount;
147 double burstLossRate;
148 double burstDiscardRate;
150 double gapDiscardRate;
153 /* RTCInboundRTPStreamStats */
154 gst_structure_set (in, "remote-id", G_TYPE_STRING, r_out_id, NULL);
155 /* XXX: framesDecoded, lastPacketReceivedTimestamp */
157 r_in = gst_structure_new_empty (r_in_id);
158 _set_base_stats (r_in, GST_WEBRTC_STATS_REMOTE_INBOUND_RTP, ts, r_in_id);
161 gst_structure_set (r_in, "ssrc", G_TYPE_UINT, ssrc, NULL);
162 gst_structure_set (r_in, "codec-id", G_TYPE_STRING, codec_id, NULL);
163 gst_structure_set (r_in, "transport-id", G_TYPE_STRING, transport_id, NULL);
164 /* XXX: mediaType, trackId, sliCount, qpSum */
166 /* RTCReceivedRTPStreamStats */
168 if (gst_structure_get_uint (source_stats, "sent-rb-jitter", &jitter))
169 gst_structure_set (r_in, "jitter", G_TYPE_DOUBLE,
170 CLOCK_RATE_VALUE_TO_SECONDS (jitter, clock_rate), NULL);
171 if (gst_structure_get_int (source_stats, "sent-rb-packetslost", &lost))
172 gst_structure_set (r_in, "packets-lost", G_TYPE_INT, lost, NULL);
173 /* packetsReceived, bytesReceived */
176 gst_structure_set (r_in, "jitter", G_TYPE_DOUBLE, 0.0, "packets-lost",
177 G_TYPE_INT, 0, NULL);
179 /* XXX: RTCReceivedRTPStreamStats
181 unsigned long packetsDiscarded;
182 unsigned long packetsFailedDecryption;
183 unsigned long packetsRepaired;
184 unsigned long burstPacketsLost;
185 unsigned long burstPacketsDiscarded;
186 unsigned long burstLossCount;
187 unsigned long burstDiscardCount;
188 double burstLossRate;
189 double burstDiscardRate;
191 double gapDiscardRate;
194 /* RTCRemoteInboundRTPStreamStats */
195 gst_structure_set (r_in, "local-id", G_TYPE_STRING, out_id, NULL);
198 if (gst_structure_get_uint (source_stats, "rb-round-trip", &rtt)) {
199 /* 16.16 fixed point to double */
201 (double) ((rtt & 0xffff0000) >> 16) + ((rtt & 0xffff) / 65536.0);
202 gst_structure_set (r_in, "round-trip-time", G_TYPE_DOUBLE, val, NULL);
206 gst_structure_set (r_in, "round-trip-time", G_TYPE_DOUBLE, 0.0, NULL);
208 /* XXX: framesDecoded, lastPacketReceivedTimestamp */
210 out = gst_structure_new_empty (out_id);
211 _set_base_stats (out, GST_WEBRTC_STATS_OUTBOUND_RTP, ts, out_id);
214 gst_structure_set (out, "ssrc", G_TYPE_UINT, ssrc, NULL);
215 gst_structure_set (out, "codec-id", G_TYPE_STRING, codec_id, NULL);
216 gst_structure_set (out, "transport-id", G_TYPE_STRING, transport_id, NULL);
217 if (gst_structure_get_uint (source_stats, "sent-fir-count", &fir))
218 gst_structure_set (out, "fir-count", G_TYPE_UINT, fir, NULL);
219 if (gst_structure_get_uint (source_stats, "sent-pli-count", &pli))
220 gst_structure_set (out, "pli-count", G_TYPE_UINT, pli, NULL);
221 if (gst_structure_get_uint (source_stats, "sent-nack-count", &nack))
222 gst_structure_set (out, "nack-count", G_TYPE_UINT, nack, NULL);
223 /* XXX: mediaType, trackId, sliCount, qpSum */
225 /* RTCSentRTPStreamStats */
226 if (gst_structure_get_uint64 (source_stats, "octets-sent", &bytes))
227 gst_structure_set (out, "bytes-sent", G_TYPE_UINT64, bytes, NULL);
228 if (gst_structure_get_uint64 (source_stats, "packets-sent", &packets))
229 gst_structure_set (out, "packets-sent", G_TYPE_UINT64, packets, NULL);
231 unsigned long packetsDiscardedOnSend;
232 unsigned long long bytesDiscardedOnSend;
235 /* RTCOutboundRTPStreamStats */
236 gst_structure_set (out, "remote-id", G_TYPE_STRING, r_in_id, NULL);
238 DOMHighResTimeStamp lastPacketSentTimestamp;
239 double targetBitrate;
240 unsigned long framesEncoded;
241 double totalEncodeTime;
242 double averageRTCPInterval;
245 r_out = gst_structure_new_empty (r_out_id);
246 _set_base_stats (r_out, GST_WEBRTC_STATS_REMOTE_OUTBOUND_RTP, ts, r_out_id);
248 gst_structure_set (r_out, "ssrc", G_TYPE_UINT, ssrc, NULL);
249 gst_structure_set (r_out, "codec-id", G_TYPE_STRING, codec_id, NULL);
250 gst_structure_set (r_out, "transport-id", G_TYPE_STRING, transport_id, NULL);
251 /* XXX: mediaType, trackId, sliCount, qpSum */
253 /* RTCSentRTPStreamStats */
254 /* if (gst_structure_get_uint64 (source_stats, "octets-sent", &bytes))
255 gst_structure_set (r_out, "bytes-sent", G_TYPE_UINT64, bytes, NULL);
256 if (gst_structure_get_uint64 (source_stats, "packets-sent", &packets))
257 gst_structure_set (r_out, "packets-sent", G_TYPE_UINT64, packets, NULL);*/
259 unsigned long packetsDiscardedOnSend;
260 unsigned long long bytesDiscardedOnSend;
263 gst_structure_set (r_out, "local-id", G_TYPE_STRING, in_id, NULL);
265 gst_structure_set (s, in_id, GST_TYPE_STRUCTURE, in, NULL);
266 gst_structure_set (s, out_id, GST_TYPE_STRUCTURE, out, NULL);
267 gst_structure_set (s, r_in_id, GST_TYPE_STRUCTURE, r_in, NULL);
268 gst_structure_set (s, r_out_id, GST_TYPE_STRUCTURE, r_out, NULL);
270 gst_structure_free (in);
271 gst_structure_free (out);
272 gst_structure_free (r_in);
273 gst_structure_free (r_out);
281 /* https://www.w3.org/TR/webrtc-stats/#candidatepair-dict* */
283 _get_stats_from_ice_transport (GstWebRTCBin * webrtc,
284 GstWebRTCICETransport * transport, GstStructure * s)
290 gst_structure_get_double (s, "timestamp", &ts);
292 id = g_strdup_printf ("ice-candidate-pair_%s", GST_OBJECT_NAME (transport));
293 stats = gst_structure_new_empty (id);
294 _set_base_stats (stats, GST_WEBRTC_STATS_TRANSPORT, ts, id);
296 /* XXX: RTCIceCandidatePairStats
297 DOMString transportId;
298 DOMString localCandidateId;
299 DOMString remoteCandidateId;
300 RTCStatsIceCandidatePairState state;
301 unsigned long long priority;
303 unsigned long packetsSent;
304 unsigned long packetsReceived;
305 unsigned long long bytesSent;
306 unsigned long long bytesReceived;
307 DOMHighResTimeStamp lastPacketSentTimestamp;
308 DOMHighResTimeStamp lastPacketReceivedTimestamp;
309 DOMHighResTimeStamp firstRequestTimestamp;
310 DOMHighResTimeStamp lastRequestTimestamp;
311 DOMHighResTimeStamp lastResponseTimestamp;
312 double totalRoundTripTime;
313 double currentRoundTripTime;
314 double availableOutgoingBitrate;
315 double availableIncomingBitrate;
316 unsigned long circuitBreakerTriggerCount;
317 unsigned long long requestsReceived;
318 unsigned long long requestsSent;
319 unsigned long long responsesReceived;
320 unsigned long long responsesSent;
321 unsigned long long retransmissionsReceived;
322 unsigned long long retransmissionsSent;
323 unsigned long long consentRequestsSent;
324 DOMHighResTimeStamp consentExpiredTimestamp;
327 /* XXX: RTCIceCandidateStats
328 DOMString transportId;
330 RTCNetworkType networkType;
334 RTCIceCandidateType candidateType;
337 DOMString relayProtocol;
338 boolean deleted = false;
342 gst_structure_set (s, id, GST_TYPE_STRUCTURE, stats, NULL);
343 gst_structure_free (stats);
348 /* https://www.w3.org/TR/webrtc-stats/#dom-rtctransportstats */
350 _get_stats_from_dtls_transport (GstWebRTCBin * webrtc,
351 GstWebRTCDTLSTransport * transport, GstStructure * s)
357 gst_structure_get_double (s, "timestamp", &ts);
359 id = g_strdup_printf ("transport-stats_%s", GST_OBJECT_NAME (transport));
360 stats = gst_structure_new_empty (id);
361 _set_base_stats (stats, GST_WEBRTC_STATS_TRANSPORT, ts, id);
363 /* XXX: RTCTransportStats
364 unsigned long packetsSent;
365 unsigned long packetsReceived;
366 unsigned long long bytesSent;
367 unsigned long long bytesReceived;
368 DOMString rtcpTransportStatsId;
370 RTCDtlsTransportState dtlsState;
371 DOMString selectedCandidatePairId;
372 DOMString localCertificateId;
373 DOMString remoteCertificateId;
376 /* XXX: RTCCertificateStats
377 DOMString fingerprint;
378 DOMString fingerprintAlgorithm;
379 DOMString base64Certificate;
380 DOMString issuerCertificateId;
383 /* XXX: RTCIceCandidateStats
384 DOMString transportId;
389 RTCIceCandidateType candidateType;
392 boolean deleted = false;
395 gst_structure_set (s, id, GST_TYPE_STRUCTURE, stats, NULL);
396 gst_structure_free (stats);
398 _get_stats_from_ice_transport (webrtc, transport->transport, s);
404 _get_stats_from_transport_channel (GstWebRTCBin * webrtc,
405 TransportStream * stream, const gchar * codec_id, guint ssrc,
408 GstWebRTCDTLSTransport *transport;
409 GObject *rtp_session;
410 GstStructure *rtp_stats;
411 GValueArray *source_stats;
416 gst_structure_get_double (s, "timestamp", &ts);
418 transport = stream->transport;
420 transport = stream->transport;
424 g_signal_emit_by_name (webrtc->rtpbin, "get-internal-session",
425 stream->session_id, &rtp_session);
426 g_object_get (rtp_session, "stats", &rtp_stats, NULL);
428 gst_structure_get (rtp_stats, "source-stats", G_TYPE_VALUE_ARRAY,
429 &source_stats, NULL);
431 GST_DEBUG_OBJECT (webrtc, "retrieving rtp stream stats from transport %"
432 GST_PTR_FORMAT " rtp session %" GST_PTR_FORMAT " with %u rtp sources, "
433 "transport %" GST_PTR_FORMAT, stream, rtp_session, source_stats->n_values,
436 transport_id = _get_stats_from_dtls_transport (webrtc, transport, s);
438 /* construct stats objects */
439 for (i = 0; i < source_stats->n_values; i++) {
440 const GstStructure *stats;
441 const GValue *val = g_value_array_get_nth (source_stats, i);
443 guint stats_ssrc = 0;
445 stats = gst_value_get_structure (val);
447 /* skip internal or foreign sources */
448 gst_structure_get (stats,
449 "internal", G_TYPE_BOOLEAN, &internal,
450 "ssrc", G_TYPE_UINT, &stats_ssrc, NULL);
451 if (internal || (ssrc && stats_ssrc && ssrc != stats_ssrc))
454 _get_stats_from_rtp_source_stats (webrtc, stats, codec_id, transport_id, s);
457 g_object_unref (rtp_session);
458 gst_structure_free (rtp_stats);
459 g_value_array_free (source_stats);
460 g_free (transport_id);
463 /* https://www.w3.org/TR/webrtc-stats/#codec-dict* */
465 _get_codec_stats_from_pad (GstWebRTCBin * webrtc, GstPad * pad,
466 GstStructure * s, gchar ** out_id, guint * out_ssrc)
474 gst_structure_get_double (s, "timestamp", &ts);
476 stats = gst_structure_new_empty ("unused");
477 id = g_strdup_printf ("codec-stats-%s", GST_OBJECT_NAME (pad));
478 _set_base_stats (stats, GST_WEBRTC_STATS_CODEC, ts, id);
480 caps = gst_pad_get_current_caps (pad);
481 if (caps && gst_caps_is_fixed (caps)) {
482 GstStructure *caps_s = gst_caps_get_structure (caps, 0);
485 if (gst_structure_get_int (caps_s, "payload", &pt))
486 gst_structure_set (stats, "payload-type", G_TYPE_UINT, pt, NULL);
488 if (gst_structure_get_int (caps_s, "clock-rate", &clock_rate))
489 gst_structure_set (stats, "clock-rate", G_TYPE_UINT, clock_rate, NULL);
491 if (gst_structure_get_uint (caps_s, "ssrc", &ssrc))
492 gst_structure_set (stats, "ssrc", G_TYPE_UINT, ssrc, NULL);
494 /* FIXME: codecType, mimeType, channels, sdpFmtpLine, implementation, transportId */
498 gst_caps_unref (caps);
500 gst_structure_set (s, id, GST_TYPE_STRUCTURE, stats, NULL);
501 gst_structure_free (stats);
513 _get_stats_from_pad (GstWebRTCBin * webrtc, GstPad * pad, GstStructure * s)
515 GstWebRTCBinPad *wpad = GST_WEBRTC_BIN_PAD (pad);
516 TransportStream *stream;
520 _get_codec_stats_from_pad (webrtc, pad, s, &codec_id, &ssrc);
525 stream = WEBRTC_TRANSCEIVER (wpad->trans)->stream;
529 _get_stats_from_transport_channel (webrtc, stream, codec_id, ssrc, s);
537 gst_webrtc_bin_update_stats (GstWebRTCBin * webrtc)
539 GstStructure *s = gst_structure_new_empty ("application/x-webrtc-stats");
540 double ts = monotonic_time_as_double_milliseconds ();
541 GstStructure *pc_stats;
545 gst_structure_set (s, "timestamp", G_TYPE_DOUBLE, ts, NULL);
547 /* FIXME: better unique IDs */
548 /* FIXME: rate limitting stat updates? */
549 /* FIXME: all stats need to be kept forever */
551 GST_DEBUG_OBJECT (webrtc, "updating stats at time %f", ts);
553 if ((pc_stats = _get_peer_connection_stats (webrtc))) {
554 const gchar *id = "peer-connection-stats";
555 _set_base_stats (pc_stats, GST_WEBRTC_STATS_PEER_CONNECTION, ts, id);
556 gst_structure_set (s, id, GST_TYPE_STRUCTURE, pc_stats, NULL);
557 gst_structure_free (pc_stats);
560 gst_element_foreach_pad (GST_ELEMENT (webrtc),
561 (GstElementForeachPadFunc) _get_stats_from_pad, s);
563 gst_structure_remove_field (s, "timestamp");
565 if (webrtc->priv->stats)
566 gst_structure_free (webrtc->priv->stats);
567 webrtc->priv->stats = s;