blob: a6046635063975b36089c264ba092983d12e032a [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;
hbos6ded1902016-11-01 01:50:46 -0700322}
323
hbos02ba2112016-10-28 05:14:53 -0700324void ProduceCertificateStatsFromSSLCertificateStats(
325 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
326 RTCStatsReport* report) {
327 RTCCertificateStats* prev_certificate_stats = nullptr;
328 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
329 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800330 std::string certificate_stats_id =
331 RTCCertificateIDFromFingerprint(s->fingerprint);
332 // It is possible for the same certificate to show up multiple times, e.g.
333 // if local and remote side use the same certificate in a loopback call.
334 // If the report already contains stats for this certificate, skip it.
335 if (report->Get(certificate_stats_id)) {
336 RTC_DCHECK_EQ(s, &certificate_stats);
337 break;
338 }
hbos02ba2112016-10-28 05:14:53 -0700339 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800340 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700341 certificate_stats->fingerprint = s->fingerprint;
342 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
343 certificate_stats->base64_certificate = s->base64_certificate;
344 if (prev_certificate_stats)
345 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
346 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
347 prev_certificate_stats = certificate_stats;
348 }
349}
350
351const std::string& ProduceIceCandidateStats(
352 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800353 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700354 const std::string& id = "RTCIceCandidate_" + candidate.id();
355 const RTCStats* stats = report->Get(id);
356 if (!stats) {
357 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
358 if (is_local)
359 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
360 else
361 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800362 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800363 if (is_local) {
364 candidate_stats->network_type =
365 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200366 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
367 std::string relay_protocol = candidate.relay_protocol();
368 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
369 relay_protocol.compare("tcp") == 0 ||
370 relay_protocol.compare("tls") == 0);
371 candidate_stats->relay_protocol = relay_protocol;
372 }
Gary Liu37e489c2017-11-21 10:49:36 -0800373 } else {
374 // We don't expect to know the adapter type of remote candidates.
375 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
376 }
hbos02ba2112016-10-28 05:14:53 -0700377 candidate_stats->ip = candidate.address().ipaddr().ToString();
378 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
379 candidate_stats->protocol = candidate.protocol();
380 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
381 candidate.type());
382 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
383
384 stats = candidate_stats.get();
385 report->AddStats(std::move(candidate_stats));
386 }
387 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
388 : RTCRemoteIceCandidateStats::kType);
389 return stats->id();
390}
391
hbos9e302742017-01-20 02:47:10 -0800392std::unique_ptr<RTCMediaStreamTrackStats>
393ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
394 int64_t timestamp_us,
395 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100396 const cricket::VoiceSenderInfo& voice_sender_info,
397 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800398 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
399 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100400 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
401 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100402 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800403 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
404 audio_track, audio_track_stats.get());
405 audio_track_stats->remote_source = false;
406 audio_track_stats->detached = false;
407 if (voice_sender_info.audio_level >= 0) {
408 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
409 voice_sender_info.audio_level);
410 }
zsteine76bd3a2017-07-14 12:17:49 -0700411 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
412 audio_track_stats->total_samples_duration =
413 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100414 if (voice_sender_info.apm_statistics.echo_return_loss) {
415 audio_track_stats->echo_return_loss =
416 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800417 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100418 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
419 audio_track_stats->echo_return_loss_enhancement =
420 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800421 }
422 return audio_track_stats;
423}
424
425std::unique_ptr<RTCMediaStreamTrackStats>
426ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
427 int64_t timestamp_us,
428 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100429 const cricket::VoiceReceiverInfo& voice_receiver_info,
430 int attachment_id) {
431 // Since receiver tracks can't be reattached, we use the SSRC as
432 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800433 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
434 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100435 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
436 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100437 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800438 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
439 audio_track, audio_track_stats.get());
440 audio_track_stats->remote_source = true;
441 audio_track_stats->detached = false;
442 if (voice_receiver_info.audio_level >= 0) {
443 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
444 voice_receiver_info.audio_level);
445 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200446 audio_track_stats->jitter_buffer_delay =
447 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100448 audio_track_stats->jitter_buffer_emitted_count =
449 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 12:17:49 -0700450 audio_track_stats->total_audio_energy =
451 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700452 audio_track_stats->total_samples_received =
453 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700454 audio_track_stats->total_samples_duration =
455 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700456 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200457 audio_track_stats->concealment_events =
458 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100459 audio_track_stats->jitter_buffer_flushes =
460 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100461 audio_track_stats->delayed_packet_outage_samples =
462 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100463 audio_track_stats->relative_packet_arrival_delay =
464 voice_receiver_info.relative_packet_arrival_delay_seconds;
hbos9e302742017-01-20 02:47:10 -0800465 return audio_track_stats;
466}
467
468std::unique_ptr<RTCMediaStreamTrackStats>
469ProduceMediaStreamTrackStatsFromVideoSenderInfo(
470 int64_t timestamp_us,
471 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100472 const cricket::VideoSenderInfo& video_sender_info,
473 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800474 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
475 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100476 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
477
478 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100479 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800480 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
481 video_track, video_track_stats.get());
482 video_track_stats->remote_source = false;
483 video_track_stats->detached = false;
484 video_track_stats->frame_width = static_cast<uint32_t>(
485 video_sender_info.send_frame_width);
486 video_track_stats->frame_height = static_cast<uint32_t>(
487 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800488 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100489 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800490 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100491 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800492 return video_track_stats;
493}
494
495std::unique_ptr<RTCMediaStreamTrackStats>
496ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
497 int64_t timestamp_us,
498 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100499 const cricket::VideoReceiverInfo& video_receiver_info,
500 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800501 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
502 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100503 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
504
505 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100506 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800507 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
508 video_track, video_track_stats.get());
509 video_track_stats->remote_source = true;
510 video_track_stats->detached = false;
511 if (video_receiver_info.frame_width > 0 &&
512 video_receiver_info.frame_height > 0) {
513 video_track_stats->frame_width = static_cast<uint32_t>(
514 video_receiver_info.frame_width);
515 video_track_stats->frame_height = static_cast<uint32_t>(
516 video_receiver_info.frame_height);
517 }
hbos42f6d2f2017-01-20 03:56:50 -0800518 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800519 // TODO(hbos): When we support receiving simulcast, this should be the total
520 // number of frames correctly decoded, independent of which SSRC it was
521 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100522 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800523 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800524 RTC_DCHECK_GE(video_receiver_info.frames_received,
525 video_receiver_info.frames_rendered);
526 video_track_stats->frames_dropped = video_receiver_info.frames_received -
527 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100528 video_track_stats->freeze_count = video_receiver_info.freeze_count;
529 video_track_stats->pause_count = video_receiver_info.pause_count;
530 video_track_stats->total_freezes_duration =
531 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
532 rtc::kNumMillisecsPerSec;
533 video_track_stats->total_pauses_duration =
534 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
535 rtc::kNumMillisecsPerSec;
536 video_track_stats->total_frames_duration =
537 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
538 rtc::kNumMillisecsPerSec;
539 video_track_stats->sum_squared_frame_durations =
540 video_receiver_info.sum_squared_frame_durations;
541
hbos9e302742017-01-20 02:47:10 -0800542 return video_track_stats;
543}
544
Harald Alvestrand89061872018-01-02 14:08:34 +0100545void ProduceSenderMediaTrackStats(
546 int64_t timestamp_us,
547 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800548 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100549 RTCStatsReport* report) {
550 // This function iterates over the senders to generate outgoing track stats.
551
552 // TODO(hbos): Return stats of detached tracks. We have to perform stats
553 // gathering at the time of detachment to get accurate stats and timestamps.
554 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100555 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100556 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
557 AudioTrackInterface* track =
558 static_cast<AudioTrackInterface*>(sender->track().get());
559 if (!track)
560 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100561 cricket::VoiceSenderInfo null_sender_info;
562 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
563 // TODO(hta): Checking on ssrc is not proper. There should be a way
564 // to see from a sender whether it's connected or not.
565 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800566 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100567 // When pc.close is called, sender info is discarded, so
568 // we generate zeroes instead. Bug: It should be retained.
569 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800570 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100571 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100572 if (sender_info) {
573 voice_sender_info = sender_info;
574 } else {
575 RTC_LOG(LS_INFO)
576 << "RTCStatsCollector: No voice sender info for sender with ssrc "
577 << sender->ssrc();
578 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100579 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100580 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100581 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
582 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100583 report->AddStats(std::move(audio_track_stats));
584 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
585 VideoTrackInterface* track =
586 static_cast<VideoTrackInterface*>(sender->track().get());
587 if (!track)
588 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100589 cricket::VideoSenderInfo null_sender_info;
590 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
591 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100592 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
593 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800594 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100595 // When pc.close is called, sender info is discarded, so
596 // we generate zeroes instead. Bug: It should be retained.
597 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800598 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100599 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100600 if (sender_info) {
601 video_sender_info = sender_info;
602 } else {
603 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
604 << sender->ssrc();
605 }
606 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100607 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100608 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
609 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100610 report->AddStats(std::move(video_track_stats));
611 } else {
612 RTC_NOTREACHED();
613 }
614 }
615}
616
617void ProduceReceiverMediaTrackStats(
618 int64_t timestamp_us,
619 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800620 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100621 RTCStatsReport* report) {
622 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100623 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100624 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
625 AudioTrackInterface* track =
626 static_cast<AudioTrackInterface*>(receiver->track().get());
627 const cricket::VoiceReceiverInfo* voice_receiver_info =
628 track_media_info_map.GetVoiceReceiverInfo(*track);
629 if (!voice_receiver_info) {
630 continue;
631 }
632 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
633 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100634 timestamp_us, *track, *voice_receiver_info,
635 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100636 report->AddStats(std::move(audio_track_stats));
637 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
638 VideoTrackInterface* track =
639 static_cast<VideoTrackInterface*>(receiver->track().get());
640 const cricket::VideoReceiverInfo* video_receiver_info =
641 track_media_info_map.GetVideoReceiverInfo(*track);
642 if (!video_receiver_info) {
643 continue;
644 }
645 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
646 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100647 timestamp_us, *track, *video_receiver_info,
648 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100649 report->AddStats(std::move(video_track_stats));
650 } else {
651 RTC_NOTREACHED();
652 }
653 }
654}
655
Henrik Boström5b3541f2018-03-19 13:52:56 +0100656rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
657 bool filter_by_sender_selector,
658 rtc::scoped_refptr<const RTCStatsReport> report,
659 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
660 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
661 std::vector<std::string> rtpstream_ids;
662 if (filter_by_sender_selector) {
663 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
664 if (sender_selector) {
665 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
666 // reference the sender stats.
667 // Because we do not implement sender stats, we look at outbound-rtp(s)
668 // that reference the track attachment stats for the sender instead.
669 std::string track_id =
670 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
671 kSender, sender_selector->AttachmentId());
672 for (const auto& stats : *report) {
673 if (stats.type() != RTCOutboundRTPStreamStats::kType)
674 continue;
675 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
676 if (outbound_rtp.track_id.is_defined() &&
677 *outbound_rtp.track_id == track_id) {
678 rtpstream_ids.push_back(outbound_rtp.id());
679 }
680 }
681 }
682 } else {
683 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
684 if (receiver_selector) {
685 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
686 // reference the receiver stats.
687 // Because we do not implement receiver stats, we look at inbound-rtp(s)
688 // that reference the track attachment stats for the receiver instead.
689 std::string track_id =
690 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
691 kReceiver, receiver_selector->AttachmentId());
692 for (const auto& stats : *report) {
693 if (stats.type() != RTCInboundRTPStreamStats::kType)
694 continue;
695 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
696 if (inbound_rtp.track_id.is_defined() &&
697 *inbound_rtp.track_id == track_id) {
698 rtpstream_ids.push_back(inbound_rtp.id());
699 }
700 }
701 }
702 }
703 if (rtpstream_ids.empty())
704 return RTCStatsReport::Create(report->timestamp_us());
705 return TakeReferencedStats(report->Copy(), rtpstream_ids);
706}
707
hboscc555c52016-10-18 12:48:31 -0700708} // namespace
709
Henrik Boström5b3541f2018-03-19 13:52:56 +0100710RTCStatsCollector::RequestInfo::RequestInfo(
711 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
712 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
713
714RTCStatsCollector::RequestInfo::RequestInfo(
715 rtc::scoped_refptr<RtpSenderInternal> selector,
716 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
717 : RequestInfo(FilterMode::kSenderSelector,
718 std::move(callback),
719 std::move(selector),
720 nullptr) {}
721
722RTCStatsCollector::RequestInfo::RequestInfo(
723 rtc::scoped_refptr<RtpReceiverInternal> selector,
724 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
725 : RequestInfo(FilterMode::kReceiverSelector,
726 std::move(callback),
727 nullptr,
728 std::move(selector)) {}
729
730RTCStatsCollector::RequestInfo::RequestInfo(
731 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
732 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
733 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
734 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
735 : filter_mode_(filter_mode),
736 callback_(std::move(callback)),
737 sender_selector_(std::move(sender_selector)),
738 receiver_selector_(std::move(receiver_selector)) {
739 RTC_DCHECK(callback_);
740 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
741}
742
hbosc82f2e12016-09-05 01:36:50 -0700743rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800744 PeerConnectionInternal* pc,
745 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700746 return rtc::scoped_refptr<RTCStatsCollector>(
747 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
748}
749
Steve Anton2d8609c2018-01-23 16:38:46 -0800750RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700751 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700752 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700753 signaling_thread_(pc->signaling_thread()),
754 worker_thread_(pc->worker_thread()),
755 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700756 num_pending_partial_reports_(0),
757 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100758 network_report_event_(true /* manual_reset */,
759 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700760 cache_timestamp_us_(0),
761 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700762 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700763 RTC_DCHECK(signaling_thread_);
764 RTC_DCHECK(worker_thread_);
765 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700766 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800767 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800768 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700769}
770
hbosb78306a2016-12-19 05:06:57 -0800771RTCStatsCollector::~RTCStatsCollector() {
772 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
773}
774
hbosc82f2e12016-09-05 01:36:50 -0700775void RTCStatsCollector::GetStatsReport(
776 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100777 GetStatsReportInternal(RequestInfo(std::move(callback)));
778}
779
780void RTCStatsCollector::GetStatsReport(
781 rtc::scoped_refptr<RtpSenderInternal> selector,
782 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
783 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
784}
785
786void RTCStatsCollector::GetStatsReport(
787 rtc::scoped_refptr<RtpReceiverInternal> selector,
788 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
789 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
790}
791
792void RTCStatsCollector::GetStatsReportInternal(
793 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700794 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100795 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700796
hbos0e6758d2016-08-31 07:57:36 -0700797 // "Now" using a monotonically increasing timer.
798 int64_t cache_now_us = rtc::TimeMicros();
799 if (cached_report_ &&
800 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800801 // We have a fresh cached report to deliver. Deliver asynchronously, since
802 // the caller may not be expecting a synchronous callback, and it avoids
803 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100804 std::vector<RequestInfo> requests;
805 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100806 signaling_thread_->PostTask(
807 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
808 cached_report_, 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öm40b030e2019-02-28 09:49:31 +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 network_thread_->PostTask(
841 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700842 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +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öm40b030e2019-02-28 09:49:31 +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öm40b030e2019-02-28 09:49:31 +0100863 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700864
Henrik Boström40b030e2019-02-28 09:49:31 +0100865 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
866 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700867
Henrik Boström40b030e2019-02-28 09:49:31 +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öm40b030e2019-02-28 09:49:31 +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öm40b030e2019-02-28 09:49:31 +0100898 ProducePartialResultsOnNetworkThreadImpl(
899 timestamp_us, transport_stats_by_name, transport_cert_stats,
900 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000901
Henrik Boström40b030e2019-02-28 09:49:31 +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 signaling_thread_->PostTask(
906 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100907}
908
Henrik Boström40b030e2019-02-28 09:49:31 +0100909void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
910 int64_t timestamp_us,
911 const std::map<std::string, cricket::TransportStats>&
912 transport_stats_by_name,
913 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
914 RTCStatsReport* partial_report) {
915 RTC_DCHECK(network_thread_->IsCurrent());
916 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
917 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
918 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
919 call_stats_, partial_report);
920 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
921 partial_report);
922 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
923 transport_cert_stats, partial_report);
924}
925
926void RTCStatsCollector::MergeNetworkReport_s() {
927 RTC_DCHECK(signaling_thread_->IsCurrent());
928 // The |network_report_event_| must be signaled for it to be safe to touch
929 // |network_report_|. This is normally not blocking, but if
930 // WaitForPendingRequest() is called while a request is pending, we might have
931 // to wait until the network thread is done touching |network_report_|.
932 network_report_event_.Wait(rtc::Event::kForever);
933 if (!network_report_) {
934 // Normally, MergeNetworkReport_s() is executed because it is posted from
935 // the network thread. But if WaitForPendingRequest() is called while a
936 // request is pending, an early call to MergeNetworkReport_s() is made,
937 // merging the report and setting |network_report_| to null. If so, when the
938 // previously posted MergeNetworkReport_s() is later executed, the report is
939 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -0700940 return;
941 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000942 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +0100943 RTC_DCHECK(partial_report_);
944 partial_report_->TakeMembersFrom(network_report_);
945 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000946 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +0100947 // |network_report_| is currently the only partial report collected
948 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
949 // ready to deliver the result.
950 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
951 cache_timestamp_us_ = partial_report_timestamp_us_;
952 cached_report_ = partial_report_;
953 partial_report_ = nullptr;
954 transceiver_stats_infos_.clear();
955 // Trace WebRTC Stats when getStats is called on Javascript.
956 // This allows access to WebRTC stats from trace logs. To enable them,
957 // select the "webrtc_stats" category when recording traces.
958 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
959 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000960
Henrik Boström40b030e2019-02-28 09:49:31 +0100961 // Deliver report and clear |requests_|.
962 std::vector<RequestInfo> requests;
963 requests.swap(requests_);
964 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700965}
966
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800967void RTCStatsCollector::DeliverCachedReport(
968 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100969 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700970 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100971 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800972 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800973
Henrik Boström5b3541f2018-03-19 13:52:56 +0100974 for (const RequestInfo& request : requests) {
975 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
976 request.callback()->OnStatsDelivered(cached_report);
977 } else {
978 bool filter_by_sender_selector;
979 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
980 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
981 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
982 filter_by_sender_selector = true;
983 sender_selector = request.sender_selector();
984 } else {
985 RTC_DCHECK(request.filter_mode() ==
986 RequestInfo::FilterMode::kReceiverSelector);
987 filter_by_sender_selector = false;
988 receiver_selector = request.receiver_selector();
989 }
990 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
991 filter_by_sender_selector, cached_report, sender_selector,
992 receiver_selector));
993 }
hbosc82f2e12016-09-05 01:36:50 -0700994 }
hbosd565b732016-08-30 14:04:35 -0700995}
996
hbosdf6075a2016-12-19 04:58:02 -0800997void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700998 int64_t timestamp_us,
999 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001000 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001001 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001002 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1003 if (transport_cert_stats_pair.second.local) {
1004 ProduceCertificateStatsFromSSLCertificateStats(
1005 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001006 }
hbos02ba2112016-10-28 05:14:53 -07001007 if (transport_cert_stats_pair.second.remote) {
1008 ProduceCertificateStatsFromSSLCertificateStats(
1009 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001010 }
1011 }
1012}
1013
hbosdf6075a2016-12-19 04:58:02 -08001014void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001015 int64_t timestamp_us,
1016 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001017 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001018 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001019 for (const auto& stats : transceiver_stats_infos) {
1020 if (!stats.mid) {
1021 continue;
hbos0adb8282016-11-23 02:32:06 -08001022 }
Steve Anton57858b32018-02-15 15:19:50 -08001023 const cricket::VoiceMediaInfo* voice_media_info =
1024 stats.track_media_info_map->voice_media_info();
1025 const cricket::VideoMediaInfo* video_media_info =
1026 stats.track_media_info_map->video_media_info();
1027 // Audio
1028 if (voice_media_info) {
1029 // Inbound
1030 for (const auto& pair : voice_media_info->receive_codecs) {
1031 report->AddStats(CodecStatsFromRtpCodecParameters(
1032 timestamp_us, *stats.mid, true, pair.second));
1033 }
1034 // Outbound
1035 for (const auto& pair : voice_media_info->send_codecs) {
1036 report->AddStats(CodecStatsFromRtpCodecParameters(
1037 timestamp_us, *stats.mid, false, pair.second));
1038 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001039 }
Steve Anton57858b32018-02-15 15:19:50 -08001040 // Video
1041 if (video_media_info) {
1042 // Inbound
1043 for (const auto& pair : video_media_info->receive_codecs) {
1044 report->AddStats(CodecStatsFromRtpCodecParameters(
1045 timestamp_us, *stats.mid, true, pair.second));
1046 }
1047 // Outbound
1048 for (const auto& pair : video_media_info->send_codecs) {
1049 report->AddStats(CodecStatsFromRtpCodecParameters(
1050 timestamp_us, *stats.mid, false, pair.second));
1051 }
hbos0adb8282016-11-23 02:32:06 -08001052 }
1053 }
1054}
1055
hboscc555c52016-10-18 12:48:31 -07001056void RTCStatsCollector::ProduceDataChannelStats_s(
1057 int64_t timestamp_us, RTCStatsReport* report) const {
1058 RTC_DCHECK(signaling_thread_->IsCurrent());
1059 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1060 pc_->sctp_data_channels()) {
1061 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1062 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001063 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001064 timestamp_us));
1065 data_channel_stats->label = data_channel->label();
1066 data_channel_stats->protocol = data_channel->protocol();
1067 data_channel_stats->datachannelid = data_channel->id();
1068 data_channel_stats->state =
1069 DataStateToRTCDataChannelState(data_channel->state());
1070 data_channel_stats->messages_sent = data_channel->messages_sent();
1071 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1072 data_channel_stats->messages_received = data_channel->messages_received();
1073 data_channel_stats->bytes_received = data_channel->bytes_received();
1074 report->AddStats(std::move(data_channel_stats));
1075 }
1076}
1077
hbosdf6075a2016-12-19 04:58:02 -08001078void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001079 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001080 const std::map<std::string, cricket::TransportStats>&
1081 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001082 const Call::Stats& call_stats,
1083 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001084 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001085 for (const auto& entry : transport_stats_by_name) {
1086 const std::string& transport_name = entry.first;
1087 const cricket::TransportStats& transport_stats = entry.second;
1088 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001089 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001090 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001091 for (const cricket::ConnectionInfo& info :
1092 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001093 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001094 new RTCIceCandidatePairStats(
1095 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1096 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001097
hbos0583b282016-11-30 01:50:14 -08001098 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001099 // TODO(hbos): There could be other candidates that are not paired with
1100 // anything. We don't have a complete list. Local candidates come from
1101 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001102 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001103 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001104 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001105 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001106 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001107 candidate_pair_stats->state =
1108 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1109 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001110 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001111 // TODO(hbos): This writable is different than the spec. It goes to
1112 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001113 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001114 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001115 candidate_pair_stats->bytes_sent =
1116 static_cast<uint64_t>(info.sent_total_bytes);
1117 candidate_pair_stats->bytes_received =
1118 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001119 candidate_pair_stats->total_round_trip_time =
1120 static_cast<double>(info.total_round_trip_time_ms) /
1121 rtc::kNumMillisecsPerSec;
1122 if (info.current_round_trip_time_ms) {
1123 candidate_pair_stats->current_round_trip_time =
1124 static_cast<double>(*info.current_round_trip_time_ms) /
1125 rtc::kNumMillisecsPerSec;
1126 }
stefanf79ade12017-06-02 06:44:03 -07001127 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001128 // The bandwidth estimations we have are for the selected candidate
1129 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001130 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1131 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1132 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001133 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001134 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001135 }
stefanf79ade12017-06-02 06:44:03 -07001136 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001137 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001138 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001139 }
1140 }
hbosd82f5122016-12-09 04:12:39 -08001141 candidate_pair_stats->requests_received =
1142 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001143 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1144 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001145 candidate_pair_stats->responses_received =
1146 static_cast<uint64_t>(info.recv_ping_responses);
1147 candidate_pair_stats->responses_sent =
1148 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001149 RTC_DCHECK_GE(info.sent_ping_requests_total,
1150 info.sent_ping_requests_before_first_response);
1151 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1152 info.sent_ping_requests_total -
1153 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001154
1155 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001156 }
1157 }
1158 }
1159}
1160
Steve Anton57858b32018-02-15 15:19:50 -08001161void RTCStatsCollector::ProduceMediaStreamStats_s(
1162 int64_t timestamp_us,
1163 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001164 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001165
1166 std::map<std::string, std::vector<std::string>> track_ids;
1167
1168 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001169 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001170 std::string track_id =
1171 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1172 kSender, sender->internal()->AttachmentId());
1173 for (auto& stream_id : sender->stream_ids()) {
1174 track_ids[stream_id].push_back(track_id);
1175 }
1176 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001177 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001178 std::string track_id =
1179 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1180 kReceiver, receiver->internal()->AttachmentId());
1181 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001182 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001183 }
1184 }
1185 }
1186
1187 // Build stats for each stream ID known.
1188 for (auto& it : track_ids) {
1189 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1190 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1191 stream_stats->stream_identifier = it.first;
1192 stream_stats->track_ids = it.second;
1193 report->AddStats(std::move(stream_stats));
1194 }
1195}
1196
1197void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1198 int64_t timestamp_us,
1199 RTCStatsReport* report) const {
1200 RTC_DCHECK(signaling_thread_->IsCurrent());
1201 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1202 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001203 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001204 senders.push_back(sender->internal());
1205 }
1206 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1207 senders, report);
1208
1209 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001210 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001211 receivers.push_back(receiver->internal());
1212 }
1213 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1214 receivers, report);
1215 }
hbos09bc1282016-11-08 06:29:22 -08001216}
1217
hbos6ab97ce2016-10-03 14:16:56 -07001218void RTCStatsCollector::ProducePeerConnectionStats_s(
1219 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001220 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001221 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001222 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001223 stats->data_channels_opened = internal_record_.data_channels_opened;
1224 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001225 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001226}
1227
hbosdf6075a2016-12-19 04:58:02 -08001228void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001229 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001230 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001231 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001232 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001233
Steve Anton57858b32018-02-15 15:19:50 -08001234 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1235 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1236 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1237 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1238 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1239 } else {
1240 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001241 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001242 }
Steve Anton57858b32018-02-15 15:19:50 -08001243}
1244
1245void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1246 int64_t timestamp_us,
1247 const RtpTransceiverStatsInfo& stats,
1248 RTCStatsReport* report) const {
1249 if (!stats.mid || !stats.transport_name) {
1250 return;
1251 }
1252 RTC_DCHECK(stats.track_media_info_map);
1253 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1254 RTC_DCHECK(track_media_info_map.voice_media_info());
1255 std::string mid = *stats.mid;
1256 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1257 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1258 // Inbound
1259 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1260 track_media_info_map.voice_media_info()->receivers) {
1261 if (!voice_receiver_info.connected())
1262 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001263 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001264 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1265 timestamp_us);
1266 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1267 inbound_audio.get());
1268 // TODO(hta): This lookup should look for the sender, not the track.
1269 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1270 track_media_info_map.GetAudioTrack(voice_receiver_info);
1271 if (audio_track) {
1272 inbound_audio->track_id =
1273 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1274 kReceiver,
1275 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001276 }
Steve Anton57858b32018-02-15 15:19:50 -08001277 inbound_audio->transport_id = transport_id;
1278 report->AddStats(std::move(inbound_audio));
1279 }
1280 // Outbound
1281 for (const cricket::VoiceSenderInfo& voice_sender_info :
1282 track_media_info_map.voice_media_info()->senders) {
1283 if (!voice_sender_info.connected())
1284 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001285 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001286 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1287 timestamp_us);
1288 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1289 outbound_audio.get());
1290 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1291 track_media_info_map.GetAudioTrack(voice_sender_info);
1292 if (audio_track) {
1293 outbound_audio->track_id =
1294 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1295 kSender,
1296 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001297 }
Steve Anton57858b32018-02-15 15:19:50 -08001298 outbound_audio->transport_id = transport_id;
1299 report->AddStats(std::move(outbound_audio));
1300 }
1301}
1302
1303void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1304 int64_t timestamp_us,
1305 const RtpTransceiverStatsInfo& stats,
1306 RTCStatsReport* report) const {
1307 if (!stats.mid || !stats.transport_name) {
1308 return;
1309 }
1310 RTC_DCHECK(stats.track_media_info_map);
1311 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1312 RTC_DCHECK(track_media_info_map.video_media_info());
1313 std::string mid = *stats.mid;
1314 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1315 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1316 // Inbound
1317 for (const cricket::VideoReceiverInfo& video_receiver_info :
1318 track_media_info_map.video_media_info()->receivers) {
1319 if (!video_receiver_info.connected())
1320 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001321 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001322 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1323 timestamp_us);
1324 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1325 inbound_video.get());
1326 rtc::scoped_refptr<VideoTrackInterface> video_track =
1327 track_media_info_map.GetVideoTrack(video_receiver_info);
1328 if (video_track) {
1329 inbound_video->track_id =
1330 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1331 kReceiver,
1332 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1333 }
1334 inbound_video->transport_id = transport_id;
1335 report->AddStats(std::move(inbound_video));
1336 }
1337 // Outbound
1338 for (const cricket::VideoSenderInfo& video_sender_info :
1339 track_media_info_map.video_media_info()->senders) {
1340 if (!video_sender_info.connected())
1341 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001342 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001343 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1344 timestamp_us);
1345 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1346 outbound_video.get());
1347 rtc::scoped_refptr<VideoTrackInterface> video_track =
1348 track_media_info_map.GetVideoTrack(video_sender_info);
1349 if (video_track) {
1350 outbound_video->track_id =
1351 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1352 kSender,
1353 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1354 }
1355 outbound_video->transport_id = transport_id;
1356 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001357 }
1358}
1359
hbosdf6075a2016-12-19 04:58:02 -08001360void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001361 int64_t timestamp_us,
1362 const std::map<std::string, cricket::TransportStats>&
1363 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001364 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1365 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001366 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001367 for (const auto& entry : transport_stats_by_name) {
1368 const std::string& transport_name = entry.first;
1369 const cricket::TransportStats& transport_stats = entry.second;
1370
hbos2fa7c672016-10-24 04:00:05 -07001371 // Get reference to RTCP channel, if it exists.
1372 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001373 for (const cricket::TransportChannelStats& channel_stats :
1374 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001375 if (channel_stats.component ==
1376 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1377 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001378 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001379 break;
1380 }
1381 }
1382
1383 // Get reference to local and remote certificates of this transport, if they
1384 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001385 const auto& certificate_stats_it =
1386 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001387 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1388 std::string local_certificate_id;
1389 if (certificate_stats_it->second.local) {
1390 local_certificate_id = RTCCertificateIDFromFingerprint(
1391 certificate_stats_it->second.local->fingerprint);
1392 }
1393 std::string remote_certificate_id;
1394 if (certificate_stats_it->second.remote) {
1395 remote_certificate_id = RTCCertificateIDFromFingerprint(
1396 certificate_stats_it->second.remote->fingerprint);
1397 }
1398
1399 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001400 for (const cricket::TransportChannelStats& channel_stats :
1401 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001402 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001403 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1404 transport_name, channel_stats.component),
1405 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001406 transport_stats->bytes_sent = 0;
1407 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001408 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1409 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001410 for (const cricket::ConnectionInfo& info :
1411 channel_stats.connection_infos) {
1412 *transport_stats->bytes_sent += info.sent_total_bytes;
1413 *transport_stats->bytes_received += info.recv_total_bytes;
1414 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001415 transport_stats->selected_candidate_pair_id =
1416 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1417 }
1418 }
1419 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1420 !rtcp_transport_stats_id.empty()) {
1421 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1422 }
1423 if (!local_certificate_id.empty())
1424 transport_stats->local_certificate_id = local_certificate_id;
1425 if (!remote_certificate_id.empty())
1426 transport_stats->remote_certificate_id = remote_certificate_id;
1427 report->AddStats(std::move(transport_stats));
1428 }
1429 }
1430}
1431
1432std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001433RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001434 const std::map<std::string, cricket::TransportStats>&
1435 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001436 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001437 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001438 for (const auto& entry : transport_stats_by_name) {
1439 const std::string& transport_name = entry.first;
1440
hbos2fa7c672016-10-24 04:00:05 -07001441 CertificateStatsPair certificate_stats_pair;
1442 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001443 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001444 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001445 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001446 }
Steve Anton5dfde182018-02-06 10:34:40 -08001447
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001448 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1449 pc_->GetRemoteSSLCertChain(transport_name);
1450 if (remote_cert_chain) {
1451 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001452 }
Steve Anton5dfde182018-02-06 10:34:40 -08001453
hbos2fa7c672016-10-24 04:00:05 -07001454 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001455 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001456 }
1457 return transport_cert_stats;
1458}
1459
Steve Anton57858b32018-02-15 15:19:50 -08001460std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1461RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1462 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001463
Steve Anton57858b32018-02-15 15:19:50 -08001464 // These are used to invoke GetStats for all the media channels together in
1465 // one worker thread hop.
1466 std::map<cricket::VoiceMediaChannel*,
1467 std::unique_ptr<cricket::VoiceMediaInfo>>
1468 voice_stats;
1469 std::map<cricket::VideoMediaChannel*,
1470 std::unique_ptr<cricket::VideoMediaInfo>>
1471 video_stats;
1472
Mirko Bonadei739baf02019-01-27 17:29:42 +01001473 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001474 cricket::MediaType media_type = transceiver->media_type();
1475
1476 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1477 // stats have been fetched on the worker thread.
1478 transceiver_stats_infos.emplace_back();
1479 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1480 stats.transceiver = transceiver->internal();
1481 stats.media_type = media_type;
1482
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001483 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001484 if (!channel) {
1485 // The remaining fields require a BaseChannel.
1486 continue;
1487 }
1488
1489 stats.mid = channel->content_name();
1490 stats.transport_name = channel->transport_name();
1491
1492 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1493 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1494 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1495 voice_stats.end());
1496 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001497 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001498 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1499 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1500 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1501 video_stats.end());
1502 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001503 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001504 } else {
1505 RTC_NOTREACHED();
1506 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001507 }
Steve Anton57858b32018-02-15 15:19:50 -08001508
1509 // Call GetStats for all media channels together on the worker thread in one
1510 // hop.
1511 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1512 for (const auto& entry : voice_stats) {
1513 if (!entry.first->GetStats(entry.second.get())) {
1514 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1515 }
1516 }
1517 for (const auto& entry : video_stats) {
1518 if (!entry.first->GetStats(entry.second.get())) {
1519 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1520 }
1521 }
1522 });
1523
1524 // Create the TrackMediaInfoMap for each transceiver stats object.
1525 for (auto& stats : transceiver_stats_infos) {
1526 auto transceiver = stats.transceiver;
1527 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1528 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1529 if (transceiver->channel()) {
1530 cricket::MediaType media_type = transceiver->media_type();
1531 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1532 auto* voice_channel =
1533 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1534 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1535 voice_media_info =
1536 std::move(voice_stats[voice_channel->media_channel()]);
1537 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1538 auto* video_channel =
1539 static_cast<cricket::VideoChannel*>(transceiver->channel());
1540 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1541 video_media_info =
1542 std::move(video_stats[video_channel->media_channel()]);
1543 }
1544 }
1545 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001546 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001547 senders.push_back(sender->internal());
1548 }
1549 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001550 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001551 receivers.push_back(receiver->internal());
1552 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001553 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001554 std::move(voice_media_info), std::move(video_media_info), senders,
1555 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001556 }
Steve Anton57858b32018-02-15 15:19:50 -08001557
1558 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001559}
1560
Steve Anton7eca0932018-03-30 15:18:41 -07001561std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1562 std::set<std::string> transport_names;
1563 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1564 if (transceiver->internal()->channel()) {
1565 transport_names.insert(
1566 transceiver->internal()->channel()->transport_name());
1567 }
1568 }
1569 if (pc_->rtp_data_channel()) {
1570 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1571 }
1572 if (pc_->sctp_transport_name()) {
1573 transport_names.insert(*pc_->sctp_transport_name());
1574 }
1575 return transport_names;
1576}
1577
hbos82ebe022016-11-14 01:41:09 -08001578void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1579 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1580 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1581}
1582
1583void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1584 RTC_DCHECK(signaling_thread_->IsCurrent());
1585 bool result = internal_record_.opened_data_channels.insert(
1586 reinterpret_cast<uintptr_t>(channel)).second;
1587 ++internal_record_.data_channels_opened;
1588 RTC_DCHECK(result);
1589}
1590
1591void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1592 RTC_DCHECK(signaling_thread_->IsCurrent());
1593 // Only channels that have been fully opened (and have increased the
1594 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001595 if (internal_record_.opened_data_channels.erase(
1596 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001597 ++internal_record_.data_channels_closed;
1598 }
1599}
1600
hboscc555c52016-10-18 12:48:31 -07001601const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1602 const std::string& type) {
1603 return CandidateTypeToRTCIceCandidateType(type);
1604}
1605
1606const char* DataStateToRTCDataChannelStateForTesting(
1607 DataChannelInterface::DataState state) {
1608 return DataStateToRTCDataChannelState(state);
1609}
1610
hbosd565b732016-08-30 14:04:35 -07001611} // namespace webrtc