blob: eba54f6e2650645c32c7769c6702ce8daaf858b7 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
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 09:11:00 -080011#include "pc/rtc_stats_collector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <string>
hbosd565b732016-08-30 14:04:35 -070015#include <utility>
16#include <vector>
17
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "api/media_stream_interface.h"
21#include "api/peer_connection_interface.h"
Henrik Boström2e069262019-04-09 13:59:31 +020022#include "api/video/video_content_type.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "media/base/media_channel.h"
24#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "pc/peer_connection.h"
27#include "pc/rtc_stats_traversal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
Jonas Olsson43568dd2018-06-11 16:25:54 +020029#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070032
33namespace webrtc {
34
hboscc555c52016-10-18 12:48:31 -070035namespace {
36
Henrik Boström646fda02019-05-22 15:49:42 +020037// TODO(https://crbug.com/webrtc/10656): Consider making IDs less predictable.
hbos2fa7c672016-10-24 04:00:05 -070038std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
39 return "RTCCertificate_" + fingerprint;
40}
41
Steve Anton57858b32018-02-15 15:19:50 -080042std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
43 bool inbound,
44 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020045 char buf[1024];
46 rtc::SimpleStringBuilder sb(buf);
47 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
48 << payload_type;
49 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080050}
51
hbos2fa7c672016-10-24 04:00:05 -070052std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
53 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020054 char buf[4096];
55 rtc::SimpleStringBuilder sb(buf);
56 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
57 << info.remote_candidate.id();
58 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070059}
60
Harald Alvestranda3dab842018-01-14 09:18:58 +010061const char kSender[] = "sender";
62const char kReceiver[] = "receiver";
63
64std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
65 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010066 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020067 char buf[1024];
68 rtc::SimpleStringBuilder sb(buf);
69 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
70 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080071}
72
hbos2fa7c672016-10-24 04:00:05 -070073std::string RTCTransportStatsIDFromTransportChannel(
Jonas Olssona4d87372019-07-05 19:08:33 +020074 const std::string& transport_name,
75 int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020076 char buf[1024];
77 rtc::SimpleStringBuilder sb(buf);
78 sb << "RTCTransport_" << transport_name << "_" << channel_component;
79 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070080}
81
hboseeafe942016-11-01 03:00:17 -070082std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020083 char buf[1024];
84 rtc::SimpleStringBuilder sb(buf);
85 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
86 return sb.str();
hboseeafe942016-11-01 03:00:17 -070087}
88
hbos6ded1902016-11-01 01:50:46 -070089std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020090 char buf[1024];
91 rtc::SimpleStringBuilder sb(buf);
92 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
93 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070094}
95
Henrik Boström8605fbf2019-06-24 16:44:51 +020096std::string RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(
Henrik Boström883eefc2019-05-27 13:40:25 +020097 cricket::MediaType media_type,
Henrik Boström883eefc2019-05-27 13:40:25 +020098 uint32_t source_ssrc) {
99 char buf[1024];
100 rtc::SimpleStringBuilder sb(buf);
101 sb << "RTCRemoteInboundRtp"
102 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
Henrik Boström8605fbf2019-06-24 16:44:51 +0200103 << "Stream_" << source_ssrc;
Henrik Boström883eefc2019-05-27 13:40:25 +0200104 return sb.str();
105}
106
Henrik Boström646fda02019-05-22 15:49:42 +0200107std::string RTCMediaSourceStatsIDFromKindAndAttachment(
108 cricket::MediaType media_type,
109 int attachment_id) {
110 char buf[1024];
111 rtc::SimpleStringBuilder sb(buf);
112 sb << "RTC" << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
113 << "Source_" << attachment_id;
114 return sb.str();
115}
116
hbosab9f6e42016-10-07 02:18:47 -0700117const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
118 if (type == cricket::LOCAL_PORT_TYPE)
119 return RTCIceCandidateType::kHost;
120 if (type == cricket::STUN_PORT_TYPE)
121 return RTCIceCandidateType::kSrflx;
122 if (type == cricket::PRFLX_PORT_TYPE)
123 return RTCIceCandidateType::kPrflx;
124 if (type == cricket::RELAY_PORT_TYPE)
125 return RTCIceCandidateType::kRelay;
126 RTC_NOTREACHED();
127 return nullptr;
128}
129
hboscc555c52016-10-18 12:48:31 -0700130const char* DataStateToRTCDataChannelState(
131 DataChannelInterface::DataState state) {
132 switch (state) {
133 case DataChannelInterface::kConnecting:
134 return RTCDataChannelState::kConnecting;
135 case DataChannelInterface::kOpen:
136 return RTCDataChannelState::kOpen;
137 case DataChannelInterface::kClosing:
138 return RTCDataChannelState::kClosing;
139 case DataChannelInterface::kClosed:
140 return RTCDataChannelState::kClosed;
141 default:
142 RTC_NOTREACHED();
143 return nullptr;
144 }
145}
146
hbos06495bc2017-01-02 08:08:18 -0800147const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
148 cricket::IceCandidatePairState state) {
149 switch (state) {
150 case cricket::IceCandidatePairState::WAITING:
151 return RTCStatsIceCandidatePairState::kWaiting;
152 case cricket::IceCandidatePairState::IN_PROGRESS:
153 return RTCStatsIceCandidatePairState::kInProgress;
154 case cricket::IceCandidatePairState::SUCCEEDED:
155 return RTCStatsIceCandidatePairState::kSucceeded;
156 case cricket::IceCandidatePairState::FAILED:
157 return RTCStatsIceCandidatePairState::kFailed;
158 default:
159 RTC_NOTREACHED();
160 return nullptr;
161 }
162}
163
hbos7064d592017-01-16 07:38:02 -0800164const char* DtlsTransportStateToRTCDtlsTransportState(
165 cricket::DtlsTransportState state) {
166 switch (state) {
167 case cricket::DTLS_TRANSPORT_NEW:
168 return RTCDtlsTransportState::kNew;
169 case cricket::DTLS_TRANSPORT_CONNECTING:
170 return RTCDtlsTransportState::kConnecting;
171 case cricket::DTLS_TRANSPORT_CONNECTED:
172 return RTCDtlsTransportState::kConnected;
173 case cricket::DTLS_TRANSPORT_CLOSED:
174 return RTCDtlsTransportState::kClosed;
175 case cricket::DTLS_TRANSPORT_FAILED:
176 return RTCDtlsTransportState::kFailed;
177 default:
178 RTC_NOTREACHED();
179 return nullptr;
180 }
181}
182
Gary Liu37e489c2017-11-21 10:49:36 -0800183const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
184 switch (type) {
185 case rtc::ADAPTER_TYPE_CELLULAR:
186 return RTCNetworkType::kCellular;
187 case rtc::ADAPTER_TYPE_ETHERNET:
188 return RTCNetworkType::kEthernet;
189 case rtc::ADAPTER_TYPE_WIFI:
190 return RTCNetworkType::kWifi;
191 case rtc::ADAPTER_TYPE_VPN:
192 return RTCNetworkType::kVpn;
193 case rtc::ADAPTER_TYPE_UNKNOWN:
194 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700195 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800196 return RTCNetworkType::kUnknown;
197 }
198 RTC_NOTREACHED();
199 return nullptr;
200}
201
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200202const char* QualityLimitationReasonToRTCQualityLimitationReason(
203 QualityLimitationReason reason) {
204 switch (reason) {
205 case QualityLimitationReason::kNone:
206 return RTCQualityLimitationReason::kNone;
207 case QualityLimitationReason::kCpu:
208 return RTCQualityLimitationReason::kCpu;
209 case QualityLimitationReason::kBandwidth:
210 return RTCQualityLimitationReason::kBandwidth;
211 case QualityLimitationReason::kOther:
212 return RTCQualityLimitationReason::kOther;
213 }
214}
215
hbos9e302742017-01-20 02:47:10 -0800216double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
217 RTC_DCHECK_GE(audio_level, 0);
218 RTC_DCHECK_LE(audio_level, 32767);
219 return audio_level / 32767.0;
220}
221
hbos0adb8282016-11-23 02:32:06 -0800222std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800223 uint64_t timestamp_us,
224 const std::string& mid,
225 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800226 const RtpCodecParameters& codec_params) {
227 RTC_DCHECK_GE(codec_params.payload_type, 0);
228 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800229 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800230 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
231 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800232 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800233 timestamp_us));
234 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800235 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800236 if (codec_params.clock_rate) {
237 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
238 }
hbos0adb8282016-11-23 02:32:06 -0800239 return codec_stats;
240}
241
hbos09bc1282016-11-08 06:29:22 -0800242void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
243 const MediaStreamTrackInterface& track,
244 RTCMediaStreamTrackStats* track_stats) {
245 track_stats->track_identifier = track.id();
246 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
247}
248
hbos820f5782016-11-22 03:16:50 -0800249// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700250void SetInboundRTPStreamStatsFromMediaReceiverInfo(
251 const cricket::MediaReceiverInfo& media_receiver_info,
252 RTCInboundRTPStreamStats* inbound_stats) {
253 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800254 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100255 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700256 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700257 inbound_stats->packets_received =
258 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
259 inbound_stats->bytes_received =
260 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800261 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100262 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700263}
264
265void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800266 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700267 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800268 RTCInboundRTPStreamStats* inbound_audio) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200269 SetInboundRTPStreamStatsFromMediaReceiverInfo(voice_receiver_info,
270 inbound_audio);
hbos820f5782016-11-22 03:16:50 -0800271 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200272 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800273 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800274 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
275 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800276 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200277 inbound_audio->jitter = static_cast<double>(voice_receiver_info.jitter_ms) /
278 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800279 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
280 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 17:32:00 +0200281 if (voice_receiver_info.last_packet_received_timestamp_ms) {
282 inbound_audio->last_packet_received_timestamp =
283 static_cast<double>(
284 *voice_receiver_info.last_packet_received_timestamp_ms) /
285 rtc::kNumMillisecsPerSec;
286 }
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200287 inbound_audio->fec_packets_received =
288 voice_receiver_info.fec_packets_received;
289 inbound_audio->fec_packets_discarded =
290 voice_receiver_info.fec_packets_discarded;
hboseeafe942016-11-01 03:00:17 -0700291}
292
293void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800294 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700295 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800296 RTCInboundRTPStreamStats* inbound_video) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200297 SetInboundRTPStreamStatsFromMediaReceiverInfo(video_receiver_info,
298 inbound_video);
hbos820f5782016-11-22 03:16:50 -0800299 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200300 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800301 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800302 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
303 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800304 }
hbos820f5782016-11-22 03:16:50 -0800305 inbound_video->fir_count =
306 static_cast<uint32_t>(video_receiver_info.firs_sent);
307 inbound_video->pli_count =
308 static_cast<uint32_t>(video_receiver_info.plis_sent);
309 inbound_video->nack_count =
310 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800311 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
Rasmus Brandt2efae772019-06-27 14:29:34 +0200312 inbound_video->key_frames_decoded = video_receiver_info.key_frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800313 if (video_receiver_info.qp_sum)
314 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Johannes Kronbfd343b2019-07-01 10:07:50 +0200315 inbound_video->total_decode_time =
316 static_cast<double>(video_receiver_info.total_decode_time_ms) /
317 rtc::kNumMillisecsPerSec;
Henrik Boström01738c62019-04-15 17:32:00 +0200318 if (video_receiver_info.last_packet_received_timestamp_ms) {
319 inbound_video->last_packet_received_timestamp =
320 static_cast<double>(
321 *video_receiver_info.last_packet_received_timestamp_ms) /
322 rtc::kNumMillisecsPerSec;
323 }
Henrik Boström2e069262019-04-09 13:59:31 +0200324 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
325 // optional, support the "unspecified" value.
326 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
327 inbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 13:09:51 +0200328 if (!video_receiver_info.decoder_implementation_name.empty()) {
329 inbound_video->decoder_implementation =
330 video_receiver_info.decoder_implementation_name;
331 }
hboseeafe942016-11-01 03:00:17 -0700332}
333
hbos820f5782016-11-22 03:16:50 -0800334// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700335void SetOutboundRTPStreamStatsFromMediaSenderInfo(
336 const cricket::MediaSenderInfo& media_sender_info,
337 RTCOutboundRTPStreamStats* outbound_stats) {
338 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800339 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100340 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700341 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700342 outbound_stats->packets_sent =
343 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200344 outbound_stats->retransmitted_packets_sent =
345 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 01:50:46 -0700346 outbound_stats->bytes_sent =
347 static_cast<uint64_t>(media_sender_info.bytes_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200348 outbound_stats->retransmitted_bytes_sent =
349 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 01:50:46 -0700350}
351
352void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800353 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700354 const cricket::VoiceSenderInfo& voice_sender_info,
355 RTCOutboundRTPStreamStats* outbound_audio) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200356 SetOutboundRTPStreamStatsFromMediaSenderInfo(voice_sender_info,
357 outbound_audio);
hbos6ded1902016-11-01 01:50:46 -0700358 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200359 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800360 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800361 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
362 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800363 }
hbos6ded1902016-11-01 01:50:46 -0700364 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
365 // purposefully left undefined for audio.
366}
367
368void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800369 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700370 const cricket::VideoSenderInfo& video_sender_info,
371 RTCOutboundRTPStreamStats* outbound_video) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200372 SetOutboundRTPStreamStatsFromMediaSenderInfo(video_sender_info,
373 outbound_video);
hbos6ded1902016-11-01 01:50:46 -0700374 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200375 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800376 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800377 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
378 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800379 }
hbos6ded1902016-11-01 01:50:46 -0700380 outbound_video->fir_count =
381 static_cast<uint32_t>(video_sender_info.firs_rcvd);
382 outbound_video->pli_count =
383 static_cast<uint32_t>(video_sender_info.plis_rcvd);
384 outbound_video->nack_count =
385 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800386 if (video_sender_info.qp_sum)
387 outbound_video->qp_sum = *video_sender_info.qp_sum;
388 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Rasmus Brandt2efae772019-06-27 14:29:34 +0200389 outbound_video->key_frames_encoded = video_sender_info.key_frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200390 outbound_video->total_encode_time =
391 static_cast<double>(video_sender_info.total_encode_time_ms) /
392 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200393 outbound_video->total_encoded_bytes_target =
394 video_sender_info.total_encoded_bytes_target;
Henrik Boström9fe18342019-05-16 18:38:20 +0200395 outbound_video->total_packet_send_delay =
396 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
397 rtc::kNumMillisecsPerSec;
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200398 outbound_video->quality_limitation_reason =
399 QualityLimitationReasonToRTCQualityLimitationReason(
400 video_sender_info.quality_limitation_reason);
Evan Shrubsolecc62b162019-09-09 11:26:45 +0200401 outbound_video->quality_limitation_resolution_changes =
402 video_sender_info.quality_limitation_resolution_changes;
Henrik Boström2e069262019-04-09 13:59:31 +0200403 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
404 // optional, support the "unspecified" value.
405 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
406 outbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 13:09:51 +0200407 if (!video_sender_info.encoder_implementation_name.empty()) {
408 outbound_video->encoder_implementation =
409 video_sender_info.encoder_implementation_name;
410 }
hbos6ded1902016-11-01 01:50:46 -0700411}
412
Henrik Boström883eefc2019-05-27 13:40:25 +0200413std::unique_ptr<RTCRemoteInboundRtpStreamStats>
414ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
415 const ReportBlockData& report_block_data,
416 cricket::MediaType media_type,
417 const RTCStatsReport& report) {
418 const auto& report_block = report_block_data.report_block();
419 // RTCStats' timestamp generally refers to when the metric was sampled, but
420 // for "remote-[outbound/inbound]-rtp" it refers to the local time when the
421 // Report Block was received.
422 auto remote_inbound = absl::make_unique<RTCRemoteInboundRtpStreamStats>(
Henrik Boström8605fbf2019-06-24 16:44:51 +0200423 RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(media_type,
424 report_block.source_ssrc),
Henrik Boström883eefc2019-05-27 13:40:25 +0200425 /*timestamp=*/report_block_data.report_block_timestamp_utc_us());
Henrik Boström8605fbf2019-06-24 16:44:51 +0200426 remote_inbound->ssrc = report_block.source_ssrc;
Henrik Boström883eefc2019-05-27 13:40:25 +0200427 remote_inbound->kind =
428 media_type == cricket::MEDIA_TYPE_AUDIO ? "audio" : "video";
429 remote_inbound->packets_lost = report_block.packets_lost;
430 remote_inbound->round_trip_time =
431 static_cast<double>(report_block_data.last_rtt_ms()) /
432 rtc::kNumMillisecsPerSec;
433
434 std::string local_id = RTCOutboundRTPStreamStatsIDFromSSRC(
435 media_type == cricket::MEDIA_TYPE_AUDIO, report_block.source_ssrc);
436 const auto* local_id_stat = report.Get(local_id);
437 if (local_id_stat) {
438 remote_inbound->local_id = local_id;
439 const auto& outbound_rtp =
440 local_id_stat->cast_to<RTCOutboundRTPStreamStats>();
441 // The RTP/RTCP transport is obtained from the
442 // RTCOutboundRtpStreamStats's transport.
443 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
444 ? report.Get(*outbound_rtp.transport_id)
445 : nullptr;
446 if (transport_from_id) {
447 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
448 // If RTP and RTCP are not multiplexed, there is a separate RTCP
449 // transport paired with the RTP transport, otherwise the same
450 // transport is used for RTCP and RTP.
451 remote_inbound->transport_id =
452 transport.rtcp_transport_stats_id.is_defined()
453 ? *transport.rtcp_transport_stats_id
454 : *outbound_rtp.transport_id;
455 }
456 // We're assuming the same codec is used on both ends. However if the
457 // codec is switched out on the fly we may have received a Report Block
458 // based on the previous codec and there is no way to tell which point in
459 // time the codec changed for the remote end.
460 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
461 ? report.Get(*outbound_rtp.codec_id)
462 : nullptr;
463 if (codec_from_id) {
464 remote_inbound->codec_id = *outbound_rtp.codec_id;
465 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
466 if (codec.clock_rate.is_defined()) {
467 // The Report Block jitter is expressed in RTP timestamp units
468 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
469 // to seconds we divide by the codec's clock rate.
470 remote_inbound->jitter =
471 static_cast<double>(report_block.jitter) / *codec.clock_rate;
472 }
473 }
474 }
475 return remote_inbound;
476}
477
hbos02ba2112016-10-28 05:14:53 -0700478void ProduceCertificateStatsFromSSLCertificateStats(
Jonas Olssona4d87372019-07-05 19:08:33 +0200479 int64_t timestamp_us,
480 const rtc::SSLCertificateStats& certificate_stats,
hbos02ba2112016-10-28 05:14:53 -0700481 RTCStatsReport* report) {
482 RTCCertificateStats* prev_certificate_stats = nullptr;
483 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
484 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800485 std::string certificate_stats_id =
486 RTCCertificateIDFromFingerprint(s->fingerprint);
487 // It is possible for the same certificate to show up multiple times, e.g.
488 // if local and remote side use the same certificate in a loopback call.
489 // If the report already contains stats for this certificate, skip it.
490 if (report->Get(certificate_stats_id)) {
491 RTC_DCHECK_EQ(s, &certificate_stats);
492 break;
493 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200494 RTCCertificateStats* certificate_stats =
495 new RTCCertificateStats(certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700496 certificate_stats->fingerprint = s->fingerprint;
497 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
498 certificate_stats->base64_certificate = s->base64_certificate;
499 if (prev_certificate_stats)
500 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
501 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
502 prev_certificate_stats = certificate_stats;
503 }
504}
505
Jonas Olssona4d87372019-07-05 19:08:33 +0200506const std::string& ProduceIceCandidateStats(int64_t timestamp_us,
507 const cricket::Candidate& candidate,
508 bool is_local,
509 const std::string& transport_id,
510 RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700511 const std::string& id = "RTCIceCandidate_" + candidate.id();
512 const RTCStats* stats = report->Get(id);
513 if (!stats) {
514 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
515 if (is_local)
516 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
517 else
518 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800519 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800520 if (is_local) {
521 candidate_stats->network_type =
522 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200523 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
524 std::string relay_protocol = candidate.relay_protocol();
525 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
526 relay_protocol.compare("tcp") == 0 ||
527 relay_protocol.compare("tls") == 0);
528 candidate_stats->relay_protocol = relay_protocol;
529 }
Gary Liu37e489c2017-11-21 10:49:36 -0800530 } else {
531 // We don't expect to know the adapter type of remote candidates.
532 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
533 }
hbos02ba2112016-10-28 05:14:53 -0700534 candidate_stats->ip = candidate.address().ipaddr().ToString();
535 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
536 candidate_stats->protocol = candidate.protocol();
Jonas Olssona4d87372019-07-05 19:08:33 +0200537 candidate_stats->candidate_type =
538 CandidateTypeToRTCIceCandidateType(candidate.type());
hbos02ba2112016-10-28 05:14:53 -0700539 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
540
541 stats = candidate_stats.get();
542 report->AddStats(std::move(candidate_stats));
543 }
544 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
545 : RTCRemoteIceCandidateStats::kType);
546 return stats->id();
547}
548
hbos9e302742017-01-20 02:47:10 -0800549std::unique_ptr<RTCMediaStreamTrackStats>
550ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
551 int64_t timestamp_us,
552 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100553 const cricket::VoiceSenderInfo& voice_sender_info,
554 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800555 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
556 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100557 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
558 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100559 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800560 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
561 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200562 audio_track_stats->media_source_id =
563 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
564 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800565 audio_track_stats->remote_source = false;
566 audio_track_stats->detached = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100567 if (voice_sender_info.apm_statistics.echo_return_loss) {
568 audio_track_stats->echo_return_loss =
569 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800570 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100571 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
572 audio_track_stats->echo_return_loss_enhancement =
573 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800574 }
575 return audio_track_stats;
576}
577
578std::unique_ptr<RTCMediaStreamTrackStats>
579ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
580 int64_t timestamp_us,
581 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100582 const cricket::VoiceReceiverInfo& voice_receiver_info,
583 int attachment_id) {
584 // Since receiver tracks can't be reattached, we use the SSRC as
585 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800586 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
587 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100588 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
589 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100590 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800591 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
592 audio_track, audio_track_stats.get());
593 audio_track_stats->remote_source = true;
594 audio_track_stats->detached = false;
595 if (voice_receiver_info.audio_level >= 0) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200596 audio_track_stats->audio_level =
597 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
hbos9e302742017-01-20 02:47:10 -0800598 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200599 audio_track_stats->jitter_buffer_delay =
600 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100601 audio_track_stats->jitter_buffer_emitted_count =
602 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200603 audio_track_stats->inserted_samples_for_deceleration =
604 voice_receiver_info.inserted_samples_for_deceleration;
605 audio_track_stats->removed_samples_for_acceleration =
606 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 12:17:49 -0700607 audio_track_stats->total_audio_energy =
608 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700609 audio_track_stats->total_samples_received =
610 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700611 audio_track_stats->total_samples_duration =
612 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700613 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200614 audio_track_stats->silent_concealed_samples =
615 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200616 audio_track_stats->concealment_events =
617 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100618 audio_track_stats->jitter_buffer_flushes =
619 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100620 audio_track_stats->delayed_packet_outage_samples =
621 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100622 audio_track_stats->relative_packet_arrival_delay =
623 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200624 audio_track_stats->interruption_count =
625 voice_receiver_info.interruption_count >= 0
626 ? voice_receiver_info.interruption_count
627 : 0;
628 audio_track_stats->total_interruption_duration =
629 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
630 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800631 return audio_track_stats;
632}
633
634std::unique_ptr<RTCMediaStreamTrackStats>
635ProduceMediaStreamTrackStatsFromVideoSenderInfo(
636 int64_t timestamp_us,
637 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100638 const cricket::VideoSenderInfo& video_sender_info,
639 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800640 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
641 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100642 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 09:18:58 +0100643 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100644 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800645 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
646 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200647 video_track_stats->media_source_id =
648 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
649 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800650 video_track_stats->remote_source = false;
651 video_track_stats->detached = false;
Jonas Olssona4d87372019-07-05 19:08:33 +0200652 video_track_stats->frame_width =
653 static_cast<uint32_t>(video_sender_info.send_frame_width);
654 video_track_stats->frame_height =
655 static_cast<uint32_t>(video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800656 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100657 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800658 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100659 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800660 return video_track_stats;
661}
662
663std::unique_ptr<RTCMediaStreamTrackStats>
664ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
665 int64_t timestamp_us,
666 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100667 const cricket::VideoReceiverInfo& video_receiver_info,
668 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800669 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
670 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100671 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
672
673 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100674 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800675 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
676 video_track, video_track_stats.get());
677 video_track_stats->remote_source = true;
678 video_track_stats->detached = false;
679 if (video_receiver_info.frame_width > 0 &&
680 video_receiver_info.frame_height > 0) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200681 video_track_stats->frame_width =
682 static_cast<uint32_t>(video_receiver_info.frame_width);
683 video_track_stats->frame_height =
684 static_cast<uint32_t>(video_receiver_info.frame_height);
hbos9e302742017-01-20 02:47:10 -0800685 }
Guido Urdaneta67378412019-05-28 17:38:08 +0200686 video_track_stats->jitter_buffer_delay =
687 video_receiver_info.jitter_buffer_delay_seconds;
688 video_track_stats->jitter_buffer_emitted_count =
689 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 03:56:50 -0800690 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800691 // TODO(hbos): When we support receiving simulcast, this should be the total
692 // number of frames correctly decoded, independent of which SSRC it was
693 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100694 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800695 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
Johannes Kron0c141c52019-08-26 15:04:43 +0200696 video_track_stats->frames_dropped = video_receiver_info.frames_dropped;
Sergey Silkin02371062019-01-31 16:45:42 +0100697 video_track_stats->freeze_count = video_receiver_info.freeze_count;
698 video_track_stats->pause_count = video_receiver_info.pause_count;
699 video_track_stats->total_freezes_duration =
700 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
701 rtc::kNumMillisecsPerSec;
702 video_track_stats->total_pauses_duration =
703 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
704 rtc::kNumMillisecsPerSec;
705 video_track_stats->total_frames_duration =
706 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
707 rtc::kNumMillisecsPerSec;
708 video_track_stats->sum_squared_frame_durations =
709 video_receiver_info.sum_squared_frame_durations;
710
hbos9e302742017-01-20 02:47:10 -0800711 return video_track_stats;
712}
713
Harald Alvestrand89061872018-01-02 14:08:34 +0100714void ProduceSenderMediaTrackStats(
715 int64_t timestamp_us,
716 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800717 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100718 RTCStatsReport* report) {
719 // This function iterates over the senders to generate outgoing track stats.
720
721 // TODO(hbos): Return stats of detached tracks. We have to perform stats
722 // gathering at the time of detachment to get accurate stats and timestamps.
723 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100724 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100725 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
726 AudioTrackInterface* track =
727 static_cast<AudioTrackInterface*>(sender->track().get());
728 if (!track)
729 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100730 cricket::VoiceSenderInfo null_sender_info;
731 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
732 // TODO(hta): Checking on ssrc is not proper. There should be a way
733 // to see from a sender whether it's connected or not.
734 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800735 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100736 // When pc.close is called, sender info is discarded, so
737 // we generate zeroes instead. Bug: It should be retained.
738 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800739 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100740 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100741 if (sender_info) {
742 voice_sender_info = sender_info;
743 } else {
744 RTC_LOG(LS_INFO)
745 << "RTCStatsCollector: No voice sender info for sender with ssrc "
746 << sender->ssrc();
747 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100748 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100749 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100750 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
751 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100752 report->AddStats(std::move(audio_track_stats));
753 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
754 VideoTrackInterface* track =
755 static_cast<VideoTrackInterface*>(sender->track().get());
756 if (!track)
757 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100758 cricket::VideoSenderInfo null_sender_info;
759 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
760 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100761 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
762 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800763 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100764 // When pc.close is called, sender info is discarded, so
765 // we generate zeroes instead. Bug: It should be retained.
766 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800767 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100768 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100769 if (sender_info) {
770 video_sender_info = sender_info;
771 } else {
772 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
773 << sender->ssrc();
774 }
775 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100776 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100777 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
778 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100779 report->AddStats(std::move(video_track_stats));
780 } else {
781 RTC_NOTREACHED();
782 }
783 }
784}
785
786void ProduceReceiverMediaTrackStats(
787 int64_t timestamp_us,
788 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800789 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100790 RTCStatsReport* report) {
791 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100792 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100793 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
794 AudioTrackInterface* track =
795 static_cast<AudioTrackInterface*>(receiver->track().get());
796 const cricket::VoiceReceiverInfo* voice_receiver_info =
797 track_media_info_map.GetVoiceReceiverInfo(*track);
798 if (!voice_receiver_info) {
799 continue;
800 }
801 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
802 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100803 timestamp_us, *track, *voice_receiver_info,
804 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100805 report->AddStats(std::move(audio_track_stats));
806 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
807 VideoTrackInterface* track =
808 static_cast<VideoTrackInterface*>(receiver->track().get());
809 const cricket::VideoReceiverInfo* video_receiver_info =
810 track_media_info_map.GetVideoReceiverInfo(*track);
811 if (!video_receiver_info) {
812 continue;
813 }
814 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
815 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100816 timestamp_us, *track, *video_receiver_info,
817 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100818 report->AddStats(std::move(video_track_stats));
819 } else {
820 RTC_NOTREACHED();
821 }
822 }
823}
824
Henrik Boström5b3541f2018-03-19 13:52:56 +0100825rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
826 bool filter_by_sender_selector,
827 rtc::scoped_refptr<const RTCStatsReport> report,
828 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
829 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
830 std::vector<std::string> rtpstream_ids;
831 if (filter_by_sender_selector) {
832 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
833 if (sender_selector) {
834 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
835 // reference the sender stats.
836 // Because we do not implement sender stats, we look at outbound-rtp(s)
837 // that reference the track attachment stats for the sender instead.
838 std::string track_id =
839 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
840 kSender, sender_selector->AttachmentId());
841 for (const auto& stats : *report) {
842 if (stats.type() != RTCOutboundRTPStreamStats::kType)
843 continue;
844 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
845 if (outbound_rtp.track_id.is_defined() &&
846 *outbound_rtp.track_id == track_id) {
847 rtpstream_ids.push_back(outbound_rtp.id());
848 }
849 }
850 }
851 } else {
852 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
853 if (receiver_selector) {
854 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
855 // reference the receiver stats.
856 // Because we do not implement receiver stats, we look at inbound-rtp(s)
857 // that reference the track attachment stats for the receiver instead.
858 std::string track_id =
859 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
860 kReceiver, receiver_selector->AttachmentId());
861 for (const auto& stats : *report) {
862 if (stats.type() != RTCInboundRTPStreamStats::kType)
863 continue;
864 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
865 if (inbound_rtp.track_id.is_defined() &&
866 *inbound_rtp.track_id == track_id) {
867 rtpstream_ids.push_back(inbound_rtp.id());
868 }
869 }
870 }
871 }
872 if (rtpstream_ids.empty())
873 return RTCStatsReport::Create(report->timestamp_us());
874 return TakeReferencedStats(report->Copy(), rtpstream_ids);
875}
876
hboscc555c52016-10-18 12:48:31 -0700877} // namespace
878
Henrik Boström5b3541f2018-03-19 13:52:56 +0100879RTCStatsCollector::RequestInfo::RequestInfo(
880 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
881 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
882
883RTCStatsCollector::RequestInfo::RequestInfo(
884 rtc::scoped_refptr<RtpSenderInternal> selector,
885 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
886 : RequestInfo(FilterMode::kSenderSelector,
887 std::move(callback),
888 std::move(selector),
889 nullptr) {}
890
891RTCStatsCollector::RequestInfo::RequestInfo(
892 rtc::scoped_refptr<RtpReceiverInternal> selector,
893 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
894 : RequestInfo(FilterMode::kReceiverSelector,
895 std::move(callback),
896 nullptr,
897 std::move(selector)) {}
898
899RTCStatsCollector::RequestInfo::RequestInfo(
900 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
901 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
902 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
903 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
904 : filter_mode_(filter_mode),
905 callback_(std::move(callback)),
906 sender_selector_(std::move(sender_selector)),
907 receiver_selector_(std::move(receiver_selector)) {
908 RTC_DCHECK(callback_);
909 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
910}
911
hbosc82f2e12016-09-05 01:36:50 -0700912rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800913 PeerConnectionInternal* pc,
914 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700915 return rtc::scoped_refptr<RTCStatsCollector>(
916 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
917}
918
Steve Anton2d8609c2018-01-23 16:38:46 -0800919RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700920 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700921 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700922 signaling_thread_(pc->signaling_thread()),
923 worker_thread_(pc->worker_thread()),
924 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700925 num_pending_partial_reports_(0),
926 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100927 network_report_event_(true /* manual_reset */,
928 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700929 cache_timestamp_us_(0),
930 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700931 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700932 RTC_DCHECK(signaling_thread_);
933 RTC_DCHECK(worker_thread_);
934 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700935 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800936 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800937 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700938}
939
hbosb78306a2016-12-19 05:06:57 -0800940RTCStatsCollector::~RTCStatsCollector() {
941 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
942}
943
hbosc82f2e12016-09-05 01:36:50 -0700944void RTCStatsCollector::GetStatsReport(
945 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100946 GetStatsReportInternal(RequestInfo(std::move(callback)));
947}
948
949void RTCStatsCollector::GetStatsReport(
950 rtc::scoped_refptr<RtpSenderInternal> selector,
951 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
952 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
953}
954
955void RTCStatsCollector::GetStatsReport(
956 rtc::scoped_refptr<RtpReceiverInternal> selector,
957 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
958 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
959}
960
961void RTCStatsCollector::GetStatsReportInternal(
962 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700963 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100964 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700965
hbos0e6758d2016-08-31 07:57:36 -0700966 // "Now" using a monotonically increasing timer.
967 int64_t cache_now_us = rtc::TimeMicros();
968 if (cached_report_ &&
969 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800970 // We have a fresh cached report to deliver. Deliver asynchronously, since
971 // the caller may not be expecting a synchronous callback, and it avoids
972 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100973 std::vector<RequestInfo> requests;
974 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100975 signaling_thread_->PostTask(
976 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
977 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700978 } else if (!num_pending_partial_reports_) {
979 // Only start gathering stats if we're not already gathering stats. In the
980 // case of already gathering stats, |callback_| will be invoked when there
981 // are no more pending partial reports.
982
983 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
984 // UTC), in microseconds. The system clock could be modified and is not
985 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700986 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700987
hbosf415f8a2017-01-02 04:28:51 -0800988 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700989 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800990
Steve Anton57858b32018-02-15 15:19:50 -0800991 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800992 // |ProducePartialResultsOnNetworkThread| and
993 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800994 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700995 // Prepare |transport_names_| for use in
996 // |ProducePartialResultsOnNetworkThread|.
997 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800998
stefanf79ade12017-06-02 06:44:03 -0700999 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
1000 // thread.
1001 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
1002 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -07001003 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -07001004
Henrik Boström40b030e2019-02-28 09:49:31 +01001005 // Don't touch |network_report_| on the signaling thread until
1006 // ProducePartialResultsOnNetworkThread() has signaled the
1007 // |network_report_event_|.
1008 network_report_event_.Reset();
1009 network_thread_->PostTask(
1010 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -07001011 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +01001012 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -08001013 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -07001014 }
hbosd565b732016-08-30 14:04:35 -07001015}
1016
1017void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -07001018 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001019 cached_report_ = nullptr;
1020}
1021
hbosb78306a2016-12-19 05:06:57 -08001022void RTCStatsCollector::WaitForPendingRequest() {
1023 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +01001024 // If a request is pending, blocks until the |network_report_event_| is
1025 // signaled and then delivers the result. Otherwise this is a NO-OP.
1026 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -08001027}
1028
hbosc82f2e12016-09-05 01:36:50 -07001029void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1030 int64_t timestamp_us) {
1031 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +01001032 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -07001033
Henrik Boström40b030e2019-02-28 09:49:31 +01001034 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1035 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -07001036
Henrik Boström40b030e2019-02-28 09:49:31 +01001037 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1038 // signaling thread, so it is always the first partial result delivered on the
1039 // signaling thread. The request is not complete until MergeNetworkReport_s()
1040 // happens; we don't have to do anything here.
1041 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1042 --num_pending_partial_reports_;
1043}
1044
1045void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1046 int64_t timestamp_us,
1047 RTCStatsReport* partial_report) {
1048 RTC_DCHECK(signaling_thread_->IsCurrent());
1049 ProduceDataChannelStats_s(timestamp_us, partial_report);
1050 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1051 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 15:49:42 +02001052 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001053 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -07001054}
1055
hbosc82f2e12016-09-05 01:36:50 -07001056void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
1057 int64_t timestamp_us) {
1058 RTC_DCHECK(network_thread_->IsCurrent());
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +02001059 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 09:49:31 +01001060 // |network_report_event_| is reset before this method is invoked.
1061 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -07001062
Steve Anton5dfde182018-02-06 10:34:40 -08001063 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -07001064 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -08001065 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1066 PrepareTransportCertificateStats_n(transport_stats_by_name);
1067
Henrik Boström40b030e2019-02-28 09:49:31 +01001068 ProducePartialResultsOnNetworkThreadImpl(
1069 timestamp_us, transport_stats_by_name, transport_cert_stats,
1070 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001071
Henrik Boström40b030e2019-02-28 09:49:31 +01001072 // Signal that it is now safe to touch |network_report_| on the signaling
1073 // thread, and post a task to merge it into the final results.
1074 network_report_event_.Set();
1075 signaling_thread_->PostTask(
1076 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +01001077}
1078
Henrik Boström40b030e2019-02-28 09:49:31 +01001079void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1080 int64_t timestamp_us,
1081 const std::map<std::string, cricket::TransportStats>&
1082 transport_stats_by_name,
1083 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1084 RTCStatsReport* partial_report) {
1085 RTC_DCHECK(network_thread_->IsCurrent());
1086 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1087 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1088 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1089 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001090 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1091 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 13:40:25 +02001092 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1093 partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001094}
1095
1096void RTCStatsCollector::MergeNetworkReport_s() {
1097 RTC_DCHECK(signaling_thread_->IsCurrent());
1098 // The |network_report_event_| must be signaled for it to be safe to touch
1099 // |network_report_|. This is normally not blocking, but if
1100 // WaitForPendingRequest() is called while a request is pending, we might have
1101 // to wait until the network thread is done touching |network_report_|.
1102 network_report_event_.Wait(rtc::Event::kForever);
1103 if (!network_report_) {
1104 // Normally, MergeNetworkReport_s() is executed because it is posted from
1105 // the network thread. But if WaitForPendingRequest() is called while a
1106 // request is pending, an early call to MergeNetworkReport_s() is made,
1107 // merging the report and setting |network_report_| to null. If so, when the
1108 // previously posted MergeNetworkReport_s() is later executed, the report is
1109 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -07001110 return;
1111 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001112 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +01001113 RTC_DCHECK(partial_report_);
1114 partial_report_->TakeMembersFrom(network_report_);
1115 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001116 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +01001117 // |network_report_| is currently the only partial report collected
1118 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1119 // ready to deliver the result.
1120 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1121 cache_timestamp_us_ = partial_report_timestamp_us_;
1122 cached_report_ = partial_report_;
1123 partial_report_ = nullptr;
1124 transceiver_stats_infos_.clear();
1125 // Trace WebRTC Stats when getStats is called on Javascript.
1126 // This allows access to WebRTC stats from trace logs. To enable them,
1127 // select the "webrtc_stats" category when recording traces.
1128 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1129 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001130
Henrik Boström40b030e2019-02-28 09:49:31 +01001131 // Deliver report and clear |requests_|.
1132 std::vector<RequestInfo> requests;
1133 requests.swap(requests_);
1134 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001135}
1136
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001137void RTCStatsCollector::DeliverCachedReport(
1138 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001139 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001140 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001141 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001142 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001143
Henrik Boström5b3541f2018-03-19 13:52:56 +01001144 for (const RequestInfo& request : requests) {
1145 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1146 request.callback()->OnStatsDelivered(cached_report);
1147 } else {
1148 bool filter_by_sender_selector;
1149 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1150 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1151 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1152 filter_by_sender_selector = true;
1153 sender_selector = request.sender_selector();
1154 } else {
1155 RTC_DCHECK(request.filter_mode() ==
1156 RequestInfo::FilterMode::kReceiverSelector);
1157 filter_by_sender_selector = false;
1158 receiver_selector = request.receiver_selector();
1159 }
1160 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1161 filter_by_sender_selector, cached_report, sender_selector,
1162 receiver_selector));
1163 }
hbosc82f2e12016-09-05 01:36:50 -07001164 }
hbosd565b732016-08-30 14:04:35 -07001165}
1166
hbosdf6075a2016-12-19 04:58:02 -08001167void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001168 int64_t timestamp_us,
1169 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001170 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001171 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001172 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1173 if (transport_cert_stats_pair.second.local) {
1174 ProduceCertificateStatsFromSSLCertificateStats(
1175 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001176 }
hbos02ba2112016-10-28 05:14:53 -07001177 if (transport_cert_stats_pair.second.remote) {
1178 ProduceCertificateStatsFromSSLCertificateStats(
1179 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001180 }
1181 }
1182}
1183
hbosdf6075a2016-12-19 04:58:02 -08001184void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001185 int64_t timestamp_us,
1186 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001187 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001188 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001189 for (const auto& stats : transceiver_stats_infos) {
1190 if (!stats.mid) {
1191 continue;
hbos0adb8282016-11-23 02:32:06 -08001192 }
Steve Anton57858b32018-02-15 15:19:50 -08001193 const cricket::VoiceMediaInfo* voice_media_info =
1194 stats.track_media_info_map->voice_media_info();
1195 const cricket::VideoMediaInfo* video_media_info =
1196 stats.track_media_info_map->video_media_info();
1197 // Audio
1198 if (voice_media_info) {
1199 // Inbound
1200 for (const auto& pair : voice_media_info->receive_codecs) {
1201 report->AddStats(CodecStatsFromRtpCodecParameters(
1202 timestamp_us, *stats.mid, true, pair.second));
1203 }
1204 // Outbound
1205 for (const auto& pair : voice_media_info->send_codecs) {
1206 report->AddStats(CodecStatsFromRtpCodecParameters(
1207 timestamp_us, *stats.mid, false, pair.second));
1208 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001209 }
Steve Anton57858b32018-02-15 15:19:50 -08001210 // Video
1211 if (video_media_info) {
1212 // Inbound
1213 for (const auto& pair : video_media_info->receive_codecs) {
1214 report->AddStats(CodecStatsFromRtpCodecParameters(
1215 timestamp_us, *stats.mid, true, pair.second));
1216 }
1217 // Outbound
1218 for (const auto& pair : video_media_info->send_codecs) {
1219 report->AddStats(CodecStatsFromRtpCodecParameters(
1220 timestamp_us, *stats.mid, false, pair.second));
1221 }
hbos0adb8282016-11-23 02:32:06 -08001222 }
1223 }
1224}
1225
hboscc555c52016-10-18 12:48:31 -07001226void RTCStatsCollector::ProduceDataChannelStats_s(
Jonas Olssona4d87372019-07-05 19:08:33 +02001227 int64_t timestamp_us,
1228 RTCStatsReport* report) const {
hboscc555c52016-10-18 12:48:31 -07001229 RTC_DCHECK(signaling_thread_->IsCurrent());
1230 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1231 pc_->sctp_data_channels()) {
1232 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1233 new RTCDataChannelStats(
Harald Alvestrand928e7a32019-07-31 07:16:45 -04001234 "RTCDataChannel_" + rtc::ToString(data_channel->internal_id()),
hboscc555c52016-10-18 12:48:31 -07001235 timestamp_us));
1236 data_channel_stats->label = data_channel->label();
1237 data_channel_stats->protocol = data_channel->protocol();
1238 data_channel_stats->datachannelid = data_channel->id();
1239 data_channel_stats->state =
1240 DataStateToRTCDataChannelState(data_channel->state());
1241 data_channel_stats->messages_sent = data_channel->messages_sent();
1242 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1243 data_channel_stats->messages_received = data_channel->messages_received();
1244 data_channel_stats->bytes_received = data_channel->bytes_received();
1245 report->AddStats(std::move(data_channel_stats));
1246 }
1247}
1248
hbosdf6075a2016-12-19 04:58:02 -08001249void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001250 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001251 const std::map<std::string, cricket::TransportStats>&
1252 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001253 const Call::Stats& call_stats,
1254 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001255 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001256 for (const auto& entry : transport_stats_by_name) {
1257 const std::string& transport_name = entry.first;
1258 const cricket::TransportStats& transport_stats = entry.second;
1259 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001260 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001261 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001262 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 08:10:27 +02001263 channel_stats.ice_transport_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001264 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001265 new RTCIceCandidatePairStats(
1266 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1267 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001268
hbos0583b282016-11-30 01:50:14 -08001269 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001270 // TODO(hbos): There could be other candidates that are not paired with
1271 // anything. We don't have a complete list. Local candidates come from
1272 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001273 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001274 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001275 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001276 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001277 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001278 candidate_pair_stats->state =
1279 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1280 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001281 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001282 // TODO(hbos): This writable is different than the spec. It goes to
1283 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001284 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001285 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001286 candidate_pair_stats->bytes_sent =
1287 static_cast<uint64_t>(info.sent_total_bytes);
1288 candidate_pair_stats->bytes_received =
1289 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001290 candidate_pair_stats->total_round_trip_time =
1291 static_cast<double>(info.total_round_trip_time_ms) /
1292 rtc::kNumMillisecsPerSec;
1293 if (info.current_round_trip_time_ms) {
1294 candidate_pair_stats->current_round_trip_time =
1295 static_cast<double>(*info.current_round_trip_time_ms) /
1296 rtc::kNumMillisecsPerSec;
1297 }
stefanf79ade12017-06-02 06:44:03 -07001298 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001299 // The bandwidth estimations we have are for the selected candidate
1300 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001301 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1302 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1303 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001304 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001305 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001306 }
stefanf79ade12017-06-02 06:44:03 -07001307 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001308 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001309 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001310 }
1311 }
hbosd82f5122016-12-09 04:12:39 -08001312 candidate_pair_stats->requests_received =
1313 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001314 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1315 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001316 candidate_pair_stats->responses_received =
1317 static_cast<uint64_t>(info.recv_ping_responses);
1318 candidate_pair_stats->responses_sent =
1319 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001320 RTC_DCHECK_GE(info.sent_ping_requests_total,
1321 info.sent_ping_requests_before_first_response);
1322 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1323 info.sent_ping_requests_total -
1324 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001325
1326 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001327 }
1328 }
1329 }
1330}
1331
Steve Anton57858b32018-02-15 15:19:50 -08001332void RTCStatsCollector::ProduceMediaStreamStats_s(
1333 int64_t timestamp_us,
1334 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001335 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001336
1337 std::map<std::string, std::vector<std::string>> track_ids;
1338
1339 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001340 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001341 std::string track_id =
1342 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1343 kSender, sender->internal()->AttachmentId());
1344 for (auto& stream_id : sender->stream_ids()) {
1345 track_ids[stream_id].push_back(track_id);
1346 }
1347 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001348 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001349 std::string track_id =
1350 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1351 kReceiver, receiver->internal()->AttachmentId());
1352 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001353 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001354 }
1355 }
1356 }
1357
1358 // Build stats for each stream ID known.
1359 for (auto& it : track_ids) {
1360 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1361 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1362 stream_stats->stream_identifier = it.first;
1363 stream_stats->track_ids = it.second;
1364 report->AddStats(std::move(stream_stats));
1365 }
1366}
1367
1368void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1369 int64_t timestamp_us,
1370 RTCStatsReport* report) const {
1371 RTC_DCHECK(signaling_thread_->IsCurrent());
1372 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1373 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001374 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001375 senders.push_back(sender->internal());
1376 }
1377 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1378 senders, report);
1379
1380 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001381 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001382 receivers.push_back(receiver->internal());
1383 }
1384 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1385 receivers, report);
1386 }
hbos09bc1282016-11-08 06:29:22 -08001387}
1388
Henrik Boström646fda02019-05-22 15:49:42 +02001389void RTCStatsCollector::ProduceMediaSourceStats_s(
1390 int64_t timestamp_us,
1391 RTCStatsReport* report) const {
1392 RTC_DCHECK(signaling_thread_->IsCurrent());
1393 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1394 transceiver_stats_infos_) {
1395 const auto& track_media_info_map =
1396 transceiver_stats_info.track_media_info_map;
1397 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1398 const auto& sender_internal = sender->internal();
1399 const auto& track = sender_internal->track();
1400 if (!track)
1401 continue;
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001402 // TODO(https://crbug.com/webrtc/10771): The same track could be attached
1403 // to multiple senders which should result in multiple senders referencing
1404 // the same media-source stats. When all media source related metrics are
1405 // moved to the track's source (e.g. input frame rate is moved from
1406 // cricket::VideoSenderInfo to VideoTrackSourceInterface::Stats and audio
1407 // levels are moved to the corresponding audio track/source object), don't
1408 // create separate media source stats objects on a per-attachment basis.
Henrik Boström646fda02019-05-22 15:49:42 +02001409 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1410 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001411 auto audio_source_stats = absl::make_unique<RTCAudioSourceStats>(
Henrik Boström646fda02019-05-22 15:49:42 +02001412 RTCMediaSourceStatsIDFromKindAndAttachment(
1413 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1414 timestamp_us);
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001415 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1416 // SSRC assigned (there shouldn't need to exist a send-stream, created
1417 // by an O/A exchange) in order to read audio media-source stats.
1418 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1419 // value indicating no SSRC.
1420 if (sender_internal->ssrc() != 0) {
1421 auto* voice_sender_info =
1422 track_media_info_map->GetVoiceSenderInfoBySsrc(
1423 sender_internal->ssrc());
1424 if (voice_sender_info) {
1425 audio_source_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
1426 voice_sender_info->audio_level);
1427 audio_source_stats->total_audio_energy =
1428 voice_sender_info->total_input_energy;
1429 audio_source_stats->total_samples_duration =
1430 voice_sender_info->total_input_duration;
1431 }
1432 }
1433 media_source_stats = std::move(audio_source_stats);
Henrik Boström646fda02019-05-22 15:49:42 +02001434 } else {
1435 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
1436 auto video_source_stats = absl::make_unique<RTCVideoSourceStats>(
1437 RTCMediaSourceStatsIDFromKindAndAttachment(
1438 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1439 timestamp_us);
1440 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1441 auto* video_source = video_track->GetSource();
1442 VideoTrackSourceInterface::Stats source_stats;
1443 if (video_source && video_source->GetStats(&source_stats)) {
1444 video_source_stats->width = source_stats.input_width;
1445 video_source_stats->height = source_stats.input_height;
1446 }
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001447 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1448 // SSRC assigned (there shouldn't need to exist a send-stream, created
1449 // by an O/A exchange) in order to get framesPerSecond.
1450 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1451 // value indicating no SSRC.
Henrik Boström646fda02019-05-22 15:49:42 +02001452 if (sender_internal->ssrc() != 0) {
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001453 auto* video_sender_info =
1454 track_media_info_map->GetVideoSenderInfoBySsrc(
1455 sender_internal->ssrc());
1456 if (video_sender_info) {
Henrik Boström646fda02019-05-22 15:49:42 +02001457 video_source_stats->frames_per_second =
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001458 video_sender_info->framerate_input;
Henrik Boström646fda02019-05-22 15:49:42 +02001459 }
1460 }
1461 media_source_stats = std::move(video_source_stats);
1462 }
1463 media_source_stats->track_identifier = track->id();
1464 media_source_stats->kind = track->kind();
1465 report->AddStats(std::move(media_source_stats));
1466 }
1467 }
1468}
1469
hbos6ab97ce2016-10-03 14:16:56 -07001470void RTCStatsCollector::ProducePeerConnectionStats_s(
Jonas Olssona4d87372019-07-05 19:08:33 +02001471 int64_t timestamp_us,
1472 RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001473 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001474 std::unique_ptr<RTCPeerConnectionStats> stats(
Jonas Olssona4d87372019-07-05 19:08:33 +02001475 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001476 stats->data_channels_opened = internal_record_.data_channels_opened;
1477 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001478 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001479}
1480
hbosdf6075a2016-12-19 04:58:02 -08001481void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001482 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001483 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001484 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001485 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001486
Steve Anton57858b32018-02-15 15:19:50 -08001487 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1488 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1489 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1490 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1491 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1492 } else {
1493 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001494 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001495 }
Steve Anton57858b32018-02-15 15:19:50 -08001496}
1497
1498void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1499 int64_t timestamp_us,
1500 const RtpTransceiverStatsInfo& stats,
1501 RTCStatsReport* report) const {
1502 if (!stats.mid || !stats.transport_name) {
1503 return;
1504 }
1505 RTC_DCHECK(stats.track_media_info_map);
1506 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1507 RTC_DCHECK(track_media_info_map.voice_media_info());
1508 std::string mid = *stats.mid;
1509 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1510 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1511 // Inbound
1512 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1513 track_media_info_map.voice_media_info()->receivers) {
1514 if (!voice_receiver_info.connected())
1515 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001516 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001517 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1518 timestamp_us);
1519 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1520 inbound_audio.get());
1521 // TODO(hta): This lookup should look for the sender, not the track.
1522 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1523 track_media_info_map.GetAudioTrack(voice_receiver_info);
1524 if (audio_track) {
1525 inbound_audio->track_id =
1526 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1527 kReceiver,
1528 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001529 }
Steve Anton57858b32018-02-15 15:19:50 -08001530 inbound_audio->transport_id = transport_id;
1531 report->AddStats(std::move(inbound_audio));
1532 }
1533 // Outbound
1534 for (const cricket::VoiceSenderInfo& voice_sender_info :
1535 track_media_info_map.voice_media_info()->senders) {
1536 if (!voice_sender_info.connected())
1537 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001538 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001539 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1540 timestamp_us);
1541 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1542 outbound_audio.get());
1543 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1544 track_media_info_map.GetAudioTrack(voice_sender_info);
1545 if (audio_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001546 int attachment_id =
1547 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001548 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001549 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1550 attachment_id);
1551 outbound_audio->media_source_id =
1552 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1553 attachment_id);
Steve Anton56bae8d2018-02-14 16:07:42 -08001554 }
Steve Anton57858b32018-02-15 15:19:50 -08001555 outbound_audio->transport_id = transport_id;
1556 report->AddStats(std::move(outbound_audio));
1557 }
Henrik Boström883eefc2019-05-27 13:40:25 +02001558 // Remote-inbound
1559 // These are Report Block-based, information sent from the remote endpoint,
1560 // providing metrics about our Outbound streams. We take advantage of the fact
1561 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1562 // been added to the report.
1563 for (const cricket::VoiceSenderInfo& voice_sender_info :
1564 track_media_info_map.voice_media_info()->senders) {
1565 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1566 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
1567 report_block_data, cricket::MEDIA_TYPE_AUDIO, *report));
1568 }
1569 }
Steve Anton57858b32018-02-15 15:19:50 -08001570}
1571
1572void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1573 int64_t timestamp_us,
1574 const RtpTransceiverStatsInfo& stats,
1575 RTCStatsReport* report) const {
1576 if (!stats.mid || !stats.transport_name) {
1577 return;
1578 }
1579 RTC_DCHECK(stats.track_media_info_map);
1580 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1581 RTC_DCHECK(track_media_info_map.video_media_info());
1582 std::string mid = *stats.mid;
1583 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1584 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1585 // Inbound
1586 for (const cricket::VideoReceiverInfo& video_receiver_info :
1587 track_media_info_map.video_media_info()->receivers) {
1588 if (!video_receiver_info.connected())
1589 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001590 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001591 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1592 timestamp_us);
1593 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1594 inbound_video.get());
1595 rtc::scoped_refptr<VideoTrackInterface> video_track =
1596 track_media_info_map.GetVideoTrack(video_receiver_info);
1597 if (video_track) {
1598 inbound_video->track_id =
1599 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1600 kReceiver,
1601 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1602 }
1603 inbound_video->transport_id = transport_id;
1604 report->AddStats(std::move(inbound_video));
1605 }
1606 // Outbound
1607 for (const cricket::VideoSenderInfo& video_sender_info :
1608 track_media_info_map.video_media_info()->senders) {
1609 if (!video_sender_info.connected())
1610 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001611 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001612 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1613 timestamp_us);
1614 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1615 outbound_video.get());
1616 rtc::scoped_refptr<VideoTrackInterface> video_track =
1617 track_media_info_map.GetVideoTrack(video_sender_info);
1618 if (video_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001619 int attachment_id =
1620 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001621 outbound_video->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001622 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1623 attachment_id);
1624 outbound_video->media_source_id =
1625 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1626 attachment_id);
Steve Anton57858b32018-02-15 15:19:50 -08001627 }
1628 outbound_video->transport_id = transport_id;
1629 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001630 }
Henrik Boström883eefc2019-05-27 13:40:25 +02001631 // Remote-inbound
1632 // These are Report Block-based, information sent from the remote endpoint,
1633 // providing metrics about our Outbound streams. We take advantage of the fact
1634 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1635 // been added to the report.
1636 for (const cricket::VideoSenderInfo& video_sender_info :
1637 track_media_info_map.video_media_info()->senders) {
1638 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1639 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
1640 report_block_data, cricket::MEDIA_TYPE_VIDEO, *report));
1641 }
1642 }
hbos6ded1902016-11-01 01:50:46 -07001643}
1644
hbosdf6075a2016-12-19 04:58:02 -08001645void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001646 int64_t timestamp_us,
1647 const std::map<std::string, cricket::TransportStats>&
1648 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001649 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1650 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001651 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001652 for (const auto& entry : transport_stats_by_name) {
1653 const std::string& transport_name = entry.first;
1654 const cricket::TransportStats& transport_stats = entry.second;
1655
hbos2fa7c672016-10-24 04:00:05 -07001656 // Get reference to RTCP channel, if it exists.
1657 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001658 for (const cricket::TransportChannelStats& channel_stats :
1659 transport_stats.channel_stats) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001660 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
hbos2fa7c672016-10-24 04:00:05 -07001661 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001662 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001663 break;
1664 }
1665 }
1666
1667 // Get reference to local and remote certificates of this transport, if they
1668 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001669 const auto& certificate_stats_it =
1670 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001671 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1672 std::string local_certificate_id;
1673 if (certificate_stats_it->second.local) {
1674 local_certificate_id = RTCCertificateIDFromFingerprint(
1675 certificate_stats_it->second.local->fingerprint);
1676 }
1677 std::string remote_certificate_id;
1678 if (certificate_stats_it->second.remote) {
1679 remote_certificate_id = RTCCertificateIDFromFingerprint(
1680 certificate_stats_it->second.remote->fingerprint);
1681 }
1682
1683 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001684 for (const cricket::TransportChannelStats& channel_stats :
1685 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001686 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001687 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1688 transport_name, channel_stats.component),
1689 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001690 transport_stats->bytes_sent = 0;
1691 transport_stats->bytes_received = 0;
Jonas Olssona4d87372019-07-05 19:08:33 +02001692 transport_stats->dtls_state =
1693 DtlsTransportStateToRTCDtlsTransportState(channel_stats.dtls_state);
Jonas Oreland149dc722019-08-28 08:10:27 +02001694 transport_stats->selected_candidate_pair_changes =
1695 channel_stats.ice_transport_stats.selected_candidate_pair_changes;
hbos2fa7c672016-10-24 04:00:05 -07001696 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 08:10:27 +02001697 channel_stats.ice_transport_stats.connection_infos) {
hbos2fa7c672016-10-24 04:00:05 -07001698 *transport_stats->bytes_sent += info.sent_total_bytes;
1699 *transport_stats->bytes_received += info.recv_total_bytes;
1700 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001701 transport_stats->selected_candidate_pair_id =
1702 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1703 }
1704 }
1705 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1706 !rtcp_transport_stats_id.empty()) {
1707 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1708 }
1709 if (!local_certificate_id.empty())
1710 transport_stats->local_certificate_id = local_certificate_id;
1711 if (!remote_certificate_id.empty())
1712 transport_stats->remote_certificate_id = remote_certificate_id;
1713 report->AddStats(std::move(transport_stats));
1714 }
1715 }
1716}
1717
1718std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001719RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001720 const std::map<std::string, cricket::TransportStats>&
1721 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001722 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001723 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001724 for (const auto& entry : transport_stats_by_name) {
1725 const std::string& transport_name = entry.first;
1726
hbos2fa7c672016-10-24 04:00:05 -07001727 CertificateStatsPair certificate_stats_pair;
1728 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001729 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001730 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001731 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001732 }
Steve Anton5dfde182018-02-06 10:34:40 -08001733
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001734 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1735 pc_->GetRemoteSSLCertChain(transport_name);
1736 if (remote_cert_chain) {
1737 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001738 }
Steve Anton5dfde182018-02-06 10:34:40 -08001739
hbos2fa7c672016-10-24 04:00:05 -07001740 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001741 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001742 }
1743 return transport_cert_stats;
1744}
1745
Steve Anton57858b32018-02-15 15:19:50 -08001746std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1747RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1748 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001749
Steve Anton57858b32018-02-15 15:19:50 -08001750 // These are used to invoke GetStats for all the media channels together in
1751 // one worker thread hop.
1752 std::map<cricket::VoiceMediaChannel*,
1753 std::unique_ptr<cricket::VoiceMediaInfo>>
1754 voice_stats;
1755 std::map<cricket::VideoMediaChannel*,
1756 std::unique_ptr<cricket::VideoMediaInfo>>
1757 video_stats;
1758
Mirko Bonadei739baf02019-01-27 17:29:42 +01001759 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001760 cricket::MediaType media_type = transceiver->media_type();
1761
1762 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1763 // stats have been fetched on the worker thread.
1764 transceiver_stats_infos.emplace_back();
1765 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1766 stats.transceiver = transceiver->internal();
1767 stats.media_type = media_type;
1768
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001769 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001770 if (!channel) {
1771 // The remaining fields require a BaseChannel.
1772 continue;
1773 }
1774
1775 stats.mid = channel->content_name();
1776 stats.transport_name = channel->transport_name();
1777
1778 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1779 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1780 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1781 voice_stats.end());
1782 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001783 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001784 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1785 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1786 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1787 video_stats.end());
1788 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001789 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001790 } else {
1791 RTC_NOTREACHED();
1792 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001793 }
Steve Anton57858b32018-02-15 15:19:50 -08001794
1795 // Call GetStats for all media channels together on the worker thread in one
1796 // hop.
1797 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1798 for (const auto& entry : voice_stats) {
1799 if (!entry.first->GetStats(entry.second.get())) {
1800 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1801 }
1802 }
1803 for (const auto& entry : video_stats) {
1804 if (!entry.first->GetStats(entry.second.get())) {
1805 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1806 }
1807 }
1808 });
1809
1810 // Create the TrackMediaInfoMap for each transceiver stats object.
1811 for (auto& stats : transceiver_stats_infos) {
1812 auto transceiver = stats.transceiver;
1813 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1814 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1815 if (transceiver->channel()) {
1816 cricket::MediaType media_type = transceiver->media_type();
1817 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1818 auto* voice_channel =
1819 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1820 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1821 voice_media_info =
1822 std::move(voice_stats[voice_channel->media_channel()]);
1823 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1824 auto* video_channel =
1825 static_cast<cricket::VideoChannel*>(transceiver->channel());
1826 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1827 video_media_info =
1828 std::move(video_stats[video_channel->media_channel()]);
1829 }
1830 }
1831 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001832 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001833 senders.push_back(sender->internal());
1834 }
1835 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001836 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001837 receivers.push_back(receiver->internal());
1838 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001839 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001840 std::move(voice_media_info), std::move(video_media_info), senders,
1841 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001842 }
Steve Anton57858b32018-02-15 15:19:50 -08001843
1844 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001845}
1846
Steve Anton7eca0932018-03-30 15:18:41 -07001847std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1848 std::set<std::string> transport_names;
1849 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1850 if (transceiver->internal()->channel()) {
1851 transport_names.insert(
1852 transceiver->internal()->channel()->transport_name());
1853 }
1854 }
1855 if (pc_->rtp_data_channel()) {
1856 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1857 }
1858 if (pc_->sctp_transport_name()) {
1859 transport_names.insert(*pc_->sctp_transport_name());
1860 }
1861 return transport_names;
1862}
1863
hbos82ebe022016-11-14 01:41:09 -08001864void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1865 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1866 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1867}
1868
1869void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1870 RTC_DCHECK(signaling_thread_->IsCurrent());
Jonas Olssona4d87372019-07-05 19:08:33 +02001871 bool result = internal_record_.opened_data_channels
1872 .insert(reinterpret_cast<uintptr_t>(channel))
1873 .second;
hbos82ebe022016-11-14 01:41:09 -08001874 ++internal_record_.data_channels_opened;
1875 RTC_DCHECK(result);
1876}
1877
1878void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1879 RTC_DCHECK(signaling_thread_->IsCurrent());
1880 // Only channels that have been fully opened (and have increased the
1881 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001882 if (internal_record_.opened_data_channels.erase(
1883 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001884 ++internal_record_.data_channels_closed;
1885 }
1886}
1887
hboscc555c52016-10-18 12:48:31 -07001888const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1889 const std::string& type) {
1890 return CandidateTypeToRTCIceCandidateType(type);
1891}
1892
1893const char* DataStateToRTCDataChannelStateForTesting(
1894 DataChannelInterface::DataState state) {
1895 return DataStateToRTCDataChannelState(state);
1896}
1897
hbosd565b732016-08-30 14:04:35 -07001898} // namespace webrtc