blob: 3e11a920611b2d5638d9c75e888ba1494b056870 [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 }
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200253 inbound_audio->fec_packets_received =
254 voice_receiver_info.fec_packets_received;
255 inbound_audio->fec_packets_discarded =
256 voice_receiver_info.fec_packets_discarded;
hboseeafe942016-11-01 03:00:17 -0700257}
258
259void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800260 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700261 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800262 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700263 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800264 video_receiver_info, inbound_video);
265 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200266 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800267 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800268 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
269 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800270 }
hbos820f5782016-11-22 03:16:50 -0800271 inbound_video->fir_count =
272 static_cast<uint32_t>(video_receiver_info.firs_sent);
273 inbound_video->pli_count =
274 static_cast<uint32_t>(video_receiver_info.plis_sent);
275 inbound_video->nack_count =
276 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800277 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800278 if (video_receiver_info.qp_sum)
279 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Henrik Boström01738c62019-04-15 17:32:00 +0200280 if (video_receiver_info.last_packet_received_timestamp_ms) {
281 inbound_video->last_packet_received_timestamp =
282 static_cast<double>(
283 *video_receiver_info.last_packet_received_timestamp_ms) /
284 rtc::kNumMillisecsPerSec;
285 }
Henrik Boström2e069262019-04-09 13:59:31 +0200286 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
287 // optional, support the "unspecified" value.
288 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
289 inbound_video->content_type = RTCContentType::kScreenshare;
hboseeafe942016-11-01 03:00:17 -0700290}
291
hbos820f5782016-11-22 03:16:50 -0800292// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700293void SetOutboundRTPStreamStatsFromMediaSenderInfo(
294 const cricket::MediaSenderInfo& media_sender_info,
295 RTCOutboundRTPStreamStats* outbound_stats) {
296 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800297 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100298 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700299 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700300 outbound_stats->packets_sent =
301 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200302 outbound_stats->retransmitted_packets_sent =
303 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 01:50:46 -0700304 outbound_stats->bytes_sent =
305 static_cast<uint64_t>(media_sender_info.bytes_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200306 outbound_stats->retransmitted_bytes_sent =
307 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 01:50:46 -0700308}
309
310void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800311 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700312 const cricket::VoiceSenderInfo& voice_sender_info,
313 RTCOutboundRTPStreamStats* outbound_audio) {
314 SetOutboundRTPStreamStatsFromMediaSenderInfo(
315 voice_sender_info, outbound_audio);
316 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200317 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800318 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800319 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
320 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800321 }
hbos6ded1902016-11-01 01:50:46 -0700322 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
323 // purposefully left undefined for audio.
324}
325
326void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800327 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700328 const cricket::VideoSenderInfo& video_sender_info,
329 RTCOutboundRTPStreamStats* outbound_video) {
330 SetOutboundRTPStreamStatsFromMediaSenderInfo(
331 video_sender_info, outbound_video);
332 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200333 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800334 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800335 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
336 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800337 }
hbos6ded1902016-11-01 01:50:46 -0700338 outbound_video->fir_count =
339 static_cast<uint32_t>(video_sender_info.firs_rcvd);
340 outbound_video->pli_count =
341 static_cast<uint32_t>(video_sender_info.plis_rcvd);
342 outbound_video->nack_count =
343 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800344 if (video_sender_info.qp_sum)
345 outbound_video->qp_sum = *video_sender_info.qp_sum;
346 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200347 outbound_video->total_encode_time =
348 static_cast<double>(video_sender_info.total_encode_time_ms) /
349 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200350 outbound_video->total_encoded_bytes_target =
351 video_sender_info.total_encoded_bytes_target;
Henrik Boström9fe18342019-05-16 18:38:20 +0200352 outbound_video->total_packet_send_delay =
353 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
354 rtc::kNumMillisecsPerSec;
Henrik Boström2e069262019-04-09 13:59:31 +0200355 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
356 // optional, support the "unspecified" value.
357 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
358 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700359}
360
hbos02ba2112016-10-28 05:14:53 -0700361void ProduceCertificateStatsFromSSLCertificateStats(
362 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
363 RTCStatsReport* report) {
364 RTCCertificateStats* prev_certificate_stats = nullptr;
365 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
366 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800367 std::string certificate_stats_id =
368 RTCCertificateIDFromFingerprint(s->fingerprint);
369 // It is possible for the same certificate to show up multiple times, e.g.
370 // if local and remote side use the same certificate in a loopback call.
371 // If the report already contains stats for this certificate, skip it.
372 if (report->Get(certificate_stats_id)) {
373 RTC_DCHECK_EQ(s, &certificate_stats);
374 break;
375 }
hbos02ba2112016-10-28 05:14:53 -0700376 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800377 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700378 certificate_stats->fingerprint = s->fingerprint;
379 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
380 certificate_stats->base64_certificate = s->base64_certificate;
381 if (prev_certificate_stats)
382 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
383 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
384 prev_certificate_stats = certificate_stats;
385 }
386}
387
388const std::string& ProduceIceCandidateStats(
389 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800390 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700391 const std::string& id = "RTCIceCandidate_" + candidate.id();
392 const RTCStats* stats = report->Get(id);
393 if (!stats) {
394 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
395 if (is_local)
396 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
397 else
398 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800399 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800400 if (is_local) {
401 candidate_stats->network_type =
402 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200403 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
404 std::string relay_protocol = candidate.relay_protocol();
405 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
406 relay_protocol.compare("tcp") == 0 ||
407 relay_protocol.compare("tls") == 0);
408 candidate_stats->relay_protocol = relay_protocol;
409 }
Gary Liu37e489c2017-11-21 10:49:36 -0800410 } else {
411 // We don't expect to know the adapter type of remote candidates.
412 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
413 }
hbos02ba2112016-10-28 05:14:53 -0700414 candidate_stats->ip = candidate.address().ipaddr().ToString();
415 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
416 candidate_stats->protocol = candidate.protocol();
417 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
418 candidate.type());
419 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
420
421 stats = candidate_stats.get();
422 report->AddStats(std::move(candidate_stats));
423 }
424 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
425 : RTCRemoteIceCandidateStats::kType);
426 return stats->id();
427}
428
hbos9e302742017-01-20 02:47:10 -0800429std::unique_ptr<RTCMediaStreamTrackStats>
430ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
431 int64_t timestamp_us,
432 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100433 const cricket::VoiceSenderInfo& voice_sender_info,
434 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800435 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
436 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100437 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
438 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100439 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800440 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
441 audio_track, audio_track_stats.get());
442 audio_track_stats->remote_source = false;
443 audio_track_stats->detached = false;
444 if (voice_sender_info.audio_level >= 0) {
445 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
446 voice_sender_info.audio_level);
447 }
zsteine76bd3a2017-07-14 12:17:49 -0700448 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
449 audio_track_stats->total_samples_duration =
450 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100451 if (voice_sender_info.apm_statistics.echo_return_loss) {
452 audio_track_stats->echo_return_loss =
453 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800454 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100455 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
456 audio_track_stats->echo_return_loss_enhancement =
457 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800458 }
459 return audio_track_stats;
460}
461
462std::unique_ptr<RTCMediaStreamTrackStats>
463ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
464 int64_t timestamp_us,
465 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100466 const cricket::VoiceReceiverInfo& voice_receiver_info,
467 int attachment_id) {
468 // Since receiver tracks can't be reattached, we use the SSRC as
469 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800470 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
471 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100472 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
473 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100474 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800475 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
476 audio_track, audio_track_stats.get());
477 audio_track_stats->remote_source = true;
478 audio_track_stats->detached = false;
479 if (voice_receiver_info.audio_level >= 0) {
480 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
481 voice_receiver_info.audio_level);
482 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200483 audio_track_stats->jitter_buffer_delay =
484 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100485 audio_track_stats->jitter_buffer_emitted_count =
486 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200487 audio_track_stats->inserted_samples_for_deceleration =
488 voice_receiver_info.inserted_samples_for_deceleration;
489 audio_track_stats->removed_samples_for_acceleration =
490 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 12:17:49 -0700491 audio_track_stats->total_audio_energy =
492 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700493 audio_track_stats->total_samples_received =
494 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700495 audio_track_stats->total_samples_duration =
496 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700497 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200498 audio_track_stats->silent_concealed_samples =
499 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200500 audio_track_stats->concealment_events =
501 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100502 audio_track_stats->jitter_buffer_flushes =
503 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100504 audio_track_stats->delayed_packet_outage_samples =
505 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100506 audio_track_stats->relative_packet_arrival_delay =
507 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200508 audio_track_stats->interruption_count =
509 voice_receiver_info.interruption_count >= 0
510 ? voice_receiver_info.interruption_count
511 : 0;
512 audio_track_stats->total_interruption_duration =
513 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
514 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800515 return audio_track_stats;
516}
517
518std::unique_ptr<RTCMediaStreamTrackStats>
519ProduceMediaStreamTrackStatsFromVideoSenderInfo(
520 int64_t timestamp_us,
521 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100522 const cricket::VideoSenderInfo& video_sender_info,
523 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800524 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
525 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100526 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
527
528 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100529 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800530 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
531 video_track, video_track_stats.get());
532 video_track_stats->remote_source = false;
533 video_track_stats->detached = false;
534 video_track_stats->frame_width = static_cast<uint32_t>(
535 video_sender_info.send_frame_width);
536 video_track_stats->frame_height = static_cast<uint32_t>(
537 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800538 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100539 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800540 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100541 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800542 return video_track_stats;
543}
544
545std::unique_ptr<RTCMediaStreamTrackStats>
546ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
547 int64_t timestamp_us,
548 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100549 const cricket::VideoReceiverInfo& video_receiver_info,
550 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800551 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
552 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100553 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
554
555 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100556 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800557 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
558 video_track, video_track_stats.get());
559 video_track_stats->remote_source = true;
560 video_track_stats->detached = false;
561 if (video_receiver_info.frame_width > 0 &&
562 video_receiver_info.frame_height > 0) {
563 video_track_stats->frame_width = static_cast<uint32_t>(
564 video_receiver_info.frame_width);
565 video_track_stats->frame_height = static_cast<uint32_t>(
566 video_receiver_info.frame_height);
567 }
hbos42f6d2f2017-01-20 03:56:50 -0800568 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800569 // TODO(hbos): When we support receiving simulcast, this should be the total
570 // number of frames correctly decoded, independent of which SSRC it was
571 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100572 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800573 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800574 RTC_DCHECK_GE(video_receiver_info.frames_received,
575 video_receiver_info.frames_rendered);
576 video_track_stats->frames_dropped = video_receiver_info.frames_received -
577 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100578 video_track_stats->freeze_count = video_receiver_info.freeze_count;
579 video_track_stats->pause_count = video_receiver_info.pause_count;
580 video_track_stats->total_freezes_duration =
581 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
582 rtc::kNumMillisecsPerSec;
583 video_track_stats->total_pauses_duration =
584 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
585 rtc::kNumMillisecsPerSec;
586 video_track_stats->total_frames_duration =
587 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
588 rtc::kNumMillisecsPerSec;
589 video_track_stats->sum_squared_frame_durations =
590 video_receiver_info.sum_squared_frame_durations;
591
hbos9e302742017-01-20 02:47:10 -0800592 return video_track_stats;
593}
594
Harald Alvestrand89061872018-01-02 14:08:34 +0100595void ProduceSenderMediaTrackStats(
596 int64_t timestamp_us,
597 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800598 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100599 RTCStatsReport* report) {
600 // This function iterates over the senders to generate outgoing track stats.
601
602 // TODO(hbos): Return stats of detached tracks. We have to perform stats
603 // gathering at the time of detachment to get accurate stats and timestamps.
604 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100605 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100606 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
607 AudioTrackInterface* track =
608 static_cast<AudioTrackInterface*>(sender->track().get());
609 if (!track)
610 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100611 cricket::VoiceSenderInfo null_sender_info;
612 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
613 // TODO(hta): Checking on ssrc is not proper. There should be a way
614 // to see from a sender whether it's connected or not.
615 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800616 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100617 // When pc.close is called, sender info is discarded, so
618 // we generate zeroes instead. Bug: It should be retained.
619 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800620 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100621 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100622 if (sender_info) {
623 voice_sender_info = sender_info;
624 } else {
625 RTC_LOG(LS_INFO)
626 << "RTCStatsCollector: No voice sender info for sender with ssrc "
627 << sender->ssrc();
628 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100629 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100630 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100631 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
632 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100633 report->AddStats(std::move(audio_track_stats));
634 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
635 VideoTrackInterface* track =
636 static_cast<VideoTrackInterface*>(sender->track().get());
637 if (!track)
638 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100639 cricket::VideoSenderInfo null_sender_info;
640 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
641 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100642 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
643 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800644 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100645 // When pc.close is called, sender info is discarded, so
646 // we generate zeroes instead. Bug: It should be retained.
647 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800648 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100649 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100650 if (sender_info) {
651 video_sender_info = sender_info;
652 } else {
653 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
654 << sender->ssrc();
655 }
656 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100657 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100658 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
659 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100660 report->AddStats(std::move(video_track_stats));
661 } else {
662 RTC_NOTREACHED();
663 }
664 }
665}
666
667void ProduceReceiverMediaTrackStats(
668 int64_t timestamp_us,
669 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800670 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100671 RTCStatsReport* report) {
672 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100673 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100674 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
675 AudioTrackInterface* track =
676 static_cast<AudioTrackInterface*>(receiver->track().get());
677 const cricket::VoiceReceiverInfo* voice_receiver_info =
678 track_media_info_map.GetVoiceReceiverInfo(*track);
679 if (!voice_receiver_info) {
680 continue;
681 }
682 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
683 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100684 timestamp_us, *track, *voice_receiver_info,
685 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100686 report->AddStats(std::move(audio_track_stats));
687 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
688 VideoTrackInterface* track =
689 static_cast<VideoTrackInterface*>(receiver->track().get());
690 const cricket::VideoReceiverInfo* video_receiver_info =
691 track_media_info_map.GetVideoReceiverInfo(*track);
692 if (!video_receiver_info) {
693 continue;
694 }
695 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
696 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100697 timestamp_us, *track, *video_receiver_info,
698 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100699 report->AddStats(std::move(video_track_stats));
700 } else {
701 RTC_NOTREACHED();
702 }
703 }
704}
705
Henrik Boström5b3541f2018-03-19 13:52:56 +0100706rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
707 bool filter_by_sender_selector,
708 rtc::scoped_refptr<const RTCStatsReport> report,
709 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
710 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
711 std::vector<std::string> rtpstream_ids;
712 if (filter_by_sender_selector) {
713 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
714 if (sender_selector) {
715 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
716 // reference the sender stats.
717 // Because we do not implement sender stats, we look at outbound-rtp(s)
718 // that reference the track attachment stats for the sender instead.
719 std::string track_id =
720 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
721 kSender, sender_selector->AttachmentId());
722 for (const auto& stats : *report) {
723 if (stats.type() != RTCOutboundRTPStreamStats::kType)
724 continue;
725 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
726 if (outbound_rtp.track_id.is_defined() &&
727 *outbound_rtp.track_id == track_id) {
728 rtpstream_ids.push_back(outbound_rtp.id());
729 }
730 }
731 }
732 } else {
733 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
734 if (receiver_selector) {
735 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
736 // reference the receiver stats.
737 // Because we do not implement receiver stats, we look at inbound-rtp(s)
738 // that reference the track attachment stats for the receiver instead.
739 std::string track_id =
740 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
741 kReceiver, receiver_selector->AttachmentId());
742 for (const auto& stats : *report) {
743 if (stats.type() != RTCInboundRTPStreamStats::kType)
744 continue;
745 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
746 if (inbound_rtp.track_id.is_defined() &&
747 *inbound_rtp.track_id == track_id) {
748 rtpstream_ids.push_back(inbound_rtp.id());
749 }
750 }
751 }
752 }
753 if (rtpstream_ids.empty())
754 return RTCStatsReport::Create(report->timestamp_us());
755 return TakeReferencedStats(report->Copy(), rtpstream_ids);
756}
757
hboscc555c52016-10-18 12:48:31 -0700758} // namespace
759
Henrik Boström5b3541f2018-03-19 13:52:56 +0100760RTCStatsCollector::RequestInfo::RequestInfo(
761 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
762 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
763
764RTCStatsCollector::RequestInfo::RequestInfo(
765 rtc::scoped_refptr<RtpSenderInternal> selector,
766 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
767 : RequestInfo(FilterMode::kSenderSelector,
768 std::move(callback),
769 std::move(selector),
770 nullptr) {}
771
772RTCStatsCollector::RequestInfo::RequestInfo(
773 rtc::scoped_refptr<RtpReceiverInternal> selector,
774 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
775 : RequestInfo(FilterMode::kReceiverSelector,
776 std::move(callback),
777 nullptr,
778 std::move(selector)) {}
779
780RTCStatsCollector::RequestInfo::RequestInfo(
781 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
782 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
783 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
784 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
785 : filter_mode_(filter_mode),
786 callback_(std::move(callback)),
787 sender_selector_(std::move(sender_selector)),
788 receiver_selector_(std::move(receiver_selector)) {
789 RTC_DCHECK(callback_);
790 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
791}
792
hbosc82f2e12016-09-05 01:36:50 -0700793rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800794 PeerConnectionInternal* pc,
795 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700796 return rtc::scoped_refptr<RTCStatsCollector>(
797 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
798}
799
Steve Anton2d8609c2018-01-23 16:38:46 -0800800RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700801 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700802 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700803 signaling_thread_(pc->signaling_thread()),
804 worker_thread_(pc->worker_thread()),
805 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700806 num_pending_partial_reports_(0),
807 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100808 network_report_event_(true /* manual_reset */,
809 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700810 cache_timestamp_us_(0),
811 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700812 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700813 RTC_DCHECK(signaling_thread_);
814 RTC_DCHECK(worker_thread_);
815 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700816 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800817 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800818 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700819}
820
hbosb78306a2016-12-19 05:06:57 -0800821RTCStatsCollector::~RTCStatsCollector() {
822 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
823}
824
hbosc82f2e12016-09-05 01:36:50 -0700825void RTCStatsCollector::GetStatsReport(
826 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100827 GetStatsReportInternal(RequestInfo(std::move(callback)));
828}
829
830void RTCStatsCollector::GetStatsReport(
831 rtc::scoped_refptr<RtpSenderInternal> selector,
832 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
833 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
834}
835
836void RTCStatsCollector::GetStatsReport(
837 rtc::scoped_refptr<RtpReceiverInternal> selector,
838 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
839 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
840}
841
842void RTCStatsCollector::GetStatsReportInternal(
843 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700844 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100845 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700846
hbos0e6758d2016-08-31 07:57:36 -0700847 // "Now" using a monotonically increasing timer.
848 int64_t cache_now_us = rtc::TimeMicros();
849 if (cached_report_ &&
850 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800851 // We have a fresh cached report to deliver. Deliver asynchronously, since
852 // the caller may not be expecting a synchronous callback, and it avoids
853 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100854 std::vector<RequestInfo> requests;
855 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100856 signaling_thread_->PostTask(
857 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
858 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700859 } else if (!num_pending_partial_reports_) {
860 // Only start gathering stats if we're not already gathering stats. In the
861 // case of already gathering stats, |callback_| will be invoked when there
862 // are no more pending partial reports.
863
864 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
865 // UTC), in microseconds. The system clock could be modified and is not
866 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700867 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700868
hbosf415f8a2017-01-02 04:28:51 -0800869 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700870 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800871
Steve Anton57858b32018-02-15 15:19:50 -0800872 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800873 // |ProducePartialResultsOnNetworkThread| and
874 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800875 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700876 // Prepare |transport_names_| for use in
877 // |ProducePartialResultsOnNetworkThread|.
878 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800879
stefanf79ade12017-06-02 06:44:03 -0700880 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
881 // thread.
882 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
883 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700884 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700885
Henrik Boström40b030e2019-02-28 09:49:31 +0100886 // Don't touch |network_report_| on the signaling thread until
887 // ProducePartialResultsOnNetworkThread() has signaled the
888 // |network_report_event_|.
889 network_report_event_.Reset();
890 network_thread_->PostTask(
891 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700892 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100893 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800894 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700895 }
hbosd565b732016-08-30 14:04:35 -0700896}
897
898void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700899 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700900 cached_report_ = nullptr;
901}
902
hbosb78306a2016-12-19 05:06:57 -0800903void RTCStatsCollector::WaitForPendingRequest() {
904 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100905 // If a request is pending, blocks until the |network_report_event_| is
906 // signaled and then delivers the result. Otherwise this is a NO-OP.
907 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800908}
909
hbosc82f2e12016-09-05 01:36:50 -0700910void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
911 int64_t timestamp_us) {
912 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100913 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700914
Henrik Boström40b030e2019-02-28 09:49:31 +0100915 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
916 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700917
Henrik Boström40b030e2019-02-28 09:49:31 +0100918 // ProducePartialResultsOnSignalingThread() is running synchronously on the
919 // signaling thread, so it is always the first partial result delivered on the
920 // signaling thread. The request is not complete until MergeNetworkReport_s()
921 // happens; we don't have to do anything here.
922 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
923 --num_pending_partial_reports_;
924}
925
926void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
927 int64_t timestamp_us,
928 RTCStatsReport* partial_report) {
929 RTC_DCHECK(signaling_thread_->IsCurrent());
930 ProduceDataChannelStats_s(timestamp_us, partial_report);
931 ProduceMediaStreamStats_s(timestamp_us, partial_report);
932 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
933 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700934}
935
hbosc82f2e12016-09-05 01:36:50 -0700936void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
937 int64_t timestamp_us) {
938 RTC_DCHECK(network_thread_->IsCurrent());
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200939 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 09:49:31 +0100940 // |network_report_event_| is reset before this method is invoked.
941 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700942
Steve Anton5dfde182018-02-06 10:34:40 -0800943 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700944 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800945 std::map<std::string, CertificateStatsPair> transport_cert_stats =
946 PrepareTransportCertificateStats_n(transport_stats_by_name);
947
Henrik Boström40b030e2019-02-28 09:49:31 +0100948 ProducePartialResultsOnNetworkThreadImpl(
949 timestamp_us, transport_stats_by_name, transport_cert_stats,
950 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000951
Henrik Boström40b030e2019-02-28 09:49:31 +0100952 // Signal that it is now safe to touch |network_report_| on the signaling
953 // thread, and post a task to merge it into the final results.
954 network_report_event_.Set();
955 signaling_thread_->PostTask(
956 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100957}
958
Henrik Boström40b030e2019-02-28 09:49:31 +0100959void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
960 int64_t timestamp_us,
961 const std::map<std::string, cricket::TransportStats>&
962 transport_stats_by_name,
963 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
964 RTCStatsReport* partial_report) {
965 RTC_DCHECK(network_thread_->IsCurrent());
966 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
967 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
968 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
969 call_stats_, partial_report);
970 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
971 partial_report);
972 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
973 transport_cert_stats, partial_report);
974}
975
976void RTCStatsCollector::MergeNetworkReport_s() {
977 RTC_DCHECK(signaling_thread_->IsCurrent());
978 // The |network_report_event_| must be signaled for it to be safe to touch
979 // |network_report_|. This is normally not blocking, but if
980 // WaitForPendingRequest() is called while a request is pending, we might have
981 // to wait until the network thread is done touching |network_report_|.
982 network_report_event_.Wait(rtc::Event::kForever);
983 if (!network_report_) {
984 // Normally, MergeNetworkReport_s() is executed because it is posted from
985 // the network thread. But if WaitForPendingRequest() is called while a
986 // request is pending, an early call to MergeNetworkReport_s() is made,
987 // merging the report and setting |network_report_| to null. If so, when the
988 // previously posted MergeNetworkReport_s() is later executed, the report is
989 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700990 return;
991 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000992 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100993 RTC_DCHECK(partial_report_);
994 partial_report_->TakeMembersFrom(network_report_);
995 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000996 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100997 // |network_report_| is currently the only partial report collected
998 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
999 // ready to deliver the result.
1000 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1001 cache_timestamp_us_ = partial_report_timestamp_us_;
1002 cached_report_ = partial_report_;
1003 partial_report_ = nullptr;
1004 transceiver_stats_infos_.clear();
1005 // Trace WebRTC Stats when getStats is called on Javascript.
1006 // This allows access to WebRTC stats from trace logs. To enable them,
1007 // select the "webrtc_stats" category when recording traces.
1008 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1009 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001010
Henrik Boström40b030e2019-02-28 09:49:31 +01001011 // Deliver report and clear |requests_|.
1012 std::vector<RequestInfo> requests;
1013 requests.swap(requests_);
1014 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001015}
1016
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001017void RTCStatsCollector::DeliverCachedReport(
1018 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001019 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001020 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001021 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001022 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001023
Henrik Boström5b3541f2018-03-19 13:52:56 +01001024 for (const RequestInfo& request : requests) {
1025 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1026 request.callback()->OnStatsDelivered(cached_report);
1027 } else {
1028 bool filter_by_sender_selector;
1029 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1030 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1031 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1032 filter_by_sender_selector = true;
1033 sender_selector = request.sender_selector();
1034 } else {
1035 RTC_DCHECK(request.filter_mode() ==
1036 RequestInfo::FilterMode::kReceiverSelector);
1037 filter_by_sender_selector = false;
1038 receiver_selector = request.receiver_selector();
1039 }
1040 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1041 filter_by_sender_selector, cached_report, sender_selector,
1042 receiver_selector));
1043 }
hbosc82f2e12016-09-05 01:36:50 -07001044 }
hbosd565b732016-08-30 14:04:35 -07001045}
1046
hbosdf6075a2016-12-19 04:58:02 -08001047void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001048 int64_t timestamp_us,
1049 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001050 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001051 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001052 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1053 if (transport_cert_stats_pair.second.local) {
1054 ProduceCertificateStatsFromSSLCertificateStats(
1055 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001056 }
hbos02ba2112016-10-28 05:14:53 -07001057 if (transport_cert_stats_pair.second.remote) {
1058 ProduceCertificateStatsFromSSLCertificateStats(
1059 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001060 }
1061 }
1062}
1063
hbosdf6075a2016-12-19 04:58:02 -08001064void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001065 int64_t timestamp_us,
1066 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001067 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001068 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001069 for (const auto& stats : transceiver_stats_infos) {
1070 if (!stats.mid) {
1071 continue;
hbos0adb8282016-11-23 02:32:06 -08001072 }
Steve Anton57858b32018-02-15 15:19:50 -08001073 const cricket::VoiceMediaInfo* voice_media_info =
1074 stats.track_media_info_map->voice_media_info();
1075 const cricket::VideoMediaInfo* video_media_info =
1076 stats.track_media_info_map->video_media_info();
1077 // Audio
1078 if (voice_media_info) {
1079 // Inbound
1080 for (const auto& pair : voice_media_info->receive_codecs) {
1081 report->AddStats(CodecStatsFromRtpCodecParameters(
1082 timestamp_us, *stats.mid, true, pair.second));
1083 }
1084 // Outbound
1085 for (const auto& pair : voice_media_info->send_codecs) {
1086 report->AddStats(CodecStatsFromRtpCodecParameters(
1087 timestamp_us, *stats.mid, false, pair.second));
1088 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001089 }
Steve Anton57858b32018-02-15 15:19:50 -08001090 // Video
1091 if (video_media_info) {
1092 // Inbound
1093 for (const auto& pair : video_media_info->receive_codecs) {
1094 report->AddStats(CodecStatsFromRtpCodecParameters(
1095 timestamp_us, *stats.mid, true, pair.second));
1096 }
1097 // Outbound
1098 for (const auto& pair : video_media_info->send_codecs) {
1099 report->AddStats(CodecStatsFromRtpCodecParameters(
1100 timestamp_us, *stats.mid, false, pair.second));
1101 }
hbos0adb8282016-11-23 02:32:06 -08001102 }
1103 }
1104}
1105
hboscc555c52016-10-18 12:48:31 -07001106void RTCStatsCollector::ProduceDataChannelStats_s(
1107 int64_t timestamp_us, RTCStatsReport* report) const {
1108 RTC_DCHECK(signaling_thread_->IsCurrent());
1109 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1110 pc_->sctp_data_channels()) {
1111 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1112 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001113 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001114 timestamp_us));
1115 data_channel_stats->label = data_channel->label();
1116 data_channel_stats->protocol = data_channel->protocol();
1117 data_channel_stats->datachannelid = data_channel->id();
1118 data_channel_stats->state =
1119 DataStateToRTCDataChannelState(data_channel->state());
1120 data_channel_stats->messages_sent = data_channel->messages_sent();
1121 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1122 data_channel_stats->messages_received = data_channel->messages_received();
1123 data_channel_stats->bytes_received = data_channel->bytes_received();
1124 report->AddStats(std::move(data_channel_stats));
1125 }
1126}
1127
hbosdf6075a2016-12-19 04:58:02 -08001128void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001129 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001130 const std::map<std::string, cricket::TransportStats>&
1131 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001132 const Call::Stats& call_stats,
1133 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001134 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001135 for (const auto& entry : transport_stats_by_name) {
1136 const std::string& transport_name = entry.first;
1137 const cricket::TransportStats& transport_stats = entry.second;
1138 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001139 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001140 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001141 for (const cricket::ConnectionInfo& info :
1142 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001143 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001144 new RTCIceCandidatePairStats(
1145 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1146 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001147
hbos0583b282016-11-30 01:50:14 -08001148 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001149 // TODO(hbos): There could be other candidates that are not paired with
1150 // anything. We don't have a complete list. Local candidates come from
1151 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001152 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001153 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001154 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001155 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001156 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001157 candidate_pair_stats->state =
1158 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1159 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001160 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001161 // TODO(hbos): This writable is different than the spec. It goes to
1162 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001163 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001164 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001165 candidate_pair_stats->bytes_sent =
1166 static_cast<uint64_t>(info.sent_total_bytes);
1167 candidate_pair_stats->bytes_received =
1168 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001169 candidate_pair_stats->total_round_trip_time =
1170 static_cast<double>(info.total_round_trip_time_ms) /
1171 rtc::kNumMillisecsPerSec;
1172 if (info.current_round_trip_time_ms) {
1173 candidate_pair_stats->current_round_trip_time =
1174 static_cast<double>(*info.current_round_trip_time_ms) /
1175 rtc::kNumMillisecsPerSec;
1176 }
stefanf79ade12017-06-02 06:44:03 -07001177 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001178 // The bandwidth estimations we have are for the selected candidate
1179 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001180 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1181 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1182 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001183 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001184 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001185 }
stefanf79ade12017-06-02 06:44:03 -07001186 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001187 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001188 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001189 }
1190 }
hbosd82f5122016-12-09 04:12:39 -08001191 candidate_pair_stats->requests_received =
1192 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001193 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1194 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001195 candidate_pair_stats->responses_received =
1196 static_cast<uint64_t>(info.recv_ping_responses);
1197 candidate_pair_stats->responses_sent =
1198 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001199 RTC_DCHECK_GE(info.sent_ping_requests_total,
1200 info.sent_ping_requests_before_first_response);
1201 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1202 info.sent_ping_requests_total -
1203 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001204
1205 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001206 }
1207 }
1208 }
1209}
1210
Steve Anton57858b32018-02-15 15:19:50 -08001211void RTCStatsCollector::ProduceMediaStreamStats_s(
1212 int64_t timestamp_us,
1213 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001214 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001215
1216 std::map<std::string, std::vector<std::string>> track_ids;
1217
1218 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001219 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001220 std::string track_id =
1221 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1222 kSender, sender->internal()->AttachmentId());
1223 for (auto& stream_id : sender->stream_ids()) {
1224 track_ids[stream_id].push_back(track_id);
1225 }
1226 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001227 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001228 std::string track_id =
1229 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1230 kReceiver, receiver->internal()->AttachmentId());
1231 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001232 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001233 }
1234 }
1235 }
1236
1237 // Build stats for each stream ID known.
1238 for (auto& it : track_ids) {
1239 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1240 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1241 stream_stats->stream_identifier = it.first;
1242 stream_stats->track_ids = it.second;
1243 report->AddStats(std::move(stream_stats));
1244 }
1245}
1246
1247void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1248 int64_t timestamp_us,
1249 RTCStatsReport* report) const {
1250 RTC_DCHECK(signaling_thread_->IsCurrent());
1251 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1252 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001253 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001254 senders.push_back(sender->internal());
1255 }
1256 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1257 senders, report);
1258
1259 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001260 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001261 receivers.push_back(receiver->internal());
1262 }
1263 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1264 receivers, report);
1265 }
hbos09bc1282016-11-08 06:29:22 -08001266}
1267
hbos6ab97ce2016-10-03 14:16:56 -07001268void RTCStatsCollector::ProducePeerConnectionStats_s(
1269 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001270 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001271 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001272 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001273 stats->data_channels_opened = internal_record_.data_channels_opened;
1274 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001275 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001276}
1277
hbosdf6075a2016-12-19 04:58:02 -08001278void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001279 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001280 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001281 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001282 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001283
Steve Anton57858b32018-02-15 15:19:50 -08001284 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1285 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1286 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1287 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1288 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1289 } else {
1290 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001291 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001292 }
Steve Anton57858b32018-02-15 15:19:50 -08001293}
1294
1295void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1296 int64_t timestamp_us,
1297 const RtpTransceiverStatsInfo& stats,
1298 RTCStatsReport* report) const {
1299 if (!stats.mid || !stats.transport_name) {
1300 return;
1301 }
1302 RTC_DCHECK(stats.track_media_info_map);
1303 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1304 RTC_DCHECK(track_media_info_map.voice_media_info());
1305 std::string mid = *stats.mid;
1306 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1307 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1308 // Inbound
1309 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1310 track_media_info_map.voice_media_info()->receivers) {
1311 if (!voice_receiver_info.connected())
1312 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001313 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001314 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1315 timestamp_us);
1316 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1317 inbound_audio.get());
1318 // TODO(hta): This lookup should look for the sender, not the track.
1319 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1320 track_media_info_map.GetAudioTrack(voice_receiver_info);
1321 if (audio_track) {
1322 inbound_audio->track_id =
1323 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1324 kReceiver,
1325 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001326 }
Steve Anton57858b32018-02-15 15:19:50 -08001327 inbound_audio->transport_id = transport_id;
1328 report->AddStats(std::move(inbound_audio));
1329 }
1330 // Outbound
1331 for (const cricket::VoiceSenderInfo& voice_sender_info :
1332 track_media_info_map.voice_media_info()->senders) {
1333 if (!voice_sender_info.connected())
1334 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001335 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001336 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1337 timestamp_us);
1338 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1339 outbound_audio.get());
1340 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1341 track_media_info_map.GetAudioTrack(voice_sender_info);
1342 if (audio_track) {
1343 outbound_audio->track_id =
1344 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1345 kSender,
1346 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001347 }
Steve Anton57858b32018-02-15 15:19:50 -08001348 outbound_audio->transport_id = transport_id;
1349 report->AddStats(std::move(outbound_audio));
1350 }
1351}
1352
1353void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1354 int64_t timestamp_us,
1355 const RtpTransceiverStatsInfo& stats,
1356 RTCStatsReport* report) const {
1357 if (!stats.mid || !stats.transport_name) {
1358 return;
1359 }
1360 RTC_DCHECK(stats.track_media_info_map);
1361 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1362 RTC_DCHECK(track_media_info_map.video_media_info());
1363 std::string mid = *stats.mid;
1364 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1365 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1366 // Inbound
1367 for (const cricket::VideoReceiverInfo& video_receiver_info :
1368 track_media_info_map.video_media_info()->receivers) {
1369 if (!video_receiver_info.connected())
1370 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001371 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001372 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1373 timestamp_us);
1374 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1375 inbound_video.get());
1376 rtc::scoped_refptr<VideoTrackInterface> video_track =
1377 track_media_info_map.GetVideoTrack(video_receiver_info);
1378 if (video_track) {
1379 inbound_video->track_id =
1380 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1381 kReceiver,
1382 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1383 }
1384 inbound_video->transport_id = transport_id;
1385 report->AddStats(std::move(inbound_video));
1386 }
1387 // Outbound
1388 for (const cricket::VideoSenderInfo& video_sender_info :
1389 track_media_info_map.video_media_info()->senders) {
1390 if (!video_sender_info.connected())
1391 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001392 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001393 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1394 timestamp_us);
1395 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1396 outbound_video.get());
1397 rtc::scoped_refptr<VideoTrackInterface> video_track =
1398 track_media_info_map.GetVideoTrack(video_sender_info);
1399 if (video_track) {
1400 outbound_video->track_id =
1401 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1402 kSender,
1403 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1404 }
1405 outbound_video->transport_id = transport_id;
1406 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001407 }
1408}
1409
hbosdf6075a2016-12-19 04:58:02 -08001410void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001411 int64_t timestamp_us,
1412 const std::map<std::string, cricket::TransportStats>&
1413 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001414 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1415 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001416 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001417 for (const auto& entry : transport_stats_by_name) {
1418 const std::string& transport_name = entry.first;
1419 const cricket::TransportStats& transport_stats = entry.second;
1420
hbos2fa7c672016-10-24 04:00:05 -07001421 // Get reference to RTCP channel, if it exists.
1422 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001423 for (const cricket::TransportChannelStats& channel_stats :
1424 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001425 if (channel_stats.component ==
1426 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1427 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001428 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001429 break;
1430 }
1431 }
1432
1433 // Get reference to local and remote certificates of this transport, if they
1434 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001435 const auto& certificate_stats_it =
1436 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001437 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1438 std::string local_certificate_id;
1439 if (certificate_stats_it->second.local) {
1440 local_certificate_id = RTCCertificateIDFromFingerprint(
1441 certificate_stats_it->second.local->fingerprint);
1442 }
1443 std::string remote_certificate_id;
1444 if (certificate_stats_it->second.remote) {
1445 remote_certificate_id = RTCCertificateIDFromFingerprint(
1446 certificate_stats_it->second.remote->fingerprint);
1447 }
1448
1449 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001450 for (const cricket::TransportChannelStats& channel_stats :
1451 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001452 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001453 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1454 transport_name, channel_stats.component),
1455 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001456 transport_stats->bytes_sent = 0;
1457 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001458 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1459 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001460 for (const cricket::ConnectionInfo& info :
1461 channel_stats.connection_infos) {
1462 *transport_stats->bytes_sent += info.sent_total_bytes;
1463 *transport_stats->bytes_received += info.recv_total_bytes;
1464 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001465 transport_stats->selected_candidate_pair_id =
1466 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1467 }
1468 }
1469 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1470 !rtcp_transport_stats_id.empty()) {
1471 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1472 }
1473 if (!local_certificate_id.empty())
1474 transport_stats->local_certificate_id = local_certificate_id;
1475 if (!remote_certificate_id.empty())
1476 transport_stats->remote_certificate_id = remote_certificate_id;
1477 report->AddStats(std::move(transport_stats));
1478 }
1479 }
1480}
1481
1482std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001483RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001484 const std::map<std::string, cricket::TransportStats>&
1485 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001486 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001487 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001488 for (const auto& entry : transport_stats_by_name) {
1489 const std::string& transport_name = entry.first;
1490
hbos2fa7c672016-10-24 04:00:05 -07001491 CertificateStatsPair certificate_stats_pair;
1492 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001493 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001494 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001495 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001496 }
Steve Anton5dfde182018-02-06 10:34:40 -08001497
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001498 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1499 pc_->GetRemoteSSLCertChain(transport_name);
1500 if (remote_cert_chain) {
1501 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001502 }
Steve Anton5dfde182018-02-06 10:34:40 -08001503
hbos2fa7c672016-10-24 04:00:05 -07001504 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001505 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001506 }
1507 return transport_cert_stats;
1508}
1509
Steve Anton57858b32018-02-15 15:19:50 -08001510std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1511RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1512 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001513
Steve Anton57858b32018-02-15 15:19:50 -08001514 // These are used to invoke GetStats for all the media channels together in
1515 // one worker thread hop.
1516 std::map<cricket::VoiceMediaChannel*,
1517 std::unique_ptr<cricket::VoiceMediaInfo>>
1518 voice_stats;
1519 std::map<cricket::VideoMediaChannel*,
1520 std::unique_ptr<cricket::VideoMediaInfo>>
1521 video_stats;
1522
Mirko Bonadei739baf02019-01-27 17:29:42 +01001523 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001524 cricket::MediaType media_type = transceiver->media_type();
1525
1526 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1527 // stats have been fetched on the worker thread.
1528 transceiver_stats_infos.emplace_back();
1529 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1530 stats.transceiver = transceiver->internal();
1531 stats.media_type = media_type;
1532
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001533 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001534 if (!channel) {
1535 // The remaining fields require a BaseChannel.
1536 continue;
1537 }
1538
1539 stats.mid = channel->content_name();
1540 stats.transport_name = channel->transport_name();
1541
1542 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1543 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1544 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1545 voice_stats.end());
1546 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001547 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001548 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1549 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1550 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1551 video_stats.end());
1552 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001553 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001554 } else {
1555 RTC_NOTREACHED();
1556 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001557 }
Steve Anton57858b32018-02-15 15:19:50 -08001558
1559 // Call GetStats for all media channels together on the worker thread in one
1560 // hop.
1561 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1562 for (const auto& entry : voice_stats) {
1563 if (!entry.first->GetStats(entry.second.get())) {
1564 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1565 }
1566 }
1567 for (const auto& entry : video_stats) {
1568 if (!entry.first->GetStats(entry.second.get())) {
1569 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1570 }
1571 }
1572 });
1573
1574 // Create the TrackMediaInfoMap for each transceiver stats object.
1575 for (auto& stats : transceiver_stats_infos) {
1576 auto transceiver = stats.transceiver;
1577 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1578 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1579 if (transceiver->channel()) {
1580 cricket::MediaType media_type = transceiver->media_type();
1581 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1582 auto* voice_channel =
1583 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1584 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1585 voice_media_info =
1586 std::move(voice_stats[voice_channel->media_channel()]);
1587 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1588 auto* video_channel =
1589 static_cast<cricket::VideoChannel*>(transceiver->channel());
1590 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1591 video_media_info =
1592 std::move(video_stats[video_channel->media_channel()]);
1593 }
1594 }
1595 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001596 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001597 senders.push_back(sender->internal());
1598 }
1599 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001600 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001601 receivers.push_back(receiver->internal());
1602 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001603 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001604 std::move(voice_media_info), std::move(video_media_info), senders,
1605 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001606 }
Steve Anton57858b32018-02-15 15:19:50 -08001607
1608 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001609}
1610
Steve Anton7eca0932018-03-30 15:18:41 -07001611std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1612 std::set<std::string> transport_names;
1613 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1614 if (transceiver->internal()->channel()) {
1615 transport_names.insert(
1616 transceiver->internal()->channel()->transport_name());
1617 }
1618 }
1619 if (pc_->rtp_data_channel()) {
1620 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1621 }
1622 if (pc_->sctp_transport_name()) {
1623 transport_names.insert(*pc_->sctp_transport_name());
1624 }
1625 return transport_names;
1626}
1627
hbos82ebe022016-11-14 01:41:09 -08001628void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1629 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1630 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1631}
1632
1633void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1634 RTC_DCHECK(signaling_thread_->IsCurrent());
1635 bool result = internal_record_.opened_data_channels.insert(
1636 reinterpret_cast<uintptr_t>(channel)).second;
1637 ++internal_record_.data_channels_opened;
1638 RTC_DCHECK(result);
1639}
1640
1641void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1642 RTC_DCHECK(signaling_thread_->IsCurrent());
1643 // Only channels that have been fully opened (and have increased the
1644 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001645 if (internal_record_.opened_data_channels.erase(
1646 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001647 ++internal_record_.data_channels_closed;
1648 }
1649}
1650
hboscc555c52016-10-18 12:48:31 -07001651const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1652 const std::string& type) {
1653 return CandidateTypeToRTCIceCandidateType(type);
1654}
1655
1656const char* DataStateToRTCDataChannelStateForTesting(
1657 DataChannelInterface::DataState state) {
1658 return DataStateToRTCDataChannelState(state);
1659}
1660
hbosd565b732016-08-30 14:04:35 -07001661} // namespace webrtc