blob: 8849d8614c226c6e8bb72184c08c5cff137214b0 [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);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200298 outbound_stats->retransmitted_packets_sent =
299 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 01:50:46 -0700300 outbound_stats->bytes_sent =
301 static_cast<uint64_t>(media_sender_info.bytes_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200302 outbound_stats->retransmitted_bytes_sent =
303 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 01:50:46 -0700304}
305
306void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800307 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700308 const cricket::VoiceSenderInfo& voice_sender_info,
309 RTCOutboundRTPStreamStats* outbound_audio) {
310 SetOutboundRTPStreamStatsFromMediaSenderInfo(
311 voice_sender_info, outbound_audio);
312 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200313 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800314 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800315 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
316 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800317 }
hbos6ded1902016-11-01 01:50:46 -0700318 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
319 // purposefully left undefined for audio.
320}
321
322void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800323 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700324 const cricket::VideoSenderInfo& video_sender_info,
325 RTCOutboundRTPStreamStats* outbound_video) {
326 SetOutboundRTPStreamStatsFromMediaSenderInfo(
327 video_sender_info, outbound_video);
328 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200329 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800330 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800331 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
332 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800333 }
hbos6ded1902016-11-01 01:50:46 -0700334 outbound_video->fir_count =
335 static_cast<uint32_t>(video_sender_info.firs_rcvd);
336 outbound_video->pli_count =
337 static_cast<uint32_t>(video_sender_info.plis_rcvd);
338 outbound_video->nack_count =
339 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800340 if (video_sender_info.qp_sum)
341 outbound_video->qp_sum = *video_sender_info.qp_sum;
342 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200343 outbound_video->total_encode_time =
344 static_cast<double>(video_sender_info.total_encode_time_ms) /
345 rtc::kNumMillisecsPerSec;
Henrik Boström2e069262019-04-09 13:59:31 +0200346 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
347 // optional, support the "unspecified" value.
348 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
349 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700350}
351
hbos02ba2112016-10-28 05:14:53 -0700352void ProduceCertificateStatsFromSSLCertificateStats(
353 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
354 RTCStatsReport* report) {
355 RTCCertificateStats* prev_certificate_stats = nullptr;
356 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
357 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800358 std::string certificate_stats_id =
359 RTCCertificateIDFromFingerprint(s->fingerprint);
360 // It is possible for the same certificate to show up multiple times, e.g.
361 // if local and remote side use the same certificate in a loopback call.
362 // If the report already contains stats for this certificate, skip it.
363 if (report->Get(certificate_stats_id)) {
364 RTC_DCHECK_EQ(s, &certificate_stats);
365 break;
366 }
hbos02ba2112016-10-28 05:14:53 -0700367 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800368 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700369 certificate_stats->fingerprint = s->fingerprint;
370 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
371 certificate_stats->base64_certificate = s->base64_certificate;
372 if (prev_certificate_stats)
373 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
374 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
375 prev_certificate_stats = certificate_stats;
376 }
377}
378
379const std::string& ProduceIceCandidateStats(
380 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800381 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700382 const std::string& id = "RTCIceCandidate_" + candidate.id();
383 const RTCStats* stats = report->Get(id);
384 if (!stats) {
385 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
386 if (is_local)
387 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
388 else
389 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800390 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800391 if (is_local) {
392 candidate_stats->network_type =
393 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200394 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
395 std::string relay_protocol = candidate.relay_protocol();
396 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
397 relay_protocol.compare("tcp") == 0 ||
398 relay_protocol.compare("tls") == 0);
399 candidate_stats->relay_protocol = relay_protocol;
400 }
Gary Liu37e489c2017-11-21 10:49:36 -0800401 } else {
402 // We don't expect to know the adapter type of remote candidates.
403 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
404 }
hbos02ba2112016-10-28 05:14:53 -0700405 candidate_stats->ip = candidate.address().ipaddr().ToString();
406 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
407 candidate_stats->protocol = candidate.protocol();
408 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
409 candidate.type());
410 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
411
412 stats = candidate_stats.get();
413 report->AddStats(std::move(candidate_stats));
414 }
415 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
416 : RTCRemoteIceCandidateStats::kType);
417 return stats->id();
418}
419
hbos9e302742017-01-20 02:47:10 -0800420std::unique_ptr<RTCMediaStreamTrackStats>
421ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
422 int64_t timestamp_us,
423 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100424 const cricket::VoiceSenderInfo& voice_sender_info,
425 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800426 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
427 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100428 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
429 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100430 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800431 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
432 audio_track, audio_track_stats.get());
433 audio_track_stats->remote_source = false;
434 audio_track_stats->detached = false;
435 if (voice_sender_info.audio_level >= 0) {
436 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
437 voice_sender_info.audio_level);
438 }
zsteine76bd3a2017-07-14 12:17:49 -0700439 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
440 audio_track_stats->total_samples_duration =
441 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100442 if (voice_sender_info.apm_statistics.echo_return_loss) {
443 audio_track_stats->echo_return_loss =
444 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800445 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100446 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
447 audio_track_stats->echo_return_loss_enhancement =
448 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800449 }
450 return audio_track_stats;
451}
452
453std::unique_ptr<RTCMediaStreamTrackStats>
454ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
455 int64_t timestamp_us,
456 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100457 const cricket::VoiceReceiverInfo& voice_receiver_info,
458 int attachment_id) {
459 // Since receiver tracks can't be reattached, we use the SSRC as
460 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800461 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
462 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100463 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
464 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100465 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800466 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
467 audio_track, audio_track_stats.get());
468 audio_track_stats->remote_source = true;
469 audio_track_stats->detached = false;
470 if (voice_receiver_info.audio_level >= 0) {
471 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
472 voice_receiver_info.audio_level);
473 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200474 audio_track_stats->jitter_buffer_delay =
475 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100476 audio_track_stats->jitter_buffer_emitted_count =
477 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 12:17:49 -0700478 audio_track_stats->total_audio_energy =
479 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700480 audio_track_stats->total_samples_received =
481 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700482 audio_track_stats->total_samples_duration =
483 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700484 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200485 audio_track_stats->concealment_events =
486 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100487 audio_track_stats->jitter_buffer_flushes =
488 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100489 audio_track_stats->delayed_packet_outage_samples =
490 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100491 audio_track_stats->relative_packet_arrival_delay =
492 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200493 audio_track_stats->interruption_count =
494 voice_receiver_info.interruption_count >= 0
495 ? voice_receiver_info.interruption_count
496 : 0;
497 audio_track_stats->total_interruption_duration =
498 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
499 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800500 return audio_track_stats;
501}
502
503std::unique_ptr<RTCMediaStreamTrackStats>
504ProduceMediaStreamTrackStatsFromVideoSenderInfo(
505 int64_t timestamp_us,
506 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100507 const cricket::VideoSenderInfo& video_sender_info,
508 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800509 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
510 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100511 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
512
513 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100514 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800515 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
516 video_track, video_track_stats.get());
517 video_track_stats->remote_source = false;
518 video_track_stats->detached = false;
519 video_track_stats->frame_width = static_cast<uint32_t>(
520 video_sender_info.send_frame_width);
521 video_track_stats->frame_height = static_cast<uint32_t>(
522 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800523 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100524 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800525 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100526 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800527 return video_track_stats;
528}
529
530std::unique_ptr<RTCMediaStreamTrackStats>
531ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
532 int64_t timestamp_us,
533 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100534 const cricket::VideoReceiverInfo& video_receiver_info,
535 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800536 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
537 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100538 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
539
540 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100541 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800542 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
543 video_track, video_track_stats.get());
544 video_track_stats->remote_source = true;
545 video_track_stats->detached = false;
546 if (video_receiver_info.frame_width > 0 &&
547 video_receiver_info.frame_height > 0) {
548 video_track_stats->frame_width = static_cast<uint32_t>(
549 video_receiver_info.frame_width);
550 video_track_stats->frame_height = static_cast<uint32_t>(
551 video_receiver_info.frame_height);
552 }
hbos42f6d2f2017-01-20 03:56:50 -0800553 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800554 // TODO(hbos): When we support receiving simulcast, this should be the total
555 // number of frames correctly decoded, independent of which SSRC it was
556 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100557 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800558 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800559 RTC_DCHECK_GE(video_receiver_info.frames_received,
560 video_receiver_info.frames_rendered);
561 video_track_stats->frames_dropped = video_receiver_info.frames_received -
562 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100563 video_track_stats->freeze_count = video_receiver_info.freeze_count;
564 video_track_stats->pause_count = video_receiver_info.pause_count;
565 video_track_stats->total_freezes_duration =
566 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
567 rtc::kNumMillisecsPerSec;
568 video_track_stats->total_pauses_duration =
569 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
570 rtc::kNumMillisecsPerSec;
571 video_track_stats->total_frames_duration =
572 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
573 rtc::kNumMillisecsPerSec;
574 video_track_stats->sum_squared_frame_durations =
575 video_receiver_info.sum_squared_frame_durations;
576
hbos9e302742017-01-20 02:47:10 -0800577 return video_track_stats;
578}
579
Harald Alvestrand89061872018-01-02 14:08:34 +0100580void ProduceSenderMediaTrackStats(
581 int64_t timestamp_us,
582 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800583 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100584 RTCStatsReport* report) {
585 // This function iterates over the senders to generate outgoing track stats.
586
587 // TODO(hbos): Return stats of detached tracks. We have to perform stats
588 // gathering at the time of detachment to get accurate stats and timestamps.
589 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100590 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100591 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
592 AudioTrackInterface* track =
593 static_cast<AudioTrackInterface*>(sender->track().get());
594 if (!track)
595 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100596 cricket::VoiceSenderInfo null_sender_info;
597 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
598 // TODO(hta): Checking on ssrc is not proper. There should be a way
599 // to see from a sender whether it's connected or not.
600 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800601 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100602 // When pc.close is called, sender info is discarded, so
603 // we generate zeroes instead. Bug: It should be retained.
604 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800605 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100606 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100607 if (sender_info) {
608 voice_sender_info = sender_info;
609 } else {
610 RTC_LOG(LS_INFO)
611 << "RTCStatsCollector: No voice sender info for sender with ssrc "
612 << sender->ssrc();
613 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100614 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100615 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100616 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
617 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100618 report->AddStats(std::move(audio_track_stats));
619 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
620 VideoTrackInterface* track =
621 static_cast<VideoTrackInterface*>(sender->track().get());
622 if (!track)
623 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100624 cricket::VideoSenderInfo null_sender_info;
625 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
626 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100627 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
628 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800629 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100630 // When pc.close is called, sender info is discarded, so
631 // we generate zeroes instead. Bug: It should be retained.
632 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800633 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100634 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100635 if (sender_info) {
636 video_sender_info = sender_info;
637 } else {
638 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
639 << sender->ssrc();
640 }
641 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100642 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100643 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
644 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100645 report->AddStats(std::move(video_track_stats));
646 } else {
647 RTC_NOTREACHED();
648 }
649 }
650}
651
652void ProduceReceiverMediaTrackStats(
653 int64_t timestamp_us,
654 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800655 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100656 RTCStatsReport* report) {
657 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100658 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100659 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
660 AudioTrackInterface* track =
661 static_cast<AudioTrackInterface*>(receiver->track().get());
662 const cricket::VoiceReceiverInfo* voice_receiver_info =
663 track_media_info_map.GetVoiceReceiverInfo(*track);
664 if (!voice_receiver_info) {
665 continue;
666 }
667 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
668 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100669 timestamp_us, *track, *voice_receiver_info,
670 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100671 report->AddStats(std::move(audio_track_stats));
672 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
673 VideoTrackInterface* track =
674 static_cast<VideoTrackInterface*>(receiver->track().get());
675 const cricket::VideoReceiverInfo* video_receiver_info =
676 track_media_info_map.GetVideoReceiverInfo(*track);
677 if (!video_receiver_info) {
678 continue;
679 }
680 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
681 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100682 timestamp_us, *track, *video_receiver_info,
683 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100684 report->AddStats(std::move(video_track_stats));
685 } else {
686 RTC_NOTREACHED();
687 }
688 }
689}
690
Henrik Boström5b3541f2018-03-19 13:52:56 +0100691rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
692 bool filter_by_sender_selector,
693 rtc::scoped_refptr<const RTCStatsReport> report,
694 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
695 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
696 std::vector<std::string> rtpstream_ids;
697 if (filter_by_sender_selector) {
698 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
699 if (sender_selector) {
700 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
701 // reference the sender stats.
702 // Because we do not implement sender stats, we look at outbound-rtp(s)
703 // that reference the track attachment stats for the sender instead.
704 std::string track_id =
705 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
706 kSender, sender_selector->AttachmentId());
707 for (const auto& stats : *report) {
708 if (stats.type() != RTCOutboundRTPStreamStats::kType)
709 continue;
710 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
711 if (outbound_rtp.track_id.is_defined() &&
712 *outbound_rtp.track_id == track_id) {
713 rtpstream_ids.push_back(outbound_rtp.id());
714 }
715 }
716 }
717 } else {
718 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
719 if (receiver_selector) {
720 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
721 // reference the receiver stats.
722 // Because we do not implement receiver stats, we look at inbound-rtp(s)
723 // that reference the track attachment stats for the receiver instead.
724 std::string track_id =
725 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
726 kReceiver, receiver_selector->AttachmentId());
727 for (const auto& stats : *report) {
728 if (stats.type() != RTCInboundRTPStreamStats::kType)
729 continue;
730 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
731 if (inbound_rtp.track_id.is_defined() &&
732 *inbound_rtp.track_id == track_id) {
733 rtpstream_ids.push_back(inbound_rtp.id());
734 }
735 }
736 }
737 }
738 if (rtpstream_ids.empty())
739 return RTCStatsReport::Create(report->timestamp_us());
740 return TakeReferencedStats(report->Copy(), rtpstream_ids);
741}
742
hboscc555c52016-10-18 12:48:31 -0700743} // namespace
744
Henrik Boström5b3541f2018-03-19 13:52:56 +0100745RTCStatsCollector::RequestInfo::RequestInfo(
746 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
747 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
748
749RTCStatsCollector::RequestInfo::RequestInfo(
750 rtc::scoped_refptr<RtpSenderInternal> selector,
751 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
752 : RequestInfo(FilterMode::kSenderSelector,
753 std::move(callback),
754 std::move(selector),
755 nullptr) {}
756
757RTCStatsCollector::RequestInfo::RequestInfo(
758 rtc::scoped_refptr<RtpReceiverInternal> selector,
759 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
760 : RequestInfo(FilterMode::kReceiverSelector,
761 std::move(callback),
762 nullptr,
763 std::move(selector)) {}
764
765RTCStatsCollector::RequestInfo::RequestInfo(
766 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
767 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
768 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
769 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
770 : filter_mode_(filter_mode),
771 callback_(std::move(callback)),
772 sender_selector_(std::move(sender_selector)),
773 receiver_selector_(std::move(receiver_selector)) {
774 RTC_DCHECK(callback_);
775 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
776}
777
hbosc82f2e12016-09-05 01:36:50 -0700778rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800779 PeerConnectionInternal* pc,
780 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700781 return rtc::scoped_refptr<RTCStatsCollector>(
782 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
783}
784
Steve Anton2d8609c2018-01-23 16:38:46 -0800785RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700786 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700787 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700788 signaling_thread_(pc->signaling_thread()),
789 worker_thread_(pc->worker_thread()),
790 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700791 num_pending_partial_reports_(0),
792 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100793 network_report_event_(true /* manual_reset */,
794 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700795 cache_timestamp_us_(0),
796 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700797 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700798 RTC_DCHECK(signaling_thread_);
799 RTC_DCHECK(worker_thread_);
800 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700801 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800802 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800803 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700804}
805
hbosb78306a2016-12-19 05:06:57 -0800806RTCStatsCollector::~RTCStatsCollector() {
807 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
808}
809
hbosc82f2e12016-09-05 01:36:50 -0700810void RTCStatsCollector::GetStatsReport(
811 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100812 GetStatsReportInternal(RequestInfo(std::move(callback)));
813}
814
815void RTCStatsCollector::GetStatsReport(
816 rtc::scoped_refptr<RtpSenderInternal> selector,
817 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
818 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
819}
820
821void RTCStatsCollector::GetStatsReport(
822 rtc::scoped_refptr<RtpReceiverInternal> selector,
823 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
824 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
825}
826
827void RTCStatsCollector::GetStatsReportInternal(
828 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700829 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100830 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700831
hbos0e6758d2016-08-31 07:57:36 -0700832 // "Now" using a monotonically increasing timer.
833 int64_t cache_now_us = rtc::TimeMicros();
834 if (cached_report_ &&
835 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800836 // We have a fresh cached report to deliver. Deliver asynchronously, since
837 // the caller may not be expecting a synchronous callback, and it avoids
838 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100839 std::vector<RequestInfo> requests;
840 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100841 signaling_thread_->PostTask(
842 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
843 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700844 } else if (!num_pending_partial_reports_) {
845 // Only start gathering stats if we're not already gathering stats. In the
846 // case of already gathering stats, |callback_| will be invoked when there
847 // are no more pending partial reports.
848
849 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
850 // UTC), in microseconds. The system clock could be modified and is not
851 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700852 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700853
hbosf415f8a2017-01-02 04:28:51 -0800854 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700855 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800856
Steve Anton57858b32018-02-15 15:19:50 -0800857 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800858 // |ProducePartialResultsOnNetworkThread| and
859 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800860 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700861 // Prepare |transport_names_| for use in
862 // |ProducePartialResultsOnNetworkThread|.
863 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800864
stefanf79ade12017-06-02 06:44:03 -0700865 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
866 // thread.
867 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
868 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700869 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700870
Henrik Boström40b030e2019-02-28 09:49:31 +0100871 // Don't touch |network_report_| on the signaling thread until
872 // ProducePartialResultsOnNetworkThread() has signaled the
873 // |network_report_event_|.
874 network_report_event_.Reset();
875 network_thread_->PostTask(
876 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700877 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100878 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800879 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700880 }
hbosd565b732016-08-30 14:04:35 -0700881}
882
883void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700884 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700885 cached_report_ = nullptr;
886}
887
hbosb78306a2016-12-19 05:06:57 -0800888void RTCStatsCollector::WaitForPendingRequest() {
889 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100890 // If a request is pending, blocks until the |network_report_event_| is
891 // signaled and then delivers the result. Otherwise this is a NO-OP.
892 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800893}
894
hbosc82f2e12016-09-05 01:36:50 -0700895void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
896 int64_t timestamp_us) {
897 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100898 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700899
Henrik Boström40b030e2019-02-28 09:49:31 +0100900 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
901 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700902
Henrik Boström40b030e2019-02-28 09:49:31 +0100903 // ProducePartialResultsOnSignalingThread() is running synchronously on the
904 // signaling thread, so it is always the first partial result delivered on the
905 // signaling thread. The request is not complete until MergeNetworkReport_s()
906 // happens; we don't have to do anything here.
907 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
908 --num_pending_partial_reports_;
909}
910
911void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
912 int64_t timestamp_us,
913 RTCStatsReport* partial_report) {
914 RTC_DCHECK(signaling_thread_->IsCurrent());
915 ProduceDataChannelStats_s(timestamp_us, partial_report);
916 ProduceMediaStreamStats_s(timestamp_us, partial_report);
917 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
918 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700919}
920
hbosc82f2e12016-09-05 01:36:50 -0700921void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
922 int64_t timestamp_us) {
923 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100924 // Touching |network_report_| on this thread is safe by this method because
925 // |network_report_event_| is reset before this method is invoked.
926 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700927
Steve Anton5dfde182018-02-06 10:34:40 -0800928 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700929 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800930 std::map<std::string, CertificateStatsPair> transport_cert_stats =
931 PrepareTransportCertificateStats_n(transport_stats_by_name);
932
Henrik Boström40b030e2019-02-28 09:49:31 +0100933 ProducePartialResultsOnNetworkThreadImpl(
934 timestamp_us, transport_stats_by_name, transport_cert_stats,
935 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000936
Henrik Boström40b030e2019-02-28 09:49:31 +0100937 // Signal that it is now safe to touch |network_report_| on the signaling
938 // thread, and post a task to merge it into the final results.
939 network_report_event_.Set();
940 signaling_thread_->PostTask(
941 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100942}
943
Henrik Boström40b030e2019-02-28 09:49:31 +0100944void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
945 int64_t timestamp_us,
946 const std::map<std::string, cricket::TransportStats>&
947 transport_stats_by_name,
948 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
949 RTCStatsReport* partial_report) {
950 RTC_DCHECK(network_thread_->IsCurrent());
951 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
952 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
953 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
954 call_stats_, partial_report);
955 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
956 partial_report);
957 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
958 transport_cert_stats, partial_report);
959}
960
961void RTCStatsCollector::MergeNetworkReport_s() {
962 RTC_DCHECK(signaling_thread_->IsCurrent());
963 // The |network_report_event_| must be signaled for it to be safe to touch
964 // |network_report_|. This is normally not blocking, but if
965 // WaitForPendingRequest() is called while a request is pending, we might have
966 // to wait until the network thread is done touching |network_report_|.
967 network_report_event_.Wait(rtc::Event::kForever);
968 if (!network_report_) {
969 // Normally, MergeNetworkReport_s() is executed because it is posted from
970 // the network thread. But if WaitForPendingRequest() is called while a
971 // request is pending, an early call to MergeNetworkReport_s() is made,
972 // merging the report and setting |network_report_| to null. If so, when the
973 // previously posted MergeNetworkReport_s() is later executed, the report is
974 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700975 return;
976 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000977 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100978 RTC_DCHECK(partial_report_);
979 partial_report_->TakeMembersFrom(network_report_);
980 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000981 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100982 // |network_report_| is currently the only partial report collected
983 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
984 // ready to deliver the result.
985 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
986 cache_timestamp_us_ = partial_report_timestamp_us_;
987 cached_report_ = partial_report_;
988 partial_report_ = nullptr;
989 transceiver_stats_infos_.clear();
990 // Trace WebRTC Stats when getStats is called on Javascript.
991 // This allows access to WebRTC stats from trace logs. To enable them,
992 // select the "webrtc_stats" category when recording traces.
993 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
994 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000995
Henrik Boström40b030e2019-02-28 09:49:31 +0100996 // Deliver report and clear |requests_|.
997 std::vector<RequestInfo> requests;
998 requests.swap(requests_);
999 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001000}
1001
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001002void RTCStatsCollector::DeliverCachedReport(
1003 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001004 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001005 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001006 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001007 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001008
Henrik Boström5b3541f2018-03-19 13:52:56 +01001009 for (const RequestInfo& request : requests) {
1010 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1011 request.callback()->OnStatsDelivered(cached_report);
1012 } else {
1013 bool filter_by_sender_selector;
1014 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1015 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1016 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1017 filter_by_sender_selector = true;
1018 sender_selector = request.sender_selector();
1019 } else {
1020 RTC_DCHECK(request.filter_mode() ==
1021 RequestInfo::FilterMode::kReceiverSelector);
1022 filter_by_sender_selector = false;
1023 receiver_selector = request.receiver_selector();
1024 }
1025 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1026 filter_by_sender_selector, cached_report, sender_selector,
1027 receiver_selector));
1028 }
hbosc82f2e12016-09-05 01:36:50 -07001029 }
hbosd565b732016-08-30 14:04:35 -07001030}
1031
hbosdf6075a2016-12-19 04:58:02 -08001032void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001033 int64_t timestamp_us,
1034 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001035 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001036 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001037 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1038 if (transport_cert_stats_pair.second.local) {
1039 ProduceCertificateStatsFromSSLCertificateStats(
1040 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001041 }
hbos02ba2112016-10-28 05:14:53 -07001042 if (transport_cert_stats_pair.second.remote) {
1043 ProduceCertificateStatsFromSSLCertificateStats(
1044 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001045 }
1046 }
1047}
1048
hbosdf6075a2016-12-19 04:58:02 -08001049void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001050 int64_t timestamp_us,
1051 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001052 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001053 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001054 for (const auto& stats : transceiver_stats_infos) {
1055 if (!stats.mid) {
1056 continue;
hbos0adb8282016-11-23 02:32:06 -08001057 }
Steve Anton57858b32018-02-15 15:19:50 -08001058 const cricket::VoiceMediaInfo* voice_media_info =
1059 stats.track_media_info_map->voice_media_info();
1060 const cricket::VideoMediaInfo* video_media_info =
1061 stats.track_media_info_map->video_media_info();
1062 // Audio
1063 if (voice_media_info) {
1064 // Inbound
1065 for (const auto& pair : voice_media_info->receive_codecs) {
1066 report->AddStats(CodecStatsFromRtpCodecParameters(
1067 timestamp_us, *stats.mid, true, pair.second));
1068 }
1069 // Outbound
1070 for (const auto& pair : voice_media_info->send_codecs) {
1071 report->AddStats(CodecStatsFromRtpCodecParameters(
1072 timestamp_us, *stats.mid, false, pair.second));
1073 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001074 }
Steve Anton57858b32018-02-15 15:19:50 -08001075 // Video
1076 if (video_media_info) {
1077 // Inbound
1078 for (const auto& pair : video_media_info->receive_codecs) {
1079 report->AddStats(CodecStatsFromRtpCodecParameters(
1080 timestamp_us, *stats.mid, true, pair.second));
1081 }
1082 // Outbound
1083 for (const auto& pair : video_media_info->send_codecs) {
1084 report->AddStats(CodecStatsFromRtpCodecParameters(
1085 timestamp_us, *stats.mid, false, pair.second));
1086 }
hbos0adb8282016-11-23 02:32:06 -08001087 }
1088 }
1089}
1090
hboscc555c52016-10-18 12:48:31 -07001091void RTCStatsCollector::ProduceDataChannelStats_s(
1092 int64_t timestamp_us, RTCStatsReport* report) const {
1093 RTC_DCHECK(signaling_thread_->IsCurrent());
1094 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1095 pc_->sctp_data_channels()) {
1096 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1097 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001098 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001099 timestamp_us));
1100 data_channel_stats->label = data_channel->label();
1101 data_channel_stats->protocol = data_channel->protocol();
1102 data_channel_stats->datachannelid = data_channel->id();
1103 data_channel_stats->state =
1104 DataStateToRTCDataChannelState(data_channel->state());
1105 data_channel_stats->messages_sent = data_channel->messages_sent();
1106 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1107 data_channel_stats->messages_received = data_channel->messages_received();
1108 data_channel_stats->bytes_received = data_channel->bytes_received();
1109 report->AddStats(std::move(data_channel_stats));
1110 }
1111}
1112
hbosdf6075a2016-12-19 04:58:02 -08001113void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001114 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001115 const std::map<std::string, cricket::TransportStats>&
1116 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001117 const Call::Stats& call_stats,
1118 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001119 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001120 for (const auto& entry : transport_stats_by_name) {
1121 const std::string& transport_name = entry.first;
1122 const cricket::TransportStats& transport_stats = entry.second;
1123 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001124 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001125 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001126 for (const cricket::ConnectionInfo& info :
1127 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001128 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001129 new RTCIceCandidatePairStats(
1130 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1131 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001132
hbos0583b282016-11-30 01:50:14 -08001133 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001134 // TODO(hbos): There could be other candidates that are not paired with
1135 // anything. We don't have a complete list. Local candidates come from
1136 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001137 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001138 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001139 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001140 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001141 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001142 candidate_pair_stats->state =
1143 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1144 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001145 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001146 // TODO(hbos): This writable is different than the spec. It goes to
1147 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001148 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001149 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001150 candidate_pair_stats->bytes_sent =
1151 static_cast<uint64_t>(info.sent_total_bytes);
1152 candidate_pair_stats->bytes_received =
1153 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001154 candidate_pair_stats->total_round_trip_time =
1155 static_cast<double>(info.total_round_trip_time_ms) /
1156 rtc::kNumMillisecsPerSec;
1157 if (info.current_round_trip_time_ms) {
1158 candidate_pair_stats->current_round_trip_time =
1159 static_cast<double>(*info.current_round_trip_time_ms) /
1160 rtc::kNumMillisecsPerSec;
1161 }
stefanf79ade12017-06-02 06:44:03 -07001162 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001163 // The bandwidth estimations we have are for the selected candidate
1164 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001165 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1166 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1167 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001168 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001169 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001170 }
stefanf79ade12017-06-02 06:44:03 -07001171 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001172 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001173 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001174 }
1175 }
hbosd82f5122016-12-09 04:12:39 -08001176 candidate_pair_stats->requests_received =
1177 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001178 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1179 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001180 candidate_pair_stats->responses_received =
1181 static_cast<uint64_t>(info.recv_ping_responses);
1182 candidate_pair_stats->responses_sent =
1183 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001184 RTC_DCHECK_GE(info.sent_ping_requests_total,
1185 info.sent_ping_requests_before_first_response);
1186 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1187 info.sent_ping_requests_total -
1188 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001189
1190 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001191 }
1192 }
1193 }
1194}
1195
Steve Anton57858b32018-02-15 15:19:50 -08001196void RTCStatsCollector::ProduceMediaStreamStats_s(
1197 int64_t timestamp_us,
1198 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001199 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001200
1201 std::map<std::string, std::vector<std::string>> track_ids;
1202
1203 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001204 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001205 std::string track_id =
1206 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1207 kSender, sender->internal()->AttachmentId());
1208 for (auto& stream_id : sender->stream_ids()) {
1209 track_ids[stream_id].push_back(track_id);
1210 }
1211 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001212 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001213 std::string track_id =
1214 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1215 kReceiver, receiver->internal()->AttachmentId());
1216 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001217 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001218 }
1219 }
1220 }
1221
1222 // Build stats for each stream ID known.
1223 for (auto& it : track_ids) {
1224 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1225 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1226 stream_stats->stream_identifier = it.first;
1227 stream_stats->track_ids = it.second;
1228 report->AddStats(std::move(stream_stats));
1229 }
1230}
1231
1232void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1233 int64_t timestamp_us,
1234 RTCStatsReport* report) const {
1235 RTC_DCHECK(signaling_thread_->IsCurrent());
1236 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1237 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001238 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001239 senders.push_back(sender->internal());
1240 }
1241 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1242 senders, report);
1243
1244 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001245 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001246 receivers.push_back(receiver->internal());
1247 }
1248 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1249 receivers, report);
1250 }
hbos09bc1282016-11-08 06:29:22 -08001251}
1252
hbos6ab97ce2016-10-03 14:16:56 -07001253void RTCStatsCollector::ProducePeerConnectionStats_s(
1254 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001255 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001256 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001257 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001258 stats->data_channels_opened = internal_record_.data_channels_opened;
1259 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001260 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001261}
1262
hbosdf6075a2016-12-19 04:58:02 -08001263void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001264 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001265 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001266 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001267 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001268
Steve Anton57858b32018-02-15 15:19:50 -08001269 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1270 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1271 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1272 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1273 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1274 } else {
1275 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001276 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001277 }
Steve Anton57858b32018-02-15 15:19:50 -08001278}
1279
1280void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1281 int64_t timestamp_us,
1282 const RtpTransceiverStatsInfo& stats,
1283 RTCStatsReport* report) const {
1284 if (!stats.mid || !stats.transport_name) {
1285 return;
1286 }
1287 RTC_DCHECK(stats.track_media_info_map);
1288 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1289 RTC_DCHECK(track_media_info_map.voice_media_info());
1290 std::string mid = *stats.mid;
1291 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1292 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1293 // Inbound
1294 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1295 track_media_info_map.voice_media_info()->receivers) {
1296 if (!voice_receiver_info.connected())
1297 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001298 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001299 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1300 timestamp_us);
1301 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1302 inbound_audio.get());
1303 // TODO(hta): This lookup should look for the sender, not the track.
1304 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1305 track_media_info_map.GetAudioTrack(voice_receiver_info);
1306 if (audio_track) {
1307 inbound_audio->track_id =
1308 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1309 kReceiver,
1310 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001311 }
Steve Anton57858b32018-02-15 15:19:50 -08001312 inbound_audio->transport_id = transport_id;
1313 report->AddStats(std::move(inbound_audio));
1314 }
1315 // Outbound
1316 for (const cricket::VoiceSenderInfo& voice_sender_info :
1317 track_media_info_map.voice_media_info()->senders) {
1318 if (!voice_sender_info.connected())
1319 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001320 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001321 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1322 timestamp_us);
1323 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1324 outbound_audio.get());
1325 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1326 track_media_info_map.GetAudioTrack(voice_sender_info);
1327 if (audio_track) {
1328 outbound_audio->track_id =
1329 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1330 kSender,
1331 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001332 }
Steve Anton57858b32018-02-15 15:19:50 -08001333 outbound_audio->transport_id = transport_id;
1334 report->AddStats(std::move(outbound_audio));
1335 }
1336}
1337
1338void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1339 int64_t timestamp_us,
1340 const RtpTransceiverStatsInfo& stats,
1341 RTCStatsReport* report) const {
1342 if (!stats.mid || !stats.transport_name) {
1343 return;
1344 }
1345 RTC_DCHECK(stats.track_media_info_map);
1346 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1347 RTC_DCHECK(track_media_info_map.video_media_info());
1348 std::string mid = *stats.mid;
1349 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1350 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1351 // Inbound
1352 for (const cricket::VideoReceiverInfo& video_receiver_info :
1353 track_media_info_map.video_media_info()->receivers) {
1354 if (!video_receiver_info.connected())
1355 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001356 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001357 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1358 timestamp_us);
1359 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1360 inbound_video.get());
1361 rtc::scoped_refptr<VideoTrackInterface> video_track =
1362 track_media_info_map.GetVideoTrack(video_receiver_info);
1363 if (video_track) {
1364 inbound_video->track_id =
1365 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1366 kReceiver,
1367 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1368 }
1369 inbound_video->transport_id = transport_id;
1370 report->AddStats(std::move(inbound_video));
1371 }
1372 // Outbound
1373 for (const cricket::VideoSenderInfo& video_sender_info :
1374 track_media_info_map.video_media_info()->senders) {
1375 if (!video_sender_info.connected())
1376 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001377 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001378 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1379 timestamp_us);
1380 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1381 outbound_video.get());
1382 rtc::scoped_refptr<VideoTrackInterface> video_track =
1383 track_media_info_map.GetVideoTrack(video_sender_info);
1384 if (video_track) {
1385 outbound_video->track_id =
1386 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1387 kSender,
1388 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1389 }
1390 outbound_video->transport_id = transport_id;
1391 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001392 }
1393}
1394
hbosdf6075a2016-12-19 04:58:02 -08001395void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001396 int64_t timestamp_us,
1397 const std::map<std::string, cricket::TransportStats>&
1398 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001399 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1400 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001401 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001402 for (const auto& entry : transport_stats_by_name) {
1403 const std::string& transport_name = entry.first;
1404 const cricket::TransportStats& transport_stats = entry.second;
1405
hbos2fa7c672016-10-24 04:00:05 -07001406 // Get reference to RTCP channel, if it exists.
1407 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001408 for (const cricket::TransportChannelStats& channel_stats :
1409 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001410 if (channel_stats.component ==
1411 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1412 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001413 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001414 break;
1415 }
1416 }
1417
1418 // Get reference to local and remote certificates of this transport, if they
1419 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001420 const auto& certificate_stats_it =
1421 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001422 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1423 std::string local_certificate_id;
1424 if (certificate_stats_it->second.local) {
1425 local_certificate_id = RTCCertificateIDFromFingerprint(
1426 certificate_stats_it->second.local->fingerprint);
1427 }
1428 std::string remote_certificate_id;
1429 if (certificate_stats_it->second.remote) {
1430 remote_certificate_id = RTCCertificateIDFromFingerprint(
1431 certificate_stats_it->second.remote->fingerprint);
1432 }
1433
1434 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001435 for (const cricket::TransportChannelStats& channel_stats :
1436 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001437 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001438 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1439 transport_name, channel_stats.component),
1440 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001441 transport_stats->bytes_sent = 0;
1442 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001443 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1444 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001445 for (const cricket::ConnectionInfo& info :
1446 channel_stats.connection_infos) {
1447 *transport_stats->bytes_sent += info.sent_total_bytes;
1448 *transport_stats->bytes_received += info.recv_total_bytes;
1449 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001450 transport_stats->selected_candidate_pair_id =
1451 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1452 }
1453 }
1454 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1455 !rtcp_transport_stats_id.empty()) {
1456 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1457 }
1458 if (!local_certificate_id.empty())
1459 transport_stats->local_certificate_id = local_certificate_id;
1460 if (!remote_certificate_id.empty())
1461 transport_stats->remote_certificate_id = remote_certificate_id;
1462 report->AddStats(std::move(transport_stats));
1463 }
1464 }
1465}
1466
1467std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001468RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001469 const std::map<std::string, cricket::TransportStats>&
1470 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001471 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001472 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001473 for (const auto& entry : transport_stats_by_name) {
1474 const std::string& transport_name = entry.first;
1475
hbos2fa7c672016-10-24 04:00:05 -07001476 CertificateStatsPair certificate_stats_pair;
1477 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001478 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001479 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001480 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001481 }
Steve Anton5dfde182018-02-06 10:34:40 -08001482
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001483 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1484 pc_->GetRemoteSSLCertChain(transport_name);
1485 if (remote_cert_chain) {
1486 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001487 }
Steve Anton5dfde182018-02-06 10:34:40 -08001488
hbos2fa7c672016-10-24 04:00:05 -07001489 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001490 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001491 }
1492 return transport_cert_stats;
1493}
1494
Steve Anton57858b32018-02-15 15:19:50 -08001495std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1496RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1497 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001498
Steve Anton57858b32018-02-15 15:19:50 -08001499 // These are used to invoke GetStats for all the media channels together in
1500 // one worker thread hop.
1501 std::map<cricket::VoiceMediaChannel*,
1502 std::unique_ptr<cricket::VoiceMediaInfo>>
1503 voice_stats;
1504 std::map<cricket::VideoMediaChannel*,
1505 std::unique_ptr<cricket::VideoMediaInfo>>
1506 video_stats;
1507
Mirko Bonadei739baf02019-01-27 17:29:42 +01001508 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001509 cricket::MediaType media_type = transceiver->media_type();
1510
1511 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1512 // stats have been fetched on the worker thread.
1513 transceiver_stats_infos.emplace_back();
1514 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1515 stats.transceiver = transceiver->internal();
1516 stats.media_type = media_type;
1517
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001518 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001519 if (!channel) {
1520 // The remaining fields require a BaseChannel.
1521 continue;
1522 }
1523
1524 stats.mid = channel->content_name();
1525 stats.transport_name = channel->transport_name();
1526
1527 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1528 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1529 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1530 voice_stats.end());
1531 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001532 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001533 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1534 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1535 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1536 video_stats.end());
1537 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001538 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001539 } else {
1540 RTC_NOTREACHED();
1541 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001542 }
Steve Anton57858b32018-02-15 15:19:50 -08001543
1544 // Call GetStats for all media channels together on the worker thread in one
1545 // hop.
1546 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1547 for (const auto& entry : voice_stats) {
1548 if (!entry.first->GetStats(entry.second.get())) {
1549 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1550 }
1551 }
1552 for (const auto& entry : video_stats) {
1553 if (!entry.first->GetStats(entry.second.get())) {
1554 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1555 }
1556 }
1557 });
1558
1559 // Create the TrackMediaInfoMap for each transceiver stats object.
1560 for (auto& stats : transceiver_stats_infos) {
1561 auto transceiver = stats.transceiver;
1562 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1563 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1564 if (transceiver->channel()) {
1565 cricket::MediaType media_type = transceiver->media_type();
1566 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1567 auto* voice_channel =
1568 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1569 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1570 voice_media_info =
1571 std::move(voice_stats[voice_channel->media_channel()]);
1572 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1573 auto* video_channel =
1574 static_cast<cricket::VideoChannel*>(transceiver->channel());
1575 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1576 video_media_info =
1577 std::move(video_stats[video_channel->media_channel()]);
1578 }
1579 }
1580 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001581 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001582 senders.push_back(sender->internal());
1583 }
1584 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001585 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001586 receivers.push_back(receiver->internal());
1587 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001588 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001589 std::move(voice_media_info), std::move(video_media_info), senders,
1590 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001591 }
Steve Anton57858b32018-02-15 15:19:50 -08001592
1593 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001594}
1595
Steve Anton7eca0932018-03-30 15:18:41 -07001596std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1597 std::set<std::string> transport_names;
1598 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1599 if (transceiver->internal()->channel()) {
1600 transport_names.insert(
1601 transceiver->internal()->channel()->transport_name());
1602 }
1603 }
1604 if (pc_->rtp_data_channel()) {
1605 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1606 }
1607 if (pc_->sctp_transport_name()) {
1608 transport_names.insert(*pc_->sctp_transport_name());
1609 }
1610 return transport_names;
1611}
1612
hbos82ebe022016-11-14 01:41:09 -08001613void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1614 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1615 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1616}
1617
1618void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1619 RTC_DCHECK(signaling_thread_->IsCurrent());
1620 bool result = internal_record_.opened_data_channels.insert(
1621 reinterpret_cast<uintptr_t>(channel)).second;
1622 ++internal_record_.data_channels_opened;
1623 RTC_DCHECK(result);
1624}
1625
1626void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1627 RTC_DCHECK(signaling_thread_->IsCurrent());
1628 // Only channels that have been fully opened (and have increased the
1629 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001630 if (internal_record_.opened_data_channels.erase(
1631 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001632 ++internal_record_.data_channels_closed;
1633 }
1634}
1635
hboscc555c52016-10-18 12:48:31 -07001636const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1637 const std::string& type) {
1638 return CandidateTypeToRTCIceCandidateType(type);
1639}
1640
1641const char* DataStateToRTCDataChannelStateForTesting(
1642 DataChannelInterface::DataState state) {
1643 return DataStateToRTCDataChannelState(state);
1644}
1645
hbosd565b732016-08-30 14:04:35 -07001646} // namespace webrtc