blob: df2f09ca0ff5d75f7ba22bd2e19628b411089097 [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"
Jonas Olsson43568dd2018-06-11 16:25:54 +020028#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070031
32namespace webrtc {
33
hboscc555c52016-10-18 12:48:31 -070034namespace {
35
hbos2fa7c672016-10-24 04:00:05 -070036std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
37 return "RTCCertificate_" + fingerprint;
38}
39
Steve Anton57858b32018-02-15 15:19:50 -080040std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
41 bool inbound,
42 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020043 char buf[1024];
44 rtc::SimpleStringBuilder sb(buf);
45 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
46 << payload_type;
47 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080048}
49
hbos2fa7c672016-10-24 04:00:05 -070050std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
51 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020052 char buf[4096];
53 rtc::SimpleStringBuilder sb(buf);
54 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
55 << info.remote_candidate.id();
56 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070057}
58
Harald Alvestranda3dab842018-01-14 09:18:58 +010059const char kSender[] = "sender";
60const char kReceiver[] = "receiver";
61
62std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
63 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010064 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020065 char buf[1024];
66 rtc::SimpleStringBuilder sb(buf);
67 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
68 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080069}
70
hbos2fa7c672016-10-24 04:00:05 -070071std::string RTCTransportStatsIDFromTransportChannel(
72 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020073 char buf[1024];
74 rtc::SimpleStringBuilder sb(buf);
75 sb << "RTCTransport_" << transport_name << "_" << channel_component;
76 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070077}
78
hboseeafe942016-11-01 03:00:17 -070079std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020080 char buf[1024];
81 rtc::SimpleStringBuilder sb(buf);
82 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
83 return sb.str();
hboseeafe942016-11-01 03:00:17 -070084}
85
hbos6ded1902016-11-01 01:50:46 -070086std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020087 char buf[1024];
88 rtc::SimpleStringBuilder sb(buf);
89 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
90 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070091}
92
hbosab9f6e42016-10-07 02:18:47 -070093const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
94 if (type == cricket::LOCAL_PORT_TYPE)
95 return RTCIceCandidateType::kHost;
96 if (type == cricket::STUN_PORT_TYPE)
97 return RTCIceCandidateType::kSrflx;
98 if (type == cricket::PRFLX_PORT_TYPE)
99 return RTCIceCandidateType::kPrflx;
100 if (type == cricket::RELAY_PORT_TYPE)
101 return RTCIceCandidateType::kRelay;
102 RTC_NOTREACHED();
103 return nullptr;
104}
105
hboscc555c52016-10-18 12:48:31 -0700106const char* DataStateToRTCDataChannelState(
107 DataChannelInterface::DataState state) {
108 switch (state) {
109 case DataChannelInterface::kConnecting:
110 return RTCDataChannelState::kConnecting;
111 case DataChannelInterface::kOpen:
112 return RTCDataChannelState::kOpen;
113 case DataChannelInterface::kClosing:
114 return RTCDataChannelState::kClosing;
115 case DataChannelInterface::kClosed:
116 return RTCDataChannelState::kClosed;
117 default:
118 RTC_NOTREACHED();
119 return nullptr;
120 }
121}
122
hbos06495bc2017-01-02 08:08:18 -0800123const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
124 cricket::IceCandidatePairState state) {
125 switch (state) {
126 case cricket::IceCandidatePairState::WAITING:
127 return RTCStatsIceCandidatePairState::kWaiting;
128 case cricket::IceCandidatePairState::IN_PROGRESS:
129 return RTCStatsIceCandidatePairState::kInProgress;
130 case cricket::IceCandidatePairState::SUCCEEDED:
131 return RTCStatsIceCandidatePairState::kSucceeded;
132 case cricket::IceCandidatePairState::FAILED:
133 return RTCStatsIceCandidatePairState::kFailed;
134 default:
135 RTC_NOTREACHED();
136 return nullptr;
137 }
138}
139
hbos7064d592017-01-16 07:38:02 -0800140const char* DtlsTransportStateToRTCDtlsTransportState(
141 cricket::DtlsTransportState state) {
142 switch (state) {
143 case cricket::DTLS_TRANSPORT_NEW:
144 return RTCDtlsTransportState::kNew;
145 case cricket::DTLS_TRANSPORT_CONNECTING:
146 return RTCDtlsTransportState::kConnecting;
147 case cricket::DTLS_TRANSPORT_CONNECTED:
148 return RTCDtlsTransportState::kConnected;
149 case cricket::DTLS_TRANSPORT_CLOSED:
150 return RTCDtlsTransportState::kClosed;
151 case cricket::DTLS_TRANSPORT_FAILED:
152 return RTCDtlsTransportState::kFailed;
153 default:
154 RTC_NOTREACHED();
155 return nullptr;
156 }
157}
158
Gary Liu37e489c2017-11-21 10:49:36 -0800159const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
160 switch (type) {
161 case rtc::ADAPTER_TYPE_CELLULAR:
162 return RTCNetworkType::kCellular;
163 case rtc::ADAPTER_TYPE_ETHERNET:
164 return RTCNetworkType::kEthernet;
165 case rtc::ADAPTER_TYPE_WIFI:
166 return RTCNetworkType::kWifi;
167 case rtc::ADAPTER_TYPE_VPN:
168 return RTCNetworkType::kVpn;
169 case rtc::ADAPTER_TYPE_UNKNOWN:
170 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700171 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800172 return RTCNetworkType::kUnknown;
173 }
174 RTC_NOTREACHED();
175 return nullptr;
176}
177
hbos9e302742017-01-20 02:47:10 -0800178double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
179 RTC_DCHECK_GE(audio_level, 0);
180 RTC_DCHECK_LE(audio_level, 32767);
181 return audio_level / 32767.0;
182}
183
hbos0adb8282016-11-23 02:32:06 -0800184std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800185 uint64_t timestamp_us,
186 const std::string& mid,
187 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800188 const RtpCodecParameters& codec_params) {
189 RTC_DCHECK_GE(codec_params.payload_type, 0);
190 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800191 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800192 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
193 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800194 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800195 timestamp_us));
196 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800197 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800198 if (codec_params.clock_rate) {
199 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
200 }
hbos0adb8282016-11-23 02:32:06 -0800201 return codec_stats;
202}
203
hbos09bc1282016-11-08 06:29:22 -0800204void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
205 const MediaStreamTrackInterface& track,
206 RTCMediaStreamTrackStats* track_stats) {
207 track_stats->track_identifier = track.id();
208 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
209}
210
hbos820f5782016-11-22 03:16:50 -0800211// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700212void SetInboundRTPStreamStatsFromMediaReceiverInfo(
213 const cricket::MediaReceiverInfo& media_receiver_info,
214 RTCInboundRTPStreamStats* inbound_stats) {
215 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800216 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100217 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700218 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700219 inbound_stats->packets_received =
220 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
221 inbound_stats->bytes_received =
222 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800223 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100224 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700225 inbound_stats->fraction_lost =
226 static_cast<double>(media_receiver_info.fraction_lost);
227}
228
229void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800230 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700231 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800232 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700233 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800234 voice_receiver_info, inbound_audio);
235 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200236 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800237 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800238 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
239 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800240 }
hbos820f5782016-11-22 03:16:50 -0800241 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700242 static_cast<double>(voice_receiver_info.jitter_ms) /
243 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800244 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
245 // purposefully left undefined for audio.
hboseeafe942016-11-01 03:00:17 -0700246}
247
248void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800249 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700250 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800251 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700252 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800253 video_receiver_info, inbound_video);
254 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200255 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800256 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800257 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
258 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800259 }
hbos820f5782016-11-22 03:16:50 -0800260 inbound_video->fir_count =
261 static_cast<uint32_t>(video_receiver_info.firs_sent);
262 inbound_video->pli_count =
263 static_cast<uint32_t>(video_receiver_info.plis_sent);
264 inbound_video->nack_count =
265 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800266 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800267 if (video_receiver_info.qp_sum)
268 inbound_video->qp_sum = *video_receiver_info.qp_sum;
hboseeafe942016-11-01 03:00:17 -0700269}
270
hbos820f5782016-11-22 03:16:50 -0800271// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700272void SetOutboundRTPStreamStatsFromMediaSenderInfo(
273 const cricket::MediaSenderInfo& media_sender_info,
274 RTCOutboundRTPStreamStats* outbound_stats) {
275 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800276 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100277 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700278 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700279 outbound_stats->packets_sent =
280 static_cast<uint32_t>(media_sender_info.packets_sent);
281 outbound_stats->bytes_sent =
282 static_cast<uint64_t>(media_sender_info.bytes_sent);
hbos6ded1902016-11-01 01:50:46 -0700283}
284
285void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800286 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700287 const cricket::VoiceSenderInfo& voice_sender_info,
288 RTCOutboundRTPStreamStats* outbound_audio) {
289 SetOutboundRTPStreamStatsFromMediaSenderInfo(
290 voice_sender_info, outbound_audio);
291 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200292 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800293 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800294 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
295 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800296 }
hbos6ded1902016-11-01 01:50:46 -0700297 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
298 // purposefully left undefined for audio.
299}
300
301void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800302 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700303 const cricket::VideoSenderInfo& video_sender_info,
304 RTCOutboundRTPStreamStats* outbound_video) {
305 SetOutboundRTPStreamStatsFromMediaSenderInfo(
306 video_sender_info, outbound_video);
307 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200308 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800309 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800310 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
311 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800312 }
hbos6ded1902016-11-01 01:50:46 -0700313 outbound_video->fir_count =
314 static_cast<uint32_t>(video_sender_info.firs_rcvd);
315 outbound_video->pli_count =
316 static_cast<uint32_t>(video_sender_info.plis_rcvd);
317 outbound_video->nack_count =
318 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800319 if (video_sender_info.qp_sum)
320 outbound_video->qp_sum = *video_sender_info.qp_sum;
321 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200322 outbound_video->total_encode_time =
323 static_cast<double>(video_sender_info.total_encode_time_ms) /
324 rtc::kNumMillisecsPerSec;
hbos6ded1902016-11-01 01:50:46 -0700325}
326
hbos02ba2112016-10-28 05:14:53 -0700327void ProduceCertificateStatsFromSSLCertificateStats(
328 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
329 RTCStatsReport* report) {
330 RTCCertificateStats* prev_certificate_stats = nullptr;
331 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
332 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800333 std::string certificate_stats_id =
334 RTCCertificateIDFromFingerprint(s->fingerprint);
335 // It is possible for the same certificate to show up multiple times, e.g.
336 // if local and remote side use the same certificate in a loopback call.
337 // If the report already contains stats for this certificate, skip it.
338 if (report->Get(certificate_stats_id)) {
339 RTC_DCHECK_EQ(s, &certificate_stats);
340 break;
341 }
hbos02ba2112016-10-28 05:14:53 -0700342 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800343 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700344 certificate_stats->fingerprint = s->fingerprint;
345 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
346 certificate_stats->base64_certificate = s->base64_certificate;
347 if (prev_certificate_stats)
348 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
349 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
350 prev_certificate_stats = certificate_stats;
351 }
352}
353
354const std::string& ProduceIceCandidateStats(
355 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800356 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700357 const std::string& id = "RTCIceCandidate_" + candidate.id();
358 const RTCStats* stats = report->Get(id);
359 if (!stats) {
360 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
361 if (is_local)
362 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
363 else
364 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800365 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800366 if (is_local) {
367 candidate_stats->network_type =
368 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200369 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
370 std::string relay_protocol = candidate.relay_protocol();
371 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
372 relay_protocol.compare("tcp") == 0 ||
373 relay_protocol.compare("tls") == 0);
374 candidate_stats->relay_protocol = relay_protocol;
375 }
Gary Liu37e489c2017-11-21 10:49:36 -0800376 } else {
377 // We don't expect to know the adapter type of remote candidates.
378 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
379 }
hbos02ba2112016-10-28 05:14:53 -0700380 candidate_stats->ip = candidate.address().ipaddr().ToString();
381 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
382 candidate_stats->protocol = candidate.protocol();
383 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
384 candidate.type());
385 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
386
387 stats = candidate_stats.get();
388 report->AddStats(std::move(candidate_stats));
389 }
390 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
391 : RTCRemoteIceCandidateStats::kType);
392 return stats->id();
393}
394
hbos9e302742017-01-20 02:47:10 -0800395std::unique_ptr<RTCMediaStreamTrackStats>
396ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
397 int64_t timestamp_us,
398 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100399 const cricket::VoiceSenderInfo& voice_sender_info,
400 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800401 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
402 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100403 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
404 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100405 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800406 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
407 audio_track, audio_track_stats.get());
408 audio_track_stats->remote_source = false;
409 audio_track_stats->detached = false;
410 if (voice_sender_info.audio_level >= 0) {
411 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
412 voice_sender_info.audio_level);
413 }
zsteine76bd3a2017-07-14 12:17:49 -0700414 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
415 audio_track_stats->total_samples_duration =
416 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100417 if (voice_sender_info.apm_statistics.echo_return_loss) {
418 audio_track_stats->echo_return_loss =
419 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800420 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100421 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
422 audio_track_stats->echo_return_loss_enhancement =
423 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800424 }
425 return audio_track_stats;
426}
427
428std::unique_ptr<RTCMediaStreamTrackStats>
429ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
430 int64_t timestamp_us,
431 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100432 const cricket::VoiceReceiverInfo& voice_receiver_info,
433 int attachment_id) {
434 // Since receiver tracks can't be reattached, we use the SSRC as
435 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800436 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
437 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100438 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
439 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100440 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800441 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
442 audio_track, audio_track_stats.get());
443 audio_track_stats->remote_source = true;
444 audio_track_stats->detached = false;
445 if (voice_receiver_info.audio_level >= 0) {
446 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
447 voice_receiver_info.audio_level);
448 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200449 audio_track_stats->jitter_buffer_delay =
450 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100451 audio_track_stats->jitter_buffer_emitted_count =
452 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 12:17:49 -0700453 audio_track_stats->total_audio_energy =
454 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700455 audio_track_stats->total_samples_received =
456 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700457 audio_track_stats->total_samples_duration =
458 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700459 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200460 audio_track_stats->concealment_events =
461 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100462 audio_track_stats->jitter_buffer_flushes =
463 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100464 audio_track_stats->delayed_packet_outage_samples =
465 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100466 audio_track_stats->relative_packet_arrival_delay =
467 voice_receiver_info.relative_packet_arrival_delay_seconds;
hbos9e302742017-01-20 02:47:10 -0800468 return audio_track_stats;
469}
470
471std::unique_ptr<RTCMediaStreamTrackStats>
472ProduceMediaStreamTrackStatsFromVideoSenderInfo(
473 int64_t timestamp_us,
474 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100475 const cricket::VideoSenderInfo& video_sender_info,
476 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800477 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
478 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100479 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
480
481 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100482 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800483 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
484 video_track, video_track_stats.get());
485 video_track_stats->remote_source = false;
486 video_track_stats->detached = false;
487 video_track_stats->frame_width = static_cast<uint32_t>(
488 video_sender_info.send_frame_width);
489 video_track_stats->frame_height = static_cast<uint32_t>(
490 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800491 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100492 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800493 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100494 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800495 return video_track_stats;
496}
497
498std::unique_ptr<RTCMediaStreamTrackStats>
499ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
500 int64_t timestamp_us,
501 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100502 const cricket::VideoReceiverInfo& video_receiver_info,
503 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800504 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
505 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100506 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
507
508 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100509 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800510 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
511 video_track, video_track_stats.get());
512 video_track_stats->remote_source = true;
513 video_track_stats->detached = false;
514 if (video_receiver_info.frame_width > 0 &&
515 video_receiver_info.frame_height > 0) {
516 video_track_stats->frame_width = static_cast<uint32_t>(
517 video_receiver_info.frame_width);
518 video_track_stats->frame_height = static_cast<uint32_t>(
519 video_receiver_info.frame_height);
520 }
hbos42f6d2f2017-01-20 03:56:50 -0800521 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800522 // TODO(hbos): When we support receiving simulcast, this should be the total
523 // number of frames correctly decoded, independent of which SSRC it was
524 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100525 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800526 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800527 RTC_DCHECK_GE(video_receiver_info.frames_received,
528 video_receiver_info.frames_rendered);
529 video_track_stats->frames_dropped = video_receiver_info.frames_received -
530 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100531 video_track_stats->freeze_count = video_receiver_info.freeze_count;
532 video_track_stats->pause_count = video_receiver_info.pause_count;
533 video_track_stats->total_freezes_duration =
534 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
535 rtc::kNumMillisecsPerSec;
536 video_track_stats->total_pauses_duration =
537 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
538 rtc::kNumMillisecsPerSec;
539 video_track_stats->total_frames_duration =
540 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
541 rtc::kNumMillisecsPerSec;
542 video_track_stats->sum_squared_frame_durations =
543 video_receiver_info.sum_squared_frame_durations;
544
hbos9e302742017-01-20 02:47:10 -0800545 return video_track_stats;
546}
547
Harald Alvestrand89061872018-01-02 14:08:34 +0100548void ProduceSenderMediaTrackStats(
549 int64_t timestamp_us,
550 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800551 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100552 RTCStatsReport* report) {
553 // This function iterates over the senders to generate outgoing track stats.
554
555 // TODO(hbos): Return stats of detached tracks. We have to perform stats
556 // gathering at the time of detachment to get accurate stats and timestamps.
557 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100558 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100559 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
560 AudioTrackInterface* track =
561 static_cast<AudioTrackInterface*>(sender->track().get());
562 if (!track)
563 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100564 cricket::VoiceSenderInfo null_sender_info;
565 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
566 // TODO(hta): Checking on ssrc is not proper. There should be a way
567 // to see from a sender whether it's connected or not.
568 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800569 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100570 // When pc.close is called, sender info is discarded, so
571 // we generate zeroes instead. Bug: It should be retained.
572 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800573 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100574 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100575 if (sender_info) {
576 voice_sender_info = sender_info;
577 } else {
578 RTC_LOG(LS_INFO)
579 << "RTCStatsCollector: No voice sender info for sender with ssrc "
580 << sender->ssrc();
581 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100582 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100583 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100584 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
585 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100586 report->AddStats(std::move(audio_track_stats));
587 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
588 VideoTrackInterface* track =
589 static_cast<VideoTrackInterface*>(sender->track().get());
590 if (!track)
591 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100592 cricket::VideoSenderInfo null_sender_info;
593 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
594 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100595 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
596 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800597 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100598 // When pc.close is called, sender info is discarded, so
599 // we generate zeroes instead. Bug: It should be retained.
600 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800601 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100602 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100603 if (sender_info) {
604 video_sender_info = sender_info;
605 } else {
606 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
607 << sender->ssrc();
608 }
609 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100610 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100611 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
612 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100613 report->AddStats(std::move(video_track_stats));
614 } else {
615 RTC_NOTREACHED();
616 }
617 }
618}
619
620void ProduceReceiverMediaTrackStats(
621 int64_t timestamp_us,
622 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800623 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100624 RTCStatsReport* report) {
625 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100626 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100627 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
628 AudioTrackInterface* track =
629 static_cast<AudioTrackInterface*>(receiver->track().get());
630 const cricket::VoiceReceiverInfo* voice_receiver_info =
631 track_media_info_map.GetVoiceReceiverInfo(*track);
632 if (!voice_receiver_info) {
633 continue;
634 }
635 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
636 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100637 timestamp_us, *track, *voice_receiver_info,
638 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100639 report->AddStats(std::move(audio_track_stats));
640 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
641 VideoTrackInterface* track =
642 static_cast<VideoTrackInterface*>(receiver->track().get());
643 const cricket::VideoReceiverInfo* video_receiver_info =
644 track_media_info_map.GetVideoReceiverInfo(*track);
645 if (!video_receiver_info) {
646 continue;
647 }
648 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
649 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100650 timestamp_us, *track, *video_receiver_info,
651 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100652 report->AddStats(std::move(video_track_stats));
653 } else {
654 RTC_NOTREACHED();
655 }
656 }
657}
658
Henrik Boström5b3541f2018-03-19 13:52:56 +0100659rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
660 bool filter_by_sender_selector,
661 rtc::scoped_refptr<const RTCStatsReport> report,
662 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
663 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
664 std::vector<std::string> rtpstream_ids;
665 if (filter_by_sender_selector) {
666 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
667 if (sender_selector) {
668 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
669 // reference the sender stats.
670 // Because we do not implement sender stats, we look at outbound-rtp(s)
671 // that reference the track attachment stats for the sender instead.
672 std::string track_id =
673 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
674 kSender, sender_selector->AttachmentId());
675 for (const auto& stats : *report) {
676 if (stats.type() != RTCOutboundRTPStreamStats::kType)
677 continue;
678 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
679 if (outbound_rtp.track_id.is_defined() &&
680 *outbound_rtp.track_id == track_id) {
681 rtpstream_ids.push_back(outbound_rtp.id());
682 }
683 }
684 }
685 } else {
686 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
687 if (receiver_selector) {
688 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
689 // reference the receiver stats.
690 // Because we do not implement receiver stats, we look at inbound-rtp(s)
691 // that reference the track attachment stats for the receiver instead.
692 std::string track_id =
693 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
694 kReceiver, receiver_selector->AttachmentId());
695 for (const auto& stats : *report) {
696 if (stats.type() != RTCInboundRTPStreamStats::kType)
697 continue;
698 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
699 if (inbound_rtp.track_id.is_defined() &&
700 *inbound_rtp.track_id == track_id) {
701 rtpstream_ids.push_back(inbound_rtp.id());
702 }
703 }
704 }
705 }
706 if (rtpstream_ids.empty())
707 return RTCStatsReport::Create(report->timestamp_us());
708 return TakeReferencedStats(report->Copy(), rtpstream_ids);
709}
710
hboscc555c52016-10-18 12:48:31 -0700711} // namespace
712
Henrik Boström5b3541f2018-03-19 13:52:56 +0100713RTCStatsCollector::RequestInfo::RequestInfo(
714 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
715 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
716
717RTCStatsCollector::RequestInfo::RequestInfo(
718 rtc::scoped_refptr<RtpSenderInternal> selector,
719 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
720 : RequestInfo(FilterMode::kSenderSelector,
721 std::move(callback),
722 std::move(selector),
723 nullptr) {}
724
725RTCStatsCollector::RequestInfo::RequestInfo(
726 rtc::scoped_refptr<RtpReceiverInternal> selector,
727 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
728 : RequestInfo(FilterMode::kReceiverSelector,
729 std::move(callback),
730 nullptr,
731 std::move(selector)) {}
732
733RTCStatsCollector::RequestInfo::RequestInfo(
734 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
735 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
736 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
737 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
738 : filter_mode_(filter_mode),
739 callback_(std::move(callback)),
740 sender_selector_(std::move(sender_selector)),
741 receiver_selector_(std::move(receiver_selector)) {
742 RTC_DCHECK(callback_);
743 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
744}
745
hbosc82f2e12016-09-05 01:36:50 -0700746rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800747 PeerConnectionInternal* pc,
748 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700749 return rtc::scoped_refptr<RTCStatsCollector>(
750 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
751}
752
Steve Anton2d8609c2018-01-23 16:38:46 -0800753RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700754 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700755 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700756 signaling_thread_(pc->signaling_thread()),
757 worker_thread_(pc->worker_thread()),
758 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700759 num_pending_partial_reports_(0),
760 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100761 network_report_event_(true /* manual_reset */,
762 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700763 cache_timestamp_us_(0),
764 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700765 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700766 RTC_DCHECK(signaling_thread_);
767 RTC_DCHECK(worker_thread_);
768 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700769 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800770 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800771 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700772}
773
hbosb78306a2016-12-19 05:06:57 -0800774RTCStatsCollector::~RTCStatsCollector() {
775 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
776}
777
hbosc82f2e12016-09-05 01:36:50 -0700778void RTCStatsCollector::GetStatsReport(
779 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100780 GetStatsReportInternal(RequestInfo(std::move(callback)));
781}
782
783void RTCStatsCollector::GetStatsReport(
784 rtc::scoped_refptr<RtpSenderInternal> selector,
785 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
786 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
787}
788
789void RTCStatsCollector::GetStatsReport(
790 rtc::scoped_refptr<RtpReceiverInternal> selector,
791 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
792 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
793}
794
795void RTCStatsCollector::GetStatsReportInternal(
796 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700797 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100798 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700799
hbos0e6758d2016-08-31 07:57:36 -0700800 // "Now" using a monotonically increasing timer.
801 int64_t cache_now_us = rtc::TimeMicros();
802 if (cached_report_ &&
803 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800804 // We have a fresh cached report to deliver. Deliver asynchronously, since
805 // the caller may not be expecting a synchronous callback, and it avoids
806 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100807 std::vector<RequestInfo> requests;
808 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100809 signaling_thread_->PostTask(
810 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
811 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700812 } else if (!num_pending_partial_reports_) {
813 // Only start gathering stats if we're not already gathering stats. In the
814 // case of already gathering stats, |callback_| will be invoked when there
815 // are no more pending partial reports.
816
817 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
818 // UTC), in microseconds. The system clock could be modified and is not
819 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700820 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700821
hbosf415f8a2017-01-02 04:28:51 -0800822 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700823 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800824
Steve Anton57858b32018-02-15 15:19:50 -0800825 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800826 // |ProducePartialResultsOnNetworkThread| and
827 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800828 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700829 // Prepare |transport_names_| for use in
830 // |ProducePartialResultsOnNetworkThread|.
831 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800832
stefanf79ade12017-06-02 06:44:03 -0700833 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
834 // thread.
835 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
836 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700837 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700838
Henrik Boström40b030e2019-02-28 09:49:31 +0100839 // Don't touch |network_report_| on the signaling thread until
840 // ProducePartialResultsOnNetworkThread() has signaled the
841 // |network_report_event_|.
842 network_report_event_.Reset();
843 network_thread_->PostTask(
844 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700845 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100846 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800847 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700848 }
hbosd565b732016-08-30 14:04:35 -0700849}
850
851void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700852 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700853 cached_report_ = nullptr;
854}
855
hbosb78306a2016-12-19 05:06:57 -0800856void RTCStatsCollector::WaitForPendingRequest() {
857 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100858 // If a request is pending, blocks until the |network_report_event_| is
859 // signaled and then delivers the result. Otherwise this is a NO-OP.
860 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800861}
862
hbosc82f2e12016-09-05 01:36:50 -0700863void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
864 int64_t timestamp_us) {
865 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100866 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700867
Henrik Boström40b030e2019-02-28 09:49:31 +0100868 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
869 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700870
Henrik Boström40b030e2019-02-28 09:49:31 +0100871 // ProducePartialResultsOnSignalingThread() is running synchronously on the
872 // signaling thread, so it is always the first partial result delivered on the
873 // signaling thread. The request is not complete until MergeNetworkReport_s()
874 // happens; we don't have to do anything here.
875 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
876 --num_pending_partial_reports_;
877}
878
879void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
880 int64_t timestamp_us,
881 RTCStatsReport* partial_report) {
882 RTC_DCHECK(signaling_thread_->IsCurrent());
883 ProduceDataChannelStats_s(timestamp_us, partial_report);
884 ProduceMediaStreamStats_s(timestamp_us, partial_report);
885 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
886 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700887}
888
hbosc82f2e12016-09-05 01:36:50 -0700889void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
890 int64_t timestamp_us) {
891 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100892 // Touching |network_report_| on this thread is safe by this method because
893 // |network_report_event_| is reset before this method is invoked.
894 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700895
Steve Anton5dfde182018-02-06 10:34:40 -0800896 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700897 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800898 std::map<std::string, CertificateStatsPair> transport_cert_stats =
899 PrepareTransportCertificateStats_n(transport_stats_by_name);
900
Henrik Boström40b030e2019-02-28 09:49:31 +0100901 ProducePartialResultsOnNetworkThreadImpl(
902 timestamp_us, transport_stats_by_name, transport_cert_stats,
903 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000904
Henrik Boström40b030e2019-02-28 09:49:31 +0100905 // Signal that it is now safe to touch |network_report_| on the signaling
906 // thread, and post a task to merge it into the final results.
907 network_report_event_.Set();
908 signaling_thread_->PostTask(
909 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100910}
911
Henrik Boström40b030e2019-02-28 09:49:31 +0100912void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
913 int64_t timestamp_us,
914 const std::map<std::string, cricket::TransportStats>&
915 transport_stats_by_name,
916 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
917 RTCStatsReport* partial_report) {
918 RTC_DCHECK(network_thread_->IsCurrent());
919 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
920 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
921 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
922 call_stats_, partial_report);
923 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
924 partial_report);
925 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
926 transport_cert_stats, partial_report);
927}
928
929void RTCStatsCollector::MergeNetworkReport_s() {
930 RTC_DCHECK(signaling_thread_->IsCurrent());
931 // The |network_report_event_| must be signaled for it to be safe to touch
932 // |network_report_|. This is normally not blocking, but if
933 // WaitForPendingRequest() is called while a request is pending, we might have
934 // to wait until the network thread is done touching |network_report_|.
935 network_report_event_.Wait(rtc::Event::kForever);
936 if (!network_report_) {
937 // Normally, MergeNetworkReport_s() is executed because it is posted from
938 // the network thread. But if WaitForPendingRequest() is called while a
939 // request is pending, an early call to MergeNetworkReport_s() is made,
940 // merging the report and setting |network_report_| to null. If so, when the
941 // previously posted MergeNetworkReport_s() is later executed, the report is
942 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700943 return;
944 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000945 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100946 RTC_DCHECK(partial_report_);
947 partial_report_->TakeMembersFrom(network_report_);
948 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000949 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100950 // |network_report_| is currently the only partial report collected
951 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
952 // ready to deliver the result.
953 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
954 cache_timestamp_us_ = partial_report_timestamp_us_;
955 cached_report_ = partial_report_;
956 partial_report_ = nullptr;
957 transceiver_stats_infos_.clear();
958 // Trace WebRTC Stats when getStats is called on Javascript.
959 // This allows access to WebRTC stats from trace logs. To enable them,
960 // select the "webrtc_stats" category when recording traces.
961 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
962 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000963
Henrik Boström40b030e2019-02-28 09:49:31 +0100964 // Deliver report and clear |requests_|.
965 std::vector<RequestInfo> requests;
966 requests.swap(requests_);
967 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700968}
969
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800970void RTCStatsCollector::DeliverCachedReport(
971 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100972 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700973 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100974 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800975 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800976
Henrik Boström5b3541f2018-03-19 13:52:56 +0100977 for (const RequestInfo& request : requests) {
978 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
979 request.callback()->OnStatsDelivered(cached_report);
980 } else {
981 bool filter_by_sender_selector;
982 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
983 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
984 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
985 filter_by_sender_selector = true;
986 sender_selector = request.sender_selector();
987 } else {
988 RTC_DCHECK(request.filter_mode() ==
989 RequestInfo::FilterMode::kReceiverSelector);
990 filter_by_sender_selector = false;
991 receiver_selector = request.receiver_selector();
992 }
993 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
994 filter_by_sender_selector, cached_report, sender_selector,
995 receiver_selector));
996 }
hbosc82f2e12016-09-05 01:36:50 -0700997 }
hbosd565b732016-08-30 14:04:35 -0700998}
999
hbosdf6075a2016-12-19 04:58:02 -08001000void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001001 int64_t timestamp_us,
1002 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001003 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001004 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001005 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1006 if (transport_cert_stats_pair.second.local) {
1007 ProduceCertificateStatsFromSSLCertificateStats(
1008 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001009 }
hbos02ba2112016-10-28 05:14:53 -07001010 if (transport_cert_stats_pair.second.remote) {
1011 ProduceCertificateStatsFromSSLCertificateStats(
1012 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001013 }
1014 }
1015}
1016
hbosdf6075a2016-12-19 04:58:02 -08001017void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001018 int64_t timestamp_us,
1019 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001020 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001021 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001022 for (const auto& stats : transceiver_stats_infos) {
1023 if (!stats.mid) {
1024 continue;
hbos0adb8282016-11-23 02:32:06 -08001025 }
Steve Anton57858b32018-02-15 15:19:50 -08001026 const cricket::VoiceMediaInfo* voice_media_info =
1027 stats.track_media_info_map->voice_media_info();
1028 const cricket::VideoMediaInfo* video_media_info =
1029 stats.track_media_info_map->video_media_info();
1030 // Audio
1031 if (voice_media_info) {
1032 // Inbound
1033 for (const auto& pair : voice_media_info->receive_codecs) {
1034 report->AddStats(CodecStatsFromRtpCodecParameters(
1035 timestamp_us, *stats.mid, true, pair.second));
1036 }
1037 // Outbound
1038 for (const auto& pair : voice_media_info->send_codecs) {
1039 report->AddStats(CodecStatsFromRtpCodecParameters(
1040 timestamp_us, *stats.mid, false, pair.second));
1041 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001042 }
Steve Anton57858b32018-02-15 15:19:50 -08001043 // Video
1044 if (video_media_info) {
1045 // Inbound
1046 for (const auto& pair : video_media_info->receive_codecs) {
1047 report->AddStats(CodecStatsFromRtpCodecParameters(
1048 timestamp_us, *stats.mid, true, pair.second));
1049 }
1050 // Outbound
1051 for (const auto& pair : video_media_info->send_codecs) {
1052 report->AddStats(CodecStatsFromRtpCodecParameters(
1053 timestamp_us, *stats.mid, false, pair.second));
1054 }
hbos0adb8282016-11-23 02:32:06 -08001055 }
1056 }
1057}
1058
hboscc555c52016-10-18 12:48:31 -07001059void RTCStatsCollector::ProduceDataChannelStats_s(
1060 int64_t timestamp_us, RTCStatsReport* report) const {
1061 RTC_DCHECK(signaling_thread_->IsCurrent());
1062 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1063 pc_->sctp_data_channels()) {
1064 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1065 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001066 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001067 timestamp_us));
1068 data_channel_stats->label = data_channel->label();
1069 data_channel_stats->protocol = data_channel->protocol();
1070 data_channel_stats->datachannelid = data_channel->id();
1071 data_channel_stats->state =
1072 DataStateToRTCDataChannelState(data_channel->state());
1073 data_channel_stats->messages_sent = data_channel->messages_sent();
1074 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1075 data_channel_stats->messages_received = data_channel->messages_received();
1076 data_channel_stats->bytes_received = data_channel->bytes_received();
1077 report->AddStats(std::move(data_channel_stats));
1078 }
1079}
1080
hbosdf6075a2016-12-19 04:58:02 -08001081void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001082 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001083 const std::map<std::string, cricket::TransportStats>&
1084 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001085 const Call::Stats& call_stats,
1086 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001087 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001088 for (const auto& entry : transport_stats_by_name) {
1089 const std::string& transport_name = entry.first;
1090 const cricket::TransportStats& transport_stats = entry.second;
1091 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001092 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001093 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001094 for (const cricket::ConnectionInfo& info :
1095 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001096 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001097 new RTCIceCandidatePairStats(
1098 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1099 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001100
hbos0583b282016-11-30 01:50:14 -08001101 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001102 // TODO(hbos): There could be other candidates that are not paired with
1103 // anything. We don't have a complete list. Local candidates come from
1104 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001105 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001106 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001107 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001108 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001109 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001110 candidate_pair_stats->state =
1111 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1112 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001113 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001114 // TODO(hbos): This writable is different than the spec. It goes to
1115 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001116 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001117 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001118 candidate_pair_stats->bytes_sent =
1119 static_cast<uint64_t>(info.sent_total_bytes);
1120 candidate_pair_stats->bytes_received =
1121 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001122 candidate_pair_stats->total_round_trip_time =
1123 static_cast<double>(info.total_round_trip_time_ms) /
1124 rtc::kNumMillisecsPerSec;
1125 if (info.current_round_trip_time_ms) {
1126 candidate_pair_stats->current_round_trip_time =
1127 static_cast<double>(*info.current_round_trip_time_ms) /
1128 rtc::kNumMillisecsPerSec;
1129 }
stefanf79ade12017-06-02 06:44:03 -07001130 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001131 // The bandwidth estimations we have are for the selected candidate
1132 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001133 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1134 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1135 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001136 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001137 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001138 }
stefanf79ade12017-06-02 06:44:03 -07001139 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001140 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001141 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001142 }
1143 }
hbosd82f5122016-12-09 04:12:39 -08001144 candidate_pair_stats->requests_received =
1145 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001146 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1147 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001148 candidate_pair_stats->responses_received =
1149 static_cast<uint64_t>(info.recv_ping_responses);
1150 candidate_pair_stats->responses_sent =
1151 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001152 RTC_DCHECK_GE(info.sent_ping_requests_total,
1153 info.sent_ping_requests_before_first_response);
1154 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1155 info.sent_ping_requests_total -
1156 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001157
1158 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001159 }
1160 }
1161 }
1162}
1163
Steve Anton57858b32018-02-15 15:19:50 -08001164void RTCStatsCollector::ProduceMediaStreamStats_s(
1165 int64_t timestamp_us,
1166 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001167 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001168
1169 std::map<std::string, std::vector<std::string>> track_ids;
1170
1171 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001172 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001173 std::string track_id =
1174 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1175 kSender, sender->internal()->AttachmentId());
1176 for (auto& stream_id : sender->stream_ids()) {
1177 track_ids[stream_id].push_back(track_id);
1178 }
1179 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001180 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001181 std::string track_id =
1182 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1183 kReceiver, receiver->internal()->AttachmentId());
1184 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001185 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001186 }
1187 }
1188 }
1189
1190 // Build stats for each stream ID known.
1191 for (auto& it : track_ids) {
1192 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1193 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1194 stream_stats->stream_identifier = it.first;
1195 stream_stats->track_ids = it.second;
1196 report->AddStats(std::move(stream_stats));
1197 }
1198}
1199
1200void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1201 int64_t timestamp_us,
1202 RTCStatsReport* report) const {
1203 RTC_DCHECK(signaling_thread_->IsCurrent());
1204 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1205 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001206 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001207 senders.push_back(sender->internal());
1208 }
1209 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1210 senders, report);
1211
1212 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001213 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001214 receivers.push_back(receiver->internal());
1215 }
1216 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1217 receivers, report);
1218 }
hbos09bc1282016-11-08 06:29:22 -08001219}
1220
hbos6ab97ce2016-10-03 14:16:56 -07001221void RTCStatsCollector::ProducePeerConnectionStats_s(
1222 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001223 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001224 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001225 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001226 stats->data_channels_opened = internal_record_.data_channels_opened;
1227 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001228 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001229}
1230
hbosdf6075a2016-12-19 04:58:02 -08001231void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001232 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001233 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001234 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001235 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001236
Steve Anton57858b32018-02-15 15:19:50 -08001237 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1238 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1239 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1240 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1241 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1242 } else {
1243 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001244 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001245 }
Steve Anton57858b32018-02-15 15:19:50 -08001246}
1247
1248void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1249 int64_t timestamp_us,
1250 const RtpTransceiverStatsInfo& stats,
1251 RTCStatsReport* report) const {
1252 if (!stats.mid || !stats.transport_name) {
1253 return;
1254 }
1255 RTC_DCHECK(stats.track_media_info_map);
1256 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1257 RTC_DCHECK(track_media_info_map.voice_media_info());
1258 std::string mid = *stats.mid;
1259 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1260 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1261 // Inbound
1262 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1263 track_media_info_map.voice_media_info()->receivers) {
1264 if (!voice_receiver_info.connected())
1265 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001266 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001267 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1268 timestamp_us);
1269 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1270 inbound_audio.get());
1271 // TODO(hta): This lookup should look for the sender, not the track.
1272 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1273 track_media_info_map.GetAudioTrack(voice_receiver_info);
1274 if (audio_track) {
1275 inbound_audio->track_id =
1276 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1277 kReceiver,
1278 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001279 }
Steve Anton57858b32018-02-15 15:19:50 -08001280 inbound_audio->transport_id = transport_id;
1281 report->AddStats(std::move(inbound_audio));
1282 }
1283 // Outbound
1284 for (const cricket::VoiceSenderInfo& voice_sender_info :
1285 track_media_info_map.voice_media_info()->senders) {
1286 if (!voice_sender_info.connected())
1287 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001288 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001289 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1290 timestamp_us);
1291 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1292 outbound_audio.get());
1293 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1294 track_media_info_map.GetAudioTrack(voice_sender_info);
1295 if (audio_track) {
1296 outbound_audio->track_id =
1297 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1298 kSender,
1299 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001300 }
Steve Anton57858b32018-02-15 15:19:50 -08001301 outbound_audio->transport_id = transport_id;
1302 report->AddStats(std::move(outbound_audio));
1303 }
1304}
1305
1306void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1307 int64_t timestamp_us,
1308 const RtpTransceiverStatsInfo& stats,
1309 RTCStatsReport* report) const {
1310 if (!stats.mid || !stats.transport_name) {
1311 return;
1312 }
1313 RTC_DCHECK(stats.track_media_info_map);
1314 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1315 RTC_DCHECK(track_media_info_map.video_media_info());
1316 std::string mid = *stats.mid;
1317 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1318 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1319 // Inbound
1320 for (const cricket::VideoReceiverInfo& video_receiver_info :
1321 track_media_info_map.video_media_info()->receivers) {
1322 if (!video_receiver_info.connected())
1323 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001324 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001325 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1326 timestamp_us);
1327 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1328 inbound_video.get());
1329 rtc::scoped_refptr<VideoTrackInterface> video_track =
1330 track_media_info_map.GetVideoTrack(video_receiver_info);
1331 if (video_track) {
1332 inbound_video->track_id =
1333 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1334 kReceiver,
1335 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1336 }
1337 inbound_video->transport_id = transport_id;
1338 report->AddStats(std::move(inbound_video));
1339 }
1340 // Outbound
1341 for (const cricket::VideoSenderInfo& video_sender_info :
1342 track_media_info_map.video_media_info()->senders) {
1343 if (!video_sender_info.connected())
1344 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001345 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001346 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1347 timestamp_us);
1348 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1349 outbound_video.get());
1350 rtc::scoped_refptr<VideoTrackInterface> video_track =
1351 track_media_info_map.GetVideoTrack(video_sender_info);
1352 if (video_track) {
1353 outbound_video->track_id =
1354 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1355 kSender,
1356 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1357 }
1358 outbound_video->transport_id = transport_id;
1359 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001360 }
1361}
1362
hbosdf6075a2016-12-19 04:58:02 -08001363void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001364 int64_t timestamp_us,
1365 const std::map<std::string, cricket::TransportStats>&
1366 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001367 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1368 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001369 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001370 for (const auto& entry : transport_stats_by_name) {
1371 const std::string& transport_name = entry.first;
1372 const cricket::TransportStats& transport_stats = entry.second;
1373
hbos2fa7c672016-10-24 04:00:05 -07001374 // Get reference to RTCP channel, if it exists.
1375 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001376 for (const cricket::TransportChannelStats& channel_stats :
1377 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001378 if (channel_stats.component ==
1379 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1380 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001381 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001382 break;
1383 }
1384 }
1385
1386 // Get reference to local and remote certificates of this transport, if they
1387 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001388 const auto& certificate_stats_it =
1389 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001390 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1391 std::string local_certificate_id;
1392 if (certificate_stats_it->second.local) {
1393 local_certificate_id = RTCCertificateIDFromFingerprint(
1394 certificate_stats_it->second.local->fingerprint);
1395 }
1396 std::string remote_certificate_id;
1397 if (certificate_stats_it->second.remote) {
1398 remote_certificate_id = RTCCertificateIDFromFingerprint(
1399 certificate_stats_it->second.remote->fingerprint);
1400 }
1401
1402 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001403 for (const cricket::TransportChannelStats& channel_stats :
1404 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001405 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001406 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1407 transport_name, channel_stats.component),
1408 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001409 transport_stats->bytes_sent = 0;
1410 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001411 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1412 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001413 for (const cricket::ConnectionInfo& info :
1414 channel_stats.connection_infos) {
1415 *transport_stats->bytes_sent += info.sent_total_bytes;
1416 *transport_stats->bytes_received += info.recv_total_bytes;
1417 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001418 transport_stats->selected_candidate_pair_id =
1419 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1420 }
1421 }
1422 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1423 !rtcp_transport_stats_id.empty()) {
1424 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1425 }
1426 if (!local_certificate_id.empty())
1427 transport_stats->local_certificate_id = local_certificate_id;
1428 if (!remote_certificate_id.empty())
1429 transport_stats->remote_certificate_id = remote_certificate_id;
1430 report->AddStats(std::move(transport_stats));
1431 }
1432 }
1433}
1434
1435std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001436RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001437 const std::map<std::string, cricket::TransportStats>&
1438 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001439 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001440 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001441 for (const auto& entry : transport_stats_by_name) {
1442 const std::string& transport_name = entry.first;
1443
hbos2fa7c672016-10-24 04:00:05 -07001444 CertificateStatsPair certificate_stats_pair;
1445 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001446 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001447 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001448 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001449 }
Steve Anton5dfde182018-02-06 10:34:40 -08001450
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001451 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1452 pc_->GetRemoteSSLCertChain(transport_name);
1453 if (remote_cert_chain) {
1454 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001455 }
Steve Anton5dfde182018-02-06 10:34:40 -08001456
hbos2fa7c672016-10-24 04:00:05 -07001457 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001458 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001459 }
1460 return transport_cert_stats;
1461}
1462
Steve Anton57858b32018-02-15 15:19:50 -08001463std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1464RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1465 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001466
Steve Anton57858b32018-02-15 15:19:50 -08001467 // These are used to invoke GetStats for all the media channels together in
1468 // one worker thread hop.
1469 std::map<cricket::VoiceMediaChannel*,
1470 std::unique_ptr<cricket::VoiceMediaInfo>>
1471 voice_stats;
1472 std::map<cricket::VideoMediaChannel*,
1473 std::unique_ptr<cricket::VideoMediaInfo>>
1474 video_stats;
1475
Mirko Bonadei739baf02019-01-27 17:29:42 +01001476 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001477 cricket::MediaType media_type = transceiver->media_type();
1478
1479 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1480 // stats have been fetched on the worker thread.
1481 transceiver_stats_infos.emplace_back();
1482 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1483 stats.transceiver = transceiver->internal();
1484 stats.media_type = media_type;
1485
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001486 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001487 if (!channel) {
1488 // The remaining fields require a BaseChannel.
1489 continue;
1490 }
1491
1492 stats.mid = channel->content_name();
1493 stats.transport_name = channel->transport_name();
1494
1495 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1496 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1497 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1498 voice_stats.end());
1499 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001500 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001501 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1502 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1503 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1504 video_stats.end());
1505 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001506 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001507 } else {
1508 RTC_NOTREACHED();
1509 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001510 }
Steve Anton57858b32018-02-15 15:19:50 -08001511
1512 // Call GetStats for all media channels together on the worker thread in one
1513 // hop.
1514 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1515 for (const auto& entry : voice_stats) {
1516 if (!entry.first->GetStats(entry.second.get())) {
1517 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1518 }
1519 }
1520 for (const auto& entry : video_stats) {
1521 if (!entry.first->GetStats(entry.second.get())) {
1522 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1523 }
1524 }
1525 });
1526
1527 // Create the TrackMediaInfoMap for each transceiver stats object.
1528 for (auto& stats : transceiver_stats_infos) {
1529 auto transceiver = stats.transceiver;
1530 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1531 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1532 if (transceiver->channel()) {
1533 cricket::MediaType media_type = transceiver->media_type();
1534 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1535 auto* voice_channel =
1536 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1537 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1538 voice_media_info =
1539 std::move(voice_stats[voice_channel->media_channel()]);
1540 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1541 auto* video_channel =
1542 static_cast<cricket::VideoChannel*>(transceiver->channel());
1543 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1544 video_media_info =
1545 std::move(video_stats[video_channel->media_channel()]);
1546 }
1547 }
1548 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001549 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001550 senders.push_back(sender->internal());
1551 }
1552 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001553 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001554 receivers.push_back(receiver->internal());
1555 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001556 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001557 std::move(voice_media_info), std::move(video_media_info), senders,
1558 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001559 }
Steve Anton57858b32018-02-15 15:19:50 -08001560
1561 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001562}
1563
Steve Anton7eca0932018-03-30 15:18:41 -07001564std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1565 std::set<std::string> transport_names;
1566 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1567 if (transceiver->internal()->channel()) {
1568 transport_names.insert(
1569 transceiver->internal()->channel()->transport_name());
1570 }
1571 }
1572 if (pc_->rtp_data_channel()) {
1573 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1574 }
1575 if (pc_->sctp_transport_name()) {
1576 transport_names.insert(*pc_->sctp_transport_name());
1577 }
1578 return transport_names;
1579}
1580
hbos82ebe022016-11-14 01:41:09 -08001581void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1582 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1583 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1584}
1585
1586void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1587 RTC_DCHECK(signaling_thread_->IsCurrent());
1588 bool result = internal_record_.opened_data_channels.insert(
1589 reinterpret_cast<uintptr_t>(channel)).second;
1590 ++internal_record_.data_channels_opened;
1591 RTC_DCHECK(result);
1592}
1593
1594void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1595 RTC_DCHECK(signaling_thread_->IsCurrent());
1596 // Only channels that have been fully opened (and have increased the
1597 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001598 if (internal_record_.opened_data_channels.erase(
1599 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001600 ++internal_record_.data_channels_closed;
1601 }
1602}
1603
hboscc555c52016-10-18 12:48:31 -07001604const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1605 const std::string& type) {
1606 return CandidateTypeToRTCIceCandidateType(type);
1607}
1608
1609const char* DataStateToRTCDataChannelStateForTesting(
1610 DataChannelInterface::DataState state) {
1611 return DataStateToRTCDataChannelState(state);
1612}
1613
hbosd565b732016-08-30 14:04:35 -07001614} // namespace webrtc