blob: 9f82abe663dd64b61845eba9b2d1ed0f56bf1094 [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"
Henrik Boström2e069262019-04-09 13:59:31 +020022#include "api/video/video_content_type.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "media/base/media_channel.h"
24#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "pc/peer_connection.h"
27#include "pc/rtc_stats_traversal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
Jonas Olsson43568dd2018-06-11 16:25:54 +020029#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070032
33namespace webrtc {
34
hboscc555c52016-10-18 12:48:31 -070035namespace {
36
Henrik Boström646fda02019-05-22 15:49:42 +020037// TODO(https://crbug.com/webrtc/10656): Consider making IDs less predictable.
hbos2fa7c672016-10-24 04:00:05 -070038std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
39 return "RTCCertificate_" + fingerprint;
40}
41
Steve Anton57858b32018-02-15 15:19:50 -080042std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
43 bool inbound,
44 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020045 char buf[1024];
46 rtc::SimpleStringBuilder sb(buf);
47 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
48 << payload_type;
49 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080050}
51
hbos2fa7c672016-10-24 04:00:05 -070052std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
53 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020054 char buf[4096];
55 rtc::SimpleStringBuilder sb(buf);
56 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
57 << info.remote_candidate.id();
58 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070059}
60
Harald Alvestranda3dab842018-01-14 09:18:58 +010061const char kSender[] = "sender";
62const char kReceiver[] = "receiver";
63
64std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
65 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010066 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020067 char buf[1024];
68 rtc::SimpleStringBuilder sb(buf);
69 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
70 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080071}
72
hbos2fa7c672016-10-24 04:00:05 -070073std::string RTCTransportStatsIDFromTransportChannel(
74 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020075 char buf[1024];
76 rtc::SimpleStringBuilder sb(buf);
77 sb << "RTCTransport_" << transport_name << "_" << channel_component;
78 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070079}
80
hboseeafe942016-11-01 03:00:17 -070081std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020082 char buf[1024];
83 rtc::SimpleStringBuilder sb(buf);
84 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
85 return sb.str();
hboseeafe942016-11-01 03:00:17 -070086}
87
hbos6ded1902016-11-01 01:50:46 -070088std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020089 char buf[1024];
90 rtc::SimpleStringBuilder sb(buf);
91 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
92 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070093}
94
Henrik Boström646fda02019-05-22 15:49:42 +020095std::string RTCMediaSourceStatsIDFromKindAndAttachment(
96 cricket::MediaType media_type,
97 int attachment_id) {
98 char buf[1024];
99 rtc::SimpleStringBuilder sb(buf);
100 sb << "RTC" << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
101 << "Source_" << attachment_id;
102 return sb.str();
103}
104
hbosab9f6e42016-10-07 02:18:47 -0700105const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
106 if (type == cricket::LOCAL_PORT_TYPE)
107 return RTCIceCandidateType::kHost;
108 if (type == cricket::STUN_PORT_TYPE)
109 return RTCIceCandidateType::kSrflx;
110 if (type == cricket::PRFLX_PORT_TYPE)
111 return RTCIceCandidateType::kPrflx;
112 if (type == cricket::RELAY_PORT_TYPE)
113 return RTCIceCandidateType::kRelay;
114 RTC_NOTREACHED();
115 return nullptr;
116}
117
hboscc555c52016-10-18 12:48:31 -0700118const char* DataStateToRTCDataChannelState(
119 DataChannelInterface::DataState state) {
120 switch (state) {
121 case DataChannelInterface::kConnecting:
122 return RTCDataChannelState::kConnecting;
123 case DataChannelInterface::kOpen:
124 return RTCDataChannelState::kOpen;
125 case DataChannelInterface::kClosing:
126 return RTCDataChannelState::kClosing;
127 case DataChannelInterface::kClosed:
128 return RTCDataChannelState::kClosed;
129 default:
130 RTC_NOTREACHED();
131 return nullptr;
132 }
133}
134
hbos06495bc2017-01-02 08:08:18 -0800135const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
136 cricket::IceCandidatePairState state) {
137 switch (state) {
138 case cricket::IceCandidatePairState::WAITING:
139 return RTCStatsIceCandidatePairState::kWaiting;
140 case cricket::IceCandidatePairState::IN_PROGRESS:
141 return RTCStatsIceCandidatePairState::kInProgress;
142 case cricket::IceCandidatePairState::SUCCEEDED:
143 return RTCStatsIceCandidatePairState::kSucceeded;
144 case cricket::IceCandidatePairState::FAILED:
145 return RTCStatsIceCandidatePairState::kFailed;
146 default:
147 RTC_NOTREACHED();
148 return nullptr;
149 }
150}
151
hbos7064d592017-01-16 07:38:02 -0800152const char* DtlsTransportStateToRTCDtlsTransportState(
153 cricket::DtlsTransportState state) {
154 switch (state) {
155 case cricket::DTLS_TRANSPORT_NEW:
156 return RTCDtlsTransportState::kNew;
157 case cricket::DTLS_TRANSPORT_CONNECTING:
158 return RTCDtlsTransportState::kConnecting;
159 case cricket::DTLS_TRANSPORT_CONNECTED:
160 return RTCDtlsTransportState::kConnected;
161 case cricket::DTLS_TRANSPORT_CLOSED:
162 return RTCDtlsTransportState::kClosed;
163 case cricket::DTLS_TRANSPORT_FAILED:
164 return RTCDtlsTransportState::kFailed;
165 default:
166 RTC_NOTREACHED();
167 return nullptr;
168 }
169}
170
Gary Liu37e489c2017-11-21 10:49:36 -0800171const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
172 switch (type) {
173 case rtc::ADAPTER_TYPE_CELLULAR:
174 return RTCNetworkType::kCellular;
175 case rtc::ADAPTER_TYPE_ETHERNET:
176 return RTCNetworkType::kEthernet;
177 case rtc::ADAPTER_TYPE_WIFI:
178 return RTCNetworkType::kWifi;
179 case rtc::ADAPTER_TYPE_VPN:
180 return RTCNetworkType::kVpn;
181 case rtc::ADAPTER_TYPE_UNKNOWN:
182 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700183 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800184 return RTCNetworkType::kUnknown;
185 }
186 RTC_NOTREACHED();
187 return nullptr;
188}
189
hbos9e302742017-01-20 02:47:10 -0800190double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
191 RTC_DCHECK_GE(audio_level, 0);
192 RTC_DCHECK_LE(audio_level, 32767);
193 return audio_level / 32767.0;
194}
195
hbos0adb8282016-11-23 02:32:06 -0800196std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800197 uint64_t timestamp_us,
198 const std::string& mid,
199 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800200 const RtpCodecParameters& codec_params) {
201 RTC_DCHECK_GE(codec_params.payload_type, 0);
202 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800203 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800204 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
205 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800206 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800207 timestamp_us));
208 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800209 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800210 if (codec_params.clock_rate) {
211 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
212 }
hbos0adb8282016-11-23 02:32:06 -0800213 return codec_stats;
214}
215
hbos09bc1282016-11-08 06:29:22 -0800216void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
217 const MediaStreamTrackInterface& track,
218 RTCMediaStreamTrackStats* track_stats) {
219 track_stats->track_identifier = track.id();
220 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
221}
222
hbos820f5782016-11-22 03:16:50 -0800223// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700224void SetInboundRTPStreamStatsFromMediaReceiverInfo(
225 const cricket::MediaReceiverInfo& media_receiver_info,
226 RTCInboundRTPStreamStats* inbound_stats) {
227 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800228 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100229 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700230 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700231 inbound_stats->packets_received =
232 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
233 inbound_stats->bytes_received =
234 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800235 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100236 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700237 inbound_stats->fraction_lost =
238 static_cast<double>(media_receiver_info.fraction_lost);
239}
240
241void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800242 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700243 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800244 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700245 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800246 voice_receiver_info, inbound_audio);
247 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200248 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800249 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800250 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
251 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800252 }
hbos820f5782016-11-22 03:16:50 -0800253 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700254 static_cast<double>(voice_receiver_info.jitter_ms) /
255 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800256 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
257 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 17:32:00 +0200258 if (voice_receiver_info.last_packet_received_timestamp_ms) {
259 inbound_audio->last_packet_received_timestamp =
260 static_cast<double>(
261 *voice_receiver_info.last_packet_received_timestamp_ms) /
262 rtc::kNumMillisecsPerSec;
263 }
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200264 inbound_audio->fec_packets_received =
265 voice_receiver_info.fec_packets_received;
266 inbound_audio->fec_packets_discarded =
267 voice_receiver_info.fec_packets_discarded;
hboseeafe942016-11-01 03:00:17 -0700268}
269
270void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800271 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700272 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800273 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700274 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800275 video_receiver_info, inbound_video);
276 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200277 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800278 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800279 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
280 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800281 }
hbos820f5782016-11-22 03:16:50 -0800282 inbound_video->fir_count =
283 static_cast<uint32_t>(video_receiver_info.firs_sent);
284 inbound_video->pli_count =
285 static_cast<uint32_t>(video_receiver_info.plis_sent);
286 inbound_video->nack_count =
287 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800288 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800289 if (video_receiver_info.qp_sum)
290 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Henrik Boström01738c62019-04-15 17:32:00 +0200291 if (video_receiver_info.last_packet_received_timestamp_ms) {
292 inbound_video->last_packet_received_timestamp =
293 static_cast<double>(
294 *video_receiver_info.last_packet_received_timestamp_ms) /
295 rtc::kNumMillisecsPerSec;
296 }
Henrik Boström2e069262019-04-09 13:59:31 +0200297 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
298 // optional, support the "unspecified" value.
299 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
300 inbound_video->content_type = RTCContentType::kScreenshare;
hboseeafe942016-11-01 03:00:17 -0700301}
302
hbos820f5782016-11-22 03:16:50 -0800303// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700304void SetOutboundRTPStreamStatsFromMediaSenderInfo(
305 const cricket::MediaSenderInfo& media_sender_info,
306 RTCOutboundRTPStreamStats* outbound_stats) {
307 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800308 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100309 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700310 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700311 outbound_stats->packets_sent =
312 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200313 outbound_stats->retransmitted_packets_sent =
314 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 01:50:46 -0700315 outbound_stats->bytes_sent =
316 static_cast<uint64_t>(media_sender_info.bytes_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200317 outbound_stats->retransmitted_bytes_sent =
318 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 01:50:46 -0700319}
320
321void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800322 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700323 const cricket::VoiceSenderInfo& voice_sender_info,
324 RTCOutboundRTPStreamStats* outbound_audio) {
325 SetOutboundRTPStreamStatsFromMediaSenderInfo(
326 voice_sender_info, outbound_audio);
327 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200328 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800329 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800330 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
331 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800332 }
hbos6ded1902016-11-01 01:50:46 -0700333 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
334 // purposefully left undefined for audio.
335}
336
337void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800338 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700339 const cricket::VideoSenderInfo& video_sender_info,
340 RTCOutboundRTPStreamStats* outbound_video) {
341 SetOutboundRTPStreamStatsFromMediaSenderInfo(
342 video_sender_info, outbound_video);
343 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200344 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800345 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800346 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
347 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800348 }
hbos6ded1902016-11-01 01:50:46 -0700349 outbound_video->fir_count =
350 static_cast<uint32_t>(video_sender_info.firs_rcvd);
351 outbound_video->pli_count =
352 static_cast<uint32_t>(video_sender_info.plis_rcvd);
353 outbound_video->nack_count =
354 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800355 if (video_sender_info.qp_sum)
356 outbound_video->qp_sum = *video_sender_info.qp_sum;
357 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200358 outbound_video->total_encode_time =
359 static_cast<double>(video_sender_info.total_encode_time_ms) /
360 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200361 outbound_video->total_encoded_bytes_target =
362 video_sender_info.total_encoded_bytes_target;
Henrik Boström9fe18342019-05-16 18:38:20 +0200363 outbound_video->total_packet_send_delay =
364 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
365 rtc::kNumMillisecsPerSec;
Henrik Boström2e069262019-04-09 13:59:31 +0200366 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
367 // optional, support the "unspecified" value.
368 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
369 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700370}
371
hbos02ba2112016-10-28 05:14:53 -0700372void ProduceCertificateStatsFromSSLCertificateStats(
373 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
374 RTCStatsReport* report) {
375 RTCCertificateStats* prev_certificate_stats = nullptr;
376 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
377 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800378 std::string certificate_stats_id =
379 RTCCertificateIDFromFingerprint(s->fingerprint);
380 // It is possible for the same certificate to show up multiple times, e.g.
381 // if local and remote side use the same certificate in a loopback call.
382 // If the report already contains stats for this certificate, skip it.
383 if (report->Get(certificate_stats_id)) {
384 RTC_DCHECK_EQ(s, &certificate_stats);
385 break;
386 }
hbos02ba2112016-10-28 05:14:53 -0700387 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800388 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700389 certificate_stats->fingerprint = s->fingerprint;
390 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
391 certificate_stats->base64_certificate = s->base64_certificate;
392 if (prev_certificate_stats)
393 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
394 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
395 prev_certificate_stats = certificate_stats;
396 }
397}
398
399const std::string& ProduceIceCandidateStats(
400 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800401 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700402 const std::string& id = "RTCIceCandidate_" + candidate.id();
403 const RTCStats* stats = report->Get(id);
404 if (!stats) {
405 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
406 if (is_local)
407 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
408 else
409 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800410 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800411 if (is_local) {
412 candidate_stats->network_type =
413 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200414 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
415 std::string relay_protocol = candidate.relay_protocol();
416 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
417 relay_protocol.compare("tcp") == 0 ||
418 relay_protocol.compare("tls") == 0);
419 candidate_stats->relay_protocol = relay_protocol;
420 }
Gary Liu37e489c2017-11-21 10:49:36 -0800421 } else {
422 // We don't expect to know the adapter type of remote candidates.
423 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
424 }
hbos02ba2112016-10-28 05:14:53 -0700425 candidate_stats->ip = candidate.address().ipaddr().ToString();
426 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
427 candidate_stats->protocol = candidate.protocol();
428 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
429 candidate.type());
430 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
431
432 stats = candidate_stats.get();
433 report->AddStats(std::move(candidate_stats));
434 }
435 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
436 : RTCRemoteIceCandidateStats::kType);
437 return stats->id();
438}
439
hbos9e302742017-01-20 02:47:10 -0800440std::unique_ptr<RTCMediaStreamTrackStats>
441ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
442 int64_t timestamp_us,
443 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100444 const cricket::VoiceSenderInfo& voice_sender_info,
445 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800446 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
447 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100448 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
449 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100450 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800451 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
452 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200453 audio_track_stats->media_source_id =
454 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
455 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800456 audio_track_stats->remote_source = false;
457 audio_track_stats->detached = false;
458 if (voice_sender_info.audio_level >= 0) {
459 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
460 voice_sender_info.audio_level);
461 }
zsteine76bd3a2017-07-14 12:17:49 -0700462 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
463 audio_track_stats->total_samples_duration =
464 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100465 if (voice_sender_info.apm_statistics.echo_return_loss) {
466 audio_track_stats->echo_return_loss =
467 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800468 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100469 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
470 audio_track_stats->echo_return_loss_enhancement =
471 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800472 }
473 return audio_track_stats;
474}
475
476std::unique_ptr<RTCMediaStreamTrackStats>
477ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
478 int64_t timestamp_us,
479 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100480 const cricket::VoiceReceiverInfo& voice_receiver_info,
481 int attachment_id) {
482 // Since receiver tracks can't be reattached, we use the SSRC as
483 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800484 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
485 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100486 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
487 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100488 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800489 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
490 audio_track, audio_track_stats.get());
491 audio_track_stats->remote_source = true;
492 audio_track_stats->detached = false;
493 if (voice_receiver_info.audio_level >= 0) {
494 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
495 voice_receiver_info.audio_level);
496 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200497 audio_track_stats->jitter_buffer_delay =
498 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100499 audio_track_stats->jitter_buffer_emitted_count =
500 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200501 audio_track_stats->inserted_samples_for_deceleration =
502 voice_receiver_info.inserted_samples_for_deceleration;
503 audio_track_stats->removed_samples_for_acceleration =
504 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 12:17:49 -0700505 audio_track_stats->total_audio_energy =
506 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700507 audio_track_stats->total_samples_received =
508 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700509 audio_track_stats->total_samples_duration =
510 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700511 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200512 audio_track_stats->silent_concealed_samples =
513 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200514 audio_track_stats->concealment_events =
515 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100516 audio_track_stats->jitter_buffer_flushes =
517 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100518 audio_track_stats->delayed_packet_outage_samples =
519 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100520 audio_track_stats->relative_packet_arrival_delay =
521 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200522 audio_track_stats->interruption_count =
523 voice_receiver_info.interruption_count >= 0
524 ? voice_receiver_info.interruption_count
525 : 0;
526 audio_track_stats->total_interruption_duration =
527 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
528 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800529 return audio_track_stats;
530}
531
532std::unique_ptr<RTCMediaStreamTrackStats>
533ProduceMediaStreamTrackStatsFromVideoSenderInfo(
534 int64_t timestamp_us,
535 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100536 const cricket::VideoSenderInfo& video_sender_info,
537 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800538 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
539 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100540 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 09:18:58 +0100541 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100542 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800543 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
544 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200545 video_track_stats->media_source_id =
546 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
547 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800548 video_track_stats->remote_source = false;
549 video_track_stats->detached = false;
550 video_track_stats->frame_width = static_cast<uint32_t>(
551 video_sender_info.send_frame_width);
552 video_track_stats->frame_height = static_cast<uint32_t>(
553 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800554 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100555 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800556 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100557 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800558 return video_track_stats;
559}
560
561std::unique_ptr<RTCMediaStreamTrackStats>
562ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
563 int64_t timestamp_us,
564 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100565 const cricket::VideoReceiverInfo& video_receiver_info,
566 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800567 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
568 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100569 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
570
571 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100572 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800573 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
574 video_track, video_track_stats.get());
575 video_track_stats->remote_source = true;
576 video_track_stats->detached = false;
577 if (video_receiver_info.frame_width > 0 &&
578 video_receiver_info.frame_height > 0) {
579 video_track_stats->frame_width = static_cast<uint32_t>(
580 video_receiver_info.frame_width);
581 video_track_stats->frame_height = static_cast<uint32_t>(
582 video_receiver_info.frame_height);
583 }
hbos42f6d2f2017-01-20 03:56:50 -0800584 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800585 // TODO(hbos): When we support receiving simulcast, this should be the total
586 // number of frames correctly decoded, independent of which SSRC it was
587 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100588 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800589 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800590 RTC_DCHECK_GE(video_receiver_info.frames_received,
591 video_receiver_info.frames_rendered);
592 video_track_stats->frames_dropped = video_receiver_info.frames_received -
593 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100594 video_track_stats->freeze_count = video_receiver_info.freeze_count;
595 video_track_stats->pause_count = video_receiver_info.pause_count;
596 video_track_stats->total_freezes_duration =
597 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
598 rtc::kNumMillisecsPerSec;
599 video_track_stats->total_pauses_duration =
600 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
601 rtc::kNumMillisecsPerSec;
602 video_track_stats->total_frames_duration =
603 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
604 rtc::kNumMillisecsPerSec;
605 video_track_stats->sum_squared_frame_durations =
606 video_receiver_info.sum_squared_frame_durations;
607
hbos9e302742017-01-20 02:47:10 -0800608 return video_track_stats;
609}
610
Harald Alvestrand89061872018-01-02 14:08:34 +0100611void ProduceSenderMediaTrackStats(
612 int64_t timestamp_us,
613 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800614 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100615 RTCStatsReport* report) {
616 // This function iterates over the senders to generate outgoing track stats.
617
618 // TODO(hbos): Return stats of detached tracks. We have to perform stats
619 // gathering at the time of detachment to get accurate stats and timestamps.
620 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100621 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100622 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
623 AudioTrackInterface* track =
624 static_cast<AudioTrackInterface*>(sender->track().get());
625 if (!track)
626 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100627 cricket::VoiceSenderInfo null_sender_info;
628 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
629 // TODO(hta): Checking on ssrc is not proper. There should be a way
630 // to see from a sender whether it's connected or not.
631 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800632 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100633 // When pc.close is called, sender info is discarded, so
634 // we generate zeroes instead. Bug: It should be retained.
635 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800636 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100637 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100638 if (sender_info) {
639 voice_sender_info = sender_info;
640 } else {
641 RTC_LOG(LS_INFO)
642 << "RTCStatsCollector: No voice sender info for sender with ssrc "
643 << sender->ssrc();
644 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100645 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100646 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100647 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
648 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100649 report->AddStats(std::move(audio_track_stats));
650 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
651 VideoTrackInterface* track =
652 static_cast<VideoTrackInterface*>(sender->track().get());
653 if (!track)
654 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100655 cricket::VideoSenderInfo null_sender_info;
656 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
657 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100658 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
659 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800660 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100661 // When pc.close is called, sender info is discarded, so
662 // we generate zeroes instead. Bug: It should be retained.
663 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800664 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100665 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100666 if (sender_info) {
667 video_sender_info = sender_info;
668 } else {
669 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
670 << sender->ssrc();
671 }
672 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100673 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100674 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
675 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100676 report->AddStats(std::move(video_track_stats));
677 } else {
678 RTC_NOTREACHED();
679 }
680 }
681}
682
683void ProduceReceiverMediaTrackStats(
684 int64_t timestamp_us,
685 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800686 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100687 RTCStatsReport* report) {
688 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100689 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100690 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
691 AudioTrackInterface* track =
692 static_cast<AudioTrackInterface*>(receiver->track().get());
693 const cricket::VoiceReceiverInfo* voice_receiver_info =
694 track_media_info_map.GetVoiceReceiverInfo(*track);
695 if (!voice_receiver_info) {
696 continue;
697 }
698 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
699 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100700 timestamp_us, *track, *voice_receiver_info,
701 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100702 report->AddStats(std::move(audio_track_stats));
703 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
704 VideoTrackInterface* track =
705 static_cast<VideoTrackInterface*>(receiver->track().get());
706 const cricket::VideoReceiverInfo* video_receiver_info =
707 track_media_info_map.GetVideoReceiverInfo(*track);
708 if (!video_receiver_info) {
709 continue;
710 }
711 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
712 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100713 timestamp_us, *track, *video_receiver_info,
714 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100715 report->AddStats(std::move(video_track_stats));
716 } else {
717 RTC_NOTREACHED();
718 }
719 }
720}
721
Henrik Boström5b3541f2018-03-19 13:52:56 +0100722rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
723 bool filter_by_sender_selector,
724 rtc::scoped_refptr<const RTCStatsReport> report,
725 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
726 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
727 std::vector<std::string> rtpstream_ids;
728 if (filter_by_sender_selector) {
729 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
730 if (sender_selector) {
731 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
732 // reference the sender stats.
733 // Because we do not implement sender stats, we look at outbound-rtp(s)
734 // that reference the track attachment stats for the sender instead.
735 std::string track_id =
736 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
737 kSender, sender_selector->AttachmentId());
738 for (const auto& stats : *report) {
739 if (stats.type() != RTCOutboundRTPStreamStats::kType)
740 continue;
741 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
742 if (outbound_rtp.track_id.is_defined() &&
743 *outbound_rtp.track_id == track_id) {
744 rtpstream_ids.push_back(outbound_rtp.id());
745 }
746 }
747 }
748 } else {
749 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
750 if (receiver_selector) {
751 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
752 // reference the receiver stats.
753 // Because we do not implement receiver stats, we look at inbound-rtp(s)
754 // that reference the track attachment stats for the receiver instead.
755 std::string track_id =
756 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
757 kReceiver, receiver_selector->AttachmentId());
758 for (const auto& stats : *report) {
759 if (stats.type() != RTCInboundRTPStreamStats::kType)
760 continue;
761 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
762 if (inbound_rtp.track_id.is_defined() &&
763 *inbound_rtp.track_id == track_id) {
764 rtpstream_ids.push_back(inbound_rtp.id());
765 }
766 }
767 }
768 }
769 if (rtpstream_ids.empty())
770 return RTCStatsReport::Create(report->timestamp_us());
771 return TakeReferencedStats(report->Copy(), rtpstream_ids);
772}
773
hboscc555c52016-10-18 12:48:31 -0700774} // namespace
775
Henrik Boström5b3541f2018-03-19 13:52:56 +0100776RTCStatsCollector::RequestInfo::RequestInfo(
777 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
778 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
779
780RTCStatsCollector::RequestInfo::RequestInfo(
781 rtc::scoped_refptr<RtpSenderInternal> selector,
782 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
783 : RequestInfo(FilterMode::kSenderSelector,
784 std::move(callback),
785 std::move(selector),
786 nullptr) {}
787
788RTCStatsCollector::RequestInfo::RequestInfo(
789 rtc::scoped_refptr<RtpReceiverInternal> selector,
790 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
791 : RequestInfo(FilterMode::kReceiverSelector,
792 std::move(callback),
793 nullptr,
794 std::move(selector)) {}
795
796RTCStatsCollector::RequestInfo::RequestInfo(
797 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
798 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
799 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
800 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
801 : filter_mode_(filter_mode),
802 callback_(std::move(callback)),
803 sender_selector_(std::move(sender_selector)),
804 receiver_selector_(std::move(receiver_selector)) {
805 RTC_DCHECK(callback_);
806 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
807}
808
hbosc82f2e12016-09-05 01:36:50 -0700809rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800810 PeerConnectionInternal* pc,
811 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700812 return rtc::scoped_refptr<RTCStatsCollector>(
813 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
814}
815
Steve Anton2d8609c2018-01-23 16:38:46 -0800816RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700817 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700818 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700819 signaling_thread_(pc->signaling_thread()),
820 worker_thread_(pc->worker_thread()),
821 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700822 num_pending_partial_reports_(0),
823 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100824 network_report_event_(true /* manual_reset */,
825 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700826 cache_timestamp_us_(0),
827 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700828 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700829 RTC_DCHECK(signaling_thread_);
830 RTC_DCHECK(worker_thread_);
831 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700832 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800833 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800834 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700835}
836
hbosb78306a2016-12-19 05:06:57 -0800837RTCStatsCollector::~RTCStatsCollector() {
838 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
839}
840
hbosc82f2e12016-09-05 01:36:50 -0700841void RTCStatsCollector::GetStatsReport(
842 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100843 GetStatsReportInternal(RequestInfo(std::move(callback)));
844}
845
846void RTCStatsCollector::GetStatsReport(
847 rtc::scoped_refptr<RtpSenderInternal> selector,
848 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
849 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
850}
851
852void RTCStatsCollector::GetStatsReport(
853 rtc::scoped_refptr<RtpReceiverInternal> selector,
854 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
855 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
856}
857
858void RTCStatsCollector::GetStatsReportInternal(
859 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700860 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100861 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700862
hbos0e6758d2016-08-31 07:57:36 -0700863 // "Now" using a monotonically increasing timer.
864 int64_t cache_now_us = rtc::TimeMicros();
865 if (cached_report_ &&
866 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800867 // We have a fresh cached report to deliver. Deliver asynchronously, since
868 // the caller may not be expecting a synchronous callback, and it avoids
869 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100870 std::vector<RequestInfo> requests;
871 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100872 signaling_thread_->PostTask(
873 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
874 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700875 } else if (!num_pending_partial_reports_) {
876 // Only start gathering stats if we're not already gathering stats. In the
877 // case of already gathering stats, |callback_| will be invoked when there
878 // are no more pending partial reports.
879
880 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
881 // UTC), in microseconds. The system clock could be modified and is not
882 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700883 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700884
hbosf415f8a2017-01-02 04:28:51 -0800885 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700886 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800887
Steve Anton57858b32018-02-15 15:19:50 -0800888 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800889 // |ProducePartialResultsOnNetworkThread| and
890 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800891 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700892 // Prepare |transport_names_| for use in
893 // |ProducePartialResultsOnNetworkThread|.
894 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800895
stefanf79ade12017-06-02 06:44:03 -0700896 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
897 // thread.
898 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
899 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -0700900 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700901
Henrik Boström40b030e2019-02-28 09:49:31 +0100902 // Don't touch |network_report_| on the signaling thread until
903 // ProducePartialResultsOnNetworkThread() has signaled the
904 // |network_report_event_|.
905 network_report_event_.Reset();
906 network_thread_->PostTask(
907 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -0700908 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +0100909 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -0800910 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -0700911 }
hbosd565b732016-08-30 14:04:35 -0700912}
913
914void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -0700915 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -0700916 cached_report_ = nullptr;
917}
918
hbosb78306a2016-12-19 05:06:57 -0800919void RTCStatsCollector::WaitForPendingRequest() {
920 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100921 // If a request is pending, blocks until the |network_report_event_| is
922 // signaled and then delivers the result. Otherwise this is a NO-OP.
923 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -0800924}
925
hbosc82f2e12016-09-05 01:36:50 -0700926void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
927 int64_t timestamp_us) {
928 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +0100929 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700930
Henrik Boström40b030e2019-02-28 09:49:31 +0100931 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
932 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -0700933
Henrik Boström40b030e2019-02-28 09:49:31 +0100934 // ProducePartialResultsOnSignalingThread() is running synchronously on the
935 // signaling thread, so it is always the first partial result delivered on the
936 // signaling thread. The request is not complete until MergeNetworkReport_s()
937 // happens; we don't have to do anything here.
938 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
939 --num_pending_partial_reports_;
940}
941
942void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
943 int64_t timestamp_us,
944 RTCStatsReport* partial_report) {
945 RTC_DCHECK(signaling_thread_->IsCurrent());
946 ProduceDataChannelStats_s(timestamp_us, partial_report);
947 ProduceMediaStreamStats_s(timestamp_us, partial_report);
948 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 15:49:42 +0200949 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +0100950 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -0700951}
952
hbosc82f2e12016-09-05 01:36:50 -0700953void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
954 int64_t timestamp_us) {
955 RTC_DCHECK(network_thread_->IsCurrent());
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200956 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 09:49:31 +0100957 // |network_report_event_| is reset before this method is invoked.
958 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -0700959
Steve Anton5dfde182018-02-06 10:34:40 -0800960 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -0700961 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -0800962 std::map<std::string, CertificateStatsPair> transport_cert_stats =
963 PrepareTransportCertificateStats_n(transport_stats_by_name);
964
Henrik Boström40b030e2019-02-28 09:49:31 +0100965 ProducePartialResultsOnNetworkThreadImpl(
966 timestamp_us, transport_stats_by_name, transport_cert_stats,
967 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +0000968
Henrik Boström40b030e2019-02-28 09:49:31 +0100969 // Signal that it is now safe to touch |network_report_| on the signaling
970 // thread, and post a task to merge it into the final results.
971 network_report_event_.Set();
972 signaling_thread_->PostTask(
973 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +0100974}
975
Henrik Boström40b030e2019-02-28 09:49:31 +0100976void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
977 int64_t timestamp_us,
978 const std::map<std::string, cricket::TransportStats>&
979 transport_stats_by_name,
980 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
981 RTCStatsReport* partial_report) {
982 RTC_DCHECK(network_thread_->IsCurrent());
983 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
984 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
985 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
986 call_stats_, partial_report);
987 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
988 partial_report);
989 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
990 transport_cert_stats, partial_report);
991}
992
993void RTCStatsCollector::MergeNetworkReport_s() {
994 RTC_DCHECK(signaling_thread_->IsCurrent());
995 // The |network_report_event_| must be signaled for it to be safe to touch
996 // |network_report_|. This is normally not blocking, but if
997 // WaitForPendingRequest() is called while a request is pending, we might have
998 // to wait until the network thread is done touching |network_report_|.
999 network_report_event_.Wait(rtc::Event::kForever);
1000 if (!network_report_) {
1001 // Normally, MergeNetworkReport_s() is executed because it is posted from
1002 // the network thread. But if WaitForPendingRequest() is called while a
1003 // request is pending, an early call to MergeNetworkReport_s() is made,
1004 // merging the report and setting |network_report_| to null. If so, when the
1005 // previously posted MergeNetworkReport_s() is later executed, the report is
1006 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -07001007 return;
1008 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001009 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +01001010 RTC_DCHECK(partial_report_);
1011 partial_report_->TakeMembersFrom(network_report_);
1012 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001013 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +01001014 // |network_report_| is currently the only partial report collected
1015 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1016 // ready to deliver the result.
1017 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1018 cache_timestamp_us_ = partial_report_timestamp_us_;
1019 cached_report_ = partial_report_;
1020 partial_report_ = nullptr;
1021 transceiver_stats_infos_.clear();
1022 // Trace WebRTC Stats when getStats is called on Javascript.
1023 // This allows access to WebRTC stats from trace logs. To enable them,
1024 // select the "webrtc_stats" category when recording traces.
1025 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1026 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001027
Henrik Boström40b030e2019-02-28 09:49:31 +01001028 // Deliver report and clear |requests_|.
1029 std::vector<RequestInfo> requests;
1030 requests.swap(requests_);
1031 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001032}
1033
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001034void RTCStatsCollector::DeliverCachedReport(
1035 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001036 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001037 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001038 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001039 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001040
Henrik Boström5b3541f2018-03-19 13:52:56 +01001041 for (const RequestInfo& request : requests) {
1042 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1043 request.callback()->OnStatsDelivered(cached_report);
1044 } else {
1045 bool filter_by_sender_selector;
1046 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1047 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1048 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1049 filter_by_sender_selector = true;
1050 sender_selector = request.sender_selector();
1051 } else {
1052 RTC_DCHECK(request.filter_mode() ==
1053 RequestInfo::FilterMode::kReceiverSelector);
1054 filter_by_sender_selector = false;
1055 receiver_selector = request.receiver_selector();
1056 }
1057 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1058 filter_by_sender_selector, cached_report, sender_selector,
1059 receiver_selector));
1060 }
hbosc82f2e12016-09-05 01:36:50 -07001061 }
hbosd565b732016-08-30 14:04:35 -07001062}
1063
hbosdf6075a2016-12-19 04:58:02 -08001064void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001065 int64_t timestamp_us,
1066 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001067 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001068 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001069 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1070 if (transport_cert_stats_pair.second.local) {
1071 ProduceCertificateStatsFromSSLCertificateStats(
1072 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001073 }
hbos02ba2112016-10-28 05:14:53 -07001074 if (transport_cert_stats_pair.second.remote) {
1075 ProduceCertificateStatsFromSSLCertificateStats(
1076 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001077 }
1078 }
1079}
1080
hbosdf6075a2016-12-19 04:58:02 -08001081void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001082 int64_t timestamp_us,
1083 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001084 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001085 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001086 for (const auto& stats : transceiver_stats_infos) {
1087 if (!stats.mid) {
1088 continue;
hbos0adb8282016-11-23 02:32:06 -08001089 }
Steve Anton57858b32018-02-15 15:19:50 -08001090 const cricket::VoiceMediaInfo* voice_media_info =
1091 stats.track_media_info_map->voice_media_info();
1092 const cricket::VideoMediaInfo* video_media_info =
1093 stats.track_media_info_map->video_media_info();
1094 // Audio
1095 if (voice_media_info) {
1096 // Inbound
1097 for (const auto& pair : voice_media_info->receive_codecs) {
1098 report->AddStats(CodecStatsFromRtpCodecParameters(
1099 timestamp_us, *stats.mid, true, pair.second));
1100 }
1101 // Outbound
1102 for (const auto& pair : voice_media_info->send_codecs) {
1103 report->AddStats(CodecStatsFromRtpCodecParameters(
1104 timestamp_us, *stats.mid, false, pair.second));
1105 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001106 }
Steve Anton57858b32018-02-15 15:19:50 -08001107 // Video
1108 if (video_media_info) {
1109 // Inbound
1110 for (const auto& pair : video_media_info->receive_codecs) {
1111 report->AddStats(CodecStatsFromRtpCodecParameters(
1112 timestamp_us, *stats.mid, true, pair.second));
1113 }
1114 // Outbound
1115 for (const auto& pair : video_media_info->send_codecs) {
1116 report->AddStats(CodecStatsFromRtpCodecParameters(
1117 timestamp_us, *stats.mid, false, pair.second));
1118 }
hbos0adb8282016-11-23 02:32:06 -08001119 }
1120 }
1121}
1122
hboscc555c52016-10-18 12:48:31 -07001123void RTCStatsCollector::ProduceDataChannelStats_s(
1124 int64_t timestamp_us, RTCStatsReport* report) const {
1125 RTC_DCHECK(signaling_thread_->IsCurrent());
1126 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1127 pc_->sctp_data_channels()) {
1128 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1129 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001130 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001131 timestamp_us));
1132 data_channel_stats->label = data_channel->label();
1133 data_channel_stats->protocol = data_channel->protocol();
1134 data_channel_stats->datachannelid = data_channel->id();
1135 data_channel_stats->state =
1136 DataStateToRTCDataChannelState(data_channel->state());
1137 data_channel_stats->messages_sent = data_channel->messages_sent();
1138 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1139 data_channel_stats->messages_received = data_channel->messages_received();
1140 data_channel_stats->bytes_received = data_channel->bytes_received();
1141 report->AddStats(std::move(data_channel_stats));
1142 }
1143}
1144
hbosdf6075a2016-12-19 04:58:02 -08001145void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001146 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001147 const std::map<std::string, cricket::TransportStats>&
1148 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001149 const Call::Stats& call_stats,
1150 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001151 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001152 for (const auto& entry : transport_stats_by_name) {
1153 const std::string& transport_name = entry.first;
1154 const cricket::TransportStats& transport_stats = entry.second;
1155 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001156 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001157 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001158 for (const cricket::ConnectionInfo& info :
1159 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001160 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001161 new RTCIceCandidatePairStats(
1162 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1163 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001164
hbos0583b282016-11-30 01:50:14 -08001165 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001166 // TODO(hbos): There could be other candidates that are not paired with
1167 // anything. We don't have a complete list. Local candidates come from
1168 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001169 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001170 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001171 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001172 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001173 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001174 candidate_pair_stats->state =
1175 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1176 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001177 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001178 // TODO(hbos): This writable is different than the spec. It goes to
1179 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001180 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001181 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001182 candidate_pair_stats->bytes_sent =
1183 static_cast<uint64_t>(info.sent_total_bytes);
1184 candidate_pair_stats->bytes_received =
1185 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001186 candidate_pair_stats->total_round_trip_time =
1187 static_cast<double>(info.total_round_trip_time_ms) /
1188 rtc::kNumMillisecsPerSec;
1189 if (info.current_round_trip_time_ms) {
1190 candidate_pair_stats->current_round_trip_time =
1191 static_cast<double>(*info.current_round_trip_time_ms) /
1192 rtc::kNumMillisecsPerSec;
1193 }
stefanf79ade12017-06-02 06:44:03 -07001194 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001195 // The bandwidth estimations we have are for the selected candidate
1196 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001197 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1198 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1199 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001200 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001201 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001202 }
stefanf79ade12017-06-02 06:44:03 -07001203 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001204 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001205 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001206 }
1207 }
hbosd82f5122016-12-09 04:12:39 -08001208 candidate_pair_stats->requests_received =
1209 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001210 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1211 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001212 candidate_pair_stats->responses_received =
1213 static_cast<uint64_t>(info.recv_ping_responses);
1214 candidate_pair_stats->responses_sent =
1215 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001216 RTC_DCHECK_GE(info.sent_ping_requests_total,
1217 info.sent_ping_requests_before_first_response);
1218 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1219 info.sent_ping_requests_total -
1220 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001221
1222 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001223 }
1224 }
1225 }
1226}
1227
Steve Anton57858b32018-02-15 15:19:50 -08001228void RTCStatsCollector::ProduceMediaStreamStats_s(
1229 int64_t timestamp_us,
1230 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001231 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001232
1233 std::map<std::string, std::vector<std::string>> track_ids;
1234
1235 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001236 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001237 std::string track_id =
1238 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1239 kSender, sender->internal()->AttachmentId());
1240 for (auto& stream_id : sender->stream_ids()) {
1241 track_ids[stream_id].push_back(track_id);
1242 }
1243 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001244 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001245 std::string track_id =
1246 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1247 kReceiver, receiver->internal()->AttachmentId());
1248 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001249 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001250 }
1251 }
1252 }
1253
1254 // Build stats for each stream ID known.
1255 for (auto& it : track_ids) {
1256 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1257 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1258 stream_stats->stream_identifier = it.first;
1259 stream_stats->track_ids = it.second;
1260 report->AddStats(std::move(stream_stats));
1261 }
1262}
1263
1264void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1265 int64_t timestamp_us,
1266 RTCStatsReport* report) const {
1267 RTC_DCHECK(signaling_thread_->IsCurrent());
1268 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1269 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001270 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001271 senders.push_back(sender->internal());
1272 }
1273 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1274 senders, report);
1275
1276 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001277 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001278 receivers.push_back(receiver->internal());
1279 }
1280 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1281 receivers, report);
1282 }
hbos09bc1282016-11-08 06:29:22 -08001283}
1284
Henrik Boström646fda02019-05-22 15:49:42 +02001285void RTCStatsCollector::ProduceMediaSourceStats_s(
1286 int64_t timestamp_us,
1287 RTCStatsReport* report) const {
1288 RTC_DCHECK(signaling_thread_->IsCurrent());
1289 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1290 transceiver_stats_infos_) {
1291 const auto& track_media_info_map =
1292 transceiver_stats_info.track_media_info_map;
1293 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1294 const auto& sender_internal = sender->internal();
1295 const auto& track = sender_internal->track();
1296 if (!track)
1297 continue;
1298 // TODO(hbos): The same track could be attached to multiple senders which
1299 // should result in multiple senders referencing the same media source
1300 // stats. When all media source related metrics are moved to the track's
1301 // source (e.g. input frame rate is moved from cricket::VideoSenderInfo to
1302 // VideoTrackSourceInterface::Stats), don't create separate media source
1303 // stats objects on a per-attachment basis.
1304 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1305 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
1306 media_source_stats = absl::make_unique<RTCAudioSourceStats>(
1307 RTCMediaSourceStatsIDFromKindAndAttachment(
1308 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1309 timestamp_us);
1310 } else {
1311 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
1312 auto video_source_stats = absl::make_unique<RTCVideoSourceStats>(
1313 RTCMediaSourceStatsIDFromKindAndAttachment(
1314 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1315 timestamp_us);
1316 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1317 auto* video_source = video_track->GetSource();
1318 VideoTrackSourceInterface::Stats source_stats;
1319 if (video_source && video_source->GetStats(&source_stats)) {
1320 video_source_stats->width = source_stats.input_width;
1321 video_source_stats->height = source_stats.input_height;
1322 }
1323 // TODO(hbos): Source stats should not depend on whether or not we are
1324 // connected/have an SSRC assigned. Related to
1325 // https://crbug.com/webrtc/8694 (using ssrc 0 to indicate "none").
1326 if (sender_internal->ssrc() != 0) {
1327 auto* sender_info = track_media_info_map->GetVideoSenderInfoBySsrc(
1328 sender_internal->ssrc());
1329 if (sender_info) {
1330 video_source_stats->frames_per_second =
1331 sender_info->framerate_input;
1332 }
1333 }
1334 media_source_stats = std::move(video_source_stats);
1335 }
1336 media_source_stats->track_identifier = track->id();
1337 media_source_stats->kind = track->kind();
1338 report->AddStats(std::move(media_source_stats));
1339 }
1340 }
1341}
1342
hbos6ab97ce2016-10-03 14:16:56 -07001343void RTCStatsCollector::ProducePeerConnectionStats_s(
1344 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001345 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001346 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001347 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001348 stats->data_channels_opened = internal_record_.data_channels_opened;
1349 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001350 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001351}
1352
hbosdf6075a2016-12-19 04:58:02 -08001353void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001354 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001355 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001356 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001357 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001358
Steve Anton57858b32018-02-15 15:19:50 -08001359 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1360 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1361 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1362 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1363 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1364 } else {
1365 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001366 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001367 }
Steve Anton57858b32018-02-15 15:19:50 -08001368}
1369
1370void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1371 int64_t timestamp_us,
1372 const RtpTransceiverStatsInfo& stats,
1373 RTCStatsReport* report) const {
1374 if (!stats.mid || !stats.transport_name) {
1375 return;
1376 }
1377 RTC_DCHECK(stats.track_media_info_map);
1378 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1379 RTC_DCHECK(track_media_info_map.voice_media_info());
1380 std::string mid = *stats.mid;
1381 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1382 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1383 // Inbound
1384 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1385 track_media_info_map.voice_media_info()->receivers) {
1386 if (!voice_receiver_info.connected())
1387 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001388 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001389 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1390 timestamp_us);
1391 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1392 inbound_audio.get());
1393 // TODO(hta): This lookup should look for the sender, not the track.
1394 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1395 track_media_info_map.GetAudioTrack(voice_receiver_info);
1396 if (audio_track) {
1397 inbound_audio->track_id =
1398 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1399 kReceiver,
1400 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001401 }
Steve Anton57858b32018-02-15 15:19:50 -08001402 inbound_audio->transport_id = transport_id;
1403 report->AddStats(std::move(inbound_audio));
1404 }
1405 // Outbound
1406 for (const cricket::VoiceSenderInfo& voice_sender_info :
1407 track_media_info_map.voice_media_info()->senders) {
1408 if (!voice_sender_info.connected())
1409 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001410 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001411 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1412 timestamp_us);
1413 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1414 outbound_audio.get());
1415 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1416 track_media_info_map.GetAudioTrack(voice_sender_info);
1417 if (audio_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001418 int attachment_id =
1419 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001420 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001421 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1422 attachment_id);
1423 outbound_audio->media_source_id =
1424 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1425 attachment_id);
Steve Anton56bae8d2018-02-14 16:07:42 -08001426 }
Steve Anton57858b32018-02-15 15:19:50 -08001427 outbound_audio->transport_id = transport_id;
1428 report->AddStats(std::move(outbound_audio));
1429 }
1430}
1431
1432void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1433 int64_t timestamp_us,
1434 const RtpTransceiverStatsInfo& stats,
1435 RTCStatsReport* report) const {
1436 if (!stats.mid || !stats.transport_name) {
1437 return;
1438 }
1439 RTC_DCHECK(stats.track_media_info_map);
1440 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1441 RTC_DCHECK(track_media_info_map.video_media_info());
1442 std::string mid = *stats.mid;
1443 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1444 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1445 // Inbound
1446 for (const cricket::VideoReceiverInfo& video_receiver_info :
1447 track_media_info_map.video_media_info()->receivers) {
1448 if (!video_receiver_info.connected())
1449 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001450 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001451 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1452 timestamp_us);
1453 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1454 inbound_video.get());
1455 rtc::scoped_refptr<VideoTrackInterface> video_track =
1456 track_media_info_map.GetVideoTrack(video_receiver_info);
1457 if (video_track) {
1458 inbound_video->track_id =
1459 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1460 kReceiver,
1461 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1462 }
1463 inbound_video->transport_id = transport_id;
1464 report->AddStats(std::move(inbound_video));
1465 }
1466 // Outbound
1467 for (const cricket::VideoSenderInfo& video_sender_info :
1468 track_media_info_map.video_media_info()->senders) {
1469 if (!video_sender_info.connected())
1470 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001471 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001472 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1473 timestamp_us);
1474 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1475 outbound_video.get());
1476 rtc::scoped_refptr<VideoTrackInterface> video_track =
1477 track_media_info_map.GetVideoTrack(video_sender_info);
1478 if (video_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001479 int attachment_id =
1480 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001481 outbound_video->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001482 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1483 attachment_id);
1484 outbound_video->media_source_id =
1485 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1486 attachment_id);
Steve Anton57858b32018-02-15 15:19:50 -08001487 }
1488 outbound_video->transport_id = transport_id;
1489 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001490 }
1491}
1492
hbosdf6075a2016-12-19 04:58:02 -08001493void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001494 int64_t timestamp_us,
1495 const std::map<std::string, cricket::TransportStats>&
1496 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001497 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1498 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001499 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001500 for (const auto& entry : transport_stats_by_name) {
1501 const std::string& transport_name = entry.first;
1502 const cricket::TransportStats& transport_stats = entry.second;
1503
hbos2fa7c672016-10-24 04:00:05 -07001504 // Get reference to RTCP channel, if it exists.
1505 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001506 for (const cricket::TransportChannelStats& channel_stats :
1507 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001508 if (channel_stats.component ==
1509 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1510 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001511 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001512 break;
1513 }
1514 }
1515
1516 // Get reference to local and remote certificates of this transport, if they
1517 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001518 const auto& certificate_stats_it =
1519 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001520 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1521 std::string local_certificate_id;
1522 if (certificate_stats_it->second.local) {
1523 local_certificate_id = RTCCertificateIDFromFingerprint(
1524 certificate_stats_it->second.local->fingerprint);
1525 }
1526 std::string remote_certificate_id;
1527 if (certificate_stats_it->second.remote) {
1528 remote_certificate_id = RTCCertificateIDFromFingerprint(
1529 certificate_stats_it->second.remote->fingerprint);
1530 }
1531
1532 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001533 for (const cricket::TransportChannelStats& channel_stats :
1534 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001535 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001536 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1537 transport_name, channel_stats.component),
1538 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001539 transport_stats->bytes_sent = 0;
1540 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001541 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1542 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001543 for (const cricket::ConnectionInfo& info :
1544 channel_stats.connection_infos) {
1545 *transport_stats->bytes_sent += info.sent_total_bytes;
1546 *transport_stats->bytes_received += info.recv_total_bytes;
1547 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001548 transport_stats->selected_candidate_pair_id =
1549 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1550 }
1551 }
1552 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1553 !rtcp_transport_stats_id.empty()) {
1554 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1555 }
1556 if (!local_certificate_id.empty())
1557 transport_stats->local_certificate_id = local_certificate_id;
1558 if (!remote_certificate_id.empty())
1559 transport_stats->remote_certificate_id = remote_certificate_id;
1560 report->AddStats(std::move(transport_stats));
1561 }
1562 }
1563}
1564
1565std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001566RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001567 const std::map<std::string, cricket::TransportStats>&
1568 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001569 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001570 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001571 for (const auto& entry : transport_stats_by_name) {
1572 const std::string& transport_name = entry.first;
1573
hbos2fa7c672016-10-24 04:00:05 -07001574 CertificateStatsPair certificate_stats_pair;
1575 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001576 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001577 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001578 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001579 }
Steve Anton5dfde182018-02-06 10:34:40 -08001580
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001581 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1582 pc_->GetRemoteSSLCertChain(transport_name);
1583 if (remote_cert_chain) {
1584 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001585 }
Steve Anton5dfde182018-02-06 10:34:40 -08001586
hbos2fa7c672016-10-24 04:00:05 -07001587 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001588 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001589 }
1590 return transport_cert_stats;
1591}
1592
Steve Anton57858b32018-02-15 15:19:50 -08001593std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1594RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1595 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001596
Steve Anton57858b32018-02-15 15:19:50 -08001597 // These are used to invoke GetStats for all the media channels together in
1598 // one worker thread hop.
1599 std::map<cricket::VoiceMediaChannel*,
1600 std::unique_ptr<cricket::VoiceMediaInfo>>
1601 voice_stats;
1602 std::map<cricket::VideoMediaChannel*,
1603 std::unique_ptr<cricket::VideoMediaInfo>>
1604 video_stats;
1605
Mirko Bonadei739baf02019-01-27 17:29:42 +01001606 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001607 cricket::MediaType media_type = transceiver->media_type();
1608
1609 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1610 // stats have been fetched on the worker thread.
1611 transceiver_stats_infos.emplace_back();
1612 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1613 stats.transceiver = transceiver->internal();
1614 stats.media_type = media_type;
1615
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001616 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001617 if (!channel) {
1618 // The remaining fields require a BaseChannel.
1619 continue;
1620 }
1621
1622 stats.mid = channel->content_name();
1623 stats.transport_name = channel->transport_name();
1624
1625 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1626 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1627 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1628 voice_stats.end());
1629 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001630 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001631 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1632 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1633 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1634 video_stats.end());
1635 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001636 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001637 } else {
1638 RTC_NOTREACHED();
1639 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001640 }
Steve Anton57858b32018-02-15 15:19:50 -08001641
1642 // Call GetStats for all media channels together on the worker thread in one
1643 // hop.
1644 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1645 for (const auto& entry : voice_stats) {
1646 if (!entry.first->GetStats(entry.second.get())) {
1647 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1648 }
1649 }
1650 for (const auto& entry : video_stats) {
1651 if (!entry.first->GetStats(entry.second.get())) {
1652 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1653 }
1654 }
1655 });
1656
1657 // Create the TrackMediaInfoMap for each transceiver stats object.
1658 for (auto& stats : transceiver_stats_infos) {
1659 auto transceiver = stats.transceiver;
1660 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1661 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1662 if (transceiver->channel()) {
1663 cricket::MediaType media_type = transceiver->media_type();
1664 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1665 auto* voice_channel =
1666 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1667 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1668 voice_media_info =
1669 std::move(voice_stats[voice_channel->media_channel()]);
1670 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1671 auto* video_channel =
1672 static_cast<cricket::VideoChannel*>(transceiver->channel());
1673 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1674 video_media_info =
1675 std::move(video_stats[video_channel->media_channel()]);
1676 }
1677 }
1678 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001679 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001680 senders.push_back(sender->internal());
1681 }
1682 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001683 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001684 receivers.push_back(receiver->internal());
1685 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001686 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001687 std::move(voice_media_info), std::move(video_media_info), senders,
1688 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001689 }
Steve Anton57858b32018-02-15 15:19:50 -08001690
1691 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001692}
1693
Steve Anton7eca0932018-03-30 15:18:41 -07001694std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1695 std::set<std::string> transport_names;
1696 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1697 if (transceiver->internal()->channel()) {
1698 transport_names.insert(
1699 transceiver->internal()->channel()->transport_name());
1700 }
1701 }
1702 if (pc_->rtp_data_channel()) {
1703 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1704 }
1705 if (pc_->sctp_transport_name()) {
1706 transport_names.insert(*pc_->sctp_transport_name());
1707 }
1708 return transport_names;
1709}
1710
hbos82ebe022016-11-14 01:41:09 -08001711void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1712 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1713 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1714}
1715
1716void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1717 RTC_DCHECK(signaling_thread_->IsCurrent());
1718 bool result = internal_record_.opened_data_channels.insert(
1719 reinterpret_cast<uintptr_t>(channel)).second;
1720 ++internal_record_.data_channels_opened;
1721 RTC_DCHECK(result);
1722}
1723
1724void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1725 RTC_DCHECK(signaling_thread_->IsCurrent());
1726 // Only channels that have been fully opened (and have increased the
1727 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001728 if (internal_record_.opened_data_channels.erase(
1729 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001730 ++internal_record_.data_channels_closed;
1731 }
1732}
1733
hboscc555c52016-10-18 12:48:31 -07001734const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1735 const std::string& type) {
1736 return CandidateTypeToRTCIceCandidateType(type);
1737}
1738
1739const char* DataStateToRTCDataChannelStateForTesting(
1740 DataChannelInterface::DataState state) {
1741 return DataStateToRTCDataChannelState(state);
1742}
1743
hbosd565b732016-08-30 14:04:35 -07001744} // namespace webrtc