blob: 1fa731b912bfe3850fab1d22be02feb77e5b3265 [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;
173 RTC_NOTREACHED();
174 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:
189 RTC_NOTREACHED();
190 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:
206 RTC_NOTREACHED();
207 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 }
249 RTC_NOTREACHED();
250 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;
272 for (const auto& elem : durations_ms) {
273 result[QualityLimitationReasonToRTCQualityLimitationReason(elem.first)] =
274 elem.second;
275 }
276 return result;
277}
278
hbos9e302742017-01-20 10:47:10279double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
280 RTC_DCHECK_GE(audio_level, 0);
281 RTC_DCHECK_LE(audio_level, 32767);
282 return audio_level / 32767.0;
283}
284
hbos0adb8282016-11-23 10:32:06285std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 23:19:50286 uint64_t timestamp_us,
287 const std::string& mid,
Philipp Hancke95157a02020-11-16 19:08:27288 const std::string& transport_id,
Steve Anton57858b32018-02-15 23:19:50289 bool inbound,
hbos0adb8282016-11-23 10:32:06290 const RtpCodecParameters& codec_params) {
291 RTC_DCHECK_GE(codec_params.payload_type, 0);
292 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 20:09:01293 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 10:32:06294 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
295 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 23:19:50296 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 10:32:06297 timestamp_us));
298 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 14:56:04299 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 20:09:01300 if (codec_params.clock_rate) {
301 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
302 }
Johannes Kron72d69152020-02-10 13:05:55303 if (codec_params.num_channels) {
304 codec_stats->channels = *codec_params.num_channels;
305 }
306
307 rtc::StringBuilder fmtp;
308 if (WriteFmtpParameters(codec_params.parameters, &fmtp)) {
309 codec_stats->sdp_fmtp_line = fmtp.Release();
310 }
Philipp Hancke95157a02020-11-16 19:08:27311 codec_stats->transport_id = transport_id;
hbos0adb8282016-11-23 10:32:06312 return codec_stats;
313}
314
hbos09bc1282016-11-08 14:29:22315void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
316 const MediaStreamTrackInterface& track,
317 RTCMediaStreamTrackStats* track_stats) {
318 track_stats->track_identifier = track.id();
319 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
320}
321
hbos820f5782016-11-22 11:16:50322// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 10:00:17323void SetInboundRTPStreamStatsFromMediaReceiverInfo(
324 const cricket::MediaReceiverInfo& media_receiver_info,
325 RTCInboundRTPStreamStats* inbound_stats) {
326 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 14:28:11327 inbound_stats->ssrc = media_receiver_info.ssrc();
hboseeafe942016-11-01 10:00:17328 inbound_stats->packets_received =
329 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
330 inbound_stats->bytes_received =
Niels Möllerac0a4cb2019-10-09 13:01:33331 static_cast<uint64_t>(media_receiver_info.payload_bytes_rcvd);
332 inbound_stats->header_bytes_received =
333 static_cast<uint64_t>(media_receiver_info.header_and_padding_bytes_rcvd);
hbos02cd4d62016-12-09 12:19:44334 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 11:26:04335 static_cast<int32_t>(media_receiver_info.packets_lost);
Byoungchan Lee899b29e2021-06-29 13:09:18336 inbound_stats->jitter_buffer_delay =
337 media_receiver_info.jitter_buffer_delay_seconds;
338 inbound_stats->jitter_buffer_emitted_count =
339 media_receiver_info.jitter_buffer_emitted_count;
hboseeafe942016-11-01 10:00:17340}
341
Alessio Bazzicaf7b1b952021-03-23 16:23:04342std::unique_ptr<RTCInboundRTPStreamStats> CreateInboundAudioStreamStats(
hboseeafe942016-11-01 10:00:17343 const cricket::VoiceReceiverInfo& voice_receiver_info,
Alessio Bazzicaf7b1b952021-03-23 16:23:04344 const std::string& mid,
345 int64_t timestamp_us) {
346 auto inbound_audio = std::make_unique<RTCInboundRTPStreamStats>(
347 /*id=*/RTCInboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_AUDIO,
348 voice_receiver_info.ssrc()),
349 timestamp_us);
Jonas Olssona4d87372019-07-05 17:08:33350 SetInboundRTPStreamStatsFromMediaReceiverInfo(voice_receiver_info,
Alessio Bazzicaf7b1b952021-03-23 16:23:04351 inbound_audio.get());
hbos820f5782016-11-22 11:16:50352 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03353 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16354 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50355 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04356 mid, /*inbound=*/true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16357 }
Jonas Olssona4d87372019-07-05 17:08:33358 inbound_audio->jitter = static_cast<double>(voice_receiver_info.jitter_ms) /
359 rtc::kNumMillisecsPerSec;
Eldar Rello4e5bc9f2020-07-06 11:18:07360 inbound_audio->total_samples_received =
361 voice_receiver_info.total_samples_received;
362 inbound_audio->concealed_samples = voice_receiver_info.concealed_samples;
363 inbound_audio->silent_concealed_samples =
364 voice_receiver_info.silent_concealed_samples;
365 inbound_audio->concealment_events = voice_receiver_info.concealment_events;
366 inbound_audio->inserted_samples_for_deceleration =
367 voice_receiver_info.inserted_samples_for_deceleration;
368 inbound_audio->removed_samples_for_acceleration =
369 voice_receiver_info.removed_samples_for_acceleration;
Philipp Hanckeaa83cc72020-10-27 08:50:36370 if (voice_receiver_info.audio_level >= 0) {
371 inbound_audio->audio_level =
372 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
373 }
Eldar Rello4e5bc9f2020-07-06 11:18:07374 inbound_audio->total_audio_energy = voice_receiver_info.total_output_energy;
375 inbound_audio->total_samples_duration =
376 voice_receiver_info.total_output_duration;
hbos820f5782016-11-22 11:16:50377 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
378 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 15:32:00379 if (voice_receiver_info.last_packet_received_timestamp_ms) {
Alessio Bazzicac366d512021-03-22 14:36:53380 inbound_audio->last_packet_received_timestamp = static_cast<double>(
381 *voice_receiver_info.last_packet_received_timestamp_ms);
Henrik Boström01738c62019-04-15 15:32:00382 }
Åsa Perssonfcf79cc2019-10-22 13:23:44383 if (voice_receiver_info.estimated_playout_ntp_timestamp_ms) {
Alessio Bazzica5cf8c2c2021-03-24 07:51:26384 // TODO(bugs.webrtc.org/10529): Fix time origin.
Åsa Perssonfcf79cc2019-10-22 13:23:44385 inbound_audio->estimated_playout_timestamp = static_cast<double>(
386 *voice_receiver_info.estimated_playout_ntp_timestamp_ms);
387 }
Ivo Creusen8d8ffdb2019-04-30 07:45:21388 inbound_audio->fec_packets_received =
389 voice_receiver_info.fec_packets_received;
390 inbound_audio->fec_packets_discarded =
391 voice_receiver_info.fec_packets_discarded;
Alessio Bazzicaf7b1b952021-03-23 16:23:04392 return inbound_audio;
393}
394
395std::unique_ptr<RTCRemoteOutboundRtpStreamStats>
396CreateRemoteOutboundAudioStreamStats(
397 const cricket::VoiceReceiverInfo& voice_receiver_info,
398 const std::string& mid,
399 const std::string& inbound_audio_id,
400 const std::string& transport_id) {
401 if (!voice_receiver_info.last_sender_report_timestamp_ms.has_value()) {
402 // Cannot create `RTCRemoteOutboundRtpStreamStats` when the RTCP SR arrival
403 // timestamp is not available - i.e., until the first sender report is
404 // received.
405 return nullptr;
406 }
407 RTC_DCHECK_GT(voice_receiver_info.sender_reports_reports_count, 0);
408
409 // Create.
410 auto stats = std::make_unique<RTCRemoteOutboundRtpStreamStats>(
411 /*id=*/RTCRemoteOutboundRTPStreamStatsIDFromSSRC(
412 cricket::MEDIA_TYPE_AUDIO, voice_receiver_info.ssrc()),
413 /*timestamp_us=*/rtc::kNumMicrosecsPerMillisec *
414 voice_receiver_info.last_sender_report_timestamp_ms.value());
415
416 // Populate.
417 // - RTCRtpStreamStats.
418 stats->ssrc = voice_receiver_info.ssrc();
419 stats->kind = "audio";
420 stats->transport_id = transport_id;
421 stats->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
422 mid,
423 /*inbound=*/true, // Remote-outbound same as local-inbound.
424 *voice_receiver_info.codec_payload_type);
425 // - RTCSentRtpStreamStats.
426 stats->packets_sent = voice_receiver_info.sender_reports_packets_sent;
427 stats->bytes_sent = voice_receiver_info.sender_reports_bytes_sent;
428 // - RTCRemoteOutboundRtpStreamStats.
429 stats->local_id = inbound_audio_id;
430 RTC_DCHECK(
431 voice_receiver_info.last_sender_report_remote_timestamp_ms.has_value());
432 stats->remote_timestamp = static_cast<double>(
433 voice_receiver_info.last_sender_report_remote_timestamp_ms.value());
434 stats->reports_sent = voice_receiver_info.sender_reports_reports_count;
435
436 return stats;
hboseeafe942016-11-01 10:00:17437}
438
439void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 23:19:50440 const std::string& mid,
hboseeafe942016-11-01 10:00:17441 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 11:16:50442 RTCInboundRTPStreamStats* inbound_video) {
Jonas Olssona4d87372019-07-05 17:08:33443 SetInboundRTPStreamStatsFromMediaReceiverInfo(video_receiver_info,
444 inbound_video);
hbos820f5782016-11-22 11:16:50445 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03446 inbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16447 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50448 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04449 mid, /*inbound=*/true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16450 }
Di Wu (RP Room Eng)8af6b492021-02-20 01:34:22451 inbound_video->jitter = static_cast<double>(video_receiver_info.jitter_ms) /
452 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 11:16:50453 inbound_video->fir_count =
454 static_cast<uint32_t>(video_receiver_info.firs_sent);
455 inbound_video->pli_count =
456 static_cast<uint32_t>(video_receiver_info.plis_sent);
457 inbound_video->nack_count =
458 static_cast<uint32_t>(video_receiver_info.nacks_sent);
Eldar Rello4e5bc9f2020-07-06 11:18:07459 inbound_video->frames_received = video_receiver_info.frames_received;
hbos6769c492017-01-02 16:35:13460 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
Eldar Rello4e5bc9f2020-07-06 11:18:07461 inbound_video->frames_dropped = video_receiver_info.frames_dropped;
Rasmus Brandt2efae772019-06-27 12:29:34462 inbound_video->key_frames_decoded = video_receiver_info.key_frames_decoded;
Eldar Rello4e5bc9f2020-07-06 11:18:07463 if (video_receiver_info.frame_width > 0) {
464 inbound_video->frame_width =
465 static_cast<uint32_t>(video_receiver_info.frame_width);
466 }
467 if (video_receiver_info.frame_height > 0) {
468 inbound_video->frame_height =
469 static_cast<uint32_t>(video_receiver_info.frame_height);
470 }
471 if (video_receiver_info.framerate_rcvd > 0) {
472 inbound_video->frames_per_second = video_receiver_info.framerate_rcvd;
473 }
hbosa51d4f32017-02-16 13:34:48474 if (video_receiver_info.qp_sum)
475 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Johannes Kronbfd343b2019-07-01 08:07:50476 inbound_video->total_decode_time =
477 static_cast<double>(video_receiver_info.total_decode_time_ms) /
478 rtc::kNumMillisecsPerSec;
Johannes Kron00376e12019-11-25 09:25:42479 inbound_video->total_inter_frame_delay =
480 video_receiver_info.total_inter_frame_delay;
481 inbound_video->total_squared_inter_frame_delay =
482 video_receiver_info.total_squared_inter_frame_delay;
Henrik Boström01738c62019-04-15 15:32:00483 if (video_receiver_info.last_packet_received_timestamp_ms) {
Alessio Bazzica5cf8c2c2021-03-24 07:51:26484 inbound_video->last_packet_received_timestamp = static_cast<double>(
485 *video_receiver_info.last_packet_received_timestamp_ms);
Henrik Boström01738c62019-04-15 15:32:00486 }
Åsa Perssonfcf79cc2019-10-22 13:23:44487 if (video_receiver_info.estimated_playout_ntp_timestamp_ms) {
Alessio Bazzica5cf8c2c2021-03-24 07:51:26488 // TODO(bugs.webrtc.org/10529): Fix time origin if needed.
Åsa Perssonfcf79cc2019-10-22 13:23:44489 inbound_video->estimated_playout_timestamp = static_cast<double>(
490 *video_receiver_info.estimated_playout_ntp_timestamp_ms);
491 }
Alessio Bazzica5cf8c2c2021-03-24 07:51:26492 // TODO(bugs.webrtc.org/10529): When info's |content_info| is optional
493 // support the "unspecified" value.
Henrik Boström2e069262019-04-09 11:59:31494 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
495 inbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 11:09:51496 if (!video_receiver_info.decoder_implementation_name.empty()) {
497 inbound_video->decoder_implementation =
498 video_receiver_info.decoder_implementation_name;
499 }
hboseeafe942016-11-01 10:00:17500}
501
hbos820f5782016-11-22 11:16:50502// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 08:50:46503void SetOutboundRTPStreamStatsFromMediaSenderInfo(
504 const cricket::MediaSenderInfo& media_sender_info,
505 RTCOutboundRTPStreamStats* outbound_stats) {
506 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 14:28:11507 outbound_stats->ssrc = media_sender_info.ssrc();
hbos6ded1902016-11-01 08:50:46508 outbound_stats->packets_sent =
509 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53510 outbound_stats->retransmitted_packets_sent =
511 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 08:50:46512 outbound_stats->bytes_sent =
Niels Möllerac0a4cb2019-10-09 13:01:33513 static_cast<uint64_t>(media_sender_info.payload_bytes_sent);
514 outbound_stats->header_bytes_sent =
515 static_cast<uint64_t>(media_sender_info.header_and_padding_bytes_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53516 outbound_stats->retransmitted_bytes_sent =
517 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 08:50:46518}
519
520void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 23:19:50521 const std::string& mid,
hbos6ded1902016-11-01 08:50:46522 const cricket::VoiceSenderInfo& voice_sender_info,
523 RTCOutboundRTPStreamStats* outbound_audio) {
Jonas Olssona4d87372019-07-05 17:08:33524 SetOutboundRTPStreamStatsFromMediaSenderInfo(voice_sender_info,
525 outbound_audio);
hbos6ded1902016-11-01 08:50:46526 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03527 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16528 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50529 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04530 mid, /*inbound=*/false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16531 }
hbos6ded1902016-11-01 08:50:46532 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
533 // purposefully left undefined for audio.
534}
535
536void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 23:19:50537 const std::string& mid,
hbos6ded1902016-11-01 08:50:46538 const cricket::VideoSenderInfo& video_sender_info,
539 RTCOutboundRTPStreamStats* outbound_video) {
Jonas Olssona4d87372019-07-05 17:08:33540 SetOutboundRTPStreamStatsFromMediaSenderInfo(video_sender_info,
541 outbound_video);
hbos6ded1902016-11-01 08:50:46542 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03543 outbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16544 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50545 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
Alessio Bazzicaf7b1b952021-03-23 16:23:04546 mid, /*inbound=*/false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16547 }
hbos6ded1902016-11-01 08:50:46548 outbound_video->fir_count =
549 static_cast<uint32_t>(video_sender_info.firs_rcvd);
550 outbound_video->pli_count =
551 static_cast<uint32_t>(video_sender_info.plis_rcvd);
552 outbound_video->nack_count =
553 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 16:35:13554 if (video_sender_info.qp_sum)
555 outbound_video->qp_sum = *video_sender_info.qp_sum;
556 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Rasmus Brandt2efae772019-06-27 12:29:34557 outbound_video->key_frames_encoded = video_sender_info.key_frames_encoded;
Henrik Boströmf71362f2019-04-08 14:14:23558 outbound_video->total_encode_time =
559 static_cast<double>(video_sender_info.total_encode_time_ms) /
560 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 13:15:38561 outbound_video->total_encoded_bytes_target =
562 video_sender_info.total_encoded_bytes_target;
Eldar Rello9276e2c2020-06-10 14:53:39563 if (video_sender_info.send_frame_width > 0) {
564 outbound_video->frame_width =
565 static_cast<uint32_t>(video_sender_info.send_frame_width);
Henrik Boströma0ff50c2020-05-05 13:54:46566 }
Eldar Rello9276e2c2020-06-10 14:53:39567 if (video_sender_info.send_frame_height > 0) {
568 outbound_video->frame_height =
569 static_cast<uint32_t>(video_sender_info.send_frame_height);
570 }
571 if (video_sender_info.framerate_sent > 0) {
572 outbound_video->frames_per_second = video_sender_info.framerate_sent;
573 }
574 outbound_video->frames_sent = video_sender_info.frames_sent;
575 outbound_video->huge_frames_sent = video_sender_info.huge_frames_sent;
Henrik Boström9fe18342019-05-16 16:38:20576 outbound_video->total_packet_send_delay =
577 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
578 rtc::kNumMillisecsPerSec;
Henrik Boströmce33b6a2019-05-28 15:42:38579 outbound_video->quality_limitation_reason =
580 QualityLimitationReasonToRTCQualityLimitationReason(
581 video_sender_info.quality_limitation_reason);
Byoungchan Lee7d235352021-05-28 12:32:04582 outbound_video->quality_limitation_durations =
583 QualityLimitationDurationToRTCQualityLimitationDuration(
584 video_sender_info.quality_limitation_durations_ms);
Evan Shrubsolecc62b162019-09-09 09:26:45585 outbound_video->quality_limitation_resolution_changes =
586 video_sender_info.quality_limitation_resolution_changes;
Henrik Boström2e069262019-04-09 11:59:31587 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
588 // optional, support the "unspecified" value.
589 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
590 outbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 11:09:51591 if (!video_sender_info.encoder_implementation_name.empty()) {
592 outbound_video->encoder_implementation =
593 video_sender_info.encoder_implementation_name;
594 }
Henrik Boströma0ff50c2020-05-05 13:54:46595 if (video_sender_info.rid) {
596 outbound_video->rid = *video_sender_info.rid;
597 }
hbos6ded1902016-11-01 08:50:46598}
599
Henrik Boström883eefc2019-05-27 11:40:25600std::unique_ptr<RTCRemoteInboundRtpStreamStats>
601ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
602 const ReportBlockData& report_block_data,
603 cricket::MediaType media_type,
Eldar Relloc07e9042020-07-03 08:08:07604 const std::map<std::string, RTCOutboundRTPStreamStats*>& outbound_rtps,
Henrik Boström883eefc2019-05-27 11:40:25605 const RTCStatsReport& report) {
606 const auto& report_block = report_block_data.report_block();
607 // RTCStats' timestamp generally refers to when the metric was sampled, but
608 // for "remote-[outbound/inbound]-rtp" it refers to the local time when the
609 // Report Block was received.
Mirko Bonadei317a1f02019-09-17 15:06:18610 auto remote_inbound = std::make_unique<RTCRemoteInboundRtpStreamStats>(
Henrik Boström8605fbf2019-06-24 14:44:51611 RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(media_type,
612 report_block.source_ssrc),
Henrik Boström883eefc2019-05-27 11:40:25613 /*timestamp=*/report_block_data.report_block_timestamp_utc_us());
Henrik Boström8605fbf2019-06-24 14:44:51614 remote_inbound->ssrc = report_block.source_ssrc;
Henrik Boström883eefc2019-05-27 11:40:25615 remote_inbound->kind =
616 media_type == cricket::MEDIA_TYPE_AUDIO ? "audio" : "video";
617 remote_inbound->packets_lost = report_block.packets_lost;
Di Wu86f04ad2021-03-01 07:36:03618 remote_inbound->fraction_lost =
619 static_cast<double>(report_block.fraction_lost) / (1 << 8);
Henrik Boström883eefc2019-05-27 11:40:25620 remote_inbound->round_trip_time =
621 static_cast<double>(report_block_data.last_rtt_ms()) /
622 rtc::kNumMillisecsPerSec;
Di Wu88a51b22021-03-01 19:22:06623 remote_inbound->total_round_trip_time =
624 static_cast<double>(report_block_data.sum_rtt_ms()) /
625 rtc::kNumMillisecsPerSec;
626 remote_inbound->round_trip_time_measurements =
627 report_block_data.num_rtts();
Henrik Boström883eefc2019-05-27 11:40:25628
Alessio Bazzicaf7b1b952021-03-23 16:23:04629 std::string local_id =
630 RTCOutboundRTPStreamStatsIDFromSSRC(media_type, report_block.source_ssrc);
Henrik Boström4f40fa52019-12-19 12:27:27631 // Look up local stat from |outbound_rtps| where the pointers are non-const.
632 auto local_id_it = outbound_rtps.find(local_id);
633 if (local_id_it != outbound_rtps.end()) {
Henrik Boström883eefc2019-05-27 11:40:25634 remote_inbound->local_id = local_id;
Henrik Boström4f40fa52019-12-19 12:27:27635 auto& outbound_rtp = *local_id_it->second;
636 outbound_rtp.remote_id = remote_inbound->id();
Henrik Boström883eefc2019-05-27 11:40:25637 // The RTP/RTCP transport is obtained from the
638 // RTCOutboundRtpStreamStats's transport.
639 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
640 ? report.Get(*outbound_rtp.transport_id)
641 : nullptr;
642 if (transport_from_id) {
643 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
644 // If RTP and RTCP are not multiplexed, there is a separate RTCP
645 // transport paired with the RTP transport, otherwise the same
646 // transport is used for RTCP and RTP.
647 remote_inbound->transport_id =
648 transport.rtcp_transport_stats_id.is_defined()
649 ? *transport.rtcp_transport_stats_id
650 : *outbound_rtp.transport_id;
651 }
652 // We're assuming the same codec is used on both ends. However if the
653 // codec is switched out on the fly we may have received a Report Block
654 // based on the previous codec and there is no way to tell which point in
655 // time the codec changed for the remote end.
656 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
657 ? report.Get(*outbound_rtp.codec_id)
658 : nullptr;
659 if (codec_from_id) {
660 remote_inbound->codec_id = *outbound_rtp.codec_id;
661 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
662 if (codec.clock_rate.is_defined()) {
663 // The Report Block jitter is expressed in RTP timestamp units
664 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
665 // to seconds we divide by the codec's clock rate.
666 remote_inbound->jitter =
667 static_cast<double>(report_block.jitter) / *codec.clock_rate;
668 }
669 }
670 }
671 return remote_inbound;
672}
673
hbos02ba2112016-10-28 12:14:53674void ProduceCertificateStatsFromSSLCertificateStats(
Jonas Olssona4d87372019-07-05 17:08:33675 int64_t timestamp_us,
676 const rtc::SSLCertificateStats& certificate_stats,
hbos02ba2112016-10-28 12:14:53677 RTCStatsReport* report) {
678 RTCCertificateStats* prev_certificate_stats = nullptr;
679 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
680 s = s->issuer.get()) {
hbos02d2a922016-12-21 09:29:05681 std::string certificate_stats_id =
682 RTCCertificateIDFromFingerprint(s->fingerprint);
683 // It is possible for the same certificate to show up multiple times, e.g.
684 // if local and remote side use the same certificate in a loopback call.
685 // If the report already contains stats for this certificate, skip it.
686 if (report->Get(certificate_stats_id)) {
687 RTC_DCHECK_EQ(s, &certificate_stats);
688 break;
689 }
Jonas Olssona4d87372019-07-05 17:08:33690 RTCCertificateStats* certificate_stats =
691 new RTCCertificateStats(certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 12:14:53692 certificate_stats->fingerprint = s->fingerprint;
693 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
694 certificate_stats->base64_certificate = s->base64_certificate;
695 if (prev_certificate_stats)
696 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
697 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
698 prev_certificate_stats = certificate_stats;
699 }
700}
701
Jonas Olssona4d87372019-07-05 17:08:33702const std::string& ProduceIceCandidateStats(int64_t timestamp_us,
703 const cricket::Candidate& candidate,
704 bool is_local,
705 const std::string& transport_id,
706 RTCStatsReport* report) {
hbos02ba2112016-10-28 12:14:53707 const std::string& id = "RTCIceCandidate_" + candidate.id();
708 const RTCStats* stats = report->Get(id);
709 if (!stats) {
710 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
711 if (is_local)
712 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
713 else
714 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 17:59:31715 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 18:49:36716 if (is_local) {
717 candidate_stats->network_type =
718 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 12:40:08719 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
720 std::string relay_protocol = candidate.relay_protocol();
721 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
722 relay_protocol.compare("tcp") == 0 ||
723 relay_protocol.compare("tls") == 0);
724 candidate_stats->relay_protocol = relay_protocol;
725 }
Gary Liu37e489c2017-11-21 18:49:36726 } else {
727 // We don't expect to know the adapter type of remote candidates.
728 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
729 }
hbos02ba2112016-10-28 12:14:53730 candidate_stats->ip = candidate.address().ipaddr().ToString();
Philipp Hanckea9ba4502021-03-22 12:22:54731 candidate_stats->address = candidate.address().ipaddr().ToString();
hbos02ba2112016-10-28 12:14:53732 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
733 candidate_stats->protocol = candidate.protocol();
Jonas Olssona4d87372019-07-05 17:08:33734 candidate_stats->candidate_type =
735 CandidateTypeToRTCIceCandidateType(candidate.type());
hbos02ba2112016-10-28 12:14:53736 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
737
738 stats = candidate_stats.get();
739 report->AddStats(std::move(candidate_stats));
740 }
741 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
742 : RTCRemoteIceCandidateStats::kType);
743 return stats->id();
744}
745
Taylor Brandstetter64851c02021-06-24 20:32:50746template <typename StatsType>
747void SetAudioProcessingStats(StatsType* stats,
748 const AudioProcessingStats& apm_stats) {
749 if (apm_stats.echo_return_loss) {
750 stats->echo_return_loss = *apm_stats.echo_return_loss;
751 }
752 if (apm_stats.echo_return_loss_enhancement) {
753 stats->echo_return_loss_enhancement =
754 *apm_stats.echo_return_loss_enhancement;
755 }
756}
757
hbos9e302742017-01-20 10:47:10758std::unique_ptr<RTCMediaStreamTrackStats>
759ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
760 int64_t timestamp_us,
Taylor Brandstetter64851c02021-06-24 20:32:50761 AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19762 const cricket::VoiceSenderInfo& voice_sender_info,
763 int attachment_id) {
hbos9e302742017-01-20 10:47:10764 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
765 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58766 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
767 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19768 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10769 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
770 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42771 audio_track_stats->media_source_id =
772 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
773 attachment_id);
hbos9e302742017-01-20 10:47:10774 audio_track_stats->remote_source = false;
775 audio_track_stats->detached = false;
Taylor Brandstetter64851c02021-06-24 20:32:50776 // Audio processor may be attached to either the track or the send
777 // stream, so look in both places.
778 SetAudioProcessingStats(audio_track_stats.get(),
779 voice_sender_info.apm_statistics);
780 auto audio_processor(audio_track.GetAudioProcessor());
781 if (audio_processor.get()) {
782 // The |has_remote_tracks| argument is obsolete; makes no difference if it's
783 // set to true or false.
784 AudioProcessorInterface::AudioProcessorStatistics ap_stats =
785 audio_processor->GetStats(/*has_remote_tracks=*/false);
786 SetAudioProcessingStats(audio_track_stats.get(), ap_stats.apm_statistics);
hbos9e302742017-01-20 10:47:10787 }
788 return audio_track_stats;
789}
790
791std::unique_ptr<RTCMediaStreamTrackStats>
792ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
793 int64_t timestamp_us,
794 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19795 const cricket::VoiceReceiverInfo& voice_receiver_info,
796 int attachment_id) {
797 // Since receiver tracks can't be reattached, we use the SSRC as
798 // an attachment identifier.
hbos9e302742017-01-20 10:47:10799 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
800 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58801 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
802 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19803 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10804 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
805 audio_track, audio_track_stats.get());
806 audio_track_stats->remote_source = true;
807 audio_track_stats->detached = false;
808 if (voice_receiver_info.audio_level >= 0) {
Jonas Olssona4d87372019-07-05 17:08:33809 audio_track_stats->audio_level =
810 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
hbos9e302742017-01-20 10:47:10811 }
Gustaf Ullbergb0a02072017-10-02 10:00:34812 audio_track_stats->jitter_buffer_delay =
813 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 14:46:29814 audio_track_stats->jitter_buffer_emitted_count =
815 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 07:45:21816 audio_track_stats->inserted_samples_for_deceleration =
817 voice_receiver_info.inserted_samples_for_deceleration;
818 audio_track_stats->removed_samples_for_acceleration =
819 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 19:17:49820 audio_track_stats->total_audio_energy =
821 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-25 00:15:13822 audio_track_stats->total_samples_received =
823 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 19:17:49824 audio_track_stats->total_samples_duration =
825 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-25 00:15:13826 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 07:45:21827 audio_track_stats->silent_concealed_samples =
828 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 07:28:20829 audio_track_stats->concealment_events =
830 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 16:21:10831 audio_track_stats->jitter_buffer_flushes =
832 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 11:52:16833 audio_track_stats->delayed_packet_outage_samples =
834 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 08:18:40835 audio_track_stats->relative_packet_arrival_delay =
836 voice_receiver_info.relative_packet_arrival_delay_seconds;
Artem Titove618cc92020-03-11 10:18:54837 audio_track_stats->jitter_buffer_target_delay =
838 voice_receiver_info.jitter_buffer_target_delay_seconds;
Henrik Lundin44125fa2019-04-29 15:00:46839 audio_track_stats->interruption_count =
840 voice_receiver_info.interruption_count >= 0
841 ? voice_receiver_info.interruption_count
842 : 0;
843 audio_track_stats->total_interruption_duration =
844 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
845 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 10:47:10846 return audio_track_stats;
847}
848
849std::unique_ptr<RTCMediaStreamTrackStats>
850ProduceMediaStreamTrackStatsFromVideoSenderInfo(
851 int64_t timestamp_us,
852 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19853 const cricket::VideoSenderInfo& video_sender_info,
854 int attachment_id) {
hbos9e302742017-01-20 10:47:10855 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
856 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58857 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 08:18:58858 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19859 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10860 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
861 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42862 video_track_stats->media_source_id =
863 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
864 attachment_id);
hbos9e302742017-01-20 10:47:10865 video_track_stats->remote_source = false;
866 video_track_stats->detached = false;
Jonas Olssona4d87372019-07-05 17:08:33867 video_track_stats->frame_width =
868 static_cast<uint32_t>(video_sender_info.send_frame_width);
869 video_track_stats->frame_height =
870 static_cast<uint32_t>(video_sender_info.send_frame_height);
hbosfefe0762017-01-20 14:14:25871 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 13:08:34872 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 14:14:25873 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 15:35:03874 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 10:47:10875 return video_track_stats;
876}
877
878std::unique_ptr<RTCMediaStreamTrackStats>
879ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
880 int64_t timestamp_us,
881 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19882 const cricket::VideoReceiverInfo& video_receiver_info,
883 int attachment_id) {
hbos9e302742017-01-20 10:47:10884 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
885 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58886 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
887
888 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19889 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10890 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
891 video_track, video_track_stats.get());
892 video_track_stats->remote_source = true;
893 video_track_stats->detached = false;
894 if (video_receiver_info.frame_width > 0 &&
895 video_receiver_info.frame_height > 0) {
Jonas Olssona4d87372019-07-05 17:08:33896 video_track_stats->frame_width =
897 static_cast<uint32_t>(video_receiver_info.frame_width);
898 video_track_stats->frame_height =
899 static_cast<uint32_t>(video_receiver_info.frame_height);
hbos9e302742017-01-20 10:47:10900 }
Guido Urdaneta67378412019-05-28 15:38:08901 video_track_stats->jitter_buffer_delay =
902 video_receiver_info.jitter_buffer_delay_seconds;
903 video_track_stats->jitter_buffer_emitted_count =
904 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 11:56:50905 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 15:39:09906 // TODO(hbos): When we support receiving simulcast, this should be the total
907 // number of frames correctly decoded, independent of which SSRC it was
908 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 13:08:34909 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 15:39:09910 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
Johannes Kron0c141c52019-08-26 13:04:43911 video_track_stats->frames_dropped = video_receiver_info.frames_dropped;
Sergey Silkin02371062019-01-31 15:45:42912 video_track_stats->freeze_count = video_receiver_info.freeze_count;
913 video_track_stats->pause_count = video_receiver_info.pause_count;
914 video_track_stats->total_freezes_duration =
915 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
916 rtc::kNumMillisecsPerSec;
917 video_track_stats->total_pauses_duration =
918 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
919 rtc::kNumMillisecsPerSec;
920 video_track_stats->total_frames_duration =
921 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
922 rtc::kNumMillisecsPerSec;
923 video_track_stats->sum_squared_frame_durations =
924 video_receiver_info.sum_squared_frame_durations;
925
hbos9e302742017-01-20 10:47:10926 return video_track_stats;
927}
928
Harald Alvestrand89061872018-01-02 13:08:34929void ProduceSenderMediaTrackStats(
930 int64_t timestamp_us,
931 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50932 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 13:08:34933 RTCStatsReport* report) {
934 // This function iterates over the senders to generate outgoing track stats.
935
936 // TODO(hbos): Return stats of detached tracks. We have to perform stats
937 // gathering at the time of detachment to get accurate stats and timestamps.
938 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 16:29:42939 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 13:08:34940 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
941 AudioTrackInterface* track =
942 static_cast<AudioTrackInterface*>(sender->track().get());
943 if (!track)
944 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16945 cricket::VoiceSenderInfo null_sender_info;
946 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
947 // TODO(hta): Checking on ssrc is not proper. There should be a way
948 // to see from a sender whether it's connected or not.
949 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 23:19:50950 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29951 // When pc.close is called, sender info is discarded, so
952 // we generate zeroes instead. Bug: It should be retained.
953 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50954 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16955 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29956 if (sender_info) {
957 voice_sender_info = sender_info;
958 } else {
959 RTC_LOG(LS_INFO)
960 << "RTCStatsCollector: No voice sender info for sender with ssrc "
961 << sender->ssrc();
962 }
Harald Alvestrandb8e12012018-01-23 14:28:16963 }
Harald Alvestrand89061872018-01-02 13:08:34964 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19965 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
966 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34967 report->AddStats(std::move(audio_track_stats));
968 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
969 VideoTrackInterface* track =
970 static_cast<VideoTrackInterface*>(sender->track().get());
971 if (!track)
972 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16973 cricket::VideoSenderInfo null_sender_info;
974 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
975 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 13:43:29976 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
977 // "none")
Steve Anton57858b32018-02-15 23:19:50978 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29979 // When pc.close is called, sender info is discarded, so
980 // we generate zeroes instead. Bug: It should be retained.
981 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50982 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16983 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29984 if (sender_info) {
985 video_sender_info = sender_info;
986 } else {
987 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
988 << sender->ssrc();
989 }
990 }
Harald Alvestrand89061872018-01-02 13:08:34991 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19992 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
993 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34994 report->AddStats(std::move(video_track_stats));
995 } else {
996 RTC_NOTREACHED();
997 }
998 }
999}
1000
1001void ProduceReceiverMediaTrackStats(
1002 int64_t timestamp_us,
1003 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:501004 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 13:08:341005 RTCStatsReport* report) {
1006 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 16:29:421007 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 13:08:341008 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
1009 AudioTrackInterface* track =
1010 static_cast<AudioTrackInterface*>(receiver->track().get());
1011 const cricket::VoiceReceiverInfo* voice_receiver_info =
1012 track_media_info_map.GetVoiceReceiverInfo(*track);
1013 if (!voice_receiver_info) {
1014 continue;
1015 }
1016 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
1017 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:191018 timestamp_us, *track, *voice_receiver_info,
1019 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:341020 report->AddStats(std::move(audio_track_stats));
1021 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
1022 VideoTrackInterface* track =
1023 static_cast<VideoTrackInterface*>(receiver->track().get());
1024 const cricket::VideoReceiverInfo* video_receiver_info =
1025 track_media_info_map.GetVideoReceiverInfo(*track);
1026 if (!video_receiver_info) {
1027 continue;
1028 }
1029 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
1030 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:191031 timestamp_us, *track, *video_receiver_info,
1032 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:341033 report->AddStats(std::move(video_track_stats));
1034 } else {
1035 RTC_NOTREACHED();
1036 }
1037 }
1038}
1039
Henrik Boström5b3541f2018-03-19 12:52:561040rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
1041 bool filter_by_sender_selector,
1042 rtc::scoped_refptr<const RTCStatsReport> report,
1043 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
1044 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
1045 std::vector<std::string> rtpstream_ids;
1046 if (filter_by_sender_selector) {
1047 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
1048 if (sender_selector) {
1049 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
1050 // reference the sender stats.
1051 // Because we do not implement sender stats, we look at outbound-rtp(s)
1052 // that reference the track attachment stats for the sender instead.
1053 std::string track_id =
1054 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1055 kSender, sender_selector->AttachmentId());
1056 for (const auto& stats : *report) {
1057 if (stats.type() != RTCOutboundRTPStreamStats::kType)
1058 continue;
1059 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
1060 if (outbound_rtp.track_id.is_defined() &&
1061 *outbound_rtp.track_id == track_id) {
1062 rtpstream_ids.push_back(outbound_rtp.id());
1063 }
1064 }
1065 }
1066 } else {
1067 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
1068 if (receiver_selector) {
1069 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
1070 // reference the receiver stats.
1071 // Because we do not implement receiver stats, we look at inbound-rtp(s)
1072 // that reference the track attachment stats for the receiver instead.
1073 std::string track_id =
1074 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1075 kReceiver, receiver_selector->AttachmentId());
1076 for (const auto& stats : *report) {
1077 if (stats.type() != RTCInboundRTPStreamStats::kType)
1078 continue;
1079 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
1080 if (inbound_rtp.track_id.is_defined() &&
1081 *inbound_rtp.track_id == track_id) {
1082 rtpstream_ids.push_back(inbound_rtp.id());
1083 }
1084 }
1085 }
1086 }
1087 if (rtpstream_ids.empty())
1088 return RTCStatsReport::Create(report->timestamp_us());
1089 return TakeReferencedStats(report->Copy(), rtpstream_ids);
1090}
1091
hboscc555c52016-10-18 19:48:311092} // namespace
1093
Henrik Boström5b3541f2018-03-19 12:52:561094RTCStatsCollector::RequestInfo::RequestInfo(
1095 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1096 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
1097
1098RTCStatsCollector::RequestInfo::RequestInfo(
1099 rtc::scoped_refptr<RtpSenderInternal> selector,
1100 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1101 : RequestInfo(FilterMode::kSenderSelector,
1102 std::move(callback),
1103 std::move(selector),
1104 nullptr) {}
1105
1106RTCStatsCollector::RequestInfo::RequestInfo(
1107 rtc::scoped_refptr<RtpReceiverInternal> selector,
1108 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1109 : RequestInfo(FilterMode::kReceiverSelector,
1110 std::move(callback),
1111 nullptr,
1112 std::move(selector)) {}
1113
1114RTCStatsCollector::RequestInfo::RequestInfo(
1115 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
1116 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
1117 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
1118 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
1119 : filter_mode_(filter_mode),
1120 callback_(std::move(callback)),
1121 sender_selector_(std::move(sender_selector)),
1122 receiver_selector_(std::move(receiver_selector)) {
1123 RTC_DCHECK(callback_);
1124 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
1125}
1126
hbosc82f2e12016-09-05 08:36:501127rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-24 00:38:461128 PeerConnectionInternal* pc,
1129 int64_t cache_lifetime_us) {
Tommi87f70902021-04-27 12:43:081130 return rtc::make_ref_counted<RTCStatsCollector>(pc, cache_lifetime_us);
hbosc82f2e12016-09-05 08:36:501131}
1132
Steve Anton2d8609c2018-01-24 00:38:461133RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 08:36:501134 int64_t cache_lifetime_us)
hbosd565b732016-08-30 21:04:351135 : pc_(pc),
Steve Anton978b8762017-09-29 19:15:021136 signaling_thread_(pc->signaling_thread()),
1137 worker_thread_(pc->worker_thread()),
1138 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 08:36:501139 num_pending_partial_reports_(0),
1140 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 08:49:311141 network_report_event_(true /* manual_reset */,
1142 true /* initially_signaled */),
hbos0e6758d2016-08-31 14:57:361143 cache_timestamp_us_(0),
1144 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 21:04:351145 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 08:36:501146 RTC_DCHECK(signaling_thread_);
1147 RTC_DCHECK(worker_thread_);
1148 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 14:57:361149 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Taylor Brandstetter3a034e12020-07-09 22:32:341150 pc_->SignalSctpDataChannelCreated().connect(
1151 this, &RTCStatsCollector::OnSctpDataChannelCreated);
hbosd565b732016-08-30 21:04:351152}
1153
hbosb78306a2016-12-19 13:06:571154RTCStatsCollector::~RTCStatsCollector() {
1155 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1156}
1157
hbosc82f2e12016-09-05 08:36:501158void RTCStatsCollector::GetStatsReport(
1159 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 12:52:561160 GetStatsReportInternal(RequestInfo(std::move(callback)));
1161}
1162
1163void RTCStatsCollector::GetStatsReport(
1164 rtc::scoped_refptr<RtpSenderInternal> selector,
1165 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1166 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1167}
1168
1169void RTCStatsCollector::GetStatsReport(
1170 rtc::scoped_refptr<RtpReceiverInternal> selector,
1171 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1172 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1173}
1174
1175void RTCStatsCollector::GetStatsReportInternal(
1176 RTCStatsCollector::RequestInfo request) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051177 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boström5b3541f2018-03-19 12:52:561178 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 08:36:501179
hbos0e6758d2016-08-31 14:57:361180 // "Now" using a monotonically increasing timer.
1181 int64_t cache_now_us = rtc::TimeMicros();
1182 if (cached_report_ &&
1183 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 17:53:471184 // We have a fresh cached report to deliver. Deliver asynchronously, since
1185 // the caller may not be expecting a synchronous callback, and it avoids
1186 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 12:52:561187 std::vector<RequestInfo> requests;
1188 requests.swap(requests_);
Niels Möller4bab23f2021-01-18 08:24:331189
1190 // Task subclass to take ownership of the requests.
1191 // TODO(nisse): Delete when we can use C++14, and do lambda capture with
1192 // std::move.
1193 class DeliveryTask : public QueuedTask {
1194 public:
1195 DeliveryTask(rtc::scoped_refptr<RTCStatsCollector> collector,
1196 rtc::scoped_refptr<const RTCStatsReport> cached_report,
1197 std::vector<RequestInfo> requests)
1198 : collector_(collector),
1199 cached_report_(cached_report),
1200 requests_(std::move(requests)) {}
1201 bool Run() override {
1202 collector_->DeliverCachedReport(cached_report_, std::move(requests_));
1203 return true;
1204 }
1205
1206 private:
1207 rtc::scoped_refptr<RTCStatsCollector> collector_;
1208 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
1209 std::vector<RequestInfo> requests_;
1210 };
1211 signaling_thread_->PostTask(std::make_unique<DeliveryTask>(
1212 this, cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 08:36:501213 } else if (!num_pending_partial_reports_) {
1214 // Only start gathering stats if we're not already gathering stats. In the
1215 // case of already gathering stats, |callback_| will be invoked when there
1216 // are no more pending partial reports.
1217
1218 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
1219 // UTC), in microseconds. The system clock could be modified and is not
1220 // necessarily monotonically increasing.
nissecdf37a92016-09-14 06:41:471221 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 08:36:501222
hbosf415f8a2017-01-02 12:28:511223 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 08:36:501224 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 12:58:021225
Henrik Boström180faeb2020-11-13 08:05:211226 // Prepare |transceiver_stats_infos_| and |call_stats_| for use in
hbos84abeb12017-01-16 14:16:441227 // |ProducePartialResultsOnNetworkThread| and
1228 // |ProducePartialResultsOnSignalingThread|.
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571229 PrepareTransceiverStatsInfosAndCallStats_s_w_n();
Henrik Boström40b030e2019-02-28 08:49:311230 // Don't touch |network_report_| on the signaling thread until
1231 // ProducePartialResultsOnNetworkThread() has signaled the
1232 // |network_report_event_|.
1233 network_report_event_.Reset();
Niels Möller4bab23f2021-01-18 08:24:331234 rtc::scoped_refptr<RTCStatsCollector> collector(this);
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571235 network_thread_->PostTask(
1236 RTC_FROM_HERE,
1237 [collector, sctp_transport_name = pc_->sctp_transport_name(),
1238 timestamp_us]() mutable {
1239 collector->ProducePartialResultsOnNetworkThread(
1240 timestamp_us, std::move(sctp_transport_name));
1241 });
hbosf415f8a2017-01-02 12:28:511242 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 14:57:361243 }
hbosd565b732016-08-30 21:04:351244}
1245
1246void RTCStatsCollector::ClearCachedStatsReport() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051247 RTC_DCHECK_RUN_ON(signaling_thread_);
hbosd565b732016-08-30 21:04:351248 cached_report_ = nullptr;
1249}
1250
hbosb78306a2016-12-19 13:06:571251void RTCStatsCollector::WaitForPendingRequest() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051252 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boström40b030e2019-02-28 08:49:311253 // If a request is pending, blocks until the |network_report_event_| is
1254 // signaled and then delivers the result. Otherwise this is a NO-OP.
1255 MergeNetworkReport_s();
hbosb78306a2016-12-19 13:06:571256}
1257
hbosc82f2e12016-09-05 08:36:501258void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1259 int64_t timestamp_us) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051260 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501261 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1262
Henrik Boström40b030e2019-02-28 08:49:311263 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501264
Henrik Boström40b030e2019-02-28 08:49:311265 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1266 partial_report_.get());
hbosc82f2e12016-09-05 08:36:501267
Henrik Boström40b030e2019-02-28 08:49:311268 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1269 // signaling thread, so it is always the first partial result delivered on the
1270 // signaling thread. The request is not complete until MergeNetworkReport_s()
1271 // happens; we don't have to do anything here.
1272 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1273 --num_pending_partial_reports_;
1274}
1275
1276void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1277 int64_t timestamp_us,
1278 RTCStatsReport* partial_report) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051279 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501280 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1281
Henrik Boström40b030e2019-02-28 08:49:311282 ProduceDataChannelStats_s(timestamp_us, partial_report);
1283 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1284 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 13:49:421285 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311286 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 08:36:501287}
1288
hbosc82f2e12016-09-05 08:36:501289void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571290 int64_t timestamp_us,
1291 absl::optional<std::string> sctp_transport_name) {
Markus Handell518669d2021-06-07 11:30:461292 TRACE_EVENT0("webrtc",
1293 "RTCStatsCollector::ProducePartialResultsOnNetworkThread");
Tomas Gunnarsson67b1fa22021-04-08 13:18:051294 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501295 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1296
Ivo Creusen8d8ffdb2019-04-30 07:45:211297 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 08:49:311298 // |network_report_event_| is reset before this method is invoked.
1299 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501300
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571301 std::set<std::string> transport_names;
1302 if (sctp_transport_name) {
1303 transport_names.emplace(std::move(*sctp_transport_name));
1304 }
1305
1306 for (const auto& info : transceiver_stats_infos_) {
1307 if (info.transport_name)
1308 transport_names.insert(*info.transport_name);
1309 }
1310
Steve Anton5dfde182018-02-06 18:34:401311 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Tomas Gunnarssonbfd9ba82021-04-18 09:55:571312 pc_->GetTransportStatsByNames(transport_names);
Steve Anton5dfde182018-02-06 18:34:401313 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1314 PrepareTransportCertificateStats_n(transport_stats_by_name);
1315
Henrik Boström40b030e2019-02-28 08:49:311316 ProducePartialResultsOnNetworkThreadImpl(
1317 timestamp_us, transport_stats_by_name, transport_cert_stats,
1318 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:401319
Henrik Boström40b030e2019-02-28 08:49:311320 // Signal that it is now safe to touch |network_report_| on the signaling
1321 // thread, and post a task to merge it into the final results.
1322 network_report_event_.Set();
Niels Möller4bab23f2021-01-18 08:24:331323 rtc::scoped_refptr<RTCStatsCollector> collector(this);
Henrik Boström40b030e2019-02-28 08:49:311324 signaling_thread_->PostTask(
Niels Möller4bab23f2021-01-18 08:24:331325 RTC_FROM_HERE, [collector] { collector->MergeNetworkReport_s(); });
Henrik Boström05d43c62019-02-15 09:23:081326}
1327
Henrik Boström40b030e2019-02-28 08:49:311328void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1329 int64_t timestamp_us,
1330 const std::map<std::string, cricket::TransportStats>&
1331 transport_stats_by_name,
1332 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1333 RTCStatsReport* partial_report) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051334 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501335 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1336
Henrik Boström40b030e2019-02-28 08:49:311337 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1338 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1339 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1340 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311341 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1342 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 11:40:251343 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1344 partial_report);
Henrik Boström40b030e2019-02-28 08:49:311345}
1346
1347void RTCStatsCollector::MergeNetworkReport_s() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051348 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boström40b030e2019-02-28 08:49:311349 // The |network_report_event_| must be signaled for it to be safe to touch
1350 // |network_report_|. This is normally not blocking, but if
1351 // WaitForPendingRequest() is called while a request is pending, we might have
1352 // to wait until the network thread is done touching |network_report_|.
1353 network_report_event_.Wait(rtc::Event::kForever);
1354 if (!network_report_) {
1355 // Normally, MergeNetworkReport_s() is executed because it is posted from
1356 // the network thread. But if WaitForPendingRequest() is called while a
1357 // request is pending, an early call to MergeNetworkReport_s() is made,
1358 // merging the report and setting |network_report_| to null. If so, when the
1359 // previously posted MergeNetworkReport_s() is later executed, the report is
1360 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 08:36:501361 return;
1362 }
Mirko Bonadeica890ee2019-02-15 21:10:401363 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 08:49:311364 RTC_DCHECK(partial_report_);
1365 partial_report_->TakeMembersFrom(network_report_);
1366 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:401367 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 08:49:311368 // |network_report_| is currently the only partial report collected
1369 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1370 // ready to deliver the result.
1371 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1372 cache_timestamp_us_ = partial_report_timestamp_us_;
1373 cached_report_ = partial_report_;
1374 partial_report_ = nullptr;
1375 transceiver_stats_infos_.clear();
1376 // Trace WebRTC Stats when getStats is called on Javascript.
1377 // This allows access to WebRTC stats from trace logs. To enable them,
1378 // select the "webrtc_stats" category when recording traces.
1379 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1380 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:401381
Henrik Boström40b030e2019-02-28 08:49:311382 // Deliver report and clear |requests_|.
1383 std::vector<RequestInfo> requests;
1384 requests.swap(requests_);
1385 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 08:36:501386}
1387
Taylor Brandstetter25e022f2018-03-08 17:53:471388void RTCStatsCollector::DeliverCachedReport(
1389 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 12:52:561390 std::vector<RTCStatsCollector::RequestInfo> requests) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051391 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boström5b3541f2018-03-19 12:52:561392 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 17:53:471393 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 17:42:251394
Henrik Boström5b3541f2018-03-19 12:52:561395 for (const RequestInfo& request : requests) {
1396 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1397 request.callback()->OnStatsDelivered(cached_report);
1398 } else {
1399 bool filter_by_sender_selector;
1400 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1401 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1402 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1403 filter_by_sender_selector = true;
1404 sender_selector = request.sender_selector();
1405 } else {
1406 RTC_DCHECK(request.filter_mode() ==
1407 RequestInfo::FilterMode::kReceiverSelector);
1408 filter_by_sender_selector = false;
1409 receiver_selector = request.receiver_selector();
1410 }
1411 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1412 filter_by_sender_selector, cached_report, sender_selector,
1413 receiver_selector));
1414 }
hbosc82f2e12016-09-05 08:36:501415 }
hbosd565b732016-08-30 21:04:351416}
1417
hbosdf6075a2016-12-19 12:58:021418void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 11:00:051419 int64_t timestamp_us,
1420 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce02016-10-03 21:16:561421 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051422 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501423 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1424
hbos02ba2112016-10-28 12:14:531425 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1426 if (transport_cert_stats_pair.second.local) {
1427 ProduceCertificateStatsFromSSLCertificateStats(
1428 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce02016-10-03 21:16:561429 }
hbos02ba2112016-10-28 12:14:531430 if (transport_cert_stats_pair.second.remote) {
1431 ProduceCertificateStatsFromSSLCertificateStats(
1432 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce02016-10-03 21:16:561433 }
1434 }
1435}
1436
hbosdf6075a2016-12-19 12:58:021437void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 23:19:501438 int64_t timestamp_us,
1439 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 10:32:061440 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051441 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501442 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1443
Steve Anton57858b32018-02-15 23:19:501444 for (const auto& stats : transceiver_stats_infos) {
1445 if (!stats.mid) {
1446 continue;
hbos0adb8282016-11-23 10:32:061447 }
Philipp Hancke95157a02020-11-16 19:08:271448 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1449 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1450
Steve Anton57858b32018-02-15 23:19:501451 const cricket::VoiceMediaInfo* voice_media_info =
1452 stats.track_media_info_map->voice_media_info();
1453 const cricket::VideoMediaInfo* video_media_info =
1454 stats.track_media_info_map->video_media_info();
1455 // Audio
1456 if (voice_media_info) {
1457 // Inbound
1458 for (const auto& pair : voice_media_info->receive_codecs) {
1459 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271460 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501461 }
1462 // Outbound
1463 for (const auto& pair : voice_media_info->send_codecs) {
1464 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271465 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501466 }
Guido Urdanetaee2388f2018-02-15 16:36:191467 }
Steve Anton57858b32018-02-15 23:19:501468 // Video
1469 if (video_media_info) {
1470 // Inbound
1471 for (const auto& pair : video_media_info->receive_codecs) {
1472 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271473 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501474 }
1475 // Outbound
1476 for (const auto& pair : video_media_info->send_codecs) {
1477 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271478 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501479 }
hbos0adb8282016-11-23 10:32:061480 }
1481 }
1482}
1483
hboscc555c52016-10-18 19:48:311484void RTCStatsCollector::ProduceDataChannelStats_s(
Jonas Olssona4d87372019-07-05 17:08:331485 int64_t timestamp_us,
1486 RTCStatsReport* report) const {
Tomas Gunnarsson2e94de52020-06-16 14:54:101487 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501488 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Taylor Brandstetter3a034e12020-07-09 22:32:341489 std::vector<DataChannelStats> data_stats = pc_->GetDataChannelStats();
Tomas Gunnarsson2e94de52020-06-16 14:54:101490 for (const auto& stats : data_stats) {
hboscc555c52016-10-18 19:48:311491 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1492 new RTCDataChannelStats(
Tomas Gunnarsson2e94de52020-06-16 14:54:101493 "RTCDataChannel_" + rtc::ToString(stats.internal_id),
hboscc555c52016-10-18 19:48:311494 timestamp_us));
Tomas Gunnarsson2e94de52020-06-16 14:54:101495 data_channel_stats->label = std::move(stats.label);
1496 data_channel_stats->protocol = std::move(stats.protocol);
1497 data_channel_stats->data_channel_identifier = stats.id;
1498 data_channel_stats->state = DataStateToRTCDataChannelState(stats.state);
1499 data_channel_stats->messages_sent = stats.messages_sent;
1500 data_channel_stats->bytes_sent = stats.bytes_sent;
1501 data_channel_stats->messages_received = stats.messages_received;
1502 data_channel_stats->bytes_received = stats.bytes_received;
hboscc555c52016-10-18 19:48:311503 report->AddStats(std::move(data_channel_stats));
1504 }
1505}
1506
hbosdf6075a2016-12-19 12:58:021507void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 13:44:031508 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 18:34:401509 const std::map<std::string, cricket::TransportStats>&
1510 transport_stats_by_name,
stefanf79ade12017-06-02 13:44:031511 const Call::Stats& call_stats,
1512 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051513 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501514 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1515
Steve Anton5dfde182018-02-06 18:34:401516 for (const auto& entry : transport_stats_by_name) {
1517 const std::string& transport_name = entry.first;
1518 const cricket::TransportStats& transport_stats = entry.second;
1519 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 09:50:141520 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401521 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 21:54:491522 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:271523 channel_stats.ice_transport_stats.connection_infos) {
hbosc47a0c32016-10-11 21:54:491524 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 11:00:051525 new RTCIceCandidatePairStats(
1526 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1527 timestamp_us));
hbosc47a0c32016-10-11 21:54:491528
hbos0583b282016-11-30 09:50:141529 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 09:18:471530 // TODO(hbos): There could be other candidates that are not paired with
1531 // anything. We don't have a complete list. Local candidates come from
1532 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 13:08:341533 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 12:14:531534 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311535 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 12:14:531536 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311537 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 16:08:181538 candidate_pair_stats->state =
1539 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1540 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 09:38:081541 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 21:54:491542 // TODO(hbos): This writable is different than the spec. It goes to
1543 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 13:08:341544 // https://crbug.com/633550
hbosc47a0c32016-10-11 21:54:491545 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 21:54:491546 candidate_pair_stats->bytes_sent =
1547 static_cast<uint64_t>(info.sent_total_bytes);
1548 candidate_pair_stats->bytes_received =
1549 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 14:34:471550 candidate_pair_stats->total_round_trip_time =
1551 static_cast<double>(info.total_round_trip_time_ms) /
1552 rtc::kNumMillisecsPerSec;
1553 if (info.current_round_trip_time_ms) {
1554 candidate_pair_stats->current_round_trip_time =
1555 static_cast<double>(*info.current_round_trip_time_ms) /
1556 rtc::kNumMillisecsPerSec;
1557 }
stefanf79ade12017-06-02 13:44:031558 if (info.best_connection) {
hbos338f78a2017-02-07 14:41:211559 // The bandwidth estimations we have are for the selected candidate
1560 // pair ("info.best_connection").
stefanf79ade12017-06-02 13:44:031561 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1562 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1563 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211564 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 13:44:031565 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211566 }
stefanf79ade12017-06-02 13:44:031567 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211568 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 13:44:031569 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211570 }
1571 }
hbosd82f5122016-12-09 12:12:391572 candidate_pair_stats->requests_received =
1573 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 09:22:531574 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1575 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491576 candidate_pair_stats->responses_received =
1577 static_cast<uint64_t>(info.recv_ping_responses);
1578 candidate_pair_stats->responses_sent =
1579 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 09:22:531580 RTC_DCHECK_GE(info.sent_ping_requests_total,
1581 info.sent_ping_requests_before_first_response);
1582 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1583 info.sent_ping_requests_total -
1584 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491585
1586 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 09:18:471587 }
1588 }
1589 }
1590}
1591
Steve Anton57858b32018-02-15 23:19:501592void RTCStatsCollector::ProduceMediaStreamStats_s(
1593 int64_t timestamp_us,
1594 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051595 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501596 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:501597
1598 std::map<std::string, std::vector<std::string>> track_ids;
1599
1600 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 16:29:421601 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501602 std::string track_id =
1603 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1604 kSender, sender->internal()->AttachmentId());
1605 for (auto& stream_id : sender->stream_ids()) {
1606 track_ids[stream_id].push_back(track_id);
1607 }
1608 }
Mirko Bonadei739baf02019-01-27 16:29:421609 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501610 std::string track_id =
1611 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1612 kReceiver, receiver->internal()->AttachmentId());
1613 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 23:05:281614 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 23:19:501615 }
1616 }
1617 }
1618
1619 // Build stats for each stream ID known.
1620 for (auto& it : track_ids) {
1621 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1622 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1623 stream_stats->stream_identifier = it.first;
1624 stream_stats->track_ids = it.second;
1625 report->AddStats(std::move(stream_stats));
1626 }
1627}
1628
1629void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1630 int64_t timestamp_us,
1631 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051632 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501633 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1634
Steve Anton57858b32018-02-15 23:19:501635 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1636 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 16:29:421637 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501638 senders.push_back(sender->internal());
1639 }
1640 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1641 senders, report);
1642
1643 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 16:29:421644 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501645 receivers.push_back(receiver->internal());
1646 }
1647 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1648 receivers, report);
1649 }
hbos09bc1282016-11-08 14:29:221650}
1651
Henrik Boström646fda02019-05-22 13:49:421652void RTCStatsCollector::ProduceMediaSourceStats_s(
1653 int64_t timestamp_us,
1654 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051655 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501656 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1657
Henrik Boström646fda02019-05-22 13:49:421658 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1659 transceiver_stats_infos_) {
1660 const auto& track_media_info_map =
1661 transceiver_stats_info.track_media_info_map;
1662 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1663 const auto& sender_internal = sender->internal();
1664 const auto& track = sender_internal->track();
1665 if (!track)
1666 continue;
Henrik Boströmd2c336f2019-07-03 15:11:101667 // TODO(https://crbug.com/webrtc/10771): The same track could be attached
1668 // to multiple senders which should result in multiple senders referencing
1669 // the same media-source stats. When all media source related metrics are
1670 // moved to the track's source (e.g. input frame rate is moved from
1671 // cricket::VideoSenderInfo to VideoTrackSourceInterface::Stats and audio
1672 // levels are moved to the corresponding audio track/source object), don't
1673 // create separate media source stats objects on a per-attachment basis.
Henrik Boström646fda02019-05-22 13:49:421674 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1675 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Taylor Brandstetter64851c02021-06-24 20:32:501676 AudioTrackInterface* audio_track =
1677 static_cast<AudioTrackInterface*>(track.get());
Mirko Bonadei317a1f02019-09-17 15:06:181678 auto audio_source_stats = std::make_unique<RTCAudioSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421679 RTCMediaSourceStatsIDFromKindAndAttachment(
1680 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1681 timestamp_us);
Henrik Boströmd2c336f2019-07-03 15:11:101682 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1683 // SSRC assigned (there shouldn't need to exist a send-stream, created
1684 // by an O/A exchange) in order to read audio media-source stats.
1685 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1686 // value indicating no SSRC.
1687 if (sender_internal->ssrc() != 0) {
1688 auto* voice_sender_info =
1689 track_media_info_map->GetVoiceSenderInfoBySsrc(
1690 sender_internal->ssrc());
1691 if (voice_sender_info) {
1692 audio_source_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
1693 voice_sender_info->audio_level);
1694 audio_source_stats->total_audio_energy =
1695 voice_sender_info->total_input_energy;
1696 audio_source_stats->total_samples_duration =
1697 voice_sender_info->total_input_duration;
Taylor Brandstetter64851c02021-06-24 20:32:501698 SetAudioProcessingStats(audio_source_stats.get(),
1699 voice_sender_info->apm_statistics);
Henrik Boströmd2c336f2019-07-03 15:11:101700 }
1701 }
Taylor Brandstetter64851c02021-06-24 20:32:501702 // Audio processor may be attached to either the track or the send
1703 // stream, so look in both places.
1704 auto audio_processor(audio_track->GetAudioProcessor());
1705 if (audio_processor.get()) {
1706 // The |has_remote_tracks| argument is obsolete; makes no difference
1707 // if it's set to true or false.
1708 AudioProcessorInterface::AudioProcessorStatistics ap_stats =
1709 audio_processor->GetStats(/*has_remote_tracks=*/false);
1710 SetAudioProcessingStats(audio_source_stats.get(),
1711 ap_stats.apm_statistics);
1712 }
Henrik Boströmd2c336f2019-07-03 15:11:101713 media_source_stats = std::move(audio_source_stats);
Henrik Boström646fda02019-05-22 13:49:421714 } else {
1715 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Mirko Bonadei317a1f02019-09-17 15:06:181716 auto video_source_stats = std::make_unique<RTCVideoSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421717 RTCMediaSourceStatsIDFromKindAndAttachment(
1718 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1719 timestamp_us);
1720 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1721 auto* video_source = video_track->GetSource();
1722 VideoTrackSourceInterface::Stats source_stats;
1723 if (video_source && video_source->GetStats(&source_stats)) {
1724 video_source_stats->width = source_stats.input_width;
1725 video_source_stats->height = source_stats.input_height;
1726 }
Henrik Boströmd2c336f2019-07-03 15:11:101727 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1728 // SSRC assigned (there shouldn't need to exist a send-stream, created
1729 // by an O/A exchange) in order to get framesPerSecond.
1730 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1731 // value indicating no SSRC.
Henrik Boström646fda02019-05-22 13:49:421732 if (sender_internal->ssrc() != 0) {
Henrik Boströmd2c336f2019-07-03 15:11:101733 auto* video_sender_info =
1734 track_media_info_map->GetVideoSenderInfoBySsrc(
1735 sender_internal->ssrc());
1736 if (video_sender_info) {
Henrik Boström646fda02019-05-22 13:49:421737 video_source_stats->frames_per_second =
Henrik Boströmd2c336f2019-07-03 15:11:101738 video_sender_info->framerate_input;
Di Wu668dbf62021-02-27 08:29:151739 video_source_stats->frames = video_sender_info->frames;
Henrik Boström646fda02019-05-22 13:49:421740 }
1741 }
1742 media_source_stats = std::move(video_source_stats);
1743 }
1744 media_source_stats->track_identifier = track->id();
1745 media_source_stats->kind = track->kind();
1746 report->AddStats(std::move(media_source_stats));
1747 }
1748 }
1749}
1750
hbos6ab97ce02016-10-03 21:16:561751void RTCStatsCollector::ProducePeerConnectionStats_s(
Jonas Olssona4d87372019-07-05 17:08:331752 int64_t timestamp_us,
1753 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051754 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501755 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1756
hbosd565b732016-08-30 21:04:351757 std::unique_ptr<RTCPeerConnectionStats> stats(
Jonas Olssona4d87372019-07-05 17:08:331758 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 09:41:091759 stats->data_channels_opened = internal_record_.data_channels_opened;
1760 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce02016-10-03 21:16:561761 report->AddStats(std::move(stats));
hbosd565b732016-08-30 21:04:351762}
1763
hbosdf6075a2016-12-19 12:58:021764void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 19:44:481765 int64_t timestamp_us,
Steve Anton57858b32018-02-15 23:19:501766 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 14:16:441767 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051768 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501769 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
hbos6ded1902016-11-01 08:50:461770
Steve Anton57858b32018-02-15 23:19:501771 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1772 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1773 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1774 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1775 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1776 } else {
1777 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:191778 }
Steve Anton56bae8d2018-02-15 00:07:421779 }
Steve Anton57858b32018-02-15 23:19:501780}
1781
1782void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1783 int64_t timestamp_us,
1784 const RtpTransceiverStatsInfo& stats,
1785 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051786 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501787 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1788
Steve Anton57858b32018-02-15 23:19:501789 if (!stats.mid || !stats.transport_name) {
1790 return;
1791 }
1792 RTC_DCHECK(stats.track_media_info_map);
1793 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1794 RTC_DCHECK(track_media_info_map.voice_media_info());
1795 std::string mid = *stats.mid;
1796 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1797 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
Alessio Bazzicaf7b1b952021-03-23 16:23:041798 // Inbound and remote-outbound.
1799 // The remote-outbound stats are based on RTCP sender reports sent from the
1800 // remote endpoint providing metrics about the remote outbound streams.
Steve Anton57858b32018-02-15 23:19:501801 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1802 track_media_info_map.voice_media_info()->receivers) {
1803 if (!voice_receiver_info.connected())
1804 continue;
Alessio Bazzicaf7b1b952021-03-23 16:23:041805 // Inbound.
1806 auto inbound_audio =
1807 CreateInboundAudioStreamStats(voice_receiver_info, mid, timestamp_us);
Steve Anton57858b32018-02-15 23:19:501808 // TODO(hta): This lookup should look for the sender, not the track.
1809 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1810 track_media_info_map.GetAudioTrack(voice_receiver_info);
1811 if (audio_track) {
1812 inbound_audio->track_id =
1813 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1814 kReceiver,
1815 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 08:50:461816 }
Steve Anton57858b32018-02-15 23:19:501817 inbound_audio->transport_id = transport_id;
Alessio Bazzicaf7b1b952021-03-23 16:23:041818 // Remote-outbound.
1819 auto remote_outbound_audio = CreateRemoteOutboundAudioStreamStats(
1820 voice_receiver_info, mid, inbound_audio->id(), transport_id);
1821 // Add stats.
1822 if (remote_outbound_audio) {
1823 // When the remote outbound stats are available, the remote ID for the
1824 // local inbound stats is set.
1825 inbound_audio->remote_id = remote_outbound_audio->id();
1826 report->AddStats(std::move(remote_outbound_audio));
1827 }
Steve Anton57858b32018-02-15 23:19:501828 report->AddStats(std::move(inbound_audio));
1829 }
Alessio Bazzicaf7b1b952021-03-23 16:23:041830 // Outbound.
Henrik Boström4f40fa52019-12-19 12:27:271831 std::map<std::string, RTCOutboundRTPStreamStats*> audio_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501832 for (const cricket::VoiceSenderInfo& voice_sender_info :
1833 track_media_info_map.voice_media_info()->senders) {
1834 if (!voice_sender_info.connected())
1835 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181836 auto outbound_audio = std::make_unique<RTCOutboundRTPStreamStats>(
Alessio Bazzicaf7b1b952021-03-23 16:23:041837 RTCOutboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_AUDIO,
1838 voice_sender_info.ssrc()),
Steve Anton57858b32018-02-15 23:19:501839 timestamp_us);
1840 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1841 outbound_audio.get());
1842 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1843 track_media_info_map.GetAudioTrack(voice_sender_info);
1844 if (audio_track) {
Henrik Boström646fda02019-05-22 13:49:421845 int attachment_id =
1846 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 23:19:501847 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 13:49:421848 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1849 attachment_id);
1850 outbound_audio->media_source_id =
1851 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1852 attachment_id);
Steve Anton56bae8d2018-02-15 00:07:421853 }
Steve Anton57858b32018-02-15 23:19:501854 outbound_audio->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271855 audio_outbound_rtps.insert(
1856 std::make_pair(outbound_audio->id(), outbound_audio.get()));
Steve Anton57858b32018-02-15 23:19:501857 report->AddStats(std::move(outbound_audio));
1858 }
Alessio Bazzicaf7b1b952021-03-23 16:23:041859 // Remote-inbound.
Henrik Boström883eefc2019-05-27 11:40:251860 // These are Report Block-based, information sent from the remote endpoint,
1861 // providing metrics about our Outbound streams. We take advantage of the fact
1862 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1863 // been added to the report.
1864 for (const cricket::VoiceSenderInfo& voice_sender_info :
1865 track_media_info_map.voice_media_info()->senders) {
1866 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1867 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071868 report_block_data, cricket::MEDIA_TYPE_AUDIO, audio_outbound_rtps,
1869 *report));
Henrik Boström883eefc2019-05-27 11:40:251870 }
1871 }
Steve Anton57858b32018-02-15 23:19:501872}
1873
1874void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1875 int64_t timestamp_us,
1876 const RtpTransceiverStatsInfo& stats,
1877 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051878 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501879 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1880
Steve Anton57858b32018-02-15 23:19:501881 if (!stats.mid || !stats.transport_name) {
1882 return;
1883 }
1884 RTC_DCHECK(stats.track_media_info_map);
1885 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1886 RTC_DCHECK(track_media_info_map.video_media_info());
1887 std::string mid = *stats.mid;
1888 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1889 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1890 // Inbound
1891 for (const cricket::VideoReceiverInfo& video_receiver_info :
1892 track_media_info_map.video_media_info()->receivers) {
1893 if (!video_receiver_info.connected())
1894 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181895 auto inbound_video = std::make_unique<RTCInboundRTPStreamStats>(
Alessio Bazzicaf7b1b952021-03-23 16:23:041896 RTCInboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_VIDEO,
1897 video_receiver_info.ssrc()),
Steve Anton57858b32018-02-15 23:19:501898 timestamp_us);
1899 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1900 inbound_video.get());
1901 rtc::scoped_refptr<VideoTrackInterface> video_track =
1902 track_media_info_map.GetVideoTrack(video_receiver_info);
1903 if (video_track) {
1904 inbound_video->track_id =
1905 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1906 kReceiver,
1907 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1908 }
1909 inbound_video->transport_id = transport_id;
1910 report->AddStats(std::move(inbound_video));
Alessio Bazzicaf7b1b952021-03-23 16:23:041911 // TODO(crbug.com/webrtc/12529): Add remote-outbound stats.
Steve Anton57858b32018-02-15 23:19:501912 }
1913 // Outbound
Henrik Boström4f40fa52019-12-19 12:27:271914 std::map<std::string, RTCOutboundRTPStreamStats*> video_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501915 for (const cricket::VideoSenderInfo& video_sender_info :
Eldar Rello9276e2c2020-06-10 14:53:391916 track_media_info_map.video_media_info()->senders) {
Steve Anton57858b32018-02-15 23:19:501917 if (!video_sender_info.connected())
1918 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181919 auto outbound_video = std::make_unique<RTCOutboundRTPStreamStats>(
Alessio Bazzicaf7b1b952021-03-23 16:23:041920 RTCOutboundRTPStreamStatsIDFromSSRC(cricket::MEDIA_TYPE_VIDEO,
1921 video_sender_info.ssrc()),
Steve Anton57858b32018-02-15 23:19:501922 timestamp_us);
Eldar Rello9276e2c2020-06-10 14:53:391923 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1924 outbound_video.get());
Steve Anton57858b32018-02-15 23:19:501925 rtc::scoped_refptr<VideoTrackInterface> video_track =
1926 track_media_info_map.GetVideoTrack(video_sender_info);
1927 if (video_track) {
Henrik Boström646fda02019-05-22 13:49:421928 int attachment_id =
1929 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 23:19:501930 outbound_video->track_id =
Henrik Boström646fda02019-05-22 13:49:421931 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1932 attachment_id);
1933 outbound_video->media_source_id =
1934 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1935 attachment_id);
Steve Anton57858b32018-02-15 23:19:501936 }
1937 outbound_video->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271938 video_outbound_rtps.insert(
1939 std::make_pair(outbound_video->id(), outbound_video.get()));
Steve Anton57858b32018-02-15 23:19:501940 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 08:50:461941 }
Henrik Boström883eefc2019-05-27 11:40:251942 // Remote-inbound
1943 // These are Report Block-based, information sent from the remote endpoint,
1944 // providing metrics about our Outbound streams. We take advantage of the fact
1945 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1946 // been added to the report.
1947 for (const cricket::VideoSenderInfo& video_sender_info :
1948 track_media_info_map.video_media_info()->senders) {
1949 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1950 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071951 report_block_data, cricket::MEDIA_TYPE_VIDEO, video_outbound_rtps,
1952 *report));
Henrik Boström883eefc2019-05-27 11:40:251953 }
1954 }
hbos6ded1902016-11-01 08:50:461955}
1956
hbosdf6075a2016-12-19 12:58:021957void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 18:34:401958 int64_t timestamp_us,
1959 const std::map<std::string, cricket::TransportStats>&
1960 transport_stats_by_name,
hbos2fa7c672016-10-24 11:00:051961 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1962 RTCStatsReport* report) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:051963 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501964 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1965
Steve Anton5dfde182018-02-06 18:34:401966 for (const auto& entry : transport_stats_by_name) {
1967 const std::string& transport_name = entry.first;
1968 const cricket::TransportStats& transport_stats = entry.second;
1969
hbos2fa7c672016-10-24 11:00:051970 // Get reference to RTCP channel, if it exists.
1971 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 18:34:401972 for (const cricket::TransportChannelStats& channel_stats :
1973 transport_stats.channel_stats) {
Jonas Olssona4d87372019-07-05 17:08:331974 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
hbos2fa7c672016-10-24 11:00:051975 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401976 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 11:00:051977 break;
1978 }
1979 }
1980
1981 // Get reference to local and remote certificates of this transport, if they
1982 // exist.
Steve Anton5dfde182018-02-06 18:34:401983 const auto& certificate_stats_it =
1984 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 11:00:051985 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1986 std::string local_certificate_id;
1987 if (certificate_stats_it->second.local) {
1988 local_certificate_id = RTCCertificateIDFromFingerprint(
1989 certificate_stats_it->second.local->fingerprint);
1990 }
1991 std::string remote_certificate_id;
1992 if (certificate_stats_it->second.remote) {
1993 remote_certificate_id = RTCCertificateIDFromFingerprint(
1994 certificate_stats_it->second.remote->fingerprint);
1995 }
1996
1997 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 18:34:401998 for (const cricket::TransportChannelStats& channel_stats :
1999 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 11:00:052000 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 18:34:402001 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
2002 transport_name, channel_stats.component),
2003 timestamp_us));
hbos2fa7c672016-10-24 11:00:052004 transport_stats->bytes_sent = 0;
Artem Titovedacbd52020-07-06 14:06:372005 transport_stats->packets_sent = 0;
hbos2fa7c672016-10-24 11:00:052006 transport_stats->bytes_received = 0;
Artem Titovedacbd52020-07-06 14:06:372007 transport_stats->packets_received = 0;
Jonas Olssona4d87372019-07-05 17:08:332008 transport_stats->dtls_state =
2009 DtlsTransportStateToRTCDtlsTransportState(channel_stats.dtls_state);
Jonas Oreland149dc722019-08-28 06:10:272010 transport_stats->selected_candidate_pair_changes =
2011 channel_stats.ice_transport_stats.selected_candidate_pair_changes;
hbos2fa7c672016-10-24 11:00:052012 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:272013 channel_stats.ice_transport_stats.connection_infos) {
hbos2fa7c672016-10-24 11:00:052014 *transport_stats->bytes_sent += info.sent_total_bytes;
Artem Titovedacbd52020-07-06 14:06:372015 *transport_stats->packets_sent +=
2016 info.sent_total_packets - info.sent_discarded_packets;
hbos2fa7c672016-10-24 11:00:052017 *transport_stats->bytes_received += info.recv_total_bytes;
Artem Titovedacbd52020-07-06 14:06:372018 *transport_stats->packets_received += info.packets_received;
hbos2fa7c672016-10-24 11:00:052019 if (info.best_connection) {
hbos2fa7c672016-10-24 11:00:052020 transport_stats->selected_candidate_pair_id =
2021 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
2022 }
2023 }
2024 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
2025 !rtcp_transport_stats_id.empty()) {
2026 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
2027 }
2028 if (!local_certificate_id.empty())
2029 transport_stats->local_certificate_id = local_certificate_id;
2030 if (!remote_certificate_id.empty())
2031 transport_stats->remote_certificate_id = remote_certificate_id;
Harald Alvestrand5cb78072019-10-28 08:51:172032 // Crypto information
2033 if (channel_stats.ssl_version_bytes) {
2034 char bytes[5];
2035 snprintf(bytes, sizeof(bytes), "%04X", channel_stats.ssl_version_bytes);
2036 transport_stats->tls_version = bytes;
2037 }
2038 if (channel_stats.ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
2039 rtc::SSLStreamAdapter::SslCipherSuiteToName(
2040 channel_stats.ssl_cipher_suite)
2041 .length()) {
2042 transport_stats->dtls_cipher =
2043 rtc::SSLStreamAdapter::SslCipherSuiteToName(
2044 channel_stats.ssl_cipher_suite);
2045 }
2046 if (channel_stats.srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
2047 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite)
2048 .length()) {
2049 transport_stats->srtp_cipher =
2050 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite);
2051 }
hbos2fa7c672016-10-24 11:00:052052 report->AddStats(std::move(transport_stats));
2053 }
2054 }
2055}
2056
2057std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 12:58:022058RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 18:34:402059 const std::map<std::string, cricket::TransportStats>&
2060 transport_stats_by_name) const {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052061 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boströme88c95e2020-07-08 09:18:502062 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2063
hbos2fa7c672016-10-24 11:00:052064 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 18:34:402065 for (const auto& entry : transport_stats_by_name) {
2066 const std::string& transport_name = entry.first;
2067
hbos2fa7c672016-10-24 11:00:052068 CertificateStatsPair certificate_stats_pair;
2069 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 18:34:402070 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 11:00:052071 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 08:16:262072 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 11:00:052073 }
Steve Anton5dfde182018-02-06 18:34:402074
Taylor Brandstetterc3928662018-02-23 21:04:512075 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
2076 pc_->GetRemoteSSLCertChain(transport_name);
2077 if (remote_cert_chain) {
2078 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 11:00:052079 }
Steve Anton5dfde182018-02-06 18:34:402080
hbos2fa7c672016-10-24 11:00:052081 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 18:34:402082 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 11:00:052083 }
2084 return transport_cert_stats;
2085}
2086
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572087void RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w_n() {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052088 RTC_DCHECK_RUN_ON(signaling_thread_);
Steve Anton56bae8d2018-02-15 00:07:422089
Henrik Boström180faeb2020-11-13 08:05:212090 transceiver_stats_infos_.clear();
Steve Anton57858b32018-02-15 23:19:502091 // These are used to invoke GetStats for all the media channels together in
2092 // one worker thread hop.
2093 std::map<cricket::VoiceMediaChannel*,
2094 std::unique_ptr<cricket::VoiceMediaInfo>>
2095 voice_stats;
2096 std::map<cricket::VideoMediaChannel*,
2097 std::unique_ptr<cricket::VideoMediaInfo>>
2098 video_stats;
2099
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572100 auto transceivers = pc_->GetTransceiversInternal();
2101
2102 // TODO(tommi): See if we can avoid synchronously blocking the signaling
2103 // thread while we do this (or avoid the Invoke at all).
2104 network_thread_->Invoke<void>(RTC_FROM_HERE, [this, &transceivers,
2105 &voice_stats, &video_stats] {
Henrik Boströme88c95e2020-07-08 09:18:502106 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:502107
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572108 for (const auto& transceiver_proxy : transceivers) {
2109 RtpTransceiver* transceiver = transceiver_proxy->internal();
Henrik Boströme88c95e2020-07-08 09:18:502110 cricket::MediaType media_type = transceiver->media_type();
Steve Anton57858b32018-02-15 23:19:502111
Henrik Boströme88c95e2020-07-08 09:18:502112 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
2113 // stats have been fetched on the worker thread.
Henrik Boström180faeb2020-11-13 08:05:212114 transceiver_stats_infos_.emplace_back();
2115 RtpTransceiverStatsInfo& stats = transceiver_stats_infos_.back();
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572116 stats.transceiver = transceiver;
Henrik Boströme88c95e2020-07-08 09:18:502117 stats.media_type = media_type;
Steve Anton57858b32018-02-15 23:19:502118
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572119 cricket::ChannelInterface* channel = transceiver->channel();
Henrik Boströme88c95e2020-07-08 09:18:502120 if (!channel) {
2121 // The remaining fields require a BaseChannel.
2122 continue;
2123 }
Steve Anton57858b32018-02-15 23:19:502124
Henrik Boströme88c95e2020-07-08 09:18:502125 stats.mid = channel->content_name();
2126 stats.transport_name = channel->transport_name();
2127
2128 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2129 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
2130 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
2131 voice_stats.end());
2132 voice_stats[voice_channel->media_channel()] =
2133 std::make_unique<cricket::VoiceMediaInfo>();
2134 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2135 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
2136 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
2137 video_stats.end());
2138 video_stats[video_channel->media_channel()] =
2139 std::make_unique<cricket::VideoMediaInfo>();
2140 } else {
2141 RTC_NOTREACHED();
2142 }
Steve Anton57858b32018-02-15 23:19:502143 }
Tomas Gunnarssonbfd9ba82021-04-18 09:55:572144 });
Steve Anton57858b32018-02-15 23:19:502145
Henrik Boström180faeb2020-11-13 08:05:212146 // We jump to the worker thread and call GetStats() on each media channel as
2147 // well as GetCallStats(). At the same time we construct the
2148 // TrackMediaInfoMaps, which also needs info from the worker thread. This
2149 // minimizes the number of thread jumps.
Steve Anton57858b32018-02-15 23:19:502150 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Henrik Boström6fafbe32020-07-08 08:37:002151 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2152
Steve Anton57858b32018-02-15 23:19:502153 for (const auto& entry : voice_stats) {
Niels Möller6b4d9622020-09-14 08:47:502154 if (!entry.first->GetStats(entry.second.get(),
2155 /*get_and_clear_legacy_stats=*/false)) {
Steve Anton57858b32018-02-15 23:19:502156 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
2157 }
2158 }
2159 for (const auto& entry : video_stats) {
2160 if (!entry.first->GetStats(entry.second.get())) {
2161 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
2162 }
2163 }
Steve Anton57858b32018-02-15 23:19:502164
Henrik Boström6fafbe32020-07-08 08:37:002165 // Create the TrackMediaInfoMap for each transceiver stats object.
Henrik Boström180faeb2020-11-13 08:05:212166 for (auto& stats : transceiver_stats_infos_) {
Henrik Boström6fafbe32020-07-08 08:37:002167 auto transceiver = stats.transceiver;
2168 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
2169 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
2170 if (transceiver->channel()) {
2171 cricket::MediaType media_type = transceiver->media_type();
2172 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2173 auto* voice_channel =
2174 static_cast<cricket::VoiceChannel*>(transceiver->channel());
2175 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
2176 voice_media_info =
2177 std::move(voice_stats[voice_channel->media_channel()]);
2178 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2179 auto* video_channel =
2180 static_cast<cricket::VideoChannel*>(transceiver->channel());
2181 RTC_DCHECK(video_stats[video_channel->media_channel()]);
2182 video_media_info =
2183 std::move(video_stats[video_channel->media_channel()]);
2184 }
Steve Anton57858b32018-02-15 23:19:502185 }
Henrik Boström6fafbe32020-07-08 08:37:002186 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
2187 for (const auto& sender : transceiver->senders()) {
2188 senders.push_back(sender->internal());
2189 }
2190 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
2191 for (const auto& receiver : transceiver->receivers()) {
2192 receivers.push_back(receiver->internal());
2193 }
2194 stats.track_media_info_map = std::make_unique<TrackMediaInfoMap>(
2195 std::move(voice_media_info), std::move(video_media_info), senders,
2196 receivers);
Steve Anton57858b32018-02-15 23:19:502197 }
Steve Anton57858b32018-02-15 23:19:502198
Henrik Boström180faeb2020-11-13 08:05:212199 call_stats_ = pc_->GetCallStats();
2200 });
hbos0adb8282016-11-23 10:32:062201}
2202
Taylor Brandstetter3a034e12020-07-09 22:32:342203void RTCStatsCollector::OnSctpDataChannelCreated(SctpDataChannel* channel) {
2204 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
2205 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
2206}
2207
2208void RTCStatsCollector::OnDataChannelOpened(DataChannelInterface* channel) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052209 RTC_DCHECK_RUN_ON(signaling_thread_);
Jonas Olssona4d87372019-07-05 17:08:332210 bool result = internal_record_.opened_data_channels
2211 .insert(reinterpret_cast<uintptr_t>(channel))
2212 .second;
hbos82ebe022016-11-14 09:41:092213 ++internal_record_.data_channels_opened;
2214 RTC_DCHECK(result);
2215}
2216
Taylor Brandstetter3a034e12020-07-09 22:32:342217void RTCStatsCollector::OnDataChannelClosed(DataChannelInterface* channel) {
Tomas Gunnarsson67b1fa22021-04-08 13:18:052218 RTC_DCHECK_RUN_ON(signaling_thread_);
hbos82ebe022016-11-14 09:41:092219 // Only channels that have been fully opened (and have increased the
2220 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 10:14:142221 if (internal_record_.opened_data_channels.erase(
2222 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 09:41:092223 ++internal_record_.data_channels_closed;
2224 }
2225}
2226
hboscc555c52016-10-18 19:48:312227const char* CandidateTypeToRTCIceCandidateTypeForTesting(
2228 const std::string& type) {
2229 return CandidateTypeToRTCIceCandidateType(type);
2230}
2231
2232const char* DataStateToRTCDataChannelStateForTesting(
2233 DataChannelInterface::DataState state) {
2234 return DataStateToRTCDataChannelState(state);
2235}
2236
hbosd565b732016-08-30 21:04:352237} // namespace webrtc