blob: 5a741a16cdc2b3b65b42593fa27dca5291543c28 [file] [log] [blame]
hbosd565b732016-08-30 21:04:351/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/rtc_stats_collector.h"
hbosd565b732016-08-30 21:04:3512
Harald Alvestrand5761e7b2021-01-29 14:45:0813#include <stdio.h>
Artem Titovd15a5752021-02-10 13:31:2414
Harald Alvestrand5761e7b2021-01-29 14:45:0815#include <algorithm>
16#include <cstdint>
Johannes Kron72d69152020-02-10 13:05:5517#include <map>
hbosd565b732016-08-30 21:04:3518#include <memory>
Steve Anton36b29d12017-10-30 16:57:4219#include <string>
hbosd565b732016-08-30 21:04:3520#include <utility>
21#include <vector>
22
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "api/array_view.h"
Patrik Höglunde2d6a062017-10-05 12:53:3324#include "api/candidate.h"
Steve Anton10542f22019-01-11 17:11:0025#include "api/media_stream_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0826#include "api/rtp_parameters.h"
27#include "api/rtp_receiver_interface.h"
28#include "api/rtp_sender_interface.h"
Artem Titovd15a5752021-02-10 13:31:2429#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0830#include "api/stats/rtc_stats.h"
31#include "api/stats/rtcstats_objects.h"
32#include "api/task_queue/queued_task.h"
Henrik Boström2e069262019-04-09 11:59:3133#include "api/video/video_content_type.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0834#include "common_video/include/quality_limitation_reason.h"
Steve Anton10542f22019-01-11 17:11:0035#include "media/base/media_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0836#include "modules/audio_processing/include/audio_processing_statistics.h"
37#include "modules/rtp_rtcp/include/report_block_data.h"
38#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
39#include "p2p/base/connection_info.h"
40#include "p2p/base/dtls_transport_internal.h"
41#include "p2p/base/ice_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0042#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3143#include "p2p/base/port.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0844#include "pc/channel.h"
45#include "pc/channel_interface.h"
46#include "pc/data_channel_utils.h"
Steve Anton10542f22019-01-11 17:11:0047#include "pc/rtc_stats_traversal.h"
Johannes Kron72d69152020-02-10 13:05:5548#include "pc/webrtc_sdp.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3149#include "rtc_base/checks.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0850#include "rtc_base/ip_address.h"
51#include "rtc_base/location.h"
52#include "rtc_base/logging.h"
53#include "rtc_base/network_constants.h"
54#include "rtc_base/ref_counted_object.h"
55#include "rtc_base/rtc_certificate.h"
56#include "rtc_base/socket_address.h"
57#include "rtc_base/ssl_stream_adapter.h"
58#include "rtc_base/string_encode.h"
Jonas Olsson43568dd2018-06-11 14:25:5459#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 17:11:0060#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3161#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 21:04:3562
63namespace webrtc {
64
hboscc555c52016-10-18 19:48:3165namespace {
66
Henrik Boström646fda02019-05-22 13:49:4267// TODO(https://crbug.com/webrtc/10656): Consider making IDs less predictable.
hbos2fa7c672016-10-24 11:00:0568std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
69 return "RTCCertificate_" + fingerprint;
70}
71
Steve Anton57858b32018-02-15 23:19:5072std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
73 bool inbound,
74 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 14:25:5475 char buf[1024];
76 rtc::SimpleStringBuilder sb(buf);
77 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
78 << payload_type;
79 return sb.str();
hbos0adb8282016-11-23 10:32:0680}
81
hbos2fa7c672016-10-24 11:00:0582std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
83 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 14:25:5484 char buf[4096];
85 rtc::SimpleStringBuilder sb(buf);
86 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
87 << info.remote_candidate.id();
88 return sb.str();
hbos2fa7c672016-10-24 11:00:0589}
90
Harald Alvestranda3dab842018-01-14 08:18:5891const char kSender[] = "sender";
92const char kReceiver[] = "receiver";
93
94std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
95 const char* direction,
Harald Alvestrandc72af932018-01-11 16:18:1996 int attachment_id) {
Jonas Olsson43568dd2018-06-11 14:25:5497 char buf[1024];
98 rtc::SimpleStringBuilder sb(buf);
99 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
100 return sb.str();
hbos09bc1282016-11-08 14:29:22101}
102
hbos2fa7c672016-10-24 11:00:05103std::string RTCTransportStatsIDFromTransportChannel(
Jonas Olssona4d87372019-07-05 17:08:33104 const std::string& transport_name,
105 int channel_component) {
Jonas Olsson43568dd2018-06-11 14:25:54106 char buf[1024];
107 rtc::SimpleStringBuilder sb(buf);
108 sb << "RTCTransport_" << transport_name << "_" << channel_component;
109 return sb.str();
hbos2fa7c672016-10-24 11:00:05110}
111
hboseeafe942016-11-01 10:00:17112std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 14:25:54113 char buf[1024];
114 rtc::SimpleStringBuilder sb(buf);
115 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
116 return sb.str();
hboseeafe942016-11-01 10:00:17117}
118
hbos6ded1902016-11-01 08:50:46119std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 14:25:54120 char buf[1024];
121 rtc::SimpleStringBuilder sb(buf);
122 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
123 return sb.str();
hbos6ded1902016-11-01 08:50:46124}
125
Henrik Boström8605fbf2019-06-24 14:44:51126std::string RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(
Henrik Boström883eefc2019-05-27 11:40:25127 cricket::MediaType media_type,
Henrik Boström883eefc2019-05-27 11:40:25128 uint32_t source_ssrc) {
129 char buf[1024];
130 rtc::SimpleStringBuilder sb(buf);
131 sb << "RTCRemoteInboundRtp"
132 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
Henrik Boström8605fbf2019-06-24 14:44:51133 << "Stream_" << source_ssrc;
Henrik Boström883eefc2019-05-27 11:40:25134 return sb.str();
135}
136
Henrik Boström646fda02019-05-22 13:49:42137std::string RTCMediaSourceStatsIDFromKindAndAttachment(
138 cricket::MediaType media_type,
139 int attachment_id) {
140 char buf[1024];
141 rtc::SimpleStringBuilder sb(buf);
142 sb << "RTC" << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
143 << "Source_" << attachment_id;
144 return sb.str();
145}
146
hbosab9f6e42016-10-07 09:18:47147const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
148 if (type == cricket::LOCAL_PORT_TYPE)
149 return RTCIceCandidateType::kHost;
150 if (type == cricket::STUN_PORT_TYPE)
151 return RTCIceCandidateType::kSrflx;
152 if (type == cricket::PRFLX_PORT_TYPE)
153 return RTCIceCandidateType::kPrflx;
154 if (type == cricket::RELAY_PORT_TYPE)
155 return RTCIceCandidateType::kRelay;
156 RTC_NOTREACHED();
157 return nullptr;
158}
159
hboscc555c52016-10-18 19:48:31160const char* DataStateToRTCDataChannelState(
161 DataChannelInterface::DataState state) {
162 switch (state) {
163 case DataChannelInterface::kConnecting:
164 return RTCDataChannelState::kConnecting;
165 case DataChannelInterface::kOpen:
166 return RTCDataChannelState::kOpen;
167 case DataChannelInterface::kClosing:
168 return RTCDataChannelState::kClosing;
169 case DataChannelInterface::kClosed:
170 return RTCDataChannelState::kClosed;
171 default:
172 RTC_NOTREACHED();
173 return nullptr;
174 }
175}
176
hbos06495bc2017-01-02 16:08:18177const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
178 cricket::IceCandidatePairState state) {
179 switch (state) {
180 case cricket::IceCandidatePairState::WAITING:
181 return RTCStatsIceCandidatePairState::kWaiting;
182 case cricket::IceCandidatePairState::IN_PROGRESS:
183 return RTCStatsIceCandidatePairState::kInProgress;
184 case cricket::IceCandidatePairState::SUCCEEDED:
185 return RTCStatsIceCandidatePairState::kSucceeded;
186 case cricket::IceCandidatePairState::FAILED:
187 return RTCStatsIceCandidatePairState::kFailed;
188 default:
189 RTC_NOTREACHED();
190 return nullptr;
191 }
192}
193
hbos7064d592017-01-16 15:38:02194const char* DtlsTransportStateToRTCDtlsTransportState(
195 cricket::DtlsTransportState state) {
196 switch (state) {
197 case cricket::DTLS_TRANSPORT_NEW:
198 return RTCDtlsTransportState::kNew;
199 case cricket::DTLS_TRANSPORT_CONNECTING:
200 return RTCDtlsTransportState::kConnecting;
201 case cricket::DTLS_TRANSPORT_CONNECTED:
202 return RTCDtlsTransportState::kConnected;
203 case cricket::DTLS_TRANSPORT_CLOSED:
204 return RTCDtlsTransportState::kClosed;
205 case cricket::DTLS_TRANSPORT_FAILED:
206 return RTCDtlsTransportState::kFailed;
207 default:
208 RTC_NOTREACHED();
209 return nullptr;
210 }
211}
212
Gary Liu37e489c2017-11-21 18:49:36213const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
214 switch (type) {
215 case rtc::ADAPTER_TYPE_CELLULAR:
Jonas Oreland08d18062020-04-02 05:19:12216 case rtc::ADAPTER_TYPE_CELLULAR_2G:
217 case rtc::ADAPTER_TYPE_CELLULAR_3G:
218 case rtc::ADAPTER_TYPE_CELLULAR_4G:
219 case rtc::ADAPTER_TYPE_CELLULAR_5G:
Gary Liu37e489c2017-11-21 18:49:36220 return RTCNetworkType::kCellular;
221 case rtc::ADAPTER_TYPE_ETHERNET:
222 return RTCNetworkType::kEthernet;
223 case rtc::ADAPTER_TYPE_WIFI:
224 return RTCNetworkType::kWifi;
225 case rtc::ADAPTER_TYPE_VPN:
226 return RTCNetworkType::kVpn;
227 case rtc::ADAPTER_TYPE_UNKNOWN:
228 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 22:38:09229 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 18:49:36230 return RTCNetworkType::kUnknown;
231 }
232 RTC_NOTREACHED();
233 return nullptr;
234}
235
Henrik Boströmce33b6a2019-05-28 15:42:38236const char* QualityLimitationReasonToRTCQualityLimitationReason(
237 QualityLimitationReason reason) {
238 switch (reason) {
239 case QualityLimitationReason::kNone:
240 return RTCQualityLimitationReason::kNone;
241 case QualityLimitationReason::kCpu:
242 return RTCQualityLimitationReason::kCpu;
243 case QualityLimitationReason::kBandwidth:
244 return RTCQualityLimitationReason::kBandwidth;
245 case QualityLimitationReason::kOther:
246 return RTCQualityLimitationReason::kOther;
247 }
Karl Wibergc95b9392020-11-07 23:49:37248 RTC_CHECK_NOTREACHED();
Henrik Boströmce33b6a2019-05-28 15:42:38249}
250
hbos9e302742017-01-20 10:47:10251double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
252 RTC_DCHECK_GE(audio_level, 0);
253 RTC_DCHECK_LE(audio_level, 32767);
254 return audio_level / 32767.0;
255}
256
hbos0adb8282016-11-23 10:32:06257std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 23:19:50258 uint64_t timestamp_us,
259 const std::string& mid,
Philipp Hancke95157a02020-11-16 19:08:27260 const std::string& transport_id,
Steve Anton57858b32018-02-15 23:19:50261 bool inbound,
hbos0adb8282016-11-23 10:32:06262 const RtpCodecParameters& codec_params) {
263 RTC_DCHECK_GE(codec_params.payload_type, 0);
264 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 20:09:01265 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 10:32:06266 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
267 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 23:19:50268 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 10:32:06269 timestamp_us));
270 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 14:56:04271 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 20:09:01272 if (codec_params.clock_rate) {
273 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
274 }
Johannes Kron72d69152020-02-10 13:05:55275 if (codec_params.num_channels) {
276 codec_stats->channels = *codec_params.num_channels;
277 }
278
279 rtc::StringBuilder fmtp;
280 if (WriteFmtpParameters(codec_params.parameters, &fmtp)) {
281 codec_stats->sdp_fmtp_line = fmtp.Release();
282 }
Philipp Hancke95157a02020-11-16 19:08:27283 codec_stats->transport_id = transport_id;
hbos0adb8282016-11-23 10:32:06284 return codec_stats;
285}
286
hbos09bc1282016-11-08 14:29:22287void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
288 const MediaStreamTrackInterface& track,
289 RTCMediaStreamTrackStats* track_stats) {
290 track_stats->track_identifier = track.id();
291 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
292}
293
hbos820f5782016-11-22 11:16:50294// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 10:00:17295void SetInboundRTPStreamStatsFromMediaReceiverInfo(
296 const cricket::MediaReceiverInfo& media_receiver_info,
297 RTCInboundRTPStreamStats* inbound_stats) {
298 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 14:28:11299 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 13:08:34300 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 10:00:17301 inbound_stats->is_remote = false;
hboseeafe942016-11-01 10:00:17302 inbound_stats->packets_received =
303 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
304 inbound_stats->bytes_received =
Niels Möllerac0a4cb2019-10-09 13:01:33305 static_cast<uint64_t>(media_receiver_info.payload_bytes_rcvd);
306 inbound_stats->header_bytes_received =
307 static_cast<uint64_t>(media_receiver_info.header_and_padding_bytes_rcvd);
hbos02cd4d62016-12-09 12:19:44308 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 11:26:04309 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 10:00:17310}
311
312void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 23:19:50313 const std::string& mid,
hboseeafe942016-11-01 10:00:17314 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 11:16:50315 RTCInboundRTPStreamStats* inbound_audio) {
Jonas Olssona4d87372019-07-05 17:08:33316 SetInboundRTPStreamStatsFromMediaReceiverInfo(voice_receiver_info,
317 inbound_audio);
hbos820f5782016-11-22 11:16:50318 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03319 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16320 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50321 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
322 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16323 }
Jonas Olssona4d87372019-07-05 17:08:33324 inbound_audio->jitter = static_cast<double>(voice_receiver_info.jitter_ms) /
325 rtc::kNumMillisecsPerSec;
Eldar Rello4e5bc9f2020-07-06 11:18:07326 inbound_audio->jitter_buffer_delay =
327 voice_receiver_info.jitter_buffer_delay_seconds;
328 inbound_audio->jitter_buffer_emitted_count =
329 voice_receiver_info.jitter_buffer_emitted_count;
330 inbound_audio->total_samples_received =
331 voice_receiver_info.total_samples_received;
332 inbound_audio->concealed_samples = voice_receiver_info.concealed_samples;
333 inbound_audio->silent_concealed_samples =
334 voice_receiver_info.silent_concealed_samples;
335 inbound_audio->concealment_events = voice_receiver_info.concealment_events;
336 inbound_audio->inserted_samples_for_deceleration =
337 voice_receiver_info.inserted_samples_for_deceleration;
338 inbound_audio->removed_samples_for_acceleration =
339 voice_receiver_info.removed_samples_for_acceleration;
Philipp Hanckeaa83cc72020-10-27 08:50:36340 if (voice_receiver_info.audio_level >= 0) {
341 inbound_audio->audio_level =
342 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
343 }
Eldar Rello4e5bc9f2020-07-06 11:18:07344 inbound_audio->total_audio_energy = voice_receiver_info.total_output_energy;
345 inbound_audio->total_samples_duration =
346 voice_receiver_info.total_output_duration;
hbos820f5782016-11-22 11:16:50347 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
348 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 15:32:00349 if (voice_receiver_info.last_packet_received_timestamp_ms) {
350 inbound_audio->last_packet_received_timestamp =
351 static_cast<double>(
352 *voice_receiver_info.last_packet_received_timestamp_ms) /
353 rtc::kNumMillisecsPerSec;
354 }
Åsa Perssonfcf79cc2019-10-22 13:23:44355 if (voice_receiver_info.estimated_playout_ntp_timestamp_ms) {
356 inbound_audio->estimated_playout_timestamp = static_cast<double>(
357 *voice_receiver_info.estimated_playout_ntp_timestamp_ms);
358 }
Ivo Creusen8d8ffdb2019-04-30 07:45:21359 inbound_audio->fec_packets_received =
360 voice_receiver_info.fec_packets_received;
361 inbound_audio->fec_packets_discarded =
362 voice_receiver_info.fec_packets_discarded;
hboseeafe942016-11-01 10:00:17363}
364
365void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 23:19:50366 const std::string& mid,
hboseeafe942016-11-01 10:00:17367 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 11:16:50368 RTCInboundRTPStreamStats* inbound_video) {
Jonas Olssona4d87372019-07-05 17:08:33369 SetInboundRTPStreamStatsFromMediaReceiverInfo(video_receiver_info,
370 inbound_video);
hbos820f5782016-11-22 11:16:50371 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03372 inbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16373 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50374 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
375 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16376 }
Di Wu (RP Room Eng)8af6b492021-02-20 01:34:22377 inbound_video->jitter = static_cast<double>(video_receiver_info.jitter_ms) /
378 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 11:16:50379 inbound_video->fir_count =
380 static_cast<uint32_t>(video_receiver_info.firs_sent);
381 inbound_video->pli_count =
382 static_cast<uint32_t>(video_receiver_info.plis_sent);
383 inbound_video->nack_count =
384 static_cast<uint32_t>(video_receiver_info.nacks_sent);
Eldar Rello4e5bc9f2020-07-06 11:18:07385 inbound_video->frames_received = video_receiver_info.frames_received;
hbos6769c492017-01-02 16:35:13386 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
Eldar Rello4e5bc9f2020-07-06 11:18:07387 inbound_video->frames_dropped = video_receiver_info.frames_dropped;
Rasmus Brandt2efae772019-06-27 12:29:34388 inbound_video->key_frames_decoded = video_receiver_info.key_frames_decoded;
Eldar Rello4e5bc9f2020-07-06 11:18:07389 if (video_receiver_info.frame_width > 0) {
390 inbound_video->frame_width =
391 static_cast<uint32_t>(video_receiver_info.frame_width);
392 }
393 if (video_receiver_info.frame_height > 0) {
394 inbound_video->frame_height =
395 static_cast<uint32_t>(video_receiver_info.frame_height);
396 }
397 if (video_receiver_info.framerate_rcvd > 0) {
398 inbound_video->frames_per_second = video_receiver_info.framerate_rcvd;
399 }
hbosa51d4f32017-02-16 13:34:48400 if (video_receiver_info.qp_sum)
401 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Johannes Kronbfd343b2019-07-01 08:07:50402 inbound_video->total_decode_time =
403 static_cast<double>(video_receiver_info.total_decode_time_ms) /
404 rtc::kNumMillisecsPerSec;
Johannes Kron00376e12019-11-25 09:25:42405 inbound_video->total_inter_frame_delay =
406 video_receiver_info.total_inter_frame_delay;
407 inbound_video->total_squared_inter_frame_delay =
408 video_receiver_info.total_squared_inter_frame_delay;
Henrik Boström01738c62019-04-15 15:32:00409 if (video_receiver_info.last_packet_received_timestamp_ms) {
410 inbound_video->last_packet_received_timestamp =
411 static_cast<double>(
412 *video_receiver_info.last_packet_received_timestamp_ms) /
413 rtc::kNumMillisecsPerSec;
414 }
Åsa Perssonfcf79cc2019-10-22 13:23:44415 if (video_receiver_info.estimated_playout_ntp_timestamp_ms) {
416 inbound_video->estimated_playout_timestamp = static_cast<double>(
417 *video_receiver_info.estimated_playout_ntp_timestamp_ms);
418 }
Henrik Boström2e069262019-04-09 11:59:31419 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
420 // optional, support the "unspecified" value.
421 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
422 inbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 11:09:51423 if (!video_receiver_info.decoder_implementation_name.empty()) {
424 inbound_video->decoder_implementation =
425 video_receiver_info.decoder_implementation_name;
426 }
hboseeafe942016-11-01 10:00:17427}
428
hbos820f5782016-11-22 11:16:50429// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 08:50:46430void SetOutboundRTPStreamStatsFromMediaSenderInfo(
431 const cricket::MediaSenderInfo& media_sender_info,
432 RTCOutboundRTPStreamStats* outbound_stats) {
433 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 14:28:11434 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 13:08:34435 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 08:50:46436 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 08:50:46437 outbound_stats->packets_sent =
438 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53439 outbound_stats->retransmitted_packets_sent =
440 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 08:50:46441 outbound_stats->bytes_sent =
Niels Möllerac0a4cb2019-10-09 13:01:33442 static_cast<uint64_t>(media_sender_info.payload_bytes_sent);
443 outbound_stats->header_bytes_sent =
444 static_cast<uint64_t>(media_sender_info.header_and_padding_bytes_sent);
Henrik Boströmcf96e0f2019-04-17 11:51:53445 outbound_stats->retransmitted_bytes_sent =
446 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 08:50:46447}
448
449void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 23:19:50450 const std::string& mid,
hbos6ded1902016-11-01 08:50:46451 const cricket::VoiceSenderInfo& voice_sender_info,
452 RTCOutboundRTPStreamStats* outbound_audio) {
Jonas Olssona4d87372019-07-05 17:08:33453 SetOutboundRTPStreamStatsFromMediaSenderInfo(voice_sender_info,
454 outbound_audio);
hbos6ded1902016-11-01 08:50:46455 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 12:55:03456 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 12:59:16457 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50458 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
459 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16460 }
hbos6ded1902016-11-01 08:50:46461 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
462 // purposefully left undefined for audio.
463}
464
465void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 23:19:50466 const std::string& mid,
hbos6ded1902016-11-01 08:50:46467 const cricket::VideoSenderInfo& video_sender_info,
468 RTCOutboundRTPStreamStats* outbound_video) {
Jonas Olssona4d87372019-07-05 17:08:33469 SetOutboundRTPStreamStatsFromMediaSenderInfo(video_sender_info,
470 outbound_video);
hbos6ded1902016-11-01 08:50:46471 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 12:55:03472 outbound_video->kind = "video";
hbos585a9b12017-02-07 12:59:16473 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 23:19:50474 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
475 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 12:59:16476 }
hbos6ded1902016-11-01 08:50:46477 outbound_video->fir_count =
478 static_cast<uint32_t>(video_sender_info.firs_rcvd);
479 outbound_video->pli_count =
480 static_cast<uint32_t>(video_sender_info.plis_rcvd);
481 outbound_video->nack_count =
482 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 16:35:13483 if (video_sender_info.qp_sum)
484 outbound_video->qp_sum = *video_sender_info.qp_sum;
485 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Rasmus Brandt2efae772019-06-27 12:29:34486 outbound_video->key_frames_encoded = video_sender_info.key_frames_encoded;
Henrik Boströmf71362f2019-04-08 14:14:23487 outbound_video->total_encode_time =
488 static_cast<double>(video_sender_info.total_encode_time_ms) /
489 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 13:15:38490 outbound_video->total_encoded_bytes_target =
491 video_sender_info.total_encoded_bytes_target;
Eldar Rello9276e2c2020-06-10 14:53:39492 if (video_sender_info.send_frame_width > 0) {
493 outbound_video->frame_width =
494 static_cast<uint32_t>(video_sender_info.send_frame_width);
Henrik Boströma0ff50c2020-05-05 13:54:46495 }
Eldar Rello9276e2c2020-06-10 14:53:39496 if (video_sender_info.send_frame_height > 0) {
497 outbound_video->frame_height =
498 static_cast<uint32_t>(video_sender_info.send_frame_height);
499 }
500 if (video_sender_info.framerate_sent > 0) {
501 outbound_video->frames_per_second = video_sender_info.framerate_sent;
502 }
503 outbound_video->frames_sent = video_sender_info.frames_sent;
504 outbound_video->huge_frames_sent = video_sender_info.huge_frames_sent;
Henrik Boström9fe18342019-05-16 16:38:20505 outbound_video->total_packet_send_delay =
506 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
507 rtc::kNumMillisecsPerSec;
Henrik Boströmce33b6a2019-05-28 15:42:38508 outbound_video->quality_limitation_reason =
509 QualityLimitationReasonToRTCQualityLimitationReason(
510 video_sender_info.quality_limitation_reason);
Evan Shrubsolecc62b162019-09-09 09:26:45511 outbound_video->quality_limitation_resolution_changes =
512 video_sender_info.quality_limitation_resolution_changes;
Henrik Boström2e069262019-04-09 11:59:31513 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
514 // optional, support the "unspecified" value.
515 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
516 outbound_video->content_type = RTCContentType::kScreenshare;
Henrik Boström6b430862019-08-16 11:09:51517 if (!video_sender_info.encoder_implementation_name.empty()) {
518 outbound_video->encoder_implementation =
519 video_sender_info.encoder_implementation_name;
520 }
Henrik Boströma0ff50c2020-05-05 13:54:46521 if (video_sender_info.rid) {
522 outbound_video->rid = *video_sender_info.rid;
523 }
hbos6ded1902016-11-01 08:50:46524}
525
Henrik Boström883eefc2019-05-27 11:40:25526std::unique_ptr<RTCRemoteInboundRtpStreamStats>
527ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
528 const ReportBlockData& report_block_data,
529 cricket::MediaType media_type,
Eldar Relloc07e9042020-07-03 08:08:07530 const std::map<std::string, RTCOutboundRTPStreamStats*>& outbound_rtps,
Henrik Boström883eefc2019-05-27 11:40:25531 const RTCStatsReport& report) {
532 const auto& report_block = report_block_data.report_block();
533 // RTCStats' timestamp generally refers to when the metric was sampled, but
534 // for "remote-[outbound/inbound]-rtp" it refers to the local time when the
535 // Report Block was received.
Mirko Bonadei317a1f02019-09-17 15:06:18536 auto remote_inbound = std::make_unique<RTCRemoteInboundRtpStreamStats>(
Henrik Boström8605fbf2019-06-24 14:44:51537 RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(media_type,
538 report_block.source_ssrc),
Henrik Boström883eefc2019-05-27 11:40:25539 /*timestamp=*/report_block_data.report_block_timestamp_utc_us());
Henrik Boström8605fbf2019-06-24 14:44:51540 remote_inbound->ssrc = report_block.source_ssrc;
Henrik Boström883eefc2019-05-27 11:40:25541 remote_inbound->kind =
542 media_type == cricket::MEDIA_TYPE_AUDIO ? "audio" : "video";
543 remote_inbound->packets_lost = report_block.packets_lost;
544 remote_inbound->round_trip_time =
545 static_cast<double>(report_block_data.last_rtt_ms()) /
546 rtc::kNumMillisecsPerSec;
547
548 std::string local_id = RTCOutboundRTPStreamStatsIDFromSSRC(
549 media_type == cricket::MEDIA_TYPE_AUDIO, report_block.source_ssrc);
Henrik Boström4f40fa52019-12-19 12:27:27550 // Look up local stat from |outbound_rtps| where the pointers are non-const.
551 auto local_id_it = outbound_rtps.find(local_id);
552 if (local_id_it != outbound_rtps.end()) {
Henrik Boström883eefc2019-05-27 11:40:25553 remote_inbound->local_id = local_id;
Henrik Boström4f40fa52019-12-19 12:27:27554 auto& outbound_rtp = *local_id_it->second;
555 outbound_rtp.remote_id = remote_inbound->id();
Henrik Boström883eefc2019-05-27 11:40:25556 // The RTP/RTCP transport is obtained from the
557 // RTCOutboundRtpStreamStats's transport.
558 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
559 ? report.Get(*outbound_rtp.transport_id)
560 : nullptr;
561 if (transport_from_id) {
562 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
563 // If RTP and RTCP are not multiplexed, there is a separate RTCP
564 // transport paired with the RTP transport, otherwise the same
565 // transport is used for RTCP and RTP.
566 remote_inbound->transport_id =
567 transport.rtcp_transport_stats_id.is_defined()
568 ? *transport.rtcp_transport_stats_id
569 : *outbound_rtp.transport_id;
570 }
571 // We're assuming the same codec is used on both ends. However if the
572 // codec is switched out on the fly we may have received a Report Block
573 // based on the previous codec and there is no way to tell which point in
574 // time the codec changed for the remote end.
575 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
576 ? report.Get(*outbound_rtp.codec_id)
577 : nullptr;
578 if (codec_from_id) {
579 remote_inbound->codec_id = *outbound_rtp.codec_id;
580 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
581 if (codec.clock_rate.is_defined()) {
582 // The Report Block jitter is expressed in RTP timestamp units
583 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
584 // to seconds we divide by the codec's clock rate.
585 remote_inbound->jitter =
586 static_cast<double>(report_block.jitter) / *codec.clock_rate;
587 }
588 }
589 }
590 return remote_inbound;
591}
592
hbos02ba2112016-10-28 12:14:53593void ProduceCertificateStatsFromSSLCertificateStats(
Jonas Olssona4d87372019-07-05 17:08:33594 int64_t timestamp_us,
595 const rtc::SSLCertificateStats& certificate_stats,
hbos02ba2112016-10-28 12:14:53596 RTCStatsReport* report) {
597 RTCCertificateStats* prev_certificate_stats = nullptr;
598 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
599 s = s->issuer.get()) {
hbos02d2a922016-12-21 09:29:05600 std::string certificate_stats_id =
601 RTCCertificateIDFromFingerprint(s->fingerprint);
602 // It is possible for the same certificate to show up multiple times, e.g.
603 // if local and remote side use the same certificate in a loopback call.
604 // If the report already contains stats for this certificate, skip it.
605 if (report->Get(certificate_stats_id)) {
606 RTC_DCHECK_EQ(s, &certificate_stats);
607 break;
608 }
Jonas Olssona4d87372019-07-05 17:08:33609 RTCCertificateStats* certificate_stats =
610 new RTCCertificateStats(certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 12:14:53611 certificate_stats->fingerprint = s->fingerprint;
612 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
613 certificate_stats->base64_certificate = s->base64_certificate;
614 if (prev_certificate_stats)
615 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
616 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
617 prev_certificate_stats = certificate_stats;
618 }
619}
620
Jonas Olssona4d87372019-07-05 17:08:33621const std::string& ProduceIceCandidateStats(int64_t timestamp_us,
622 const cricket::Candidate& candidate,
623 bool is_local,
624 const std::string& transport_id,
625 RTCStatsReport* report) {
hbos02ba2112016-10-28 12:14:53626 const std::string& id = "RTCIceCandidate_" + candidate.id();
627 const RTCStats* stats = report->Get(id);
628 if (!stats) {
629 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
630 if (is_local)
631 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
632 else
633 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 17:59:31634 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 18:49:36635 if (is_local) {
636 candidate_stats->network_type =
637 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 12:40:08638 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
639 std::string relay_protocol = candidate.relay_protocol();
640 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
641 relay_protocol.compare("tcp") == 0 ||
642 relay_protocol.compare("tls") == 0);
643 candidate_stats->relay_protocol = relay_protocol;
644 }
Gary Liu37e489c2017-11-21 18:49:36645 } else {
646 // We don't expect to know the adapter type of remote candidates.
647 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
648 }
hbos02ba2112016-10-28 12:14:53649 candidate_stats->ip = candidate.address().ipaddr().ToString();
650 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
651 candidate_stats->protocol = candidate.protocol();
Jonas Olssona4d87372019-07-05 17:08:33652 candidate_stats->candidate_type =
653 CandidateTypeToRTCIceCandidateType(candidate.type());
hbos02ba2112016-10-28 12:14:53654 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
655
656 stats = candidate_stats.get();
657 report->AddStats(std::move(candidate_stats));
658 }
659 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
660 : RTCRemoteIceCandidateStats::kType);
661 return stats->id();
662}
663
hbos9e302742017-01-20 10:47:10664std::unique_ptr<RTCMediaStreamTrackStats>
665ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
666 int64_t timestamp_us,
667 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19668 const cricket::VoiceSenderInfo& voice_sender_info,
669 int attachment_id) {
hbos9e302742017-01-20 10:47:10670 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
671 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58672 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
673 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19674 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10675 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
676 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42677 audio_track_stats->media_source_id =
678 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
679 attachment_id);
hbos9e302742017-01-20 10:47:10680 audio_track_stats->remote_source = false;
681 audio_track_stats->detached = false;
Ivo Creusen56d460902017-11-24 16:29:59682 if (voice_sender_info.apm_statistics.echo_return_loss) {
683 audio_track_stats->echo_return_loss =
684 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 10:47:10685 }
Ivo Creusen56d460902017-11-24 16:29:59686 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
687 audio_track_stats->echo_return_loss_enhancement =
688 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 10:47:10689 }
690 return audio_track_stats;
691}
692
693std::unique_ptr<RTCMediaStreamTrackStats>
694ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
695 int64_t timestamp_us,
696 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19697 const cricket::VoiceReceiverInfo& voice_receiver_info,
698 int attachment_id) {
699 // Since receiver tracks can't be reattached, we use the SSRC as
700 // an attachment identifier.
hbos9e302742017-01-20 10:47:10701 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
702 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58703 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
704 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19705 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10706 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
707 audio_track, audio_track_stats.get());
708 audio_track_stats->remote_source = true;
709 audio_track_stats->detached = false;
710 if (voice_receiver_info.audio_level >= 0) {
Jonas Olssona4d87372019-07-05 17:08:33711 audio_track_stats->audio_level =
712 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
hbos9e302742017-01-20 10:47:10713 }
Gustaf Ullbergb0a02072017-10-02 10:00:34714 audio_track_stats->jitter_buffer_delay =
715 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 14:46:29716 audio_track_stats->jitter_buffer_emitted_count =
717 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 07:45:21718 audio_track_stats->inserted_samples_for_deceleration =
719 voice_receiver_info.inserted_samples_for_deceleration;
720 audio_track_stats->removed_samples_for_acceleration =
721 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 19:17:49722 audio_track_stats->total_audio_energy =
723 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-25 00:15:13724 audio_track_stats->total_samples_received =
725 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 19:17:49726 audio_track_stats->total_samples_duration =
727 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-25 00:15:13728 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 07:45:21729 audio_track_stats->silent_concealed_samples =
730 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 07:28:20731 audio_track_stats->concealment_events =
732 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 16:21:10733 audio_track_stats->jitter_buffer_flushes =
734 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 11:52:16735 audio_track_stats->delayed_packet_outage_samples =
736 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 08:18:40737 audio_track_stats->relative_packet_arrival_delay =
738 voice_receiver_info.relative_packet_arrival_delay_seconds;
Artem Titove618cc92020-03-11 10:18:54739 audio_track_stats->jitter_buffer_target_delay =
740 voice_receiver_info.jitter_buffer_target_delay_seconds;
Henrik Lundin44125fa2019-04-29 15:00:46741 audio_track_stats->interruption_count =
742 voice_receiver_info.interruption_count >= 0
743 ? voice_receiver_info.interruption_count
744 : 0;
745 audio_track_stats->total_interruption_duration =
746 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
747 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 10:47:10748 return audio_track_stats;
749}
750
751std::unique_ptr<RTCMediaStreamTrackStats>
752ProduceMediaStreamTrackStatsFromVideoSenderInfo(
753 int64_t timestamp_us,
754 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19755 const cricket::VideoSenderInfo& video_sender_info,
756 int attachment_id) {
hbos9e302742017-01-20 10:47:10757 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
758 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58759 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 08:18:58760 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19761 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10762 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
763 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42764 video_track_stats->media_source_id =
765 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
766 attachment_id);
hbos9e302742017-01-20 10:47:10767 video_track_stats->remote_source = false;
768 video_track_stats->detached = false;
Jonas Olssona4d87372019-07-05 17:08:33769 video_track_stats->frame_width =
770 static_cast<uint32_t>(video_sender_info.send_frame_width);
771 video_track_stats->frame_height =
772 static_cast<uint32_t>(video_sender_info.send_frame_height);
hbosfefe0762017-01-20 14:14:25773 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 13:08:34774 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 14:14:25775 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 15:35:03776 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 10:47:10777 return video_track_stats;
778}
779
780std::unique_ptr<RTCMediaStreamTrackStats>
781ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
782 int64_t timestamp_us,
783 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19784 const cricket::VideoReceiverInfo& video_receiver_info,
785 int attachment_id) {
hbos9e302742017-01-20 10:47:10786 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
787 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58788 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
789
790 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19791 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10792 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
793 video_track, video_track_stats.get());
794 video_track_stats->remote_source = true;
795 video_track_stats->detached = false;
796 if (video_receiver_info.frame_width > 0 &&
797 video_receiver_info.frame_height > 0) {
Jonas Olssona4d87372019-07-05 17:08:33798 video_track_stats->frame_width =
799 static_cast<uint32_t>(video_receiver_info.frame_width);
800 video_track_stats->frame_height =
801 static_cast<uint32_t>(video_receiver_info.frame_height);
hbos9e302742017-01-20 10:47:10802 }
Guido Urdaneta67378412019-05-28 15:38:08803 video_track_stats->jitter_buffer_delay =
804 video_receiver_info.jitter_buffer_delay_seconds;
805 video_track_stats->jitter_buffer_emitted_count =
806 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 11:56:50807 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 15:39:09808 // TODO(hbos): When we support receiving simulcast, this should be the total
809 // number of frames correctly decoded, independent of which SSRC it was
810 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 13:08:34811 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 15:39:09812 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
Johannes Kron0c141c52019-08-26 13:04:43813 video_track_stats->frames_dropped = video_receiver_info.frames_dropped;
Sergey Silkin02371062019-01-31 15:45:42814 video_track_stats->freeze_count = video_receiver_info.freeze_count;
815 video_track_stats->pause_count = video_receiver_info.pause_count;
816 video_track_stats->total_freezes_duration =
817 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
818 rtc::kNumMillisecsPerSec;
819 video_track_stats->total_pauses_duration =
820 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
821 rtc::kNumMillisecsPerSec;
822 video_track_stats->total_frames_duration =
823 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
824 rtc::kNumMillisecsPerSec;
825 video_track_stats->sum_squared_frame_durations =
826 video_receiver_info.sum_squared_frame_durations;
827
hbos9e302742017-01-20 10:47:10828 return video_track_stats;
829}
830
Harald Alvestrand89061872018-01-02 13:08:34831void ProduceSenderMediaTrackStats(
832 int64_t timestamp_us,
833 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50834 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 13:08:34835 RTCStatsReport* report) {
836 // This function iterates over the senders to generate outgoing track stats.
837
838 // TODO(hbos): Return stats of detached tracks. We have to perform stats
839 // gathering at the time of detachment to get accurate stats and timestamps.
840 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 16:29:42841 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 13:08:34842 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
843 AudioTrackInterface* track =
844 static_cast<AudioTrackInterface*>(sender->track().get());
845 if (!track)
846 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16847 cricket::VoiceSenderInfo null_sender_info;
848 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
849 // TODO(hta): Checking on ssrc is not proper. There should be a way
850 // to see from a sender whether it's connected or not.
851 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 23:19:50852 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29853 // When pc.close is called, sender info is discarded, so
854 // we generate zeroes instead. Bug: It should be retained.
855 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50856 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16857 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29858 if (sender_info) {
859 voice_sender_info = sender_info;
860 } else {
861 RTC_LOG(LS_INFO)
862 << "RTCStatsCollector: No voice sender info for sender with ssrc "
863 << sender->ssrc();
864 }
Harald Alvestrandb8e12012018-01-23 14:28:16865 }
Harald Alvestrand89061872018-01-02 13:08:34866 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19867 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
868 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34869 report->AddStats(std::move(audio_track_stats));
870 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
871 VideoTrackInterface* track =
872 static_cast<VideoTrackInterface*>(sender->track().get());
873 if (!track)
874 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16875 cricket::VideoSenderInfo null_sender_info;
876 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
877 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 13:43:29878 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
879 // "none")
Steve Anton57858b32018-02-15 23:19:50880 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29881 // When pc.close is called, sender info is discarded, so
882 // we generate zeroes instead. Bug: It should be retained.
883 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50884 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16885 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29886 if (sender_info) {
887 video_sender_info = sender_info;
888 } else {
889 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
890 << sender->ssrc();
891 }
892 }
Harald Alvestrand89061872018-01-02 13:08:34893 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19894 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
895 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34896 report->AddStats(std::move(video_track_stats));
897 } else {
898 RTC_NOTREACHED();
899 }
900 }
901}
902
903void ProduceReceiverMediaTrackStats(
904 int64_t timestamp_us,
905 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50906 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 13:08:34907 RTCStatsReport* report) {
908 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 16:29:42909 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 13:08:34910 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
911 AudioTrackInterface* track =
912 static_cast<AudioTrackInterface*>(receiver->track().get());
913 const cricket::VoiceReceiverInfo* voice_receiver_info =
914 track_media_info_map.GetVoiceReceiverInfo(*track);
915 if (!voice_receiver_info) {
916 continue;
917 }
918 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
919 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:19920 timestamp_us, *track, *voice_receiver_info,
921 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34922 report->AddStats(std::move(audio_track_stats));
923 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
924 VideoTrackInterface* track =
925 static_cast<VideoTrackInterface*>(receiver->track().get());
926 const cricket::VideoReceiverInfo* video_receiver_info =
927 track_media_info_map.GetVideoReceiverInfo(*track);
928 if (!video_receiver_info) {
929 continue;
930 }
931 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
932 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:19933 timestamp_us, *track, *video_receiver_info,
934 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34935 report->AddStats(std::move(video_track_stats));
936 } else {
937 RTC_NOTREACHED();
938 }
939 }
940}
941
Henrik Boström5b3541f2018-03-19 12:52:56942rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
943 bool filter_by_sender_selector,
944 rtc::scoped_refptr<const RTCStatsReport> report,
945 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
946 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
947 std::vector<std::string> rtpstream_ids;
948 if (filter_by_sender_selector) {
949 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
950 if (sender_selector) {
951 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
952 // reference the sender stats.
953 // Because we do not implement sender stats, we look at outbound-rtp(s)
954 // that reference the track attachment stats for the sender instead.
955 std::string track_id =
956 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
957 kSender, sender_selector->AttachmentId());
958 for (const auto& stats : *report) {
959 if (stats.type() != RTCOutboundRTPStreamStats::kType)
960 continue;
961 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
962 if (outbound_rtp.track_id.is_defined() &&
963 *outbound_rtp.track_id == track_id) {
964 rtpstream_ids.push_back(outbound_rtp.id());
965 }
966 }
967 }
968 } else {
969 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
970 if (receiver_selector) {
971 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
972 // reference the receiver stats.
973 // Because we do not implement receiver stats, we look at inbound-rtp(s)
974 // that reference the track attachment stats for the receiver instead.
975 std::string track_id =
976 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
977 kReceiver, receiver_selector->AttachmentId());
978 for (const auto& stats : *report) {
979 if (stats.type() != RTCInboundRTPStreamStats::kType)
980 continue;
981 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
982 if (inbound_rtp.track_id.is_defined() &&
983 *inbound_rtp.track_id == track_id) {
984 rtpstream_ids.push_back(inbound_rtp.id());
985 }
986 }
987 }
988 }
989 if (rtpstream_ids.empty())
990 return RTCStatsReport::Create(report->timestamp_us());
991 return TakeReferencedStats(report->Copy(), rtpstream_ids);
992}
993
hboscc555c52016-10-18 19:48:31994} // namespace
995
Henrik Boström5b3541f2018-03-19 12:52:56996RTCStatsCollector::RequestInfo::RequestInfo(
997 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
998 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
999
1000RTCStatsCollector::RequestInfo::RequestInfo(
1001 rtc::scoped_refptr<RtpSenderInternal> selector,
1002 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1003 : RequestInfo(FilterMode::kSenderSelector,
1004 std::move(callback),
1005 std::move(selector),
1006 nullptr) {}
1007
1008RTCStatsCollector::RequestInfo::RequestInfo(
1009 rtc::scoped_refptr<RtpReceiverInternal> selector,
1010 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1011 : RequestInfo(FilterMode::kReceiverSelector,
1012 std::move(callback),
1013 nullptr,
1014 std::move(selector)) {}
1015
1016RTCStatsCollector::RequestInfo::RequestInfo(
1017 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
1018 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
1019 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
1020 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
1021 : filter_mode_(filter_mode),
1022 callback_(std::move(callback)),
1023 sender_selector_(std::move(sender_selector)),
1024 receiver_selector_(std::move(receiver_selector)) {
1025 RTC_DCHECK(callback_);
1026 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
1027}
1028
hbosc82f2e12016-09-05 08:36:501029rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-24 00:38:461030 PeerConnectionInternal* pc,
1031 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 08:36:501032 return rtc::scoped_refptr<RTCStatsCollector>(
1033 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
1034}
1035
Steve Anton2d8609c2018-01-24 00:38:461036RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 08:36:501037 int64_t cache_lifetime_us)
hbosd565b732016-08-30 21:04:351038 : pc_(pc),
Steve Anton978b8762017-09-29 19:15:021039 signaling_thread_(pc->signaling_thread()),
1040 worker_thread_(pc->worker_thread()),
1041 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 08:36:501042 num_pending_partial_reports_(0),
1043 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 08:49:311044 network_report_event_(true /* manual_reset */,
1045 true /* initially_signaled */),
hbos0e6758d2016-08-31 14:57:361046 cache_timestamp_us_(0),
1047 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 21:04:351048 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 08:36:501049 RTC_DCHECK(signaling_thread_);
1050 RTC_DCHECK(worker_thread_);
1051 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 14:57:361052 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Taylor Brandstetter3a034e12020-07-09 22:32:341053 pc_->SignalRtpDataChannelCreated().connect(
1054 this, &RTCStatsCollector::OnRtpDataChannelCreated);
1055 pc_->SignalSctpDataChannelCreated().connect(
1056 this, &RTCStatsCollector::OnSctpDataChannelCreated);
hbosd565b732016-08-30 21:04:351057}
1058
hbosb78306a2016-12-19 13:06:571059RTCStatsCollector::~RTCStatsCollector() {
1060 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1061}
1062
hbosc82f2e12016-09-05 08:36:501063void RTCStatsCollector::GetStatsReport(
1064 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 12:52:561065 GetStatsReportInternal(RequestInfo(std::move(callback)));
1066}
1067
1068void RTCStatsCollector::GetStatsReport(
1069 rtc::scoped_refptr<RtpSenderInternal> selector,
1070 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1071 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1072}
1073
1074void RTCStatsCollector::GetStatsReport(
1075 rtc::scoped_refptr<RtpReceiverInternal> selector,
1076 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1077 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1078}
1079
1080void RTCStatsCollector::GetStatsReportInternal(
1081 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 08:36:501082 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 12:52:561083 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 08:36:501084
hbos0e6758d2016-08-31 14:57:361085 // "Now" using a monotonically increasing timer.
1086 int64_t cache_now_us = rtc::TimeMicros();
1087 if (cached_report_ &&
1088 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 17:53:471089 // We have a fresh cached report to deliver. Deliver asynchronously, since
1090 // the caller may not be expecting a synchronous callback, and it avoids
1091 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 12:52:561092 std::vector<RequestInfo> requests;
1093 requests.swap(requests_);
Niels Möller4bab23f2021-01-18 08:24:331094
1095 // Task subclass to take ownership of the requests.
1096 // TODO(nisse): Delete when we can use C++14, and do lambda capture with
1097 // std::move.
1098 class DeliveryTask : public QueuedTask {
1099 public:
1100 DeliveryTask(rtc::scoped_refptr<RTCStatsCollector> collector,
1101 rtc::scoped_refptr<const RTCStatsReport> cached_report,
1102 std::vector<RequestInfo> requests)
1103 : collector_(collector),
1104 cached_report_(cached_report),
1105 requests_(std::move(requests)) {}
1106 bool Run() override {
1107 collector_->DeliverCachedReport(cached_report_, std::move(requests_));
1108 return true;
1109 }
1110
1111 private:
1112 rtc::scoped_refptr<RTCStatsCollector> collector_;
1113 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
1114 std::vector<RequestInfo> requests_;
1115 };
1116 signaling_thread_->PostTask(std::make_unique<DeliveryTask>(
1117 this, cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 08:36:501118 } else if (!num_pending_partial_reports_) {
1119 // Only start gathering stats if we're not already gathering stats. In the
1120 // case of already gathering stats, |callback_| will be invoked when there
1121 // are no more pending partial reports.
1122
1123 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
1124 // UTC), in microseconds. The system clock could be modified and is not
1125 // necessarily monotonically increasing.
nissecdf37a92016-09-14 06:41:471126 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 08:36:501127
hbosf415f8a2017-01-02 12:28:511128 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 08:36:501129 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 12:58:021130
Henrik Boström180faeb2020-11-13 08:05:211131 // Prepare |transceiver_stats_infos_| and |call_stats_| for use in
hbos84abeb12017-01-16 14:16:441132 // |ProducePartialResultsOnNetworkThread| and
1133 // |ProducePartialResultsOnSignalingThread|.
Henrik Boström180faeb2020-11-13 08:05:211134 PrepareTransceiverStatsInfosAndCallStats_s_w();
Steve Anton7eca0932018-03-30 22:18:411135 // Prepare |transport_names_| for use in
1136 // |ProducePartialResultsOnNetworkThread|.
1137 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 18:34:401138
Henrik Boström40b030e2019-02-28 08:49:311139 // Don't touch |network_report_| on the signaling thread until
1140 // ProducePartialResultsOnNetworkThread() has signaled the
1141 // |network_report_event_|.
1142 network_report_event_.Reset();
Niels Möller4bab23f2021-01-18 08:24:331143 rtc::scoped_refptr<RTCStatsCollector> collector(this);
1144 network_thread_->PostTask(RTC_FROM_HERE, [collector, timestamp_us] {
1145 collector->ProducePartialResultsOnNetworkThread(timestamp_us);
1146 });
hbosf415f8a2017-01-02 12:28:511147 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 14:57:361148 }
hbosd565b732016-08-30 21:04:351149}
1150
1151void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 08:36:501152 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 21:04:351153 cached_report_ = nullptr;
1154}
1155
hbosb78306a2016-12-19 13:06:571156void RTCStatsCollector::WaitForPendingRequest() {
1157 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 08:49:311158 // If a request is pending, blocks until the |network_report_event_| is
1159 // signaled and then delivers the result. Otherwise this is a NO-OP.
1160 MergeNetworkReport_s();
hbosb78306a2016-12-19 13:06:571161}
1162
hbosc82f2e12016-09-05 08:36:501163void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1164 int64_t timestamp_us) {
1165 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501166 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1167
Henrik Boström40b030e2019-02-28 08:49:311168 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501169
Henrik Boström40b030e2019-02-28 08:49:311170 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1171 partial_report_.get());
hbosc82f2e12016-09-05 08:36:501172
Henrik Boström40b030e2019-02-28 08:49:311173 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1174 // signaling thread, so it is always the first partial result delivered on the
1175 // signaling thread. The request is not complete until MergeNetworkReport_s()
1176 // happens; we don't have to do anything here.
1177 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1178 --num_pending_partial_reports_;
1179}
1180
1181void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1182 int64_t timestamp_us,
1183 RTCStatsReport* partial_report) {
1184 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501185 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1186
Henrik Boström40b030e2019-02-28 08:49:311187 ProduceDataChannelStats_s(timestamp_us, partial_report);
1188 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1189 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 13:49:421190 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311191 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 08:36:501192}
1193
hbosc82f2e12016-09-05 08:36:501194void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
1195 int64_t timestamp_us) {
1196 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501197 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1198
Ivo Creusen8d8ffdb2019-04-30 07:45:211199 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 08:49:311200 // |network_report_event_| is reset before this method is invoked.
1201 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501202
Steve Anton5dfde182018-02-06 18:34:401203 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 22:18:411204 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 18:34:401205 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1206 PrepareTransportCertificateStats_n(transport_stats_by_name);
1207
Henrik Boström40b030e2019-02-28 08:49:311208 ProducePartialResultsOnNetworkThreadImpl(
1209 timestamp_us, transport_stats_by_name, transport_cert_stats,
1210 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:401211
Henrik Boström40b030e2019-02-28 08:49:311212 // Signal that it is now safe to touch |network_report_| on the signaling
1213 // thread, and post a task to merge it into the final results.
1214 network_report_event_.Set();
Niels Möller4bab23f2021-01-18 08:24:331215 rtc::scoped_refptr<RTCStatsCollector> collector(this);
Henrik Boström40b030e2019-02-28 08:49:311216 signaling_thread_->PostTask(
Niels Möller4bab23f2021-01-18 08:24:331217 RTC_FROM_HERE, [collector] { collector->MergeNetworkReport_s(); });
Henrik Boström05d43c62019-02-15 09:23:081218}
1219
Henrik Boström40b030e2019-02-28 08:49:311220void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1221 int64_t timestamp_us,
1222 const std::map<std::string, cricket::TransportStats>&
1223 transport_stats_by_name,
1224 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1225 RTCStatsReport* partial_report) {
1226 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501227 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1228
Henrik Boström40b030e2019-02-28 08:49:311229 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1230 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1231 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1232 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311233 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1234 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 11:40:251235 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1236 partial_report);
Henrik Boström40b030e2019-02-28 08:49:311237}
1238
1239void RTCStatsCollector::MergeNetworkReport_s() {
1240 RTC_DCHECK(signaling_thread_->IsCurrent());
1241 // The |network_report_event_| must be signaled for it to be safe to touch
1242 // |network_report_|. This is normally not blocking, but if
1243 // WaitForPendingRequest() is called while a request is pending, we might have
1244 // to wait until the network thread is done touching |network_report_|.
1245 network_report_event_.Wait(rtc::Event::kForever);
1246 if (!network_report_) {
1247 // Normally, MergeNetworkReport_s() is executed because it is posted from
1248 // the network thread. But if WaitForPendingRequest() is called while a
1249 // request is pending, an early call to MergeNetworkReport_s() is made,
1250 // merging the report and setting |network_report_| to null. If so, when the
1251 // previously posted MergeNetworkReport_s() is later executed, the report is
1252 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 08:36:501253 return;
1254 }
Mirko Bonadeica890ee2019-02-15 21:10:401255 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 08:49:311256 RTC_DCHECK(partial_report_);
1257 partial_report_->TakeMembersFrom(network_report_);
1258 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:401259 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 08:49:311260 // |network_report_| is currently the only partial report collected
1261 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1262 // ready to deliver the result.
1263 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1264 cache_timestamp_us_ = partial_report_timestamp_us_;
1265 cached_report_ = partial_report_;
1266 partial_report_ = nullptr;
1267 transceiver_stats_infos_.clear();
1268 // Trace WebRTC Stats when getStats is called on Javascript.
1269 // This allows access to WebRTC stats from trace logs. To enable them,
1270 // select the "webrtc_stats" category when recording traces.
1271 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1272 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:401273
Henrik Boström40b030e2019-02-28 08:49:311274 // Deliver report and clear |requests_|.
1275 std::vector<RequestInfo> requests;
1276 requests.swap(requests_);
1277 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 08:36:501278}
1279
Taylor Brandstetter25e022f2018-03-08 17:53:471280void RTCStatsCollector::DeliverCachedReport(
1281 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 12:52:561282 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 08:36:501283 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 12:52:561284 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 17:53:471285 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 17:42:251286
Henrik Boström5b3541f2018-03-19 12:52:561287 for (const RequestInfo& request : requests) {
1288 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1289 request.callback()->OnStatsDelivered(cached_report);
1290 } else {
1291 bool filter_by_sender_selector;
1292 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1293 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1294 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1295 filter_by_sender_selector = true;
1296 sender_selector = request.sender_selector();
1297 } else {
1298 RTC_DCHECK(request.filter_mode() ==
1299 RequestInfo::FilterMode::kReceiverSelector);
1300 filter_by_sender_selector = false;
1301 receiver_selector = request.receiver_selector();
1302 }
1303 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1304 filter_by_sender_selector, cached_report, sender_selector,
1305 receiver_selector));
1306 }
hbosc82f2e12016-09-05 08:36:501307 }
hbosd565b732016-08-30 21:04:351308}
1309
hbosdf6075a2016-12-19 12:58:021310void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 11:00:051311 int64_t timestamp_us,
1312 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce02016-10-03 21:16:561313 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021314 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501315 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1316
hbos02ba2112016-10-28 12:14:531317 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1318 if (transport_cert_stats_pair.second.local) {
1319 ProduceCertificateStatsFromSSLCertificateStats(
1320 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce02016-10-03 21:16:561321 }
hbos02ba2112016-10-28 12:14:531322 if (transport_cert_stats_pair.second.remote) {
1323 ProduceCertificateStatsFromSSLCertificateStats(
1324 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce02016-10-03 21:16:561325 }
1326 }
1327}
1328
hbosdf6075a2016-12-19 12:58:021329void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 23:19:501330 int64_t timestamp_us,
1331 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 10:32:061332 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021333 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501334 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1335
Steve Anton57858b32018-02-15 23:19:501336 for (const auto& stats : transceiver_stats_infos) {
1337 if (!stats.mid) {
1338 continue;
hbos0adb8282016-11-23 10:32:061339 }
Philipp Hancke95157a02020-11-16 19:08:271340 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1341 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1342
Steve Anton57858b32018-02-15 23:19:501343 const cricket::VoiceMediaInfo* voice_media_info =
1344 stats.track_media_info_map->voice_media_info();
1345 const cricket::VideoMediaInfo* video_media_info =
1346 stats.track_media_info_map->video_media_info();
1347 // Audio
1348 if (voice_media_info) {
1349 // Inbound
1350 for (const auto& pair : voice_media_info->receive_codecs) {
1351 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271352 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501353 }
1354 // Outbound
1355 for (const auto& pair : voice_media_info->send_codecs) {
1356 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271357 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501358 }
Guido Urdanetaee2388f2018-02-15 16:36:191359 }
Steve Anton57858b32018-02-15 23:19:501360 // Video
1361 if (video_media_info) {
1362 // Inbound
1363 for (const auto& pair : video_media_info->receive_codecs) {
1364 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271365 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501366 }
1367 // Outbound
1368 for (const auto& pair : video_media_info->send_codecs) {
1369 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271370 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501371 }
hbos0adb8282016-11-23 10:32:061372 }
1373 }
1374}
1375
hboscc555c52016-10-18 19:48:311376void RTCStatsCollector::ProduceDataChannelStats_s(
Jonas Olssona4d87372019-07-05 17:08:331377 int64_t timestamp_us,
1378 RTCStatsReport* report) const {
Tomas Gunnarsson2e94de52020-06-16 14:54:101379 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501380 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Taylor Brandstetter3a034e12020-07-09 22:32:341381 std::vector<DataChannelStats> data_stats = pc_->GetDataChannelStats();
Tomas Gunnarsson2e94de52020-06-16 14:54:101382 for (const auto& stats : data_stats) {
hboscc555c52016-10-18 19:48:311383 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1384 new RTCDataChannelStats(
Tomas Gunnarsson2e94de52020-06-16 14:54:101385 "RTCDataChannel_" + rtc::ToString(stats.internal_id),
hboscc555c52016-10-18 19:48:311386 timestamp_us));
Tomas Gunnarsson2e94de52020-06-16 14:54:101387 data_channel_stats->label = std::move(stats.label);
1388 data_channel_stats->protocol = std::move(stats.protocol);
1389 data_channel_stats->data_channel_identifier = stats.id;
1390 data_channel_stats->state = DataStateToRTCDataChannelState(stats.state);
1391 data_channel_stats->messages_sent = stats.messages_sent;
1392 data_channel_stats->bytes_sent = stats.bytes_sent;
1393 data_channel_stats->messages_received = stats.messages_received;
1394 data_channel_stats->bytes_received = stats.bytes_received;
hboscc555c52016-10-18 19:48:311395 report->AddStats(std::move(data_channel_stats));
1396 }
1397}
1398
hbosdf6075a2016-12-19 12:58:021399void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 13:44:031400 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 18:34:401401 const std::map<std::string, cricket::TransportStats>&
1402 transport_stats_by_name,
stefanf79ade12017-06-02 13:44:031403 const Call::Stats& call_stats,
1404 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021405 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501406 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1407
Steve Anton5dfde182018-02-06 18:34:401408 for (const auto& entry : transport_stats_by_name) {
1409 const std::string& transport_name = entry.first;
1410 const cricket::TransportStats& transport_stats = entry.second;
1411 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 09:50:141412 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401413 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 21:54:491414 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:271415 channel_stats.ice_transport_stats.connection_infos) {
hbosc47a0c32016-10-11 21:54:491416 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 11:00:051417 new RTCIceCandidatePairStats(
1418 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1419 timestamp_us));
hbosc47a0c32016-10-11 21:54:491420
hbos0583b282016-11-30 09:50:141421 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 09:18:471422 // TODO(hbos): There could be other candidates that are not paired with
1423 // anything. We don't have a complete list. Local candidates come from
1424 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 13:08:341425 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 12:14:531426 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311427 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 12:14:531428 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311429 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 16:08:181430 candidate_pair_stats->state =
1431 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1432 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 09:38:081433 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 21:54:491434 // TODO(hbos): This writable is different than the spec. It goes to
1435 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 13:08:341436 // https://crbug.com/633550
hbosc47a0c32016-10-11 21:54:491437 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 21:54:491438 candidate_pair_stats->bytes_sent =
1439 static_cast<uint64_t>(info.sent_total_bytes);
1440 candidate_pair_stats->bytes_received =
1441 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 14:34:471442 candidate_pair_stats->total_round_trip_time =
1443 static_cast<double>(info.total_round_trip_time_ms) /
1444 rtc::kNumMillisecsPerSec;
1445 if (info.current_round_trip_time_ms) {
1446 candidate_pair_stats->current_round_trip_time =
1447 static_cast<double>(*info.current_round_trip_time_ms) /
1448 rtc::kNumMillisecsPerSec;
1449 }
stefanf79ade12017-06-02 13:44:031450 if (info.best_connection) {
hbos338f78a2017-02-07 14:41:211451 // The bandwidth estimations we have are for the selected candidate
1452 // pair ("info.best_connection").
stefanf79ade12017-06-02 13:44:031453 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1454 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1455 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211456 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 13:44:031457 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211458 }
stefanf79ade12017-06-02 13:44:031459 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211460 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 13:44:031461 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211462 }
1463 }
hbosd82f5122016-12-09 12:12:391464 candidate_pair_stats->requests_received =
1465 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 09:22:531466 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1467 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491468 candidate_pair_stats->responses_received =
1469 static_cast<uint64_t>(info.recv_ping_responses);
1470 candidate_pair_stats->responses_sent =
1471 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 09:22:531472 RTC_DCHECK_GE(info.sent_ping_requests_total,
1473 info.sent_ping_requests_before_first_response);
1474 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1475 info.sent_ping_requests_total -
1476 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491477
1478 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 09:18:471479 }
1480 }
1481 }
1482}
1483
Steve Anton57858b32018-02-15 23:19:501484void RTCStatsCollector::ProduceMediaStreamStats_s(
1485 int64_t timestamp_us,
1486 RTCStatsReport* report) const {
hbos09bc1282016-11-08 14:29:221487 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501488 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:501489
1490 std::map<std::string, std::vector<std::string>> track_ids;
1491
1492 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 16:29:421493 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501494 std::string track_id =
1495 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1496 kSender, sender->internal()->AttachmentId());
1497 for (auto& stream_id : sender->stream_ids()) {
1498 track_ids[stream_id].push_back(track_id);
1499 }
1500 }
Mirko Bonadei739baf02019-01-27 16:29:421501 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501502 std::string track_id =
1503 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1504 kReceiver, receiver->internal()->AttachmentId());
1505 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 23:05:281506 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 23:19:501507 }
1508 }
1509 }
1510
1511 // Build stats for each stream ID known.
1512 for (auto& it : track_ids) {
1513 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1514 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1515 stream_stats->stream_identifier = it.first;
1516 stream_stats->track_ids = it.second;
1517 report->AddStats(std::move(stream_stats));
1518 }
1519}
1520
1521void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1522 int64_t timestamp_us,
1523 RTCStatsReport* report) const {
1524 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501525 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1526
Steve Anton57858b32018-02-15 23:19:501527 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1528 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 16:29:421529 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501530 senders.push_back(sender->internal());
1531 }
1532 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1533 senders, report);
1534
1535 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 16:29:421536 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501537 receivers.push_back(receiver->internal());
1538 }
1539 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1540 receivers, report);
1541 }
hbos09bc1282016-11-08 14:29:221542}
1543
Henrik Boström646fda02019-05-22 13:49:421544void RTCStatsCollector::ProduceMediaSourceStats_s(
1545 int64_t timestamp_us,
1546 RTCStatsReport* report) const {
1547 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501548 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1549
Henrik Boström646fda02019-05-22 13:49:421550 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1551 transceiver_stats_infos_) {
1552 const auto& track_media_info_map =
1553 transceiver_stats_info.track_media_info_map;
1554 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1555 const auto& sender_internal = sender->internal();
1556 const auto& track = sender_internal->track();
1557 if (!track)
1558 continue;
Henrik Boströmd2c336f2019-07-03 15:11:101559 // TODO(https://crbug.com/webrtc/10771): The same track could be attached
1560 // to multiple senders which should result in multiple senders referencing
1561 // the same media-source stats. When all media source related metrics are
1562 // moved to the track's source (e.g. input frame rate is moved from
1563 // cricket::VideoSenderInfo to VideoTrackSourceInterface::Stats and audio
1564 // levels are moved to the corresponding audio track/source object), don't
1565 // create separate media source stats objects on a per-attachment basis.
Henrik Boström646fda02019-05-22 13:49:421566 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1567 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Mirko Bonadei317a1f02019-09-17 15:06:181568 auto audio_source_stats = std::make_unique<RTCAudioSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421569 RTCMediaSourceStatsIDFromKindAndAttachment(
1570 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1571 timestamp_us);
Henrik Boströmd2c336f2019-07-03 15:11:101572 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1573 // SSRC assigned (there shouldn't need to exist a send-stream, created
1574 // by an O/A exchange) in order to read audio media-source stats.
1575 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1576 // value indicating no SSRC.
1577 if (sender_internal->ssrc() != 0) {
1578 auto* voice_sender_info =
1579 track_media_info_map->GetVoiceSenderInfoBySsrc(
1580 sender_internal->ssrc());
1581 if (voice_sender_info) {
1582 audio_source_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
1583 voice_sender_info->audio_level);
1584 audio_source_stats->total_audio_energy =
1585 voice_sender_info->total_input_energy;
1586 audio_source_stats->total_samples_duration =
1587 voice_sender_info->total_input_duration;
1588 }
1589 }
1590 media_source_stats = std::move(audio_source_stats);
Henrik Boström646fda02019-05-22 13:49:421591 } else {
1592 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Mirko Bonadei317a1f02019-09-17 15:06:181593 auto video_source_stats = std::make_unique<RTCVideoSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421594 RTCMediaSourceStatsIDFromKindAndAttachment(
1595 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1596 timestamp_us);
1597 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1598 auto* video_source = video_track->GetSource();
1599 VideoTrackSourceInterface::Stats source_stats;
1600 if (video_source && video_source->GetStats(&source_stats)) {
1601 video_source_stats->width = source_stats.input_width;
1602 video_source_stats->height = source_stats.input_height;
1603 }
Henrik Boströmd2c336f2019-07-03 15:11:101604 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1605 // SSRC assigned (there shouldn't need to exist a send-stream, created
1606 // by an O/A exchange) in order to get framesPerSecond.
1607 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1608 // value indicating no SSRC.
Henrik Boström646fda02019-05-22 13:49:421609 if (sender_internal->ssrc() != 0) {
Henrik Boströmd2c336f2019-07-03 15:11:101610 auto* video_sender_info =
1611 track_media_info_map->GetVideoSenderInfoBySsrc(
1612 sender_internal->ssrc());
1613 if (video_sender_info) {
Henrik Boström646fda02019-05-22 13:49:421614 video_source_stats->frames_per_second =
Henrik Boströmd2c336f2019-07-03 15:11:101615 video_sender_info->framerate_input;
Henrik Boström646fda02019-05-22 13:49:421616 }
1617 }
1618 media_source_stats = std::move(video_source_stats);
1619 }
1620 media_source_stats->track_identifier = track->id();
1621 media_source_stats->kind = track->kind();
1622 report->AddStats(std::move(media_source_stats));
1623 }
1624 }
1625}
1626
hbos6ab97ce02016-10-03 21:16:561627void RTCStatsCollector::ProducePeerConnectionStats_s(
Jonas Olssona4d87372019-07-05 17:08:331628 int64_t timestamp_us,
1629 RTCStatsReport* report) const {
hbosc82f2e12016-09-05 08:36:501630 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501631 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1632
hbosd565b732016-08-30 21:04:351633 std::unique_ptr<RTCPeerConnectionStats> stats(
Jonas Olssona4d87372019-07-05 17:08:331634 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 09:41:091635 stats->data_channels_opened = internal_record_.data_channels_opened;
1636 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce02016-10-03 21:16:561637 report->AddStats(std::move(stats));
hbosd565b732016-08-30 21:04:351638}
1639
hbosdf6075a2016-12-19 12:58:021640void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 19:44:481641 int64_t timestamp_us,
Steve Anton57858b32018-02-15 23:19:501642 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 14:16:441643 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021644 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501645 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
hbos6ded1902016-11-01 08:50:461646
Steve Anton57858b32018-02-15 23:19:501647 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1648 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1649 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1650 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1651 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1652 } else {
1653 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:191654 }
Steve Anton56bae8d2018-02-15 00:07:421655 }
Steve Anton57858b32018-02-15 23:19:501656}
1657
1658void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1659 int64_t timestamp_us,
1660 const RtpTransceiverStatsInfo& stats,
1661 RTCStatsReport* report) const {
Henrik Boströme88c95e2020-07-08 09:18:501662 RTC_DCHECK(network_thread_->IsCurrent());
1663 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1664
Steve Anton57858b32018-02-15 23:19:501665 if (!stats.mid || !stats.transport_name) {
1666 return;
1667 }
1668 RTC_DCHECK(stats.track_media_info_map);
1669 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1670 RTC_DCHECK(track_media_info_map.voice_media_info());
1671 std::string mid = *stats.mid;
1672 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1673 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1674 // Inbound
1675 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1676 track_media_info_map.voice_media_info()->receivers) {
1677 if (!voice_receiver_info.connected())
1678 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181679 auto inbound_audio = std::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501680 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1681 timestamp_us);
1682 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1683 inbound_audio.get());
1684 // TODO(hta): This lookup should look for the sender, not the track.
1685 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1686 track_media_info_map.GetAudioTrack(voice_receiver_info);
1687 if (audio_track) {
1688 inbound_audio->track_id =
1689 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1690 kReceiver,
1691 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 08:50:461692 }
Steve Anton57858b32018-02-15 23:19:501693 inbound_audio->transport_id = transport_id;
1694 report->AddStats(std::move(inbound_audio));
1695 }
1696 // Outbound
Henrik Boström4f40fa52019-12-19 12:27:271697 std::map<std::string, RTCOutboundRTPStreamStats*> audio_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501698 for (const cricket::VoiceSenderInfo& voice_sender_info :
1699 track_media_info_map.voice_media_info()->senders) {
1700 if (!voice_sender_info.connected())
1701 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181702 auto outbound_audio = std::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501703 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1704 timestamp_us);
1705 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1706 outbound_audio.get());
1707 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1708 track_media_info_map.GetAudioTrack(voice_sender_info);
1709 if (audio_track) {
Henrik Boström646fda02019-05-22 13:49:421710 int attachment_id =
1711 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 23:19:501712 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 13:49:421713 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1714 attachment_id);
1715 outbound_audio->media_source_id =
1716 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1717 attachment_id);
Steve Anton56bae8d2018-02-15 00:07:421718 }
Steve Anton57858b32018-02-15 23:19:501719 outbound_audio->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271720 audio_outbound_rtps.insert(
1721 std::make_pair(outbound_audio->id(), outbound_audio.get()));
Steve Anton57858b32018-02-15 23:19:501722 report->AddStats(std::move(outbound_audio));
1723 }
Henrik Boström883eefc2019-05-27 11:40:251724 // Remote-inbound
1725 // These are Report Block-based, information sent from the remote endpoint,
1726 // providing metrics about our Outbound streams. We take advantage of the fact
1727 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1728 // been added to the report.
1729 for (const cricket::VoiceSenderInfo& voice_sender_info :
1730 track_media_info_map.voice_media_info()->senders) {
1731 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1732 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071733 report_block_data, cricket::MEDIA_TYPE_AUDIO, audio_outbound_rtps,
1734 *report));
Henrik Boström883eefc2019-05-27 11:40:251735 }
1736 }
Steve Anton57858b32018-02-15 23:19:501737}
1738
1739void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1740 int64_t timestamp_us,
1741 const RtpTransceiverStatsInfo& stats,
1742 RTCStatsReport* report) const {
Henrik Boströme88c95e2020-07-08 09:18:501743 RTC_DCHECK(network_thread_->IsCurrent());
1744 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1745
Steve Anton57858b32018-02-15 23:19:501746 if (!stats.mid || !stats.transport_name) {
1747 return;
1748 }
1749 RTC_DCHECK(stats.track_media_info_map);
1750 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1751 RTC_DCHECK(track_media_info_map.video_media_info());
1752 std::string mid = *stats.mid;
1753 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1754 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1755 // Inbound
1756 for (const cricket::VideoReceiverInfo& video_receiver_info :
1757 track_media_info_map.video_media_info()->receivers) {
1758 if (!video_receiver_info.connected())
1759 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181760 auto inbound_video = std::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501761 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1762 timestamp_us);
1763 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1764 inbound_video.get());
1765 rtc::scoped_refptr<VideoTrackInterface> video_track =
1766 track_media_info_map.GetVideoTrack(video_receiver_info);
1767 if (video_track) {
1768 inbound_video->track_id =
1769 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1770 kReceiver,
1771 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1772 }
1773 inbound_video->transport_id = transport_id;
1774 report->AddStats(std::move(inbound_video));
1775 }
1776 // Outbound
Henrik Boström4f40fa52019-12-19 12:27:271777 std::map<std::string, RTCOutboundRTPStreamStats*> video_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501778 for (const cricket::VideoSenderInfo& video_sender_info :
Eldar Rello9276e2c2020-06-10 14:53:391779 track_media_info_map.video_media_info()->senders) {
Steve Anton57858b32018-02-15 23:19:501780 if (!video_sender_info.connected())
1781 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181782 auto outbound_video = std::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501783 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1784 timestamp_us);
Eldar Rello9276e2c2020-06-10 14:53:391785 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1786 outbound_video.get());
Steve Anton57858b32018-02-15 23:19:501787 rtc::scoped_refptr<VideoTrackInterface> video_track =
1788 track_media_info_map.GetVideoTrack(video_sender_info);
1789 if (video_track) {
Henrik Boström646fda02019-05-22 13:49:421790 int attachment_id =
1791 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 23:19:501792 outbound_video->track_id =
Henrik Boström646fda02019-05-22 13:49:421793 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1794 attachment_id);
1795 outbound_video->media_source_id =
1796 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1797 attachment_id);
Steve Anton57858b32018-02-15 23:19:501798 }
1799 outbound_video->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271800 video_outbound_rtps.insert(
1801 std::make_pair(outbound_video->id(), outbound_video.get()));
Steve Anton57858b32018-02-15 23:19:501802 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 08:50:461803 }
Henrik Boström883eefc2019-05-27 11:40:251804 // Remote-inbound
1805 // These are Report Block-based, information sent from the remote endpoint,
1806 // providing metrics about our Outbound streams. We take advantage of the fact
1807 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1808 // been added to the report.
1809 for (const cricket::VideoSenderInfo& video_sender_info :
1810 track_media_info_map.video_media_info()->senders) {
1811 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1812 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071813 report_block_data, cricket::MEDIA_TYPE_VIDEO, video_outbound_rtps,
1814 *report));
Henrik Boström883eefc2019-05-27 11:40:251815 }
1816 }
hbos6ded1902016-11-01 08:50:461817}
1818
hbosdf6075a2016-12-19 12:58:021819void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 18:34:401820 int64_t timestamp_us,
1821 const std::map<std::string, cricket::TransportStats>&
1822 transport_stats_by_name,
hbos2fa7c672016-10-24 11:00:051823 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1824 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021825 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501826 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1827
Steve Anton5dfde182018-02-06 18:34:401828 for (const auto& entry : transport_stats_by_name) {
1829 const std::string& transport_name = entry.first;
1830 const cricket::TransportStats& transport_stats = entry.second;
1831
hbos2fa7c672016-10-24 11:00:051832 // Get reference to RTCP channel, if it exists.
1833 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 18:34:401834 for (const cricket::TransportChannelStats& channel_stats :
1835 transport_stats.channel_stats) {
Jonas Olssona4d87372019-07-05 17:08:331836 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
hbos2fa7c672016-10-24 11:00:051837 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401838 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 11:00:051839 break;
1840 }
1841 }
1842
1843 // Get reference to local and remote certificates of this transport, if they
1844 // exist.
Steve Anton5dfde182018-02-06 18:34:401845 const auto& certificate_stats_it =
1846 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 11:00:051847 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1848 std::string local_certificate_id;
1849 if (certificate_stats_it->second.local) {
1850 local_certificate_id = RTCCertificateIDFromFingerprint(
1851 certificate_stats_it->second.local->fingerprint);
1852 }
1853 std::string remote_certificate_id;
1854 if (certificate_stats_it->second.remote) {
1855 remote_certificate_id = RTCCertificateIDFromFingerprint(
1856 certificate_stats_it->second.remote->fingerprint);
1857 }
1858
1859 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 18:34:401860 for (const cricket::TransportChannelStats& channel_stats :
1861 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 11:00:051862 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 18:34:401863 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1864 transport_name, channel_stats.component),
1865 timestamp_us));
hbos2fa7c672016-10-24 11:00:051866 transport_stats->bytes_sent = 0;
Artem Titovedacbd52020-07-06 14:06:371867 transport_stats->packets_sent = 0;
hbos2fa7c672016-10-24 11:00:051868 transport_stats->bytes_received = 0;
Artem Titovedacbd52020-07-06 14:06:371869 transport_stats->packets_received = 0;
Jonas Olssona4d87372019-07-05 17:08:331870 transport_stats->dtls_state =
1871 DtlsTransportStateToRTCDtlsTransportState(channel_stats.dtls_state);
Jonas Oreland149dc722019-08-28 06:10:271872 transport_stats->selected_candidate_pair_changes =
1873 channel_stats.ice_transport_stats.selected_candidate_pair_changes;
hbos2fa7c672016-10-24 11:00:051874 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:271875 channel_stats.ice_transport_stats.connection_infos) {
hbos2fa7c672016-10-24 11:00:051876 *transport_stats->bytes_sent += info.sent_total_bytes;
Artem Titovedacbd52020-07-06 14:06:371877 *transport_stats->packets_sent +=
1878 info.sent_total_packets - info.sent_discarded_packets;
hbos2fa7c672016-10-24 11:00:051879 *transport_stats->bytes_received += info.recv_total_bytes;
Artem Titovedacbd52020-07-06 14:06:371880 *transport_stats->packets_received += info.packets_received;
hbos2fa7c672016-10-24 11:00:051881 if (info.best_connection) {
hbos2fa7c672016-10-24 11:00:051882 transport_stats->selected_candidate_pair_id =
1883 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1884 }
1885 }
1886 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1887 !rtcp_transport_stats_id.empty()) {
1888 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1889 }
1890 if (!local_certificate_id.empty())
1891 transport_stats->local_certificate_id = local_certificate_id;
1892 if (!remote_certificate_id.empty())
1893 transport_stats->remote_certificate_id = remote_certificate_id;
Harald Alvestrand5cb78072019-10-28 08:51:171894 // Crypto information
1895 if (channel_stats.ssl_version_bytes) {
1896 char bytes[5];
1897 snprintf(bytes, sizeof(bytes), "%04X", channel_stats.ssl_version_bytes);
1898 transport_stats->tls_version = bytes;
1899 }
1900 if (channel_stats.ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
1901 rtc::SSLStreamAdapter::SslCipherSuiteToName(
1902 channel_stats.ssl_cipher_suite)
1903 .length()) {
1904 transport_stats->dtls_cipher =
1905 rtc::SSLStreamAdapter::SslCipherSuiteToName(
1906 channel_stats.ssl_cipher_suite);
1907 }
1908 if (channel_stats.srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
1909 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite)
1910 .length()) {
1911 transport_stats->srtp_cipher =
1912 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite);
1913 }
hbos2fa7c672016-10-24 11:00:051914 report->AddStats(std::move(transport_stats));
1915 }
1916 }
1917}
1918
1919std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 12:58:021920RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 18:34:401921 const std::map<std::string, cricket::TransportStats>&
1922 transport_stats_by_name) const {
hbosdf6075a2016-12-19 12:58:021923 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501924 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1925
hbos2fa7c672016-10-24 11:00:051926 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 18:34:401927 for (const auto& entry : transport_stats_by_name) {
1928 const std::string& transport_name = entry.first;
1929
hbos2fa7c672016-10-24 11:00:051930 CertificateStatsPair certificate_stats_pair;
1931 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 18:34:401932 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 11:00:051933 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 08:16:261934 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 11:00:051935 }
Steve Anton5dfde182018-02-06 18:34:401936
Taylor Brandstetterc3928662018-02-23 21:04:511937 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1938 pc_->GetRemoteSSLCertChain(transport_name);
1939 if (remote_cert_chain) {
1940 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 11:00:051941 }
Steve Anton5dfde182018-02-06 18:34:401942
hbos2fa7c672016-10-24 11:00:051943 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 18:34:401944 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 11:00:051945 }
1946 return transport_cert_stats;
1947}
1948
Henrik Boström180faeb2020-11-13 08:05:211949void RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w() {
Henrik Boströme88c95e2020-07-08 09:18:501950 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton56bae8d2018-02-15 00:07:421951
Henrik Boström180faeb2020-11-13 08:05:211952 transceiver_stats_infos_.clear();
Steve Anton57858b32018-02-15 23:19:501953 // These are used to invoke GetStats for all the media channels together in
1954 // one worker thread hop.
1955 std::map<cricket::VoiceMediaChannel*,
1956 std::unique_ptr<cricket::VoiceMediaInfo>>
1957 voice_stats;
1958 std::map<cricket::VideoMediaChannel*,
1959 std::unique_ptr<cricket::VideoMediaInfo>>
1960 video_stats;
1961
Henrik Boströme88c95e2020-07-08 09:18:501962 {
1963 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:501964
Henrik Boströme88c95e2020-07-08 09:18:501965 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1966 cricket::MediaType media_type = transceiver->media_type();
Steve Anton57858b32018-02-15 23:19:501967
Henrik Boströme88c95e2020-07-08 09:18:501968 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1969 // stats have been fetched on the worker thread.
Henrik Boström180faeb2020-11-13 08:05:211970 transceiver_stats_infos_.emplace_back();
1971 RtpTransceiverStatsInfo& stats = transceiver_stats_infos_.back();
Henrik Boströme88c95e2020-07-08 09:18:501972 stats.transceiver = transceiver->internal();
1973 stats.media_type = media_type;
Steve Anton57858b32018-02-15 23:19:501974
Henrik Boströme88c95e2020-07-08 09:18:501975 cricket::ChannelInterface* channel = transceiver->internal()->channel();
1976 if (!channel) {
1977 // The remaining fields require a BaseChannel.
1978 continue;
1979 }
Steve Anton57858b32018-02-15 23:19:501980
Henrik Boströme88c95e2020-07-08 09:18:501981 stats.mid = channel->content_name();
1982 stats.transport_name = channel->transport_name();
1983
1984 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1985 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1986 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1987 voice_stats.end());
1988 voice_stats[voice_channel->media_channel()] =
1989 std::make_unique<cricket::VoiceMediaInfo>();
1990 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1991 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1992 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1993 video_stats.end());
1994 video_stats[video_channel->media_channel()] =
1995 std::make_unique<cricket::VideoMediaInfo>();
1996 } else {
1997 RTC_NOTREACHED();
1998 }
Steve Anton57858b32018-02-15 23:19:501999 }
Guido Urdanetaee2388f2018-02-15 16:36:192000 }
Steve Anton57858b32018-02-15 23:19:502001
Henrik Boström180faeb2020-11-13 08:05:212002 // We jump to the worker thread and call GetStats() on each media channel as
2003 // well as GetCallStats(). At the same time we construct the
2004 // TrackMediaInfoMaps, which also needs info from the worker thread. This
2005 // minimizes the number of thread jumps.
Steve Anton57858b32018-02-15 23:19:502006 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Henrik Boström6fafbe32020-07-08 08:37:002007 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2008
Steve Anton57858b32018-02-15 23:19:502009 for (const auto& entry : voice_stats) {
Niels Möller6b4d9622020-09-14 08:47:502010 if (!entry.first->GetStats(entry.second.get(),
2011 /*get_and_clear_legacy_stats=*/false)) {
Steve Anton57858b32018-02-15 23:19:502012 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
2013 }
2014 }
2015 for (const auto& entry : video_stats) {
2016 if (!entry.first->GetStats(entry.second.get())) {
2017 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
2018 }
2019 }
Steve Anton57858b32018-02-15 23:19:502020
Henrik Boström6fafbe32020-07-08 08:37:002021 // Create the TrackMediaInfoMap for each transceiver stats object.
Henrik Boström180faeb2020-11-13 08:05:212022 for (auto& stats : transceiver_stats_infos_) {
Henrik Boström6fafbe32020-07-08 08:37:002023 auto transceiver = stats.transceiver;
2024 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
2025 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
2026 if (transceiver->channel()) {
2027 cricket::MediaType media_type = transceiver->media_type();
2028 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2029 auto* voice_channel =
2030 static_cast<cricket::VoiceChannel*>(transceiver->channel());
2031 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
2032 voice_media_info =
2033 std::move(voice_stats[voice_channel->media_channel()]);
2034 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2035 auto* video_channel =
2036 static_cast<cricket::VideoChannel*>(transceiver->channel());
2037 RTC_DCHECK(video_stats[video_channel->media_channel()]);
2038 video_media_info =
2039 std::move(video_stats[video_channel->media_channel()]);
2040 }
Steve Anton57858b32018-02-15 23:19:502041 }
Henrik Boström6fafbe32020-07-08 08:37:002042 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
2043 for (const auto& sender : transceiver->senders()) {
2044 senders.push_back(sender->internal());
2045 }
2046 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
2047 for (const auto& receiver : transceiver->receivers()) {
2048 receivers.push_back(receiver->internal());
2049 }
2050 stats.track_media_info_map = std::make_unique<TrackMediaInfoMap>(
2051 std::move(voice_media_info), std::move(video_media_info), senders,
2052 receivers);
Steve Anton57858b32018-02-15 23:19:502053 }
Steve Anton57858b32018-02-15 23:19:502054
Henrik Boström180faeb2020-11-13 08:05:212055 call_stats_ = pc_->GetCallStats();
2056 });
hbos0adb8282016-11-23 10:32:062057}
2058
Steve Anton7eca0932018-03-30 22:18:412059std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
Henrik Boströme88c95e2020-07-08 09:18:502060 RTC_DCHECK(signaling_thread_->IsCurrent());
2061 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2062
Steve Anton7eca0932018-03-30 22:18:412063 std::set<std::string> transport_names;
2064 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
2065 if (transceiver->internal()->channel()) {
2066 transport_names.insert(
2067 transceiver->internal()->channel()->transport_name());
2068 }
2069 }
2070 if (pc_->rtp_data_channel()) {
2071 transport_names.insert(pc_->rtp_data_channel()->transport_name());
2072 }
2073 if (pc_->sctp_transport_name()) {
2074 transport_names.insert(*pc_->sctp_transport_name());
2075 }
2076 return transport_names;
2077}
2078
Taylor Brandstetter3a034e12020-07-09 22:32:342079void RTCStatsCollector::OnRtpDataChannelCreated(RtpDataChannel* channel) {
hbos82ebe022016-11-14 09:41:092080 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
2081 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
2082}
2083
Taylor Brandstetter3a034e12020-07-09 22:32:342084void RTCStatsCollector::OnSctpDataChannelCreated(SctpDataChannel* channel) {
2085 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
2086 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
2087}
2088
2089void RTCStatsCollector::OnDataChannelOpened(DataChannelInterface* channel) {
hbos82ebe022016-11-14 09:41:092090 RTC_DCHECK(signaling_thread_->IsCurrent());
Jonas Olssona4d87372019-07-05 17:08:332091 bool result = internal_record_.opened_data_channels
2092 .insert(reinterpret_cast<uintptr_t>(channel))
2093 .second;
hbos82ebe022016-11-14 09:41:092094 ++internal_record_.data_channels_opened;
2095 RTC_DCHECK(result);
2096}
2097
Taylor Brandstetter3a034e12020-07-09 22:32:342098void RTCStatsCollector::OnDataChannelClosed(DataChannelInterface* channel) {
hbos82ebe022016-11-14 09:41:092099 RTC_DCHECK(signaling_thread_->IsCurrent());
2100 // Only channels that have been fully opened (and have increased the
2101 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 10:14:142102 if (internal_record_.opened_data_channels.erase(
2103 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 09:41:092104 ++internal_record_.data_channels_closed;
2105 }
2106}
2107
hboscc555c52016-10-18 19:48:312108const char* CandidateTypeToRTCIceCandidateTypeForTesting(
2109 const std::string& type) {
2110 return CandidateTypeToRTCIceCandidateType(type);
2111}
2112
2113const char* DataStateToRTCDataChannelStateForTesting(
2114 DataChannelInterface::DataState state) {
2115 return DataStateToRTCDataChannelState(state);
2116}
2117
hbosd565b732016-08-30 21:04:352118} // namespace webrtc