blob: ca1671a39e702a71eca67a374fbf209277ea1f3b [file] [log] [blame]
hbosd565b732016-08-30 21:04:351/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/rtc_stats_collector.h"
hbosd565b732016-08-30 21:04:3512
Harald Alvestrand5761e7b2021-01-29 14:45:0813#include <stdio.h>
Artem Titovd15a5752021-02-10 13:31:2414
Harald Alvestrand5761e7b2021-01-29 14:45:0815#include <algorithm>
16#include <cstdint>
Johannes Kron72d69152020-02-10 13:05:5517#include <map>
hbosd565b732016-08-30 21:04:3518#include <memory>
Steve Anton36b29d12017-10-30 16:57:4219#include <string>
hbosd565b732016-08-30 21:04:3520#include <utility>
21#include <vector>
22
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "api/array_view.h"
Patrik Höglunde2d6a062017-10-05 12:53:3324#include "api/candidate.h"
Steve Anton10542f22019-01-11 17:11:0025#include "api/media_stream_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0826#include "api/rtp_parameters.h"
27#include "api/rtp_receiver_interface.h"
28#include "api/rtp_sender_interface.h"
Artem Titovd15a5752021-02-10 13:31:2429#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0830#include "api/stats/rtc_stats.h"
31#include "api/stats/rtcstats_objects.h"
32#include "api/task_queue/queued_task.h"
Henrik Boström2e069262019-04-09 11:59:3133#include "api/video/video_content_type.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0834#include "common_video/include/quality_limitation_reason.h"
Steve Anton10542f22019-01-11 17:11:0035#include "media/base/media_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0836#include "modules/audio_processing/include/audio_processing_statistics.h"
37#include "modules/rtp_rtcp/include/report_block_data.h"
38#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
39#include "p2p/base/connection_info.h"
40#include "p2p/base/dtls_transport_internal.h"
41#include "p2p/base/ice_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0042#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3143#include "p2p/base/port.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0844#include "pc/channel.h"
45#include "pc/channel_interface.h"
46#include "pc/data_channel_utils.h"
Steve Anton10542f22019-01-11 17:11:0047#include "pc/rtc_stats_traversal.h"
Johannes Kron72d69152020-02-10 13:05:5548#include "pc/webrtc_sdp.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3149#include "rtc_base/checks.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0850#include "rtc_base/ip_address.h"
51#include "rtc_base/location.h"
52#include "rtc_base/logging.h"
53#include "rtc_base/network_constants.h"
54#include "rtc_base/ref_counted_object.h"
55#include "rtc_base/rtc_certificate.h"
56#include "rtc_base/socket_address.h"
57#include "rtc_base/ssl_stream_adapter.h"
58#include "rtc_base/string_encode.h"
Jonas Olsson43568dd2018-06-11 14:25:5459#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 17:11:0060#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3161#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 21:04:3562
63namespace webrtc {
64
hboscc555c52016-10-18 19:48:3165namespace {
66
Henrik Boström646fda02019-05-22 13:49:4267// TODO(https://crbug.com/webrtc/10656): Consider making IDs less predictable.
hbos2fa7c672016-10-24 11:00:0568std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
69 return "RTCCertificate_" + fingerprint;
70}
71
Steve Anton57858b32018-02-15 23:19:5072std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
73 bool inbound,
74 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 14:25:5475 char buf[1024];
76 rtc::SimpleStringBuilder sb(buf);
77 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
78 << payload_type;
79 return sb.str();
hbos0adb8282016-11-23 10:32:0680}
81
hbos2fa7c672016-10-24 11:00:0582std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
83 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 14:25:5484 char buf[4096];
85 rtc::SimpleStringBuilder sb(buf);
86 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
87 << info.remote_candidate.id();
88 return sb.str();
hbos2fa7c672016-10-24 11:00:0589}
90
Harald Alvestranda3dab842018-01-14 08:18:5891const char kSender[] = "sender";
92const char kReceiver[] = "receiver";
93
94std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
95 const char* direction,
Harald Alvestrandc72af932018-01-11 16:18:1996 int attachment_id) {
Jonas Olsson43568dd2018-06-11 14:25:5497 char buf[1024];
98 rtc::SimpleStringBuilder sb(buf);
99 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
100 return sb.str();
hbos09bc1282016-11-08 14:29:22101}
102
hbos2fa7c672016-10-24 11:00:05103std::string RTCTransportStatsIDFromTransportChannel(
Jonas Olssona4d87372019-07-05 17:08:33104 const std::string& transport_name,
105 int channel_component) {
Jonas Olsson43568dd2018-06-11 14:25:54106 char buf[1024];
107 rtc::SimpleStringBuilder sb(buf);
108 sb << "RTCTransport_" << transport_name << "_" << channel_component;
109 return sb.str();
hbos2fa7c672016-10-24 11:00:05110}
111
Alessio Bazzicaf7b1b952021-03-23 16:23:04112std::string RTCInboundRTPStreamStatsIDFromSSRC(cricket::MediaType media_type,
113 uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 14:25:54114 char buf[1024];
115 rtc::SimpleStringBuilder sb(buf);
Alessio Bazzicaf7b1b952021-03-23 16:23:04116 sb << "RTCInboundRTP"
117 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
118 << "Stream_" << ssrc;
Jonas Olsson43568dd2018-06-11 14:25:54119 return sb.str();
hboseeafe942016-11-01 10:00:17120}
121
Alessio Bazzicaf7b1b952021-03-23 16:23:04122std::string RTCOutboundRTPStreamStatsIDFromSSRC(cricket::MediaType media_type,
123 uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 14:25:54124 char buf[1024];
125 rtc::SimpleStringBuilder sb(buf);
Alessio Bazzicaf7b1b952021-03-23 16:23:04126 sb << "RTCOutboundRTP"
127 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
128 << "Stream_" << ssrc;
Jonas Olsson43568dd2018-06-11 14:25:54129 return sb.str();
hbos6ded1902016-11-01 08:50:46130}
131
Henrik Boström8605fbf2019-06-24 14:44:51132std::string RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(
Henrik Boström883eefc2019-05-27 11:40:25133 cricket::MediaType media_type,
Henrik Boström883eefc2019-05-27 11:40:25134 uint32_t source_ssrc) {
135 char buf[1024];
136 rtc::SimpleStringBuilder sb(buf);
137 sb << "RTCRemoteInboundRtp"
138 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
Henrik Boström8605fbf2019-06-24 14:44:51139 << "Stream_" << source_ssrc;
Henrik Boström883eefc2019-05-27 11:40:25140 return sb.str();
141}
142
Alessio Bazzicaf7b1b952021-03-23 16:23:04143std::string RTCRemoteOutboundRTPStreamStatsIDFromSSRC(
144 cricket::MediaType media_type,
145 uint32_t source_ssrc) {
146 char buf[1024];
147 rtc::SimpleStringBuilder sb(buf);
148 sb << "RTCRemoteOutboundRTP"
149 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
150 << "Stream_" << source_ssrc;
151 return sb.str();
152}
153
Henrik Boström646fda02019-05-22 13:49:42154std::string RTCMediaSourceStatsIDFromKindAndAttachment(
155 cricket::MediaType media_type,
156 int attachment_id) {
157 char buf[1024];
158 rtc::SimpleStringBuilder sb(buf);
159 sb << "RTC" << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
160 << "Source_" << attachment_id;
161 return sb.str();
162}
163
hbosab9f6e42016-10-07 09:18:47164const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
165 if (type == cricket::LOCAL_PORT_TYPE)
166 return RTCIceCandidateType::kHost;
167 if (type == cricket::STUN_PORT_TYPE)
168 return RTCIceCandidateType::kSrflx;
169 if (type == cricket::PRFLX_PORT_TYPE)
170 return RTCIceCandidateType::kPrflx;
171 if (type == cricket::RELAY_PORT_TYPE)
172 return RTCIceCandidateType::kRelay;
Artem Titovd3251962021-11-15 15:57:07173 RTC_DCHECK_NOTREACHED();
hbosab9f6e42016-10-07 09:18:47174 return nullptr;
175}
176
hboscc555c52016-10-18 19:48:31177const char* DataStateToRTCDataChannelState(
178 DataChannelInterface::DataState state) {
179 switch (state) {
180 case DataChannelInterface::kConnecting:
181 return RTCDataChannelState::kConnecting;
182 case DataChannelInterface::kOpen:
183 return RTCDataChannelState::kOpen;
184 case DataChannelInterface::kClosing:
185 return RTCDataChannelState::kClosing;
186 case DataChannelInterface::kClosed:
187 return RTCDataChannelState::kClosed;
188 default:
Artem Titovd3251962021-11-15 15:57:07189 RTC_DCHECK_NOTREACHED();
hboscc555c52016-10-18 19:48:31190 return nullptr;
191 }
192}
193
hbos06495bc2017-01-02 16:08:18194const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
195 cricket::IceCandidatePairState state) {
196 switch (state) {
197 case cricket::IceCandidatePairState::WAITING:
198 return RTCStatsIceCandidatePairState::kWaiting;
199 case cricket::IceCandidatePairState::IN_PROGRESS:
200 return RTCStatsIceCandidatePairState::kInProgress;
201 case cricket::IceCandidatePairState::SUCCEEDED:
202 return RTCStatsIceCandidatePairState::kSucceeded;
203 case cricket::IceCandidatePairState::FAILED:
204 return RTCStatsIceCandidatePairState::kFailed;
205 default:
Artem Titovd3251962021-11-15 15:57:07206 RTC_DCHECK_NOTREACHED();
hbos06495bc2017-01-02 16:08:18207 return nullptr;
208 }
209}
210
hbos7064d592017-01-16 15:38:02211const char* DtlsTransportStateToRTCDtlsTransportState(
Mirko Bonadei9f6808b2021-05-21 18:46:09212 DtlsTransportState state) {
hbos7064d592017-01-16 15:38:02213 switch (state) {
Mirko Bonadei9f6808b2021-05-21 18:46:09214 case DtlsTransportState::kNew:
hbos7064d592017-01-16 15:38:02215 return RTCDtlsTransportState::kNew;
Mirko Bonadei9f6808b2021-05-21 18:46:09216 case DtlsTransportState::kConnecting:
hbos7064d592017-01-16 15:38:02217 return RTCDtlsTransportState::kConnecting;
Mirko Bonadei9f6808b2021-05-21 18:46:09218 case DtlsTransportState::kConnected:
hbos7064d592017-01-16 15:38:02219 return RTCDtlsTransportState::kConnected;
Mirko Bonadei9f6808b2021-05-21 18:46:09220 case DtlsTransportState::kClosed:
hbos7064d592017-01-16 15:38:02221 return RTCDtlsTransportState::kClosed;
Mirko Bonadei9f6808b2021-05-21 18:46:09222 case DtlsTransportState::kFailed:
hbos7064d592017-01-16 15:38:02223 return RTCDtlsTransportState::kFailed;
224 default:
Mirko Bonadei9f6808b2021-05-21 18:46:09225 RTC_CHECK_NOTREACHED();
hbos7064d592017-01-16 15:38:02226 return nullptr;
227 }
228}
229
Gary Liu37e489c2017-11-21 18:49:36230const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
231 switch (type) {
232 case rtc::ADAPTER_TYPE_CELLULAR:
Jonas Oreland08d18062020-04-02 05:19:12233 case rtc::ADAPTER_TYPE_CELLULAR_2G:
234 case rtc::ADAPTER_TYPE_CELLULAR_3G:
235 case rtc::ADAPTER_TYPE_CELLULAR_4G:
236 case rtc::ADAPTER_TYPE_CELLULAR_5G:
Gary Liu37e489c2017-11-21 18:49:36237 return RTCNetworkType::kCellular;
238 case rtc::ADAPTER_TYPE_ETHERNET:
239 return RTCNetworkType::kEthernet;
240 case rtc::ADAPTER_TYPE_WIFI:
241 return RTCNetworkType::kWifi;
242 case rtc::ADAPTER_TYPE_VPN:
243 return RTCNetworkType::kVpn;
244 case rtc::ADAPTER_TYPE_UNKNOWN:
245 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 22:38:09246 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 18:49:36247 return RTCNetworkType::kUnknown;
248 }
Artem Titovd3251962021-11-15 15:57:07249 RTC_DCHECK_NOTREACHED();
Gary Liu37e489c2017-11-21 18:49:36250 return nullptr;
251}
252
Henrik Boströmce33b6a2019-05-28 15:42:38253const char* QualityLimitationReasonToRTCQualityLimitationReason(
254 QualityLimitationReason reason) {
255 switch (reason) {
256 case QualityLimitationReason::kNone:
257 return RTCQualityLimitationReason::kNone;
258 case QualityLimitationReason::kCpu:
259 return RTCQualityLimitationReason::kCpu;
260 case QualityLimitationReason::kBandwidth:
261 return RTCQualityLimitationReason::kBandwidth;
262 case QualityLimitationReason::kOther:
263 return RTCQualityLimitationReason::kOther;
264 }
Karl Wibergc95b9392020-11-07 23:49:37265 RTC_CHECK_NOTREACHED();
Henrik Boströmce33b6a2019-05-28 15:42:38266}
267
Byoungchan Lee7d235352021-05-28 12:32:04268std::map<std::string, double>
269QualityLimitationDurationToRTCQualityLimitationDuration(
270 std::map<webrtc::QualityLimitationReason, int64_t> durations_ms) {
271 std::map<std::string, double> result;
Philipp Hancke3fd9cbc2022-01-10 16:41:43272 // The internal duration is defined in milliseconds while the spec defines
273 // the value in seconds:
274 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
Byoungchan Lee7d235352021-05-28 12:32:04275 for (const auto& elem : durations_ms) {
276 result[QualityLimitationReasonToRTCQualityLimitationReason(elem.first)] =
Philipp Hancke3fd9cbc2022-01-10 16:41:43277 elem.second / static_cast<double>(rtc::kNumMillisecsPerSec);
Byoungchan Lee7d235352021-05-28 12:32:04278 }
279 return result;
280}
281
hbos9e302742017-01-20 10:47:10282double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
283 RTC_DCHECK_GE(audio_level, 0);
284 RTC_DCHECK_LE(audio_level, 32767);
285 return audio_level / 32767.0;
286}
287
hbos0adb8282016-11-23 10:32:06288std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 23:19:50289 uint64_t timestamp_us,
290 const std::string& mid,
Philipp Hancke95157a02020-11-16 19:08:27291 const std::string& transport_id,
Steve Anton57858b32018-02-15 23:19:50292 bool inbound,
hbos0adb8282016-11-23 10:32:06293 const RtpCodecParameters& codec_params) {
294 RTC_DCHECK_GE(codec_params.payload_type, 0);
295 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 20:09:01296 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 10:32:06297 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
298 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 23:19:50299 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 10:32:06300 timestamp_us));
301 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 14:56:04302 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 20:09:01303 if (codec_params.clock_rate) {
304 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
305 }
Johannes Kron72d69152020-02-10 13:05:55306 if (codec_params.num_channels) {
307 codec_stats->channels = *codec_params.num_channels;
308 }
309
310 rtc::StringBuilder fmtp;
311 if (WriteFmtpParameters(codec_params.parameters, &fmtp)) {
312 codec_stats->sdp_fmtp_line = fmtp.Release();
313 }
Philipp Hancke95157a02020-11-16 19:08:27314 codec_stats->transport_id = transport_id;
hbos0adb8282016-11-23 10:32:06315 return codec_stats;
316}
317
hbos09bc1282016-11-08 14:29:22318void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
319 const MediaStreamTrackInterface& track,
320 RTCMediaStreamTrackStats* track_stats) {
321 track_stats->track_identifier = track.id();
322 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
323}
324
hbos820f5782016-11-22 11:16:50325// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 10:00:17326void SetInboundRTPStreamStatsFromMediaReceiverInfo(
327 const cricket::MediaReceiverInfo& media_receiver_info,
328 RTCInboundRTPStreamStats* inbound_stats) {
329 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 14:28:11330 inbound_stats->ssrc = media_receiver_info.ssrc();
hboseeafe942016-11-01 10:00:17331 inbound_stats->packets_received =
332 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
333 inbound_stats->bytes_received =
Niels Möllerac0a4cb2019-10-09 13:01:33334 static_cast<uint64_t>(media_receiver_info.payload_bytes_rcvd);
335 inbound_stats->header_bytes_received =
336 static_cast<uint64_t>(media_receiver_info.header_and_padding_bytes_rcvd);
hbos02cd4d62016-12-09 12:19:44337 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 11:26:04338 static_cast<int32_t>(media_receiver_info.packets_lost);
Byoungchan Lee899b29e2021-06-29 13:09:18339 inbound_stats->jitter_buffer_delay =
340 media_receiver_info.jitter_buffer_delay_seconds;
341 inbound_stats->jitter_buffer_emitted_count =
342 media_receiver_info.jitter_buffer_emitted_count;
Jakob Ivarssone54914a2021-07-01 09:16:05343 if (media_receiver_info.nacks_sent) {
344 inbound_stats->nack_count = *media_receiver_info.nacks_sent;
345 }
hboseeafe942016-11-01 10:00:17346}
347
Alessio Bazzicaf7b1b952021-03-23 16:23:04348std::unique_ptr<RTCInboundRTPStreamStats> CreateInboundAudioStreamStats(
hboseeafe942016-11-01 10:00:17349 const cricket::VoiceReceiverInfo& voice_receiver_info,
Alessio Bazzicaf7b1b952021-03-23 16:23:04350 const std::string& mid,
351 int64_t timestamp_us) {
352 auto inbound_audio = std::make_unique<RTCInboundRTPStreamStats>(
353 /*id=*/RTCInboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_AUDIO,
354 voice_receiver_info.ssrc()),
355 timestamp_us);
Jonas Olssona4d87372019-07-05 17:08:33356 SetInboundRTPStreamStatsFromMediaReceiverInfo(voice_receiver_info,
Alessio Bazzicaf7b1b952021-03-23 16:23:04357 inbound_audio.get());
hbos820f5782016-11-22 11:16:50358 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03359 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16360 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50361 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04362 mid, /*inbound=*/true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16363 }
Jonas Olssona4d87372019-07-05 17:08:33364 inbound_audio->jitter = static_cast<double>(voice_receiver_info.jitter_ms) /
365 rtc::kNumMillisecsPerSec;
Eldar Rello4e5bc9f2020-07-06 11:18:07366 inbound_audio->total_samples_received =
367 voice_receiver_info.total_samples_received;
368 inbound_audio->concealed_samples = voice_receiver_info.concealed_samples;
369 inbound_audio->silent_concealed_samples =
370 voice_receiver_info.silent_concealed_samples;
371 inbound_audio->concealment_events = voice_receiver_info.concealment_events;
372 inbound_audio->inserted_samples_for_deceleration =
373 voice_receiver_info.inserted_samples_for_deceleration;
374 inbound_audio->removed_samples_for_acceleration =
375 voice_receiver_info.removed_samples_for_acceleration;
Philipp Hanckeaa83cc72020-10-27 08:50:36376 if (voice_receiver_info.audio_level >= 0) {
377 inbound_audio->audio_level =
378 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
379 }
Eldar Rello4e5bc9f2020-07-06 11:18:07380 inbound_audio->total_audio_energy = voice_receiver_info.total_output_energy;
381 inbound_audio->total_samples_duration =
382 voice_receiver_info.total_output_duration;
Artem Titov880fa812021-07-30 20:30:23383 // `fir_count`, `pli_count` and `sli_count` are only valid for video and are
hbos820f5782016-11-22 11:16:50384 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 15:32:00385 if (voice_receiver_info.last_packet_received_timestamp_ms) {
Alessio Bazzicac366d512021-03-22 14:36:53386 inbound_audio->last_packet_received_timestamp = static_cast<double>(
387 *voice_receiver_info.last_packet_received_timestamp_ms);
Henrik Boström01738c62019-04-15 15:32:00388 }
Åsa Perssonfcf79cc2019-10-22 13:23:44389 if (voice_receiver_info.estimated_playout_ntp_timestamp_ms) {
Alessio Bazzica5cf8c2c2021-03-24 07:51:26390 // TODO(bugs.webrtc.org/10529): Fix time origin.
Åsa Perssonfcf79cc2019-10-22 13:23:44391 inbound_audio->estimated_playout_timestamp = static_cast<double>(
392 *voice_receiver_info.estimated_playout_ntp_timestamp_ms);
393 }
Ivo Creusen8d8ffdb2019-04-30 07:45:21394 inbound_audio->fec_packets_received =
395 voice_receiver_info.fec_packets_received;
396 inbound_audio->fec_packets_discarded =
397 voice_receiver_info.fec_packets_discarded;
Minyue Li28a2c632021-07-07 13:53:38398 inbound_audio->packets_discarded = voice_receiver_info.packets_discarded;
Alessio Bazzicaf7b1b952021-03-23 16:23:04399 return inbound_audio;
400}
401
402std::unique_ptr<RTCRemoteOutboundRtpStreamStats>
403CreateRemoteOutboundAudioStreamStats(
404 const cricket::VoiceReceiverInfo& voice_receiver_info,
405 const std::string& mid,
406 const std::string& inbound_audio_id,
407 const std::string& transport_id) {
408 if (!voice_receiver_info.last_sender_report_timestamp_ms.has_value()) {
409 // Cannot create `RTCRemoteOutboundRtpStreamStats` when the RTCP SR arrival
410 // timestamp is not available - i.e., until the first sender report is
411 // received.
412 return nullptr;
413 }
414 RTC_DCHECK_GT(voice_receiver_info.sender_reports_reports_count, 0);
415
416 // Create.
417 auto stats = std::make_unique<RTCRemoteOutboundRtpStreamStats>(
418 /*id=*/RTCRemoteOutboundRTPStreamStatsIDFromSSRC(
419 cricket::MEDIA_TYPE_AUDIO, voice_receiver_info.ssrc()),
420 /*timestamp_us=*/rtc::kNumMicrosecsPerMillisec *
421 voice_receiver_info.last_sender_report_timestamp_ms.value());
422
423 // Populate.
424 // - RTCRtpStreamStats.
425 stats->ssrc = voice_receiver_info.ssrc();
426 stats->kind = "audio";
427 stats->transport_id = transport_id;
428 stats->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
429 mid,
430 /*inbound=*/true, // Remote-outbound same as local-inbound.
431 *voice_receiver_info.codec_payload_type);
432 // - RTCSentRtpStreamStats.
433 stats->packets_sent = voice_receiver_info.sender_reports_packets_sent;
434 stats->bytes_sent = voice_receiver_info.sender_reports_bytes_sent;
435 // - RTCRemoteOutboundRtpStreamStats.
436 stats->local_id = inbound_audio_id;
437 RTC_DCHECK(
438 voice_receiver_info.last_sender_report_remote_timestamp_ms.has_value());
439 stats->remote_timestamp = static_cast<double>(
440 voice_receiver_info.last_sender_report_remote_timestamp_ms.value());
441 stats->reports_sent = voice_receiver_info.sender_reports_reports_count;
Ivo Creusen2562cf02021-09-03 14:51:22442 if (voice_receiver_info.round_trip_time) {
443 stats->round_trip_time =
444 voice_receiver_info.round_trip_time->seconds<double>();
445 }
446 stats->round_trip_time_measurements =
447 voice_receiver_info.round_trip_time_measurements;
448 stats->total_round_trip_time =
449 voice_receiver_info.total_round_trip_time.seconds<double>();
Alessio Bazzicaf7b1b952021-03-23 16:23:04450
451 return stats;
hboseeafe942016-11-01 10:00:17452}
453
454void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 23:19:50455 const std::string& mid,
hboseeafe942016-11-01 10:00:17456 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 11:16:50457 RTCInboundRTPStreamStats* inbound_video) {
Jonas Olssona4d87372019-07-05 17:08:33458 SetInboundRTPStreamStatsFromMediaReceiverInfo(video_receiver_info,
459 inbound_video);
hbos820f5782016-11-22 11:16:50460 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03461 inbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16462 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50463 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04464 mid, /*inbound=*/true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16465 }
Di Wu (RP Room Eng)8af6b492021-02-20 01:34:22466 inbound_video->jitter = static_cast<double>(video_receiver_info.jitter_ms) /
467 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 11:16:50468 inbound_video->fir_count =
469 static_cast<uint32_t>(video_receiver_info.firs_sent);
470 inbound_video->pli_count =
471 static_cast<uint32_t>(video_receiver_info.plis_sent);
Eldar Rello4e5bc9f2020-07-06 11:18:07472 inbound_video->frames_received = video_receiver_info.frames_received;
hbos6769c492017-01-02 16:35:13473 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
Eldar Rello4e5bc9f2020-07-06 11:18:07474 inbound_video->frames_dropped = video_receiver_info.frames_dropped;
Rasmus Brandt2efae772019-06-27 12:29:34475 inbound_video->key_frames_decoded = video_receiver_info.key_frames_decoded;
Eldar Rello4e5bc9f2020-07-06 11:18:07476 if (video_receiver_info.frame_width > 0) {
477 inbound_video->frame_width =
478 static_cast<uint32_t>(video_receiver_info.frame_width);
479 }
480 if (video_receiver_info.frame_height > 0) {
481 inbound_video->frame_height =
482 static_cast<uint32_t>(video_receiver_info.frame_height);
483 }
484 if (video_receiver_info.framerate_rcvd > 0) {
485 inbound_video->frames_per_second = video_receiver_info.framerate_rcvd;
486 }
hbosa51d4f32017-02-16 13:34:48487 if (video_receiver_info.qp_sum)
488 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Johannes Kronbfd343b2019-07-01 08:07:50489 inbound_video->total_decode_time =
490 static_cast<double>(video_receiver_info.total_decode_time_ms) /
491 rtc::kNumMillisecsPerSec;
Johannes Kron00376e12019-11-25 09:25:42492 inbound_video->total_inter_frame_delay =
493 video_receiver_info.total_inter_frame_delay;
494 inbound_video->total_squared_inter_frame_delay =
495 video_receiver_info.total_squared_inter_frame_delay;
Henrik Boström01738c62019-04-15 15:32:00496 if (video_receiver_info.last_packet_received_timestamp_ms) {
Alessio Bazzica5cf8c2c2021-03-24 07:51:26497 inbound_video->last_packet_received_timestamp = static_cast<double>(
498 *video_receiver_info.last_packet_received_timestamp_ms);
Henrik Boström01738c62019-04-15 15:32:00499 }
Åsa Perssonfcf79cc2019-10-22 13:23:44500 if (video_receiver_info.estimated_playout_ntp_timestamp_ms) {
Alessio Bazzica5cf8c2c2021-03-24 07:51:26501 // TODO(bugs.webrtc.org/10529): Fix time origin if needed.
Åsa Perssonfcf79cc2019-10-22 13:23:44502 inbound_video->estimated_playout_timestamp = static_cast<double>(
503 *video_receiver_info.estimated_playout_ntp_timestamp_ms);
504 }
Artem Titov880fa812021-07-30 20:30:23505 // TODO(bugs.webrtc.org/10529): When info's `content_info` is optional
Alessio Bazzica5cf8c2c2021-03-24 07:51:26506 // support the "unspecified" value.
Henrik Boström2e069262019-04-09 11:59:31507 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
508 inbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 11:09:51509 if (!video_receiver_info.decoder_implementation_name.empty()) {
510 inbound_video->decoder_implementation =
511 video_receiver_info.decoder_implementation_name;
512 }
hboseeafe942016-11-01 10:00:17513}
514
hbos820f5782016-11-22 11:16:50515// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 08:50:46516void SetOutboundRTPStreamStatsFromMediaSenderInfo(
517 const cricket::MediaSenderInfo& media_sender_info,
518 RTCOutboundRTPStreamStats* outbound_stats) {
519 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 14:28:11520 outbound_stats->ssrc = media_sender_info.ssrc();
hbos6ded1902016-11-01 08:50:46521 outbound_stats->packets_sent =
522 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53523 outbound_stats->retransmitted_packets_sent =
524 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 08:50:46525 outbound_stats->bytes_sent =
Niels Möllerac0a4cb2019-10-09 13:01:33526 static_cast<uint64_t>(media_sender_info.payload_bytes_sent);
527 outbound_stats->header_bytes_sent =
528 static_cast<uint64_t>(media_sender_info.header_and_padding_bytes_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53529 outbound_stats->retransmitted_bytes_sent =
530 media_sender_info.retransmitted_bytes_sent;
Jakob Ivarssone91c9922021-07-06 07:55:43531 outbound_stats->nack_count = media_sender_info.nacks_rcvd;
hbos6ded1902016-11-01 08:50:46532}
533
534void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 23:19:50535 const std::string& mid,
hbos6ded1902016-11-01 08:50:46536 const cricket::VoiceSenderInfo& voice_sender_info,
537 RTCOutboundRTPStreamStats* outbound_audio) {
Jonas Olssona4d87372019-07-05 17:08:33538 SetOutboundRTPStreamStatsFromMediaSenderInfo(voice_sender_info,
539 outbound_audio);
hbos6ded1902016-11-01 08:50:46540 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03541 outbound_audio->kind = "audio";
Jakob Ivarssonbf087452021-11-11 12:43:49542 if (voice_sender_info.target_bitrate > 0) {
543 outbound_audio->target_bitrate = voice_sender_info.target_bitrate;
544 }
hbos585a9b12017-02-07 12:59:16545 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50546 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04547 mid, /*inbound=*/false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16548 }
Artem Titov880fa812021-07-30 20:30:23549 // `fir_count`, `pli_count` and `sli_count` are only valid for video and are
hbos6ded1902016-11-01 08:50:46550 // purposefully left undefined for audio.
551}
552
553void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 23:19:50554 const std::string& mid,
hbos6ded1902016-11-01 08:50:46555 const cricket::VideoSenderInfo& video_sender_info,
556 RTCOutboundRTPStreamStats* outbound_video) {
Jonas Olssona4d87372019-07-05 17:08:33557 SetOutboundRTPStreamStatsFromMediaSenderInfo(video_sender_info,
558 outbound_video);
hbos6ded1902016-11-01 08:50:46559 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03560 outbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16561 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50562 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04563 mid, /*inbound=*/false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16564 }
hbos6ded1902016-11-01 08:50:46565 outbound_video->fir_count =
566 static_cast<uint32_t>(video_sender_info.firs_rcvd);
567 outbound_video->pli_count =
568 static_cast<uint32_t>(video_sender_info.plis_rcvd);
hbos6769c492017-01-02 16:35:13569 if (video_sender_info.qp_sum)
570 outbound_video->qp_sum = *video_sender_info.qp_sum;
571 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Rasmus Brandt2efae772019-06-27 12:29:34572 outbound_video->key_frames_encoded = video_sender_info.key_frames_encoded;
Henrik Boströmf71362f2019-04-08 14:14:23573 outbound_video->total_encode_time =
574 static_cast<double>(video_sender_info.total_encode_time_ms) /
575 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 13:15:38576 outbound_video->total_encoded_bytes_target =
577 video_sender_info.total_encoded_bytes_target;
Eldar Rello9276e2c2020-06-10 14:53:39578 if (video_sender_info.send_frame_width > 0) {
579 outbound_video->frame_width =
580 static_cast<uint32_t>(video_sender_info.send_frame_width);
Henrik Boströma0ff50c2020-05-05 13:54:46581 }
Eldar Rello9276e2c2020-06-10 14:53:39582 if (video_sender_info.send_frame_height > 0) {
583 outbound_video->frame_height =
584 static_cast<uint32_t>(video_sender_info.send_frame_height);
585 }
586 if (video_sender_info.framerate_sent > 0) {
587 outbound_video->frames_per_second = video_sender_info.framerate_sent;
588 }
589 outbound_video->frames_sent = video_sender_info.frames_sent;
590 outbound_video->huge_frames_sent = video_sender_info.huge_frames_sent;
Henrik Boström9fe18342019-05-16 16:38:20591 outbound_video->total_packet_send_delay =
592 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
593 rtc::kNumMillisecsPerSec;
Henrik Boströmce33b6a2019-05-28 15:42:38594 outbound_video->quality_limitation_reason =
595 QualityLimitationReasonToRTCQualityLimitationReason(
596 video_sender_info.quality_limitation_reason);
Byoungchan Lee7d235352021-05-28 12:32:04597 outbound_video->quality_limitation_durations =
598 QualityLimitationDurationToRTCQualityLimitationDuration(
599 video_sender_info.quality_limitation_durations_ms);
Evan Shrubsolecc62b162019-09-09 09:26:45600 outbound_video->quality_limitation_resolution_changes =
601 video_sender_info.quality_limitation_resolution_changes;
Artem Titov880fa812021-07-30 20:30:23602 // TODO(https://crbug.com/webrtc/10529): When info's `content_info` is
Henrik Boström2e069262019-04-09 11:59:31603 // optional, support the "unspecified" value.
604 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
605 outbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 11:09:51606 if (!video_sender_info.encoder_implementation_name.empty()) {
607 outbound_video->encoder_implementation =
608 video_sender_info.encoder_implementation_name;
609 }
Henrik Boströma0ff50c2020-05-05 13:54:46610 if (video_sender_info.rid) {
611 outbound_video->rid = *video_sender_info.rid;
612 }
hbos6ded1902016-11-01 08:50:46613}
614
Henrik Boström883eefc2019-05-27 11:40:25615std::unique_ptr<RTCRemoteInboundRtpStreamStats>
616ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
617 const ReportBlockData& report_block_data,
618 cricket::MediaType media_type,
Eldar Relloc07e9042020-07-03 08:08:07619 const std::map<std::string, RTCOutboundRTPStreamStats*>& outbound_rtps,
Henrik Boström883eefc2019-05-27 11:40:25620 const RTCStatsReport& report) {
621 const auto& report_block = report_block_data.report_block();
622 // RTCStats' timestamp generally refers to when the metric was sampled, but
623 // for "remote-[outbound/inbound]-rtp" it refers to the local time when the
624 // Report Block was received.
Mirko Bonadei317a1f02019-09-17 15:06:18625 auto remote_inbound = std::make_unique<RTCRemoteInboundRtpStreamStats>(
Henrik Boström8605fbf2019-06-24 14:44:51626 RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(media_type,
627 report_block.source_ssrc),
Henrik Boström883eefc2019-05-27 11:40:25628 /*timestamp=*/report_block_data.report_block_timestamp_utc_us());
Henrik Boström8605fbf2019-06-24 14:44:51629 remote_inbound->ssrc = report_block.source_ssrc;
Henrik Boström883eefc2019-05-27 11:40:25630 remote_inbound->kind =
631 media_type == cricket::MEDIA_TYPE_AUDIO ? "audio" : "video";
632 remote_inbound->packets_lost = report_block.packets_lost;
Di Wu86f04ad2021-03-01 07:36:03633 remote_inbound->fraction_lost =
634 static_cast<double>(report_block.fraction_lost) / (1 << 8);
Henrik Boström883eefc2019-05-27 11:40:25635 remote_inbound->round_trip_time =
636 static_cast<double>(report_block_data.last_rtt_ms()) /
637 rtc::kNumMillisecsPerSec;
Di Wu88a51b22021-03-01 19:22:06638 remote_inbound->total_round_trip_time =
639 static_cast<double>(report_block_data.sum_rtt_ms()) /
640 rtc::kNumMillisecsPerSec;
641 remote_inbound->round_trip_time_measurements =
642 report_block_data.num_rtts();
Henrik Boström883eefc2019-05-27 11:40:25643
Alessio Bazzicaf7b1b952021-03-23 16:23:04644 std::string local_id =
645 RTCOutboundRTPStreamStatsIDFromSSRC(media_type, report_block.source_ssrc);
Artem Titov880fa812021-07-30 20:30:23646 // Look up local stat from `outbound_rtps` where the pointers are non-const.
Henrik Boström4f40fa52019-12-19 12:27:27647 auto local_id_it = outbound_rtps.find(local_id);
648 if (local_id_it != outbound_rtps.end()) {
Henrik Boström883eefc2019-05-27 11:40:25649 remote_inbound->local_id = local_id;
Henrik Boström4f40fa52019-12-19 12:27:27650 auto& outbound_rtp = *local_id_it->second;
651 outbound_rtp.remote_id = remote_inbound->id();
Henrik Boström883eefc2019-05-27 11:40:25652 // The RTP/RTCP transport is obtained from the
653 // RTCOutboundRtpStreamStats's transport.
654 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
655 ? report.Get(*outbound_rtp.transport_id)
656 : nullptr;
657 if (transport_from_id) {
658 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
659 // If RTP and RTCP are not multiplexed, there is a separate RTCP
660 // transport paired with the RTP transport, otherwise the same
661 // transport is used for RTCP and RTP.
662 remote_inbound->transport_id =
663 transport.rtcp_transport_stats_id.is_defined()
664 ? *transport.rtcp_transport_stats_id
665 : *outbound_rtp.transport_id;
666 }
667 // We're assuming the same codec is used on both ends. However if the
668 // codec is switched out on the fly we may have received a Report Block
669 // based on the previous codec and there is no way to tell which point in
670 // time the codec changed for the remote end.
671 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
672 ? report.Get(*outbound_rtp.codec_id)
673 : nullptr;
674 if (codec_from_id) {
675 remote_inbound->codec_id = *outbound_rtp.codec_id;
676 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
677 if (codec.clock_rate.is_defined()) {
678 // The Report Block jitter is expressed in RTP timestamp units
679 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
680 // to seconds we divide by the codec's clock rate.
681 remote_inbound->jitter =
682 static_cast<double>(report_block.jitter) / *codec.clock_rate;
683 }
684 }
685 }
686 return remote_inbound;
687}
688
hbos02ba2112016-10-28 12:14:53689void ProduceCertificateStatsFromSSLCertificateStats(
Jonas Olssona4d87372019-07-05 17:08:33690 int64_t timestamp_us,
691 const rtc::SSLCertificateStats& certificate_stats,
hbos02ba2112016-10-28 12:14:53692 RTCStatsReport* report) {
693 RTCCertificateStats* prev_certificate_stats = nullptr;
694 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
695 s = s->issuer.get()) {
hbos02d2a922016-12-21 09:29:05696 std::string certificate_stats_id =
697 RTCCertificateIDFromFingerprint(s->fingerprint);
698 // It is possible for the same certificate to show up multiple times, e.g.
699 // if local and remote side use the same certificate in a loopback call.
700 // If the report already contains stats for this certificate, skip it.
701 if (report->Get(certificate_stats_id)) {
702 RTC_DCHECK_EQ(s, &certificate_stats);
703 break;
704 }
Jonas Olssona4d87372019-07-05 17:08:33705 RTCCertificateStats* certificate_stats =
706 new RTCCertificateStats(certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 12:14:53707 certificate_stats->fingerprint = s->fingerprint;
708 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
709 certificate_stats->base64_certificate = s->base64_certificate;
710 if (prev_certificate_stats)
711 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
712 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
713 prev_certificate_stats = certificate_stats;
714 }
715}
716
Jonas Olssona4d87372019-07-05 17:08:33717const std::string& ProduceIceCandidateStats(int64_t timestamp_us,
718 const cricket::Candidate& candidate,
719 bool is_local,
720 const std::string& transport_id,
721 RTCStatsReport* report) {
hbos02ba2112016-10-28 12:14:53722 const std::string& id = "RTCIceCandidate_" + candidate.id();
723 const RTCStats* stats = report->Get(id);
724 if (!stats) {
725 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
726 if (is_local)
727 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
728 else
729 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 17:59:31730 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 18:49:36731 if (is_local) {
732 candidate_stats->network_type =
733 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke21c4b1e2021-11-11 06:45:59734 const std::string& candidate_type = candidate.type();
735 const std::string& relay_protocol = candidate.relay_protocol();
736 if (candidate_type == cricket::RELAY_PORT_TYPE ||
737 (candidate_type == cricket::PRFLX_PORT_TYPE &&
738 !relay_protocol.empty())) {
Philipp Hancke95513752018-09-27 12:40:08739 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
740 relay_protocol.compare("tcp") == 0 ||
741 relay_protocol.compare("tls") == 0);
742 candidate_stats->relay_protocol = relay_protocol;
743 }
Gary Liu37e489c2017-11-21 18:49:36744 } else {
745 // We don't expect to know the adapter type of remote candidates.
746 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
747 }
hbos02ba2112016-10-28 12:14:53748 candidate_stats->ip = candidate.address().ipaddr().ToString();
Philipp Hanckea9ba4502021-03-22 12:22:54749 candidate_stats->address = candidate.address().ipaddr().ToString();
hbos02ba2112016-10-28 12:14:53750 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
751 candidate_stats->protocol = candidate.protocol();
Jonas Olssona4d87372019-07-05 17:08:33752 candidate_stats->candidate_type =
753 CandidateTypeToRTCIceCandidateType(candidate.type());
hbos02ba2112016-10-28 12:14:53754 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
755
756 stats = candidate_stats.get();
757 report->AddStats(std::move(candidate_stats));
758 }
759 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
760 : RTCRemoteIceCandidateStats::kType);
761 return stats->id();
762}
763
Taylor Brandstetter64851c02021-06-24 20:32:50764template <typename StatsType>
765void SetAudioProcessingStats(StatsType* stats,
766 const AudioProcessingStats& apm_stats) {
767 if (apm_stats.echo_return_loss) {
768 stats->echo_return_loss = *apm_stats.echo_return_loss;
769 }
770 if (apm_stats.echo_return_loss_enhancement) {
771 stats->echo_return_loss_enhancement =
772 *apm_stats.echo_return_loss_enhancement;
773 }
774}
775
hbos9e302742017-01-20 10:47:10776std::unique_ptr<RTCMediaStreamTrackStats>
777ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
778 int64_t timestamp_us,
Taylor Brandstetter64851c02021-06-24 20:32:50779 AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19780 const cricket::VoiceSenderInfo& voice_sender_info,
781 int attachment_id) {
hbos9e302742017-01-20 10:47:10782 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
783 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58784 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
785 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19786 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10787 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
788 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42789 audio_track_stats->media_source_id =
790 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
791 attachment_id);
hbos9e302742017-01-20 10:47:10792 audio_track_stats->remote_source = false;
793 audio_track_stats->detached = false;
Taylor Brandstetter64851c02021-06-24 20:32:50794 // Audio processor may be attached to either the track or the send
795 // stream, so look in both places.
796 SetAudioProcessingStats(audio_track_stats.get(),
797 voice_sender_info.apm_statistics);
798 auto audio_processor(audio_track.GetAudioProcessor());
799 if (audio_processor.get()) {
Artem Titov880fa812021-07-30 20:30:23800 // The `has_remote_tracks` argument is obsolete; makes no difference if it's
Taylor Brandstetter64851c02021-06-24 20:32:50801 // set to true or false.
802 AudioProcessorInterface::AudioProcessorStatistics ap_stats =
803 audio_processor->GetStats(/*has_remote_tracks=*/false);
804 SetAudioProcessingStats(audio_track_stats.get(), ap_stats.apm_statistics);
hbos9e302742017-01-20 10:47:10805 }
806 return audio_track_stats;
807}
808
809std::unique_ptr<RTCMediaStreamTrackStats>
810ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
811 int64_t timestamp_us,
812 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19813 const cricket::VoiceReceiverInfo& voice_receiver_info,
814 int attachment_id) {
815 // Since receiver tracks can't be reattached, we use the SSRC as
816 // an attachment identifier.
hbos9e302742017-01-20 10:47:10817 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
818 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58819 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
820 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19821 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10822 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
823 audio_track, audio_track_stats.get());
824 audio_track_stats->remote_source = true;
825 audio_track_stats->detached = false;
826 if (voice_receiver_info.audio_level >= 0) {
Jonas Olssona4d87372019-07-05 17:08:33827 audio_track_stats->audio_level =
828 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
hbos9e302742017-01-20 10:47:10829 }
Gustaf Ullbergb0a02072017-10-02 10:00:34830 audio_track_stats->jitter_buffer_delay =
831 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 14:46:29832 audio_track_stats->jitter_buffer_emitted_count =
833 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 07:45:21834 audio_track_stats->inserted_samples_for_deceleration =
835 voice_receiver_info.inserted_samples_for_deceleration;
836 audio_track_stats->removed_samples_for_acceleration =
837 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 19:17:49838 audio_track_stats->total_audio_energy =
839 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-25 00:15:13840 audio_track_stats->total_samples_received =
841 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 19:17:49842 audio_track_stats->total_samples_duration =
843 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-25 00:15:13844 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 07:45:21845 audio_track_stats->silent_concealed_samples =
846 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 07:28:20847 audio_track_stats->concealment_events =
848 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 16:21:10849 audio_track_stats->jitter_buffer_flushes =
850 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 11:52:16851 audio_track_stats->delayed_packet_outage_samples =
852 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 08:18:40853 audio_track_stats->relative_packet_arrival_delay =
854 voice_receiver_info.relative_packet_arrival_delay_seconds;
Artem Titove618cc92020-03-11 10:18:54855 audio_track_stats->jitter_buffer_target_delay =
856 voice_receiver_info.jitter_buffer_target_delay_seconds;
Henrik Lundin44125fa2019-04-29 15:00:46857 audio_track_stats->interruption_count =
858 voice_receiver_info.interruption_count >= 0
859 ? voice_receiver_info.interruption_count
860 : 0;
861 audio_track_stats->total_interruption_duration =
862 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
863 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 10:47:10864 return audio_track_stats;
865}
866
867std::unique_ptr<RTCMediaStreamTrackStats>
868ProduceMediaStreamTrackStatsFromVideoSenderInfo(
869 int64_t timestamp_us,
870 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19871 const cricket::VideoSenderInfo& video_sender_info,
872 int attachment_id) {
hbos9e302742017-01-20 10:47:10873 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
874 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58875 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 08:18:58876 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19877 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10878 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
879 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42880 video_track_stats->media_source_id =
881 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
882 attachment_id);
hbos9e302742017-01-20 10:47:10883 video_track_stats->remote_source = false;
884 video_track_stats->detached = false;
Jonas Olssona4d87372019-07-05 17:08:33885 video_track_stats->frame_width =
886 static_cast<uint32_t>(video_sender_info.send_frame_width);
887 video_track_stats->frame_height =
888 static_cast<uint32_t>(video_sender_info.send_frame_height);
hbosfefe0762017-01-20 14:14:25889 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 13:08:34890 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 14:14:25891 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 15:35:03892 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 10:47:10893 return video_track_stats;
894}
895
896std::unique_ptr<RTCMediaStreamTrackStats>
897ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
898 int64_t timestamp_us,
899 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19900 const cricket::VideoReceiverInfo& video_receiver_info,
901 int attachment_id) {
hbos9e302742017-01-20 10:47:10902 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
903 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58904 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
905
906 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19907 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10908 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
909 video_track, video_track_stats.get());
910 video_track_stats->remote_source = true;
911 video_track_stats->detached = false;
912 if (video_receiver_info.frame_width > 0 &&
913 video_receiver_info.frame_height > 0) {
Jonas Olssona4d87372019-07-05 17:08:33914 video_track_stats->frame_width =
915 static_cast<uint32_t>(video_receiver_info.frame_width);
916 video_track_stats->frame_height =
917 static_cast<uint32_t>(video_receiver_info.frame_height);
hbos9e302742017-01-20 10:47:10918 }
Guido Urdaneta67378412019-05-28 15:38:08919 video_track_stats->jitter_buffer_delay =
920 video_receiver_info.jitter_buffer_delay_seconds;
921 video_track_stats->jitter_buffer_emitted_count =
922 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 11:56:50923 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 15:39:09924 // TODO(hbos): When we support receiving simulcast, this should be the total
925 // number of frames correctly decoded, independent of which SSRC it was
926 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 13:08:34927 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 15:39:09928 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
Johannes Kron0c141c52019-08-26 13:04:43929 video_track_stats->frames_dropped = video_receiver_info.frames_dropped;
Sergey Silkin02371062019-01-31 15:45:42930 video_track_stats->freeze_count = video_receiver_info.freeze_count;
931 video_track_stats->pause_count = video_receiver_info.pause_count;
932 video_track_stats->total_freezes_duration =
933 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
934 rtc::kNumMillisecsPerSec;
935 video_track_stats->total_pauses_duration =
936 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
937 rtc::kNumMillisecsPerSec;
938 video_track_stats->total_frames_duration =
939 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
940 rtc::kNumMillisecsPerSec;
941 video_track_stats->sum_squared_frame_durations =
942 video_receiver_info.sum_squared_frame_durations;
943
hbos9e302742017-01-20 10:47:10944 return video_track_stats;
945}
946
Harald Alvestrand89061872018-01-02 13:08:34947void ProduceSenderMediaTrackStats(
948 int64_t timestamp_us,
949 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50950 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 13:08:34951 RTCStatsReport* report) {
952 // This function iterates over the senders to generate outgoing track stats.
953
954 // TODO(hbos): Return stats of detached tracks. We have to perform stats
955 // gathering at the time of detachment to get accurate stats and timestamps.
956 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 16:29:42957 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 13:08:34958 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
959 AudioTrackInterface* track =
960 static_cast<AudioTrackInterface*>(sender->track().get());
961 if (!track)
962 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16963 cricket::VoiceSenderInfo null_sender_info;
964 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
965 // TODO(hta): Checking on ssrc is not proper. There should be a way
966 // to see from a sender whether it's connected or not.
967 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 23:19:50968 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29969 // When pc.close is called, sender info is discarded, so
970 // we generate zeroes instead. Bug: It should be retained.
971 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50972 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16973 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29974 if (sender_info) {
975 voice_sender_info = sender_info;
976 } else {
977 RTC_LOG(LS_INFO)
978 << "RTCStatsCollector: No voice sender info for sender with ssrc "
979 << sender->ssrc();
980 }
Harald Alvestrandb8e12012018-01-23 14:28:16981 }
Harald Alvestrand89061872018-01-02 13:08:34982 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19983 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
984 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34985 report->AddStats(std::move(audio_track_stats));
986 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
987 VideoTrackInterface* track =
988 static_cast<VideoTrackInterface*>(sender->track().get());
989 if (!track)
990 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16991 cricket::VideoSenderInfo null_sender_info;
992 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
993 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 13:43:29994 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
995 // "none")
Steve Anton57858b32018-02-15 23:19:50996 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29997 // When pc.close is called, sender info is discarded, so
998 // we generate zeroes instead. Bug: It should be retained.
999 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:501000 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:161001 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:291002 if (sender_info) {
1003 video_sender_info = sender_info;
1004 } else {
1005 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
1006 << sender->ssrc();
1007 }
1008 }
Harald Alvestrand89061872018-01-02 13:08:341009 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:191010 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
1011 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:341012 report->AddStats(std::move(video_track_stats));
1013 } else {
Artem Titovd3251962021-11-15 15:57:071014 RTC_DCHECK_NOTREACHED();
Harald Alvestrand89061872018-01-02 13:08:341015 }
1016 }
1017}
1018
1019void ProduceReceiverMediaTrackStats(
1020 int64_t timestamp_us,
1021 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:501022 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 13:08:341023 RTCStatsReport* report) {
1024 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 16:29:421025 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 13:08:341026 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
1027 AudioTrackInterface* track =
1028 static_cast<AudioTrackInterface*>(receiver->track().get());
1029 const cricket::VoiceReceiverInfo* voice_receiver_info =
1030 track_media_info_map.GetVoiceReceiverInfo(*track);
1031 if (!voice_receiver_info) {
1032 continue;
1033 }
1034 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
1035 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:191036 timestamp_us, *track, *voice_receiver_info,
1037 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:341038 report->AddStats(std::move(audio_track_stats));
1039 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1040 VideoTrackInterface* track =
1041 static_cast<VideoTrackInterface*>(receiver->track().get());
1042 const cricket::VideoReceiverInfo* video_receiver_info =
1043 track_media_info_map.GetVideoReceiverInfo(*track);
1044 if (!video_receiver_info) {
1045 continue;
1046 }
1047 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
1048 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:191049 timestamp_us, *track, *video_receiver_info,
1050 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:341051 report->AddStats(std::move(video_track_stats));
1052 } else {
Artem Titovd3251962021-11-15 15:57:071053 RTC_DCHECK_NOTREACHED();
Harald Alvestrand89061872018-01-02 13:08:341054 }
1055 }
1056}
1057
Henrik Boström5b3541f2018-03-19 12:52:561058rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
1059 bool filter_by_sender_selector,
1060 rtc::scoped_refptr<const RTCStatsReport> report,
1061 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
1062 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
1063 std::vector<std::string> rtpstream_ids;
1064 if (filter_by_sender_selector) {
1065 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
1066 if (sender_selector) {
1067 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
1068 // reference the sender stats.
1069 // Because we do not implement sender stats, we look at outbound-rtp(s)
1070 // that reference the track attachment stats for the sender instead.
1071 std::string track_id =
1072 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1073 kSender, sender_selector->AttachmentId());
1074 for (const auto& stats : *report) {
1075 if (stats.type() != RTCOutboundRTPStreamStats::kType)
1076 continue;
1077 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
1078 if (outbound_rtp.track_id.is_defined() &&
1079 *outbound_rtp.track_id == track_id) {
1080 rtpstream_ids.push_back(outbound_rtp.id());
1081 }
1082 }
1083 }
1084 } else {
1085 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
1086 if (receiver_selector) {
1087 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
1088 // reference the receiver stats.
1089 // Because we do not implement receiver stats, we look at inbound-rtp(s)
1090 // that reference the track attachment stats for the receiver instead.
1091 std::string track_id =
1092 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1093 kReceiver, receiver_selector->AttachmentId());
1094 for (const auto& stats : *report) {
1095 if (stats.type() != RTCInboundRTPStreamStats::kType)
1096 continue;
1097 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
1098 if (inbound_rtp.track_id.is_defined() &&
1099 *inbound_rtp.track_id == track_id) {
1100 rtpstream_ids.push_back(inbound_rtp.id());
1101 }
1102 }
1103 }
1104 }
1105 if (rtpstream_ids.empty())
1106 return RTCStatsReport::Create(report->timestamp_us());
1107 return TakeReferencedStats(report->Copy(), rtpstream_ids);
1108}
1109
hboscc555c52016-10-18 19:48:311110} // namespace
1111
Henrik Boström5b3541f2018-03-19 12:52:561112RTCStatsCollector::RequestInfo::RequestInfo(
1113 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1114 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
1115
1116RTCStatsCollector::RequestInfo::RequestInfo(
1117 rtc::scoped_refptr<RtpSenderInternal> selector,
1118 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1119 : RequestInfo(FilterMode::kSenderSelector,
1120 std::move(callback),
1121 std::move(selector),
1122 nullptr) {}
1123
1124RTCStatsCollector::RequestInfo::RequestInfo(
1125 rtc::scoped_refptr<RtpReceiverInternal> selector,
1126 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1127 : RequestInfo(FilterMode::kReceiverSelector,
1128 std::move(callback),
1129 nullptr,
1130 std::move(selector)) {}
1131
1132RTCStatsCollector::RequestInfo::RequestInfo(
1133 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
1134 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
1135 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
1136 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
1137 : filter_mode_(filter_mode),
1138 callback_(std::move(callback)),
1139 sender_selector_(std::move(sender_selector)),
1140 receiver_selector_(std::move(receiver_selector)) {
1141 RTC_DCHECK(callback_);
1142 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
1143}
1144
hbosc82f2e12016-09-05 08:36:501145rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-24 00:38:461146 PeerConnectionInternal* pc,
1147 int64_t cache_lifetime_us) {
Tommi87f70902021-04-27 12:43:081148 return rtc::make_ref_counted<RTCStatsCollector>(pc, cache_lifetime_us);
hbosc82f2e12016-09-05 08:36:501149}
1150
Steve Anton2d8609c2018-01-24 00:38:461151RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 08:36:501152 int64_t cache_lifetime_us)
hbosd565b732016-08-30 21:04:351153 : pc_(pc),
Steve Anton978b8762017-09-29 19:15:021154 signaling_thread_(pc->signaling_thread()),
1155 worker_thread_(pc->worker_thread()),
1156 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 08:36:501157 num_pending_partial_reports_(0),
1158 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 08:49:311159 network_report_event_(true /* manual_reset */,
1160 true /* initially_signaled */),
hbos0e6758d2016-08-31 14:57:361161 cache_timestamp_us_(0),
1162 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 21:04:351163 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 08:36:501164 RTC_DCHECK(signaling_thread_);
1165 RTC_DCHECK(worker_thread_);
1166 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 14:57:361167 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Taylor Brandstetter3a034e12020-07-09 22:32:341168 pc_->SignalSctpDataChannelCreated().connect(
1169 this, &RTCStatsCollector::OnSctpDataChannelCreated);
hbosd565b732016-08-30 21:04:351170}
1171
hbosb78306a2016-12-19 13:06:571172RTCStatsCollector::~RTCStatsCollector() {
1173 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1174}
1175
hbosc82f2e12016-09-05 08:36:501176void RTCStatsCollector::GetStatsReport(
1177 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 12:52:561178 GetStatsReportInternal(RequestInfo(std::move(callback)));
1179}
1180
1181void RTCStatsCollector::GetStatsReport(
1182 rtc::scoped_refptr<RtpSenderInternal> selector,
1183 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1184 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1185}
1186
1187void RTCStatsCollector::GetStatsReport(
1188 rtc::scoped_refptr<RtpReceiverInternal> selector,
1189 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1190 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1191}
1192
1193void RTCStatsCollector::GetStatsReportInternal(
1194 RTCStatsCollector::RequestInfo request) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051195 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boström5b3541f2018-03-19 12:52:561196 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 08:36:501197
hbos0e6758d2016-08-31 14:57:361198 // "Now" using a monotonically increasing timer.
1199 int64_t cache_now_us = rtc::TimeMicros();
1200 if (cached_report_ &&
1201 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 17:53:471202 // We have a fresh cached report to deliver. Deliver asynchronously, since
1203 // the caller may not be expecting a synchronous callback, and it avoids
1204 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 12:52:561205 std::vector<RequestInfo> requests;
1206 requests.swap(requests_);
Niels Möller4bab23f2021-01-18 08:24:331207
1208 // Task subclass to take ownership of the requests.
1209 // TODO(nisse): Delete when we can use C++14, and do lambda capture with
1210 // std::move.
1211 class DeliveryTask : public QueuedTask {
1212 public:
1213 DeliveryTask(rtc::scoped_refptr<RTCStatsCollector> collector,
1214 rtc::scoped_refptr<const RTCStatsReport> cached_report,
1215 std::vector<RequestInfo> requests)
1216 : collector_(collector),
1217 cached_report_(cached_report),
1218 requests_(std::move(requests)) {}
1219 bool Run() override {
1220 collector_->DeliverCachedReport(cached_report_, std::move(requests_));
1221 return true;
1222 }
1223
1224 private:
1225 rtc::scoped_refptr<RTCStatsCollector> collector_;
1226 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
1227 std::vector<RequestInfo> requests_;
1228 };
1229 signaling_thread_->PostTask(std::make_unique<DeliveryTask>(
Niels Möllere7cc8832022-01-04 14:20:031230 rtc::scoped_refptr<RTCStatsCollector>(this), cached_report_,
1231 std::move(requests)));
hbosc82f2e12016-09-05 08:36:501232 } else if (!num_pending_partial_reports_) {
1233 // Only start gathering stats if we're not already gathering stats. In the
Artem Titov880fa812021-07-30 20:30:231234 // case of already gathering stats, `callback_` will be invoked when there
hbosc82f2e12016-09-05 08:36:501235 // are no more pending partial reports.
1236
1237 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
1238 // UTC), in microseconds. The system clock could be modified and is not
1239 // necessarily monotonically increasing.
nissecdf37a92016-09-14 06:41:471240 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 08:36:501241
hbosf415f8a2017-01-02 12:28:511242 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 08:36:501243 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 12:58:021244
Artem Titov880fa812021-07-30 20:30:231245 // Prepare `transceiver_stats_infos_` and `call_stats_` for use in
1246 // `ProducePartialResultsOnNetworkThread` and
1247 // `ProducePartialResultsOnSignalingThread`.
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571248 PrepareTransceiverStatsInfosAndCallStats_s_w_n();
Artem Titov880fa812021-07-30 20:30:231249 // Don't touch `network_report_` on the signaling thread until
Henrik Boström40b030e2019-02-28 08:49:311250 // ProducePartialResultsOnNetworkThread() has signaled the
Artem Titov880fa812021-07-30 20:30:231251 // `network_report_event_`.
Henrik Boström40b030e2019-02-28 08:49:311252 network_report_event_.Reset();
Niels Möller4bab23f2021-01-18 08:24:331253 rtc::scoped_refptr<RTCStatsCollector> collector(this);
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571254 network_thread_->PostTask(
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571255 [collector, sctp_transport_name = pc_->sctp_transport_name(),
1256 timestamp_us]() mutable {
1257 collector->ProducePartialResultsOnNetworkThread(
1258 timestamp_us, std::move(sctp_transport_name));
1259 });
hbosf415f8a2017-01-02 12:28:511260 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 14:57:361261 }
hbosd565b732016-08-30 21:04:351262}
1263
1264void RTCStatsCollector::ClearCachedStatsReport() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051265 RTC_DCHECK_RUN_ON(signaling_thread_);
hbosd565b732016-08-30 21:04:351266 cached_report_ = nullptr;
1267}
1268
hbosb78306a2016-12-19 13:06:571269void RTCStatsCollector::WaitForPendingRequest() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051270 RTC_DCHECK_RUN_ON(signaling_thread_);
Artem Titov880fa812021-07-30 20:30:231271 // If a request is pending, blocks until the `network_report_event_` is
Henrik Boström40b030e2019-02-28 08:49:311272 // signaled and then delivers the result. Otherwise this is a NO-OP.
1273 MergeNetworkReport_s();
hbosb78306a2016-12-19 13:06:571274}
1275
hbosc82f2e12016-09-05 08:36:501276void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1277 int64_t timestamp_us) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051278 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501279 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1280
Henrik Boström40b030e2019-02-28 08:49:311281 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501282
Henrik Boström40b030e2019-02-28 08:49:311283 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1284 partial_report_.get());
hbosc82f2e12016-09-05 08:36:501285
Henrik Boström40b030e2019-02-28 08:49:311286 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1287 // signaling thread, so it is always the first partial result delivered on the
1288 // signaling thread. The request is not complete until MergeNetworkReport_s()
1289 // happens; we don't have to do anything here.
1290 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1291 --num_pending_partial_reports_;
1292}
1293
1294void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1295 int64_t timestamp_us,
1296 RTCStatsReport* partial_report) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051297 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501298 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1299
Henrik Boström40b030e2019-02-28 08:49:311300 ProduceDataChannelStats_s(timestamp_us, partial_report);
1301 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1302 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 13:49:421303 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311304 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 08:36:501305}
1306
hbosc82f2e12016-09-05 08:36:501307void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571308 int64_t timestamp_us,
1309 absl::optional<std::string> sctp_transport_name) {
Markus Handell518669d2021-06-07 11:30:461310 TRACE_EVENT0("webrtc",
1311 "RTCStatsCollector::ProducePartialResultsOnNetworkThread");
Tomas Gunnarsson67b1fa22021-04-08 13:18:051312 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501313 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1314
Artem Titov880fa812021-07-30 20:30:231315 // Touching `network_report_` on this thread is safe by this method because
1316 // `network_report_event_` is reset before this method is invoked.
Henrik Boström40b030e2019-02-28 08:49:311317 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501318
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571319 std::set<std::string> transport_names;
1320 if (sctp_transport_name) {
1321 transport_names.emplace(std::move(*sctp_transport_name));
1322 }
1323
1324 for (const auto& info : transceiver_stats_infos_) {
1325 if (info.transport_name)
1326 transport_names.insert(*info.transport_name);
1327 }
1328
Steve Anton5dfde182018-02-06 18:34:401329 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571330 pc_->GetTransportStatsByNames(transport_names);
Steve Anton5dfde182018-02-06 18:34:401331 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1332 PrepareTransportCertificateStats_n(transport_stats_by_name);
1333
Henrik Boström40b030e2019-02-28 08:49:311334 ProducePartialResultsOnNetworkThreadImpl(
1335 timestamp_us, transport_stats_by_name, transport_cert_stats,
1336 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:401337
Artem Titov880fa812021-07-30 20:30:231338 // Signal that it is now safe to touch `network_report_` on the signaling
Henrik Boström40b030e2019-02-28 08:49:311339 // thread, and post a task to merge it into the final results.
1340 network_report_event_.Set();
Niels Möller4bab23f2021-01-18 08:24:331341 rtc::scoped_refptr<RTCStatsCollector> collector(this);
Henrik Boström40b030e2019-02-28 08:49:311342 signaling_thread_->PostTask(
Henrik Boström2deee4b2022-01-20 10:58:051343 [collector] { collector->MergeNetworkReport_s(); });
Henrik Boström05d43c62019-02-15 09:23:081344}
1345
Henrik Boström40b030e2019-02-28 08:49:311346void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1347 int64_t timestamp_us,
1348 const std::map<std::string, cricket::TransportStats>&
1349 transport_stats_by_name,
1350 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1351 RTCStatsReport* partial_report) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051352 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501353 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1354
Henrik Boström40b030e2019-02-28 08:49:311355 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1356 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1357 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1358 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311359 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1360 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 11:40:251361 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1362 partial_report);
Henrik Boström40b030e2019-02-28 08:49:311363}
1364
1365void RTCStatsCollector::MergeNetworkReport_s() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051366 RTC_DCHECK_RUN_ON(signaling_thread_);
Artem Titov880fa812021-07-30 20:30:231367 // The `network_report_event_` must be signaled for it to be safe to touch
1368 // `network_report_`. This is normally not blocking, but if
Henrik Boström40b030e2019-02-28 08:49:311369 // WaitForPendingRequest() is called while a request is pending, we might have
Artem Titov880fa812021-07-30 20:30:231370 // to wait until the network thread is done touching `network_report_`.
Henrik Boström40b030e2019-02-28 08:49:311371 network_report_event_.Wait(rtc::Event::kForever);
1372 if (!network_report_) {
1373 // Normally, MergeNetworkReport_s() is executed because it is posted from
1374 // the network thread. But if WaitForPendingRequest() is called while a
1375 // request is pending, an early call to MergeNetworkReport_s() is made,
Artem Titov880fa812021-07-30 20:30:231376 // merging the report and setting `network_report_` to null. If so, when the
Henrik Boström40b030e2019-02-28 08:49:311377 // previously posted MergeNetworkReport_s() is later executed, the report is
1378 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 08:36:501379 return;
1380 }
Mirko Bonadeica890ee2019-02-15 21:10:401381 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 08:49:311382 RTC_DCHECK(partial_report_);
1383 partial_report_->TakeMembersFrom(network_report_);
1384 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:401385 --num_pending_partial_reports_;
Artem Titov880fa812021-07-30 20:30:231386 // `network_report_` is currently the only partial report collected
1387 // asynchronously, so `num_pending_partial_reports_` must now be 0 and we are
Henrik Boström40b030e2019-02-28 08:49:311388 // ready to deliver the result.
1389 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1390 cache_timestamp_us_ = partial_report_timestamp_us_;
1391 cached_report_ = partial_report_;
1392 partial_report_ = nullptr;
1393 transceiver_stats_infos_.clear();
1394 // Trace WebRTC Stats when getStats is called on Javascript.
1395 // This allows access to WebRTC stats from trace logs. To enable them,
1396 // select the "webrtc_stats" category when recording traces.
1397 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1398 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:401399
Artem Titov880fa812021-07-30 20:30:231400 // Deliver report and clear `requests_`.
Henrik Boström40b030e2019-02-28 08:49:311401 std::vector<RequestInfo> requests;
1402 requests.swap(requests_);
1403 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 08:36:501404}
1405
Taylor Brandstetter25e022f2018-03-08 17:53:471406void RTCStatsCollector::DeliverCachedReport(
1407 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 12:52:561408 std::vector<RTCStatsCollector::RequestInfo> requests) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051409 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boström5b3541f2018-03-19 12:52:561410 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 17:53:471411 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 17:42:251412
Henrik Boström5b3541f2018-03-19 12:52:561413 for (const RequestInfo& request : requests) {
1414 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1415 request.callback()->OnStatsDelivered(cached_report);
1416 } else {
1417 bool filter_by_sender_selector;
1418 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1419 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1420 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1421 filter_by_sender_selector = true;
1422 sender_selector = request.sender_selector();
1423 } else {
1424 RTC_DCHECK(request.filter_mode() ==
1425 RequestInfo::FilterMode::kReceiverSelector);
1426 filter_by_sender_selector = false;
1427 receiver_selector = request.receiver_selector();
1428 }
1429 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1430 filter_by_sender_selector, cached_report, sender_selector,
1431 receiver_selector));
1432 }
hbosc82f2e12016-09-05 08:36:501433 }
hbosd565b732016-08-30 21:04:351434}
1435
hbosdf6075a2016-12-19 12:58:021436void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 11:00:051437 int64_t timestamp_us,
1438 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce02016-10-03 21:16:561439 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051440 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501441 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1442
hbos02ba2112016-10-28 12:14:531443 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1444 if (transport_cert_stats_pair.second.local) {
1445 ProduceCertificateStatsFromSSLCertificateStats(
1446 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce02016-10-03 21:16:561447 }
hbos02ba2112016-10-28 12:14:531448 if (transport_cert_stats_pair.second.remote) {
1449 ProduceCertificateStatsFromSSLCertificateStats(
1450 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce02016-10-03 21:16:561451 }
1452 }
1453}
1454
hbosdf6075a2016-12-19 12:58:021455void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 23:19:501456 int64_t timestamp_us,
1457 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 10:32:061458 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051459 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501460 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1461
Steve Anton57858b32018-02-15 23:19:501462 for (const auto& stats : transceiver_stats_infos) {
1463 if (!stats.mid) {
1464 continue;
hbos0adb8282016-11-23 10:32:061465 }
Philipp Hancke95157a02020-11-16 19:08:271466 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1467 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1468
Steve Anton57858b32018-02-15 23:19:501469 const cricket::VoiceMediaInfo* voice_media_info =
1470 stats.track_media_info_map->voice_media_info();
1471 const cricket::VideoMediaInfo* video_media_info =
1472 stats.track_media_info_map->video_media_info();
1473 // Audio
1474 if (voice_media_info) {
1475 // Inbound
1476 for (const auto& pair : voice_media_info->receive_codecs) {
1477 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271478 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501479 }
1480 // Outbound
1481 for (const auto& pair : voice_media_info->send_codecs) {
1482 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271483 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501484 }
Guido Urdanetaee2388f2018-02-15 16:36:191485 }
Steve Anton57858b32018-02-15 23:19:501486 // Video
1487 if (video_media_info) {
1488 // Inbound
1489 for (const auto& pair : video_media_info->receive_codecs) {
1490 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271491 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501492 }
1493 // Outbound
1494 for (const auto& pair : video_media_info->send_codecs) {
1495 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271496 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501497 }
hbos0adb8282016-11-23 10:32:061498 }
1499 }
1500}
1501
hboscc555c52016-10-18 19:48:311502void RTCStatsCollector::ProduceDataChannelStats_s(
Jonas Olssona4d87372019-07-05 17:08:331503 int64_t timestamp_us,
1504 RTCStatsReport* report) const {
Tomas Gunnarsson2e94de52020-06-16 14:54:101505 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501506 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Taylor Brandstetter3a034e12020-07-09 22:32:341507 std::vector<DataChannelStats> data_stats = pc_->GetDataChannelStats();
Tomas Gunnarsson2e94de52020-06-16 14:54:101508 for (const auto& stats : data_stats) {
hboscc555c52016-10-18 19:48:311509 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1510 new RTCDataChannelStats(
Tomas Gunnarsson2e94de52020-06-16 14:54:101511 "RTCDataChannel_" + rtc::ToString(stats.internal_id),
hboscc555c52016-10-18 19:48:311512 timestamp_us));
Tomas Gunnarsson2e94de52020-06-16 14:54:101513 data_channel_stats->label = std::move(stats.label);
1514 data_channel_stats->protocol = std::move(stats.protocol);
1515 data_channel_stats->data_channel_identifier = stats.id;
1516 data_channel_stats->state = DataStateToRTCDataChannelState(stats.state);
1517 data_channel_stats->messages_sent = stats.messages_sent;
1518 data_channel_stats->bytes_sent = stats.bytes_sent;
1519 data_channel_stats->messages_received = stats.messages_received;
1520 data_channel_stats->bytes_received = stats.bytes_received;
hboscc555c52016-10-18 19:48:311521 report->AddStats(std::move(data_channel_stats));
1522 }
1523}
1524
hbosdf6075a2016-12-19 12:58:021525void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 13:44:031526 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 18:34:401527 const std::map<std::string, cricket::TransportStats>&
1528 transport_stats_by_name,
stefanf79ade12017-06-02 13:44:031529 const Call::Stats& call_stats,
1530 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051531 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501532 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1533
Steve Anton5dfde182018-02-06 18:34:401534 for (const auto& entry : transport_stats_by_name) {
1535 const std::string& transport_name = entry.first;
1536 const cricket::TransportStats& transport_stats = entry.second;
1537 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 09:50:141538 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401539 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 21:54:491540 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:271541 channel_stats.ice_transport_stats.connection_infos) {
hbosc47a0c32016-10-11 21:54:491542 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 11:00:051543 new RTCIceCandidatePairStats(
1544 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1545 timestamp_us));
hbosc47a0c32016-10-11 21:54:491546
hbos0583b282016-11-30 09:50:141547 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 09:18:471548 // TODO(hbos): There could be other candidates that are not paired with
1549 // anything. We don't have a complete list. Local candidates come from
1550 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 13:08:341551 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 12:14:531552 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311553 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 12:14:531554 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311555 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 16:08:181556 candidate_pair_stats->state =
1557 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1558 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 09:38:081559 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 21:54:491560 // TODO(hbos): This writable is different than the spec. It goes to
1561 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 13:08:341562 // https://crbug.com/633550
hbosc47a0c32016-10-11 21:54:491563 candidate_pair_stats->writable = info.writable;
Taylor Brandstetter79326ea2021-09-28 22:09:531564 // Note that sent_total_packets includes discarded packets but
1565 // sent_total_bytes does not.
1566 candidate_pair_stats->packets_sent = static_cast<uint64_t>(
1567 info.sent_total_packets - info.sent_discarded_packets);
1568 candidate_pair_stats->packets_discarded_on_send =
1569 static_cast<uint64_t>(info.sent_discarded_packets);
1570 candidate_pair_stats->packets_received =
1571 static_cast<uint64_t>(info.packets_received);
hbosc47a0c32016-10-11 21:54:491572 candidate_pair_stats->bytes_sent =
1573 static_cast<uint64_t>(info.sent_total_bytes);
Taylor Brandstetter79326ea2021-09-28 22:09:531574 candidate_pair_stats->bytes_discarded_on_send =
1575 static_cast<uint64_t>(info.sent_discarded_bytes);
hbosc47a0c32016-10-11 21:54:491576 candidate_pair_stats->bytes_received =
1577 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 14:34:471578 candidate_pair_stats->total_round_trip_time =
1579 static_cast<double>(info.total_round_trip_time_ms) /
1580 rtc::kNumMillisecsPerSec;
1581 if (info.current_round_trip_time_ms) {
1582 candidate_pair_stats->current_round_trip_time =
1583 static_cast<double>(*info.current_round_trip_time_ms) /
1584 rtc::kNumMillisecsPerSec;
1585 }
stefanf79ade12017-06-02 13:44:031586 if (info.best_connection) {
hbos338f78a2017-02-07 14:41:211587 // The bandwidth estimations we have are for the selected candidate
1588 // pair ("info.best_connection").
stefanf79ade12017-06-02 13:44:031589 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1590 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1591 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211592 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 13:44:031593 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211594 }
stefanf79ade12017-06-02 13:44:031595 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211596 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 13:44:031597 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211598 }
1599 }
hbosd82f5122016-12-09 12:12:391600 candidate_pair_stats->requests_received =
1601 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 09:22:531602 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1603 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491604 candidate_pair_stats->responses_received =
1605 static_cast<uint64_t>(info.recv_ping_responses);
1606 candidate_pair_stats->responses_sent =
1607 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 09:22:531608 RTC_DCHECK_GE(info.sent_ping_requests_total,
1609 info.sent_ping_requests_before_first_response);
1610 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1611 info.sent_ping_requests_total -
1612 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491613
1614 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 09:18:471615 }
1616 }
1617 }
1618}
1619
Steve Anton57858b32018-02-15 23:19:501620void RTCStatsCollector::ProduceMediaStreamStats_s(
1621 int64_t timestamp_us,
1622 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051623 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501624 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:501625
1626 std::map<std::string, std::vector<std::string>> track_ids;
1627
1628 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 16:29:421629 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501630 std::string track_id =
1631 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1632 kSender, sender->internal()->AttachmentId());
1633 for (auto& stream_id : sender->stream_ids()) {
1634 track_ids[stream_id].push_back(track_id);
1635 }
1636 }
Mirko Bonadei739baf02019-01-27 16:29:421637 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501638 std::string track_id =
1639 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1640 kReceiver, receiver->internal()->AttachmentId());
1641 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 23:05:281642 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 23:19:501643 }
1644 }
1645 }
1646
1647 // Build stats for each stream ID known.
1648 for (auto& it : track_ids) {
1649 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1650 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1651 stream_stats->stream_identifier = it.first;
1652 stream_stats->track_ids = it.second;
1653 report->AddStats(std::move(stream_stats));
1654 }
1655}
1656
1657void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1658 int64_t timestamp_us,
1659 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051660 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501661 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1662
Steve Anton57858b32018-02-15 23:19:501663 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1664 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 16:29:421665 for (const auto& sender : stats.transceiver->senders()) {
Niels Möllere7cc8832022-01-04 14:20:031666 senders.push_back(
1667 rtc::scoped_refptr<RtpSenderInternal>(sender->internal()));
Steve Anton57858b32018-02-15 23:19:501668 }
1669 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1670 senders, report);
1671
1672 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 16:29:421673 for (const auto& receiver : stats.transceiver->receivers()) {
Niels Möllere7cc8832022-01-04 14:20:031674 receivers.push_back(
1675 rtc::scoped_refptr<RtpReceiverInternal>(receiver->internal()));
Steve Anton57858b32018-02-15 23:19:501676 }
1677 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1678 receivers, report);
1679 }
hbos09bc1282016-11-08 14:29:221680}
1681
Henrik Boström646fda02019-05-22 13:49:421682void RTCStatsCollector::ProduceMediaSourceStats_s(
1683 int64_t timestamp_us,
1684 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051685 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501686 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1687
Henrik Boström646fda02019-05-22 13:49:421688 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1689 transceiver_stats_infos_) {
1690 const auto& track_media_info_map =
1691 transceiver_stats_info.track_media_info_map;
1692 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1693 const auto& sender_internal = sender->internal();
1694 const auto& track = sender_internal->track();
1695 if (!track)
1696 continue;
Henrik Boströmd2c336f2019-07-03 15:11:101697 // TODO(https://crbug.com/webrtc/10771): The same track could be attached
1698 // to multiple senders which should result in multiple senders referencing
1699 // the same media-source stats. When all media source related metrics are
1700 // moved to the track's source (e.g. input frame rate is moved from
1701 // cricket::VideoSenderInfo to VideoTrackSourceInterface::Stats and audio
1702 // levels are moved to the corresponding audio track/source object), don't
1703 // create separate media source stats objects on a per-attachment basis.
Henrik Boström646fda02019-05-22 13:49:421704 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1705 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Taylor Brandstetter64851c02021-06-24 20:32:501706 AudioTrackInterface* audio_track =
1707 static_cast<AudioTrackInterface*>(track.get());
Mirko Bonadei317a1f02019-09-17 15:06:181708 auto audio_source_stats = std::make_unique<RTCAudioSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421709 RTCMediaSourceStatsIDFromKindAndAttachment(
1710 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1711 timestamp_us);
Henrik Boströmd2c336f2019-07-03 15:11:101712 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1713 // SSRC assigned (there shouldn't need to exist a send-stream, created
1714 // by an O/A exchange) in order to read audio media-source stats.
1715 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1716 // value indicating no SSRC.
1717 if (sender_internal->ssrc() != 0) {
1718 auto* voice_sender_info =
1719 track_media_info_map->GetVoiceSenderInfoBySsrc(
1720 sender_internal->ssrc());
1721 if (voice_sender_info) {
1722 audio_source_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
1723 voice_sender_info->audio_level);
1724 audio_source_stats->total_audio_energy =
1725 voice_sender_info->total_input_energy;
1726 audio_source_stats->total_samples_duration =
1727 voice_sender_info->total_input_duration;
Taylor Brandstetter64851c02021-06-24 20:32:501728 SetAudioProcessingStats(audio_source_stats.get(),
1729 voice_sender_info->apm_statistics);
Henrik Boströmd2c336f2019-07-03 15:11:101730 }
1731 }
Taylor Brandstetter64851c02021-06-24 20:32:501732 // Audio processor may be attached to either the track or the send
1733 // stream, so look in both places.
1734 auto audio_processor(audio_track->GetAudioProcessor());
1735 if (audio_processor.get()) {
Artem Titov880fa812021-07-30 20:30:231736 // The `has_remote_tracks` argument is obsolete; makes no difference
Taylor Brandstetter64851c02021-06-24 20:32:501737 // if it's set to true or false.
1738 AudioProcessorInterface::AudioProcessorStatistics ap_stats =
1739 audio_processor->GetStats(/*has_remote_tracks=*/false);
1740 SetAudioProcessingStats(audio_source_stats.get(),
1741 ap_stats.apm_statistics);
1742 }
Henrik Boströmd2c336f2019-07-03 15:11:101743 media_source_stats = std::move(audio_source_stats);
Henrik Boström646fda02019-05-22 13:49:421744 } else {
1745 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Mirko Bonadei317a1f02019-09-17 15:06:181746 auto video_source_stats = std::make_unique<RTCVideoSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421747 RTCMediaSourceStatsIDFromKindAndAttachment(
1748 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1749 timestamp_us);
1750 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1751 auto* video_source = video_track->GetSource();
1752 VideoTrackSourceInterface::Stats source_stats;
1753 if (video_source && video_source->GetStats(&source_stats)) {
1754 video_source_stats->width = source_stats.input_width;
1755 video_source_stats->height = source_stats.input_height;
1756 }
Henrik Boströmd2c336f2019-07-03 15:11:101757 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1758 // SSRC assigned (there shouldn't need to exist a send-stream, created
1759 // by an O/A exchange) in order to get framesPerSecond.
1760 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1761 // value indicating no SSRC.
Henrik Boström646fda02019-05-22 13:49:421762 if (sender_internal->ssrc() != 0) {
Henrik Boströmd2c336f2019-07-03 15:11:101763 auto* video_sender_info =
1764 track_media_info_map->GetVideoSenderInfoBySsrc(
1765 sender_internal->ssrc());
1766 if (video_sender_info) {
Henrik Boström646fda02019-05-22 13:49:421767 video_source_stats->frames_per_second =
Henrik Boströmd2c336f2019-07-03 15:11:101768 video_sender_info->framerate_input;
Di Wu668dbf62021-02-27 08:29:151769 video_source_stats->frames = video_sender_info->frames;
Henrik Boström646fda02019-05-22 13:49:421770 }
1771 }
1772 media_source_stats = std::move(video_source_stats);
1773 }
1774 media_source_stats->track_identifier = track->id();
1775 media_source_stats->kind = track->kind();
1776 report->AddStats(std::move(media_source_stats));
1777 }
1778 }
1779}
1780
hbos6ab97ce02016-10-03 21:16:561781void RTCStatsCollector::ProducePeerConnectionStats_s(
Jonas Olssona4d87372019-07-05 17:08:331782 int64_t timestamp_us,
1783 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051784 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501785 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1786
hbosd565b732016-08-30 21:04:351787 std::unique_ptr<RTCPeerConnectionStats> stats(
Jonas Olssona4d87372019-07-05 17:08:331788 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 09:41:091789 stats->data_channels_opened = internal_record_.data_channels_opened;
1790 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce02016-10-03 21:16:561791 report->AddStats(std::move(stats));
hbosd565b732016-08-30 21:04:351792}
1793
hbosdf6075a2016-12-19 12:58:021794void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 19:44:481795 int64_t timestamp_us,
Steve Anton57858b32018-02-15 23:19:501796 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 14:16:441797 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051798 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501799 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
hbos6ded1902016-11-01 08:50:461800
Steve Anton57858b32018-02-15 23:19:501801 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1802 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1803 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1804 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1805 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1806 } else {
Artem Titovd3251962021-11-15 15:57:071807 RTC_DCHECK_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:191808 }
Steve Anton56bae8d2018-02-15 00:07:421809 }
Steve Anton57858b32018-02-15 23:19:501810}
1811
1812void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1813 int64_t timestamp_us,
1814 const RtpTransceiverStatsInfo& stats,
1815 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051816 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501817 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1818
Steve Anton57858b32018-02-15 23:19:501819 if (!stats.mid || !stats.transport_name) {
1820 return;
1821 }
1822 RTC_DCHECK(stats.track_media_info_map);
1823 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1824 RTC_DCHECK(track_media_info_map.voice_media_info());
1825 std::string mid = *stats.mid;
1826 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1827 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
Alessio Bazzicaf7b1b952021-03-23 16:23:041828 // Inbound and remote-outbound.
1829 // The remote-outbound stats are based on RTCP sender reports sent from the
1830 // remote endpoint providing metrics about the remote outbound streams.
Steve Anton57858b32018-02-15 23:19:501831 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1832 track_media_info_map.voice_media_info()->receivers) {
1833 if (!voice_receiver_info.connected())
1834 continue;
Alessio Bazzicaf7b1b952021-03-23 16:23:041835 // Inbound.
1836 auto inbound_audio =
1837 CreateInboundAudioStreamStats(voice_receiver_info, mid, timestamp_us);
Steve Anton57858b32018-02-15 23:19:501838 // TODO(hta): This lookup should look for the sender, not the track.
1839 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1840 track_media_info_map.GetAudioTrack(voice_receiver_info);
1841 if (audio_track) {
1842 inbound_audio->track_id =
1843 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1844 kReceiver,
1845 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 08:50:461846 }
Steve Anton57858b32018-02-15 23:19:501847 inbound_audio->transport_id = transport_id;
Alessio Bazzicaf7b1b952021-03-23 16:23:041848 // Remote-outbound.
1849 auto remote_outbound_audio = CreateRemoteOutboundAudioStreamStats(
1850 voice_receiver_info, mid, inbound_audio->id(), transport_id);
1851 // Add stats.
1852 if (remote_outbound_audio) {
1853 // When the remote outbound stats are available, the remote ID for the
1854 // local inbound stats is set.
1855 inbound_audio->remote_id = remote_outbound_audio->id();
1856 report->AddStats(std::move(remote_outbound_audio));
1857 }
Steve Anton57858b32018-02-15 23:19:501858 report->AddStats(std::move(inbound_audio));
1859 }
Alessio Bazzicaf7b1b952021-03-23 16:23:041860 // Outbound.
Henrik Boström4f40fa52019-12-19 12:27:271861 std::map<std::string, RTCOutboundRTPStreamStats*> audio_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501862 for (const cricket::VoiceSenderInfo& voice_sender_info :
1863 track_media_info_map.voice_media_info()->senders) {
1864 if (!voice_sender_info.connected())
1865 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181866 auto outbound_audio = std::make_unique<RTCOutboundRTPStreamStats>(
Alessio Bazzicaf7b1b952021-03-23 16:23:041867 RTCOutboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_AUDIO,
1868 voice_sender_info.ssrc()),
Steve Anton57858b32018-02-15 23:19:501869 timestamp_us);
1870 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1871 outbound_audio.get());
1872 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1873 track_media_info_map.GetAudioTrack(voice_sender_info);
1874 if (audio_track) {
Henrik Boström646fda02019-05-22 13:49:421875 int attachment_id =
1876 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 23:19:501877 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 13:49:421878 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1879 attachment_id);
1880 outbound_audio->media_source_id =
1881 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1882 attachment_id);
Steve Anton56bae8d2018-02-15 00:07:421883 }
Steve Anton57858b32018-02-15 23:19:501884 outbound_audio->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271885 audio_outbound_rtps.insert(
1886 std::make_pair(outbound_audio->id(), outbound_audio.get()));
Steve Anton57858b32018-02-15 23:19:501887 report->AddStats(std::move(outbound_audio));
1888 }
Alessio Bazzicaf7b1b952021-03-23 16:23:041889 // Remote-inbound.
Henrik Boström883eefc2019-05-27 11:40:251890 // These are Report Block-based, information sent from the remote endpoint,
1891 // providing metrics about our Outbound streams. We take advantage of the fact
1892 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1893 // been added to the report.
1894 for (const cricket::VoiceSenderInfo& voice_sender_info :
1895 track_media_info_map.voice_media_info()->senders) {
1896 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1897 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071898 report_block_data, cricket::MEDIA_TYPE_AUDIO, audio_outbound_rtps,
1899 *report));
Henrik Boström883eefc2019-05-27 11:40:251900 }
1901 }
Steve Anton57858b32018-02-15 23:19:501902}
1903
1904void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1905 int64_t timestamp_us,
1906 const RtpTransceiverStatsInfo& stats,
1907 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051908 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501909 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1910
Steve Anton57858b32018-02-15 23:19:501911 if (!stats.mid || !stats.transport_name) {
1912 return;
1913 }
1914 RTC_DCHECK(stats.track_media_info_map);
1915 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1916 RTC_DCHECK(track_media_info_map.video_media_info());
1917 std::string mid = *stats.mid;
1918 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1919 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1920 // Inbound
1921 for (const cricket::VideoReceiverInfo& video_receiver_info :
1922 track_media_info_map.video_media_info()->receivers) {
1923 if (!video_receiver_info.connected())
1924 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181925 auto inbound_video = std::make_unique<RTCInboundRTPStreamStats>(
Alessio Bazzicaf7b1b952021-03-23 16:23:041926 RTCInboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_VIDEO,
1927 video_receiver_info.ssrc()),
Steve Anton57858b32018-02-15 23:19:501928 timestamp_us);
1929 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1930 inbound_video.get());
1931 rtc::scoped_refptr<VideoTrackInterface> video_track =
1932 track_media_info_map.GetVideoTrack(video_receiver_info);
1933 if (video_track) {
1934 inbound_video->track_id =
1935 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1936 kReceiver,
1937 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1938 }
1939 inbound_video->transport_id = transport_id;
1940 report->AddStats(std::move(inbound_video));
Alessio Bazzicaf7b1b952021-03-23 16:23:041941 // TODO(crbug.com/webrtc/12529): Add remote-outbound stats.
Steve Anton57858b32018-02-15 23:19:501942 }
1943 // Outbound
Henrik Boström4f40fa52019-12-19 12:27:271944 std::map<std::string, RTCOutboundRTPStreamStats*> video_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501945 for (const cricket::VideoSenderInfo& video_sender_info :
Eldar Rello9276e2c2020-06-10 14:53:391946 track_media_info_map.video_media_info()->senders) {
Steve Anton57858b32018-02-15 23:19:501947 if (!video_sender_info.connected())
1948 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181949 auto outbound_video = std::make_unique<RTCOutboundRTPStreamStats>(
Alessio Bazzicaf7b1b952021-03-23 16:23:041950 RTCOutboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_VIDEO,
1951 video_sender_info.ssrc()),
Steve Anton57858b32018-02-15 23:19:501952 timestamp_us);
Eldar Rello9276e2c2020-06-10 14:53:391953 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1954 outbound_video.get());
Steve Anton57858b32018-02-15 23:19:501955 rtc::scoped_refptr<VideoTrackInterface> video_track =
1956 track_media_info_map.GetVideoTrack(video_sender_info);
1957 if (video_track) {
Henrik Boström646fda02019-05-22 13:49:421958 int attachment_id =
1959 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 23:19:501960 outbound_video->track_id =
Henrik Boström646fda02019-05-22 13:49:421961 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1962 attachment_id);
1963 outbound_video->media_source_id =
1964 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1965 attachment_id);
Steve Anton57858b32018-02-15 23:19:501966 }
1967 outbound_video->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271968 video_outbound_rtps.insert(
1969 std::make_pair(outbound_video->id(), outbound_video.get()));
Steve Anton57858b32018-02-15 23:19:501970 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 08:50:461971 }
Henrik Boström883eefc2019-05-27 11:40:251972 // Remote-inbound
1973 // These are Report Block-based, information sent from the remote endpoint,
1974 // providing metrics about our Outbound streams. We take advantage of the fact
1975 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1976 // been added to the report.
1977 for (const cricket::VideoSenderInfo& video_sender_info :
1978 track_media_info_map.video_media_info()->senders) {
1979 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1980 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071981 report_block_data, cricket::MEDIA_TYPE_VIDEO, video_outbound_rtps,
1982 *report));
Henrik Boström883eefc2019-05-27 11:40:251983 }
1984 }
hbos6ded1902016-11-01 08:50:461985}
1986
hbosdf6075a2016-12-19 12:58:021987void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 18:34:401988 int64_t timestamp_us,
1989 const std::map<std::string, cricket::TransportStats>&
1990 transport_stats_by_name,
hbos2fa7c672016-10-24 11:00:051991 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1992 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051993 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501994 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1995
Steve Anton5dfde182018-02-06 18:34:401996 for (const auto& entry : transport_stats_by_name) {
1997 const std::string& transport_name = entry.first;
1998 const cricket::TransportStats& transport_stats = entry.second;
1999
hbos2fa7c672016-10-24 11:00:052000 // Get reference to RTCP channel, if it exists.
2001 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 18:34:402002 for (const cricket::TransportChannelStats& channel_stats :
2003 transport_stats.channel_stats) {
Jonas Olssona4d87372019-07-05 17:08:332004 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
hbos2fa7c672016-10-24 11:00:052005 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:402006 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 11:00:052007 break;
2008 }
2009 }
2010
2011 // Get reference to local and remote certificates of this transport, if they
2012 // exist.
Steve Anton5dfde182018-02-06 18:34:402013 const auto& certificate_stats_it =
2014 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 11:00:052015 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
2016 std::string local_certificate_id;
2017 if (certificate_stats_it->second.local) {
2018 local_certificate_id = RTCCertificateIDFromFingerprint(
2019 certificate_stats_it->second.local->fingerprint);
2020 }
2021 std::string remote_certificate_id;
2022 if (certificate_stats_it->second.remote) {
2023 remote_certificate_id = RTCCertificateIDFromFingerprint(
2024 certificate_stats_it->second.remote->fingerprint);
2025 }
2026
2027 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 18:34:402028 for (const cricket::TransportChannelStats& channel_stats :
2029 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 11:00:052030 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 18:34:402031 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
2032 transport_name, channel_stats.component),
2033 timestamp_us));
hbos2fa7c672016-10-24 11:00:052034 transport_stats->bytes_sent = 0;
Artem Titovedacbd52020-07-06 14:06:372035 transport_stats->packets_sent = 0;
hbos2fa7c672016-10-24 11:00:052036 transport_stats->bytes_received = 0;
Artem Titovedacbd52020-07-06 14:06:372037 transport_stats->packets_received = 0;
Jonas Olssona4d87372019-07-05 17:08:332038 transport_stats->dtls_state =
2039 DtlsTransportStateToRTCDtlsTransportState(channel_stats.dtls_state);
Jonas Oreland149dc722019-08-28 06:10:272040 transport_stats->selected_candidate_pair_changes =
2041 channel_stats.ice_transport_stats.selected_candidate_pair_changes;
hbos2fa7c672016-10-24 11:00:052042 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:272043 channel_stats.ice_transport_stats.connection_infos) {
hbos2fa7c672016-10-24 11:00:052044 *transport_stats->bytes_sent += info.sent_total_bytes;
Artem Titovedacbd52020-07-06 14:06:372045 *transport_stats->packets_sent +=
2046 info.sent_total_packets - info.sent_discarded_packets;
hbos2fa7c672016-10-24 11:00:052047 *transport_stats->bytes_received += info.recv_total_bytes;
Artem Titovedacbd52020-07-06 14:06:372048 *transport_stats->packets_received += info.packets_received;
hbos2fa7c672016-10-24 11:00:052049 if (info.best_connection) {
hbos2fa7c672016-10-24 11:00:052050 transport_stats->selected_candidate_pair_id =
2051 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
2052 }
2053 }
2054 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
2055 !rtcp_transport_stats_id.empty()) {
2056 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
2057 }
2058 if (!local_certificate_id.empty())
2059 transport_stats->local_certificate_id = local_certificate_id;
2060 if (!remote_certificate_id.empty())
2061 transport_stats->remote_certificate_id = remote_certificate_id;
Harald Alvestrand5cb78072019-10-28 08:51:172062 // Crypto information
2063 if (channel_stats.ssl_version_bytes) {
2064 char bytes[5];
2065 snprintf(bytes, sizeof(bytes), "%04X", channel_stats.ssl_version_bytes);
2066 transport_stats->tls_version = bytes;
2067 }
Mirko Bonadei7750d802021-07-26 15:27:422068 if (channel_stats.ssl_cipher_suite != rtc::kTlsNullWithNullNull &&
Harald Alvestrand5cb78072019-10-28 08:51:172069 rtc::SSLStreamAdapter::SslCipherSuiteToName(
2070 channel_stats.ssl_cipher_suite)
2071 .length()) {
2072 transport_stats->dtls_cipher =
2073 rtc::SSLStreamAdapter::SslCipherSuiteToName(
2074 channel_stats.ssl_cipher_suite);
2075 }
Mirko Bonadei7750d802021-07-26 15:27:422076 if (channel_stats.srtp_crypto_suite != rtc::kSrtpInvalidCryptoSuite &&
Harald Alvestrand5cb78072019-10-28 08:51:172077 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite)
2078 .length()) {
2079 transport_stats->srtp_cipher =
2080 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite);
2081 }
hbos2fa7c672016-10-24 11:00:052082 report->AddStats(std::move(transport_stats));
2083 }
2084 }
2085}
2086
2087std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 12:58:022088RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 18:34:402089 const std::map<std::string, cricket::TransportStats>&
2090 transport_stats_by_name) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052091 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:502092 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2093
hbos2fa7c672016-10-24 11:00:052094 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 18:34:402095 for (const auto& entry : transport_stats_by_name) {
2096 const std::string& transport_name = entry.first;
2097
hbos2fa7c672016-10-24 11:00:052098 CertificateStatsPair certificate_stats_pair;
2099 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 18:34:402100 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 11:00:052101 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 08:16:262102 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 11:00:052103 }
Steve Anton5dfde182018-02-06 18:34:402104
Taylor Brandstetterc3928662018-02-23 21:04:512105 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
2106 pc_->GetRemoteSSLCertChain(transport_name);
2107 if (remote_cert_chain) {
2108 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 11:00:052109 }
Steve Anton5dfde182018-02-06 18:34:402110
hbos2fa7c672016-10-24 11:00:052111 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 18:34:402112 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 11:00:052113 }
2114 return transport_cert_stats;
2115}
2116
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572117void RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w_n() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052118 RTC_DCHECK_RUN_ON(signaling_thread_);
Steve Anton56bae8d2018-02-15 00:07:422119
Henrik Boström180faeb2020-11-13 08:05:212120 transceiver_stats_infos_.clear();
Steve Anton57858b32018-02-15 23:19:502121 // These are used to invoke GetStats for all the media channels together in
2122 // one worker thread hop.
2123 std::map<cricket::VoiceMediaChannel*,
2124 std::unique_ptr<cricket::VoiceMediaInfo>>
2125 voice_stats;
2126 std::map<cricket::VideoMediaChannel*,
2127 std::unique_ptr<cricket::VideoMediaInfo>>
2128 video_stats;
2129
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572130 auto transceivers = pc_->GetTransceiversInternal();
2131
2132 // TODO(tommi): See if we can avoid synchronously blocking the signaling
2133 // thread while we do this (or avoid the Invoke at all).
2134 network_thread_->Invoke<void>(RTC_FROM_HERE, [this, &transceivers,
2135 &voice_stats, &video_stats] {
Henrik Boströme88c95e2020-07-08 09:18:502136 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:502137
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572138 for (const auto& transceiver_proxy : transceivers) {
2139 RtpTransceiver* transceiver = transceiver_proxy->internal();
Henrik Boströme88c95e2020-07-08 09:18:502140 cricket::MediaType media_type = transceiver->media_type();
Steve Anton57858b32018-02-15 23:19:502141
Henrik Boströme88c95e2020-07-08 09:18:502142 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
2143 // stats have been fetched on the worker thread.
Henrik Boström180faeb2020-11-13 08:05:212144 transceiver_stats_infos_.emplace_back();
2145 RtpTransceiverStatsInfo& stats = transceiver_stats_infos_.back();
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572146 stats.transceiver = transceiver;
Henrik Boströme88c95e2020-07-08 09:18:502147 stats.media_type = media_type;
Steve Anton57858b32018-02-15 23:19:502148
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572149 cricket::ChannelInterface* channel = transceiver->channel();
Henrik Boströme88c95e2020-07-08 09:18:502150 if (!channel) {
2151 // The remaining fields require a BaseChannel.
2152 continue;
2153 }
Steve Anton57858b32018-02-15 23:19:502154
Tomas Gunnarsson5411b172022-01-24 07:45:262155 stats.mid = channel->mid();
Tomas Gunnarsson94f01942022-01-03 14:59:122156 stats.transport_name = std::string(channel->transport_name());
Henrik Boströme88c95e2020-07-08 09:18:502157
2158 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2159 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
2160 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
2161 voice_stats.end());
2162 voice_stats[voice_channel->media_channel()] =
2163 std::make_unique<cricket::VoiceMediaInfo>();
2164 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2165 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
2166 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
2167 video_stats.end());
2168 video_stats[video_channel->media_channel()] =
2169 std::make_unique<cricket::VideoMediaInfo>();
2170 } else {
Artem Titovd3251962021-11-15 15:57:072171 RTC_DCHECK_NOTREACHED();
Henrik Boströme88c95e2020-07-08 09:18:502172 }
Steve Anton57858b32018-02-15 23:19:502173 }
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572174 });
Steve Anton57858b32018-02-15 23:19:502175
Henrik Boström180faeb2020-11-13 08:05:212176 // We jump to the worker thread and call GetStats() on each media channel as
2177 // well as GetCallStats(). At the same time we construct the
2178 // TrackMediaInfoMaps, which also needs info from the worker thread. This
2179 // minimizes the number of thread jumps.
Steve Anton57858b32018-02-15 23:19:502180 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Henrik Boström6fafbe32020-07-08 08:37:002181 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2182
Steve Anton57858b32018-02-15 23:19:502183 for (const auto& entry : voice_stats) {
Niels Möller6b4d9622020-09-14 08:47:502184 if (!entry.first->GetStats(entry.second.get(),
2185 /*get_and_clear_legacy_stats=*/false)) {
Steve Anton57858b32018-02-15 23:19:502186 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
2187 }
2188 }
2189 for (const auto& entry : video_stats) {
2190 if (!entry.first->GetStats(entry.second.get())) {
2191 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
2192 }
2193 }
Steve Anton57858b32018-02-15 23:19:502194
Henrik Boström6fafbe32020-07-08 08:37:002195 // Create the TrackMediaInfoMap for each transceiver stats object.
Henrik Boström180faeb2020-11-13 08:05:212196 for (auto& stats : transceiver_stats_infos_) {
Henrik Boström6fafbe32020-07-08 08:37:002197 auto transceiver = stats.transceiver;
2198 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
2199 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
2200 if (transceiver->channel()) {
2201 cricket::MediaType media_type = transceiver->media_type();
2202 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2203 auto* voice_channel =
2204 static_cast<cricket::VoiceChannel*>(transceiver->channel());
2205 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
2206 voice_media_info =
2207 std::move(voice_stats[voice_channel->media_channel()]);
2208 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2209 auto* video_channel =
2210 static_cast<cricket::VideoChannel*>(transceiver->channel());
2211 RTC_DCHECK(video_stats[video_channel->media_channel()]);
2212 video_media_info =
2213 std::move(video_stats[video_channel->media_channel()]);
2214 }
Steve Anton57858b32018-02-15 23:19:502215 }
Henrik Boström6fafbe32020-07-08 08:37:002216 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
2217 for (const auto& sender : transceiver->senders()) {
Niels Möllere7cc8832022-01-04 14:20:032218 senders.push_back(
2219 rtc::scoped_refptr<RtpSenderInternal>(sender->internal()));
Henrik Boström6fafbe32020-07-08 08:37:002220 }
2221 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
2222 for (const auto& receiver : transceiver->receivers()) {
Niels Möllere7cc8832022-01-04 14:20:032223 receivers.push_back(
2224 rtc::scoped_refptr<RtpReceiverInternal>(receiver->internal()));
Henrik Boström6fafbe32020-07-08 08:37:002225 }
2226 stats.track_media_info_map = std::make_unique<TrackMediaInfoMap>(
2227 std::move(voice_media_info), std::move(video_media_info), senders,
2228 receivers);
Steve Anton57858b32018-02-15 23:19:502229 }
Steve Anton57858b32018-02-15 23:19:502230
Henrik Boström180faeb2020-11-13 08:05:212231 call_stats_ = pc_->GetCallStats();
2232 });
hbos0adb8282016-11-23 10:32:062233}
2234
Taylor Brandstetter3a034e12020-07-09 22:32:342235void RTCStatsCollector::OnSctpDataChannelCreated(SctpDataChannel* channel) {
2236 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
2237 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
2238}
2239
2240void RTCStatsCollector::OnDataChannelOpened(DataChannelInterface* channel) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052241 RTC_DCHECK_RUN_ON(signaling_thread_);
Jonas Olssona4d87372019-07-05 17:08:332242 bool result = internal_record_.opened_data_channels
2243 .insert(reinterpret_cast<uintptr_t>(channel))
2244 .second;
hbos82ebe022016-11-14 09:41:092245 ++internal_record_.data_channels_opened;
2246 RTC_DCHECK(result);
2247}
2248
Taylor Brandstetter3a034e12020-07-09 22:32:342249void RTCStatsCollector::OnDataChannelClosed(DataChannelInterface* channel) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052250 RTC_DCHECK_RUN_ON(signaling_thread_);
hbos82ebe022016-11-14 09:41:092251 // Only channels that have been fully opened (and have increased the
Artem Titov880fa812021-07-30 20:30:232252 // `data_channels_opened_` counter) increase the closed counter.
hbos5bf9def2017-03-20 10:14:142253 if (internal_record_.opened_data_channels.erase(
2254 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 09:41:092255 ++internal_record_.data_channels_closed;
2256 }
2257}
2258
hboscc555c52016-10-18 19:48:312259const char* CandidateTypeToRTCIceCandidateTypeForTesting(
2260 const std::string& type) {
2261 return CandidateTypeToRTCIceCandidateType(type);
2262}
2263
2264const char* DataStateToRTCDataChannelStateForTesting(
2265 DataChannelInterface::DataState state) {
2266 return DataStateToRTCDataChannelState(state);
2267}
2268
hbosd565b732016-08-30 21:04:352269} // namespace webrtc