blob: 611dfe373e4d594253ed811158b2121d6bf3e241 [file] [log] [blame]
hbosd565b732016-08-30 21:04:351/*
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 17:11:0011#include "pc/rtc_stats_collector.h"
hbosd565b732016-08-30 21:04:3512
13#include <memory>
Steve Anton36b29d12017-10-30 16:57:4214#include <string>
hbosd565b732016-08-30 21:04:3515#include <utility>
16#include <vector>
17
Karl Wiberg918f50c2018-07-05 09:40:3318#include "absl/memory/memory.h"
Patrik Höglunde2d6a062017-10-05 12:53:3319#include "api/candidate.h"
Steve Anton10542f22019-01-11 17:11:0020#include "api/media_stream_interface.h"
21#include "api/peer_connection_interface.h"
Henrik Boström2e069262019-04-09 11:59:3122#include "api/video/video_content_type.h"
Steve Anton10542f22019-01-11 17:11:0023#include "media/base/media_channel.h"
24#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 17:11:0026#include "pc/peer_connection.h"
27#include "pc/rtc_stats_traversal.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "rtc_base/checks.h"
Jonas Olsson43568dd2018-06-11 14:25:5429#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 17:11:0030#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3131#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 21:04:3532
33namespace webrtc {
34
hboscc555c52016-10-18 19:48:3135namespace {
36
hbos2fa7c672016-10-24 11:00:0537std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
38 return "RTCCertificate_" + fingerprint;
39}
40
Steve Anton57858b32018-02-15 23:19:5041std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
42 bool inbound,
43 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 14:25:5444 char buf[1024];
45 rtc::SimpleStringBuilder sb(buf);
46 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
47 << payload_type;
48 return sb.str();
hbos0adb8282016-11-23 10:32:0649}
50
hbos2fa7c672016-10-24 11:00:0551std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
52 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 14:25:5453 char buf[4096];
54 rtc::SimpleStringBuilder sb(buf);
55 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
56 << info.remote_candidate.id();
57 return sb.str();
hbos2fa7c672016-10-24 11:00:0558}
59
Harald Alvestranda3dab842018-01-14 08:18:5860const char kSender[] = "sender";
61const char kReceiver[] = "receiver";
62
63std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
64 const char* direction,
Harald Alvestrandc72af932018-01-11 16:18:1965 int attachment_id) {
Jonas Olsson43568dd2018-06-11 14:25:5466 char buf[1024];
67 rtc::SimpleStringBuilder sb(buf);
68 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
69 return sb.str();
hbos09bc1282016-11-08 14:29:2270}
71
hbos2fa7c672016-10-24 11:00:0572std::string RTCTransportStatsIDFromTransportChannel(
73 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 14:25:5474 char buf[1024];
75 rtc::SimpleStringBuilder sb(buf);
76 sb << "RTCTransport_" << transport_name << "_" << channel_component;
77 return sb.str();
hbos2fa7c672016-10-24 11:00:0578}
79
hboseeafe942016-11-01 10:00:1780std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 14:25:5481 char buf[1024];
82 rtc::SimpleStringBuilder sb(buf);
83 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
84 return sb.str();
hboseeafe942016-11-01 10:00:1785}
86
hbos6ded1902016-11-01 08:50:4687std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 14:25:5488 char buf[1024];
89 rtc::SimpleStringBuilder sb(buf);
90 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
91 return sb.str();
hbos6ded1902016-11-01 08:50:4692}
93
hbosab9f6e42016-10-07 09:18:4794const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
95 if (type == cricket::LOCAL_PORT_TYPE)
96 return RTCIceCandidateType::kHost;
97 if (type == cricket::STUN_PORT_TYPE)
98 return RTCIceCandidateType::kSrflx;
99 if (type == cricket::PRFLX_PORT_TYPE)
100 return RTCIceCandidateType::kPrflx;
101 if (type == cricket::RELAY_PORT_TYPE)
102 return RTCIceCandidateType::kRelay;
103 RTC_NOTREACHED();
104 return nullptr;
105}
106
hboscc555c52016-10-18 19:48:31107const char* DataStateToRTCDataChannelState(
108 DataChannelInterface::DataState state) {
109 switch (state) {
110 case DataChannelInterface::kConnecting:
111 return RTCDataChannelState::kConnecting;
112 case DataChannelInterface::kOpen:
113 return RTCDataChannelState::kOpen;
114 case DataChannelInterface::kClosing:
115 return RTCDataChannelState::kClosing;
116 case DataChannelInterface::kClosed:
117 return RTCDataChannelState::kClosed;
118 default:
119 RTC_NOTREACHED();
120 return nullptr;
121 }
122}
123
hbos06495bc2017-01-02 16:08:18124const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
125 cricket::IceCandidatePairState state) {
126 switch (state) {
127 case cricket::IceCandidatePairState::WAITING:
128 return RTCStatsIceCandidatePairState::kWaiting;
129 case cricket::IceCandidatePairState::IN_PROGRESS:
130 return RTCStatsIceCandidatePairState::kInProgress;
131 case cricket::IceCandidatePairState::SUCCEEDED:
132 return RTCStatsIceCandidatePairState::kSucceeded;
133 case cricket::IceCandidatePairState::FAILED:
134 return RTCStatsIceCandidatePairState::kFailed;
135 default:
136 RTC_NOTREACHED();
137 return nullptr;
138 }
139}
140
hbos7064d592017-01-16 15:38:02141const char* DtlsTransportStateToRTCDtlsTransportState(
142 cricket::DtlsTransportState state) {
143 switch (state) {
144 case cricket::DTLS_TRANSPORT_NEW:
145 return RTCDtlsTransportState::kNew;
146 case cricket::DTLS_TRANSPORT_CONNECTING:
147 return RTCDtlsTransportState::kConnecting;
148 case cricket::DTLS_TRANSPORT_CONNECTED:
149 return RTCDtlsTransportState::kConnected;
150 case cricket::DTLS_TRANSPORT_CLOSED:
151 return RTCDtlsTransportState::kClosed;
152 case cricket::DTLS_TRANSPORT_FAILED:
153 return RTCDtlsTransportState::kFailed;
154 default:
155 RTC_NOTREACHED();
156 return nullptr;
157 }
158}
159
Gary Liu37e489c2017-11-21 18:49:36160const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
161 switch (type) {
162 case rtc::ADAPTER_TYPE_CELLULAR:
163 return RTCNetworkType::kCellular;
164 case rtc::ADAPTER_TYPE_ETHERNET:
165 return RTCNetworkType::kEthernet;
166 case rtc::ADAPTER_TYPE_WIFI:
167 return RTCNetworkType::kWifi;
168 case rtc::ADAPTER_TYPE_VPN:
169 return RTCNetworkType::kVpn;
170 case rtc::ADAPTER_TYPE_UNKNOWN:
171 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 22:38:09172 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 18:49:36173 return RTCNetworkType::kUnknown;
174 }
175 RTC_NOTREACHED();
176 return nullptr;
177}
178
hbos9e302742017-01-20 10:47:10179double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
180 RTC_DCHECK_GE(audio_level, 0);
181 RTC_DCHECK_LE(audio_level, 32767);
182 return audio_level / 32767.0;
183}
184
hbos0adb8282016-11-23 10:32:06185std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 23:19:50186 uint64_t timestamp_us,
187 const std::string& mid,
188 bool inbound,
hbos0adb8282016-11-23 10:32:06189 const RtpCodecParameters& codec_params) {
190 RTC_DCHECK_GE(codec_params.payload_type, 0);
191 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 20:09:01192 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 10:32:06193 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
194 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 23:19:50195 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 10:32:06196 timestamp_us));
197 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 14:56:04198 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 20:09:01199 if (codec_params.clock_rate) {
200 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
201 }
hbos0adb8282016-11-23 10:32:06202 return codec_stats;
203}
204
hbos09bc1282016-11-08 14:29:22205void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
206 const MediaStreamTrackInterface& track,
207 RTCMediaStreamTrackStats* track_stats) {
208 track_stats->track_identifier = track.id();
209 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
210}
211
hbos820f5782016-11-22 11:16:50212// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 10:00:17213void SetInboundRTPStreamStatsFromMediaReceiverInfo(
214 const cricket::MediaReceiverInfo& media_receiver_info,
215 RTCInboundRTPStreamStats* inbound_stats) {
216 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 14:28:11217 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 13:08:34218 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 10:00:17219 inbound_stats->is_remote = false;
hboseeafe942016-11-01 10:00:17220 inbound_stats->packets_received =
221 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
222 inbound_stats->bytes_received =
223 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 12:19:44224 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 11:26:04225 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 10:00:17226 inbound_stats->fraction_lost =
227 static_cast<double>(media_receiver_info.fraction_lost);
228}
229
230void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 23:19:50231 const std::string& mid,
hboseeafe942016-11-01 10:00:17232 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 11:16:50233 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 10:00:17234 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 11:16:50235 voice_receiver_info, inbound_audio);
236 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03237 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16238 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50239 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
240 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16241 }
hbos820f5782016-11-22 11:16:50242 inbound_audio->jitter =
hboseeafe942016-11-01 10:00:17243 static_cast<double>(voice_receiver_info.jitter_ms) /
244 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 11:16:50245 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
246 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 15:32:00247 if (voice_receiver_info.last_packet_received_timestamp_ms) {
248 inbound_audio->last_packet_received_timestamp =
249 static_cast<double>(
250 *voice_receiver_info.last_packet_received_timestamp_ms) /
251 rtc::kNumMillisecsPerSec;
252 }
hboseeafe942016-11-01 10:00:17253}
254
255void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 23:19:50256 const std::string& mid,
hboseeafe942016-11-01 10:00:17257 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 11:16:50258 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 10:00:17259 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 11:16:50260 video_receiver_info, inbound_video);
261 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03262 inbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16263 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50264 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
265 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16266 }
hbos820f5782016-11-22 11:16:50267 inbound_video->fir_count =
268 static_cast<uint32_t>(video_receiver_info.firs_sent);
269 inbound_video->pli_count =
270 static_cast<uint32_t>(video_receiver_info.plis_sent);
271 inbound_video->nack_count =
272 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 16:35:13273 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 13:34:48274 if (video_receiver_info.qp_sum)
275 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Henrik Boström01738c62019-04-15 15:32:00276 if (video_receiver_info.last_packet_received_timestamp_ms) {
277 inbound_video->last_packet_received_timestamp =
278 static_cast<double>(
279 *video_receiver_info.last_packet_received_timestamp_ms) /
280 rtc::kNumMillisecsPerSec;
281 }
Henrik Boström2e069262019-04-09 11:59:31282 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
283 // optional, support the "unspecified" value.
284 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
285 inbound_video->content_type = RTCContentType::kScreenshare;
hboseeafe942016-11-01 10:00:17286}
287
hbos820f5782016-11-22 11:16:50288// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 08:50:46289void SetOutboundRTPStreamStatsFromMediaSenderInfo(
290 const cricket::MediaSenderInfo& media_sender_info,
291 RTCOutboundRTPStreamStats* outbound_stats) {
292 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 14:28:11293 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 13:08:34294 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 08:50:46295 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 08:50:46296 outbound_stats->packets_sent =
297 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53298 outbound_stats->retransmitted_packets_sent =
299 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 08:50:46300 outbound_stats->bytes_sent =
301 static_cast<uint64_t>(media_sender_info.bytes_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53302 outbound_stats->retransmitted_bytes_sent =
303 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 08:50:46304}
305
306void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 23:19:50307 const std::string& mid,
hbos6ded1902016-11-01 08:50:46308 const cricket::VoiceSenderInfo& voice_sender_info,
309 RTCOutboundRTPStreamStats* outbound_audio) {
310 SetOutboundRTPStreamStatsFromMediaSenderInfo(
311 voice_sender_info, outbound_audio);
312 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03313 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16314 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50315 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
316 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16317 }
hbos6ded1902016-11-01 08:50:46318 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
319 // purposefully left undefined for audio.
320}
321
322void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 23:19:50323 const std::string& mid,
hbos6ded1902016-11-01 08:50:46324 const cricket::VideoSenderInfo& video_sender_info,
325 RTCOutboundRTPStreamStats* outbound_video) {
326 SetOutboundRTPStreamStatsFromMediaSenderInfo(
327 video_sender_info, outbound_video);
328 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03329 outbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16330 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50331 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
332 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16333 }
hbos6ded1902016-11-01 08:50:46334 outbound_video->fir_count =
335 static_cast<uint32_t>(video_sender_info.firs_rcvd);
336 outbound_video->pli_count =
337 static_cast<uint32_t>(video_sender_info.plis_rcvd);
338 outbound_video->nack_count =
339 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 16:35:13340 if (video_sender_info.qp_sum)
341 outbound_video->qp_sum = *video_sender_info.qp_sum;
342 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 14:14:23343 outbound_video->total_encode_time =
344 static_cast<double>(video_sender_info.total_encode_time_ms) /
345 rtc::kNumMillisecsPerSec;
Henrik Boström2e069262019-04-09 11:59:31346 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
347 // optional, support the "unspecified" value.
348 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
349 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 08:50:46350}
351
hbos02ba2112016-10-28 12:14:53352void ProduceCertificateStatsFromSSLCertificateStats(
353 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
354 RTCStatsReport* report) {
355 RTCCertificateStats* prev_certificate_stats = nullptr;
356 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
357 s = s->issuer.get()) {
hbos02d2a922016-12-21 09:29:05358 std::string certificate_stats_id =
359 RTCCertificateIDFromFingerprint(s->fingerprint);
360 // It is possible for the same certificate to show up multiple times, e.g.
361 // if local and remote side use the same certificate in a loopback call.
362 // If the report already contains stats for this certificate, skip it.
363 if (report->Get(certificate_stats_id)) {
364 RTC_DCHECK_EQ(s, &certificate_stats);
365 break;
366 }
hbos02ba2112016-10-28 12:14:53367 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 09:29:05368 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 12:14:53369 certificate_stats->fingerprint = s->fingerprint;
370 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
371 certificate_stats->base64_certificate = s->base64_certificate;
372 if (prev_certificate_stats)
373 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
374 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
375 prev_certificate_stats = certificate_stats;
376 }
377}
378
379const std::string& ProduceIceCandidateStats(
380 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 17:59:31381 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 12:14:53382 const std::string& id = "RTCIceCandidate_" + candidate.id();
383 const RTCStats* stats = report->Get(id);
384 if (!stats) {
385 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
386 if (is_local)
387 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
388 else
389 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 17:59:31390 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 18:49:36391 if (is_local) {
392 candidate_stats->network_type =
393 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 12:40:08394 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
395 std::string relay_protocol = candidate.relay_protocol();
396 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
397 relay_protocol.compare("tcp") == 0 ||
398 relay_protocol.compare("tls") == 0);
399 candidate_stats->relay_protocol = relay_protocol;
400 }
Gary Liu37e489c2017-11-21 18:49:36401 } else {
402 // We don't expect to know the adapter type of remote candidates.
403 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
404 }
hbos02ba2112016-10-28 12:14:53405 candidate_stats->ip = candidate.address().ipaddr().ToString();
406 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
407 candidate_stats->protocol = candidate.protocol();
408 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
409 candidate.type());
410 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
411
412 stats = candidate_stats.get();
413 report->AddStats(std::move(candidate_stats));
414 }
415 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
416 : RTCRemoteIceCandidateStats::kType);
417 return stats->id();
418}
419
hbos9e302742017-01-20 10:47:10420std::unique_ptr<RTCMediaStreamTrackStats>
421ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
422 int64_t timestamp_us,
423 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19424 const cricket::VoiceSenderInfo& voice_sender_info,
425 int attachment_id) {
hbos9e302742017-01-20 10:47:10426 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
427 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58428 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
429 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19430 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10431 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
432 audio_track, audio_track_stats.get());
433 audio_track_stats->remote_source = false;
434 audio_track_stats->detached = false;
435 if (voice_sender_info.audio_level >= 0) {
436 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
437 voice_sender_info.audio_level);
438 }
zsteine76bd3a2017-07-14 19:17:49439 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
440 audio_track_stats->total_samples_duration =
441 voice_sender_info.total_input_duration;
Ivo Creusen56d460902017-11-24 16:29:59442 if (voice_sender_info.apm_statistics.echo_return_loss) {
443 audio_track_stats->echo_return_loss =
444 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 10:47:10445 }
Ivo Creusen56d460902017-11-24 16:29:59446 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
447 audio_track_stats->echo_return_loss_enhancement =
448 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 10:47:10449 }
450 return audio_track_stats;
451}
452
453std::unique_ptr<RTCMediaStreamTrackStats>
454ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
455 int64_t timestamp_us,
456 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19457 const cricket::VoiceReceiverInfo& voice_receiver_info,
458 int attachment_id) {
459 // Since receiver tracks can't be reattached, we use the SSRC as
460 // an attachment identifier.
hbos9e302742017-01-20 10:47:10461 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
462 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58463 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
464 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19465 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10466 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
467 audio_track, audio_track_stats.get());
468 audio_track_stats->remote_source = true;
469 audio_track_stats->detached = false;
470 if (voice_receiver_info.audio_level >= 0) {
471 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
472 voice_receiver_info.audio_level);
473 }
Gustaf Ullbergb0a02072017-10-02 10:00:34474 audio_track_stats->jitter_buffer_delay =
475 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 14:46:29476 audio_track_stats->jitter_buffer_emitted_count =
477 voice_receiver_info.jitter_buffer_emitted_count;
zsteine76bd3a2017-07-14 19:17:49478 audio_track_stats->total_audio_energy =
479 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-25 00:15:13480 audio_track_stats->total_samples_received =
481 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 19:17:49482 audio_track_stats->total_samples_duration =
483 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-25 00:15:13484 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 07:28:20485 audio_track_stats->concealment_events =
486 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 16:21:10487 audio_track_stats->jitter_buffer_flushes =
488 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 11:52:16489 audio_track_stats->delayed_packet_outage_samples =
490 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 08:18:40491 audio_track_stats->relative_packet_arrival_delay =
492 voice_receiver_info.relative_packet_arrival_delay_seconds;
hbos9e302742017-01-20 10:47:10493 return audio_track_stats;
494}
495
496std::unique_ptr<RTCMediaStreamTrackStats>
497ProduceMediaStreamTrackStatsFromVideoSenderInfo(
498 int64_t timestamp_us,
499 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19500 const cricket::VideoSenderInfo& video_sender_info,
501 int attachment_id) {
hbos9e302742017-01-20 10:47:10502 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
503 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58504 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
505
506 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19507 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10508 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
509 video_track, video_track_stats.get());
510 video_track_stats->remote_source = false;
511 video_track_stats->detached = false;
512 video_track_stats->frame_width = static_cast<uint32_t>(
513 video_sender_info.send_frame_width);
514 video_track_stats->frame_height = static_cast<uint32_t>(
515 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 14:14:25516 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 13:08:34517 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 14:14:25518 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 15:35:03519 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 10:47:10520 return video_track_stats;
521}
522
523std::unique_ptr<RTCMediaStreamTrackStats>
524ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
525 int64_t timestamp_us,
526 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19527 const cricket::VideoReceiverInfo& video_receiver_info,
528 int attachment_id) {
hbos9e302742017-01-20 10:47:10529 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
530 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58531 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
532
533 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19534 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10535 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
536 video_track, video_track_stats.get());
537 video_track_stats->remote_source = true;
538 video_track_stats->detached = false;
539 if (video_receiver_info.frame_width > 0 &&
540 video_receiver_info.frame_height > 0) {
541 video_track_stats->frame_width = static_cast<uint32_t>(
542 video_receiver_info.frame_width);
543 video_track_stats->frame_height = static_cast<uint32_t>(
544 video_receiver_info.frame_height);
545 }
hbos42f6d2f2017-01-20 11:56:50546 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 15:39:09547 // TODO(hbos): When we support receiving simulcast, this should be the total
548 // number of frames correctly decoded, independent of which SSRC it was
549 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 13:08:34550 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 15:39:09551 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 15:21:55552 RTC_DCHECK_GE(video_receiver_info.frames_received,
553 video_receiver_info.frames_rendered);
554 video_track_stats->frames_dropped = video_receiver_info.frames_received -
555 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 15:45:42556 video_track_stats->freeze_count = video_receiver_info.freeze_count;
557 video_track_stats->pause_count = video_receiver_info.pause_count;
558 video_track_stats->total_freezes_duration =
559 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
560 rtc::kNumMillisecsPerSec;
561 video_track_stats->total_pauses_duration =
562 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
563 rtc::kNumMillisecsPerSec;
564 video_track_stats->total_frames_duration =
565 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
566 rtc::kNumMillisecsPerSec;
567 video_track_stats->sum_squared_frame_durations =
568 video_receiver_info.sum_squared_frame_durations;
569
hbos9e302742017-01-20 10:47:10570 return video_track_stats;
571}
572
Harald Alvestrand89061872018-01-02 13:08:34573void ProduceSenderMediaTrackStats(
574 int64_t timestamp_us,
575 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50576 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 13:08:34577 RTCStatsReport* report) {
578 // This function iterates over the senders to generate outgoing track stats.
579
580 // TODO(hbos): Return stats of detached tracks. We have to perform stats
581 // gathering at the time of detachment to get accurate stats and timestamps.
582 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 16:29:42583 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 13:08:34584 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
585 AudioTrackInterface* track =
586 static_cast<AudioTrackInterface*>(sender->track().get());
587 if (!track)
588 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16589 cricket::VoiceSenderInfo null_sender_info;
590 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
591 // TODO(hta): Checking on ssrc is not proper. There should be a way
592 // to see from a sender whether it's connected or not.
593 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 23:19:50594 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29595 // 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 23:19:50598 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16599 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29600 if (sender_info) {
601 voice_sender_info = sender_info;
602 } else {
603 RTC_LOG(LS_INFO)
604 << "RTCStatsCollector: No voice sender info for sender with ssrc "
605 << sender->ssrc();
606 }
Harald Alvestrandb8e12012018-01-23 14:28:16607 }
Harald Alvestrand89061872018-01-02 13:08:34608 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19609 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
610 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34611 report->AddStats(std::move(audio_track_stats));
612 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
613 VideoTrackInterface* track =
614 static_cast<VideoTrackInterface*>(sender->track().get());
615 if (!track)
616 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16617 cricket::VideoSenderInfo null_sender_info;
618 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
619 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 13:43:29620 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
621 // "none")
Steve Anton57858b32018-02-15 23:19:50622 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29623 // When pc.close is called, sender info is discarded, so
624 // we generate zeroes instead. Bug: It should be retained.
625 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50626 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16627 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29628 if (sender_info) {
629 video_sender_info = sender_info;
630 } else {
631 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
632 << sender->ssrc();
633 }
634 }
Harald Alvestrand89061872018-01-02 13:08:34635 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19636 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
637 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34638 report->AddStats(std::move(video_track_stats));
639 } else {
640 RTC_NOTREACHED();
641 }
642 }
643}
644
645void ProduceReceiverMediaTrackStats(
646 int64_t timestamp_us,
647 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50648 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 13:08:34649 RTCStatsReport* report) {
650 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 16:29:42651 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 13:08:34652 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
653 AudioTrackInterface* track =
654 static_cast<AudioTrackInterface*>(receiver->track().get());
655 const cricket::VoiceReceiverInfo* voice_receiver_info =
656 track_media_info_map.GetVoiceReceiverInfo(*track);
657 if (!voice_receiver_info) {
658 continue;
659 }
660 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
661 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:19662 timestamp_us, *track, *voice_receiver_info,
663 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34664 report->AddStats(std::move(audio_track_stats));
665 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
666 VideoTrackInterface* track =
667 static_cast<VideoTrackInterface*>(receiver->track().get());
668 const cricket::VideoReceiverInfo* video_receiver_info =
669 track_media_info_map.GetVideoReceiverInfo(*track);
670 if (!video_receiver_info) {
671 continue;
672 }
673 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
674 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:19675 timestamp_us, *track, *video_receiver_info,
676 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34677 report->AddStats(std::move(video_track_stats));
678 } else {
679 RTC_NOTREACHED();
680 }
681 }
682}
683
Henrik Boström5b3541f2018-03-19 12:52:56684rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
685 bool filter_by_sender_selector,
686 rtc::scoped_refptr<const RTCStatsReport> report,
687 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
688 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
689 std::vector<std::string> rtpstream_ids;
690 if (filter_by_sender_selector) {
691 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
692 if (sender_selector) {
693 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
694 // reference the sender stats.
695 // Because we do not implement sender stats, we look at outbound-rtp(s)
696 // that reference the track attachment stats for the sender instead.
697 std::string track_id =
698 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
699 kSender, sender_selector->AttachmentId());
700 for (const auto& stats : *report) {
701 if (stats.type() != RTCOutboundRTPStreamStats::kType)
702 continue;
703 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
704 if (outbound_rtp.track_id.is_defined() &&
705 *outbound_rtp.track_id == track_id) {
706 rtpstream_ids.push_back(outbound_rtp.id());
707 }
708 }
709 }
710 } else {
711 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
712 if (receiver_selector) {
713 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
714 // reference the receiver stats.
715 // Because we do not implement receiver stats, we look at inbound-rtp(s)
716 // that reference the track attachment stats for the receiver instead.
717 std::string track_id =
718 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
719 kReceiver, receiver_selector->AttachmentId());
720 for (const auto& stats : *report) {
721 if (stats.type() != RTCInboundRTPStreamStats::kType)
722 continue;
723 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
724 if (inbound_rtp.track_id.is_defined() &&
725 *inbound_rtp.track_id == track_id) {
726 rtpstream_ids.push_back(inbound_rtp.id());
727 }
728 }
729 }
730 }
731 if (rtpstream_ids.empty())
732 return RTCStatsReport::Create(report->timestamp_us());
733 return TakeReferencedStats(report->Copy(), rtpstream_ids);
734}
735
hboscc555c52016-10-18 19:48:31736} // namespace
737
Henrik Boström5b3541f2018-03-19 12:52:56738RTCStatsCollector::RequestInfo::RequestInfo(
739 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
740 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
741
742RTCStatsCollector::RequestInfo::RequestInfo(
743 rtc::scoped_refptr<RtpSenderInternal> selector,
744 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
745 : RequestInfo(FilterMode::kSenderSelector,
746 std::move(callback),
747 std::move(selector),
748 nullptr) {}
749
750RTCStatsCollector::RequestInfo::RequestInfo(
751 rtc::scoped_refptr<RtpReceiverInternal> selector,
752 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
753 : RequestInfo(FilterMode::kReceiverSelector,
754 std::move(callback),
755 nullptr,
756 std::move(selector)) {}
757
758RTCStatsCollector::RequestInfo::RequestInfo(
759 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
760 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
761 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
762 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
763 : filter_mode_(filter_mode),
764 callback_(std::move(callback)),
765 sender_selector_(std::move(sender_selector)),
766 receiver_selector_(std::move(receiver_selector)) {
767 RTC_DCHECK(callback_);
768 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
769}
770
hbosc82f2e12016-09-05 08:36:50771rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-24 00:38:46772 PeerConnectionInternal* pc,
773 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 08:36:50774 return rtc::scoped_refptr<RTCStatsCollector>(
775 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
776}
777
Steve Anton2d8609c2018-01-24 00:38:46778RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 08:36:50779 int64_t cache_lifetime_us)
hbosd565b732016-08-30 21:04:35780 : pc_(pc),
Steve Anton978b8762017-09-29 19:15:02781 signaling_thread_(pc->signaling_thread()),
782 worker_thread_(pc->worker_thread()),
783 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 08:36:50784 num_pending_partial_reports_(0),
785 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 08:49:31786 network_report_event_(true /* manual_reset */,
787 true /* initially_signaled */),
hbos0e6758d2016-08-31 14:57:36788 cache_timestamp_us_(0),
789 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 21:04:35790 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 08:36:50791 RTC_DCHECK(signaling_thread_);
792 RTC_DCHECK(worker_thread_);
793 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 14:57:36794 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-24 00:38:46795 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 09:41:09796 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 21:04:35797}
798
hbosb78306a2016-12-19 13:06:57799RTCStatsCollector::~RTCStatsCollector() {
800 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
801}
802
hbosc82f2e12016-09-05 08:36:50803void RTCStatsCollector::GetStatsReport(
804 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 12:52:56805 GetStatsReportInternal(RequestInfo(std::move(callback)));
806}
807
808void RTCStatsCollector::GetStatsReport(
809 rtc::scoped_refptr<RtpSenderInternal> selector,
810 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
811 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
812}
813
814void RTCStatsCollector::GetStatsReport(
815 rtc::scoped_refptr<RtpReceiverInternal> selector,
816 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
817 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
818}
819
820void RTCStatsCollector::GetStatsReportInternal(
821 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 08:36:50822 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 12:52:56823 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 08:36:50824
hbos0e6758d2016-08-31 14:57:36825 // "Now" using a monotonically increasing timer.
826 int64_t cache_now_us = rtc::TimeMicros();
827 if (cached_report_ &&
828 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 17:53:47829 // We have a fresh cached report to deliver. Deliver asynchronously, since
830 // the caller may not be expecting a synchronous callback, and it avoids
831 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 12:52:56832 std::vector<RequestInfo> requests;
833 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 08:49:31834 signaling_thread_->PostTask(
835 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
836 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 08:36:50837 } else if (!num_pending_partial_reports_) {
838 // Only start gathering stats if we're not already gathering stats. In the
839 // case of already gathering stats, |callback_| will be invoked when there
840 // are no more pending partial reports.
841
842 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
843 // UTC), in microseconds. The system clock could be modified and is not
844 // necessarily monotonically increasing.
nissecdf37a92016-09-14 06:41:47845 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 08:36:50846
hbosf415f8a2017-01-02 12:28:51847 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 08:36:50848 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 12:58:02849
Steve Anton57858b32018-02-15 23:19:50850 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 14:16:44851 // |ProducePartialResultsOnNetworkThread| and
852 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 23:19:50853 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 22:18:41854 // Prepare |transport_names_| for use in
855 // |ProducePartialResultsOnNetworkThread|.
856 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 18:34:40857
stefanf79ade12017-06-02 13:44:03858 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
859 // thread.
860 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
861 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 19:15:02862 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 13:44:03863
Henrik Boström40b030e2019-02-28 08:49:31864 // Don't touch |network_report_| on the signaling thread until
865 // ProducePartialResultsOnNetworkThread() has signaled the
866 // |network_report_event_|.
867 network_report_event_.Reset();
868 network_thread_->PostTask(
869 RTC_FROM_HERE,
hbosc82f2e12016-09-05 08:36:50870 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 08:49:31871 this, timestamp_us));
hbosf415f8a2017-01-02 12:28:51872 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 14:57:36873 }
hbosd565b732016-08-30 21:04:35874}
875
876void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 08:36:50877 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 21:04:35878 cached_report_ = nullptr;
879}
880
hbosb78306a2016-12-19 13:06:57881void RTCStatsCollector::WaitForPendingRequest() {
882 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 08:49:31883 // If a request is pending, blocks until the |network_report_event_| is
884 // signaled and then delivers the result. Otherwise this is a NO-OP.
885 MergeNetworkReport_s();
hbosb78306a2016-12-19 13:06:57886}
887
hbosc82f2e12016-09-05 08:36:50888void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
889 int64_t timestamp_us) {
890 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 08:49:31891 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:50892
Henrik Boström40b030e2019-02-28 08:49:31893 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
894 partial_report_.get());
hbosc82f2e12016-09-05 08:36:50895
Henrik Boström40b030e2019-02-28 08:49:31896 // ProducePartialResultsOnSignalingThread() is running synchronously on the
897 // signaling thread, so it is always the first partial result delivered on the
898 // signaling thread. The request is not complete until MergeNetworkReport_s()
899 // happens; we don't have to do anything here.
900 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
901 --num_pending_partial_reports_;
902}
903
904void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
905 int64_t timestamp_us,
906 RTCStatsReport* partial_report) {
907 RTC_DCHECK(signaling_thread_->IsCurrent());
908 ProduceDataChannelStats_s(timestamp_us, partial_report);
909 ProduceMediaStreamStats_s(timestamp_us, partial_report);
910 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
911 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 08:36:50912}
913
hbosc82f2e12016-09-05 08:36:50914void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
915 int64_t timestamp_us) {
916 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 08:49:31917 // Touching |network_report_| on this thread is safe by this method because
918 // |network_report_event_| is reset before this method is invoked.
919 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:50920
Steve Anton5dfde182018-02-06 18:34:40921 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 22:18:41922 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 18:34:40923 std::map<std::string, CertificateStatsPair> transport_cert_stats =
924 PrepareTransportCertificateStats_n(transport_stats_by_name);
925
Henrik Boström40b030e2019-02-28 08:49:31926 ProducePartialResultsOnNetworkThreadImpl(
927 timestamp_us, transport_stats_by_name, transport_cert_stats,
928 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40929
Henrik Boström40b030e2019-02-28 08:49:31930 // Signal that it is now safe to touch |network_report_| on the signaling
931 // thread, and post a task to merge it into the final results.
932 network_report_event_.Set();
933 signaling_thread_->PostTask(
934 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 09:23:08935}
936
Henrik Boström40b030e2019-02-28 08:49:31937void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
938 int64_t timestamp_us,
939 const std::map<std::string, cricket::TransportStats>&
940 transport_stats_by_name,
941 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
942 RTCStatsReport* partial_report) {
943 RTC_DCHECK(network_thread_->IsCurrent());
944 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
945 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
946 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
947 call_stats_, partial_report);
948 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
949 partial_report);
950 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
951 transport_cert_stats, partial_report);
952}
953
954void RTCStatsCollector::MergeNetworkReport_s() {
955 RTC_DCHECK(signaling_thread_->IsCurrent());
956 // The |network_report_event_| must be signaled for it to be safe to touch
957 // |network_report_|. This is normally not blocking, but if
958 // WaitForPendingRequest() is called while a request is pending, we might have
959 // to wait until the network thread is done touching |network_report_|.
960 network_report_event_.Wait(rtc::Event::kForever);
961 if (!network_report_) {
962 // Normally, MergeNetworkReport_s() is executed because it is posted from
963 // the network thread. But if WaitForPendingRequest() is called while a
964 // request is pending, an early call to MergeNetworkReport_s() is made,
965 // merging the report and setting |network_report_| to null. If so, when the
966 // previously posted MergeNetworkReport_s() is later executed, the report is
967 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 08:36:50968 return;
969 }
Mirko Bonadeica890ee2019-02-15 21:10:40970 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 08:49:31971 RTC_DCHECK(partial_report_);
972 partial_report_->TakeMembersFrom(network_report_);
973 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40974 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 08:49:31975 // |network_report_| is currently the only partial report collected
976 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
977 // ready to deliver the result.
978 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
979 cache_timestamp_us_ = partial_report_timestamp_us_;
980 cached_report_ = partial_report_;
981 partial_report_ = nullptr;
982 transceiver_stats_infos_.clear();
983 // Trace WebRTC Stats when getStats is called on Javascript.
984 // This allows access to WebRTC stats from trace logs. To enable them,
985 // select the "webrtc_stats" category when recording traces.
986 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
987 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40988
Henrik Boström40b030e2019-02-28 08:49:31989 // Deliver report and clear |requests_|.
990 std::vector<RequestInfo> requests;
991 requests.swap(requests_);
992 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 08:36:50993}
994
Taylor Brandstetter25e022f2018-03-08 17:53:47995void RTCStatsCollector::DeliverCachedReport(
996 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 12:52:56997 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 08:36:50998 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 12:52:56999 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 17:53:471000 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 17:42:251001
Henrik Boström5b3541f2018-03-19 12:52:561002 for (const RequestInfo& request : requests) {
1003 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1004 request.callback()->OnStatsDelivered(cached_report);
1005 } else {
1006 bool filter_by_sender_selector;
1007 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1008 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1009 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1010 filter_by_sender_selector = true;
1011 sender_selector = request.sender_selector();
1012 } else {
1013 RTC_DCHECK(request.filter_mode() ==
1014 RequestInfo::FilterMode::kReceiverSelector);
1015 filter_by_sender_selector = false;
1016 receiver_selector = request.receiver_selector();
1017 }
1018 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1019 filter_by_sender_selector, cached_report, sender_selector,
1020 receiver_selector));
1021 }
hbosc82f2e12016-09-05 08:36:501022 }
hbosd565b732016-08-30 21:04:351023}
1024
hbosdf6075a2016-12-19 12:58:021025void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 11:00:051026 int64_t timestamp_us,
1027 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce02016-10-03 21:16:561028 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021029 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 12:14:531030 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1031 if (transport_cert_stats_pair.second.local) {
1032 ProduceCertificateStatsFromSSLCertificateStats(
1033 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce02016-10-03 21:16:561034 }
hbos02ba2112016-10-28 12:14:531035 if (transport_cert_stats_pair.second.remote) {
1036 ProduceCertificateStatsFromSSLCertificateStats(
1037 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce02016-10-03 21:16:561038 }
1039 }
1040}
1041
hbosdf6075a2016-12-19 12:58:021042void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 23:19:501043 int64_t timestamp_us,
1044 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 10:32:061045 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021046 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 23:19:501047 for (const auto& stats : transceiver_stats_infos) {
1048 if (!stats.mid) {
1049 continue;
hbos0adb8282016-11-23 10:32:061050 }
Steve Anton57858b32018-02-15 23:19:501051 const cricket::VoiceMediaInfo* voice_media_info =
1052 stats.track_media_info_map->voice_media_info();
1053 const cricket::VideoMediaInfo* video_media_info =
1054 stats.track_media_info_map->video_media_info();
1055 // Audio
1056 if (voice_media_info) {
1057 // Inbound
1058 for (const auto& pair : voice_media_info->receive_codecs) {
1059 report->AddStats(CodecStatsFromRtpCodecParameters(
1060 timestamp_us, *stats.mid, true, pair.second));
1061 }
1062 // Outbound
1063 for (const auto& pair : voice_media_info->send_codecs) {
1064 report->AddStats(CodecStatsFromRtpCodecParameters(
1065 timestamp_us, *stats.mid, false, pair.second));
1066 }
Guido Urdanetaee2388f2018-02-15 16:36:191067 }
Steve Anton57858b32018-02-15 23:19:501068 // Video
1069 if (video_media_info) {
1070 // Inbound
1071 for (const auto& pair : video_media_info->receive_codecs) {
1072 report->AddStats(CodecStatsFromRtpCodecParameters(
1073 timestamp_us, *stats.mid, true, pair.second));
1074 }
1075 // Outbound
1076 for (const auto& pair : video_media_info->send_codecs) {
1077 report->AddStats(CodecStatsFromRtpCodecParameters(
1078 timestamp_us, *stats.mid, false, pair.second));
1079 }
hbos0adb8282016-11-23 10:32:061080 }
1081 }
1082}
1083
hboscc555c52016-10-18 19:48:311084void RTCStatsCollector::ProduceDataChannelStats_s(
1085 int64_t timestamp_us, RTCStatsReport* report) const {
1086 RTC_DCHECK(signaling_thread_->IsCurrent());
1087 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1088 pc_->sctp_data_channels()) {
1089 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1090 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 09:59:481091 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 19:48:311092 timestamp_us));
1093 data_channel_stats->label = data_channel->label();
1094 data_channel_stats->protocol = data_channel->protocol();
1095 data_channel_stats->datachannelid = data_channel->id();
1096 data_channel_stats->state =
1097 DataStateToRTCDataChannelState(data_channel->state());
1098 data_channel_stats->messages_sent = data_channel->messages_sent();
1099 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1100 data_channel_stats->messages_received = data_channel->messages_received();
1101 data_channel_stats->bytes_received = data_channel->bytes_received();
1102 report->AddStats(std::move(data_channel_stats));
1103 }
1104}
1105
hbosdf6075a2016-12-19 12:58:021106void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 13:44:031107 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 18:34:401108 const std::map<std::string, cricket::TransportStats>&
1109 transport_stats_by_name,
stefanf79ade12017-06-02 13:44:031110 const Call::Stats& call_stats,
1111 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021112 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 18:34:401113 for (const auto& entry : transport_stats_by_name) {
1114 const std::string& transport_name = entry.first;
1115 const cricket::TransportStats& transport_stats = entry.second;
1116 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 09:50:141117 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401118 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 21:54:491119 for (const cricket::ConnectionInfo& info :
1120 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 21:54:491121 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 11:00:051122 new RTCIceCandidatePairStats(
1123 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1124 timestamp_us));
hbosc47a0c32016-10-11 21:54:491125
hbos0583b282016-11-30 09:50:141126 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 09:18:471127 // TODO(hbos): There could be other candidates that are not paired with
1128 // anything. We don't have a complete list. Local candidates come from
1129 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 13:08:341130 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 12:14:531131 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311132 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 12:14:531133 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311134 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 16:08:181135 candidate_pair_stats->state =
1136 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1137 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 09:38:081138 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 21:54:491139 // TODO(hbos): This writable is different than the spec. It goes to
1140 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 13:08:341141 // https://crbug.com/633550
hbosc47a0c32016-10-11 21:54:491142 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 21:54:491143 candidate_pair_stats->bytes_sent =
1144 static_cast<uint64_t>(info.sent_total_bytes);
1145 candidate_pair_stats->bytes_received =
1146 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 14:34:471147 candidate_pair_stats->total_round_trip_time =
1148 static_cast<double>(info.total_round_trip_time_ms) /
1149 rtc::kNumMillisecsPerSec;
1150 if (info.current_round_trip_time_ms) {
1151 candidate_pair_stats->current_round_trip_time =
1152 static_cast<double>(*info.current_round_trip_time_ms) /
1153 rtc::kNumMillisecsPerSec;
1154 }
stefanf79ade12017-06-02 13:44:031155 if (info.best_connection) {
hbos338f78a2017-02-07 14:41:211156 // The bandwidth estimations we have are for the selected candidate
1157 // pair ("info.best_connection").
stefanf79ade12017-06-02 13:44:031158 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1159 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1160 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211161 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 13:44:031162 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211163 }
stefanf79ade12017-06-02 13:44:031164 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211165 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 13:44:031166 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211167 }
1168 }
hbosd82f5122016-12-09 12:12:391169 candidate_pair_stats->requests_received =
1170 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 09:22:531171 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1172 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491173 candidate_pair_stats->responses_received =
1174 static_cast<uint64_t>(info.recv_ping_responses);
1175 candidate_pair_stats->responses_sent =
1176 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 09:22:531177 RTC_DCHECK_GE(info.sent_ping_requests_total,
1178 info.sent_ping_requests_before_first_response);
1179 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1180 info.sent_ping_requests_total -
1181 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491182
1183 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 09:18:471184 }
1185 }
1186 }
1187}
1188
Steve Anton57858b32018-02-15 23:19:501189void RTCStatsCollector::ProduceMediaStreamStats_s(
1190 int64_t timestamp_us,
1191 RTCStatsReport* report) const {
hbos09bc1282016-11-08 14:29:221192 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 23:19:501193
1194 std::map<std::string, std::vector<std::string>> track_ids;
1195
1196 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 16:29:421197 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501198 std::string track_id =
1199 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1200 kSender, sender->internal()->AttachmentId());
1201 for (auto& stream_id : sender->stream_ids()) {
1202 track_ids[stream_id].push_back(track_id);
1203 }
1204 }
Mirko Bonadei739baf02019-01-27 16:29:421205 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501206 std::string track_id =
1207 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1208 kReceiver, receiver->internal()->AttachmentId());
1209 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 23:05:281210 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 23:19:501211 }
1212 }
1213 }
1214
1215 // Build stats for each stream ID known.
1216 for (auto& it : track_ids) {
1217 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1218 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1219 stream_stats->stream_identifier = it.first;
1220 stream_stats->track_ids = it.second;
1221 report->AddStats(std::move(stream_stats));
1222 }
1223}
1224
1225void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1226 int64_t timestamp_us,
1227 RTCStatsReport* report) const {
1228 RTC_DCHECK(signaling_thread_->IsCurrent());
1229 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1230 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 16:29:421231 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501232 senders.push_back(sender->internal());
1233 }
1234 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1235 senders, report);
1236
1237 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 16:29:421238 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501239 receivers.push_back(receiver->internal());
1240 }
1241 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1242 receivers, report);
1243 }
hbos09bc1282016-11-08 14:29:221244}
1245
hbos6ab97ce02016-10-03 21:16:561246void RTCStatsCollector::ProducePeerConnectionStats_s(
1247 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 08:36:501248 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 21:04:351249 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 14:57:361250 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 09:41:091251 stats->data_channels_opened = internal_record_.data_channels_opened;
1252 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce02016-10-03 21:16:561253 report->AddStats(std::move(stats));
hbosd565b732016-08-30 21:04:351254}
1255
hbosdf6075a2016-12-19 12:58:021256void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 19:44:481257 int64_t timestamp_us,
Steve Anton57858b32018-02-15 23:19:501258 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 14:16:441259 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021260 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 08:50:461261
Steve Anton57858b32018-02-15 23:19:501262 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1263 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1264 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1265 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1266 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1267 } else {
1268 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:191269 }
Steve Anton56bae8d2018-02-15 00:07:421270 }
Steve Anton57858b32018-02-15 23:19:501271}
1272
1273void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1274 int64_t timestamp_us,
1275 const RtpTransceiverStatsInfo& stats,
1276 RTCStatsReport* report) const {
1277 if (!stats.mid || !stats.transport_name) {
1278 return;
1279 }
1280 RTC_DCHECK(stats.track_media_info_map);
1281 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1282 RTC_DCHECK(track_media_info_map.voice_media_info());
1283 std::string mid = *stats.mid;
1284 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1285 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1286 // Inbound
1287 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1288 track_media_info_map.voice_media_info()->receivers) {
1289 if (!voice_receiver_info.connected())
1290 continue;
Karl Wiberg918f50c2018-07-05 09:40:331291 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501292 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1293 timestamp_us);
1294 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1295 inbound_audio.get());
1296 // TODO(hta): This lookup should look for the sender, not the track.
1297 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1298 track_media_info_map.GetAudioTrack(voice_receiver_info);
1299 if (audio_track) {
1300 inbound_audio->track_id =
1301 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1302 kReceiver,
1303 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 08:50:461304 }
Steve Anton57858b32018-02-15 23:19:501305 inbound_audio->transport_id = transport_id;
1306 report->AddStats(std::move(inbound_audio));
1307 }
1308 // Outbound
1309 for (const cricket::VoiceSenderInfo& voice_sender_info :
1310 track_media_info_map.voice_media_info()->senders) {
1311 if (!voice_sender_info.connected())
1312 continue;
Karl Wiberg918f50c2018-07-05 09:40:331313 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501314 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1315 timestamp_us);
1316 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1317 outbound_audio.get());
1318 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1319 track_media_info_map.GetAudioTrack(voice_sender_info);
1320 if (audio_track) {
1321 outbound_audio->track_id =
1322 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1323 kSender,
1324 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
Steve Anton56bae8d2018-02-15 00:07:421325 }
Steve Anton57858b32018-02-15 23:19:501326 outbound_audio->transport_id = transport_id;
1327 report->AddStats(std::move(outbound_audio));
1328 }
1329}
1330
1331void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1332 int64_t timestamp_us,
1333 const RtpTransceiverStatsInfo& stats,
1334 RTCStatsReport* report) const {
1335 if (!stats.mid || !stats.transport_name) {
1336 return;
1337 }
1338 RTC_DCHECK(stats.track_media_info_map);
1339 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1340 RTC_DCHECK(track_media_info_map.video_media_info());
1341 std::string mid = *stats.mid;
1342 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1343 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1344 // Inbound
1345 for (const cricket::VideoReceiverInfo& video_receiver_info :
1346 track_media_info_map.video_media_info()->receivers) {
1347 if (!video_receiver_info.connected())
1348 continue;
Karl Wiberg918f50c2018-07-05 09:40:331349 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501350 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1351 timestamp_us);
1352 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1353 inbound_video.get());
1354 rtc::scoped_refptr<VideoTrackInterface> video_track =
1355 track_media_info_map.GetVideoTrack(video_receiver_info);
1356 if (video_track) {
1357 inbound_video->track_id =
1358 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1359 kReceiver,
1360 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1361 }
1362 inbound_video->transport_id = transport_id;
1363 report->AddStats(std::move(inbound_video));
1364 }
1365 // Outbound
1366 for (const cricket::VideoSenderInfo& video_sender_info :
1367 track_media_info_map.video_media_info()->senders) {
1368 if (!video_sender_info.connected())
1369 continue;
Karl Wiberg918f50c2018-07-05 09:40:331370 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501371 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1372 timestamp_us);
1373 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1374 outbound_video.get());
1375 rtc::scoped_refptr<VideoTrackInterface> video_track =
1376 track_media_info_map.GetVideoTrack(video_sender_info);
1377 if (video_track) {
1378 outbound_video->track_id =
1379 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1380 kSender,
1381 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1382 }
1383 outbound_video->transport_id = transport_id;
1384 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 08:50:461385 }
1386}
1387
hbosdf6075a2016-12-19 12:58:021388void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 18:34:401389 int64_t timestamp_us,
1390 const std::map<std::string, cricket::TransportStats>&
1391 transport_stats_by_name,
hbos2fa7c672016-10-24 11:00:051392 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1393 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021394 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 18:34:401395 for (const auto& entry : transport_stats_by_name) {
1396 const std::string& transport_name = entry.first;
1397 const cricket::TransportStats& transport_stats = entry.second;
1398
hbos2fa7c672016-10-24 11:00:051399 // Get reference to RTCP channel, if it exists.
1400 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 18:34:401401 for (const cricket::TransportChannelStats& channel_stats :
1402 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 11:00:051403 if (channel_stats.component ==
1404 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1405 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401406 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 11:00:051407 break;
1408 }
1409 }
1410
1411 // Get reference to local and remote certificates of this transport, if they
1412 // exist.
Steve Anton5dfde182018-02-06 18:34:401413 const auto& certificate_stats_it =
1414 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 11:00:051415 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1416 std::string local_certificate_id;
1417 if (certificate_stats_it->second.local) {
1418 local_certificate_id = RTCCertificateIDFromFingerprint(
1419 certificate_stats_it->second.local->fingerprint);
1420 }
1421 std::string remote_certificate_id;
1422 if (certificate_stats_it->second.remote) {
1423 remote_certificate_id = RTCCertificateIDFromFingerprint(
1424 certificate_stats_it->second.remote->fingerprint);
1425 }
1426
1427 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 18:34:401428 for (const cricket::TransportChannelStats& channel_stats :
1429 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 11:00:051430 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 18:34:401431 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1432 transport_name, channel_stats.component),
1433 timestamp_us));
hbos2fa7c672016-10-24 11:00:051434 transport_stats->bytes_sent = 0;
1435 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 15:38:021436 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1437 channel_stats.dtls_state);
hbos2fa7c672016-10-24 11:00:051438 for (const cricket::ConnectionInfo& info :
1439 channel_stats.connection_infos) {
1440 *transport_stats->bytes_sent += info.sent_total_bytes;
1441 *transport_stats->bytes_received += info.recv_total_bytes;
1442 if (info.best_connection) {
hbos2fa7c672016-10-24 11:00:051443 transport_stats->selected_candidate_pair_id =
1444 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1445 }
1446 }
1447 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1448 !rtcp_transport_stats_id.empty()) {
1449 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1450 }
1451 if (!local_certificate_id.empty())
1452 transport_stats->local_certificate_id = local_certificate_id;
1453 if (!remote_certificate_id.empty())
1454 transport_stats->remote_certificate_id = remote_certificate_id;
1455 report->AddStats(std::move(transport_stats));
1456 }
1457 }
1458}
1459
1460std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 12:58:021461RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 18:34:401462 const std::map<std::string, cricket::TransportStats>&
1463 transport_stats_by_name) const {
hbosdf6075a2016-12-19 12:58:021464 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 11:00:051465 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 18:34:401466 for (const auto& entry : transport_stats_by_name) {
1467 const std::string& transport_name = entry.first;
1468
hbos2fa7c672016-10-24 11:00:051469 CertificateStatsPair certificate_stats_pair;
1470 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 18:34:401471 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 11:00:051472 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 08:16:261473 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 11:00:051474 }
Steve Anton5dfde182018-02-06 18:34:401475
Taylor Brandstetterc3928662018-02-23 21:04:511476 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1477 pc_->GetRemoteSSLCertChain(transport_name);
1478 if (remote_cert_chain) {
1479 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 11:00:051480 }
Steve Anton5dfde182018-02-06 18:34:401481
hbos2fa7c672016-10-24 11:00:051482 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 18:34:401483 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 11:00:051484 }
1485 return transport_cert_stats;
1486}
1487
Steve Anton57858b32018-02-15 23:19:501488std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1489RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1490 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-15 00:07:421491
Steve Anton57858b32018-02-15 23:19:501492 // These are used to invoke GetStats for all the media channels together in
1493 // one worker thread hop.
1494 std::map<cricket::VoiceMediaChannel*,
1495 std::unique_ptr<cricket::VoiceMediaInfo>>
1496 voice_stats;
1497 std::map<cricket::VideoMediaChannel*,
1498 std::unique_ptr<cricket::VideoMediaInfo>>
1499 video_stats;
1500
Mirko Bonadei739baf02019-01-27 16:29:421501 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 23:19:501502 cricket::MediaType media_type = transceiver->media_type();
1503
1504 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1505 // stats have been fetched on the worker thread.
1506 transceiver_stats_infos.emplace_back();
1507 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1508 stats.transceiver = transceiver->internal();
1509 stats.media_type = media_type;
1510
Amit Hilbuchdd9390c2018-11-14 00:26:051511 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 23:19:501512 if (!channel) {
1513 // The remaining fields require a BaseChannel.
1514 continue;
1515 }
1516
1517 stats.mid = channel->content_name();
1518 stats.transport_name = channel->transport_name();
1519
1520 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1521 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1522 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1523 voice_stats.end());
1524 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 09:40:331525 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 23:19:501526 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1527 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1528 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1529 video_stats.end());
1530 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 09:40:331531 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 23:19:501532 } else {
1533 RTC_NOTREACHED();
1534 }
Guido Urdanetaee2388f2018-02-15 16:36:191535 }
Steve Anton57858b32018-02-15 23:19:501536
1537 // Call GetStats for all media channels together on the worker thread in one
1538 // hop.
1539 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1540 for (const auto& entry : voice_stats) {
1541 if (!entry.first->GetStats(entry.second.get())) {
1542 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1543 }
1544 }
1545 for (const auto& entry : video_stats) {
1546 if (!entry.first->GetStats(entry.second.get())) {
1547 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1548 }
1549 }
1550 });
1551
1552 // Create the TrackMediaInfoMap for each transceiver stats object.
1553 for (auto& stats : transceiver_stats_infos) {
1554 auto transceiver = stats.transceiver;
1555 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1556 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1557 if (transceiver->channel()) {
1558 cricket::MediaType media_type = transceiver->media_type();
1559 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1560 auto* voice_channel =
1561 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1562 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1563 voice_media_info =
1564 std::move(voice_stats[voice_channel->media_channel()]);
1565 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1566 auto* video_channel =
1567 static_cast<cricket::VideoChannel*>(transceiver->channel());
1568 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1569 video_media_info =
1570 std::move(video_stats[video_channel->media_channel()]);
1571 }
1572 }
1573 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 16:29:421574 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501575 senders.push_back(sender->internal());
1576 }
1577 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 16:29:421578 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501579 receivers.push_back(receiver->internal());
1580 }
Karl Wiberg918f50c2018-07-05 09:40:331581 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 23:19:501582 std::move(voice_media_info), std::move(video_media_info), senders,
1583 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:191584 }
Steve Anton57858b32018-02-15 23:19:501585
1586 return transceiver_stats_infos;
hbos0adb8282016-11-23 10:32:061587}
1588
Steve Anton7eca0932018-03-30 22:18:411589std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1590 std::set<std::string> transport_names;
1591 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1592 if (transceiver->internal()->channel()) {
1593 transport_names.insert(
1594 transceiver->internal()->channel()->transport_name());
1595 }
1596 }
1597 if (pc_->rtp_data_channel()) {
1598 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1599 }
1600 if (pc_->sctp_transport_name()) {
1601 transport_names.insert(*pc_->sctp_transport_name());
1602 }
1603 return transport_names;
1604}
1605
hbos82ebe022016-11-14 09:41:091606void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1607 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1608 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1609}
1610
1611void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1612 RTC_DCHECK(signaling_thread_->IsCurrent());
1613 bool result = internal_record_.opened_data_channels.insert(
1614 reinterpret_cast<uintptr_t>(channel)).second;
1615 ++internal_record_.data_channels_opened;
1616 RTC_DCHECK(result);
1617}
1618
1619void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1620 RTC_DCHECK(signaling_thread_->IsCurrent());
1621 // Only channels that have been fully opened (and have increased the
1622 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 10:14:141623 if (internal_record_.opened_data_channels.erase(
1624 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 09:41:091625 ++internal_record_.data_channels_closed;
1626 }
1627}
1628
hboscc555c52016-10-18 19:48:311629const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1630 const std::string& type) {
1631 return CandidateTypeToRTCIceCandidateType(type);
1632}
1633
1634const char* DataStateToRTCDataChannelStateForTesting(
1635 DataChannelInterface::DataState state) {
1636 return DataStateToRTCDataChannelState(state);
1637}
1638
hbosd565b732016-08-30 21:04:351639} // namespace webrtc