blob: 74273562a7f9c0b5ef81a16495a533cd388158dc [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.
hboseeafe942016-11-01 03:00:17 -0700247}
248
249void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800250 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700251 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800252 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700253 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800254 video_receiver_info, inbound_video);
255 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200256 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800257 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800258 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
259 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800260 }
hbos820f5782016-11-22 03:16:50 -0800261 inbound_video->fir_count =
262 static_cast<uint32_t>(video_receiver_info.firs_sent);
263 inbound_video->pli_count =
264 static_cast<uint32_t>(video_receiver_info.plis_sent);
265 inbound_video->nack_count =
266 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800267 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800268 if (video_receiver_info.qp_sum)
269 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Henrik Boström2e069262019-04-09 13:59:31 +0200270 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
271 // optional, support the "unspecified" value.
272 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
273 inbound_video->content_type = RTCContentType::kScreenshare;
hboseeafe942016-11-01 03:00:17 -0700274}
275
hbos820f5782016-11-22 03:16:50 -0800276// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700277void SetOutboundRTPStreamStatsFromMediaSenderInfo(
278 const cricket::MediaSenderInfo& media_sender_info,
279 RTCOutboundRTPStreamStats* outbound_stats) {
280 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800281 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100282 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700283 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700284 outbound_stats->packets_sent =
285 static_cast<uint32_t>(media_sender_info.packets_sent);
286 outbound_stats->bytes_sent =
287 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700288}
289
290void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800291 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700292 const cricket::VoiceSenderInfo& voice_sender_info,
293 RTCOutboundRTPStreamStats* outbound_audio) {
294 SetOutboundRTPStreamStatsFromMediaSenderInfo(
295 voice_sender_info, outbound_audio);
296 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200297 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800298 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800299 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
300 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800301 }
hbos6ded1902016-11-01 01:50:46 -0700302 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
303 // purposefully left undefined for audio.
304}
305
306void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800307 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700308 const cricket::VideoSenderInfo& video_sender_info,
309 RTCOutboundRTPStreamStats* outbound_video) {
310 SetOutboundRTPStreamStatsFromMediaSenderInfo(
311 video_sender_info, outbound_video);
312 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200313 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800314 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800315 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
316 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800317 }
hbos6ded1902016-11-01 01:50:46 -0700318 outbound_video->fir_count =
319 static_cast<uint32_t>(video_sender_info.firs_rcvd);
320 outbound_video->pli_count =
321 static_cast<uint32_t>(video_sender_info.plis_rcvd);
322 outbound_video->nack_count =
323 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800324 if (video_sender_info.qp_sum)
325 outbound_video->qp_sum = *video_sender_info.qp_sum;
326 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200327 outbound_video->total_encode_time =
328 static_cast<double>(video_sender_info.total_encode_time_ms) /
329 rtc::kNumMillisecsPerSec;
Henrik Boström2e069262019-04-09 13:59:31 +0200330 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
331 // optional, support the "unspecified" value.
332 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
333 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700334}
335
hbos02ba2112016-10-28 05:14:53 -0700336void ProduceCertificateStatsFromSSLCertificateStats(
337 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
338 RTCStatsReport* report) {
339 RTCCertificateStats* prev_certificate_stats = nullptr;
340 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
341 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800342 std::string certificate_stats_id =
343 RTCCertificateIDFromFingerprint(s->fingerprint);
344 // It is possible for the same certificate to show up multiple times, e.g.
345 // if local and remote side use the same certificate in a loopback call.
346 // If the report already contains stats for this certificate, skip it.
347 if (report->Get(certificate_stats_id)) {
348 RTC_DCHECK_EQ(s, &certificate_stats);
349 break;
350 }
hbos02ba2112016-10-28 05:14:53 -0700351 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800352 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700353 certificate_stats->fingerprint = s->fingerprint;
354 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
355 certificate_stats->base64_certificate = s->base64_certificate;
356 if (prev_certificate_stats)
357 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
358 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
359 prev_certificate_stats = certificate_stats;
360 }
361}
362
363const std::string& ProduceIceCandidateStats(
364 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800365 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700366 const std::string& id = "RTCIceCandidate_" + candidate.id();
367 const RTCStats* stats = report->Get(id);
368 if (!stats) {
369 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
370 if (is_local)
371 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
372 else
373 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800374 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800375 if (is_local) {
376 candidate_stats->network_type =
377 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200378 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
379 std::string relay_protocol = candidate.relay_protocol();
380 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
381 relay_protocol.compare("tcp") == 0 ||
382 relay_protocol.compare("tls") == 0);
383 candidate_stats->relay_protocol = relay_protocol;
384 }
Gary Liu37e489c2017-11-21 10:49:36 -0800385 } else {
386 // We don't expect to know the adapter type of remote candidates.
387 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
388 }
hbos02ba2112016-10-28 05:14:53 -0700389 candidate_stats->ip = candidate.address().ipaddr().ToString();
390 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
391 candidate_stats->protocol = candidate.protocol();
392 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
393 candidate.type());
394 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
395
396 stats = candidate_stats.get();
397 report->AddStats(std::move(candidate_stats));
398 }
399 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
400 : RTCRemoteIceCandidateStats::kType);
401 return stats->id();
402}
403
hbos9e302742017-01-20 02:47:10 -0800404std::unique_ptr<RTCMediaStreamTrackStats>
405ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
406 int64_t timestamp_us,
407 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100408 const cricket::VoiceSenderInfo& voice_sender_info,
409 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800410 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
411 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100412 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
413 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100414 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800415 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
416 audio_track, audio_track_stats.get());
417 audio_track_stats->remote_source = false;
418 audio_track_stats->detached = false;
419 if (voice_sender_info.audio_level >= 0) {
420 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
421 voice_sender_info.audio_level);
422 }
zsteine76bd3a2017-07-14 12:17:49 -0700423 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
424 audio_track_stats->total_samples_duration =
425 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100426 if (voice_sender_info.apm_statistics.echo_return_loss) {
427 audio_track_stats->echo_return_loss =
428 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800429 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100430 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
431 audio_track_stats->echo_return_loss_enhancement =
432 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800433 }
434 return audio_track_stats;
435}
436
437std::unique_ptr<RTCMediaStreamTrackStats>
438ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
439 int64_t timestamp_us,
440 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100441 const cricket::VoiceReceiverInfo& voice_receiver_info,
442 int attachment_id) {
443 // Since receiver tracks can't be reattached, we use the SSRC as
444 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800445 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
446 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100447 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
448 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100449 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800450 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
451 audio_track, audio_track_stats.get());
452 audio_track_stats->remote_source = true;
453 audio_track_stats->detached = false;
454 if (voice_receiver_info.audio_level >= 0) {
455 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
456 voice_receiver_info.audio_level);
457 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200458 audio_track_stats->jitter_buffer_delay =
459 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100460 audio_track_stats->jitter_buffer_emitted_count =
461 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 12:17:49 -0700462 audio_track_stats->total_audio_energy =
463 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700464 audio_track_stats->total_samples_received =
465 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700466 audio_track_stats->total_samples_duration =
467 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700468 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200469 audio_track_stats->concealment_events =
470 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100471 audio_track_stats->jitter_buffer_flushes =
472 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100473 audio_track_stats->delayed_packet_outage_samples =
474 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100475 audio_track_stats->relative_packet_arrival_delay =
476 voice_receiver_info.relative_packet_arrival_delay_seconds;
hbos9e302742017-01-20 02:47:10 -0800477 return audio_track_stats;
478}
479
480std::unique_ptr<RTCMediaStreamTrackStats>
481ProduceMediaStreamTrackStatsFromVideoSenderInfo(
482 int64_t timestamp_us,
483 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100484 const cricket::VideoSenderInfo& video_sender_info,
485 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800486 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
487 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100488 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
489
490 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100491 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800492 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
493 video_track, video_track_stats.get());
494 video_track_stats->remote_source = false;
495 video_track_stats->detached = false;
496 video_track_stats->frame_width = static_cast<uint32_t>(
497 video_sender_info.send_frame_width);
498 video_track_stats->frame_height = static_cast<uint32_t>(
499 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800500 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100501 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800502 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100503 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800504 return video_track_stats;
505}
506
507std::unique_ptr<RTCMediaStreamTrackStats>
508ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
509 int64_t timestamp_us,
510 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100511 const cricket::VideoReceiverInfo& video_receiver_info,
512 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800513 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
514 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100515 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
516
517 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100518 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800519 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
520 video_track, video_track_stats.get());
521 video_track_stats->remote_source = true;
522 video_track_stats->detached = false;
523 if (video_receiver_info.frame_width > 0 &&
524 video_receiver_info.frame_height > 0) {
525 video_track_stats->frame_width = static_cast<uint32_t>(
526 video_receiver_info.frame_width);
527 video_track_stats->frame_height = static_cast<uint32_t>(
528 video_receiver_info.frame_height);
529 }
hbos42f6d2f2017-01-20 03:56:50 -0800530 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800531 // TODO(hbos): When we support receiving simulcast, this should be the total
532 // number of frames correctly decoded, independent of which SSRC it was
533 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100534 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800535 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800536 RTC_DCHECK_GE(video_receiver_info.frames_received,
537 video_receiver_info.frames_rendered);
538 video_track_stats->frames_dropped = video_receiver_info.frames_received -
539 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100540 video_track_stats->freeze_count = video_receiver_info.freeze_count;
541 video_track_stats->pause_count = video_receiver_info.pause_count;
542 video_track_stats->total_freezes_duration =
543 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
544 rtc::kNumMillisecsPerSec;
545 video_track_stats->total_pauses_duration =
546 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
547 rtc::kNumMillisecsPerSec;
548 video_track_stats->total_frames_duration =
549 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
550 rtc::kNumMillisecsPerSec;
551 video_track_stats->sum_squared_frame_durations =
552 video_receiver_info.sum_squared_frame_durations;
553
hbos9e302742017-01-20 02:47:10 -0800554 return video_track_stats;
555}
556
Harald Alvestrand89061872018-01-02 14:08:34 +0100557void ProduceSenderMediaTrackStats(
558 int64_t timestamp_us,
559 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800560 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100561 RTCStatsReport* report) {
562 // This function iterates over the senders to generate outgoing track stats.
563
564 // TODO(hbos): Return stats of detached tracks. We have to perform stats
565 // gathering at the time of detachment to get accurate stats and timestamps.
566 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100567 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100568 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
569 AudioTrackInterface* track =
570 static_cast<AudioTrackInterface*>(sender->track().get());
571 if (!track)
572 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100573 cricket::VoiceSenderInfo null_sender_info;
574 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
575 // TODO(hta): Checking on ssrc is not proper. There should be a way
576 // to see from a sender whether it's connected or not.
577 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800578 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100579 // When pc.close is called, sender info is discarded, so
580 // we generate zeroes instead. Bug: It should be retained.
581 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800582 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100583 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100584 if (sender_info) {
585 voice_sender_info = sender_info;
586 } else {
587 RTC_LOG(LS_INFO)
588 << "RTCStatsCollector: No voice sender info for sender with ssrc "
589 << sender->ssrc();
590 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100591 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100592 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100593 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
594 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100595 report->AddStats(std::move(audio_track_stats));
596 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
597 VideoTrackInterface* track =
598 static_cast<VideoTrackInterface*>(sender->track().get());
599 if (!track)
600 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100601 cricket::VideoSenderInfo null_sender_info;
602 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
603 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100604 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
605 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800606 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100607 // When pc.close is called, sender info is discarded, so
608 // we generate zeroes instead. Bug: It should be retained.
609 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800610 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100611 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100612 if (sender_info) {
613 video_sender_info = sender_info;
614 } else {
615 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
616 << sender->ssrc();
617 }
618 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100619 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100620 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
621 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100622 report->AddStats(std::move(video_track_stats));
623 } else {
624 RTC_NOTREACHED();
625 }
626 }
627}
628
629void ProduceReceiverMediaTrackStats(
630 int64_t timestamp_us,
631 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800632 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100633 RTCStatsReport* report) {
634 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100635 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100636 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
637 AudioTrackInterface* track =
638 static_cast<AudioTrackInterface*>(receiver->track().get());
639 const cricket::VoiceReceiverInfo* voice_receiver_info =
640 track_media_info_map.GetVoiceReceiverInfo(*track);
641 if (!voice_receiver_info) {
642 continue;
643 }
644 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
645 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100646 timestamp_us, *track, *voice_receiver_info,
647 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100648 report->AddStats(std::move(audio_track_stats));
649 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
650 VideoTrackInterface* track =
651 static_cast<VideoTrackInterface*>(receiver->track().get());
652 const cricket::VideoReceiverInfo* video_receiver_info =
653 track_media_info_map.GetVideoReceiverInfo(*track);
654 if (!video_receiver_info) {
655 continue;
656 }
657 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
658 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100659 timestamp_us, *track, *video_receiver_info,
660 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100661 report->AddStats(std::move(video_track_stats));
662 } else {
663 RTC_NOTREACHED();
664 }
665 }
666}
667
Henrik Boström5b3541f2018-03-19 13:52:56 +0100668rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
669 bool filter_by_sender_selector,
670 rtc::scoped_refptr<const RTCStatsReport> report,
671 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
672 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
673 std::vector<std::string> rtpstream_ids;
674 if (filter_by_sender_selector) {
675 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
676 if (sender_selector) {
677 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
678 // reference the sender stats.
679 // Because we do not implement sender stats, we look at outbound-rtp(s)
680 // that reference the track attachment stats for the sender instead.
681 std::string track_id =
682 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
683 kSender, sender_selector->AttachmentId());
684 for (const auto& stats : *report) {
685 if (stats.type() != RTCOutboundRTPStreamStats::kType)
686 continue;
687 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
688 if (outbound_rtp.track_id.is_defined() &&
689 *outbound_rtp.track_id == track_id) {
690 rtpstream_ids.push_back(outbound_rtp.id());
691 }
692 }
693 }
694 } else {
695 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
696 if (receiver_selector) {
697 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
698 // reference the receiver stats.
699 // Because we do not implement receiver stats, we look at inbound-rtp(s)
700 // that reference the track attachment stats for the receiver instead.
701 std::string track_id =
702 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
703 kReceiver, receiver_selector->AttachmentId());
704 for (const auto& stats : *report) {
705 if (stats.type() != RTCInboundRTPStreamStats::kType)
706 continue;
707 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
708 if (inbound_rtp.track_id.is_defined() &&
709 *inbound_rtp.track_id == track_id) {
710 rtpstream_ids.push_back(inbound_rtp.id());
711 }
712 }
713 }
714 }
715 if (rtpstream_ids.empty())
716 return RTCStatsReport::Create(report->timestamp_us());
717 return TakeReferencedStats(report->Copy(), rtpstream_ids);
718}
719
hboscc555c52016-10-18 12:48:31 -0700720} // namespace
721
Henrik Boström5b3541f2018-03-19 13:52:56 +0100722RTCStatsCollector::RequestInfo::RequestInfo(
723 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
724 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
725
726RTCStatsCollector::RequestInfo::RequestInfo(
727 rtc::scoped_refptr<RtpSenderInternal> selector,
728 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
729 : RequestInfo(FilterMode::kSenderSelector,
730 std::move(callback),
731 std::move(selector),
732 nullptr) {}
733
734RTCStatsCollector::RequestInfo::RequestInfo(
735 rtc::scoped_refptr<RtpReceiverInternal> selector,
736 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
737 : RequestInfo(FilterMode::kReceiverSelector,
738 std::move(callback),
739 nullptr,
740 std::move(selector)) {}
741
742RTCStatsCollector::RequestInfo::RequestInfo(
743 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
744 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
745 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
746 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
747 : filter_mode_(filter_mode),
748 callback_(std::move(callback)),
749 sender_selector_(std::move(sender_selector)),
750 receiver_selector_(std::move(receiver_selector)) {
751 RTC_DCHECK(callback_);
752 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
753}
754
hbosc82f2e12016-09-05 01:36:50 -0700755rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800756 PeerConnectionInternal* pc,
757 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700758 return rtc::scoped_refptr<RTCStatsCollector>(
759 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
760}
761
Steve Anton2d8609c2018-01-23 16:38:46 -0800762RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700763 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700764 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700765 signaling_thread_(pc->signaling_thread()),
766 worker_thread_(pc->worker_thread()),
767 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700768 num_pending_partial_reports_(0),
769 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100770 network_report_event_(true /* manual_reset */,
771 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700772 cache_timestamp_us_(0),
773 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700774 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700775 RTC_DCHECK(signaling_thread_);
776 RTC_DCHECK(worker_thread_);
777 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700778 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800779 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800780 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700781}
782
hbosb78306a2016-12-19 05:06:57 -0800783RTCStatsCollector::~RTCStatsCollector() {
784 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
785}
786
hbosc82f2e12016-09-05 01:36:50 -0700787void RTCStatsCollector::GetStatsReport(
788 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100789 GetStatsReportInternal(RequestInfo(std::move(callback)));
790}
791
792void RTCStatsCollector::GetStatsReport(
793 rtc::scoped_refptr<RtpSenderInternal> selector,
794 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
795 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
796}
797
798void RTCStatsCollector::GetStatsReport(
799 rtc::scoped_refptr<RtpReceiverInternal> selector,
800 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
801 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
802}
803
804void RTCStatsCollector::GetStatsReportInternal(
805 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700806 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100807 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700808
hbos0e6758d2016-08-31 07:57:36 -0700809 // "Now" using a monotonically increasing timer.
810 int64_t cache_now_us = rtc::TimeMicros();
811 if (cached_report_ &&
812 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800813 // We have a fresh cached report to deliver. Deliver asynchronously, since
814 // the caller may not be expecting a synchronous callback, and it avoids
815 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100816 std::vector<RequestInfo> requests;
817 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100818 signaling_thread_->PostTask(
819 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
820 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700821 } else if (!num_pending_partial_reports_) {
822 // Only start gathering stats if we're not already gathering stats. In the
823 // case of already gathering stats, |callback_| will be invoked when there
824 // are no more pending partial reports.
825
826 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
827 // UTC), in microseconds. The system clock could be modified and is not
828 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700829 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700830
hbosf415f8a2017-01-02 04:28:51 -0800831 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700832 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800833
Steve Anton57858b32018-02-15 15:19:50 -0800834 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800835 // |ProducePartialResultsOnNetworkThread| and
836 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800837 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700838 // Prepare |transport_names_| for use in
839 // |ProducePartialResultsOnNetworkThread|.
840 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800841
stefanf79ade12017-06-02 06:44:03 -0700842 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
843 // thread.
844 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
845 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700846 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700847
Henrik Boström40b030e2019-02-28 09:49:31 +0100848 // Don't touch |network_report_| on the signaling thread until
849 // ProducePartialResultsOnNetworkThread() has signaled the
850 // |network_report_event_|.
851 network_report_event_.Reset();
852 network_thread_->PostTask(
853 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700854 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100855 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800856 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700857 }
hbosd565b732016-08-30 14:04:35 -0700858}
859
860void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700861 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700862 cached_report_ = nullptr;
863}
864
hbosb78306a2016-12-19 05:06:57 -0800865void RTCStatsCollector::WaitForPendingRequest() {
866 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100867 // If a request is pending, blocks until the |network_report_event_| is
868 // signaled and then delivers the result. Otherwise this is a NO-OP.
869 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800870}
871
hbosc82f2e12016-09-05 01:36:50 -0700872void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
873 int64_t timestamp_us) {
874 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100875 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700876
Henrik Boström40b030e2019-02-28 09:49:31 +0100877 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
878 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700879
Henrik Boström40b030e2019-02-28 09:49:31 +0100880 // ProducePartialResultsOnSignalingThread() is running synchronously on the
881 // signaling thread, so it is always the first partial result delivered on the
882 // signaling thread. The request is not complete until MergeNetworkReport_s()
883 // happens; we don't have to do anything here.
884 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
885 --num_pending_partial_reports_;
886}
887
888void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
889 int64_t timestamp_us,
890 RTCStatsReport* partial_report) {
891 RTC_DCHECK(signaling_thread_->IsCurrent());
892 ProduceDataChannelStats_s(timestamp_us, partial_report);
893 ProduceMediaStreamStats_s(timestamp_us, partial_report);
894 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
895 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700896}
897
hbosc82f2e12016-09-05 01:36:50 -0700898void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
899 int64_t timestamp_us) {
900 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100901 // Touching |network_report_| on this thread is safe by this method because
902 // |network_report_event_| is reset before this method is invoked.
903 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700904
Steve Anton5dfde182018-02-06 10:34:40 -0800905 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700906 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800907 std::map<std::string, CertificateStatsPair> transport_cert_stats =
908 PrepareTransportCertificateStats_n(transport_stats_by_name);
909
Henrik Boström40b030e2019-02-28 09:49:31 +0100910 ProducePartialResultsOnNetworkThreadImpl(
911 timestamp_us, transport_stats_by_name, transport_cert_stats,
912 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000913
Henrik Boström40b030e2019-02-28 09:49:31 +0100914 // Signal that it is now safe to touch |network_report_| on the signaling
915 // thread, and post a task to merge it into the final results.
916 network_report_event_.Set();
917 signaling_thread_->PostTask(
918 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100919}
920
Henrik Boström40b030e2019-02-28 09:49:31 +0100921void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
922 int64_t timestamp_us,
923 const std::map<std::string, cricket::TransportStats>&
924 transport_stats_by_name,
925 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
926 RTCStatsReport* partial_report) {
927 RTC_DCHECK(network_thread_->IsCurrent());
928 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
929 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
930 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
931 call_stats_, partial_report);
932 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
933 partial_report);
934 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
935 transport_cert_stats, partial_report);
936}
937
938void RTCStatsCollector::MergeNetworkReport_s() {
939 RTC_DCHECK(signaling_thread_->IsCurrent());
940 // The |network_report_event_| must be signaled for it to be safe to touch
941 // |network_report_|. This is normally not blocking, but if
942 // WaitForPendingRequest() is called while a request is pending, we might have
943 // to wait until the network thread is done touching |network_report_|.
944 network_report_event_.Wait(rtc::Event::kForever);
945 if (!network_report_) {
946 // Normally, MergeNetworkReport_s() is executed because it is posted from
947 // the network thread. But if WaitForPendingRequest() is called while a
948 // request is pending, an early call to MergeNetworkReport_s() is made,
949 // merging the report and setting |network_report_| to null. If so, when the
950 // previously posted MergeNetworkReport_s() is later executed, the report is
951 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700952 return;
953 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000954 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100955 RTC_DCHECK(partial_report_);
956 partial_report_->TakeMembersFrom(network_report_);
957 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000958 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100959 // |network_report_| is currently the only partial report collected
960 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
961 // ready to deliver the result.
962 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
963 cache_timestamp_us_ = partial_report_timestamp_us_;
964 cached_report_ = partial_report_;
965 partial_report_ = nullptr;
966 transceiver_stats_infos_.clear();
967 // Trace WebRTC Stats when getStats is called on Javascript.
968 // This allows access to WebRTC stats from trace logs. To enable them,
969 // select the "webrtc_stats" category when recording traces.
970 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
971 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000972
Henrik Boström40b030e2019-02-28 09:49:31 +0100973 // Deliver report and clear |requests_|.
974 std::vector<RequestInfo> requests;
975 requests.swap(requests_);
976 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700977}
978
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800979void RTCStatsCollector::DeliverCachedReport(
980 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100981 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700982 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100983 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800984 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800985
Henrik Boström5b3541f2018-03-19 13:52:56 +0100986 for (const RequestInfo& request : requests) {
987 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
988 request.callback()->OnStatsDelivered(cached_report);
989 } else {
990 bool filter_by_sender_selector;
991 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
992 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
993 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
994 filter_by_sender_selector = true;
995 sender_selector = request.sender_selector();
996 } else {
997 RTC_DCHECK(request.filter_mode() ==
998 RequestInfo::FilterMode::kReceiverSelector);
999 filter_by_sender_selector = false;
1000 receiver_selector = request.receiver_selector();
1001 }
1002 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1003 filter_by_sender_selector, cached_report, sender_selector,
1004 receiver_selector));
1005 }
hbosc82f2e12016-09-05 01:36:50 -07001006 }
hbosd565b732016-08-30 14:04:35 -07001007}
1008
hbosdf6075a2016-12-19 04:58:02 -08001009void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001010 int64_t timestamp_us,
1011 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001012 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001013 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001014 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1015 if (transport_cert_stats_pair.second.local) {
1016 ProduceCertificateStatsFromSSLCertificateStats(
1017 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001018 }
hbos02ba2112016-10-28 05:14:53 -07001019 if (transport_cert_stats_pair.second.remote) {
1020 ProduceCertificateStatsFromSSLCertificateStats(
1021 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001022 }
1023 }
1024}
1025
hbosdf6075a2016-12-19 04:58:02 -08001026void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001027 int64_t timestamp_us,
1028 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001029 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001030 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001031 for (const auto& stats : transceiver_stats_infos) {
1032 if (!stats.mid) {
1033 continue;
hbos0adb8282016-11-23 02:32:06 -08001034 }
Steve Anton57858b32018-02-15 15:19:50 -08001035 const cricket::VoiceMediaInfo* voice_media_info =
1036 stats.track_media_info_map->voice_media_info();
1037 const cricket::VideoMediaInfo* video_media_info =
1038 stats.track_media_info_map->video_media_info();
1039 // Audio
1040 if (voice_media_info) {
1041 // Inbound
1042 for (const auto& pair : voice_media_info->receive_codecs) {
1043 report->AddStats(CodecStatsFromRtpCodecParameters(
1044 timestamp_us, *stats.mid, true, pair.second));
1045 }
1046 // Outbound
1047 for (const auto& pair : voice_media_info->send_codecs) {
1048 report->AddStats(CodecStatsFromRtpCodecParameters(
1049 timestamp_us, *stats.mid, false, pair.second));
1050 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001051 }
Steve Anton57858b32018-02-15 15:19:50 -08001052 // Video
1053 if (video_media_info) {
1054 // Inbound
1055 for (const auto& pair : video_media_info->receive_codecs) {
1056 report->AddStats(CodecStatsFromRtpCodecParameters(
1057 timestamp_us, *stats.mid, true, pair.second));
1058 }
1059 // Outbound
1060 for (const auto& pair : video_media_info->send_codecs) {
1061 report->AddStats(CodecStatsFromRtpCodecParameters(
1062 timestamp_us, *stats.mid, false, pair.second));
1063 }
hbos0adb8282016-11-23 02:32:06 -08001064 }
1065 }
1066}
1067
hboscc555c52016-10-18 12:48:31 -07001068void RTCStatsCollector::ProduceDataChannelStats_s(
1069 int64_t timestamp_us, RTCStatsReport* report) const {
1070 RTC_DCHECK(signaling_thread_->IsCurrent());
1071 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1072 pc_->sctp_data_channels()) {
1073 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1074 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001075 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001076 timestamp_us));
1077 data_channel_stats->label = data_channel->label();
1078 data_channel_stats->protocol = data_channel->protocol();
1079 data_channel_stats->datachannelid = data_channel->id();
1080 data_channel_stats->state =
1081 DataStateToRTCDataChannelState(data_channel->state());
1082 data_channel_stats->messages_sent = data_channel->messages_sent();
1083 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1084 data_channel_stats->messages_received = data_channel->messages_received();
1085 data_channel_stats->bytes_received = data_channel->bytes_received();
1086 report->AddStats(std::move(data_channel_stats));
1087 }
1088}
1089
hbosdf6075a2016-12-19 04:58:02 -08001090void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001091 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001092 const std::map<std::string, cricket::TransportStats>&
1093 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001094 const Call::Stats& call_stats,
1095 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001096 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001097 for (const auto& entry : transport_stats_by_name) {
1098 const std::string& transport_name = entry.first;
1099 const cricket::TransportStats& transport_stats = entry.second;
1100 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001101 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001102 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001103 for (const cricket::ConnectionInfo& info :
1104 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001105 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001106 new RTCIceCandidatePairStats(
1107 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1108 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001109
hbos0583b282016-11-30 01:50:14 -08001110 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001111 // TODO(hbos): There could be other candidates that are not paired with
1112 // anything. We don't have a complete list. Local candidates come from
1113 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001114 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001115 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001116 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001117 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001118 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001119 candidate_pair_stats->state =
1120 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1121 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001122 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001123 // TODO(hbos): This writable is different than the spec. It goes to
1124 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001125 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001126 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001127 candidate_pair_stats->bytes_sent =
1128 static_cast<uint64_t>(info.sent_total_bytes);
1129 candidate_pair_stats->bytes_received =
1130 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001131 candidate_pair_stats->total_round_trip_time =
1132 static_cast<double>(info.total_round_trip_time_ms) /
1133 rtc::kNumMillisecsPerSec;
1134 if (info.current_round_trip_time_ms) {
1135 candidate_pair_stats->current_round_trip_time =
1136 static_cast<double>(*info.current_round_trip_time_ms) /
1137 rtc::kNumMillisecsPerSec;
1138 }
stefanf79ade12017-06-02 06:44:03 -07001139 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001140 // The bandwidth estimations we have are for the selected candidate
1141 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001142 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1143 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1144 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001145 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001146 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001147 }
stefanf79ade12017-06-02 06:44:03 -07001148 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001149 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001150 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001151 }
1152 }
hbosd82f5122016-12-09 04:12:39 -08001153 candidate_pair_stats->requests_received =
1154 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001155 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1156 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001157 candidate_pair_stats->responses_received =
1158 static_cast<uint64_t>(info.recv_ping_responses);
1159 candidate_pair_stats->responses_sent =
1160 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001161 RTC_DCHECK_GE(info.sent_ping_requests_total,
1162 info.sent_ping_requests_before_first_response);
1163 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1164 info.sent_ping_requests_total -
1165 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001166
1167 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001168 }
1169 }
1170 }
1171}
1172
Steve Anton57858b32018-02-15 15:19:50 -08001173void RTCStatsCollector::ProduceMediaStreamStats_s(
1174 int64_t timestamp_us,
1175 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001176 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001177
1178 std::map<std::string, std::vector<std::string>> track_ids;
1179
1180 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001181 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001182 std::string track_id =
1183 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1184 kSender, sender->internal()->AttachmentId());
1185 for (auto& stream_id : sender->stream_ids()) {
1186 track_ids[stream_id].push_back(track_id);
1187 }
1188 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001189 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001190 std::string track_id =
1191 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1192 kReceiver, receiver->internal()->AttachmentId());
1193 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001194 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001195 }
1196 }
1197 }
1198
1199 // Build stats for each stream ID known.
1200 for (auto& it : track_ids) {
1201 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1202 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1203 stream_stats->stream_identifier = it.first;
1204 stream_stats->track_ids = it.second;
1205 report->AddStats(std::move(stream_stats));
1206 }
1207}
1208
1209void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1210 int64_t timestamp_us,
1211 RTCStatsReport* report) const {
1212 RTC_DCHECK(signaling_thread_->IsCurrent());
1213 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1214 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001215 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001216 senders.push_back(sender->internal());
1217 }
1218 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1219 senders, report);
1220
1221 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001222 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001223 receivers.push_back(receiver->internal());
1224 }
1225 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1226 receivers, report);
1227 }
hbos09bc1282016-11-08 06:29:22 -08001228}
1229
hbos6ab97ce2016-10-03 14:16:56 -07001230void RTCStatsCollector::ProducePeerConnectionStats_s(
1231 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001232 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001233 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001234 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001235 stats->data_channels_opened = internal_record_.data_channels_opened;
1236 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001237 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001238}
1239
hbosdf6075a2016-12-19 04:58:02 -08001240void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001241 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001242 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001243 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001244 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001245
Steve Anton57858b32018-02-15 15:19:50 -08001246 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1247 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1248 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1249 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1250 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1251 } else {
1252 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001253 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001254 }
Steve Anton57858b32018-02-15 15:19:50 -08001255}
1256
1257void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1258 int64_t timestamp_us,
1259 const RtpTransceiverStatsInfo& stats,
1260 RTCStatsReport* report) const {
1261 if (!stats.mid || !stats.transport_name) {
1262 return;
1263 }
1264 RTC_DCHECK(stats.track_media_info_map);
1265 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1266 RTC_DCHECK(track_media_info_map.voice_media_info());
1267 std::string mid = *stats.mid;
1268 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1269 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1270 // Inbound
1271 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1272 track_media_info_map.voice_media_info()->receivers) {
1273 if (!voice_receiver_info.connected())
1274 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001275 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001276 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1277 timestamp_us);
1278 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1279 inbound_audio.get());
1280 // TODO(hta): This lookup should look for the sender, not the track.
1281 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1282 track_media_info_map.GetAudioTrack(voice_receiver_info);
1283 if (audio_track) {
1284 inbound_audio->track_id =
1285 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1286 kReceiver,
1287 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001288 }
Steve Anton57858b32018-02-15 15:19:50 -08001289 inbound_audio->transport_id = transport_id;
1290 report->AddStats(std::move(inbound_audio));
1291 }
1292 // Outbound
1293 for (const cricket::VoiceSenderInfo& voice_sender_info :
1294 track_media_info_map.voice_media_info()->senders) {
1295 if (!voice_sender_info.connected())
1296 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001297 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001298 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1299 timestamp_us);
1300 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1301 outbound_audio.get());
1302 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1303 track_media_info_map.GetAudioTrack(voice_sender_info);
1304 if (audio_track) {
1305 outbound_audio->track_id =
1306 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1307 kSender,
1308 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001309 }
Steve Anton57858b32018-02-15 15:19:50 -08001310 outbound_audio->transport_id = transport_id;
1311 report->AddStats(std::move(outbound_audio));
1312 }
1313}
1314
1315void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1316 int64_t timestamp_us,
1317 const RtpTransceiverStatsInfo& stats,
1318 RTCStatsReport* report) const {
1319 if (!stats.mid || !stats.transport_name) {
1320 return;
1321 }
1322 RTC_DCHECK(stats.track_media_info_map);
1323 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1324 RTC_DCHECK(track_media_info_map.video_media_info());
1325 std::string mid = *stats.mid;
1326 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1327 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1328 // Inbound
1329 for (const cricket::VideoReceiverInfo& video_receiver_info :
1330 track_media_info_map.video_media_info()->receivers) {
1331 if (!video_receiver_info.connected())
1332 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001333 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001334 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1335 timestamp_us);
1336 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1337 inbound_video.get());
1338 rtc::scoped_refptr<VideoTrackInterface> video_track =
1339 track_media_info_map.GetVideoTrack(video_receiver_info);
1340 if (video_track) {
1341 inbound_video->track_id =
1342 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1343 kReceiver,
1344 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1345 }
1346 inbound_video->transport_id = transport_id;
1347 report->AddStats(std::move(inbound_video));
1348 }
1349 // Outbound
1350 for (const cricket::VideoSenderInfo& video_sender_info :
1351 track_media_info_map.video_media_info()->senders) {
1352 if (!video_sender_info.connected())
1353 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001354 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001355 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1356 timestamp_us);
1357 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1358 outbound_video.get());
1359 rtc::scoped_refptr<VideoTrackInterface> video_track =
1360 track_media_info_map.GetVideoTrack(video_sender_info);
1361 if (video_track) {
1362 outbound_video->track_id =
1363 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1364 kSender,
1365 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1366 }
1367 outbound_video->transport_id = transport_id;
1368 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001369 }
1370}
1371
hbosdf6075a2016-12-19 04:58:02 -08001372void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001373 int64_t timestamp_us,
1374 const std::map<std::string, cricket::TransportStats>&
1375 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001376 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1377 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001378 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001379 for (const auto& entry : transport_stats_by_name) {
1380 const std::string& transport_name = entry.first;
1381 const cricket::TransportStats& transport_stats = entry.second;
1382
hbos2fa7c672016-10-24 04:00:05 -07001383 // Get reference to RTCP channel, if it exists.
1384 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001385 for (const cricket::TransportChannelStats& channel_stats :
1386 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001387 if (channel_stats.component ==
1388 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1389 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001390 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001391 break;
1392 }
1393 }
1394
1395 // Get reference to local and remote certificates of this transport, if they
1396 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001397 const auto& certificate_stats_it =
1398 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001399 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1400 std::string local_certificate_id;
1401 if (certificate_stats_it->second.local) {
1402 local_certificate_id = RTCCertificateIDFromFingerprint(
1403 certificate_stats_it->second.local->fingerprint);
1404 }
1405 std::string remote_certificate_id;
1406 if (certificate_stats_it->second.remote) {
1407 remote_certificate_id = RTCCertificateIDFromFingerprint(
1408 certificate_stats_it->second.remote->fingerprint);
1409 }
1410
1411 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001412 for (const cricket::TransportChannelStats& channel_stats :
1413 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001414 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001415 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1416 transport_name, channel_stats.component),
1417 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001418 transport_stats->bytes_sent = 0;
1419 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001420 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1421 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001422 for (const cricket::ConnectionInfo& info :
1423 channel_stats.connection_infos) {
1424 *transport_stats->bytes_sent += info.sent_total_bytes;
1425 *transport_stats->bytes_received += info.recv_total_bytes;
1426 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001427 transport_stats->selected_candidate_pair_id =
1428 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1429 }
1430 }
1431 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1432 !rtcp_transport_stats_id.empty()) {
1433 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1434 }
1435 if (!local_certificate_id.empty())
1436 transport_stats->local_certificate_id = local_certificate_id;
1437 if (!remote_certificate_id.empty())
1438 transport_stats->remote_certificate_id = remote_certificate_id;
1439 report->AddStats(std::move(transport_stats));
1440 }
1441 }
1442}
1443
1444std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001445RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001446 const std::map<std::string, cricket::TransportStats>&
1447 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001448 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001449 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001450 for (const auto& entry : transport_stats_by_name) {
1451 const std::string& transport_name = entry.first;
1452
hbos2fa7c672016-10-24 04:00:05 -07001453 CertificateStatsPair certificate_stats_pair;
1454 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001455 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001456 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001457 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001458 }
Steve Anton5dfde182018-02-06 10:34:40 -08001459
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001460 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1461 pc_->GetRemoteSSLCertChain(transport_name);
1462 if (remote_cert_chain) {
1463 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001464 }
Steve Anton5dfde182018-02-06 10:34:40 -08001465
hbos2fa7c672016-10-24 04:00:05 -07001466 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001467 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001468 }
1469 return transport_cert_stats;
1470}
1471
Steve Anton57858b32018-02-15 15:19:50 -08001472std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1473RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1474 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001475
Steve Anton57858b32018-02-15 15:19:50 -08001476 // These are used to invoke GetStats for all the media channels together in
1477 // one worker thread hop.
1478 std::map<cricket::VoiceMediaChannel*,
1479 std::unique_ptr<cricket::VoiceMediaInfo>>
1480 voice_stats;
1481 std::map<cricket::VideoMediaChannel*,
1482 std::unique_ptr<cricket::VideoMediaInfo>>
1483 video_stats;
1484
Mirko Bonadei739baf02019-01-27 17:29:42 +01001485 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001486 cricket::MediaType media_type = transceiver->media_type();
1487
1488 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1489 // stats have been fetched on the worker thread.
1490 transceiver_stats_infos.emplace_back();
1491 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1492 stats.transceiver = transceiver->internal();
1493 stats.media_type = media_type;
1494
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001495 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001496 if (!channel) {
1497 // The remaining fields require a BaseChannel.
1498 continue;
1499 }
1500
1501 stats.mid = channel->content_name();
1502 stats.transport_name = channel->transport_name();
1503
1504 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1505 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1506 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1507 voice_stats.end());
1508 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001509 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001510 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1511 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1512 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1513 video_stats.end());
1514 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001515 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001516 } else {
1517 RTC_NOTREACHED();
1518 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001519 }
Steve Anton57858b32018-02-15 15:19:50 -08001520
1521 // Call GetStats for all media channels together on the worker thread in one
1522 // hop.
1523 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1524 for (const auto& entry : voice_stats) {
1525 if (!entry.first->GetStats(entry.second.get())) {
1526 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1527 }
1528 }
1529 for (const auto& entry : video_stats) {
1530 if (!entry.first->GetStats(entry.second.get())) {
1531 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1532 }
1533 }
1534 });
1535
1536 // Create the TrackMediaInfoMap for each transceiver stats object.
1537 for (auto& stats : transceiver_stats_infos) {
1538 auto transceiver = stats.transceiver;
1539 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1540 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1541 if (transceiver->channel()) {
1542 cricket::MediaType media_type = transceiver->media_type();
1543 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1544 auto* voice_channel =
1545 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1546 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1547 voice_media_info =
1548 std::move(voice_stats[voice_channel->media_channel()]);
1549 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1550 auto* video_channel =
1551 static_cast<cricket::VideoChannel*>(transceiver->channel());
1552 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1553 video_media_info =
1554 std::move(video_stats[video_channel->media_channel()]);
1555 }
1556 }
1557 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001558 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001559 senders.push_back(sender->internal());
1560 }
1561 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001562 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001563 receivers.push_back(receiver->internal());
1564 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001565 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001566 std::move(voice_media_info), std::move(video_media_info), senders,
1567 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001568 }
Steve Anton57858b32018-02-15 15:19:50 -08001569
1570 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001571}
1572
Steve Anton7eca0932018-03-30 15:18:41 -07001573std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1574 std::set<std::string> transport_names;
1575 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1576 if (transceiver->internal()->channel()) {
1577 transport_names.insert(
1578 transceiver->internal()->channel()->transport_name());
1579 }
1580 }
1581 if (pc_->rtp_data_channel()) {
1582 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1583 }
1584 if (pc_->sctp_transport_name()) {
1585 transport_names.insert(*pc_->sctp_transport_name());
1586 }
1587 return transport_names;
1588}
1589
hbos82ebe022016-11-14 01:41:09 -08001590void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1591 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1592 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1593}
1594
1595void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1596 RTC_DCHECK(signaling_thread_->IsCurrent());
1597 bool result = internal_record_.opened_data_channels.insert(
1598 reinterpret_cast<uintptr_t>(channel)).second;
1599 ++internal_record_.data_channels_opened;
1600 RTC_DCHECK(result);
1601}
1602
1603void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1604 RTC_DCHECK(signaling_thread_->IsCurrent());
1605 // Only channels that have been fully opened (and have increased the
1606 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001607 if (internal_record_.opened_data_channels.erase(
1608 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001609 ++internal_record_.data_channels_closed;
1610 }
1611}
1612
hboscc555c52016-10-18 12:48:31 -07001613const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1614 const std::string& type) {
1615 return CandidateTypeToRTCIceCandidateType(type);
1616}
1617
1618const char* DataStateToRTCDataChannelStateForTesting(
1619 DataChannelInterface::DataState state) {
1620 return DataStateToRTCDataChannelState(state);
1621}
1622
hbosd565b732016-08-30 14:04:35 -07001623} // namespace webrtc