blob: 0d1fbba25a24617825ca3b0884ab7c077ba28ae2 [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öm2e069262019-04-09 13:59:31 +0200350 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
351 // optional, support the "unspecified" value.
352 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
353 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700354}
355
hbos02ba2112016-10-28 05:14:53 -0700356void ProduceCertificateStatsFromSSLCertificateStats(
357 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
358 RTCStatsReport* report) {
359 RTCCertificateStats* prev_certificate_stats = nullptr;
360 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
361 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800362 std::string certificate_stats_id =
363 RTCCertificateIDFromFingerprint(s->fingerprint);
364 // It is possible for the same certificate to show up multiple times, e.g.
365 // if local and remote side use the same certificate in a loopback call.
366 // If the report already contains stats for this certificate, skip it.
367 if (report->Get(certificate_stats_id)) {
368 RTC_DCHECK_EQ(s, &certificate_stats);
369 break;
370 }
hbos02ba2112016-10-28 05:14:53 -0700371 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800372 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700373 certificate_stats->fingerprint = s->fingerprint;
374 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
375 certificate_stats->base64_certificate = s->base64_certificate;
376 if (prev_certificate_stats)
377 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
378 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
379 prev_certificate_stats = certificate_stats;
380 }
381}
382
383const std::string& ProduceIceCandidateStats(
384 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800385 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700386 const std::string& id = "RTCIceCandidate_" + candidate.id();
387 const RTCStats* stats = report->Get(id);
388 if (!stats) {
389 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
390 if (is_local)
391 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
392 else
393 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800394 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800395 if (is_local) {
396 candidate_stats->network_type =
397 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200398 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
399 std::string relay_protocol = candidate.relay_protocol();
400 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
401 relay_protocol.compare("tcp") == 0 ||
402 relay_protocol.compare("tls") == 0);
403 candidate_stats->relay_protocol = relay_protocol;
404 }
Gary Liu37e489c2017-11-21 10:49:36 -0800405 } else {
406 // We don't expect to know the adapter type of remote candidates.
407 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
408 }
hbos02ba2112016-10-28 05:14:53 -0700409 candidate_stats->ip = candidate.address().ipaddr().ToString();
410 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
411 candidate_stats->protocol = candidate.protocol();
412 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
413 candidate.type());
414 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
415
416 stats = candidate_stats.get();
417 report->AddStats(std::move(candidate_stats));
418 }
419 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
420 : RTCRemoteIceCandidateStats::kType);
421 return stats->id();
422}
423
hbos9e302742017-01-20 02:47:10 -0800424std::unique_ptr<RTCMediaStreamTrackStats>
425ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
426 int64_t timestamp_us,
427 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100428 const cricket::VoiceSenderInfo& voice_sender_info,
429 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800430 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
431 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100432 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
433 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100434 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800435 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
436 audio_track, audio_track_stats.get());
437 audio_track_stats->remote_source = false;
438 audio_track_stats->detached = false;
439 if (voice_sender_info.audio_level >= 0) {
440 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
441 voice_sender_info.audio_level);
442 }
zsteine76bd3a2017-07-14 12:17:49 -0700443 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
444 audio_track_stats->total_samples_duration =
445 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100446 if (voice_sender_info.apm_statistics.echo_return_loss) {
447 audio_track_stats->echo_return_loss =
448 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800449 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100450 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
451 audio_track_stats->echo_return_loss_enhancement =
452 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800453 }
454 return audio_track_stats;
455}
456
457std::unique_ptr<RTCMediaStreamTrackStats>
458ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
459 int64_t timestamp_us,
460 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100461 const cricket::VoiceReceiverInfo& voice_receiver_info,
462 int attachment_id) {
463 // Since receiver tracks can't be reattached, we use the SSRC as
464 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800465 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
466 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100467 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
468 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100469 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800470 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
471 audio_track, audio_track_stats.get());
472 audio_track_stats->remote_source = true;
473 audio_track_stats->detached = false;
474 if (voice_receiver_info.audio_level >= 0) {
475 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
476 voice_receiver_info.audio_level);
477 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200478 audio_track_stats->jitter_buffer_delay =
479 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100480 audio_track_stats->jitter_buffer_emitted_count =
481 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200482 audio_track_stats->inserted_samples_for_deceleration =
483 voice_receiver_info.inserted_samples_for_deceleration;
484 audio_track_stats->removed_samples_for_acceleration =
485 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 12:17:49 -0700486 audio_track_stats->total_audio_energy =
487 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700488 audio_track_stats->total_samples_received =
489 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700490 audio_track_stats->total_samples_duration =
491 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700492 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200493 audio_track_stats->silent_concealed_samples =
494 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200495 audio_track_stats->concealment_events =
496 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100497 audio_track_stats->jitter_buffer_flushes =
498 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100499 audio_track_stats->delayed_packet_outage_samples =
500 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100501 audio_track_stats->relative_packet_arrival_delay =
502 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200503 audio_track_stats->interruption_count =
504 voice_receiver_info.interruption_count >= 0
505 ? voice_receiver_info.interruption_count
506 : 0;
507 audio_track_stats->total_interruption_duration =
508 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
509 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800510 return audio_track_stats;
511}
512
513std::unique_ptr<RTCMediaStreamTrackStats>
514ProduceMediaStreamTrackStatsFromVideoSenderInfo(
515 int64_t timestamp_us,
516 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100517 const cricket::VideoSenderInfo& video_sender_info,
518 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800519 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
520 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100521 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
522
523 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100524 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800525 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
526 video_track, video_track_stats.get());
527 video_track_stats->remote_source = false;
528 video_track_stats->detached = false;
529 video_track_stats->frame_width = static_cast<uint32_t>(
530 video_sender_info.send_frame_width);
531 video_track_stats->frame_height = static_cast<uint32_t>(
532 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800533 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100534 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800535 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100536 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800537 return video_track_stats;
538}
539
540std::unique_ptr<RTCMediaStreamTrackStats>
541ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
542 int64_t timestamp_us,
543 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100544 const cricket::VideoReceiverInfo& video_receiver_info,
545 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800546 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
547 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100548 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
549
550 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100551 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800552 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
553 video_track, video_track_stats.get());
554 video_track_stats->remote_source = true;
555 video_track_stats->detached = false;
556 if (video_receiver_info.frame_width > 0 &&
557 video_receiver_info.frame_height > 0) {
558 video_track_stats->frame_width = static_cast<uint32_t>(
559 video_receiver_info.frame_width);
560 video_track_stats->frame_height = static_cast<uint32_t>(
561 video_receiver_info.frame_height);
562 }
hbos42f6d2f2017-01-20 03:56:50 -0800563 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800564 // TODO(hbos): When we support receiving simulcast, this should be the total
565 // number of frames correctly decoded, independent of which SSRC it was
566 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100567 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800568 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800569 RTC_DCHECK_GE(video_receiver_info.frames_received,
570 video_receiver_info.frames_rendered);
571 video_track_stats->frames_dropped = video_receiver_info.frames_received -
572 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100573 video_track_stats->freeze_count = video_receiver_info.freeze_count;
574 video_track_stats->pause_count = video_receiver_info.pause_count;
575 video_track_stats->total_freezes_duration =
576 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
577 rtc::kNumMillisecsPerSec;
578 video_track_stats->total_pauses_duration =
579 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
580 rtc::kNumMillisecsPerSec;
581 video_track_stats->total_frames_duration =
582 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
583 rtc::kNumMillisecsPerSec;
584 video_track_stats->sum_squared_frame_durations =
585 video_receiver_info.sum_squared_frame_durations;
586
hbos9e302742017-01-20 02:47:10 -0800587 return video_track_stats;
588}
589
Harald Alvestrand89061872018-01-02 14:08:34 +0100590void ProduceSenderMediaTrackStats(
591 int64_t timestamp_us,
592 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800593 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100594 RTCStatsReport* report) {
595 // This function iterates over the senders to generate outgoing track stats.
596
597 // TODO(hbos): Return stats of detached tracks. We have to perform stats
598 // gathering at the time of detachment to get accurate stats and timestamps.
599 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100600 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100601 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
602 AudioTrackInterface* track =
603 static_cast<AudioTrackInterface*>(sender->track().get());
604 if (!track)
605 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100606 cricket::VoiceSenderInfo null_sender_info;
607 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
608 // TODO(hta): Checking on ssrc is not proper. There should be a way
609 // to see from a sender whether it's connected or not.
610 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800611 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100612 // When pc.close is called, sender info is discarded, so
613 // we generate zeroes instead. Bug: It should be retained.
614 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800615 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100616 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100617 if (sender_info) {
618 voice_sender_info = sender_info;
619 } else {
620 RTC_LOG(LS_INFO)
621 << "RTCStatsCollector: No voice sender info for sender with ssrc "
622 << sender->ssrc();
623 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100624 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100625 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100626 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
627 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100628 report->AddStats(std::move(audio_track_stats));
629 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
630 VideoTrackInterface* track =
631 static_cast<VideoTrackInterface*>(sender->track().get());
632 if (!track)
633 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100634 cricket::VideoSenderInfo null_sender_info;
635 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
636 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100637 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
638 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800639 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100640 // When pc.close is called, sender info is discarded, so
641 // we generate zeroes instead. Bug: It should be retained.
642 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800643 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100644 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100645 if (sender_info) {
646 video_sender_info = sender_info;
647 } else {
648 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
649 << sender->ssrc();
650 }
651 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100652 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100653 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
654 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100655 report->AddStats(std::move(video_track_stats));
656 } else {
657 RTC_NOTREACHED();
658 }
659 }
660}
661
662void ProduceReceiverMediaTrackStats(
663 int64_t timestamp_us,
664 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800665 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100666 RTCStatsReport* report) {
667 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100668 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100669 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
670 AudioTrackInterface* track =
671 static_cast<AudioTrackInterface*>(receiver->track().get());
672 const cricket::VoiceReceiverInfo* voice_receiver_info =
673 track_media_info_map.GetVoiceReceiverInfo(*track);
674 if (!voice_receiver_info) {
675 continue;
676 }
677 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
678 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100679 timestamp_us, *track, *voice_receiver_info,
680 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100681 report->AddStats(std::move(audio_track_stats));
682 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
683 VideoTrackInterface* track =
684 static_cast<VideoTrackInterface*>(receiver->track().get());
685 const cricket::VideoReceiverInfo* video_receiver_info =
686 track_media_info_map.GetVideoReceiverInfo(*track);
687 if (!video_receiver_info) {
688 continue;
689 }
690 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
691 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100692 timestamp_us, *track, *video_receiver_info,
693 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100694 report->AddStats(std::move(video_track_stats));
695 } else {
696 RTC_NOTREACHED();
697 }
698 }
699}
700
Henrik Boström5b3541f2018-03-19 13:52:56 +0100701rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
702 bool filter_by_sender_selector,
703 rtc::scoped_refptr<const RTCStatsReport> report,
704 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
705 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
706 std::vector<std::string> rtpstream_ids;
707 if (filter_by_sender_selector) {
708 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
709 if (sender_selector) {
710 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
711 // reference the sender stats.
712 // Because we do not implement sender stats, we look at outbound-rtp(s)
713 // that reference the track attachment stats for the sender instead.
714 std::string track_id =
715 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
716 kSender, sender_selector->AttachmentId());
717 for (const auto& stats : *report) {
718 if (stats.type() != RTCOutboundRTPStreamStats::kType)
719 continue;
720 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
721 if (outbound_rtp.track_id.is_defined() &&
722 *outbound_rtp.track_id == track_id) {
723 rtpstream_ids.push_back(outbound_rtp.id());
724 }
725 }
726 }
727 } else {
728 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
729 if (receiver_selector) {
730 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
731 // reference the receiver stats.
732 // Because we do not implement receiver stats, we look at inbound-rtp(s)
733 // that reference the track attachment stats for the receiver instead.
734 std::string track_id =
735 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
736 kReceiver, receiver_selector->AttachmentId());
737 for (const auto& stats : *report) {
738 if (stats.type() != RTCInboundRTPStreamStats::kType)
739 continue;
740 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
741 if (inbound_rtp.track_id.is_defined() &&
742 *inbound_rtp.track_id == track_id) {
743 rtpstream_ids.push_back(inbound_rtp.id());
744 }
745 }
746 }
747 }
748 if (rtpstream_ids.empty())
749 return RTCStatsReport::Create(report->timestamp_us());
750 return TakeReferencedStats(report->Copy(), rtpstream_ids);
751}
752
hboscc555c52016-10-18 12:48:31 -0700753} // namespace
754
Henrik Boström5b3541f2018-03-19 13:52:56 +0100755RTCStatsCollector::RequestInfo::RequestInfo(
756 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
757 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
758
759RTCStatsCollector::RequestInfo::RequestInfo(
760 rtc::scoped_refptr<RtpSenderInternal> selector,
761 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
762 : RequestInfo(FilterMode::kSenderSelector,
763 std::move(callback),
764 std::move(selector),
765 nullptr) {}
766
767RTCStatsCollector::RequestInfo::RequestInfo(
768 rtc::scoped_refptr<RtpReceiverInternal> selector,
769 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
770 : RequestInfo(FilterMode::kReceiverSelector,
771 std::move(callback),
772 nullptr,
773 std::move(selector)) {}
774
775RTCStatsCollector::RequestInfo::RequestInfo(
776 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
777 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
778 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
779 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
780 : filter_mode_(filter_mode),
781 callback_(std::move(callback)),
782 sender_selector_(std::move(sender_selector)),
783 receiver_selector_(std::move(receiver_selector)) {
784 RTC_DCHECK(callback_);
785 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
786}
787
hbosc82f2e12016-09-05 01:36:50 -0700788rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800789 PeerConnectionInternal* pc,
790 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700791 return rtc::scoped_refptr<RTCStatsCollector>(
792 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
793}
794
Steve Anton2d8609c2018-01-23 16:38:46 -0800795RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700796 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700797 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700798 signaling_thread_(pc->signaling_thread()),
799 worker_thread_(pc->worker_thread()),
800 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700801 num_pending_partial_reports_(0),
802 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100803 network_report_event_(true /* manual_reset */,
804 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700805 cache_timestamp_us_(0),
806 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700807 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700808 RTC_DCHECK(signaling_thread_);
809 RTC_DCHECK(worker_thread_);
810 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700811 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800812 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800813 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700814}
815
hbosb78306a2016-12-19 05:06:57 -0800816RTCStatsCollector::~RTCStatsCollector() {
817 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
818}
819
hbosc82f2e12016-09-05 01:36:50 -0700820void RTCStatsCollector::GetStatsReport(
821 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100822 GetStatsReportInternal(RequestInfo(std::move(callback)));
823}
824
825void RTCStatsCollector::GetStatsReport(
826 rtc::scoped_refptr<RtpSenderInternal> selector,
827 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
828 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
829}
830
831void RTCStatsCollector::GetStatsReport(
832 rtc::scoped_refptr<RtpReceiverInternal> selector,
833 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
834 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
835}
836
837void RTCStatsCollector::GetStatsReportInternal(
838 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700839 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100840 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700841
hbos0e6758d2016-08-31 07:57:36 -0700842 // "Now" using a monotonically increasing timer.
843 int64_t cache_now_us = rtc::TimeMicros();
844 if (cached_report_ &&
845 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800846 // We have a fresh cached report to deliver. Deliver asynchronously, since
847 // the caller may not be expecting a synchronous callback, and it avoids
848 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100849 std::vector<RequestInfo> requests;
850 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100851 signaling_thread_->PostTask(
852 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
853 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700854 } else if (!num_pending_partial_reports_) {
855 // Only start gathering stats if we're not already gathering stats. In the
856 // case of already gathering stats, |callback_| will be invoked when there
857 // are no more pending partial reports.
858
859 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
860 // UTC), in microseconds. The system clock could be modified and is not
861 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700862 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700863
hbosf415f8a2017-01-02 04:28:51 -0800864 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700865 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800866
Steve Anton57858b32018-02-15 15:19:50 -0800867 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800868 // |ProducePartialResultsOnNetworkThread| and
869 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800870 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700871 // Prepare |transport_names_| for use in
872 // |ProducePartialResultsOnNetworkThread|.
873 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800874
stefanf79ade12017-06-02 06:44:03 -0700875 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
876 // thread.
877 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
878 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700879 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700880
Henrik Boström40b030e2019-02-28 09:49:31 +0100881 // Don't touch |network_report_| on the signaling thread until
882 // ProducePartialResultsOnNetworkThread() has signaled the
883 // |network_report_event_|.
884 network_report_event_.Reset();
885 network_thread_->PostTask(
886 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700887 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100888 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800889 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700890 }
hbosd565b732016-08-30 14:04:35 -0700891}
892
893void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700894 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700895 cached_report_ = nullptr;
896}
897
hbosb78306a2016-12-19 05:06:57 -0800898void RTCStatsCollector::WaitForPendingRequest() {
899 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100900 // If a request is pending, blocks until the |network_report_event_| is
901 // signaled and then delivers the result. Otherwise this is a NO-OP.
902 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800903}
904
hbosc82f2e12016-09-05 01:36:50 -0700905void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
906 int64_t timestamp_us) {
907 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100908 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700909
Henrik Boström40b030e2019-02-28 09:49:31 +0100910 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
911 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700912
Henrik Boström40b030e2019-02-28 09:49:31 +0100913 // ProducePartialResultsOnSignalingThread() is running synchronously on the
914 // signaling thread, so it is always the first partial result delivered on the
915 // signaling thread. The request is not complete until MergeNetworkReport_s()
916 // happens; we don't have to do anything here.
917 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
918 --num_pending_partial_reports_;
919}
920
921void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
922 int64_t timestamp_us,
923 RTCStatsReport* partial_report) {
924 RTC_DCHECK(signaling_thread_->IsCurrent());
925 ProduceDataChannelStats_s(timestamp_us, partial_report);
926 ProduceMediaStreamStats_s(timestamp_us, partial_report);
927 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
928 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700929}
930
hbosc82f2e12016-09-05 01:36:50 -0700931void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
932 int64_t timestamp_us) {
933 RTC_DCHECK(network_thread_->IsCurrent());
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200934 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 09:49:31 +0100935 // |network_report_event_| is reset before this method is invoked.
936 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700937
Steve Anton5dfde182018-02-06 10:34:40 -0800938 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700939 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800940 std::map<std::string, CertificateStatsPair> transport_cert_stats =
941 PrepareTransportCertificateStats_n(transport_stats_by_name);
942
Henrik Boström40b030e2019-02-28 09:49:31 +0100943 ProducePartialResultsOnNetworkThreadImpl(
944 timestamp_us, transport_stats_by_name, transport_cert_stats,
945 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000946
Henrik Boström40b030e2019-02-28 09:49:31 +0100947 // Signal that it is now safe to touch |network_report_| on the signaling
948 // thread, and post a task to merge it into the final results.
949 network_report_event_.Set();
950 signaling_thread_->PostTask(
951 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100952}
953
Henrik Boström40b030e2019-02-28 09:49:31 +0100954void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
955 int64_t timestamp_us,
956 const std::map<std::string, cricket::TransportStats>&
957 transport_stats_by_name,
958 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
959 RTCStatsReport* partial_report) {
960 RTC_DCHECK(network_thread_->IsCurrent());
961 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
962 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
963 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
964 call_stats_, partial_report);
965 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
966 partial_report);
967 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
968 transport_cert_stats, partial_report);
969}
970
971void RTCStatsCollector::MergeNetworkReport_s() {
972 RTC_DCHECK(signaling_thread_->IsCurrent());
973 // The |network_report_event_| must be signaled for it to be safe to touch
974 // |network_report_|. This is normally not blocking, but if
975 // WaitForPendingRequest() is called while a request is pending, we might have
976 // to wait until the network thread is done touching |network_report_|.
977 network_report_event_.Wait(rtc::Event::kForever);
978 if (!network_report_) {
979 // Normally, MergeNetworkReport_s() is executed because it is posted from
980 // the network thread. But if WaitForPendingRequest() is called while a
981 // request is pending, an early call to MergeNetworkReport_s() is made,
982 // merging the report and setting |network_report_| to null. If so, when the
983 // previously posted MergeNetworkReport_s() is later executed, the report is
984 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700985 return;
986 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000987 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100988 RTC_DCHECK(partial_report_);
989 partial_report_->TakeMembersFrom(network_report_);
990 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000991 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100992 // |network_report_| is currently the only partial report collected
993 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
994 // ready to deliver the result.
995 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
996 cache_timestamp_us_ = partial_report_timestamp_us_;
997 cached_report_ = partial_report_;
998 partial_report_ = nullptr;
999 transceiver_stats_infos_.clear();
1000 // Trace WebRTC Stats when getStats is called on Javascript.
1001 // This allows access to WebRTC stats from trace logs. To enable them,
1002 // select the "webrtc_stats" category when recording traces.
1003 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1004 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001005
Henrik Boström40b030e2019-02-28 09:49:31 +01001006 // Deliver report and clear |requests_|.
1007 std::vector<RequestInfo> requests;
1008 requests.swap(requests_);
1009 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001010}
1011
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001012void RTCStatsCollector::DeliverCachedReport(
1013 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001014 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001015 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001016 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001017 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001018
Henrik Boström5b3541f2018-03-19 13:52:56 +01001019 for (const RequestInfo& request : requests) {
1020 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1021 request.callback()->OnStatsDelivered(cached_report);
1022 } else {
1023 bool filter_by_sender_selector;
1024 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1025 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1026 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1027 filter_by_sender_selector = true;
1028 sender_selector = request.sender_selector();
1029 } else {
1030 RTC_DCHECK(request.filter_mode() ==
1031 RequestInfo::FilterMode::kReceiverSelector);
1032 filter_by_sender_selector = false;
1033 receiver_selector = request.receiver_selector();
1034 }
1035 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1036 filter_by_sender_selector, cached_report, sender_selector,
1037 receiver_selector));
1038 }
hbosc82f2e12016-09-05 01:36:50 -07001039 }
hbosd565b732016-08-30 14:04:35 -07001040}
1041
hbosdf6075a2016-12-19 04:58:02 -08001042void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001043 int64_t timestamp_us,
1044 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001045 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001046 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001047 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1048 if (transport_cert_stats_pair.second.local) {
1049 ProduceCertificateStatsFromSSLCertificateStats(
1050 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001051 }
hbos02ba2112016-10-28 05:14:53 -07001052 if (transport_cert_stats_pair.second.remote) {
1053 ProduceCertificateStatsFromSSLCertificateStats(
1054 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001055 }
1056 }
1057}
1058
hbosdf6075a2016-12-19 04:58:02 -08001059void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001060 int64_t timestamp_us,
1061 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001062 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001063 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001064 for (const auto& stats : transceiver_stats_infos) {
1065 if (!stats.mid) {
1066 continue;
hbos0adb8282016-11-23 02:32:06 -08001067 }
Steve Anton57858b32018-02-15 15:19:50 -08001068 const cricket::VoiceMediaInfo* voice_media_info =
1069 stats.track_media_info_map->voice_media_info();
1070 const cricket::VideoMediaInfo* video_media_info =
1071 stats.track_media_info_map->video_media_info();
1072 // Audio
1073 if (voice_media_info) {
1074 // Inbound
1075 for (const auto& pair : voice_media_info->receive_codecs) {
1076 report->AddStats(CodecStatsFromRtpCodecParameters(
1077 timestamp_us, *stats.mid, true, pair.second));
1078 }
1079 // Outbound
1080 for (const auto& pair : voice_media_info->send_codecs) {
1081 report->AddStats(CodecStatsFromRtpCodecParameters(
1082 timestamp_us, *stats.mid, false, pair.second));
1083 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001084 }
Steve Anton57858b32018-02-15 15:19:50 -08001085 // Video
1086 if (video_media_info) {
1087 // Inbound
1088 for (const auto& pair : video_media_info->receive_codecs) {
1089 report->AddStats(CodecStatsFromRtpCodecParameters(
1090 timestamp_us, *stats.mid, true, pair.second));
1091 }
1092 // Outbound
1093 for (const auto& pair : video_media_info->send_codecs) {
1094 report->AddStats(CodecStatsFromRtpCodecParameters(
1095 timestamp_us, *stats.mid, false, pair.second));
1096 }
hbos0adb8282016-11-23 02:32:06 -08001097 }
1098 }
1099}
1100
hboscc555c52016-10-18 12:48:31 -07001101void RTCStatsCollector::ProduceDataChannelStats_s(
1102 int64_t timestamp_us, RTCStatsReport* report) const {
1103 RTC_DCHECK(signaling_thread_->IsCurrent());
1104 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1105 pc_->sctp_data_channels()) {
1106 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1107 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001108 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001109 timestamp_us));
1110 data_channel_stats->label = data_channel->label();
1111 data_channel_stats->protocol = data_channel->protocol();
1112 data_channel_stats->datachannelid = data_channel->id();
1113 data_channel_stats->state =
1114 DataStateToRTCDataChannelState(data_channel->state());
1115 data_channel_stats->messages_sent = data_channel->messages_sent();
1116 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1117 data_channel_stats->messages_received = data_channel->messages_received();
1118 data_channel_stats->bytes_received = data_channel->bytes_received();
1119 report->AddStats(std::move(data_channel_stats));
1120 }
1121}
1122
hbosdf6075a2016-12-19 04:58:02 -08001123void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001124 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001125 const std::map<std::string, cricket::TransportStats>&
1126 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001127 const Call::Stats& call_stats,
1128 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001129 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001130 for (const auto& entry : transport_stats_by_name) {
1131 const std::string& transport_name = entry.first;
1132 const cricket::TransportStats& transport_stats = entry.second;
1133 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001134 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001135 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001136 for (const cricket::ConnectionInfo& info :
1137 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001138 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001139 new RTCIceCandidatePairStats(
1140 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1141 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001142
hbos0583b282016-11-30 01:50:14 -08001143 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001144 // TODO(hbos): There could be other candidates that are not paired with
1145 // anything. We don't have a complete list. Local candidates come from
1146 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001147 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001148 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001149 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001150 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001151 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001152 candidate_pair_stats->state =
1153 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1154 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001155 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001156 // TODO(hbos): This writable is different than the spec. It goes to
1157 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001158 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001159 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001160 candidate_pair_stats->bytes_sent =
1161 static_cast<uint64_t>(info.sent_total_bytes);
1162 candidate_pair_stats->bytes_received =
1163 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001164 candidate_pair_stats->total_round_trip_time =
1165 static_cast<double>(info.total_round_trip_time_ms) /
1166 rtc::kNumMillisecsPerSec;
1167 if (info.current_round_trip_time_ms) {
1168 candidate_pair_stats->current_round_trip_time =
1169 static_cast<double>(*info.current_round_trip_time_ms) /
1170 rtc::kNumMillisecsPerSec;
1171 }
stefanf79ade12017-06-02 06:44:03 -07001172 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001173 // The bandwidth estimations we have are for the selected candidate
1174 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001175 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1176 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1177 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001178 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001179 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001180 }
stefanf79ade12017-06-02 06:44:03 -07001181 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001182 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001183 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001184 }
1185 }
hbosd82f5122016-12-09 04:12:39 -08001186 candidate_pair_stats->requests_received =
1187 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001188 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1189 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001190 candidate_pair_stats->responses_received =
1191 static_cast<uint64_t>(info.recv_ping_responses);
1192 candidate_pair_stats->responses_sent =
1193 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001194 RTC_DCHECK_GE(info.sent_ping_requests_total,
1195 info.sent_ping_requests_before_first_response);
1196 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1197 info.sent_ping_requests_total -
1198 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001199
1200 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001201 }
1202 }
1203 }
1204}
1205
Steve Anton57858b32018-02-15 15:19:50 -08001206void RTCStatsCollector::ProduceMediaStreamStats_s(
1207 int64_t timestamp_us,
1208 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001209 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001210
1211 std::map<std::string, std::vector<std::string>> track_ids;
1212
1213 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001214 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001215 std::string track_id =
1216 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1217 kSender, sender->internal()->AttachmentId());
1218 for (auto& stream_id : sender->stream_ids()) {
1219 track_ids[stream_id].push_back(track_id);
1220 }
1221 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001222 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001223 std::string track_id =
1224 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1225 kReceiver, receiver->internal()->AttachmentId());
1226 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001227 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001228 }
1229 }
1230 }
1231
1232 // Build stats for each stream ID known.
1233 for (auto& it : track_ids) {
1234 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1235 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1236 stream_stats->stream_identifier = it.first;
1237 stream_stats->track_ids = it.second;
1238 report->AddStats(std::move(stream_stats));
1239 }
1240}
1241
1242void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1243 int64_t timestamp_us,
1244 RTCStatsReport* report) const {
1245 RTC_DCHECK(signaling_thread_->IsCurrent());
1246 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1247 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001248 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001249 senders.push_back(sender->internal());
1250 }
1251 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1252 senders, report);
1253
1254 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001255 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001256 receivers.push_back(receiver->internal());
1257 }
1258 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1259 receivers, report);
1260 }
hbos09bc1282016-11-08 06:29:22 -08001261}
1262
hbos6ab97ce2016-10-03 14:16:56 -07001263void RTCStatsCollector::ProducePeerConnectionStats_s(
1264 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001265 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001266 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001267 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001268 stats->data_channels_opened = internal_record_.data_channels_opened;
1269 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001270 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001271}
1272
hbosdf6075a2016-12-19 04:58:02 -08001273void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001274 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001275 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001276 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001277 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001278
Steve Anton57858b32018-02-15 15:19:50 -08001279 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1280 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1281 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1282 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1283 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1284 } else {
1285 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001286 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001287 }
Steve Anton57858b32018-02-15 15:19:50 -08001288}
1289
1290void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1291 int64_t timestamp_us,
1292 const RtpTransceiverStatsInfo& stats,
1293 RTCStatsReport* report) const {
1294 if (!stats.mid || !stats.transport_name) {
1295 return;
1296 }
1297 RTC_DCHECK(stats.track_media_info_map);
1298 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1299 RTC_DCHECK(track_media_info_map.voice_media_info());
1300 std::string mid = *stats.mid;
1301 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1302 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1303 // Inbound
1304 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1305 track_media_info_map.voice_media_info()->receivers) {
1306 if (!voice_receiver_info.connected())
1307 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001308 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001309 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1310 timestamp_us);
1311 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1312 inbound_audio.get());
1313 // TODO(hta): This lookup should look for the sender, not the track.
1314 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1315 track_media_info_map.GetAudioTrack(voice_receiver_info);
1316 if (audio_track) {
1317 inbound_audio->track_id =
1318 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1319 kReceiver,
1320 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001321 }
Steve Anton57858b32018-02-15 15:19:50 -08001322 inbound_audio->transport_id = transport_id;
1323 report->AddStats(std::move(inbound_audio));
1324 }
1325 // Outbound
1326 for (const cricket::VoiceSenderInfo& voice_sender_info :
1327 track_media_info_map.voice_media_info()->senders) {
1328 if (!voice_sender_info.connected())
1329 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001330 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001331 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1332 timestamp_us);
1333 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1334 outbound_audio.get());
1335 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1336 track_media_info_map.GetAudioTrack(voice_sender_info);
1337 if (audio_track) {
1338 outbound_audio->track_id =
1339 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1340 kSender,
1341 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001342 }
Steve Anton57858b32018-02-15 15:19:50 -08001343 outbound_audio->transport_id = transport_id;
1344 report->AddStats(std::move(outbound_audio));
1345 }
1346}
1347
1348void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1349 int64_t timestamp_us,
1350 const RtpTransceiverStatsInfo& stats,
1351 RTCStatsReport* report) const {
1352 if (!stats.mid || !stats.transport_name) {
1353 return;
1354 }
1355 RTC_DCHECK(stats.track_media_info_map);
1356 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1357 RTC_DCHECK(track_media_info_map.video_media_info());
1358 std::string mid = *stats.mid;
1359 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1360 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1361 // Inbound
1362 for (const cricket::VideoReceiverInfo& video_receiver_info :
1363 track_media_info_map.video_media_info()->receivers) {
1364 if (!video_receiver_info.connected())
1365 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001366 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001367 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1368 timestamp_us);
1369 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1370 inbound_video.get());
1371 rtc::scoped_refptr<VideoTrackInterface> video_track =
1372 track_media_info_map.GetVideoTrack(video_receiver_info);
1373 if (video_track) {
1374 inbound_video->track_id =
1375 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1376 kReceiver,
1377 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1378 }
1379 inbound_video->transport_id = transport_id;
1380 report->AddStats(std::move(inbound_video));
1381 }
1382 // Outbound
1383 for (const cricket::VideoSenderInfo& video_sender_info :
1384 track_media_info_map.video_media_info()->senders) {
1385 if (!video_sender_info.connected())
1386 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001387 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001388 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1389 timestamp_us);
1390 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1391 outbound_video.get());
1392 rtc::scoped_refptr<VideoTrackInterface> video_track =
1393 track_media_info_map.GetVideoTrack(video_sender_info);
1394 if (video_track) {
1395 outbound_video->track_id =
1396 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1397 kSender,
1398 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1399 }
1400 outbound_video->transport_id = transport_id;
1401 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001402 }
1403}
1404
hbosdf6075a2016-12-19 04:58:02 -08001405void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001406 int64_t timestamp_us,
1407 const std::map<std::string, cricket::TransportStats>&
1408 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001409 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1410 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001411 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001412 for (const auto& entry : transport_stats_by_name) {
1413 const std::string& transport_name = entry.first;
1414 const cricket::TransportStats& transport_stats = entry.second;
1415
hbos2fa7c672016-10-24 04:00:05 -07001416 // Get reference to RTCP channel, if it exists.
1417 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001418 for (const cricket::TransportChannelStats& channel_stats :
1419 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001420 if (channel_stats.component ==
1421 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1422 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001423 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001424 break;
1425 }
1426 }
1427
1428 // Get reference to local and remote certificates of this transport, if they
1429 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001430 const auto& certificate_stats_it =
1431 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001432 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1433 std::string local_certificate_id;
1434 if (certificate_stats_it->second.local) {
1435 local_certificate_id = RTCCertificateIDFromFingerprint(
1436 certificate_stats_it->second.local->fingerprint);
1437 }
1438 std::string remote_certificate_id;
1439 if (certificate_stats_it->second.remote) {
1440 remote_certificate_id = RTCCertificateIDFromFingerprint(
1441 certificate_stats_it->second.remote->fingerprint);
1442 }
1443
1444 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001445 for (const cricket::TransportChannelStats& channel_stats :
1446 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001447 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001448 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1449 transport_name, channel_stats.component),
1450 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001451 transport_stats->bytes_sent = 0;
1452 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001453 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1454 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001455 for (const cricket::ConnectionInfo& info :
1456 channel_stats.connection_infos) {
1457 *transport_stats->bytes_sent += info.sent_total_bytes;
1458 *transport_stats->bytes_received += info.recv_total_bytes;
1459 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001460 transport_stats->selected_candidate_pair_id =
1461 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1462 }
1463 }
1464 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1465 !rtcp_transport_stats_id.empty()) {
1466 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1467 }
1468 if (!local_certificate_id.empty())
1469 transport_stats->local_certificate_id = local_certificate_id;
1470 if (!remote_certificate_id.empty())
1471 transport_stats->remote_certificate_id = remote_certificate_id;
1472 report->AddStats(std::move(transport_stats));
1473 }
1474 }
1475}
1476
1477std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001478RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001479 const std::map<std::string, cricket::TransportStats>&
1480 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001481 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001482 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001483 for (const auto& entry : transport_stats_by_name) {
1484 const std::string& transport_name = entry.first;
1485
hbos2fa7c672016-10-24 04:00:05 -07001486 CertificateStatsPair certificate_stats_pair;
1487 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001488 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001489 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001490 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001491 }
Steve Anton5dfde182018-02-06 10:34:40 -08001492
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001493 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1494 pc_->GetRemoteSSLCertChain(transport_name);
1495 if (remote_cert_chain) {
1496 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001497 }
Steve Anton5dfde182018-02-06 10:34:40 -08001498
hbos2fa7c672016-10-24 04:00:05 -07001499 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001500 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001501 }
1502 return transport_cert_stats;
1503}
1504
Steve Anton57858b32018-02-15 15:19:50 -08001505std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1506RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1507 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001508
Steve Anton57858b32018-02-15 15:19:50 -08001509 // These are used to invoke GetStats for all the media channels together in
1510 // one worker thread hop.
1511 std::map<cricket::VoiceMediaChannel*,
1512 std::unique_ptr<cricket::VoiceMediaInfo>>
1513 voice_stats;
1514 std::map<cricket::VideoMediaChannel*,
1515 std::unique_ptr<cricket::VideoMediaInfo>>
1516 video_stats;
1517
Mirko Bonadei739baf02019-01-27 17:29:42 +01001518 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001519 cricket::MediaType media_type = transceiver->media_type();
1520
1521 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1522 // stats have been fetched on the worker thread.
1523 transceiver_stats_infos.emplace_back();
1524 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1525 stats.transceiver = transceiver->internal();
1526 stats.media_type = media_type;
1527
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001528 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001529 if (!channel) {
1530 // The remaining fields require a BaseChannel.
1531 continue;
1532 }
1533
1534 stats.mid = channel->content_name();
1535 stats.transport_name = channel->transport_name();
1536
1537 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1538 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1539 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1540 voice_stats.end());
1541 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001542 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001543 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1544 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1545 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1546 video_stats.end());
1547 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001548 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001549 } else {
1550 RTC_NOTREACHED();
1551 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001552 }
Steve Anton57858b32018-02-15 15:19:50 -08001553
1554 // Call GetStats for all media channels together on the worker thread in one
1555 // hop.
1556 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1557 for (const auto& entry : voice_stats) {
1558 if (!entry.first->GetStats(entry.second.get())) {
1559 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1560 }
1561 }
1562 for (const auto& entry : video_stats) {
1563 if (!entry.first->GetStats(entry.second.get())) {
1564 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1565 }
1566 }
1567 });
1568
1569 // Create the TrackMediaInfoMap for each transceiver stats object.
1570 for (auto& stats : transceiver_stats_infos) {
1571 auto transceiver = stats.transceiver;
1572 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1573 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1574 if (transceiver->channel()) {
1575 cricket::MediaType media_type = transceiver->media_type();
1576 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1577 auto* voice_channel =
1578 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1579 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1580 voice_media_info =
1581 std::move(voice_stats[voice_channel->media_channel()]);
1582 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1583 auto* video_channel =
1584 static_cast<cricket::VideoChannel*>(transceiver->channel());
1585 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1586 video_media_info =
1587 std::move(video_stats[video_channel->media_channel()]);
1588 }
1589 }
1590 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001591 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001592 senders.push_back(sender->internal());
1593 }
1594 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001595 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001596 receivers.push_back(receiver->internal());
1597 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001598 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001599 std::move(voice_media_info), std::move(video_media_info), senders,
1600 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001601 }
Steve Anton57858b32018-02-15 15:19:50 -08001602
1603 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001604}
1605
Steve Anton7eca0932018-03-30 15:18:41 -07001606std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1607 std::set<std::string> transport_names;
1608 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1609 if (transceiver->internal()->channel()) {
1610 transport_names.insert(
1611 transceiver->internal()->channel()->transport_name());
1612 }
1613 }
1614 if (pc_->rtp_data_channel()) {
1615 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1616 }
1617 if (pc_->sctp_transport_name()) {
1618 transport_names.insert(*pc_->sctp_transport_name());
1619 }
1620 return transport_names;
1621}
1622
hbos82ebe022016-11-14 01:41:09 -08001623void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1624 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1625 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1626}
1627
1628void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1629 RTC_DCHECK(signaling_thread_->IsCurrent());
1630 bool result = internal_record_.opened_data_channels.insert(
1631 reinterpret_cast<uintptr_t>(channel)).second;
1632 ++internal_record_.data_channels_opened;
1633 RTC_DCHECK(result);
1634}
1635
1636void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1637 RTC_DCHECK(signaling_thread_->IsCurrent());
1638 // Only channels that have been fully opened (and have increased the
1639 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001640 if (internal_record_.opened_data_channels.erase(
1641 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001642 ++internal_record_.data_channels_closed;
1643 }
1644}
1645
hboscc555c52016-10-18 12:48:31 -07001646const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1647 const std::string& type) {
1648 return CandidateTypeToRTCIceCandidateType(type);
1649}
1650
1651const char* DataStateToRTCDataChannelStateForTesting(
1652 DataChannelInterface::DataState state) {
1653 return DataStateToRTCDataChannelState(state);
1654}
1655
hbosd565b732016-08-30 14:04:35 -07001656} // namespace webrtc