blob: 3bf856636d930df1f9fb018ac81bf14bc5261588 [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
hbos2fa7c672016-10-24 04:00:05 -070037std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
38 return "RTCCertificate_" + fingerprint;
39}
40
Steve Anton57858b32018-02-15 15:19:50 -080041std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
42 bool inbound,
43 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020044 char buf[1024];
45 rtc::SimpleStringBuilder sb(buf);
46 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
47 << payload_type;
48 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080049}
50
hbos2fa7c672016-10-24 04:00:05 -070051std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
52 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020053 char buf[4096];
54 rtc::SimpleStringBuilder sb(buf);
55 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
56 << info.remote_candidate.id();
57 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070058}
59
Harald Alvestranda3dab842018-01-14 09:18:58 +010060const char kSender[] = "sender";
61const char kReceiver[] = "receiver";
62
63std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
64 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010065 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020066 char buf[1024];
67 rtc::SimpleStringBuilder sb(buf);
68 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
69 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080070}
71
hbos2fa7c672016-10-24 04:00:05 -070072std::string RTCTransportStatsIDFromTransportChannel(
73 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020074 char buf[1024];
75 rtc::SimpleStringBuilder sb(buf);
76 sb << "RTCTransport_" << transport_name << "_" << channel_component;
77 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070078}
79
hboseeafe942016-11-01 03:00:17 -070080std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020081 char buf[1024];
82 rtc::SimpleStringBuilder sb(buf);
83 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
84 return sb.str();
hboseeafe942016-11-01 03:00:17 -070085}
86
hbos6ded1902016-11-01 01:50:46 -070087std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020088 char buf[1024];
89 rtc::SimpleStringBuilder sb(buf);
90 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
91 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070092}
93
hbosab9f6e42016-10-07 02:18:47 -070094const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
95 if (type == cricket::LOCAL_PORT_TYPE)
96 return RTCIceCandidateType::kHost;
97 if (type == cricket::STUN_PORT_TYPE)
98 return RTCIceCandidateType::kSrflx;
99 if (type == cricket::PRFLX_PORT_TYPE)
100 return RTCIceCandidateType::kPrflx;
101 if (type == cricket::RELAY_PORT_TYPE)
102 return RTCIceCandidateType::kRelay;
103 RTC_NOTREACHED();
104 return nullptr;
105}
106
hboscc555c52016-10-18 12:48:31 -0700107const char* DataStateToRTCDataChannelState(
108 DataChannelInterface::DataState state) {
109 switch (state) {
110 case DataChannelInterface::kConnecting:
111 return RTCDataChannelState::kConnecting;
112 case DataChannelInterface::kOpen:
113 return RTCDataChannelState::kOpen;
114 case DataChannelInterface::kClosing:
115 return RTCDataChannelState::kClosing;
116 case DataChannelInterface::kClosed:
117 return RTCDataChannelState::kClosed;
118 default:
119 RTC_NOTREACHED();
120 return nullptr;
121 }
122}
123
hbos06495bc2017-01-02 08:08:18 -0800124const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
125 cricket::IceCandidatePairState state) {
126 switch (state) {
127 case cricket::IceCandidatePairState::WAITING:
128 return RTCStatsIceCandidatePairState::kWaiting;
129 case cricket::IceCandidatePairState::IN_PROGRESS:
130 return RTCStatsIceCandidatePairState::kInProgress;
131 case cricket::IceCandidatePairState::SUCCEEDED:
132 return RTCStatsIceCandidatePairState::kSucceeded;
133 case cricket::IceCandidatePairState::FAILED:
134 return RTCStatsIceCandidatePairState::kFailed;
135 default:
136 RTC_NOTREACHED();
137 return nullptr;
138 }
139}
140
hbos7064d592017-01-16 07:38:02 -0800141const char* DtlsTransportStateToRTCDtlsTransportState(
142 cricket::DtlsTransportState state) {
143 switch (state) {
144 case cricket::DTLS_TRANSPORT_NEW:
145 return RTCDtlsTransportState::kNew;
146 case cricket::DTLS_TRANSPORT_CONNECTING:
147 return RTCDtlsTransportState::kConnecting;
148 case cricket::DTLS_TRANSPORT_CONNECTED:
149 return RTCDtlsTransportState::kConnected;
150 case cricket::DTLS_TRANSPORT_CLOSED:
151 return RTCDtlsTransportState::kClosed;
152 case cricket::DTLS_TRANSPORT_FAILED:
153 return RTCDtlsTransportState::kFailed;
154 default:
155 RTC_NOTREACHED();
156 return nullptr;
157 }
158}
159
Gary Liu37e489c2017-11-21 10:49:36 -0800160const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
161 switch (type) {
162 case rtc::ADAPTER_TYPE_CELLULAR:
163 return RTCNetworkType::kCellular;
164 case rtc::ADAPTER_TYPE_ETHERNET:
165 return RTCNetworkType::kEthernet;
166 case rtc::ADAPTER_TYPE_WIFI:
167 return RTCNetworkType::kWifi;
168 case rtc::ADAPTER_TYPE_VPN:
169 return RTCNetworkType::kVpn;
170 case rtc::ADAPTER_TYPE_UNKNOWN:
171 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700172 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800173 return RTCNetworkType::kUnknown;
174 }
175 RTC_NOTREACHED();
176 return nullptr;
177}
178
hbos9e302742017-01-20 02:47:10 -0800179double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
180 RTC_DCHECK_GE(audio_level, 0);
181 RTC_DCHECK_LE(audio_level, 32767);
182 return audio_level / 32767.0;
183}
184
hbos0adb8282016-11-23 02:32:06 -0800185std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800186 uint64_t timestamp_us,
187 const std::string& mid,
188 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800189 const RtpCodecParameters& codec_params) {
190 RTC_DCHECK_GE(codec_params.payload_type, 0);
191 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800192 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800193 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
194 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800195 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800196 timestamp_us));
197 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800198 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800199 if (codec_params.clock_rate) {
200 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
201 }
hbos0adb8282016-11-23 02:32:06 -0800202 return codec_stats;
203}
204
hbos09bc1282016-11-08 06:29:22 -0800205void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
206 const MediaStreamTrackInterface& track,
207 RTCMediaStreamTrackStats* track_stats) {
208 track_stats->track_identifier = track.id();
209 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
210}
211
hbos820f5782016-11-22 03:16:50 -0800212// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700213void SetInboundRTPStreamStatsFromMediaReceiverInfo(
214 const cricket::MediaReceiverInfo& media_receiver_info,
215 RTCInboundRTPStreamStats* inbound_stats) {
216 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800217 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100218 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700219 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700220 inbound_stats->packets_received =
221 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
222 inbound_stats->bytes_received =
223 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800224 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100225 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700226 inbound_stats->fraction_lost =
227 static_cast<double>(media_receiver_info.fraction_lost);
228}
229
230void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800231 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700232 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800233 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700234 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800235 voice_receiver_info, inbound_audio);
236 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200237 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800238 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800239 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
240 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800241 }
hbos820f5782016-11-22 03:16:50 -0800242 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700243 static_cast<double>(voice_receiver_info.jitter_ms) /
244 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800245 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
246 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 17:32:00 +0200247 if (voice_receiver_info.last_packet_received_timestamp_ms) {
248 inbound_audio->last_packet_received_timestamp =
249 static_cast<double>(
250 *voice_receiver_info.last_packet_received_timestamp_ms) /
251 rtc::kNumMillisecsPerSec;
252 }
hboseeafe942016-11-01 03:00:17 -0700253}
254
255void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800256 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700257 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800258 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700259 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800260 video_receiver_info, inbound_video);
261 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200262 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800263 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800264 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
265 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800266 }
hbos820f5782016-11-22 03:16:50 -0800267 inbound_video->fir_count =
268 static_cast<uint32_t>(video_receiver_info.firs_sent);
269 inbound_video->pli_count =
270 static_cast<uint32_t>(video_receiver_info.plis_sent);
271 inbound_video->nack_count =
272 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800273 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800274 if (video_receiver_info.qp_sum)
275 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Henrik Boström01738c62019-04-15 17:32:00 +0200276 if (video_receiver_info.last_packet_received_timestamp_ms) {
277 inbound_video->last_packet_received_timestamp =
278 static_cast<double>(
279 *video_receiver_info.last_packet_received_timestamp_ms) /
280 rtc::kNumMillisecsPerSec;
281 }
Henrik Boström2e069262019-04-09 13:59:31 +0200282 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
283 // optional, support the "unspecified" value.
284 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
285 inbound_video->content_type = RTCContentType::kScreenshare;
hboseeafe942016-11-01 03:00:17 -0700286}
287
hbos820f5782016-11-22 03:16:50 -0800288// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700289void SetOutboundRTPStreamStatsFromMediaSenderInfo(
290 const cricket::MediaSenderInfo& media_sender_info,
291 RTCOutboundRTPStreamStats* outbound_stats) {
292 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800293 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100294 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700295 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700296 outbound_stats->packets_sent =
297 static_cast<uint32_t>(media_sender_info.packets_sent);
298 outbound_stats->bytes_sent =
299 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700300}
301
302void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800303 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700304 const cricket::VoiceSenderInfo& voice_sender_info,
305 RTCOutboundRTPStreamStats* outbound_audio) {
306 SetOutboundRTPStreamStatsFromMediaSenderInfo(
307 voice_sender_info, outbound_audio);
308 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200309 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800310 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800311 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
312 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800313 }
hbos6ded1902016-11-01 01:50:46 -0700314 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
315 // purposefully left undefined for audio.
316}
317
318void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800319 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700320 const cricket::VideoSenderInfo& video_sender_info,
321 RTCOutboundRTPStreamStats* outbound_video) {
322 SetOutboundRTPStreamStatsFromMediaSenderInfo(
323 video_sender_info, outbound_video);
324 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200325 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800326 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800327 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
328 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800329 }
hbos6ded1902016-11-01 01:50:46 -0700330 outbound_video->fir_count =
331 static_cast<uint32_t>(video_sender_info.firs_rcvd);
332 outbound_video->pli_count =
333 static_cast<uint32_t>(video_sender_info.plis_rcvd);
334 outbound_video->nack_count =
335 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800336 if (video_sender_info.qp_sum)
337 outbound_video->qp_sum = *video_sender_info.qp_sum;
338 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200339 outbound_video->total_encode_time =
340 static_cast<double>(video_sender_info.total_encode_time_ms) /
341 rtc::kNumMillisecsPerSec;
Henrik Boström2e069262019-04-09 13:59:31 +0200342 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
343 // optional, support the "unspecified" value.
344 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
345 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700346}
347
hbos02ba2112016-10-28 05:14:53 -0700348void ProduceCertificateStatsFromSSLCertificateStats(
349 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
350 RTCStatsReport* report) {
351 RTCCertificateStats* prev_certificate_stats = nullptr;
352 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
353 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800354 std::string certificate_stats_id =
355 RTCCertificateIDFromFingerprint(s->fingerprint);
356 // It is possible for the same certificate to show up multiple times, e.g.
357 // if local and remote side use the same certificate in a loopback call.
358 // If the report already contains stats for this certificate, skip it.
359 if (report->Get(certificate_stats_id)) {
360 RTC_DCHECK_EQ(s, &certificate_stats);
361 break;
362 }
hbos02ba2112016-10-28 05:14:53 -0700363 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800364 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700365 certificate_stats->fingerprint = s->fingerprint;
366 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
367 certificate_stats->base64_certificate = s->base64_certificate;
368 if (prev_certificate_stats)
369 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
370 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
371 prev_certificate_stats = certificate_stats;
372 }
373}
374
375const std::string& ProduceIceCandidateStats(
376 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800377 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700378 const std::string& id = "RTCIceCandidate_" + candidate.id();
379 const RTCStats* stats = report->Get(id);
380 if (!stats) {
381 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
382 if (is_local)
383 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
384 else
385 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800386 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800387 if (is_local) {
388 candidate_stats->network_type =
389 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200390 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
391 std::string relay_protocol = candidate.relay_protocol();
392 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
393 relay_protocol.compare("tcp") == 0 ||
394 relay_protocol.compare("tls") == 0);
395 candidate_stats->relay_protocol = relay_protocol;
396 }
Gary Liu37e489c2017-11-21 10:49:36 -0800397 } else {
398 // We don't expect to know the adapter type of remote candidates.
399 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
400 }
hbos02ba2112016-10-28 05:14:53 -0700401 candidate_stats->ip = candidate.address().ipaddr().ToString();
402 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
403 candidate_stats->protocol = candidate.protocol();
404 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
405 candidate.type());
406 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
407
408 stats = candidate_stats.get();
409 report->AddStats(std::move(candidate_stats));
410 }
411 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
412 : RTCRemoteIceCandidateStats::kType);
413 return stats->id();
414}
415
hbos9e302742017-01-20 02:47:10 -0800416std::unique_ptr<RTCMediaStreamTrackStats>
417ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
418 int64_t timestamp_us,
419 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100420 const cricket::VoiceSenderInfo& voice_sender_info,
421 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800422 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
423 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100424 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
425 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100426 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800427 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
428 audio_track, audio_track_stats.get());
429 audio_track_stats->remote_source = false;
430 audio_track_stats->detached = false;
431 if (voice_sender_info.audio_level >= 0) {
432 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
433 voice_sender_info.audio_level);
434 }
zsteine76bd3a2017-07-14 12:17:49 -0700435 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
436 audio_track_stats->total_samples_duration =
437 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100438 if (voice_sender_info.apm_statistics.echo_return_loss) {
439 audio_track_stats->echo_return_loss =
440 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800441 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100442 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
443 audio_track_stats->echo_return_loss_enhancement =
444 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800445 }
446 return audio_track_stats;
447}
448
449std::unique_ptr<RTCMediaStreamTrackStats>
450ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
451 int64_t timestamp_us,
452 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100453 const cricket::VoiceReceiverInfo& voice_receiver_info,
454 int attachment_id) {
455 // Since receiver tracks can't be reattached, we use the SSRC as
456 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800457 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
458 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100459 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
460 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100461 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800462 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
463 audio_track, audio_track_stats.get());
464 audio_track_stats->remote_source = true;
465 audio_track_stats->detached = false;
466 if (voice_receiver_info.audio_level >= 0) {
467 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
468 voice_receiver_info.audio_level);
469 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200470 audio_track_stats->jitter_buffer_delay =
471 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100472 audio_track_stats->jitter_buffer_emitted_count =
473 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 12:17:49 -0700474 audio_track_stats->total_audio_energy =
475 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700476 audio_track_stats->total_samples_received =
477 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700478 audio_track_stats->total_samples_duration =
479 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700480 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200481 audio_track_stats->concealment_events =
482 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100483 audio_track_stats->jitter_buffer_flushes =
484 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100485 audio_track_stats->delayed_packet_outage_samples =
486 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100487 audio_track_stats->relative_packet_arrival_delay =
488 voice_receiver_info.relative_packet_arrival_delay_seconds;
hbos9e302742017-01-20 02:47:10 -0800489 return audio_track_stats;
490}
491
492std::unique_ptr<RTCMediaStreamTrackStats>
493ProduceMediaStreamTrackStatsFromVideoSenderInfo(
494 int64_t timestamp_us,
495 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100496 const cricket::VideoSenderInfo& video_sender_info,
497 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800498 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
499 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100500 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
501
502 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100503 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800504 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
505 video_track, video_track_stats.get());
506 video_track_stats->remote_source = false;
507 video_track_stats->detached = false;
508 video_track_stats->frame_width = static_cast<uint32_t>(
509 video_sender_info.send_frame_width);
510 video_track_stats->frame_height = static_cast<uint32_t>(
511 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800512 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100513 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800514 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100515 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800516 return video_track_stats;
517}
518
519std::unique_ptr<RTCMediaStreamTrackStats>
520ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
521 int64_t timestamp_us,
522 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100523 const cricket::VideoReceiverInfo& video_receiver_info,
524 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800525 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
526 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100527 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
528
529 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100530 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800531 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
532 video_track, video_track_stats.get());
533 video_track_stats->remote_source = true;
534 video_track_stats->detached = false;
535 if (video_receiver_info.frame_width > 0 &&
536 video_receiver_info.frame_height > 0) {
537 video_track_stats->frame_width = static_cast<uint32_t>(
538 video_receiver_info.frame_width);
539 video_track_stats->frame_height = static_cast<uint32_t>(
540 video_receiver_info.frame_height);
541 }
hbos42f6d2f2017-01-20 03:56:50 -0800542 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800543 // TODO(hbos): When we support receiving simulcast, this should be the total
544 // number of frames correctly decoded, independent of which SSRC it was
545 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100546 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800547 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800548 RTC_DCHECK_GE(video_receiver_info.frames_received,
549 video_receiver_info.frames_rendered);
550 video_track_stats->frames_dropped = video_receiver_info.frames_received -
551 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100552 video_track_stats->freeze_count = video_receiver_info.freeze_count;
553 video_track_stats->pause_count = video_receiver_info.pause_count;
554 video_track_stats->total_freezes_duration =
555 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
556 rtc::kNumMillisecsPerSec;
557 video_track_stats->total_pauses_duration =
558 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
559 rtc::kNumMillisecsPerSec;
560 video_track_stats->total_frames_duration =
561 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
562 rtc::kNumMillisecsPerSec;
563 video_track_stats->sum_squared_frame_durations =
564 video_receiver_info.sum_squared_frame_durations;
565
hbos9e302742017-01-20 02:47:10 -0800566 return video_track_stats;
567}
568
Harald Alvestrand89061872018-01-02 14:08:34 +0100569void ProduceSenderMediaTrackStats(
570 int64_t timestamp_us,
571 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800572 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100573 RTCStatsReport* report) {
574 // This function iterates over the senders to generate outgoing track stats.
575
576 // TODO(hbos): Return stats of detached tracks. We have to perform stats
577 // gathering at the time of detachment to get accurate stats and timestamps.
578 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100579 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100580 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
581 AudioTrackInterface* track =
582 static_cast<AudioTrackInterface*>(sender->track().get());
583 if (!track)
584 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100585 cricket::VoiceSenderInfo null_sender_info;
586 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
587 // TODO(hta): Checking on ssrc is not proper. There should be a way
588 // to see from a sender whether it's connected or not.
589 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800590 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100591 // When pc.close is called, sender info is discarded, so
592 // we generate zeroes instead. Bug: It should be retained.
593 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800594 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100595 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100596 if (sender_info) {
597 voice_sender_info = sender_info;
598 } else {
599 RTC_LOG(LS_INFO)
600 << "RTCStatsCollector: No voice sender info for sender with ssrc "
601 << sender->ssrc();
602 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100603 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100604 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100605 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
606 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100607 report->AddStats(std::move(audio_track_stats));
608 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
609 VideoTrackInterface* track =
610 static_cast<VideoTrackInterface*>(sender->track().get());
611 if (!track)
612 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100613 cricket::VideoSenderInfo null_sender_info;
614 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
615 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100616 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
617 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800618 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100619 // When pc.close is called, sender info is discarded, so
620 // we generate zeroes instead. Bug: It should be retained.
621 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800622 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100623 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100624 if (sender_info) {
625 video_sender_info = sender_info;
626 } else {
627 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
628 << sender->ssrc();
629 }
630 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100631 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100632 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
633 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100634 report->AddStats(std::move(video_track_stats));
635 } else {
636 RTC_NOTREACHED();
637 }
638 }
639}
640
641void ProduceReceiverMediaTrackStats(
642 int64_t timestamp_us,
643 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800644 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100645 RTCStatsReport* report) {
646 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100647 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100648 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
649 AudioTrackInterface* track =
650 static_cast<AudioTrackInterface*>(receiver->track().get());
651 const cricket::VoiceReceiverInfo* voice_receiver_info =
652 track_media_info_map.GetVoiceReceiverInfo(*track);
653 if (!voice_receiver_info) {
654 continue;
655 }
656 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
657 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100658 timestamp_us, *track, *voice_receiver_info,
659 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100660 report->AddStats(std::move(audio_track_stats));
661 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
662 VideoTrackInterface* track =
663 static_cast<VideoTrackInterface*>(receiver->track().get());
664 const cricket::VideoReceiverInfo* video_receiver_info =
665 track_media_info_map.GetVideoReceiverInfo(*track);
666 if (!video_receiver_info) {
667 continue;
668 }
669 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
670 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100671 timestamp_us, *track, *video_receiver_info,
672 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100673 report->AddStats(std::move(video_track_stats));
674 } else {
675 RTC_NOTREACHED();
676 }
677 }
678}
679
Henrik Boström5b3541f2018-03-19 13:52:56 +0100680rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
681 bool filter_by_sender_selector,
682 rtc::scoped_refptr<const RTCStatsReport> report,
683 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
684 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
685 std::vector<std::string> rtpstream_ids;
686 if (filter_by_sender_selector) {
687 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
688 if (sender_selector) {
689 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
690 // reference the sender stats.
691 // Because we do not implement sender stats, we look at outbound-rtp(s)
692 // that reference the track attachment stats for the sender instead.
693 std::string track_id =
694 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
695 kSender, sender_selector->AttachmentId());
696 for (const auto& stats : *report) {
697 if (stats.type() != RTCOutboundRTPStreamStats::kType)
698 continue;
699 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
700 if (outbound_rtp.track_id.is_defined() &&
701 *outbound_rtp.track_id == track_id) {
702 rtpstream_ids.push_back(outbound_rtp.id());
703 }
704 }
705 }
706 } else {
707 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
708 if (receiver_selector) {
709 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
710 // reference the receiver stats.
711 // Because we do not implement receiver stats, we look at inbound-rtp(s)
712 // that reference the track attachment stats for the receiver instead.
713 std::string track_id =
714 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
715 kReceiver, receiver_selector->AttachmentId());
716 for (const auto& stats : *report) {
717 if (stats.type() != RTCInboundRTPStreamStats::kType)
718 continue;
719 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
720 if (inbound_rtp.track_id.is_defined() &&
721 *inbound_rtp.track_id == track_id) {
722 rtpstream_ids.push_back(inbound_rtp.id());
723 }
724 }
725 }
726 }
727 if (rtpstream_ids.empty())
728 return RTCStatsReport::Create(report->timestamp_us());
729 return TakeReferencedStats(report->Copy(), rtpstream_ids);
730}
731
hboscc555c52016-10-18 12:48:31 -0700732} // namespace
733
Henrik Boström5b3541f2018-03-19 13:52:56 +0100734RTCStatsCollector::RequestInfo::RequestInfo(
735 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
736 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
737
738RTCStatsCollector::RequestInfo::RequestInfo(
739 rtc::scoped_refptr<RtpSenderInternal> selector,
740 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
741 : RequestInfo(FilterMode::kSenderSelector,
742 std::move(callback),
743 std::move(selector),
744 nullptr) {}
745
746RTCStatsCollector::RequestInfo::RequestInfo(
747 rtc::scoped_refptr<RtpReceiverInternal> selector,
748 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
749 : RequestInfo(FilterMode::kReceiverSelector,
750 std::move(callback),
751 nullptr,
752 std::move(selector)) {}
753
754RTCStatsCollector::RequestInfo::RequestInfo(
755 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
756 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
757 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
758 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
759 : filter_mode_(filter_mode),
760 callback_(std::move(callback)),
761 sender_selector_(std::move(sender_selector)),
762 receiver_selector_(std::move(receiver_selector)) {
763 RTC_DCHECK(callback_);
764 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
765}
766
hbosc82f2e12016-09-05 01:36:50 -0700767rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800768 PeerConnectionInternal* pc,
769 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700770 return rtc::scoped_refptr<RTCStatsCollector>(
771 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
772}
773
Steve Anton2d8609c2018-01-23 16:38:46 -0800774RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700775 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700776 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700777 signaling_thread_(pc->signaling_thread()),
778 worker_thread_(pc->worker_thread()),
779 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700780 num_pending_partial_reports_(0),
781 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100782 network_report_event_(true /* manual_reset */,
783 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700784 cache_timestamp_us_(0),
785 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700786 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700787 RTC_DCHECK(signaling_thread_);
788 RTC_DCHECK(worker_thread_);
789 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700790 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800791 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800792 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700793}
794
hbosb78306a2016-12-19 05:06:57 -0800795RTCStatsCollector::~RTCStatsCollector() {
796 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
797}
798
hbosc82f2e12016-09-05 01:36:50 -0700799void RTCStatsCollector::GetStatsReport(
800 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100801 GetStatsReportInternal(RequestInfo(std::move(callback)));
802}
803
804void RTCStatsCollector::GetStatsReport(
805 rtc::scoped_refptr<RtpSenderInternal> selector,
806 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
807 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
808}
809
810void RTCStatsCollector::GetStatsReport(
811 rtc::scoped_refptr<RtpReceiverInternal> selector,
812 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
813 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
814}
815
816void RTCStatsCollector::GetStatsReportInternal(
817 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700818 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100819 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700820
hbos0e6758d2016-08-31 07:57:36 -0700821 // "Now" using a monotonically increasing timer.
822 int64_t cache_now_us = rtc::TimeMicros();
823 if (cached_report_ &&
824 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800825 // We have a fresh cached report to deliver. Deliver asynchronously, since
826 // the caller may not be expecting a synchronous callback, and it avoids
827 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100828 std::vector<RequestInfo> requests;
829 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100830 signaling_thread_->PostTask(
831 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
832 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700833 } else if (!num_pending_partial_reports_) {
834 // Only start gathering stats if we're not already gathering stats. In the
835 // case of already gathering stats, |callback_| will be invoked when there
836 // are no more pending partial reports.
837
838 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
839 // UTC), in microseconds. The system clock could be modified and is not
840 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700841 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700842
hbosf415f8a2017-01-02 04:28:51 -0800843 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700844 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800845
Steve Anton57858b32018-02-15 15:19:50 -0800846 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800847 // |ProducePartialResultsOnNetworkThread| and
848 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800849 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700850 // Prepare |transport_names_| for use in
851 // |ProducePartialResultsOnNetworkThread|.
852 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800853
stefanf79ade12017-06-02 06:44:03 -0700854 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
855 // thread.
856 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
857 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700858 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700859
Henrik Boström40b030e2019-02-28 09:49:31 +0100860 // Don't touch |network_report_| on the signaling thread until
861 // ProducePartialResultsOnNetworkThread() has signaled the
862 // |network_report_event_|.
863 network_report_event_.Reset();
864 network_thread_->PostTask(
865 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700866 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100867 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800868 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700869 }
hbosd565b732016-08-30 14:04:35 -0700870}
871
872void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700873 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700874 cached_report_ = nullptr;
875}
876
hbosb78306a2016-12-19 05:06:57 -0800877void RTCStatsCollector::WaitForPendingRequest() {
878 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100879 // If a request is pending, blocks until the |network_report_event_| is
880 // signaled and then delivers the result. Otherwise this is a NO-OP.
881 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800882}
883
hbosc82f2e12016-09-05 01:36:50 -0700884void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
885 int64_t timestamp_us) {
886 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100887 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700888
Henrik Boström40b030e2019-02-28 09:49:31 +0100889 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
890 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700891
Henrik Boström40b030e2019-02-28 09:49:31 +0100892 // ProducePartialResultsOnSignalingThread() is running synchronously on the
893 // signaling thread, so it is always the first partial result delivered on the
894 // signaling thread. The request is not complete until MergeNetworkReport_s()
895 // happens; we don't have to do anything here.
896 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
897 --num_pending_partial_reports_;
898}
899
900void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
901 int64_t timestamp_us,
902 RTCStatsReport* partial_report) {
903 RTC_DCHECK(signaling_thread_->IsCurrent());
904 ProduceDataChannelStats_s(timestamp_us, partial_report);
905 ProduceMediaStreamStats_s(timestamp_us, partial_report);
906 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
907 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700908}
909
hbosc82f2e12016-09-05 01:36:50 -0700910void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
911 int64_t timestamp_us) {
912 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100913 // Touching |network_report_| on this thread is safe by this method because
914 // |network_report_event_| is reset before this method is invoked.
915 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700916
Steve Anton5dfde182018-02-06 10:34:40 -0800917 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700918 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800919 std::map<std::string, CertificateStatsPair> transport_cert_stats =
920 PrepareTransportCertificateStats_n(transport_stats_by_name);
921
Henrik Boström40b030e2019-02-28 09:49:31 +0100922 ProducePartialResultsOnNetworkThreadImpl(
923 timestamp_us, transport_stats_by_name, transport_cert_stats,
924 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000925
Henrik Boström40b030e2019-02-28 09:49:31 +0100926 // Signal that it is now safe to touch |network_report_| on the signaling
927 // thread, and post a task to merge it into the final results.
928 network_report_event_.Set();
929 signaling_thread_->PostTask(
930 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100931}
932
Henrik Boström40b030e2019-02-28 09:49:31 +0100933void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
934 int64_t timestamp_us,
935 const std::map<std::string, cricket::TransportStats>&
936 transport_stats_by_name,
937 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
938 RTCStatsReport* partial_report) {
939 RTC_DCHECK(network_thread_->IsCurrent());
940 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
941 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
942 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
943 call_stats_, partial_report);
944 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
945 partial_report);
946 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
947 transport_cert_stats, partial_report);
948}
949
950void RTCStatsCollector::MergeNetworkReport_s() {
951 RTC_DCHECK(signaling_thread_->IsCurrent());
952 // The |network_report_event_| must be signaled for it to be safe to touch
953 // |network_report_|. This is normally not blocking, but if
954 // WaitForPendingRequest() is called while a request is pending, we might have
955 // to wait until the network thread is done touching |network_report_|.
956 network_report_event_.Wait(rtc::Event::kForever);
957 if (!network_report_) {
958 // Normally, MergeNetworkReport_s() is executed because it is posted from
959 // the network thread. But if WaitForPendingRequest() is called while a
960 // request is pending, an early call to MergeNetworkReport_s() is made,
961 // merging the report and setting |network_report_| to null. If so, when the
962 // previously posted MergeNetworkReport_s() is later executed, the report is
963 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700964 return;
965 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000966 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100967 RTC_DCHECK(partial_report_);
968 partial_report_->TakeMembersFrom(network_report_);
969 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000970 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100971 // |network_report_| is currently the only partial report collected
972 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
973 // ready to deliver the result.
974 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
975 cache_timestamp_us_ = partial_report_timestamp_us_;
976 cached_report_ = partial_report_;
977 partial_report_ = nullptr;
978 transceiver_stats_infos_.clear();
979 // Trace WebRTC Stats when getStats is called on Javascript.
980 // This allows access to WebRTC stats from trace logs. To enable them,
981 // select the "webrtc_stats" category when recording traces.
982 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
983 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000984
Henrik Boström40b030e2019-02-28 09:49:31 +0100985 // Deliver report and clear |requests_|.
986 std::vector<RequestInfo> requests;
987 requests.swap(requests_);
988 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700989}
990
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800991void RTCStatsCollector::DeliverCachedReport(
992 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100993 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700994 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100995 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800996 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800997
Henrik Boström5b3541f2018-03-19 13:52:56 +0100998 for (const RequestInfo& request : requests) {
999 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1000 request.callback()->OnStatsDelivered(cached_report);
1001 } else {
1002 bool filter_by_sender_selector;
1003 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1004 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1005 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1006 filter_by_sender_selector = true;
1007 sender_selector = request.sender_selector();
1008 } else {
1009 RTC_DCHECK(request.filter_mode() ==
1010 RequestInfo::FilterMode::kReceiverSelector);
1011 filter_by_sender_selector = false;
1012 receiver_selector = request.receiver_selector();
1013 }
1014 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1015 filter_by_sender_selector, cached_report, sender_selector,
1016 receiver_selector));
1017 }
hbosc82f2e12016-09-05 01:36:50 -07001018 }
hbosd565b732016-08-30 14:04:35 -07001019}
1020
hbosdf6075a2016-12-19 04:58:02 -08001021void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001022 int64_t timestamp_us,
1023 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001024 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001025 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001026 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1027 if (transport_cert_stats_pair.second.local) {
1028 ProduceCertificateStatsFromSSLCertificateStats(
1029 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001030 }
hbos02ba2112016-10-28 05:14:53 -07001031 if (transport_cert_stats_pair.second.remote) {
1032 ProduceCertificateStatsFromSSLCertificateStats(
1033 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001034 }
1035 }
1036}
1037
hbosdf6075a2016-12-19 04:58:02 -08001038void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001039 int64_t timestamp_us,
1040 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001041 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001042 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001043 for (const auto& stats : transceiver_stats_infos) {
1044 if (!stats.mid) {
1045 continue;
hbos0adb8282016-11-23 02:32:06 -08001046 }
Steve Anton57858b32018-02-15 15:19:50 -08001047 const cricket::VoiceMediaInfo* voice_media_info =
1048 stats.track_media_info_map->voice_media_info();
1049 const cricket::VideoMediaInfo* video_media_info =
1050 stats.track_media_info_map->video_media_info();
1051 // Audio
1052 if (voice_media_info) {
1053 // Inbound
1054 for (const auto& pair : voice_media_info->receive_codecs) {
1055 report->AddStats(CodecStatsFromRtpCodecParameters(
1056 timestamp_us, *stats.mid, true, pair.second));
1057 }
1058 // Outbound
1059 for (const auto& pair : voice_media_info->send_codecs) {
1060 report->AddStats(CodecStatsFromRtpCodecParameters(
1061 timestamp_us, *stats.mid, false, pair.second));
1062 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001063 }
Steve Anton57858b32018-02-15 15:19:50 -08001064 // Video
1065 if (video_media_info) {
1066 // Inbound
1067 for (const auto& pair : video_media_info->receive_codecs) {
1068 report->AddStats(CodecStatsFromRtpCodecParameters(
1069 timestamp_us, *stats.mid, true, pair.second));
1070 }
1071 // Outbound
1072 for (const auto& pair : video_media_info->send_codecs) {
1073 report->AddStats(CodecStatsFromRtpCodecParameters(
1074 timestamp_us, *stats.mid, false, pair.second));
1075 }
hbos0adb8282016-11-23 02:32:06 -08001076 }
1077 }
1078}
1079
hboscc555c52016-10-18 12:48:31 -07001080void RTCStatsCollector::ProduceDataChannelStats_s(
1081 int64_t timestamp_us, RTCStatsReport* report) const {
1082 RTC_DCHECK(signaling_thread_->IsCurrent());
1083 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1084 pc_->sctp_data_channels()) {
1085 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1086 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001087 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001088 timestamp_us));
1089 data_channel_stats->label = data_channel->label();
1090 data_channel_stats->protocol = data_channel->protocol();
1091 data_channel_stats->datachannelid = data_channel->id();
1092 data_channel_stats->state =
1093 DataStateToRTCDataChannelState(data_channel->state());
1094 data_channel_stats->messages_sent = data_channel->messages_sent();
1095 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1096 data_channel_stats->messages_received = data_channel->messages_received();
1097 data_channel_stats->bytes_received = data_channel->bytes_received();
1098 report->AddStats(std::move(data_channel_stats));
1099 }
1100}
1101
hbosdf6075a2016-12-19 04:58:02 -08001102void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001103 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001104 const std::map<std::string, cricket::TransportStats>&
1105 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001106 const Call::Stats& call_stats,
1107 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001108 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001109 for (const auto& entry : transport_stats_by_name) {
1110 const std::string& transport_name = entry.first;
1111 const cricket::TransportStats& transport_stats = entry.second;
1112 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001113 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001114 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001115 for (const cricket::ConnectionInfo& info :
1116 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001117 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001118 new RTCIceCandidatePairStats(
1119 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1120 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001121
hbos0583b282016-11-30 01:50:14 -08001122 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001123 // TODO(hbos): There could be other candidates that are not paired with
1124 // anything. We don't have a complete list. Local candidates come from
1125 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001126 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001127 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001128 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001129 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001130 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001131 candidate_pair_stats->state =
1132 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1133 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001134 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001135 // TODO(hbos): This writable is different than the spec. It goes to
1136 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001137 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001138 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001139 candidate_pair_stats->bytes_sent =
1140 static_cast<uint64_t>(info.sent_total_bytes);
1141 candidate_pair_stats->bytes_received =
1142 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001143 candidate_pair_stats->total_round_trip_time =
1144 static_cast<double>(info.total_round_trip_time_ms) /
1145 rtc::kNumMillisecsPerSec;
1146 if (info.current_round_trip_time_ms) {
1147 candidate_pair_stats->current_round_trip_time =
1148 static_cast<double>(*info.current_round_trip_time_ms) /
1149 rtc::kNumMillisecsPerSec;
1150 }
stefanf79ade12017-06-02 06:44:03 -07001151 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001152 // The bandwidth estimations we have are for the selected candidate
1153 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001154 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1155 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1156 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001157 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001158 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001159 }
stefanf79ade12017-06-02 06:44:03 -07001160 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001161 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001162 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001163 }
1164 }
hbosd82f5122016-12-09 04:12:39 -08001165 candidate_pair_stats->requests_received =
1166 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001167 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1168 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001169 candidate_pair_stats->responses_received =
1170 static_cast<uint64_t>(info.recv_ping_responses);
1171 candidate_pair_stats->responses_sent =
1172 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001173 RTC_DCHECK_GE(info.sent_ping_requests_total,
1174 info.sent_ping_requests_before_first_response);
1175 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1176 info.sent_ping_requests_total -
1177 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001178
1179 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001180 }
1181 }
1182 }
1183}
1184
Steve Anton57858b32018-02-15 15:19:50 -08001185void RTCStatsCollector::ProduceMediaStreamStats_s(
1186 int64_t timestamp_us,
1187 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001188 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001189
1190 std::map<std::string, std::vector<std::string>> track_ids;
1191
1192 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001193 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001194 std::string track_id =
1195 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1196 kSender, sender->internal()->AttachmentId());
1197 for (auto& stream_id : sender->stream_ids()) {
1198 track_ids[stream_id].push_back(track_id);
1199 }
1200 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001201 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001202 std::string track_id =
1203 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1204 kReceiver, receiver->internal()->AttachmentId());
1205 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001206 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001207 }
1208 }
1209 }
1210
1211 // Build stats for each stream ID known.
1212 for (auto& it : track_ids) {
1213 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1214 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1215 stream_stats->stream_identifier = it.first;
1216 stream_stats->track_ids = it.second;
1217 report->AddStats(std::move(stream_stats));
1218 }
1219}
1220
1221void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1222 int64_t timestamp_us,
1223 RTCStatsReport* report) const {
1224 RTC_DCHECK(signaling_thread_->IsCurrent());
1225 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1226 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001227 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001228 senders.push_back(sender->internal());
1229 }
1230 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1231 senders, report);
1232
1233 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001234 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001235 receivers.push_back(receiver->internal());
1236 }
1237 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1238 receivers, report);
1239 }
hbos09bc1282016-11-08 06:29:22 -08001240}
1241
hbos6ab97ce2016-10-03 14:16:56 -07001242void RTCStatsCollector::ProducePeerConnectionStats_s(
1243 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001244 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001245 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001246 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001247 stats->data_channels_opened = internal_record_.data_channels_opened;
1248 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001249 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001250}
1251
hbosdf6075a2016-12-19 04:58:02 -08001252void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001253 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001254 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001255 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001256 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001257
Steve Anton57858b32018-02-15 15:19:50 -08001258 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1259 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1260 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1261 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1262 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1263 } else {
1264 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001265 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001266 }
Steve Anton57858b32018-02-15 15:19:50 -08001267}
1268
1269void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1270 int64_t timestamp_us,
1271 const RtpTransceiverStatsInfo& stats,
1272 RTCStatsReport* report) const {
1273 if (!stats.mid || !stats.transport_name) {
1274 return;
1275 }
1276 RTC_DCHECK(stats.track_media_info_map);
1277 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1278 RTC_DCHECK(track_media_info_map.voice_media_info());
1279 std::string mid = *stats.mid;
1280 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1281 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1282 // Inbound
1283 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1284 track_media_info_map.voice_media_info()->receivers) {
1285 if (!voice_receiver_info.connected())
1286 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001287 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001288 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1289 timestamp_us);
1290 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1291 inbound_audio.get());
1292 // TODO(hta): This lookup should look for the sender, not the track.
1293 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1294 track_media_info_map.GetAudioTrack(voice_receiver_info);
1295 if (audio_track) {
1296 inbound_audio->track_id =
1297 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1298 kReceiver,
1299 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001300 }
Steve Anton57858b32018-02-15 15:19:50 -08001301 inbound_audio->transport_id = transport_id;
1302 report->AddStats(std::move(inbound_audio));
1303 }
1304 // Outbound
1305 for (const cricket::VoiceSenderInfo& voice_sender_info :
1306 track_media_info_map.voice_media_info()->senders) {
1307 if (!voice_sender_info.connected())
1308 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001309 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001310 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1311 timestamp_us);
1312 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1313 outbound_audio.get());
1314 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1315 track_media_info_map.GetAudioTrack(voice_sender_info);
1316 if (audio_track) {
1317 outbound_audio->track_id =
1318 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1319 kSender,
1320 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001321 }
Steve Anton57858b32018-02-15 15:19:50 -08001322 outbound_audio->transport_id = transport_id;
1323 report->AddStats(std::move(outbound_audio));
1324 }
1325}
1326
1327void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1328 int64_t timestamp_us,
1329 const RtpTransceiverStatsInfo& stats,
1330 RTCStatsReport* report) const {
1331 if (!stats.mid || !stats.transport_name) {
1332 return;
1333 }
1334 RTC_DCHECK(stats.track_media_info_map);
1335 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1336 RTC_DCHECK(track_media_info_map.video_media_info());
1337 std::string mid = *stats.mid;
1338 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1339 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1340 // Inbound
1341 for (const cricket::VideoReceiverInfo& video_receiver_info :
1342 track_media_info_map.video_media_info()->receivers) {
1343 if (!video_receiver_info.connected())
1344 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001345 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001346 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1347 timestamp_us);
1348 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1349 inbound_video.get());
1350 rtc::scoped_refptr<VideoTrackInterface> video_track =
1351 track_media_info_map.GetVideoTrack(video_receiver_info);
1352 if (video_track) {
1353 inbound_video->track_id =
1354 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1355 kReceiver,
1356 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1357 }
1358 inbound_video->transport_id = transport_id;
1359 report->AddStats(std::move(inbound_video));
1360 }
1361 // Outbound
1362 for (const cricket::VideoSenderInfo& video_sender_info :
1363 track_media_info_map.video_media_info()->senders) {
1364 if (!video_sender_info.connected())
1365 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001366 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001367 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1368 timestamp_us);
1369 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1370 outbound_video.get());
1371 rtc::scoped_refptr<VideoTrackInterface> video_track =
1372 track_media_info_map.GetVideoTrack(video_sender_info);
1373 if (video_track) {
1374 outbound_video->track_id =
1375 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1376 kSender,
1377 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1378 }
1379 outbound_video->transport_id = transport_id;
1380 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001381 }
1382}
1383
hbosdf6075a2016-12-19 04:58:02 -08001384void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001385 int64_t timestamp_us,
1386 const std::map<std::string, cricket::TransportStats>&
1387 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001388 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1389 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001390 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001391 for (const auto& entry : transport_stats_by_name) {
1392 const std::string& transport_name = entry.first;
1393 const cricket::TransportStats& transport_stats = entry.second;
1394
hbos2fa7c672016-10-24 04:00:05 -07001395 // Get reference to RTCP channel, if it exists.
1396 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001397 for (const cricket::TransportChannelStats& channel_stats :
1398 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001399 if (channel_stats.component ==
1400 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1401 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001402 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001403 break;
1404 }
1405 }
1406
1407 // Get reference to local and remote certificates of this transport, if they
1408 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001409 const auto& certificate_stats_it =
1410 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001411 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1412 std::string local_certificate_id;
1413 if (certificate_stats_it->second.local) {
1414 local_certificate_id = RTCCertificateIDFromFingerprint(
1415 certificate_stats_it->second.local->fingerprint);
1416 }
1417 std::string remote_certificate_id;
1418 if (certificate_stats_it->second.remote) {
1419 remote_certificate_id = RTCCertificateIDFromFingerprint(
1420 certificate_stats_it->second.remote->fingerprint);
1421 }
1422
1423 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001424 for (const cricket::TransportChannelStats& channel_stats :
1425 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001426 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001427 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1428 transport_name, channel_stats.component),
1429 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001430 transport_stats->bytes_sent = 0;
1431 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001432 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1433 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001434 for (const cricket::ConnectionInfo& info :
1435 channel_stats.connection_infos) {
1436 *transport_stats->bytes_sent += info.sent_total_bytes;
1437 *transport_stats->bytes_received += info.recv_total_bytes;
1438 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001439 transport_stats->selected_candidate_pair_id =
1440 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1441 }
1442 }
1443 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1444 !rtcp_transport_stats_id.empty()) {
1445 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1446 }
1447 if (!local_certificate_id.empty())
1448 transport_stats->local_certificate_id = local_certificate_id;
1449 if (!remote_certificate_id.empty())
1450 transport_stats->remote_certificate_id = remote_certificate_id;
1451 report->AddStats(std::move(transport_stats));
1452 }
1453 }
1454}
1455
1456std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001457RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001458 const std::map<std::string, cricket::TransportStats>&
1459 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001460 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001461 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001462 for (const auto& entry : transport_stats_by_name) {
1463 const std::string& transport_name = entry.first;
1464
hbos2fa7c672016-10-24 04:00:05 -07001465 CertificateStatsPair certificate_stats_pair;
1466 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001467 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001468 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001469 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001470 }
Steve Anton5dfde182018-02-06 10:34:40 -08001471
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001472 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1473 pc_->GetRemoteSSLCertChain(transport_name);
1474 if (remote_cert_chain) {
1475 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001476 }
Steve Anton5dfde182018-02-06 10:34:40 -08001477
hbos2fa7c672016-10-24 04:00:05 -07001478 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001479 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001480 }
1481 return transport_cert_stats;
1482}
1483
Steve Anton57858b32018-02-15 15:19:50 -08001484std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1485RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1486 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001487
Steve Anton57858b32018-02-15 15:19:50 -08001488 // These are used to invoke GetStats for all the media channels together in
1489 // one worker thread hop.
1490 std::map<cricket::VoiceMediaChannel*,
1491 std::unique_ptr<cricket::VoiceMediaInfo>>
1492 voice_stats;
1493 std::map<cricket::VideoMediaChannel*,
1494 std::unique_ptr<cricket::VideoMediaInfo>>
1495 video_stats;
1496
Mirko Bonadei739baf02019-01-27 17:29:42 +01001497 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001498 cricket::MediaType media_type = transceiver->media_type();
1499
1500 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1501 // stats have been fetched on the worker thread.
1502 transceiver_stats_infos.emplace_back();
1503 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1504 stats.transceiver = transceiver->internal();
1505 stats.media_type = media_type;
1506
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001507 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001508 if (!channel) {
1509 // The remaining fields require a BaseChannel.
1510 continue;
1511 }
1512
1513 stats.mid = channel->content_name();
1514 stats.transport_name = channel->transport_name();
1515
1516 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1517 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1518 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1519 voice_stats.end());
1520 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001521 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001522 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1523 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1524 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1525 video_stats.end());
1526 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001527 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001528 } else {
1529 RTC_NOTREACHED();
1530 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001531 }
Steve Anton57858b32018-02-15 15:19:50 -08001532
1533 // Call GetStats for all media channels together on the worker thread in one
1534 // hop.
1535 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1536 for (const auto& entry : voice_stats) {
1537 if (!entry.first->GetStats(entry.second.get())) {
1538 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1539 }
1540 }
1541 for (const auto& entry : video_stats) {
1542 if (!entry.first->GetStats(entry.second.get())) {
1543 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1544 }
1545 }
1546 });
1547
1548 // Create the TrackMediaInfoMap for each transceiver stats object.
1549 for (auto& stats : transceiver_stats_infos) {
1550 auto transceiver = stats.transceiver;
1551 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1552 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1553 if (transceiver->channel()) {
1554 cricket::MediaType media_type = transceiver->media_type();
1555 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1556 auto* voice_channel =
1557 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1558 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1559 voice_media_info =
1560 std::move(voice_stats[voice_channel->media_channel()]);
1561 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1562 auto* video_channel =
1563 static_cast<cricket::VideoChannel*>(transceiver->channel());
1564 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1565 video_media_info =
1566 std::move(video_stats[video_channel->media_channel()]);
1567 }
1568 }
1569 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001570 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001571 senders.push_back(sender->internal());
1572 }
1573 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001574 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001575 receivers.push_back(receiver->internal());
1576 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001577 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001578 std::move(voice_media_info), std::move(video_media_info), senders,
1579 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001580 }
Steve Anton57858b32018-02-15 15:19:50 -08001581
1582 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001583}
1584
Steve Anton7eca0932018-03-30 15:18:41 -07001585std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1586 std::set<std::string> transport_names;
1587 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1588 if (transceiver->internal()->channel()) {
1589 transport_names.insert(
1590 transceiver->internal()->channel()->transport_name());
1591 }
1592 }
1593 if (pc_->rtp_data_channel()) {
1594 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1595 }
1596 if (pc_->sctp_transport_name()) {
1597 transport_names.insert(*pc_->sctp_transport_name());
1598 }
1599 return transport_names;
1600}
1601
hbos82ebe022016-11-14 01:41:09 -08001602void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1603 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1604 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1605}
1606
1607void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1608 RTC_DCHECK(signaling_thread_->IsCurrent());
1609 bool result = internal_record_.opened_data_channels.insert(
1610 reinterpret_cast<uintptr_t>(channel)).second;
1611 ++internal_record_.data_channels_opened;
1612 RTC_DCHECK(result);
1613}
1614
1615void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1616 RTC_DCHECK(signaling_thread_->IsCurrent());
1617 // Only channels that have been fully opened (and have increased the
1618 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001619 if (internal_record_.opened_data_channels.erase(
1620 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001621 ++internal_record_.data_channels_closed;
1622 }
1623}
1624
hboscc555c52016-10-18 12:48:31 -07001625const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1626 const std::string& type) {
1627 return CandidateTypeToRTCIceCandidateType(type);
1628}
1629
1630const char* DataStateToRTCDataChannelStateForTesting(
1631 DataChannelInterface::DataState state) {
1632 return DataStateToRTCDataChannelState(state);
1633}
1634
hbosd565b732016-08-30 14:04:35 -07001635} // namespace webrtc