blob: 245293aa336ad881deabc25184ffb87300b00f36 [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;
hbos9e302742017-01-20 02:47:10 -0800463 return audio_track_stats;
464}
465
466std::unique_ptr<RTCMediaStreamTrackStats>
467ProduceMediaStreamTrackStatsFromVideoSenderInfo(
468 int64_t timestamp_us,
469 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100470 const cricket::VideoSenderInfo& video_sender_info,
471 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800472 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
473 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100474 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
475
476 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100477 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800478 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
479 video_track, video_track_stats.get());
480 video_track_stats->remote_source = false;
481 video_track_stats->detached = false;
482 video_track_stats->frame_width = static_cast<uint32_t>(
483 video_sender_info.send_frame_width);
484 video_track_stats->frame_height = static_cast<uint32_t>(
485 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800486 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100487 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800488 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100489 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800490 return video_track_stats;
491}
492
493std::unique_ptr<RTCMediaStreamTrackStats>
494ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
495 int64_t timestamp_us,
496 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100497 const cricket::VideoReceiverInfo& video_receiver_info,
498 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800499 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
500 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100501 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
502
503 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100504 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800505 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
506 video_track, video_track_stats.get());
507 video_track_stats->remote_source = true;
508 video_track_stats->detached = false;
509 if (video_receiver_info.frame_width > 0 &&
510 video_receiver_info.frame_height > 0) {
511 video_track_stats->frame_width = static_cast<uint32_t>(
512 video_receiver_info.frame_width);
513 video_track_stats->frame_height = static_cast<uint32_t>(
514 video_receiver_info.frame_height);
515 }
hbos42f6d2f2017-01-20 03:56:50 -0800516 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800517 // TODO(hbos): When we support receiving simulcast, this should be the total
518 // number of frames correctly decoded, independent of which SSRC it was
519 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100520 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800521 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800522 RTC_DCHECK_GE(video_receiver_info.frames_received,
523 video_receiver_info.frames_rendered);
524 video_track_stats->frames_dropped = video_receiver_info.frames_received -
525 video_receiver_info.frames_rendered;
hbos9e302742017-01-20 02:47:10 -0800526 return video_track_stats;
527}
528
Harald Alvestrand89061872018-01-02 14:08:34 +0100529void ProduceSenderMediaTrackStats(
530 int64_t timestamp_us,
531 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800532 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100533 RTCStatsReport* report) {
534 // This function iterates over the senders to generate outgoing track stats.
535
536 // TODO(hbos): Return stats of detached tracks. We have to perform stats
537 // gathering at the time of detachment to get accurate stats and timestamps.
538 // https://crbug.com/659137
Steve Anton57858b32018-02-15 15:19:50 -0800539 for (auto sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100540 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
541 AudioTrackInterface* track =
542 static_cast<AudioTrackInterface*>(sender->track().get());
543 if (!track)
544 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100545 cricket::VoiceSenderInfo null_sender_info;
546 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
547 // TODO(hta): Checking on ssrc is not proper. There should be a way
548 // to see from a sender whether it's connected or not.
549 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800550 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100551 // When pc.close is called, sender info is discarded, so
552 // we generate zeroes instead. Bug: It should be retained.
553 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800554 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100555 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100556 if (sender_info) {
557 voice_sender_info = sender_info;
558 } else {
559 RTC_LOG(LS_INFO)
560 << "RTCStatsCollector: No voice sender info for sender with ssrc "
561 << sender->ssrc();
562 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100563 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100564 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100565 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
566 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100567 report->AddStats(std::move(audio_track_stats));
568 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
569 VideoTrackInterface* track =
570 static_cast<VideoTrackInterface*>(sender->track().get());
571 if (!track)
572 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100573 cricket::VideoSenderInfo null_sender_info;
574 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
575 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100576 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
577 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800578 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100579 // When pc.close is called, sender info is discarded, so
580 // we generate zeroes instead. Bug: It should be retained.
581 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800582 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100583 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100584 if (sender_info) {
585 video_sender_info = sender_info;
586 } else {
587 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
588 << sender->ssrc();
589 }
590 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100591 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100592 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
593 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100594 report->AddStats(std::move(video_track_stats));
595 } else {
596 RTC_NOTREACHED();
597 }
598 }
599}
600
601void ProduceReceiverMediaTrackStats(
602 int64_t timestamp_us,
603 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800604 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100605 RTCStatsReport* report) {
606 // This function iterates over the receivers to find the remote tracks.
Steve Anton57858b32018-02-15 15:19:50 -0800607 for (auto receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100608 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
609 AudioTrackInterface* track =
610 static_cast<AudioTrackInterface*>(receiver->track().get());
611 const cricket::VoiceReceiverInfo* voice_receiver_info =
612 track_media_info_map.GetVoiceReceiverInfo(*track);
613 if (!voice_receiver_info) {
614 continue;
615 }
616 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
617 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100618 timestamp_us, *track, *voice_receiver_info,
619 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100620 report->AddStats(std::move(audio_track_stats));
621 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
622 VideoTrackInterface* track =
623 static_cast<VideoTrackInterface*>(receiver->track().get());
624 const cricket::VideoReceiverInfo* video_receiver_info =
625 track_media_info_map.GetVideoReceiverInfo(*track);
626 if (!video_receiver_info) {
627 continue;
628 }
629 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
630 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100631 timestamp_us, *track, *video_receiver_info,
632 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100633 report->AddStats(std::move(video_track_stats));
634 } else {
635 RTC_NOTREACHED();
636 }
637 }
638}
639
Henrik Boström5b3541f2018-03-19 13:52:56 +0100640rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
641 bool filter_by_sender_selector,
642 rtc::scoped_refptr<const RTCStatsReport> report,
643 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
644 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
645 std::vector<std::string> rtpstream_ids;
646 if (filter_by_sender_selector) {
647 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
648 if (sender_selector) {
649 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
650 // reference the sender stats.
651 // Because we do not implement sender stats, we look at outbound-rtp(s)
652 // that reference the track attachment stats for the sender instead.
653 std::string track_id =
654 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
655 kSender, sender_selector->AttachmentId());
656 for (const auto& stats : *report) {
657 if (stats.type() != RTCOutboundRTPStreamStats::kType)
658 continue;
659 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
660 if (outbound_rtp.track_id.is_defined() &&
661 *outbound_rtp.track_id == track_id) {
662 rtpstream_ids.push_back(outbound_rtp.id());
663 }
664 }
665 }
666 } else {
667 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
668 if (receiver_selector) {
669 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
670 // reference the receiver stats.
671 // Because we do not implement receiver stats, we look at inbound-rtp(s)
672 // that reference the track attachment stats for the receiver instead.
673 std::string track_id =
674 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
675 kReceiver, receiver_selector->AttachmentId());
676 for (const auto& stats : *report) {
677 if (stats.type() != RTCInboundRTPStreamStats::kType)
678 continue;
679 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
680 if (inbound_rtp.track_id.is_defined() &&
681 *inbound_rtp.track_id == track_id) {
682 rtpstream_ids.push_back(inbound_rtp.id());
683 }
684 }
685 }
686 }
687 if (rtpstream_ids.empty())
688 return RTCStatsReport::Create(report->timestamp_us());
689 return TakeReferencedStats(report->Copy(), rtpstream_ids);
690}
691
hboscc555c52016-10-18 12:48:31 -0700692} // namespace
693
Henrik Boström5b3541f2018-03-19 13:52:56 +0100694RTCStatsCollector::RequestInfo::RequestInfo(
695 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
696 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
697
698RTCStatsCollector::RequestInfo::RequestInfo(
699 rtc::scoped_refptr<RtpSenderInternal> selector,
700 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
701 : RequestInfo(FilterMode::kSenderSelector,
702 std::move(callback),
703 std::move(selector),
704 nullptr) {}
705
706RTCStatsCollector::RequestInfo::RequestInfo(
707 rtc::scoped_refptr<RtpReceiverInternal> selector,
708 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
709 : RequestInfo(FilterMode::kReceiverSelector,
710 std::move(callback),
711 nullptr,
712 std::move(selector)) {}
713
714RTCStatsCollector::RequestInfo::RequestInfo(
715 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
716 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
717 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
718 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
719 : filter_mode_(filter_mode),
720 callback_(std::move(callback)),
721 sender_selector_(std::move(sender_selector)),
722 receiver_selector_(std::move(receiver_selector)) {
723 RTC_DCHECK(callback_);
724 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
725}
726
hbosc82f2e12016-09-05 01:36:50 -0700727rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800728 PeerConnectionInternal* pc,
729 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700730 return rtc::scoped_refptr<RTCStatsCollector>(
731 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
732}
733
Steve Anton2d8609c2018-01-23 16:38:46 -0800734RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700735 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700736 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700737 signaling_thread_(pc->signaling_thread()),
738 worker_thread_(pc->worker_thread()),
739 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700740 num_pending_partial_reports_(0),
741 partial_report_timestamp_us_(0),
hbos0e6758d2016-08-31 07:57:36 -0700742 cache_timestamp_us_(0),
743 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700744 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700745 RTC_DCHECK(signaling_thread_);
746 RTC_DCHECK(worker_thread_);
747 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700748 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800749 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800750 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700751}
752
hbosb78306a2016-12-19 05:06:57 -0800753RTCStatsCollector::~RTCStatsCollector() {
754 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
755}
756
hbosc82f2e12016-09-05 01:36:50 -0700757void RTCStatsCollector::GetStatsReport(
758 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100759 GetStatsReportInternal(RequestInfo(std::move(callback)));
760}
761
762void RTCStatsCollector::GetStatsReport(
763 rtc::scoped_refptr<RtpSenderInternal> selector,
764 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
765 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
766}
767
768void RTCStatsCollector::GetStatsReport(
769 rtc::scoped_refptr<RtpReceiverInternal> selector,
770 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
771 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
772}
773
774void RTCStatsCollector::GetStatsReportInternal(
775 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700776 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100777 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700778
hbos0e6758d2016-08-31 07:57:36 -0700779 // "Now" using a monotonically increasing timer.
780 int64_t cache_now_us = rtc::TimeMicros();
781 if (cached_report_ &&
782 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800783 // We have a fresh cached report to deliver. Deliver asynchronously, since
784 // the caller may not be expecting a synchronous callback, and it avoids
785 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100786 std::vector<RequestInfo> requests;
787 requests.swap(requests_);
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800788 invoker_.AsyncInvoke<void>(
789 RTC_FROM_HERE, signaling_thread_,
790 rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this, cached_report_,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100791 std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700792 } else if (!num_pending_partial_reports_) {
793 // Only start gathering stats if we're not already gathering stats. In the
794 // case of already gathering stats, |callback_| will be invoked when there
795 // are no more pending partial reports.
796
797 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
798 // UTC), in microseconds. The system clock could be modified and is not
799 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700800 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700801
hbosf415f8a2017-01-02 04:28:51 -0800802 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700803 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800804
Steve Anton57858b32018-02-15 15:19:50 -0800805 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800806 // |ProducePartialResultsOnNetworkThread| and
807 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800808 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700809 // Prepare |transport_names_| for use in
810 // |ProducePartialResultsOnNetworkThread|.
811 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800812
stefanf79ade12017-06-02 06:44:03 -0700813 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
814 // thread.
815 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
816 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700817 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700818
819 invoker_.AsyncInvoke<void>(
820 RTC_FROM_HERE, network_thread_,
hbosc82f2e12016-09-05 01:36:50 -0700821 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
stefanf79ade12017-06-02 06:44:03 -0700822 rtc::scoped_refptr<RTCStatsCollector>(this), timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800823 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700824 }
hbosd565b732016-08-30 14:04:35 -0700825}
826
827void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700828 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700829 cached_report_ = nullptr;
830}
831
hbosb78306a2016-12-19 05:06:57 -0800832void RTCStatsCollector::WaitForPendingRequest() {
833 RTC_DCHECK(signaling_thread_->IsCurrent());
834 if (num_pending_partial_reports_) {
835 rtc::Thread::Current()->ProcessMessages(0);
836 while (num_pending_partial_reports_) {
837 rtc::Thread::Current()->SleepMs(1);
838 rtc::Thread::Current()->ProcessMessages(0);
839 }
840 }
841}
842
hbosc82f2e12016-09-05 01:36:50 -0700843void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
844 int64_t timestamp_us) {
845 RTC_DCHECK(signaling_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700846 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
847 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700848
hboscc555c52016-10-18 12:48:31 -0700849 ProduceDataChannelStats_s(timestamp_us, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800850 ProduceMediaStreamStats_s(timestamp_us, report.get());
851 ProduceMediaStreamTrackStats_s(timestamp_us, report.get());
hbos6ab97ce2016-10-03 14:16:56 -0700852 ProducePeerConnectionStats_s(timestamp_us, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700853
854 AddPartialResults(report);
855}
856
hbosc82f2e12016-09-05 01:36:50 -0700857void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
858 int64_t timestamp_us) {
859 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -0700860 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(
861 timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700862
Steve Anton5dfde182018-02-06 10:34:40 -0800863 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700864 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800865
866 std::map<std::string, CertificateStatsPair> transport_cert_stats =
867 PrepareTransportCertificateStats_n(transport_stats_by_name);
868
869 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800870 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800871 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
Steve Anton5dfde182018-02-06 10:34:40 -0800872 call_stats_, report.get());
Steve Anton57858b32018-02-15 15:19:50 -0800873 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_, report.get());
Steve Anton5dfde182018-02-06 10:34:40 -0800874 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
875 transport_cert_stats, report.get());
hbosc82f2e12016-09-05 01:36:50 -0700876
877 AddPartialResults(report);
878}
879
880void RTCStatsCollector::AddPartialResults(
881 const rtc::scoped_refptr<RTCStatsReport>& partial_report) {
882 if (!signaling_thread_->IsCurrent()) {
883 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
884 rtc::Bind(&RTCStatsCollector::AddPartialResults_s,
885 rtc::scoped_refptr<RTCStatsCollector>(this),
886 partial_report));
887 return;
888 }
889 AddPartialResults_s(partial_report);
890}
891
892void RTCStatsCollector::AddPartialResults_s(
893 rtc::scoped_refptr<RTCStatsReport> partial_report) {
894 RTC_DCHECK(signaling_thread_->IsCurrent());
895 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
896 if (!partial_report_)
897 partial_report_ = partial_report;
898 else
899 partial_report_->TakeMembersFrom(partial_report);
900 --num_pending_partial_reports_;
901 if (!num_pending_partial_reports_) {
902 cache_timestamp_us_ = partial_report_timestamp_us_;
903 cached_report_ = partial_report_;
904 partial_report_ = nullptr;
Steve Anton57858b32018-02-15 15:19:50 -0800905 transceiver_stats_infos_.clear();
ehmaldonadoa26196b2017-07-18 03:30:29 -0700906 // Trace WebRTC Stats when getStats is called on Javascript.
907 // This allows access to WebRTC stats from trace logs. To enable them,
908 // select the "webrtc_stats" category when recording traces.
ehmaldonado8ab0fd82017-09-04 14:35:04 -0700909 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
910 cached_report_->ToJson());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800911
Henrik Boström5b3541f2018-03-19 13:52:56 +0100912 // Deliver report and clear |requests_|.
913 std::vector<RequestInfo> requests;
914 requests.swap(requests_);
915 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -0700916 }
917}
918
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800919void RTCStatsCollector::DeliverCachedReport(
920 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +0100921 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -0700922 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100923 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800924 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -0800925
Henrik Boström5b3541f2018-03-19 13:52:56 +0100926 for (const RequestInfo& request : requests) {
927 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
928 request.callback()->OnStatsDelivered(cached_report);
929 } else {
930 bool filter_by_sender_selector;
931 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
932 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
933 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
934 filter_by_sender_selector = true;
935 sender_selector = request.sender_selector();
936 } else {
937 RTC_DCHECK(request.filter_mode() ==
938 RequestInfo::FilterMode::kReceiverSelector);
939 filter_by_sender_selector = false;
940 receiver_selector = request.receiver_selector();
941 }
942 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
943 filter_by_sender_selector, cached_report, sender_selector,
944 receiver_selector));
945 }
hbosc82f2e12016-09-05 01:36:50 -0700946 }
hbosd565b732016-08-30 14:04:35 -0700947}
948
hbosdf6075a2016-12-19 04:58:02 -0800949void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -0700950 int64_t timestamp_us,
951 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -0700952 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800953 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -0700954 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
955 if (transport_cert_stats_pair.second.local) {
956 ProduceCertificateStatsFromSSLCertificateStats(
957 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700958 }
hbos02ba2112016-10-28 05:14:53 -0700959 if (transport_cert_stats_pair.second.remote) {
960 ProduceCertificateStatsFromSSLCertificateStats(
961 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -0700962 }
963 }
964}
965
hbosdf6075a2016-12-19 04:58:02 -0800966void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -0800967 int64_t timestamp_us,
968 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -0800969 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -0800970 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -0800971 for (const auto& stats : transceiver_stats_infos) {
972 if (!stats.mid) {
973 continue;
hbos0adb8282016-11-23 02:32:06 -0800974 }
Steve Anton57858b32018-02-15 15:19:50 -0800975 const cricket::VoiceMediaInfo* voice_media_info =
976 stats.track_media_info_map->voice_media_info();
977 const cricket::VideoMediaInfo* video_media_info =
978 stats.track_media_info_map->video_media_info();
979 // Audio
980 if (voice_media_info) {
981 // Inbound
982 for (const auto& pair : voice_media_info->receive_codecs) {
983 report->AddStats(CodecStatsFromRtpCodecParameters(
984 timestamp_us, *stats.mid, true, pair.second));
985 }
986 // Outbound
987 for (const auto& pair : voice_media_info->send_codecs) {
988 report->AddStats(CodecStatsFromRtpCodecParameters(
989 timestamp_us, *stats.mid, false, pair.second));
990 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +0000991 }
Steve Anton57858b32018-02-15 15:19:50 -0800992 // Video
993 if (video_media_info) {
994 // Inbound
995 for (const auto& pair : video_media_info->receive_codecs) {
996 report->AddStats(CodecStatsFromRtpCodecParameters(
997 timestamp_us, *stats.mid, true, pair.second));
998 }
999 // Outbound
1000 for (const auto& pair : video_media_info->send_codecs) {
1001 report->AddStats(CodecStatsFromRtpCodecParameters(
1002 timestamp_us, *stats.mid, false, pair.second));
1003 }
hbos0adb8282016-11-23 02:32:06 -08001004 }
1005 }
1006}
1007
hboscc555c52016-10-18 12:48:31 -07001008void RTCStatsCollector::ProduceDataChannelStats_s(
1009 int64_t timestamp_us, RTCStatsReport* report) const {
1010 RTC_DCHECK(signaling_thread_->IsCurrent());
1011 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1012 pc_->sctp_data_channels()) {
1013 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1014 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001015 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001016 timestamp_us));
1017 data_channel_stats->label = data_channel->label();
1018 data_channel_stats->protocol = data_channel->protocol();
1019 data_channel_stats->datachannelid = data_channel->id();
1020 data_channel_stats->state =
1021 DataStateToRTCDataChannelState(data_channel->state());
1022 data_channel_stats->messages_sent = data_channel->messages_sent();
1023 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1024 data_channel_stats->messages_received = data_channel->messages_received();
1025 data_channel_stats->bytes_received = data_channel->bytes_received();
1026 report->AddStats(std::move(data_channel_stats));
1027 }
1028}
1029
hbosdf6075a2016-12-19 04:58:02 -08001030void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001031 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001032 const std::map<std::string, cricket::TransportStats>&
1033 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001034 const Call::Stats& call_stats,
1035 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001036 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001037 for (const auto& entry : transport_stats_by_name) {
1038 const std::string& transport_name = entry.first;
1039 const cricket::TransportStats& transport_stats = entry.second;
1040 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001041 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001042 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001043 for (const cricket::ConnectionInfo& info :
1044 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001045 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001046 new RTCIceCandidatePairStats(
1047 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1048 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001049
hbos0583b282016-11-30 01:50:14 -08001050 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001051 // TODO(hbos): There could be other candidates that are not paired with
1052 // anything. We don't have a complete list. Local candidates come from
1053 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001054 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001055 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001056 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001057 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001058 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001059 candidate_pair_stats->state =
1060 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1061 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001062 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001063 // TODO(hbos): This writable is different than the spec. It goes to
1064 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001065 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001066 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001067 candidate_pair_stats->bytes_sent =
1068 static_cast<uint64_t>(info.sent_total_bytes);
1069 candidate_pair_stats->bytes_received =
1070 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001071 candidate_pair_stats->total_round_trip_time =
1072 static_cast<double>(info.total_round_trip_time_ms) /
1073 rtc::kNumMillisecsPerSec;
1074 if (info.current_round_trip_time_ms) {
1075 candidate_pair_stats->current_round_trip_time =
1076 static_cast<double>(*info.current_round_trip_time_ms) /
1077 rtc::kNumMillisecsPerSec;
1078 }
stefanf79ade12017-06-02 06:44:03 -07001079 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001080 // The bandwidth estimations we have are for the selected candidate
1081 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001082 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1083 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1084 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001085 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001086 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001087 }
stefanf79ade12017-06-02 06:44:03 -07001088 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001089 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001090 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001091 }
1092 }
hbosd82f5122016-12-09 04:12:39 -08001093 candidate_pair_stats->requests_received =
1094 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001095 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1096 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001097 candidate_pair_stats->responses_received =
1098 static_cast<uint64_t>(info.recv_ping_responses);
1099 candidate_pair_stats->responses_sent =
1100 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001101 RTC_DCHECK_GE(info.sent_ping_requests_total,
1102 info.sent_ping_requests_before_first_response);
1103 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1104 info.sent_ping_requests_total -
1105 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001106
1107 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001108 }
1109 }
1110 }
1111}
1112
Steve Anton57858b32018-02-15 15:19:50 -08001113void RTCStatsCollector::ProduceMediaStreamStats_s(
1114 int64_t timestamp_us,
1115 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001116 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001117
1118 std::map<std::string, std::vector<std::string>> track_ids;
1119
1120 for (const auto& stats : transceiver_stats_infos_) {
1121 for (auto sender : stats.transceiver->senders()) {
1122 std::string track_id =
1123 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1124 kSender, sender->internal()->AttachmentId());
1125 for (auto& stream_id : sender->stream_ids()) {
1126 track_ids[stream_id].push_back(track_id);
1127 }
1128 }
1129 for (auto receiver : stats.transceiver->receivers()) {
1130 std::string track_id =
1131 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1132 kReceiver, receiver->internal()->AttachmentId());
1133 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001134 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001135 }
1136 }
1137 }
1138
1139 // Build stats for each stream ID known.
1140 for (auto& it : track_ids) {
1141 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1142 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1143 stream_stats->stream_identifier = it.first;
1144 stream_stats->track_ids = it.second;
1145 report->AddStats(std::move(stream_stats));
1146 }
1147}
1148
1149void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1150 int64_t timestamp_us,
1151 RTCStatsReport* report) const {
1152 RTC_DCHECK(signaling_thread_->IsCurrent());
1153 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1154 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1155 for (auto sender : stats.transceiver->senders()) {
1156 senders.push_back(sender->internal());
1157 }
1158 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1159 senders, report);
1160
1161 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1162 for (auto receiver : stats.transceiver->receivers()) {
1163 receivers.push_back(receiver->internal());
1164 }
1165 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1166 receivers, report);
1167 }
hbos09bc1282016-11-08 06:29:22 -08001168}
1169
hbos6ab97ce2016-10-03 14:16:56 -07001170void RTCStatsCollector::ProducePeerConnectionStats_s(
1171 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001172 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001173 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001174 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001175 stats->data_channels_opened = internal_record_.data_channels_opened;
1176 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001177 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001178}
1179
hbosdf6075a2016-12-19 04:58:02 -08001180void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001181 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001182 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001183 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001184 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001185
Steve Anton57858b32018-02-15 15:19:50 -08001186 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1187 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1188 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1189 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1190 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1191 } else {
1192 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001193 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001194 }
Steve Anton57858b32018-02-15 15:19:50 -08001195}
1196
1197void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1198 int64_t timestamp_us,
1199 const RtpTransceiverStatsInfo& stats,
1200 RTCStatsReport* report) const {
1201 if (!stats.mid || !stats.transport_name) {
1202 return;
1203 }
1204 RTC_DCHECK(stats.track_media_info_map);
1205 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1206 RTC_DCHECK(track_media_info_map.voice_media_info());
1207 std::string mid = *stats.mid;
1208 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1209 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1210 // Inbound
1211 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1212 track_media_info_map.voice_media_info()->receivers) {
1213 if (!voice_receiver_info.connected())
1214 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001215 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001216 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1217 timestamp_us);
1218 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1219 inbound_audio.get());
1220 // TODO(hta): This lookup should look for the sender, not the track.
1221 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1222 track_media_info_map.GetAudioTrack(voice_receiver_info);
1223 if (audio_track) {
1224 inbound_audio->track_id =
1225 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1226 kReceiver,
1227 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001228 }
Steve Anton57858b32018-02-15 15:19:50 -08001229 inbound_audio->transport_id = transport_id;
1230 report->AddStats(std::move(inbound_audio));
1231 }
1232 // Outbound
1233 for (const cricket::VoiceSenderInfo& voice_sender_info :
1234 track_media_info_map.voice_media_info()->senders) {
1235 if (!voice_sender_info.connected())
1236 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001237 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001238 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1239 timestamp_us);
1240 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1241 outbound_audio.get());
1242 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1243 track_media_info_map.GetAudioTrack(voice_sender_info);
1244 if (audio_track) {
1245 outbound_audio->track_id =
1246 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1247 kSender,
1248 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-14 16:07:42 -08001249 }
Steve Anton57858b32018-02-15 15:19:50 -08001250 outbound_audio->transport_id = transport_id;
1251 report->AddStats(std::move(outbound_audio));
1252 }
1253}
1254
1255void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1256 int64_t timestamp_us,
1257 const RtpTransceiverStatsInfo& stats,
1258 RTCStatsReport* report) const {
1259 if (!stats.mid || !stats.transport_name) {
1260 return;
1261 }
1262 RTC_DCHECK(stats.track_media_info_map);
1263 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1264 RTC_DCHECK(track_media_info_map.video_media_info());
1265 std::string mid = *stats.mid;
1266 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1267 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1268 // Inbound
1269 for (const cricket::VideoReceiverInfo& video_receiver_info :
1270 track_media_info_map.video_media_info()->receivers) {
1271 if (!video_receiver_info.connected())
1272 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001273 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001274 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1275 timestamp_us);
1276 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1277 inbound_video.get());
1278 rtc::scoped_refptr<VideoTrackInterface> video_track =
1279 track_media_info_map.GetVideoTrack(video_receiver_info);
1280 if (video_track) {
1281 inbound_video->track_id =
1282 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1283 kReceiver,
1284 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1285 }
1286 inbound_video->transport_id = transport_id;
1287 report->AddStats(std::move(inbound_video));
1288 }
1289 // Outbound
1290 for (const cricket::VideoSenderInfo& video_sender_info :
1291 track_media_info_map.video_media_info()->senders) {
1292 if (!video_sender_info.connected())
1293 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001294 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001295 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1296 timestamp_us);
1297 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1298 outbound_video.get());
1299 rtc::scoped_refptr<VideoTrackInterface> video_track =
1300 track_media_info_map.GetVideoTrack(video_sender_info);
1301 if (video_track) {
1302 outbound_video->track_id =
1303 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1304 kSender,
1305 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1306 }
1307 outbound_video->transport_id = transport_id;
1308 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001309 }
1310}
1311
hbosdf6075a2016-12-19 04:58:02 -08001312void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001313 int64_t timestamp_us,
1314 const std::map<std::string, cricket::TransportStats>&
1315 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001316 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1317 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001318 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001319 for (const auto& entry : transport_stats_by_name) {
1320 const std::string& transport_name = entry.first;
1321 const cricket::TransportStats& transport_stats = entry.second;
1322
hbos2fa7c672016-10-24 04:00:05 -07001323 // Get reference to RTCP channel, if it exists.
1324 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001325 for (const cricket::TransportChannelStats& channel_stats :
1326 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001327 if (channel_stats.component ==
1328 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1329 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001330 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001331 break;
1332 }
1333 }
1334
1335 // Get reference to local and remote certificates of this transport, if they
1336 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001337 const auto& certificate_stats_it =
1338 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001339 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1340 std::string local_certificate_id;
1341 if (certificate_stats_it->second.local) {
1342 local_certificate_id = RTCCertificateIDFromFingerprint(
1343 certificate_stats_it->second.local->fingerprint);
1344 }
1345 std::string remote_certificate_id;
1346 if (certificate_stats_it->second.remote) {
1347 remote_certificate_id = RTCCertificateIDFromFingerprint(
1348 certificate_stats_it->second.remote->fingerprint);
1349 }
1350
1351 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001352 for (const cricket::TransportChannelStats& channel_stats :
1353 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001354 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001355 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1356 transport_name, channel_stats.component),
1357 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001358 transport_stats->bytes_sent = 0;
1359 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001360 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1361 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001362 for (const cricket::ConnectionInfo& info :
1363 channel_stats.connection_infos) {
1364 *transport_stats->bytes_sent += info.sent_total_bytes;
1365 *transport_stats->bytes_received += info.recv_total_bytes;
1366 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001367 transport_stats->selected_candidate_pair_id =
1368 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1369 }
1370 }
1371 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1372 !rtcp_transport_stats_id.empty()) {
1373 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1374 }
1375 if (!local_certificate_id.empty())
1376 transport_stats->local_certificate_id = local_certificate_id;
1377 if (!remote_certificate_id.empty())
1378 transport_stats->remote_certificate_id = remote_certificate_id;
1379 report->AddStats(std::move(transport_stats));
1380 }
1381 }
1382}
1383
1384std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001385RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001386 const std::map<std::string, cricket::TransportStats>&
1387 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001388 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001389 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001390 for (const auto& entry : transport_stats_by_name) {
1391 const std::string& transport_name = entry.first;
1392
hbos2fa7c672016-10-24 04:00:05 -07001393 CertificateStatsPair certificate_stats_pair;
1394 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001395 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001396 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001397 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001398 }
Steve Anton5dfde182018-02-06 10:34:40 -08001399
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001400 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1401 pc_->GetRemoteSSLCertChain(transport_name);
1402 if (remote_cert_chain) {
1403 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001404 }
Steve Anton5dfde182018-02-06 10:34:40 -08001405
hbos2fa7c672016-10-24 04:00:05 -07001406 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001407 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001408 }
1409 return transport_cert_stats;
1410}
1411
Steve Anton57858b32018-02-15 15:19:50 -08001412std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1413RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1414 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001415
Steve Anton57858b32018-02-15 15:19:50 -08001416 // These are used to invoke GetStats for all the media channels together in
1417 // one worker thread hop.
1418 std::map<cricket::VoiceMediaChannel*,
1419 std::unique_ptr<cricket::VoiceMediaInfo>>
1420 voice_stats;
1421 std::map<cricket::VideoMediaChannel*,
1422 std::unique_ptr<cricket::VideoMediaInfo>>
1423 video_stats;
1424
1425 for (auto transceiver : pc_->GetTransceiversInternal()) {
1426 cricket::MediaType media_type = transceiver->media_type();
1427
1428 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1429 // stats have been fetched on the worker thread.
1430 transceiver_stats_infos.emplace_back();
1431 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1432 stats.transceiver = transceiver->internal();
1433 stats.media_type = media_type;
1434
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001435 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001436 if (!channel) {
1437 // The remaining fields require a BaseChannel.
1438 continue;
1439 }
1440
1441 stats.mid = channel->content_name();
1442 stats.transport_name = channel->transport_name();
1443
1444 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1445 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1446 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1447 voice_stats.end());
1448 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001449 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001450 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1451 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1452 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1453 video_stats.end());
1454 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001455 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001456 } else {
1457 RTC_NOTREACHED();
1458 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001459 }
Steve Anton57858b32018-02-15 15:19:50 -08001460
1461 // Call GetStats for all media channels together on the worker thread in one
1462 // hop.
1463 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1464 for (const auto& entry : voice_stats) {
1465 if (!entry.first->GetStats(entry.second.get())) {
1466 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1467 }
1468 }
1469 for (const auto& entry : video_stats) {
1470 if (!entry.first->GetStats(entry.second.get())) {
1471 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1472 }
1473 }
1474 });
1475
1476 // Create the TrackMediaInfoMap for each transceiver stats object.
1477 for (auto& stats : transceiver_stats_infos) {
1478 auto transceiver = stats.transceiver;
1479 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1480 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1481 if (transceiver->channel()) {
1482 cricket::MediaType media_type = transceiver->media_type();
1483 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1484 auto* voice_channel =
1485 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1486 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1487 voice_media_info =
1488 std::move(voice_stats[voice_channel->media_channel()]);
1489 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1490 auto* video_channel =
1491 static_cast<cricket::VideoChannel*>(transceiver->channel());
1492 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1493 video_media_info =
1494 std::move(video_stats[video_channel->media_channel()]);
1495 }
1496 }
1497 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
1498 for (auto sender : transceiver->senders()) {
1499 senders.push_back(sender->internal());
1500 }
1501 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
1502 for (auto receiver : transceiver->receivers()) {
1503 receivers.push_back(receiver->internal());
1504 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001505 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001506 std::move(voice_media_info), std::move(video_media_info), senders,
1507 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001508 }
Steve Anton57858b32018-02-15 15:19:50 -08001509
1510 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001511}
1512
Steve Anton7eca0932018-03-30 15:18:41 -07001513std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1514 std::set<std::string> transport_names;
1515 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1516 if (transceiver->internal()->channel()) {
1517 transport_names.insert(
1518 transceiver->internal()->channel()->transport_name());
1519 }
1520 }
1521 if (pc_->rtp_data_channel()) {
1522 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1523 }
1524 if (pc_->sctp_transport_name()) {
1525 transport_names.insert(*pc_->sctp_transport_name());
1526 }
1527 return transport_names;
1528}
1529
hbos82ebe022016-11-14 01:41:09 -08001530void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1531 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1532 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1533}
1534
1535void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1536 RTC_DCHECK(signaling_thread_->IsCurrent());
1537 bool result = internal_record_.opened_data_channels.insert(
1538 reinterpret_cast<uintptr_t>(channel)).second;
1539 ++internal_record_.data_channels_opened;
1540 RTC_DCHECK(result);
1541}
1542
1543void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1544 RTC_DCHECK(signaling_thread_->IsCurrent());
1545 // Only channels that have been fully opened (and have increased the
1546 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001547 if (internal_record_.opened_data_channels.erase(
1548 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001549 ++internal_record_.data_channels_closed;
1550 }
1551}
1552
hboscc555c52016-10-18 12:48:31 -07001553const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1554 const std::string& type) {
1555 return CandidateTypeToRTCIceCandidateType(type);
1556}
1557
1558const char* DataStateToRTCDataChannelStateForTesting(
1559 DataChannelInterface::DataState state) {
1560 return DataStateToRTCDataChannelState(state);
1561}
1562
hbosd565b732016-08-30 14:04:35 -07001563} // namespace webrtc