blob: 3287b92762bcf9f1e80a5e5386cb06a13c5ac634 [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"
22#include "media/base/media_channel.h"
23#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "pc/peer_connection.h"
26#include "pc/rtc_stats_traversal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Henrik Boström05d43c62019-02-15 10:23:08 +010028#include "rtc_base/post_message_with_functor.h"
Jonas Olsson43568dd2018-06-11 16:25:54 +020029#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070032
33namespace webrtc {
34
hboscc555c52016-10-18 12:48:31 -070035namespace {
36
hbos2fa7c672016-10-24 04:00:05 -070037std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
38 return "RTCCertificate_" + fingerprint;
39}
40
Steve Anton57858b32018-02-15 15:19:50 -080041std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
42 bool inbound,
43 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020044 char buf[1024];
45 rtc::SimpleStringBuilder sb(buf);
46 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
47 << payload_type;
48 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080049}
50
hbos2fa7c672016-10-24 04:00:05 -070051std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
52 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020053 char buf[4096];
54 rtc::SimpleStringBuilder sb(buf);
55 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
56 << info.remote_candidate.id();
57 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070058}
59
Harald Alvestranda3dab842018-01-14 09:18:58 +010060const char kSender[] = "sender";
61const char kReceiver[] = "receiver";
62
63std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
64 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010065 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020066 char buf[1024];
67 rtc::SimpleStringBuilder sb(buf);
68 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
69 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080070}
71
hbos2fa7c672016-10-24 04:00:05 -070072std::string RTCTransportStatsIDFromTransportChannel(
73 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020074 char buf[1024];
75 rtc::SimpleStringBuilder sb(buf);
76 sb << "RTCTransport_" << transport_name << "_" << channel_component;
77 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070078}
79
hboseeafe942016-11-01 03:00:17 -070080std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020081 char buf[1024];
82 rtc::SimpleStringBuilder sb(buf);
83 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
84 return sb.str();
hboseeafe942016-11-01 03:00:17 -070085}
86
hbos6ded1902016-11-01 01:50:46 -070087std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020088 char buf[1024];
89 rtc::SimpleStringBuilder sb(buf);
90 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
91 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070092}
93
hbosab9f6e42016-10-07 02:18:47 -070094const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
95 if (type == cricket::LOCAL_PORT_TYPE)
96 return RTCIceCandidateType::kHost;
97 if (type == cricket::STUN_PORT_TYPE)
98 return RTCIceCandidateType::kSrflx;
99 if (type == cricket::PRFLX_PORT_TYPE)
100 return RTCIceCandidateType::kPrflx;
101 if (type == cricket::RELAY_PORT_TYPE)
102 return RTCIceCandidateType::kRelay;
103 RTC_NOTREACHED();
104 return nullptr;
105}
106
hboscc555c52016-10-18 12:48:31 -0700107const char* DataStateToRTCDataChannelState(
108 DataChannelInterface::DataState state) {
109 switch (state) {
110 case DataChannelInterface::kConnecting:
111 return RTCDataChannelState::kConnecting;
112 case DataChannelInterface::kOpen:
113 return RTCDataChannelState::kOpen;
114 case DataChannelInterface::kClosing:
115 return RTCDataChannelState::kClosing;
116 case DataChannelInterface::kClosed:
117 return RTCDataChannelState::kClosed;
118 default:
119 RTC_NOTREACHED();
120 return nullptr;
121 }
122}
123
hbos06495bc2017-01-02 08:08:18 -0800124const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
125 cricket::IceCandidatePairState state) {
126 switch (state) {
127 case cricket::IceCandidatePairState::WAITING:
128 return RTCStatsIceCandidatePairState::kWaiting;
129 case cricket::IceCandidatePairState::IN_PROGRESS:
130 return RTCStatsIceCandidatePairState::kInProgress;
131 case cricket::IceCandidatePairState::SUCCEEDED:
132 return RTCStatsIceCandidatePairState::kSucceeded;
133 case cricket::IceCandidatePairState::FAILED:
134 return RTCStatsIceCandidatePairState::kFailed;
135 default:
136 RTC_NOTREACHED();
137 return nullptr;
138 }
139}
140
hbos7064d592017-01-16 07:38:02 -0800141const char* DtlsTransportStateToRTCDtlsTransportState(
142 cricket::DtlsTransportState state) {
143 switch (state) {
144 case cricket::DTLS_TRANSPORT_NEW:
145 return RTCDtlsTransportState::kNew;
146 case cricket::DTLS_TRANSPORT_CONNECTING:
147 return RTCDtlsTransportState::kConnecting;
148 case cricket::DTLS_TRANSPORT_CONNECTED:
149 return RTCDtlsTransportState::kConnected;
150 case cricket::DTLS_TRANSPORT_CLOSED:
151 return RTCDtlsTransportState::kClosed;
152 case cricket::DTLS_TRANSPORT_FAILED:
153 return RTCDtlsTransportState::kFailed;
154 default:
155 RTC_NOTREACHED();
156 return nullptr;
157 }
158}
159
Gary Liu37e489c2017-11-21 10:49:36 -0800160const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
161 switch (type) {
162 case rtc::ADAPTER_TYPE_CELLULAR:
163 return RTCNetworkType::kCellular;
164 case rtc::ADAPTER_TYPE_ETHERNET:
165 return RTCNetworkType::kEthernet;
166 case rtc::ADAPTER_TYPE_WIFI:
167 return RTCNetworkType::kWifi;
168 case rtc::ADAPTER_TYPE_VPN:
169 return RTCNetworkType::kVpn;
170 case rtc::ADAPTER_TYPE_UNKNOWN:
171 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700172 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800173 return RTCNetworkType::kUnknown;
174 }
175 RTC_NOTREACHED();
176 return nullptr;
177}
178
hbos9e302742017-01-20 02:47:10 -0800179double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
180 RTC_DCHECK_GE(audio_level, 0);
181 RTC_DCHECK_LE(audio_level, 32767);
182 return audio_level / 32767.0;
183}
184
hbos0adb8282016-11-23 02:32:06 -0800185std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800186 uint64_t timestamp_us,
187 const std::string& mid,
188 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800189 const RtpCodecParameters& codec_params) {
190 RTC_DCHECK_GE(codec_params.payload_type, 0);
191 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800192 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800193 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
194 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800195 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800196 timestamp_us));
197 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800198 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800199 if (codec_params.clock_rate) {
200 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
201 }
hbos0adb8282016-11-23 02:32:06 -0800202 return codec_stats;
203}
204
hbos09bc1282016-11-08 06:29:22 -0800205void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
206 const MediaStreamTrackInterface& track,
207 RTCMediaStreamTrackStats* track_stats) {
208 track_stats->track_identifier = track.id();
209 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
210}
211
hbos820f5782016-11-22 03:16:50 -0800212// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700213void SetInboundRTPStreamStatsFromMediaReceiverInfo(
214 const cricket::MediaReceiverInfo& media_receiver_info,
215 RTCInboundRTPStreamStats* inbound_stats) {
216 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800217 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100218 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700219 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700220 inbound_stats->packets_received =
221 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
222 inbound_stats->bytes_received =
223 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800224 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100225 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700226 inbound_stats->fraction_lost =
227 static_cast<double>(media_receiver_info.fraction_lost);
228}
229
230void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800231 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700232 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800233 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700234 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800235 voice_receiver_info, inbound_audio);
236 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200237 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800238 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800239 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
240 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800241 }
hbos820f5782016-11-22 03:16:50 -0800242 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700243 static_cast<double>(voice_receiver_info.jitter_ms) /
244 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800245 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
246 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700247}
248
249void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800250 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700251 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800252 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700253 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800254 video_receiver_info, inbound_video);
255 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200256 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800257 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800258 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
259 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800260 }
hbos820f5782016-11-22 03:16:50 -0800261 inbound_video->fir_count =
262 static_cast<uint32_t>(video_receiver_info.firs_sent);
263 inbound_video->pli_count =
264 static_cast<uint32_t>(video_receiver_info.plis_sent);
265 inbound_video->nack_count =
266 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800267 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800268 if (video_receiver_info.qp_sum)
269 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700270}
271
hbos820f5782016-11-22 03:16:50 -0800272// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700273void SetOutboundRTPStreamStatsFromMediaSenderInfo(
274 const cricket::MediaSenderInfo& media_sender_info,
275 RTCOutboundRTPStreamStats* outbound_stats) {
276 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800277 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100278 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700279 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700280 outbound_stats->packets_sent =
281 static_cast<uint32_t>(media_sender_info.packets_sent);
282 outbound_stats->bytes_sent =
283 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700284}
285
286void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800287 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700288 const cricket::VoiceSenderInfo& voice_sender_info,
289 RTCOutboundRTPStreamStats* outbound_audio) {
290 SetOutboundRTPStreamStatsFromMediaSenderInfo(
291 voice_sender_info, outbound_audio);
292 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200293 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800294 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800295 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
296 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800297 }
hbos6ded1902016-11-01 01:50:46 -0700298 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
299 // purposefully left undefined for audio.
300}
301
302void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800303 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700304 const cricket::VideoSenderInfo& video_sender_info,
305 RTCOutboundRTPStreamStats* outbound_video) {
306 SetOutboundRTPStreamStatsFromMediaSenderInfo(
307 video_sender_info, outbound_video);
308 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200309 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800310 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800311 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
312 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800313 }
hbos6ded1902016-11-01 01:50:46 -0700314 outbound_video->fir_count =
315 static_cast<uint32_t>(video_sender_info.firs_rcvd);
316 outbound_video->pli_count =
317 static_cast<uint32_t>(video_sender_info.plis_rcvd);
318 outbound_video->nack_count =
319 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800320 if (video_sender_info.qp_sum)
321 outbound_video->qp_sum = *video_sender_info.qp_sum;
322 outbound_video->frames_encoded = video_sender_info.frames_encoded;
hbos6ded1902016-11-01 01:50:46 -0700323}
324
hbos02ba2112016-10-28 05:14:53 -0700325void ProduceCertificateStatsFromSSLCertificateStats(
326 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
327 RTCStatsReport* report) {
328 RTCCertificateStats* prev_certificate_stats = nullptr;
329 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
330 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800331 std::string certificate_stats_id =
332 RTCCertificateIDFromFingerprint(s->fingerprint);
333 // It is possible for the same certificate to show up multiple times, e.g.
334 // if local and remote side use the same certificate in a loopback call.
335 // If the report already contains stats for this certificate, skip it.
336 if (report->Get(certificate_stats_id)) {
337 RTC_DCHECK_EQ(s, &certificate_stats);
338 break;
339 }
hbos02ba2112016-10-28 05:14:53 -0700340 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800341 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700342 certificate_stats->fingerprint = s->fingerprint;
343 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
344 certificate_stats->base64_certificate = s->base64_certificate;
345 if (prev_certificate_stats)
346 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
347 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
348 prev_certificate_stats = certificate_stats;
349 }
350}
351
352const std::string& ProduceIceCandidateStats(
353 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800354 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700355 const std::string& id = "RTCIceCandidate_" + candidate.id();
356 const RTCStats* stats = report->Get(id);
357 if (!stats) {
358 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
359 if (is_local)
360 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
361 else
362 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800363 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800364 if (is_local) {
365 candidate_stats->network_type =
366 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200367 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
368 std::string relay_protocol = candidate.relay_protocol();
369 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
370 relay_protocol.compare("tcp") == 0 ||
371 relay_protocol.compare("tls") == 0);
372 candidate_stats->relay_protocol = relay_protocol;
373 }
Gary Liu37e489c2017-11-21 10:49:36 -0800374 } else {
375 // We don't expect to know the adapter type of remote candidates.
376 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
377 }
hbos02ba2112016-10-28 05:14:53 -0700378 candidate_stats->ip = candidate.address().ipaddr().ToString();
379 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
380 candidate_stats->protocol = candidate.protocol();
381 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
382 candidate.type());
383 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
384
385 stats = candidate_stats.get();
386 report->AddStats(std::move(candidate_stats));
387 }
388 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
389 : RTCRemoteIceCandidateStats::kType);
390 return stats->id();
391}
392
hbos9e302742017-01-20 02:47:10 -0800393std::unique_ptr<RTCMediaStreamTrackStats>
394ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
395 int64_t timestamp_us,
396 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100397 const cricket::VoiceSenderInfo& voice_sender_info,
398 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800399 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
400 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100401 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
402 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100403 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800404 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
405 audio_track, audio_track_stats.get());
406 audio_track_stats->remote_source = false;
407 audio_track_stats->detached = false;
408 if (voice_sender_info.audio_level >= 0) {
409 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
410 voice_sender_info.audio_level);
411 }
zsteine76bd3a2017-07-14 12:17:49 -0700412 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
413 audio_track_stats->total_samples_duration =
414 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100415 if (voice_sender_info.apm_statistics.echo_return_loss) {
416 audio_track_stats->echo_return_loss =
417 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800418 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100419 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
420 audio_track_stats->echo_return_loss_enhancement =
421 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800422 }
423 return audio_track_stats;
424}
425
426std::unique_ptr<RTCMediaStreamTrackStats>
427ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
428 int64_t timestamp_us,
429 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100430 const cricket::VoiceReceiverInfo& voice_receiver_info,
431 int attachment_id) {
432 // Since receiver tracks can't be reattached, we use the SSRC as
433 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800434 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
435 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100436 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
437 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100438 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800439 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
440 audio_track, audio_track_stats.get());
441 audio_track_stats->remote_source = true;
442 audio_track_stats->detached = false;
443 if (voice_receiver_info.audio_level >= 0) {
444 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
445 voice_receiver_info.audio_level);
446 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200447 audio_track_stats->jitter_buffer_delay =
448 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100449 audio_track_stats->jitter_buffer_emitted_count =
450 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 12:17:49 -0700451 audio_track_stats->total_audio_energy =
452 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700453 audio_track_stats->total_samples_received =
454 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700455 audio_track_stats->total_samples_duration =
456 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700457 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200458 audio_track_stats->concealment_events =
459 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100460 audio_track_stats->jitter_buffer_flushes =
461 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100462 audio_track_stats->delayed_packet_outage_samples =
463 voice_receiver_info.delayed_packet_outage_samples;
hbos9e302742017-01-20 02:47:10 -0800464 return audio_track_stats;
465}
466
467std::unique_ptr<RTCMediaStreamTrackStats>
468ProduceMediaStreamTrackStatsFromVideoSenderInfo(
469 int64_t timestamp_us,
470 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100471 const cricket::VideoSenderInfo& video_sender_info,
472 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800473 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
474 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100475 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
476
477 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100478 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800479 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
480 video_track, video_track_stats.get());
481 video_track_stats->remote_source = false;
482 video_track_stats->detached = false;
483 video_track_stats->frame_width = static_cast<uint32_t>(
484 video_sender_info.send_frame_width);
485 video_track_stats->frame_height = static_cast<uint32_t>(
486 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800487 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100488 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800489 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100490 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800491 return video_track_stats;
492}
493
494std::unique_ptr<RTCMediaStreamTrackStats>
495ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
496 int64_t timestamp_us,
497 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100498 const cricket::VideoReceiverInfo& video_receiver_info,
499 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800500 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
501 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100502 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
503
504 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100505 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800506 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
507 video_track, video_track_stats.get());
508 video_track_stats->remote_source = true;
509 video_track_stats->detached = false;
510 if (video_receiver_info.frame_width > 0 &&
511 video_receiver_info.frame_height > 0) {
512 video_track_stats->frame_width = static_cast<uint32_t>(
513 video_receiver_info.frame_width);
514 video_track_stats->frame_height = static_cast<uint32_t>(
515 video_receiver_info.frame_height);
516 }
hbos42f6d2f2017-01-20 03:56:50 -0800517 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800518 // TODO(hbos): When we support receiving simulcast, this should be the total
519 // number of frames correctly decoded, independent of which SSRC it was
520 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100521 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800522 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800523 RTC_DCHECK_GE(video_receiver_info.frames_received,
524 video_receiver_info.frames_rendered);
525 video_track_stats->frames_dropped = video_receiver_info.frames_received -
526 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100527 video_track_stats->freeze_count = video_receiver_info.freeze_count;
528 video_track_stats->pause_count = video_receiver_info.pause_count;
529 video_track_stats->total_freezes_duration =
530 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
531 rtc::kNumMillisecsPerSec;
532 video_track_stats->total_pauses_duration =
533 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
534 rtc::kNumMillisecsPerSec;
535 video_track_stats->total_frames_duration =
536 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
537 rtc::kNumMillisecsPerSec;
538 video_track_stats->sum_squared_frame_durations =
539 video_receiver_info.sum_squared_frame_durations;
540
hbos9e302742017-01-20 02:47:10 -0800541 return video_track_stats;
542}
543
Harald Alvestrand89061872018-01-02 14:08:34 +0100544void ProduceSenderMediaTrackStats(
545 int64_t timestamp_us,
546 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800547 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100548 RTCStatsReport* report) {
549 // This function iterates over the senders to generate outgoing track stats.
550
551 // TODO(hbos): Return stats of detached tracks. We have to perform stats
552 // gathering at the time of detachment to get accurate stats and timestamps.
553 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100554 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100555 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
556 AudioTrackInterface* track =
557 static_cast<AudioTrackInterface*>(sender->track().get());
558 if (!track)
559 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100560 cricket::VoiceSenderInfo null_sender_info;
561 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
562 // TODO(hta): Checking on ssrc is not proper. There should be a way
563 // to see from a sender whether it's connected or not.
564 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800565 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100566 // When pc.close is called, sender info is discarded, so
567 // we generate zeroes instead. Bug: It should be retained.
568 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800569 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100570 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100571 if (sender_info) {
572 voice_sender_info = sender_info;
573 } else {
574 RTC_LOG(LS_INFO)
575 << "RTCStatsCollector: No voice sender info for sender with ssrc "
576 << sender->ssrc();
577 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100578 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100579 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100580 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
581 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100582 report->AddStats(std::move(audio_track_stats));
583 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
584 VideoTrackInterface* track =
585 static_cast<VideoTrackInterface*>(sender->track().get());
586 if (!track)
587 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100588 cricket::VideoSenderInfo null_sender_info;
589 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
590 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100591 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
592 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800593 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100594 // When pc.close is called, sender info is discarded, so
595 // we generate zeroes instead. Bug: It should be retained.
596 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800597 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100598 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100599 if (sender_info) {
600 video_sender_info = sender_info;
601 } else {
602 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
603 << sender->ssrc();
604 }
605 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100606 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100607 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
608 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100609 report->AddStats(std::move(video_track_stats));
610 } else {
611 RTC_NOTREACHED();
612 }
613 }
614}
615
616void ProduceReceiverMediaTrackStats(
617 int64_t timestamp_us,
618 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800619 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100620 RTCStatsReport* report) {
621 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100622 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100623 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
624 AudioTrackInterface* track =
625 static_cast<AudioTrackInterface*>(receiver->track().get());
626 const cricket::VoiceReceiverInfo* voice_receiver_info =
627 track_media_info_map.GetVoiceReceiverInfo(*track);
628 if (!voice_receiver_info) {
629 continue;
630 }
631 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
632 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100633 timestamp_us, *track, *voice_receiver_info,
634 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100635 report->AddStats(std::move(audio_track_stats));
636 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
637 VideoTrackInterface* track =
638 static_cast<VideoTrackInterface*>(receiver->track().get());
639 const cricket::VideoReceiverInfo* video_receiver_info =
640 track_media_info_map.GetVideoReceiverInfo(*track);
641 if (!video_receiver_info) {
642 continue;
643 }
644 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
645 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100646 timestamp_us, *track, *video_receiver_info,
647 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100648 report->AddStats(std::move(video_track_stats));
649 } else {
650 RTC_NOTREACHED();
651 }
652 }
653}
654
Henrik Boström5b3541f2018-03-19 13:52:56 +0100655rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
656 bool filter_by_sender_selector,
657 rtc::scoped_refptr<const RTCStatsReport> report,
658 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
659 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
660 std::vector<std::string> rtpstream_ids;
661 if (filter_by_sender_selector) {
662 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
663 if (sender_selector) {
664 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
665 // reference the sender stats.
666 // Because we do not implement sender stats, we look at outbound-rtp(s)
667 // that reference the track attachment stats for the sender instead.
668 std::string track_id =
669 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
670 kSender, sender_selector->AttachmentId());
671 for (const auto& stats : *report) {
672 if (stats.type() != RTCOutboundRTPStreamStats::kType)
673 continue;
674 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
675 if (outbound_rtp.track_id.is_defined() &&
676 *outbound_rtp.track_id == track_id) {
677 rtpstream_ids.push_back(outbound_rtp.id());
678 }
679 }
680 }
681 } else {
682 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
683 if (receiver_selector) {
684 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
685 // reference the receiver stats.
686 // Because we do not implement receiver stats, we look at inbound-rtp(s)
687 // that reference the track attachment stats for the receiver instead.
688 std::string track_id =
689 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
690 kReceiver, receiver_selector->AttachmentId());
691 for (const auto& stats : *report) {
692 if (stats.type() != RTCInboundRTPStreamStats::kType)
693 continue;
694 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
695 if (inbound_rtp.track_id.is_defined() &&
696 *inbound_rtp.track_id == track_id) {
697 rtpstream_ids.push_back(inbound_rtp.id());
698 }
699 }
700 }
701 }
702 if (rtpstream_ids.empty())
703 return RTCStatsReport::Create(report->timestamp_us());
704 return TakeReferencedStats(report->Copy(), rtpstream_ids);
705}
706
hboscc555c52016-10-18 12:48:31 -0700707} // namespace
708
Henrik Boström5b3541f2018-03-19 13:52:56 +0100709RTCStatsCollector::RequestInfo::RequestInfo(
710 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
711 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
712
713RTCStatsCollector::RequestInfo::RequestInfo(
714 rtc::scoped_refptr<RtpSenderInternal> selector,
715 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
716 : RequestInfo(FilterMode::kSenderSelector,
717 std::move(callback),
718 std::move(selector),
719 nullptr) {}
720
721RTCStatsCollector::RequestInfo::RequestInfo(
722 rtc::scoped_refptr<RtpReceiverInternal> selector,
723 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
724 : RequestInfo(FilterMode::kReceiverSelector,
725 std::move(callback),
726 nullptr,
727 std::move(selector)) {}
728
729RTCStatsCollector::RequestInfo::RequestInfo(
730 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
731 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
732 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
733 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
734 : filter_mode_(filter_mode),
735 callback_(std::move(callback)),
736 sender_selector_(std::move(sender_selector)),
737 receiver_selector_(std::move(receiver_selector)) {
738 RTC_DCHECK(callback_);
739 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
740}
741
hbosc82f2e12016-09-05 01:36:50 -0700742rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800743 PeerConnectionInternal* pc,
744 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700745 return rtc::scoped_refptr<RTCStatsCollector>(
746 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
747}
748
Steve Anton2d8609c2018-01-23 16:38:46 -0800749RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700750 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700751 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700752 signaling_thread_(pc->signaling_thread()),
753 worker_thread_(pc->worker_thread()),
754 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700755 num_pending_partial_reports_(0),
756 partial_report_timestamp_us_(0),
Henrik Boström05d43c62019-02-15 10:23:08 +0100757 network_report_event_(true /* manual_reset */,
758 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700759 cache_timestamp_us_(0),
760 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700761 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700762 RTC_DCHECK(signaling_thread_);
763 RTC_DCHECK(worker_thread_);
764 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700765 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800766 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800767 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700768}
769
hbosb78306a2016-12-19 05:06:57 -0800770RTCStatsCollector::~RTCStatsCollector() {
771 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
772}
773
hbosc82f2e12016-09-05 01:36:50 -0700774void RTCStatsCollector::GetStatsReport(
775 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100776 GetStatsReportInternal(RequestInfo(std::move(callback)));
777}
778
779void RTCStatsCollector::GetStatsReport(
780 rtc::scoped_refptr<RtpSenderInternal> selector,
781 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
782 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
783}
784
785void RTCStatsCollector::GetStatsReport(
786 rtc::scoped_refptr<RtpReceiverInternal> selector,
787 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
788 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
789}
790
791void RTCStatsCollector::GetStatsReportInternal(
792 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700793 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100794 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700795
hbos0e6758d2016-08-31 07:57:36 -0700796 // "Now" using a monotonically increasing timer.
797 int64_t cache_now_us = rtc::TimeMicros();
798 if (cached_report_ &&
799 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800800 // We have a fresh cached report to deliver. Deliver asynchronously, since
801 // the caller may not be expecting a synchronous callback, and it avoids
802 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100803 std::vector<RequestInfo> requests;
804 requests.swap(requests_);
Henrik Boström05d43c62019-02-15 10:23:08 +0100805 rtc::PostMessageWithFunctor(
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800806 RTC_FROM_HERE, signaling_thread_,
807 rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this, cached_report_,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100808 std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700809 } else if (!num_pending_partial_reports_) {
810 // Only start gathering stats if we're not already gathering stats. In the
811 // case of already gathering stats, |callback_| will be invoked when there
812 // are no more pending partial reports.
813
814 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
815 // UTC), in microseconds. The system clock could be modified and is not
816 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700817 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700818
hbosf415f8a2017-01-02 04:28:51 -0800819 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700820 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800821
Steve Anton57858b32018-02-15 15:19:50 -0800822 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800823 // |ProducePartialResultsOnNetworkThread| and
824 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800825 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700826 // Prepare |transport_names_| for use in
827 // |ProducePartialResultsOnNetworkThread|.
828 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800829
stefanf79ade12017-06-02 06:44:03 -0700830 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
831 // thread.
832 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
833 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700834 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700835
Henrik Boström05d43c62019-02-15 10:23:08 +0100836 // Don't touch |network_report_| on the signaling thread until
837 // ProducePartialResultsOnNetworkThread() has signaled the
838 // |network_report_event_|.
839 network_report_event_.Reset();
840 rtc::PostMessageWithFunctor(
stefanf79ade12017-06-02 06:44:03 -0700841 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700842 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström05d43c62019-02-15 10:23:08 +0100843 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800844 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700845 }
hbosd565b732016-08-30 14:04:35 -0700846}
847
848void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700849 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700850 cached_report_ = nullptr;
851}
852
hbosb78306a2016-12-19 05:06:57 -0800853void RTCStatsCollector::WaitForPendingRequest() {
854 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström05d43c62019-02-15 10:23:08 +0100855 // If a request is pending, blocks until the |network_report_event_| is
856 // signaled and then delivers the result. Otherwise this is a NO-OP.
857 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800858}
859
hbosc82f2e12016-09-05 01:36:50 -0700860void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
861 int64_t timestamp_us) {
862 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström05d43c62019-02-15 10:23:08 +0100863 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700864
Henrik Boström05d43c62019-02-15 10:23:08 +0100865 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
866 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700867
Henrik Boström05d43c62019-02-15 10:23:08 +0100868 // ProducePartialResultsOnSignalingThread() is running synchronously on the
869 // signaling thread, so it is always the first partial result delivered on the
870 // signaling thread. The request is not complete until MergeNetworkReport_s()
871 // happens; we don't have to do anything here.
872 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
873 --num_pending_partial_reports_;
874}
875
876void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
877 int64_t timestamp_us,
878 RTCStatsReport* partial_report) {
879 RTC_DCHECK(signaling_thread_->IsCurrent());
880 ProduceDataChannelStats_s(timestamp_us, partial_report);
881 ProduceMediaStreamStats_s(timestamp_us, partial_report);
882 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
883 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700884}
885
hbosc82f2e12016-09-05 01:36:50 -0700886void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
887 int64_t timestamp_us) {
888 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boström05d43c62019-02-15 10:23:08 +0100889 // Touching |network_report_| on this thread is safe by this method because
890 // |network_report_event_| is reset before this method is invoked.
891 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700892
Steve Anton5dfde182018-02-06 10:34:40 -0800893 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700894 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800895 std::map<std::string, CertificateStatsPair> transport_cert_stats =
896 PrepareTransportCertificateStats_n(transport_stats_by_name);
897
Henrik Boström05d43c62019-02-15 10:23:08 +0100898 ProducePartialResultsOnNetworkThreadImpl(
899 timestamp_us, transport_stats_by_name, transport_cert_stats,
900 network_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700901
Henrik Boström05d43c62019-02-15 10:23:08 +0100902 // Signal that it is now safe to touch |network_report_| on the signaling
903 // thread, and post a task to merge it into the final results.
904 network_report_event_.Set();
905 rtc::PostMessageWithFunctor(
906 RTC_FROM_HERE, signaling_thread_,
907 rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
hbosc82f2e12016-09-05 01:36:50 -0700908}
909
Henrik Boström05d43c62019-02-15 10:23:08 +0100910void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
911 int64_t timestamp_us,
912 const std::map<std::string, cricket::TransportStats>&
913 transport_stats_by_name,
914 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
915 RTCStatsReport* partial_report) {
916 RTC_DCHECK(network_thread_->IsCurrent());
917 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
918 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
919 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
920 call_stats_, partial_report);
921 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
922 partial_report);
923 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
924 transport_cert_stats, partial_report);
925}
926
927void RTCStatsCollector::MergeNetworkReport_s() {
928 RTC_DCHECK(signaling_thread_->IsCurrent());
929 // The |network_report_event_| must be signaled for it to be safe to touch
930 // |network_report_|. This is normally not blocking, but if
931 // WaitForPendingRequest() is called while a request is pending, we might have
932 // to wait until the network thread is done touching |network_report_|.
933 network_report_event_.Wait(rtc::Event::kForever);
934 if (!network_report_) {
935 // Normally, MergeNetworkReport_s() is executed because it is posted from
936 // the network thread. But if WaitForPendingRequest() is called while a
937 // request is pending, an early call to MergeNetworkReport_s() is made,
938 // merging the report and setting |network_report_| to null. If so, when the
939 // previously posted MergeNetworkReport_s() is later executed, the report is
940 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700941 return;
942 }
hbosc82f2e12016-09-05 01:36:50 -0700943 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström05d43c62019-02-15 10:23:08 +0100944 RTC_DCHECK(partial_report_);
945 partial_report_->TakeMembersFrom(network_report_);
946 network_report_ = nullptr;
hbosc82f2e12016-09-05 01:36:50 -0700947 --num_pending_partial_reports_;
Henrik Boström05d43c62019-02-15 10:23:08 +0100948 // |network_report_| is currently the only partial report collected
949 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
950 // ready to deliver the result.
951 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
952 cache_timestamp_us_ = partial_report_timestamp_us_;
953 cached_report_ = partial_report_;
954 partial_report_ = nullptr;
955 transceiver_stats_infos_.clear();
956 // Trace WebRTC Stats when getStats is called on Javascript.
957 // This allows access to WebRTC stats from trace logs. To enable them,
958 // select the "webrtc_stats" category when recording traces.
959 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
960 cached_report_->ToJson());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800961
Henrik Boström05d43c62019-02-15 10:23:08 +0100962 // Deliver report and clear |requests_|.
963 std::vector<RequestInfo> requests;
964 requests.swap(requests_);
965 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700966}
967
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800968void RTCStatsCollector::DeliverCachedReport(
969 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100970 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700971 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100972 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800973 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800974
Henrik Boström5b3541f2018-03-19 13:52:56 +0100975 for (const RequestInfo& request : requests) {
976 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
977 request.callback()->OnStatsDelivered(cached_report);
978 } else {
979 bool filter_by_sender_selector;
980 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
981 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
982 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
983 filter_by_sender_selector = true;
984 sender_selector = request.sender_selector();
985 } else {
986 RTC_DCHECK(request.filter_mode() ==
987 RequestInfo::FilterMode::kReceiverSelector);
988 filter_by_sender_selector = false;
989 receiver_selector = request.receiver_selector();
990 }
991 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
992 filter_by_sender_selector, cached_report, sender_selector,
993 receiver_selector));
994 }
hbosc82f2e12016-09-05 01:36:50 -0700995 }
hbosd565b732016-08-30 14:04:35 -0700996}
997
hbosdf6075a2016-12-19 04:58:02 -0800998void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700999 int64_t timestamp_us,
1000 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001001 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001002 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001003 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1004 if (transport_cert_stats_pair.second.local) {
1005 ProduceCertificateStatsFromSSLCertificateStats(
1006 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001007 }
hbos02ba2112016-10-28 05:14:53 -07001008 if (transport_cert_stats_pair.second.remote) {
1009 ProduceCertificateStatsFromSSLCertificateStats(
1010 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001011 }
1012 }
1013}
1014
hbosdf6075a2016-12-19 04:58:02 -08001015void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001016 int64_t timestamp_us,
1017 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001018 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001019 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001020 for (const auto& stats : transceiver_stats_infos) {
1021 if (!stats.mid) {
1022 continue;
hbos0adb8282016-11-23 02:32:06 -08001023 }
Steve Anton57858b32018-02-15 15:19:50 -08001024 const cricket::VoiceMediaInfo* voice_media_info =
1025 stats.track_media_info_map->voice_media_info();
1026 const cricket::VideoMediaInfo* video_media_info =
1027 stats.track_media_info_map->video_media_info();
1028 // Audio
1029 if (voice_media_info) {
1030 // Inbound
1031 for (const auto& pair : voice_media_info->receive_codecs) {
1032 report->AddStats(CodecStatsFromRtpCodecParameters(
1033 timestamp_us, *stats.mid, true, pair.second));
1034 }
1035 // Outbound
1036 for (const auto& pair : voice_media_info->send_codecs) {
1037 report->AddStats(CodecStatsFromRtpCodecParameters(
1038 timestamp_us, *stats.mid, false, pair.second));
1039 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001040 }
Steve Anton57858b32018-02-15 15:19:50 -08001041 // Video
1042 if (video_media_info) {
1043 // Inbound
1044 for (const auto& pair : video_media_info->receive_codecs) {
1045 report->AddStats(CodecStatsFromRtpCodecParameters(
1046 timestamp_us, *stats.mid, true, pair.second));
1047 }
1048 // Outbound
1049 for (const auto& pair : video_media_info->send_codecs) {
1050 report->AddStats(CodecStatsFromRtpCodecParameters(
1051 timestamp_us, *stats.mid, false, pair.second));
1052 }
hbos0adb8282016-11-23 02:32:06 -08001053 }
1054 }
1055}
1056
hboscc555c52016-10-18 12:48:31 -07001057void RTCStatsCollector::ProduceDataChannelStats_s(
1058 int64_t timestamp_us, RTCStatsReport* report) const {
1059 RTC_DCHECK(signaling_thread_->IsCurrent());
1060 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1061 pc_->sctp_data_channels()) {
1062 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1063 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001064 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001065 timestamp_us));
1066 data_channel_stats->label = data_channel->label();
1067 data_channel_stats->protocol = data_channel->protocol();
1068 data_channel_stats->datachannelid = data_channel->id();
1069 data_channel_stats->state =
1070 DataStateToRTCDataChannelState(data_channel->state());
1071 data_channel_stats->messages_sent = data_channel->messages_sent();
1072 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1073 data_channel_stats->messages_received = data_channel->messages_received();
1074 data_channel_stats->bytes_received = data_channel->bytes_received();
1075 report->AddStats(std::move(data_channel_stats));
1076 }
1077}
1078
hbosdf6075a2016-12-19 04:58:02 -08001079void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001080 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001081 const std::map<std::string, cricket::TransportStats>&
1082 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001083 const Call::Stats& call_stats,
1084 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001085 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001086 for (const auto& entry : transport_stats_by_name) {
1087 const std::string& transport_name = entry.first;
1088 const cricket::TransportStats& transport_stats = entry.second;
1089 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001090 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001091 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001092 for (const cricket::ConnectionInfo& info :
1093 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001094 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001095 new RTCIceCandidatePairStats(
1096 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1097 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001098
hbos0583b282016-11-30 01:50:14 -08001099 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001100 // TODO(hbos): There could be other candidates that are not paired with
1101 // anything. We don't have a complete list. Local candidates come from
1102 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001103 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001104 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001105 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001106 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001107 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001108 candidate_pair_stats->state =
1109 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1110 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001111 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001112 // TODO(hbos): This writable is different than the spec. It goes to
1113 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001114 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001115 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001116 candidate_pair_stats->bytes_sent =
1117 static_cast<uint64_t>(info.sent_total_bytes);
1118 candidate_pair_stats->bytes_received =
1119 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001120 candidate_pair_stats->total_round_trip_time =
1121 static_cast<double>(info.total_round_trip_time_ms) /
1122 rtc::kNumMillisecsPerSec;
1123 if (info.current_round_trip_time_ms) {
1124 candidate_pair_stats->current_round_trip_time =
1125 static_cast<double>(*info.current_round_trip_time_ms) /
1126 rtc::kNumMillisecsPerSec;
1127 }
stefanf79ade12017-06-02 06:44:03 -07001128 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001129 // The bandwidth estimations we have are for the selected candidate
1130 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001131 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1132 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1133 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001134 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001135 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001136 }
stefanf79ade12017-06-02 06:44:03 -07001137 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001138 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001139 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001140 }
1141 }
hbosd82f5122016-12-09 04:12:39 -08001142 candidate_pair_stats->requests_received =
1143 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001144 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1145 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001146 candidate_pair_stats->responses_received =
1147 static_cast<uint64_t>(info.recv_ping_responses);
1148 candidate_pair_stats->responses_sent =
1149 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001150 RTC_DCHECK_GE(info.sent_ping_requests_total,
1151 info.sent_ping_requests_before_first_response);
1152 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1153 info.sent_ping_requests_total -
1154 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001155
1156 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001157 }
1158 }
1159 }
1160}
1161
Steve Anton57858b32018-02-15 15:19:50 -08001162void RTCStatsCollector::ProduceMediaStreamStats_s(
1163 int64_t timestamp_us,
1164 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001165 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001166
1167 std::map<std::string, std::vector<std::string>> track_ids;
1168
1169 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001170 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001171 std::string track_id =
1172 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1173 kSender, sender->internal()->AttachmentId());
1174 for (auto& stream_id : sender->stream_ids()) {
1175 track_ids[stream_id].push_back(track_id);
1176 }
1177 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001178 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001179 std::string track_id =
1180 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1181 kReceiver, receiver->internal()->AttachmentId());
1182 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001183 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001184 }
1185 }
1186 }
1187
1188 // Build stats for each stream ID known.
1189 for (auto& it : track_ids) {
1190 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1191 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1192 stream_stats->stream_identifier = it.first;
1193 stream_stats->track_ids = it.second;
1194 report->AddStats(std::move(stream_stats));
1195 }
1196}
1197
1198void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1199 int64_t timestamp_us,
1200 RTCStatsReport* report) const {
1201 RTC_DCHECK(signaling_thread_->IsCurrent());
1202 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1203 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001204 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001205 senders.push_back(sender->internal());
1206 }
1207 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1208 senders, report);
1209
1210 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001211 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001212 receivers.push_back(receiver->internal());
1213 }
1214 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1215 receivers, report);
1216 }
hbos09bc1282016-11-08 06:29:22 -08001217}
1218
hbos6ab97ce2016-10-03 14:16:56 -07001219void RTCStatsCollector::ProducePeerConnectionStats_s(
1220 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001221 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001222 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001223 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001224 stats->data_channels_opened = internal_record_.data_channels_opened;
1225 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001226 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001227}
1228
hbosdf6075a2016-12-19 04:58:02 -08001229void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001230 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001231 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001232 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001233 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001234
Steve Anton57858b32018-02-15 15:19:50 -08001235 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1236 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1237 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1238 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1239 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1240 } else {
1241 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001242 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001243 }
Steve Anton57858b32018-02-15 15:19:50 -08001244}
1245
1246void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1247 int64_t timestamp_us,
1248 const RtpTransceiverStatsInfo& stats,
1249 RTCStatsReport* report) const {
1250 if (!stats.mid || !stats.transport_name) {
1251 return;
1252 }
1253 RTC_DCHECK(stats.track_media_info_map);
1254 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1255 RTC_DCHECK(track_media_info_map.voice_media_info());
1256 std::string mid = *stats.mid;
1257 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1258 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1259 // Inbound
1260 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1261 track_media_info_map.voice_media_info()->receivers) {
1262 if (!voice_receiver_info.connected())
1263 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001264 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001265 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1266 timestamp_us);
1267 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1268 inbound_audio.get());
1269 // TODO(hta): This lookup should look for the sender, not the track.
1270 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1271 track_media_info_map.GetAudioTrack(voice_receiver_info);
1272 if (audio_track) {
1273 inbound_audio->track_id =
1274 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1275 kReceiver,
1276 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001277 }
Steve Anton57858b32018-02-15 15:19:50 -08001278 inbound_audio->transport_id = transport_id;
1279 report->AddStats(std::move(inbound_audio));
1280 }
1281 // Outbound
1282 for (const cricket::VoiceSenderInfo& voice_sender_info :
1283 track_media_info_map.voice_media_info()->senders) {
1284 if (!voice_sender_info.connected())
1285 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001286 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001287 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1288 timestamp_us);
1289 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1290 outbound_audio.get());
1291 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1292 track_media_info_map.GetAudioTrack(voice_sender_info);
1293 if (audio_track) {
1294 outbound_audio->track_id =
1295 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1296 kSender,
1297 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001298 }
Steve Anton57858b32018-02-15 15:19:50 -08001299 outbound_audio->transport_id = transport_id;
1300 report->AddStats(std::move(outbound_audio));
1301 }
1302}
1303
1304void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1305 int64_t timestamp_us,
1306 const RtpTransceiverStatsInfo& stats,
1307 RTCStatsReport* report) const {
1308 if (!stats.mid || !stats.transport_name) {
1309 return;
1310 }
1311 RTC_DCHECK(stats.track_media_info_map);
1312 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1313 RTC_DCHECK(track_media_info_map.video_media_info());
1314 std::string mid = *stats.mid;
1315 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1316 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1317 // Inbound
1318 for (const cricket::VideoReceiverInfo& video_receiver_info :
1319 track_media_info_map.video_media_info()->receivers) {
1320 if (!video_receiver_info.connected())
1321 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001322 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001323 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1324 timestamp_us);
1325 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1326 inbound_video.get());
1327 rtc::scoped_refptr<VideoTrackInterface> video_track =
1328 track_media_info_map.GetVideoTrack(video_receiver_info);
1329 if (video_track) {
1330 inbound_video->track_id =
1331 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1332 kReceiver,
1333 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1334 }
1335 inbound_video->transport_id = transport_id;
1336 report->AddStats(std::move(inbound_video));
1337 }
1338 // Outbound
1339 for (const cricket::VideoSenderInfo& video_sender_info :
1340 track_media_info_map.video_media_info()->senders) {
1341 if (!video_sender_info.connected())
1342 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001343 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001344 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1345 timestamp_us);
1346 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1347 outbound_video.get());
1348 rtc::scoped_refptr<VideoTrackInterface> video_track =
1349 track_media_info_map.GetVideoTrack(video_sender_info);
1350 if (video_track) {
1351 outbound_video->track_id =
1352 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1353 kSender,
1354 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1355 }
1356 outbound_video->transport_id = transport_id;
1357 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001358 }
1359}
1360
hbosdf6075a2016-12-19 04:58:02 -08001361void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001362 int64_t timestamp_us,
1363 const std::map<std::string, cricket::TransportStats>&
1364 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001365 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1366 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001367 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001368 for (const auto& entry : transport_stats_by_name) {
1369 const std::string& transport_name = entry.first;
1370 const cricket::TransportStats& transport_stats = entry.second;
1371
hbos2fa7c672016-10-24 04:00:05 -07001372 // Get reference to RTCP channel, if it exists.
1373 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001374 for (const cricket::TransportChannelStats& channel_stats :
1375 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001376 if (channel_stats.component ==
1377 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1378 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001379 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001380 break;
1381 }
1382 }
1383
1384 // Get reference to local and remote certificates of this transport, if they
1385 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001386 const auto& certificate_stats_it =
1387 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001388 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1389 std::string local_certificate_id;
1390 if (certificate_stats_it->second.local) {
1391 local_certificate_id = RTCCertificateIDFromFingerprint(
1392 certificate_stats_it->second.local->fingerprint);
1393 }
1394 std::string remote_certificate_id;
1395 if (certificate_stats_it->second.remote) {
1396 remote_certificate_id = RTCCertificateIDFromFingerprint(
1397 certificate_stats_it->second.remote->fingerprint);
1398 }
1399
1400 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001401 for (const cricket::TransportChannelStats& channel_stats :
1402 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001403 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001404 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1405 transport_name, channel_stats.component),
1406 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001407 transport_stats->bytes_sent = 0;
1408 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001409 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1410 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001411 for (const cricket::ConnectionInfo& info :
1412 channel_stats.connection_infos) {
1413 *transport_stats->bytes_sent += info.sent_total_bytes;
1414 *transport_stats->bytes_received += info.recv_total_bytes;
1415 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001416 transport_stats->selected_candidate_pair_id =
1417 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1418 }
1419 }
1420 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1421 !rtcp_transport_stats_id.empty()) {
1422 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1423 }
1424 if (!local_certificate_id.empty())
1425 transport_stats->local_certificate_id = local_certificate_id;
1426 if (!remote_certificate_id.empty())
1427 transport_stats->remote_certificate_id = remote_certificate_id;
1428 report->AddStats(std::move(transport_stats));
1429 }
1430 }
1431}
1432
1433std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001434RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001435 const std::map<std::string, cricket::TransportStats>&
1436 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001437 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001438 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001439 for (const auto& entry : transport_stats_by_name) {
1440 const std::string& transport_name = entry.first;
1441
hbos2fa7c672016-10-24 04:00:05 -07001442 CertificateStatsPair certificate_stats_pair;
1443 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001444 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001445 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001446 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001447 }
Steve Anton5dfde182018-02-06 10:34:40 -08001448
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001449 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1450 pc_->GetRemoteSSLCertChain(transport_name);
1451 if (remote_cert_chain) {
1452 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001453 }
Steve Anton5dfde182018-02-06 10:34:40 -08001454
hbos2fa7c672016-10-24 04:00:05 -07001455 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001456 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001457 }
1458 return transport_cert_stats;
1459}
1460
Steve Anton57858b32018-02-15 15:19:50 -08001461std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1462RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1463 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001464
Steve Anton57858b32018-02-15 15:19:50 -08001465 // These are used to invoke GetStats for all the media channels together in
1466 // one worker thread hop.
1467 std::map<cricket::VoiceMediaChannel*,
1468 std::unique_ptr<cricket::VoiceMediaInfo>>
1469 voice_stats;
1470 std::map<cricket::VideoMediaChannel*,
1471 std::unique_ptr<cricket::VideoMediaInfo>>
1472 video_stats;
1473
Mirko Bonadei739baf02019-01-27 17:29:42 +01001474 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001475 cricket::MediaType media_type = transceiver->media_type();
1476
1477 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1478 // stats have been fetched on the worker thread.
1479 transceiver_stats_infos.emplace_back();
1480 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1481 stats.transceiver = transceiver->internal();
1482 stats.media_type = media_type;
1483
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001484 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001485 if (!channel) {
1486 // The remaining fields require a BaseChannel.
1487 continue;
1488 }
1489
1490 stats.mid = channel->content_name();
1491 stats.transport_name = channel->transport_name();
1492
1493 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1494 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1495 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1496 voice_stats.end());
1497 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001498 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001499 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1500 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1501 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1502 video_stats.end());
1503 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001504 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001505 } else {
1506 RTC_NOTREACHED();
1507 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001508 }
Steve Anton57858b32018-02-15 15:19:50 -08001509
1510 // Call GetStats for all media channels together on the worker thread in one
1511 // hop.
1512 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1513 for (const auto& entry : voice_stats) {
1514 if (!entry.first->GetStats(entry.second.get())) {
1515 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1516 }
1517 }
1518 for (const auto& entry : video_stats) {
1519 if (!entry.first->GetStats(entry.second.get())) {
1520 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1521 }
1522 }
1523 });
1524
1525 // Create the TrackMediaInfoMap for each transceiver stats object.
1526 for (auto& stats : transceiver_stats_infos) {
1527 auto transceiver = stats.transceiver;
1528 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1529 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1530 if (transceiver->channel()) {
1531 cricket::MediaType media_type = transceiver->media_type();
1532 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1533 auto* voice_channel =
1534 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1535 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1536 voice_media_info =
1537 std::move(voice_stats[voice_channel->media_channel()]);
1538 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1539 auto* video_channel =
1540 static_cast<cricket::VideoChannel*>(transceiver->channel());
1541 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1542 video_media_info =
1543 std::move(video_stats[video_channel->media_channel()]);
1544 }
1545 }
1546 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001547 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001548 senders.push_back(sender->internal());
1549 }
1550 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001551 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001552 receivers.push_back(receiver->internal());
1553 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001554 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001555 std::move(voice_media_info), std::move(video_media_info), senders,
1556 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001557 }
Steve Anton57858b32018-02-15 15:19:50 -08001558
1559 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001560}
1561
Steve Anton7eca0932018-03-30 15:18:41 -07001562std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1563 std::set<std::string> transport_names;
1564 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1565 if (transceiver->internal()->channel()) {
1566 transport_names.insert(
1567 transceiver->internal()->channel()->transport_name());
1568 }
1569 }
1570 if (pc_->rtp_data_channel()) {
1571 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1572 }
1573 if (pc_->sctp_transport_name()) {
1574 transport_names.insert(*pc_->sctp_transport_name());
1575 }
1576 return transport_names;
1577}
1578
hbos82ebe022016-11-14 01:41:09 -08001579void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1580 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1581 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1582}
1583
1584void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1585 RTC_DCHECK(signaling_thread_->IsCurrent());
1586 bool result = internal_record_.opened_data_channels.insert(
1587 reinterpret_cast<uintptr_t>(channel)).second;
1588 ++internal_record_.data_channels_opened;
1589 RTC_DCHECK(result);
1590}
1591
1592void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1593 RTC_DCHECK(signaling_thread_->IsCurrent());
1594 // Only channels that have been fully opened (and have increased the
1595 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001596 if (internal_record_.opened_data_channels.erase(
1597 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001598 ++internal_record_.data_channels_closed;
1599 }
1600}
1601
hboscc555c52016-10-18 12:48:31 -07001602const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1603 const std::string& type) {
1604 return CandidateTypeToRTCIceCandidateType(type);
1605}
1606
1607const char* DataStateToRTCDataChannelStateForTesting(
1608 DataChannelInterface::DataState state) {
1609 return DataStateToRTCDataChannelState(state);
1610}
1611
hbosd565b732016-08-30 14:04:35 -07001612} // namespace webrtc