blob: 116b4ba497307e7ba97a631d16b49eba4ca5af37 [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
Patrik Höglunde2d6a062017-10-05 14:53:33 +020018#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "api/media_stream_interface.h"
20#include "api/peer_connection_interface.h"
Henrik Boström2e069262019-04-09 13:59:31 +020021#include "api/video/video_content_type.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "media/base/media_channel.h"
23#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "pc/peer_connection.h"
26#include "pc/rtc_stats_traversal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Jonas Olsson43568dd2018-06-11 16:25:54 +020028#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070031
32namespace webrtc {
33
hboscc555c52016-10-18 12:48:31 -070034namespace {
35
Henrik Boström646fda02019-05-22 15:49:42 +020036// TODO(https://crbug.com/webrtc/10656): Consider making IDs less predictable.
hbos2fa7c672016-10-24 04:00:05 -070037std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
38 return "RTCCertificate_" + fingerprint;
39}
40
Steve Anton57858b32018-02-15 15:19:50 -080041std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
42 bool inbound,
43 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020044 char buf[1024];
45 rtc::SimpleStringBuilder sb(buf);
46 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
47 << payload_type;
48 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080049}
50
hbos2fa7c672016-10-24 04:00:05 -070051std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
52 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020053 char buf[4096];
54 rtc::SimpleStringBuilder sb(buf);
55 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
56 << info.remote_candidate.id();
57 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070058}
59
Harald Alvestranda3dab842018-01-14 09:18:58 +010060const char kSender[] = "sender";
61const char kReceiver[] = "receiver";
62
63std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
64 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010065 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020066 char buf[1024];
67 rtc::SimpleStringBuilder sb(buf);
68 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
69 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080070}
71
hbos2fa7c672016-10-24 04:00:05 -070072std::string RTCTransportStatsIDFromTransportChannel(
Jonas Olssona4d87372019-07-05 19:08:33 +020073 const std::string& transport_name,
74 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öm8605fbf2019-06-24 16:44:51 +020095std::string RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(
Henrik Boström883eefc2019-05-27 13:40:25 +020096 cricket::MediaType media_type,
Henrik Boström883eefc2019-05-27 13:40:25 +020097 uint32_t source_ssrc) {
98 char buf[1024];
99 rtc::SimpleStringBuilder sb(buf);
100 sb << "RTCRemoteInboundRtp"
101 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
Henrik Boström8605fbf2019-06-24 16:44:51 +0200102 << "Stream_" << source_ssrc;
Henrik Boström883eefc2019-05-27 13:40:25 +0200103 return sb.str();
104}
105
Henrik Boström646fda02019-05-22 15:49:42 +0200106std::string RTCMediaSourceStatsIDFromKindAndAttachment(
107 cricket::MediaType media_type,
108 int attachment_id) {
109 char buf[1024];
110 rtc::SimpleStringBuilder sb(buf);
111 sb << "RTC" << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
112 << "Source_" << attachment_id;
113 return sb.str();
114}
115
hbosab9f6e42016-10-07 02:18:47 -0700116const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
117 if (type == cricket::LOCAL_PORT_TYPE)
118 return RTCIceCandidateType::kHost;
119 if (type == cricket::STUN_PORT_TYPE)
120 return RTCIceCandidateType::kSrflx;
121 if (type == cricket::PRFLX_PORT_TYPE)
122 return RTCIceCandidateType::kPrflx;
123 if (type == cricket::RELAY_PORT_TYPE)
124 return RTCIceCandidateType::kRelay;
125 RTC_NOTREACHED();
126 return nullptr;
127}
128
hboscc555c52016-10-18 12:48:31 -0700129const char* DataStateToRTCDataChannelState(
130 DataChannelInterface::DataState state) {
131 switch (state) {
132 case DataChannelInterface::kConnecting:
133 return RTCDataChannelState::kConnecting;
134 case DataChannelInterface::kOpen:
135 return RTCDataChannelState::kOpen;
136 case DataChannelInterface::kClosing:
137 return RTCDataChannelState::kClosing;
138 case DataChannelInterface::kClosed:
139 return RTCDataChannelState::kClosed;
140 default:
141 RTC_NOTREACHED();
142 return nullptr;
143 }
144}
145
hbos06495bc2017-01-02 08:08:18 -0800146const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
147 cricket::IceCandidatePairState state) {
148 switch (state) {
149 case cricket::IceCandidatePairState::WAITING:
150 return RTCStatsIceCandidatePairState::kWaiting;
151 case cricket::IceCandidatePairState::IN_PROGRESS:
152 return RTCStatsIceCandidatePairState::kInProgress;
153 case cricket::IceCandidatePairState::SUCCEEDED:
154 return RTCStatsIceCandidatePairState::kSucceeded;
155 case cricket::IceCandidatePairState::FAILED:
156 return RTCStatsIceCandidatePairState::kFailed;
157 default:
158 RTC_NOTREACHED();
159 return nullptr;
160 }
161}
162
hbos7064d592017-01-16 07:38:02 -0800163const char* DtlsTransportStateToRTCDtlsTransportState(
164 cricket::DtlsTransportState state) {
165 switch (state) {
166 case cricket::DTLS_TRANSPORT_NEW:
167 return RTCDtlsTransportState::kNew;
168 case cricket::DTLS_TRANSPORT_CONNECTING:
169 return RTCDtlsTransportState::kConnecting;
170 case cricket::DTLS_TRANSPORT_CONNECTED:
171 return RTCDtlsTransportState::kConnected;
172 case cricket::DTLS_TRANSPORT_CLOSED:
173 return RTCDtlsTransportState::kClosed;
174 case cricket::DTLS_TRANSPORT_FAILED:
175 return RTCDtlsTransportState::kFailed;
176 default:
177 RTC_NOTREACHED();
178 return nullptr;
179 }
180}
181
Gary Liu37e489c2017-11-21 10:49:36 -0800182const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
183 switch (type) {
184 case rtc::ADAPTER_TYPE_CELLULAR:
185 return RTCNetworkType::kCellular;
186 case rtc::ADAPTER_TYPE_ETHERNET:
187 return RTCNetworkType::kEthernet;
188 case rtc::ADAPTER_TYPE_WIFI:
189 return RTCNetworkType::kWifi;
190 case rtc::ADAPTER_TYPE_VPN:
191 return RTCNetworkType::kVpn;
192 case rtc::ADAPTER_TYPE_UNKNOWN:
193 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700194 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800195 return RTCNetworkType::kUnknown;
196 }
197 RTC_NOTREACHED();
198 return nullptr;
199}
200
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200201const char* QualityLimitationReasonToRTCQualityLimitationReason(
202 QualityLimitationReason reason) {
203 switch (reason) {
204 case QualityLimitationReason::kNone:
205 return RTCQualityLimitationReason::kNone;
206 case QualityLimitationReason::kCpu:
207 return RTCQualityLimitationReason::kCpu;
208 case QualityLimitationReason::kBandwidth:
209 return RTCQualityLimitationReason::kBandwidth;
210 case QualityLimitationReason::kOther:
211 return RTCQualityLimitationReason::kOther;
212 }
213}
214
hbos9e302742017-01-20 02:47:10 -0800215double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
216 RTC_DCHECK_GE(audio_level, 0);
217 RTC_DCHECK_LE(audio_level, 32767);
218 return audio_level / 32767.0;
219}
220
hbos0adb8282016-11-23 02:32:06 -0800221std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800222 uint64_t timestamp_us,
223 const std::string& mid,
224 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800225 const RtpCodecParameters& codec_params) {
226 RTC_DCHECK_GE(codec_params.payload_type, 0);
227 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800228 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800229 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
230 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800231 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800232 timestamp_us));
233 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800234 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800235 if (codec_params.clock_rate) {
236 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
237 }
hbos0adb8282016-11-23 02:32:06 -0800238 return codec_stats;
239}
240
hbos09bc1282016-11-08 06:29:22 -0800241void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
242 const MediaStreamTrackInterface& track,
243 RTCMediaStreamTrackStats* track_stats) {
244 track_stats->track_identifier = track.id();
245 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
246}
247
hbos820f5782016-11-22 03:16:50 -0800248// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700249void SetInboundRTPStreamStatsFromMediaReceiverInfo(
250 const cricket::MediaReceiverInfo& media_receiver_info,
251 RTCInboundRTPStreamStats* inbound_stats) {
252 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800253 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100254 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700255 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700256 inbound_stats->packets_received =
257 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
258 inbound_stats->bytes_received =
Niels Möllerac0a4cb2019-10-09 15:01:33 +0200259 static_cast<uint64_t>(media_receiver_info.payload_bytes_rcvd);
260 inbound_stats->header_bytes_received =
261 static_cast<uint64_t>(media_receiver_info.header_and_padding_bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800262 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100263 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700264}
265
266void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800267 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700268 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800269 RTCInboundRTPStreamStats* inbound_audio) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200270 SetInboundRTPStreamStatsFromMediaReceiverInfo(voice_receiver_info,
271 inbound_audio);
hbos820f5782016-11-22 03:16:50 -0800272 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200273 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800274 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800275 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
276 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800277 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200278 inbound_audio->jitter = static_cast<double>(voice_receiver_info.jitter_ms) /
279 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800280 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
281 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 17:32:00 +0200282 if (voice_receiver_info.last_packet_received_timestamp_ms) {
283 inbound_audio->last_packet_received_timestamp =
284 static_cast<double>(
285 *voice_receiver_info.last_packet_received_timestamp_ms) /
286 rtc::kNumMillisecsPerSec;
287 }
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200288 if (voice_receiver_info.estimated_playout_ntp_timestamp_ms) {
289 inbound_audio->estimated_playout_timestamp = static_cast<double>(
290 *voice_receiver_info.estimated_playout_ntp_timestamp_ms);
291 }
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200292 inbound_audio->fec_packets_received =
293 voice_receiver_info.fec_packets_received;
294 inbound_audio->fec_packets_discarded =
295 voice_receiver_info.fec_packets_discarded;
hboseeafe942016-11-01 03:00:17 -0700296}
297
298void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800299 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700300 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800301 RTCInboundRTPStreamStats* inbound_video) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200302 SetInboundRTPStreamStatsFromMediaReceiverInfo(video_receiver_info,
303 inbound_video);
hbos820f5782016-11-22 03:16:50 -0800304 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200305 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800306 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800307 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
308 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800309 }
hbos820f5782016-11-22 03:16:50 -0800310 inbound_video->fir_count =
311 static_cast<uint32_t>(video_receiver_info.firs_sent);
312 inbound_video->pli_count =
313 static_cast<uint32_t>(video_receiver_info.plis_sent);
314 inbound_video->nack_count =
315 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800316 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
Rasmus Brandt2efae772019-06-27 14:29:34 +0200317 inbound_video->key_frames_decoded = video_receiver_info.key_frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800318 if (video_receiver_info.qp_sum)
319 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Johannes Kronbfd343b2019-07-01 10:07:50 +0200320 inbound_video->total_decode_time =
321 static_cast<double>(video_receiver_info.total_decode_time_ms) /
322 rtc::kNumMillisecsPerSec;
Johannes Kron00376e12019-11-25 10:25:42 +0100323 inbound_video->total_inter_frame_delay =
324 video_receiver_info.total_inter_frame_delay;
325 inbound_video->total_squared_inter_frame_delay =
326 video_receiver_info.total_squared_inter_frame_delay;
Henrik Boström01738c62019-04-15 17:32:00 +0200327 if (video_receiver_info.last_packet_received_timestamp_ms) {
328 inbound_video->last_packet_received_timestamp =
329 static_cast<double>(
330 *video_receiver_info.last_packet_received_timestamp_ms) /
331 rtc::kNumMillisecsPerSec;
332 }
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200333 if (video_receiver_info.estimated_playout_ntp_timestamp_ms) {
334 inbound_video->estimated_playout_timestamp = static_cast<double>(
335 *video_receiver_info.estimated_playout_ntp_timestamp_ms);
336 }
Henrik Boström2e069262019-04-09 13:59:31 +0200337 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
338 // optional, support the "unspecified" value.
339 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
340 inbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 13:09:51 +0200341 if (!video_receiver_info.decoder_implementation_name.empty()) {
342 inbound_video->decoder_implementation =
343 video_receiver_info.decoder_implementation_name;
344 }
hboseeafe942016-11-01 03:00:17 -0700345}
346
hbos820f5782016-11-22 03:16:50 -0800347// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700348void SetOutboundRTPStreamStatsFromMediaSenderInfo(
349 const cricket::MediaSenderInfo& media_sender_info,
350 RTCOutboundRTPStreamStats* outbound_stats) {
351 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800352 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100353 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700354 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700355 outbound_stats->packets_sent =
356 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200357 outbound_stats->retransmitted_packets_sent =
358 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 01:50:46 -0700359 outbound_stats->bytes_sent =
Niels Möllerac0a4cb2019-10-09 15:01:33 +0200360 static_cast<uint64_t>(media_sender_info.payload_bytes_sent);
361 outbound_stats->header_bytes_sent =
362 static_cast<uint64_t>(media_sender_info.header_and_padding_bytes_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200363 outbound_stats->retransmitted_bytes_sent =
364 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 01:50:46 -0700365}
366
367void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800368 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700369 const cricket::VoiceSenderInfo& voice_sender_info,
370 RTCOutboundRTPStreamStats* outbound_audio) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200371 SetOutboundRTPStreamStatsFromMediaSenderInfo(voice_sender_info,
372 outbound_audio);
hbos6ded1902016-11-01 01:50:46 -0700373 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200374 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800375 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800376 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
377 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800378 }
hbos6ded1902016-11-01 01:50:46 -0700379 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
380 // purposefully left undefined for audio.
381}
382
383void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800384 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700385 const cricket::VideoSenderInfo& video_sender_info,
386 RTCOutboundRTPStreamStats* outbound_video) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200387 SetOutboundRTPStreamStatsFromMediaSenderInfo(video_sender_info,
388 outbound_video);
hbos6ded1902016-11-01 01:50:46 -0700389 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200390 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800391 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800392 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
393 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800394 }
hbos6ded1902016-11-01 01:50:46 -0700395 outbound_video->fir_count =
396 static_cast<uint32_t>(video_sender_info.firs_rcvd);
397 outbound_video->pli_count =
398 static_cast<uint32_t>(video_sender_info.plis_rcvd);
399 outbound_video->nack_count =
400 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800401 if (video_sender_info.qp_sum)
402 outbound_video->qp_sum = *video_sender_info.qp_sum;
403 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Rasmus Brandt2efae772019-06-27 14:29:34 +0200404 outbound_video->key_frames_encoded = video_sender_info.key_frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200405 outbound_video->total_encode_time =
406 static_cast<double>(video_sender_info.total_encode_time_ms) /
407 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200408 outbound_video->total_encoded_bytes_target =
409 video_sender_info.total_encoded_bytes_target;
Henrik Boström9fe18342019-05-16 18:38:20 +0200410 outbound_video->total_packet_send_delay =
411 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
412 rtc::kNumMillisecsPerSec;
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200413 outbound_video->quality_limitation_reason =
414 QualityLimitationReasonToRTCQualityLimitationReason(
415 video_sender_info.quality_limitation_reason);
Evan Shrubsolecc62b162019-09-09 11:26:45 +0200416 outbound_video->quality_limitation_resolution_changes =
417 video_sender_info.quality_limitation_resolution_changes;
Henrik Boström2e069262019-04-09 13:59:31 +0200418 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
419 // optional, support the "unspecified" value.
420 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
421 outbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 13:09:51 +0200422 if (!video_sender_info.encoder_implementation_name.empty()) {
423 outbound_video->encoder_implementation =
424 video_sender_info.encoder_implementation_name;
425 }
hbos6ded1902016-11-01 01:50:46 -0700426}
427
Henrik Boström883eefc2019-05-27 13:40:25 +0200428std::unique_ptr<RTCRemoteInboundRtpStreamStats>
429ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
430 const ReportBlockData& report_block_data,
431 cricket::MediaType media_type,
Henrik Boström4f40fa52019-12-19 13:27:27 +0100432 std::map<std::string, RTCOutboundRTPStreamStats*> outbound_rtps,
Henrik Boström883eefc2019-05-27 13:40:25 +0200433 const RTCStatsReport& report) {
434 const auto& report_block = report_block_data.report_block();
435 // RTCStats' timestamp generally refers to when the metric was sampled, but
436 // for "remote-[outbound/inbound]-rtp" it refers to the local time when the
437 // Report Block was received.
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200438 auto remote_inbound = std::make_unique<RTCRemoteInboundRtpStreamStats>(
Henrik Boström8605fbf2019-06-24 16:44:51 +0200439 RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(media_type,
440 report_block.source_ssrc),
Henrik Boström883eefc2019-05-27 13:40:25 +0200441 /*timestamp=*/report_block_data.report_block_timestamp_utc_us());
Henrik Boström8605fbf2019-06-24 16:44:51 +0200442 remote_inbound->ssrc = report_block.source_ssrc;
Henrik Boström883eefc2019-05-27 13:40:25 +0200443 remote_inbound->kind =
444 media_type == cricket::MEDIA_TYPE_AUDIO ? "audio" : "video";
445 remote_inbound->packets_lost = report_block.packets_lost;
446 remote_inbound->round_trip_time =
447 static_cast<double>(report_block_data.last_rtt_ms()) /
448 rtc::kNumMillisecsPerSec;
449
450 std::string local_id = RTCOutboundRTPStreamStatsIDFromSSRC(
451 media_type == cricket::MEDIA_TYPE_AUDIO, report_block.source_ssrc);
Henrik Boström4f40fa52019-12-19 13:27:27 +0100452 // Look up local stat from |outbound_rtps| where the pointers are non-const.
453 auto local_id_it = outbound_rtps.find(local_id);
454 if (local_id_it != outbound_rtps.end()) {
Henrik Boström883eefc2019-05-27 13:40:25 +0200455 remote_inbound->local_id = local_id;
Henrik Boström4f40fa52019-12-19 13:27:27 +0100456 auto& outbound_rtp = *local_id_it->second;
457 outbound_rtp.remote_id = remote_inbound->id();
Henrik Boström883eefc2019-05-27 13:40:25 +0200458 // The RTP/RTCP transport is obtained from the
459 // RTCOutboundRtpStreamStats's transport.
460 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
461 ? report.Get(*outbound_rtp.transport_id)
462 : nullptr;
463 if (transport_from_id) {
464 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
465 // If RTP and RTCP are not multiplexed, there is a separate RTCP
466 // transport paired with the RTP transport, otherwise the same
467 // transport is used for RTCP and RTP.
468 remote_inbound->transport_id =
469 transport.rtcp_transport_stats_id.is_defined()
470 ? *transport.rtcp_transport_stats_id
471 : *outbound_rtp.transport_id;
472 }
473 // We're assuming the same codec is used on both ends. However if the
474 // codec is switched out on the fly we may have received a Report Block
475 // based on the previous codec and there is no way to tell which point in
476 // time the codec changed for the remote end.
477 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
478 ? report.Get(*outbound_rtp.codec_id)
479 : nullptr;
480 if (codec_from_id) {
481 remote_inbound->codec_id = *outbound_rtp.codec_id;
482 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
483 if (codec.clock_rate.is_defined()) {
484 // The Report Block jitter is expressed in RTP timestamp units
485 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
486 // to seconds we divide by the codec's clock rate.
487 remote_inbound->jitter =
488 static_cast<double>(report_block.jitter) / *codec.clock_rate;
489 }
490 }
491 }
492 return remote_inbound;
493}
494
hbos02ba2112016-10-28 05:14:53 -0700495void ProduceCertificateStatsFromSSLCertificateStats(
Jonas Olssona4d87372019-07-05 19:08:33 +0200496 int64_t timestamp_us,
497 const rtc::SSLCertificateStats& certificate_stats,
hbos02ba2112016-10-28 05:14:53 -0700498 RTCStatsReport* report) {
499 RTCCertificateStats* prev_certificate_stats = nullptr;
500 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
501 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800502 std::string certificate_stats_id =
503 RTCCertificateIDFromFingerprint(s->fingerprint);
504 // It is possible for the same certificate to show up multiple times, e.g.
505 // if local and remote side use the same certificate in a loopback call.
506 // If the report already contains stats for this certificate, skip it.
507 if (report->Get(certificate_stats_id)) {
508 RTC_DCHECK_EQ(s, &certificate_stats);
509 break;
510 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200511 RTCCertificateStats* certificate_stats =
512 new RTCCertificateStats(certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700513 certificate_stats->fingerprint = s->fingerprint;
514 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
515 certificate_stats->base64_certificate = s->base64_certificate;
516 if (prev_certificate_stats)
517 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
518 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
519 prev_certificate_stats = certificate_stats;
520 }
521}
522
Jonas Olssona4d87372019-07-05 19:08:33 +0200523const std::string& ProduceIceCandidateStats(int64_t timestamp_us,
524 const cricket::Candidate& candidate,
525 bool is_local,
526 const std::string& transport_id,
527 RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700528 const std::string& id = "RTCIceCandidate_" + candidate.id();
529 const RTCStats* stats = report->Get(id);
530 if (!stats) {
531 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
532 if (is_local)
533 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
534 else
535 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800536 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800537 if (is_local) {
538 candidate_stats->network_type =
539 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200540 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
541 std::string relay_protocol = candidate.relay_protocol();
542 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
543 relay_protocol.compare("tcp") == 0 ||
544 relay_protocol.compare("tls") == 0);
545 candidate_stats->relay_protocol = relay_protocol;
546 }
Gary Liu37e489c2017-11-21 10:49:36 -0800547 } else {
548 // We don't expect to know the adapter type of remote candidates.
549 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
550 }
hbos02ba2112016-10-28 05:14:53 -0700551 candidate_stats->ip = candidate.address().ipaddr().ToString();
552 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
553 candidate_stats->protocol = candidate.protocol();
Jonas Olssona4d87372019-07-05 19:08:33 +0200554 candidate_stats->candidate_type =
555 CandidateTypeToRTCIceCandidateType(candidate.type());
hbos02ba2112016-10-28 05:14:53 -0700556 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
557
558 stats = candidate_stats.get();
559 report->AddStats(std::move(candidate_stats));
560 }
561 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
562 : RTCRemoteIceCandidateStats::kType);
563 return stats->id();
564}
565
hbos9e302742017-01-20 02:47:10 -0800566std::unique_ptr<RTCMediaStreamTrackStats>
567ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
568 int64_t timestamp_us,
569 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100570 const cricket::VoiceSenderInfo& voice_sender_info,
571 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800572 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
573 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100574 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
575 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100576 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800577 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
578 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200579 audio_track_stats->media_source_id =
580 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
581 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800582 audio_track_stats->remote_source = false;
583 audio_track_stats->detached = false;
Ivo Creusen56d46092017-11-24 17:29:59 +0100584 if (voice_sender_info.apm_statistics.echo_return_loss) {
585 audio_track_stats->echo_return_loss =
586 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800587 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100588 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
589 audio_track_stats->echo_return_loss_enhancement =
590 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800591 }
592 return audio_track_stats;
593}
594
595std::unique_ptr<RTCMediaStreamTrackStats>
596ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
597 int64_t timestamp_us,
598 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100599 const cricket::VoiceReceiverInfo& voice_receiver_info,
600 int attachment_id) {
601 // Since receiver tracks can't be reattached, we use the SSRC as
602 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800603 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
604 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100605 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
606 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100607 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800608 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
609 audio_track, audio_track_stats.get());
610 audio_track_stats->remote_source = true;
611 audio_track_stats->detached = false;
612 if (voice_receiver_info.audio_level >= 0) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200613 audio_track_stats->audio_level =
614 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
hbos9e302742017-01-20 02:47:10 -0800615 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200616 audio_track_stats->jitter_buffer_delay =
617 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100618 audio_track_stats->jitter_buffer_emitted_count =
619 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200620 audio_track_stats->inserted_samples_for_deceleration =
621 voice_receiver_info.inserted_samples_for_deceleration;
622 audio_track_stats->removed_samples_for_acceleration =
623 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 12:17:49 -0700624 audio_track_stats->total_audio_energy =
625 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700626 audio_track_stats->total_samples_received =
627 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700628 audio_track_stats->total_samples_duration =
629 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700630 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200631 audio_track_stats->silent_concealed_samples =
632 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200633 audio_track_stats->concealment_events =
634 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100635 audio_track_stats->jitter_buffer_flushes =
636 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100637 audio_track_stats->delayed_packet_outage_samples =
638 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100639 audio_track_stats->relative_packet_arrival_delay =
640 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200641 audio_track_stats->interruption_count =
642 voice_receiver_info.interruption_count >= 0
643 ? voice_receiver_info.interruption_count
644 : 0;
645 audio_track_stats->total_interruption_duration =
646 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
647 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800648 return audio_track_stats;
649}
650
651std::unique_ptr<RTCMediaStreamTrackStats>
652ProduceMediaStreamTrackStatsFromVideoSenderInfo(
653 int64_t timestamp_us,
654 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100655 const cricket::VideoSenderInfo& video_sender_info,
656 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800657 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
658 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100659 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 09:18:58 +0100660 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100661 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800662 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
663 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200664 video_track_stats->media_source_id =
665 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
666 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800667 video_track_stats->remote_source = false;
668 video_track_stats->detached = false;
Jonas Olssona4d87372019-07-05 19:08:33 +0200669 video_track_stats->frame_width =
670 static_cast<uint32_t>(video_sender_info.send_frame_width);
671 video_track_stats->frame_height =
672 static_cast<uint32_t>(video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800673 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100674 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800675 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100676 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800677 return video_track_stats;
678}
679
680std::unique_ptr<RTCMediaStreamTrackStats>
681ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
682 int64_t timestamp_us,
683 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100684 const cricket::VideoReceiverInfo& video_receiver_info,
685 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800686 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
687 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100688 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
689
690 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100691 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800692 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
693 video_track, video_track_stats.get());
694 video_track_stats->remote_source = true;
695 video_track_stats->detached = false;
696 if (video_receiver_info.frame_width > 0 &&
697 video_receiver_info.frame_height > 0) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200698 video_track_stats->frame_width =
699 static_cast<uint32_t>(video_receiver_info.frame_width);
700 video_track_stats->frame_height =
701 static_cast<uint32_t>(video_receiver_info.frame_height);
hbos9e302742017-01-20 02:47:10 -0800702 }
Guido Urdaneta67378412019-05-28 17:38:08 +0200703 video_track_stats->jitter_buffer_delay =
704 video_receiver_info.jitter_buffer_delay_seconds;
705 video_track_stats->jitter_buffer_emitted_count =
706 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 03:56:50 -0800707 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800708 // TODO(hbos): When we support receiving simulcast, this should be the total
709 // number of frames correctly decoded, independent of which SSRC it was
710 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100711 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800712 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
Johannes Kron0c141c52019-08-26 15:04:43 +0200713 video_track_stats->frames_dropped = video_receiver_info.frames_dropped;
Sergey Silkin02371062019-01-31 16:45:42 +0100714 video_track_stats->freeze_count = video_receiver_info.freeze_count;
715 video_track_stats->pause_count = video_receiver_info.pause_count;
716 video_track_stats->total_freezes_duration =
717 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
718 rtc::kNumMillisecsPerSec;
719 video_track_stats->total_pauses_duration =
720 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
721 rtc::kNumMillisecsPerSec;
722 video_track_stats->total_frames_duration =
723 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
724 rtc::kNumMillisecsPerSec;
725 video_track_stats->sum_squared_frame_durations =
726 video_receiver_info.sum_squared_frame_durations;
727
hbos9e302742017-01-20 02:47:10 -0800728 return video_track_stats;
729}
730
Harald Alvestrand89061872018-01-02 14:08:34 +0100731void ProduceSenderMediaTrackStats(
732 int64_t timestamp_us,
733 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800734 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100735 RTCStatsReport* report) {
736 // This function iterates over the senders to generate outgoing track stats.
737
738 // TODO(hbos): Return stats of detached tracks. We have to perform stats
739 // gathering at the time of detachment to get accurate stats and timestamps.
740 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100741 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100742 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
743 AudioTrackInterface* track =
744 static_cast<AudioTrackInterface*>(sender->track().get());
745 if (!track)
746 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100747 cricket::VoiceSenderInfo null_sender_info;
748 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
749 // TODO(hta): Checking on ssrc is not proper. There should be a way
750 // to see from a sender whether it's connected or not.
751 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800752 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100753 // When pc.close is called, sender info is discarded, so
754 // we generate zeroes instead. Bug: It should be retained.
755 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800756 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100757 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100758 if (sender_info) {
759 voice_sender_info = sender_info;
760 } else {
761 RTC_LOG(LS_INFO)
762 << "RTCStatsCollector: No voice sender info for sender with ssrc "
763 << sender->ssrc();
764 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100765 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100766 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100767 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
768 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100769 report->AddStats(std::move(audio_track_stats));
770 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
771 VideoTrackInterface* track =
772 static_cast<VideoTrackInterface*>(sender->track().get());
773 if (!track)
774 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100775 cricket::VideoSenderInfo null_sender_info;
776 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
777 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100778 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
779 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800780 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100781 // When pc.close is called, sender info is discarded, so
782 // we generate zeroes instead. Bug: It should be retained.
783 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800784 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100785 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100786 if (sender_info) {
787 video_sender_info = sender_info;
788 } else {
789 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
790 << sender->ssrc();
791 }
792 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100793 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100794 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
795 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100796 report->AddStats(std::move(video_track_stats));
797 } else {
798 RTC_NOTREACHED();
799 }
800 }
801}
802
803void ProduceReceiverMediaTrackStats(
804 int64_t timestamp_us,
805 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800806 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100807 RTCStatsReport* report) {
808 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100809 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100810 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
811 AudioTrackInterface* track =
812 static_cast<AudioTrackInterface*>(receiver->track().get());
813 const cricket::VoiceReceiverInfo* voice_receiver_info =
814 track_media_info_map.GetVoiceReceiverInfo(*track);
815 if (!voice_receiver_info) {
816 continue;
817 }
818 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
819 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100820 timestamp_us, *track, *voice_receiver_info,
821 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100822 report->AddStats(std::move(audio_track_stats));
823 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
824 VideoTrackInterface* track =
825 static_cast<VideoTrackInterface*>(receiver->track().get());
826 const cricket::VideoReceiverInfo* video_receiver_info =
827 track_media_info_map.GetVideoReceiverInfo(*track);
828 if (!video_receiver_info) {
829 continue;
830 }
831 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
832 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100833 timestamp_us, *track, *video_receiver_info,
834 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100835 report->AddStats(std::move(video_track_stats));
836 } else {
837 RTC_NOTREACHED();
838 }
839 }
840}
841
Henrik Boström5b3541f2018-03-19 13:52:56 +0100842rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
843 bool filter_by_sender_selector,
844 rtc::scoped_refptr<const RTCStatsReport> report,
845 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
846 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
847 std::vector<std::string> rtpstream_ids;
848 if (filter_by_sender_selector) {
849 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
850 if (sender_selector) {
851 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
852 // reference the sender stats.
853 // Because we do not implement sender stats, we look at outbound-rtp(s)
854 // that reference the track attachment stats for the sender instead.
855 std::string track_id =
856 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
857 kSender, sender_selector->AttachmentId());
858 for (const auto& stats : *report) {
859 if (stats.type() != RTCOutboundRTPStreamStats::kType)
860 continue;
861 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
862 if (outbound_rtp.track_id.is_defined() &&
863 *outbound_rtp.track_id == track_id) {
864 rtpstream_ids.push_back(outbound_rtp.id());
865 }
866 }
867 }
868 } else {
869 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
870 if (receiver_selector) {
871 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
872 // reference the receiver stats.
873 // Because we do not implement receiver stats, we look at inbound-rtp(s)
874 // that reference the track attachment stats for the receiver instead.
875 std::string track_id =
876 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
877 kReceiver, receiver_selector->AttachmentId());
878 for (const auto& stats : *report) {
879 if (stats.type() != RTCInboundRTPStreamStats::kType)
880 continue;
881 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
882 if (inbound_rtp.track_id.is_defined() &&
883 *inbound_rtp.track_id == track_id) {
884 rtpstream_ids.push_back(inbound_rtp.id());
885 }
886 }
887 }
888 }
889 if (rtpstream_ids.empty())
890 return RTCStatsReport::Create(report->timestamp_us());
891 return TakeReferencedStats(report->Copy(), rtpstream_ids);
892}
893
hboscc555c52016-10-18 12:48:31 -0700894} // namespace
895
Henrik Boström5b3541f2018-03-19 13:52:56 +0100896RTCStatsCollector::RequestInfo::RequestInfo(
897 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
898 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
899
900RTCStatsCollector::RequestInfo::RequestInfo(
901 rtc::scoped_refptr<RtpSenderInternal> selector,
902 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
903 : RequestInfo(FilterMode::kSenderSelector,
904 std::move(callback),
905 std::move(selector),
906 nullptr) {}
907
908RTCStatsCollector::RequestInfo::RequestInfo(
909 rtc::scoped_refptr<RtpReceiverInternal> selector,
910 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
911 : RequestInfo(FilterMode::kReceiverSelector,
912 std::move(callback),
913 nullptr,
914 std::move(selector)) {}
915
916RTCStatsCollector::RequestInfo::RequestInfo(
917 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
918 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
919 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
920 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
921 : filter_mode_(filter_mode),
922 callback_(std::move(callback)),
923 sender_selector_(std::move(sender_selector)),
924 receiver_selector_(std::move(receiver_selector)) {
925 RTC_DCHECK(callback_);
926 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
927}
928
hbosc82f2e12016-09-05 01:36:50 -0700929rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800930 PeerConnectionInternal* pc,
931 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700932 return rtc::scoped_refptr<RTCStatsCollector>(
933 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
934}
935
Steve Anton2d8609c2018-01-23 16:38:46 -0800936RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700937 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700938 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700939 signaling_thread_(pc->signaling_thread()),
940 worker_thread_(pc->worker_thread()),
941 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700942 num_pending_partial_reports_(0),
943 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100944 network_report_event_(true /* manual_reset */,
945 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700946 cache_timestamp_us_(0),
947 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700948 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700949 RTC_DCHECK(signaling_thread_);
950 RTC_DCHECK(worker_thread_);
951 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700952 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800953 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800954 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700955}
956
hbosb78306a2016-12-19 05:06:57 -0800957RTCStatsCollector::~RTCStatsCollector() {
958 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
959}
960
hbosc82f2e12016-09-05 01:36:50 -0700961void RTCStatsCollector::GetStatsReport(
962 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100963 GetStatsReportInternal(RequestInfo(std::move(callback)));
964}
965
966void RTCStatsCollector::GetStatsReport(
967 rtc::scoped_refptr<RtpSenderInternal> selector,
968 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
969 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
970}
971
972void RTCStatsCollector::GetStatsReport(
973 rtc::scoped_refptr<RtpReceiverInternal> selector,
974 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
975 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
976}
977
978void RTCStatsCollector::GetStatsReportInternal(
979 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700980 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100981 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700982
hbos0e6758d2016-08-31 07:57:36 -0700983 // "Now" using a monotonically increasing timer.
984 int64_t cache_now_us = rtc::TimeMicros();
985 if (cached_report_ &&
986 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800987 // We have a fresh cached report to deliver. Deliver asynchronously, since
988 // the caller may not be expecting a synchronous callback, and it avoids
989 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100990 std::vector<RequestInfo> requests;
991 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100992 signaling_thread_->PostTask(
993 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
994 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700995 } else if (!num_pending_partial_reports_) {
996 // Only start gathering stats if we're not already gathering stats. In the
997 // case of already gathering stats, |callback_| will be invoked when there
998 // are no more pending partial reports.
999
1000 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
1001 // UTC), in microseconds. The system clock could be modified and is not
1002 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -07001003 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -07001004
hbosf415f8a2017-01-02 04:28:51 -08001005 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -07001006 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -08001007
Steve Anton57858b32018-02-15 15:19:50 -08001008 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -08001009 // |ProducePartialResultsOnNetworkThread| and
1010 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -08001011 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -07001012 // Prepare |transport_names_| for use in
1013 // |ProducePartialResultsOnNetworkThread|.
1014 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -08001015
stefanf79ade12017-06-02 06:44:03 -07001016 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
1017 // thread.
1018 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
1019 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -07001020 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -07001021
Henrik Boström40b030e2019-02-28 09:49:31 +01001022 // Don't touch |network_report_| on the signaling thread until
1023 // ProducePartialResultsOnNetworkThread() has signaled the
1024 // |network_report_event_|.
1025 network_report_event_.Reset();
1026 network_thread_->PostTask(
1027 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -07001028 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +01001029 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -08001030 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -07001031 }
hbosd565b732016-08-30 14:04:35 -07001032}
1033
1034void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -07001035 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001036 cached_report_ = nullptr;
1037}
1038
hbosb78306a2016-12-19 05:06:57 -08001039void RTCStatsCollector::WaitForPendingRequest() {
1040 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +01001041 // If a request is pending, blocks until the |network_report_event_| is
1042 // signaled and then delivers the result. Otherwise this is a NO-OP.
1043 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -08001044}
1045
hbosc82f2e12016-09-05 01:36:50 -07001046void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1047 int64_t timestamp_us) {
1048 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +01001049 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -07001050
Henrik Boström40b030e2019-02-28 09:49:31 +01001051 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1052 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -07001053
Henrik Boström40b030e2019-02-28 09:49:31 +01001054 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1055 // signaling thread, so it is always the first partial result delivered on the
1056 // signaling thread. The request is not complete until MergeNetworkReport_s()
1057 // happens; we don't have to do anything here.
1058 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1059 --num_pending_partial_reports_;
1060}
1061
1062void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1063 int64_t timestamp_us,
1064 RTCStatsReport* partial_report) {
1065 RTC_DCHECK(signaling_thread_->IsCurrent());
1066 ProduceDataChannelStats_s(timestamp_us, partial_report);
1067 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1068 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 15:49:42 +02001069 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001070 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -07001071}
1072
hbosc82f2e12016-09-05 01:36:50 -07001073void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
1074 int64_t timestamp_us) {
1075 RTC_DCHECK(network_thread_->IsCurrent());
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +02001076 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 09:49:31 +01001077 // |network_report_event_| is reset before this method is invoked.
1078 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -07001079
Steve Anton5dfde182018-02-06 10:34:40 -08001080 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -07001081 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -08001082 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1083 PrepareTransportCertificateStats_n(transport_stats_by_name);
1084
Henrik Boström40b030e2019-02-28 09:49:31 +01001085 ProducePartialResultsOnNetworkThreadImpl(
1086 timestamp_us, transport_stats_by_name, transport_cert_stats,
1087 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001088
Henrik Boström40b030e2019-02-28 09:49:31 +01001089 // Signal that it is now safe to touch |network_report_| on the signaling
1090 // thread, and post a task to merge it into the final results.
1091 network_report_event_.Set();
1092 signaling_thread_->PostTask(
1093 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +01001094}
1095
Henrik Boström40b030e2019-02-28 09:49:31 +01001096void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1097 int64_t timestamp_us,
1098 const std::map<std::string, cricket::TransportStats>&
1099 transport_stats_by_name,
1100 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1101 RTCStatsReport* partial_report) {
1102 RTC_DCHECK(network_thread_->IsCurrent());
1103 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1104 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1105 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1106 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001107 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1108 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 13:40:25 +02001109 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1110 partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001111}
1112
1113void RTCStatsCollector::MergeNetworkReport_s() {
1114 RTC_DCHECK(signaling_thread_->IsCurrent());
1115 // The |network_report_event_| must be signaled for it to be safe to touch
1116 // |network_report_|. This is normally not blocking, but if
1117 // WaitForPendingRequest() is called while a request is pending, we might have
1118 // to wait until the network thread is done touching |network_report_|.
1119 network_report_event_.Wait(rtc::Event::kForever);
1120 if (!network_report_) {
1121 // Normally, MergeNetworkReport_s() is executed because it is posted from
1122 // the network thread. But if WaitForPendingRequest() is called while a
1123 // request is pending, an early call to MergeNetworkReport_s() is made,
1124 // merging the report and setting |network_report_| to null. If so, when the
1125 // previously posted MergeNetworkReport_s() is later executed, the report is
1126 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -07001127 return;
1128 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001129 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +01001130 RTC_DCHECK(partial_report_);
1131 partial_report_->TakeMembersFrom(network_report_);
1132 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001133 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +01001134 // |network_report_| is currently the only partial report collected
1135 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1136 // ready to deliver the result.
1137 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1138 cache_timestamp_us_ = partial_report_timestamp_us_;
1139 cached_report_ = partial_report_;
1140 partial_report_ = nullptr;
1141 transceiver_stats_infos_.clear();
1142 // Trace WebRTC Stats when getStats is called on Javascript.
1143 // This allows access to WebRTC stats from trace logs. To enable them,
1144 // select the "webrtc_stats" category when recording traces.
1145 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1146 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001147
Henrik Boström40b030e2019-02-28 09:49:31 +01001148 // Deliver report and clear |requests_|.
1149 std::vector<RequestInfo> requests;
1150 requests.swap(requests_);
1151 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001152}
1153
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001154void RTCStatsCollector::DeliverCachedReport(
1155 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001156 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001157 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001158 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001159 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001160
Henrik Boström5b3541f2018-03-19 13:52:56 +01001161 for (const RequestInfo& request : requests) {
1162 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1163 request.callback()->OnStatsDelivered(cached_report);
1164 } else {
1165 bool filter_by_sender_selector;
1166 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1167 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1168 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1169 filter_by_sender_selector = true;
1170 sender_selector = request.sender_selector();
1171 } else {
1172 RTC_DCHECK(request.filter_mode() ==
1173 RequestInfo::FilterMode::kReceiverSelector);
1174 filter_by_sender_selector = false;
1175 receiver_selector = request.receiver_selector();
1176 }
1177 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1178 filter_by_sender_selector, cached_report, sender_selector,
1179 receiver_selector));
1180 }
hbosc82f2e12016-09-05 01:36:50 -07001181 }
hbosd565b732016-08-30 14:04:35 -07001182}
1183
hbosdf6075a2016-12-19 04:58:02 -08001184void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001185 int64_t timestamp_us,
1186 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001187 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001188 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001189 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1190 if (transport_cert_stats_pair.second.local) {
1191 ProduceCertificateStatsFromSSLCertificateStats(
1192 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001193 }
hbos02ba2112016-10-28 05:14:53 -07001194 if (transport_cert_stats_pair.second.remote) {
1195 ProduceCertificateStatsFromSSLCertificateStats(
1196 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001197 }
1198 }
1199}
1200
hbosdf6075a2016-12-19 04:58:02 -08001201void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001202 int64_t timestamp_us,
1203 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001204 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001205 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001206 for (const auto& stats : transceiver_stats_infos) {
1207 if (!stats.mid) {
1208 continue;
hbos0adb8282016-11-23 02:32:06 -08001209 }
Steve Anton57858b32018-02-15 15:19:50 -08001210 const cricket::VoiceMediaInfo* voice_media_info =
1211 stats.track_media_info_map->voice_media_info();
1212 const cricket::VideoMediaInfo* video_media_info =
1213 stats.track_media_info_map->video_media_info();
1214 // Audio
1215 if (voice_media_info) {
1216 // Inbound
1217 for (const auto& pair : voice_media_info->receive_codecs) {
1218 report->AddStats(CodecStatsFromRtpCodecParameters(
1219 timestamp_us, *stats.mid, true, pair.second));
1220 }
1221 // Outbound
1222 for (const auto& pair : voice_media_info->send_codecs) {
1223 report->AddStats(CodecStatsFromRtpCodecParameters(
1224 timestamp_us, *stats.mid, false, pair.second));
1225 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001226 }
Steve Anton57858b32018-02-15 15:19:50 -08001227 // Video
1228 if (video_media_info) {
1229 // Inbound
1230 for (const auto& pair : video_media_info->receive_codecs) {
1231 report->AddStats(CodecStatsFromRtpCodecParameters(
1232 timestamp_us, *stats.mid, true, pair.second));
1233 }
1234 // Outbound
1235 for (const auto& pair : video_media_info->send_codecs) {
1236 report->AddStats(CodecStatsFromRtpCodecParameters(
1237 timestamp_us, *stats.mid, false, pair.second));
1238 }
hbos0adb8282016-11-23 02:32:06 -08001239 }
1240 }
1241}
1242
hboscc555c52016-10-18 12:48:31 -07001243void RTCStatsCollector::ProduceDataChannelStats_s(
Jonas Olssona4d87372019-07-05 19:08:33 +02001244 int64_t timestamp_us,
1245 RTCStatsReport* report) const {
hboscc555c52016-10-18 12:48:31 -07001246 RTC_DCHECK(signaling_thread_->IsCurrent());
1247 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1248 pc_->sctp_data_channels()) {
1249 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1250 new RTCDataChannelStats(
Harald Alvestrand928e7a32019-07-31 07:16:45 -04001251 "RTCDataChannel_" + rtc::ToString(data_channel->internal_id()),
hboscc555c52016-10-18 12:48:31 -07001252 timestamp_us));
1253 data_channel_stats->label = data_channel->label();
1254 data_channel_stats->protocol = data_channel->protocol();
1255 data_channel_stats->datachannelid = data_channel->id();
1256 data_channel_stats->state =
1257 DataStateToRTCDataChannelState(data_channel->state());
1258 data_channel_stats->messages_sent = data_channel->messages_sent();
1259 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1260 data_channel_stats->messages_received = data_channel->messages_received();
1261 data_channel_stats->bytes_received = data_channel->bytes_received();
1262 report->AddStats(std::move(data_channel_stats));
1263 }
1264}
1265
hbosdf6075a2016-12-19 04:58:02 -08001266void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001267 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001268 const std::map<std::string, cricket::TransportStats>&
1269 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001270 const Call::Stats& call_stats,
1271 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001272 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001273 for (const auto& entry : transport_stats_by_name) {
1274 const std::string& transport_name = entry.first;
1275 const cricket::TransportStats& transport_stats = entry.second;
1276 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001277 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001278 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001279 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 08:10:27 +02001280 channel_stats.ice_transport_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001281 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001282 new RTCIceCandidatePairStats(
1283 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1284 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001285
hbos0583b282016-11-30 01:50:14 -08001286 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001287 // TODO(hbos): There could be other candidates that are not paired with
1288 // anything. We don't have a complete list. Local candidates come from
1289 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001290 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001291 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001292 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001293 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001294 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001295 candidate_pair_stats->state =
1296 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1297 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001298 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001299 // TODO(hbos): This writable is different than the spec. It goes to
1300 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001301 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001302 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001303 candidate_pair_stats->bytes_sent =
1304 static_cast<uint64_t>(info.sent_total_bytes);
1305 candidate_pair_stats->bytes_received =
1306 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001307 candidate_pair_stats->total_round_trip_time =
1308 static_cast<double>(info.total_round_trip_time_ms) /
1309 rtc::kNumMillisecsPerSec;
1310 if (info.current_round_trip_time_ms) {
1311 candidate_pair_stats->current_round_trip_time =
1312 static_cast<double>(*info.current_round_trip_time_ms) /
1313 rtc::kNumMillisecsPerSec;
1314 }
stefanf79ade12017-06-02 06:44:03 -07001315 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001316 // The bandwidth estimations we have are for the selected candidate
1317 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001318 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1319 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1320 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001321 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001322 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001323 }
stefanf79ade12017-06-02 06:44:03 -07001324 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001325 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001326 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001327 }
1328 }
hbosd82f5122016-12-09 04:12:39 -08001329 candidate_pair_stats->requests_received =
1330 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001331 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1332 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001333 candidate_pair_stats->responses_received =
1334 static_cast<uint64_t>(info.recv_ping_responses);
1335 candidate_pair_stats->responses_sent =
1336 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001337 RTC_DCHECK_GE(info.sent_ping_requests_total,
1338 info.sent_ping_requests_before_first_response);
1339 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1340 info.sent_ping_requests_total -
1341 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001342
1343 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001344 }
1345 }
1346 }
1347}
1348
Steve Anton57858b32018-02-15 15:19:50 -08001349void RTCStatsCollector::ProduceMediaStreamStats_s(
1350 int64_t timestamp_us,
1351 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001352 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001353
1354 std::map<std::string, std::vector<std::string>> track_ids;
1355
1356 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001357 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001358 std::string track_id =
1359 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1360 kSender, sender->internal()->AttachmentId());
1361 for (auto& stream_id : sender->stream_ids()) {
1362 track_ids[stream_id].push_back(track_id);
1363 }
1364 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001365 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001366 std::string track_id =
1367 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1368 kReceiver, receiver->internal()->AttachmentId());
1369 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001370 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001371 }
1372 }
1373 }
1374
1375 // Build stats for each stream ID known.
1376 for (auto& it : track_ids) {
1377 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1378 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1379 stream_stats->stream_identifier = it.first;
1380 stream_stats->track_ids = it.second;
1381 report->AddStats(std::move(stream_stats));
1382 }
1383}
1384
1385void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1386 int64_t timestamp_us,
1387 RTCStatsReport* report) const {
1388 RTC_DCHECK(signaling_thread_->IsCurrent());
1389 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1390 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001391 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001392 senders.push_back(sender->internal());
1393 }
1394 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1395 senders, report);
1396
1397 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001398 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001399 receivers.push_back(receiver->internal());
1400 }
1401 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1402 receivers, report);
1403 }
hbos09bc1282016-11-08 06:29:22 -08001404}
1405
Henrik Boström646fda02019-05-22 15:49:42 +02001406void RTCStatsCollector::ProduceMediaSourceStats_s(
1407 int64_t timestamp_us,
1408 RTCStatsReport* report) const {
1409 RTC_DCHECK(signaling_thread_->IsCurrent());
1410 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1411 transceiver_stats_infos_) {
1412 const auto& track_media_info_map =
1413 transceiver_stats_info.track_media_info_map;
1414 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1415 const auto& sender_internal = sender->internal();
1416 const auto& track = sender_internal->track();
1417 if (!track)
1418 continue;
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001419 // TODO(https://crbug.com/webrtc/10771): The same track could be attached
1420 // to multiple senders which should result in multiple senders referencing
1421 // the same media-source stats. When all media source related metrics are
1422 // moved to the track's source (e.g. input frame rate is moved from
1423 // cricket::VideoSenderInfo to VideoTrackSourceInterface::Stats and audio
1424 // levels are moved to the corresponding audio track/source object), don't
1425 // create separate media source stats objects on a per-attachment basis.
Henrik Boström646fda02019-05-22 15:49:42 +02001426 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1427 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001428 auto audio_source_stats = std::make_unique<RTCAudioSourceStats>(
Henrik Boström646fda02019-05-22 15:49:42 +02001429 RTCMediaSourceStatsIDFromKindAndAttachment(
1430 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1431 timestamp_us);
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001432 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1433 // SSRC assigned (there shouldn't need to exist a send-stream, created
1434 // by an O/A exchange) in order to read audio media-source stats.
1435 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1436 // value indicating no SSRC.
1437 if (sender_internal->ssrc() != 0) {
1438 auto* voice_sender_info =
1439 track_media_info_map->GetVoiceSenderInfoBySsrc(
1440 sender_internal->ssrc());
1441 if (voice_sender_info) {
1442 audio_source_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
1443 voice_sender_info->audio_level);
1444 audio_source_stats->total_audio_energy =
1445 voice_sender_info->total_input_energy;
1446 audio_source_stats->total_samples_duration =
1447 voice_sender_info->total_input_duration;
1448 }
1449 }
1450 media_source_stats = std::move(audio_source_stats);
Henrik Boström646fda02019-05-22 15:49:42 +02001451 } else {
1452 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001453 auto video_source_stats = std::make_unique<RTCVideoSourceStats>(
Henrik Boström646fda02019-05-22 15:49:42 +02001454 RTCMediaSourceStatsIDFromKindAndAttachment(
1455 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1456 timestamp_us);
1457 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1458 auto* video_source = video_track->GetSource();
1459 VideoTrackSourceInterface::Stats source_stats;
1460 if (video_source && video_source->GetStats(&source_stats)) {
1461 video_source_stats->width = source_stats.input_width;
1462 video_source_stats->height = source_stats.input_height;
1463 }
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001464 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1465 // SSRC assigned (there shouldn't need to exist a send-stream, created
1466 // by an O/A exchange) in order to get framesPerSecond.
1467 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1468 // value indicating no SSRC.
Henrik Boström646fda02019-05-22 15:49:42 +02001469 if (sender_internal->ssrc() != 0) {
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001470 auto* video_sender_info =
1471 track_media_info_map->GetVideoSenderInfoBySsrc(
1472 sender_internal->ssrc());
1473 if (video_sender_info) {
Henrik Boström646fda02019-05-22 15:49:42 +02001474 video_source_stats->frames_per_second =
Henrik Boströmd2c336f2019-07-03 17:11:10 +02001475 video_sender_info->framerate_input;
Henrik Boström646fda02019-05-22 15:49:42 +02001476 }
1477 }
1478 media_source_stats = std::move(video_source_stats);
1479 }
1480 media_source_stats->track_identifier = track->id();
1481 media_source_stats->kind = track->kind();
1482 report->AddStats(std::move(media_source_stats));
1483 }
1484 }
1485}
1486
hbos6ab97ce2016-10-03 14:16:56 -07001487void RTCStatsCollector::ProducePeerConnectionStats_s(
Jonas Olssona4d87372019-07-05 19:08:33 +02001488 int64_t timestamp_us,
1489 RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001490 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001491 std::unique_ptr<RTCPeerConnectionStats> stats(
Jonas Olssona4d87372019-07-05 19:08:33 +02001492 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001493 stats->data_channels_opened = internal_record_.data_channels_opened;
1494 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001495 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001496}
1497
hbosdf6075a2016-12-19 04:58:02 -08001498void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001499 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001500 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001501 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001502 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001503
Steve Anton57858b32018-02-15 15:19:50 -08001504 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1505 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1506 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1507 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1508 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1509 } else {
1510 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001511 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001512 }
Steve Anton57858b32018-02-15 15:19:50 -08001513}
1514
1515void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1516 int64_t timestamp_us,
1517 const RtpTransceiverStatsInfo& stats,
1518 RTCStatsReport* report) const {
1519 if (!stats.mid || !stats.transport_name) {
1520 return;
1521 }
1522 RTC_DCHECK(stats.track_media_info_map);
1523 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1524 RTC_DCHECK(track_media_info_map.voice_media_info());
1525 std::string mid = *stats.mid;
1526 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1527 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1528 // Inbound
1529 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1530 track_media_info_map.voice_media_info()->receivers) {
1531 if (!voice_receiver_info.connected())
1532 continue;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001533 auto inbound_audio = std::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001534 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1535 timestamp_us);
1536 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1537 inbound_audio.get());
1538 // TODO(hta): This lookup should look for the sender, not the track.
1539 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1540 track_media_info_map.GetAudioTrack(voice_receiver_info);
1541 if (audio_track) {
1542 inbound_audio->track_id =
1543 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1544 kReceiver,
1545 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001546 }
Steve Anton57858b32018-02-15 15:19:50 -08001547 inbound_audio->transport_id = transport_id;
1548 report->AddStats(std::move(inbound_audio));
1549 }
1550 // Outbound
Henrik Boström4f40fa52019-12-19 13:27:27 +01001551 std::map<std::string, RTCOutboundRTPStreamStats*> audio_outbound_rtps;
Steve Anton57858b32018-02-15 15:19:50 -08001552 for (const cricket::VoiceSenderInfo& voice_sender_info :
1553 track_media_info_map.voice_media_info()->senders) {
1554 if (!voice_sender_info.connected())
1555 continue;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001556 auto outbound_audio = std::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001557 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1558 timestamp_us);
1559 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1560 outbound_audio.get());
1561 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1562 track_media_info_map.GetAudioTrack(voice_sender_info);
1563 if (audio_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001564 int attachment_id =
1565 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001566 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001567 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1568 attachment_id);
1569 outbound_audio->media_source_id =
1570 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1571 attachment_id);
Steve Anton56bae8d2018-02-14 16:07:42 -08001572 }
Steve Anton57858b32018-02-15 15:19:50 -08001573 outbound_audio->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 13:27:27 +01001574 audio_outbound_rtps.insert(
1575 std::make_pair(outbound_audio->id(), outbound_audio.get()));
Steve Anton57858b32018-02-15 15:19:50 -08001576 report->AddStats(std::move(outbound_audio));
1577 }
Henrik Boström883eefc2019-05-27 13:40:25 +02001578 // Remote-inbound
1579 // These are Report Block-based, information sent from the remote endpoint,
1580 // providing metrics about our Outbound streams. We take advantage of the fact
1581 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1582 // been added to the report.
1583 for (const cricket::VoiceSenderInfo& voice_sender_info :
1584 track_media_info_map.voice_media_info()->senders) {
1585 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1586 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Henrik Boström4f40fa52019-12-19 13:27:27 +01001587 report_block_data, cricket::MEDIA_TYPE_AUDIO,
1588 std::move(audio_outbound_rtps), *report));
Henrik Boström883eefc2019-05-27 13:40:25 +02001589 }
1590 }
Steve Anton57858b32018-02-15 15:19:50 -08001591}
1592
1593void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1594 int64_t timestamp_us,
1595 const RtpTransceiverStatsInfo& stats,
1596 RTCStatsReport* report) const {
1597 if (!stats.mid || !stats.transport_name) {
1598 return;
1599 }
1600 RTC_DCHECK(stats.track_media_info_map);
1601 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1602 RTC_DCHECK(track_media_info_map.video_media_info());
1603 std::string mid = *stats.mid;
1604 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1605 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1606 // Inbound
1607 for (const cricket::VideoReceiverInfo& video_receiver_info :
1608 track_media_info_map.video_media_info()->receivers) {
1609 if (!video_receiver_info.connected())
1610 continue;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001611 auto inbound_video = std::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001612 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1613 timestamp_us);
1614 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1615 inbound_video.get());
1616 rtc::scoped_refptr<VideoTrackInterface> video_track =
1617 track_media_info_map.GetVideoTrack(video_receiver_info);
1618 if (video_track) {
1619 inbound_video->track_id =
1620 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1621 kReceiver,
1622 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1623 }
1624 inbound_video->transport_id = transport_id;
1625 report->AddStats(std::move(inbound_video));
1626 }
1627 // Outbound
Henrik Boström4f40fa52019-12-19 13:27:27 +01001628 std::map<std::string, RTCOutboundRTPStreamStats*> video_outbound_rtps;
Steve Anton57858b32018-02-15 15:19:50 -08001629 for (const cricket::VideoSenderInfo& video_sender_info :
1630 track_media_info_map.video_media_info()->senders) {
1631 if (!video_sender_info.connected())
1632 continue;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001633 auto outbound_video = std::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001634 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1635 timestamp_us);
1636 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1637 outbound_video.get());
1638 rtc::scoped_refptr<VideoTrackInterface> video_track =
1639 track_media_info_map.GetVideoTrack(video_sender_info);
1640 if (video_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001641 int attachment_id =
1642 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001643 outbound_video->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001644 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1645 attachment_id);
1646 outbound_video->media_source_id =
1647 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1648 attachment_id);
Steve Anton57858b32018-02-15 15:19:50 -08001649 }
1650 outbound_video->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 13:27:27 +01001651 video_outbound_rtps.insert(
1652 std::make_pair(outbound_video->id(), outbound_video.get()));
Steve Anton57858b32018-02-15 15:19:50 -08001653 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001654 }
Henrik Boström883eefc2019-05-27 13:40:25 +02001655 // Remote-inbound
1656 // These are Report Block-based, information sent from the remote endpoint,
1657 // providing metrics about our Outbound streams. We take advantage of the fact
1658 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1659 // been added to the report.
1660 for (const cricket::VideoSenderInfo& video_sender_info :
1661 track_media_info_map.video_media_info()->senders) {
1662 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1663 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Henrik Boström4f40fa52019-12-19 13:27:27 +01001664 report_block_data, cricket::MEDIA_TYPE_VIDEO,
1665 std::move(video_outbound_rtps), *report));
Henrik Boström883eefc2019-05-27 13:40:25 +02001666 }
1667 }
hbos6ded1902016-11-01 01:50:46 -07001668}
1669
hbosdf6075a2016-12-19 04:58:02 -08001670void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001671 int64_t timestamp_us,
1672 const std::map<std::string, cricket::TransportStats>&
1673 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001674 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1675 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001676 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001677 for (const auto& entry : transport_stats_by_name) {
1678 const std::string& transport_name = entry.first;
1679 const cricket::TransportStats& transport_stats = entry.second;
1680
hbos2fa7c672016-10-24 04:00:05 -07001681 // Get reference to RTCP channel, if it exists.
1682 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001683 for (const cricket::TransportChannelStats& channel_stats :
1684 transport_stats.channel_stats) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001685 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
hbos2fa7c672016-10-24 04:00:05 -07001686 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001687 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001688 break;
1689 }
1690 }
1691
1692 // Get reference to local and remote certificates of this transport, if they
1693 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001694 const auto& certificate_stats_it =
1695 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001696 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1697 std::string local_certificate_id;
1698 if (certificate_stats_it->second.local) {
1699 local_certificate_id = RTCCertificateIDFromFingerprint(
1700 certificate_stats_it->second.local->fingerprint);
1701 }
1702 std::string remote_certificate_id;
1703 if (certificate_stats_it->second.remote) {
1704 remote_certificate_id = RTCCertificateIDFromFingerprint(
1705 certificate_stats_it->second.remote->fingerprint);
1706 }
1707
1708 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001709 for (const cricket::TransportChannelStats& channel_stats :
1710 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001711 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001712 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1713 transport_name, channel_stats.component),
1714 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001715 transport_stats->bytes_sent = 0;
1716 transport_stats->bytes_received = 0;
Jonas Olssona4d87372019-07-05 19:08:33 +02001717 transport_stats->dtls_state =
1718 DtlsTransportStateToRTCDtlsTransportState(channel_stats.dtls_state);
Jonas Oreland149dc722019-08-28 08:10:27 +02001719 transport_stats->selected_candidate_pair_changes =
1720 channel_stats.ice_transport_stats.selected_candidate_pair_changes;
hbos2fa7c672016-10-24 04:00:05 -07001721 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 08:10:27 +02001722 channel_stats.ice_transport_stats.connection_infos) {
hbos2fa7c672016-10-24 04:00:05 -07001723 *transport_stats->bytes_sent += info.sent_total_bytes;
1724 *transport_stats->bytes_received += info.recv_total_bytes;
1725 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001726 transport_stats->selected_candidate_pair_id =
1727 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1728 }
1729 }
1730 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1731 !rtcp_transport_stats_id.empty()) {
1732 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1733 }
1734 if (!local_certificate_id.empty())
1735 transport_stats->local_certificate_id = local_certificate_id;
1736 if (!remote_certificate_id.empty())
1737 transport_stats->remote_certificate_id = remote_certificate_id;
Harald Alvestrand5cb78072019-10-28 09:51:17 +01001738 // Crypto information
1739 if (channel_stats.ssl_version_bytes) {
1740 char bytes[5];
1741 snprintf(bytes, sizeof(bytes), "%04X", channel_stats.ssl_version_bytes);
1742 transport_stats->tls_version = bytes;
1743 }
1744 if (channel_stats.ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
1745 rtc::SSLStreamAdapter::SslCipherSuiteToName(
1746 channel_stats.ssl_cipher_suite)
1747 .length()) {
1748 transport_stats->dtls_cipher =
1749 rtc::SSLStreamAdapter::SslCipherSuiteToName(
1750 channel_stats.ssl_cipher_suite);
1751 }
1752 if (channel_stats.srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
1753 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite)
1754 .length()) {
1755 transport_stats->srtp_cipher =
1756 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite);
1757 }
hbos2fa7c672016-10-24 04:00:05 -07001758 report->AddStats(std::move(transport_stats));
1759 }
1760 }
1761}
1762
1763std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001764RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001765 const std::map<std::string, cricket::TransportStats>&
1766 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001767 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001768 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001769 for (const auto& entry : transport_stats_by_name) {
1770 const std::string& transport_name = entry.first;
1771
hbos2fa7c672016-10-24 04:00:05 -07001772 CertificateStatsPair certificate_stats_pair;
1773 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001774 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001775 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001776 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001777 }
Steve Anton5dfde182018-02-06 10:34:40 -08001778
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001779 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1780 pc_->GetRemoteSSLCertChain(transport_name);
1781 if (remote_cert_chain) {
1782 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001783 }
Steve Anton5dfde182018-02-06 10:34:40 -08001784
hbos2fa7c672016-10-24 04:00:05 -07001785 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001786 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001787 }
1788 return transport_cert_stats;
1789}
1790
Steve Anton57858b32018-02-15 15:19:50 -08001791std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1792RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1793 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001794
Steve Anton57858b32018-02-15 15:19:50 -08001795 // These are used to invoke GetStats for all the media channels together in
1796 // one worker thread hop.
1797 std::map<cricket::VoiceMediaChannel*,
1798 std::unique_ptr<cricket::VoiceMediaInfo>>
1799 voice_stats;
1800 std::map<cricket::VideoMediaChannel*,
1801 std::unique_ptr<cricket::VideoMediaInfo>>
1802 video_stats;
1803
Mirko Bonadei739baf02019-01-27 17:29:42 +01001804 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001805 cricket::MediaType media_type = transceiver->media_type();
1806
1807 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1808 // stats have been fetched on the worker thread.
1809 transceiver_stats_infos.emplace_back();
1810 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1811 stats.transceiver = transceiver->internal();
1812 stats.media_type = media_type;
1813
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001814 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001815 if (!channel) {
1816 // The remaining fields require a BaseChannel.
1817 continue;
1818 }
1819
1820 stats.mid = channel->content_name();
1821 stats.transport_name = channel->transport_name();
1822
1823 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1824 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1825 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1826 voice_stats.end());
1827 voice_stats[voice_channel->media_channel()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001828 std::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001829 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1830 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1831 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1832 video_stats.end());
1833 video_stats[video_channel->media_channel()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001834 std::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001835 } else {
1836 RTC_NOTREACHED();
1837 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001838 }
Steve Anton57858b32018-02-15 15:19:50 -08001839
1840 // Call GetStats for all media channels together on the worker thread in one
1841 // hop.
1842 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1843 for (const auto& entry : voice_stats) {
1844 if (!entry.first->GetStats(entry.second.get())) {
1845 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1846 }
1847 }
1848 for (const auto& entry : video_stats) {
1849 if (!entry.first->GetStats(entry.second.get())) {
1850 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1851 }
1852 }
1853 });
1854
1855 // Create the TrackMediaInfoMap for each transceiver stats object.
1856 for (auto& stats : transceiver_stats_infos) {
1857 auto transceiver = stats.transceiver;
1858 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1859 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1860 if (transceiver->channel()) {
1861 cricket::MediaType media_type = transceiver->media_type();
1862 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1863 auto* voice_channel =
1864 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1865 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1866 voice_media_info =
1867 std::move(voice_stats[voice_channel->media_channel()]);
1868 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1869 auto* video_channel =
1870 static_cast<cricket::VideoChannel*>(transceiver->channel());
1871 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1872 video_media_info =
1873 std::move(video_stats[video_channel->media_channel()]);
1874 }
1875 }
1876 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001877 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001878 senders.push_back(sender->internal());
1879 }
1880 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001881 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001882 receivers.push_back(receiver->internal());
1883 }
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001884 stats.track_media_info_map = std::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001885 std::move(voice_media_info), std::move(video_media_info), senders,
1886 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001887 }
Steve Anton57858b32018-02-15 15:19:50 -08001888
1889 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001890}
1891
Steve Anton7eca0932018-03-30 15:18:41 -07001892std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1893 std::set<std::string> transport_names;
1894 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1895 if (transceiver->internal()->channel()) {
1896 transport_names.insert(
1897 transceiver->internal()->channel()->transport_name());
1898 }
1899 }
1900 if (pc_->rtp_data_channel()) {
1901 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1902 }
1903 if (pc_->sctp_transport_name()) {
1904 transport_names.insert(*pc_->sctp_transport_name());
1905 }
1906 return transport_names;
1907}
1908
hbos82ebe022016-11-14 01:41:09 -08001909void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1910 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1911 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1912}
1913
1914void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1915 RTC_DCHECK(signaling_thread_->IsCurrent());
Jonas Olssona4d87372019-07-05 19:08:33 +02001916 bool result = internal_record_.opened_data_channels
1917 .insert(reinterpret_cast<uintptr_t>(channel))
1918 .second;
hbos82ebe022016-11-14 01:41:09 -08001919 ++internal_record_.data_channels_opened;
1920 RTC_DCHECK(result);
1921}
1922
1923void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1924 RTC_DCHECK(signaling_thread_->IsCurrent());
1925 // Only channels that have been fully opened (and have increased the
1926 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001927 if (internal_record_.opened_data_channels.erase(
1928 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001929 ++internal_record_.data_channels_closed;
1930 }
1931}
1932
hboscc555c52016-10-18 12:48:31 -07001933const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1934 const std::string& type) {
1935 return CandidateTypeToRTCIceCandidateType(type);
1936}
1937
1938const char* DataStateToRTCDataChannelStateForTesting(
1939 DataChannelInterface::DataState state) {
1940 return DataStateToRTCDataChannelState(state);
1941}
1942
hbosd565b732016-08-30 14:04:35 -07001943} // namespace webrtc