blob: 122ae9ff5fc65fc4701bf831689e652fa09ef815 [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;
Di Wu86f04ad2021-03-01 07:36:03544 remote_inbound->fraction_lost =
545 static_cast<double>(report_block.fraction_lost) / (1 << 8);
Henrik Boström883eefc2019-05-27 11:40:25546 remote_inbound->round_trip_time =
547 static_cast<double>(report_block_data.last_rtt_ms()) /
548 rtc::kNumMillisecsPerSec;
Di Wu88a51b22021-03-01 19:22:06549 remote_inbound->total_round_trip_time =
550 static_cast<double>(report_block_data.sum_rtt_ms()) /
551 rtc::kNumMillisecsPerSec;
552 remote_inbound->round_trip_time_measurements =
553 report_block_data.num_rtts();
Henrik Boström883eefc2019-05-27 11:40:25554
555 std::string local_id = RTCOutboundRTPStreamStatsIDFromSSRC(
556 media_type == cricket::MEDIA_TYPE_AUDIO, report_block.source_ssrc);
Henrik Boström4f40fa52019-12-19 12:27:27557 // Look up local stat from |outbound_rtps| where the pointers are non-const.
558 auto local_id_it = outbound_rtps.find(local_id);
559 if (local_id_it != outbound_rtps.end()) {
Henrik Boström883eefc2019-05-27 11:40:25560 remote_inbound->local_id = local_id;
Henrik Boström4f40fa52019-12-19 12:27:27561 auto& outbound_rtp = *local_id_it->second;
562 outbound_rtp.remote_id = remote_inbound->id();
Henrik Boström883eefc2019-05-27 11:40:25563 // The RTP/RTCP transport is obtained from the
564 // RTCOutboundRtpStreamStats's transport.
565 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
566 ? report.Get(*outbound_rtp.transport_id)
567 : nullptr;
568 if (transport_from_id) {
569 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
570 // If RTP and RTCP are not multiplexed, there is a separate RTCP
571 // transport paired with the RTP transport, otherwise the same
572 // transport is used for RTCP and RTP.
573 remote_inbound->transport_id =
574 transport.rtcp_transport_stats_id.is_defined()
575 ? *transport.rtcp_transport_stats_id
576 : *outbound_rtp.transport_id;
577 }
578 // We're assuming the same codec is used on both ends. However if the
579 // codec is switched out on the fly we may have received a Report Block
580 // based on the previous codec and there is no way to tell which point in
581 // time the codec changed for the remote end.
582 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
583 ? report.Get(*outbound_rtp.codec_id)
584 : nullptr;
585 if (codec_from_id) {
586 remote_inbound->codec_id = *outbound_rtp.codec_id;
587 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
588 if (codec.clock_rate.is_defined()) {
589 // The Report Block jitter is expressed in RTP timestamp units
590 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
591 // to seconds we divide by the codec's clock rate.
592 remote_inbound->jitter =
593 static_cast<double>(report_block.jitter) / *codec.clock_rate;
594 }
595 }
596 }
597 return remote_inbound;
598}
599
hbos02ba2112016-10-28 12:14:53600void ProduceCertificateStatsFromSSLCertificateStats(
Jonas Olssona4d87372019-07-05 17:08:33601 int64_t timestamp_us,
602 const rtc::SSLCertificateStats& certificate_stats,
hbos02ba2112016-10-28 12:14:53603 RTCStatsReport* report) {
604 RTCCertificateStats* prev_certificate_stats = nullptr;
605 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
606 s = s->issuer.get()) {
hbos02d2a922016-12-21 09:29:05607 std::string certificate_stats_id =
608 RTCCertificateIDFromFingerprint(s->fingerprint);
609 // It is possible for the same certificate to show up multiple times, e.g.
610 // if local and remote side use the same certificate in a loopback call.
611 // If the report already contains stats for this certificate, skip it.
612 if (report->Get(certificate_stats_id)) {
613 RTC_DCHECK_EQ(s, &certificate_stats);
614 break;
615 }
Jonas Olssona4d87372019-07-05 17:08:33616 RTCCertificateStats* certificate_stats =
617 new RTCCertificateStats(certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 12:14:53618 certificate_stats->fingerprint = s->fingerprint;
619 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
620 certificate_stats->base64_certificate = s->base64_certificate;
621 if (prev_certificate_stats)
622 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
623 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
624 prev_certificate_stats = certificate_stats;
625 }
626}
627
Jonas Olssona4d87372019-07-05 17:08:33628const std::string& ProduceIceCandidateStats(int64_t timestamp_us,
629 const cricket::Candidate& candidate,
630 bool is_local,
631 const std::string& transport_id,
632 RTCStatsReport* report) {
hbos02ba2112016-10-28 12:14:53633 const std::string& id = "RTCIceCandidate_" + candidate.id();
634 const RTCStats* stats = report->Get(id);
635 if (!stats) {
636 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
637 if (is_local)
638 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
639 else
640 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 17:59:31641 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 18:49:36642 if (is_local) {
643 candidate_stats->network_type =
644 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 12:40:08645 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
646 std::string relay_protocol = candidate.relay_protocol();
647 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
648 relay_protocol.compare("tcp") == 0 ||
649 relay_protocol.compare("tls") == 0);
650 candidate_stats->relay_protocol = relay_protocol;
651 }
Gary Liu37e489c2017-11-21 18:49:36652 } else {
653 // We don't expect to know the adapter type of remote candidates.
654 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
655 }
hbos02ba2112016-10-28 12:14:53656 candidate_stats->ip = candidate.address().ipaddr().ToString();
657 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
658 candidate_stats->protocol = candidate.protocol();
Jonas Olssona4d87372019-07-05 17:08:33659 candidate_stats->candidate_type =
660 CandidateTypeToRTCIceCandidateType(candidate.type());
hbos02ba2112016-10-28 12:14:53661 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
662
663 stats = candidate_stats.get();
664 report->AddStats(std::move(candidate_stats));
665 }
666 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
667 : RTCRemoteIceCandidateStats::kType);
668 return stats->id();
669}
670
hbos9e302742017-01-20 10:47:10671std::unique_ptr<RTCMediaStreamTrackStats>
672ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
673 int64_t timestamp_us,
674 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19675 const cricket::VoiceSenderInfo& voice_sender_info,
676 int attachment_id) {
hbos9e302742017-01-20 10:47:10677 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
678 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58679 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
680 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19681 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10682 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
683 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42684 audio_track_stats->media_source_id =
685 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
686 attachment_id);
hbos9e302742017-01-20 10:47:10687 audio_track_stats->remote_source = false;
688 audio_track_stats->detached = false;
Ivo Creusen56d460902017-11-24 16:29:59689 if (voice_sender_info.apm_statistics.echo_return_loss) {
690 audio_track_stats->echo_return_loss =
691 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 10:47:10692 }
Ivo Creusen56d460902017-11-24 16:29:59693 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
694 audio_track_stats->echo_return_loss_enhancement =
695 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 10:47:10696 }
697 return audio_track_stats;
698}
699
700std::unique_ptr<RTCMediaStreamTrackStats>
701ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
702 int64_t timestamp_us,
703 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 16:18:19704 const cricket::VoiceReceiverInfo& voice_receiver_info,
705 int attachment_id) {
706 // Since receiver tracks can't be reattached, we use the SSRC as
707 // an attachment identifier.
hbos9e302742017-01-20 10:47:10708 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
709 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58710 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
711 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19712 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 10:47:10713 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
714 audio_track, audio_track_stats.get());
715 audio_track_stats->remote_source = true;
716 audio_track_stats->detached = false;
717 if (voice_receiver_info.audio_level >= 0) {
Jonas Olssona4d87372019-07-05 17:08:33718 audio_track_stats->audio_level =
719 DoubleAudioLevelFromIntAudioLevel(voice_receiver_info.audio_level);
hbos9e302742017-01-20 10:47:10720 }
Gustaf Ullbergb0a02072017-10-02 10:00:34721 audio_track_stats->jitter_buffer_delay =
722 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 14:46:29723 audio_track_stats->jitter_buffer_emitted_count =
724 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 07:45:21725 audio_track_stats->inserted_samples_for_deceleration =
726 voice_receiver_info.inserted_samples_for_deceleration;
727 audio_track_stats->removed_samples_for_acceleration =
728 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 19:17:49729 audio_track_stats->total_audio_energy =
730 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-25 00:15:13731 audio_track_stats->total_samples_received =
732 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 19:17:49733 audio_track_stats->total_samples_duration =
734 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-25 00:15:13735 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 07:45:21736 audio_track_stats->silent_concealed_samples =
737 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 07:28:20738 audio_track_stats->concealment_events =
739 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 16:21:10740 audio_track_stats->jitter_buffer_flushes =
741 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 11:52:16742 audio_track_stats->delayed_packet_outage_samples =
743 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 08:18:40744 audio_track_stats->relative_packet_arrival_delay =
745 voice_receiver_info.relative_packet_arrival_delay_seconds;
Artem Titove618cc92020-03-11 10:18:54746 audio_track_stats->jitter_buffer_target_delay =
747 voice_receiver_info.jitter_buffer_target_delay_seconds;
Henrik Lundin44125fa2019-04-29 15:00:46748 audio_track_stats->interruption_count =
749 voice_receiver_info.interruption_count >= 0
750 ? voice_receiver_info.interruption_count
751 : 0;
752 audio_track_stats->total_interruption_duration =
753 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
754 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 10:47:10755 return audio_track_stats;
756}
757
758std::unique_ptr<RTCMediaStreamTrackStats>
759ProduceMediaStreamTrackStatsFromVideoSenderInfo(
760 int64_t timestamp_us,
761 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19762 const cricket::VideoSenderInfo& video_sender_info,
763 int attachment_id) {
hbos9e302742017-01-20 10:47:10764 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
765 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58766 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 08:18:58767 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19768 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10769 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
770 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 13:49:42771 video_track_stats->media_source_id =
772 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
773 attachment_id);
hbos9e302742017-01-20 10:47:10774 video_track_stats->remote_source = false;
775 video_track_stats->detached = false;
Jonas Olssona4d87372019-07-05 17:08:33776 video_track_stats->frame_width =
777 static_cast<uint32_t>(video_sender_info.send_frame_width);
778 video_track_stats->frame_height =
779 static_cast<uint32_t>(video_sender_info.send_frame_height);
hbosfefe0762017-01-20 14:14:25780 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 13:08:34781 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 14:14:25782 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 15:35:03783 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 10:47:10784 return video_track_stats;
785}
786
787std::unique_ptr<RTCMediaStreamTrackStats>
788ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
789 int64_t timestamp_us,
790 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 16:18:19791 const cricket::VideoReceiverInfo& video_receiver_info,
792 int attachment_id) {
hbos9e302742017-01-20 10:47:10793 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
794 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 08:18:58795 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
796
797 attachment_id),
Harald Alvestrandc72af932018-01-11 16:18:19798 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 10:47:10799 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
800 video_track, video_track_stats.get());
801 video_track_stats->remote_source = true;
802 video_track_stats->detached = false;
803 if (video_receiver_info.frame_width > 0 &&
804 video_receiver_info.frame_height > 0) {
Jonas Olssona4d87372019-07-05 17:08:33805 video_track_stats->frame_width =
806 static_cast<uint32_t>(video_receiver_info.frame_width);
807 video_track_stats->frame_height =
808 static_cast<uint32_t>(video_receiver_info.frame_height);
hbos9e302742017-01-20 10:47:10809 }
Guido Urdaneta67378412019-05-28 15:38:08810 video_track_stats->jitter_buffer_delay =
811 video_receiver_info.jitter_buffer_delay_seconds;
812 video_track_stats->jitter_buffer_emitted_count =
813 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 11:56:50814 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 15:39:09815 // TODO(hbos): When we support receiving simulcast, this should be the total
816 // number of frames correctly decoded, independent of which SSRC it was
817 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 13:08:34818 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 15:39:09819 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
Johannes Kron0c141c52019-08-26 13:04:43820 video_track_stats->frames_dropped = video_receiver_info.frames_dropped;
Sergey Silkin02371062019-01-31 15:45:42821 video_track_stats->freeze_count = video_receiver_info.freeze_count;
822 video_track_stats->pause_count = video_receiver_info.pause_count;
823 video_track_stats->total_freezes_duration =
824 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
825 rtc::kNumMillisecsPerSec;
826 video_track_stats->total_pauses_duration =
827 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
828 rtc::kNumMillisecsPerSec;
829 video_track_stats->total_frames_duration =
830 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
831 rtc::kNumMillisecsPerSec;
832 video_track_stats->sum_squared_frame_durations =
833 video_receiver_info.sum_squared_frame_durations;
834
hbos9e302742017-01-20 10:47:10835 return video_track_stats;
836}
837
Harald Alvestrand89061872018-01-02 13:08:34838void ProduceSenderMediaTrackStats(
839 int64_t timestamp_us,
840 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50841 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 13:08:34842 RTCStatsReport* report) {
843 // This function iterates over the senders to generate outgoing track stats.
844
845 // TODO(hbos): Return stats of detached tracks. We have to perform stats
846 // gathering at the time of detachment to get accurate stats and timestamps.
847 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 16:29:42848 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 13:08:34849 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
850 AudioTrackInterface* track =
851 static_cast<AudioTrackInterface*>(sender->track().get());
852 if (!track)
853 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16854 cricket::VoiceSenderInfo null_sender_info;
855 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
856 // TODO(hta): Checking on ssrc is not proper. There should be a way
857 // to see from a sender whether it's connected or not.
858 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 23:19:50859 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29860 // When pc.close is called, sender info is discarded, so
861 // we generate zeroes instead. Bug: It should be retained.
862 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50863 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16864 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29865 if (sender_info) {
866 voice_sender_info = sender_info;
867 } else {
868 RTC_LOG(LS_INFO)
869 << "RTCStatsCollector: No voice sender info for sender with ssrc "
870 << sender->ssrc();
871 }
Harald Alvestrandb8e12012018-01-23 14:28:16872 }
Harald Alvestrand89061872018-01-02 13:08:34873 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19874 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
875 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34876 report->AddStats(std::move(audio_track_stats));
877 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
878 VideoTrackInterface* track =
879 static_cast<VideoTrackInterface*>(sender->track().get());
880 if (!track)
881 continue;
Harald Alvestrandb8e12012018-01-23 14:28:16882 cricket::VideoSenderInfo null_sender_info;
883 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
884 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 13:43:29885 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
886 // "none")
Steve Anton57858b32018-02-15 23:19:50887 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 13:43:29888 // When pc.close is called, sender info is discarded, so
889 // we generate zeroes instead. Bug: It should be retained.
890 // https://crbug.com/807174
Steve Anton57858b32018-02-15 23:19:50891 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 14:28:16892 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 13:43:29893 if (sender_info) {
894 video_sender_info = sender_info;
895 } else {
896 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
897 << sender->ssrc();
898 }
899 }
Harald Alvestrand89061872018-01-02 13:08:34900 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 16:18:19901 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
902 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34903 report->AddStats(std::move(video_track_stats));
904 } else {
905 RTC_NOTREACHED();
906 }
907 }
908}
909
910void ProduceReceiverMediaTrackStats(
911 int64_t timestamp_us,
912 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 23:19:50913 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 13:08:34914 RTCStatsReport* report) {
915 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 16:29:42916 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 13:08:34917 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
918 AudioTrackInterface* track =
919 static_cast<AudioTrackInterface*>(receiver->track().get());
920 const cricket::VoiceReceiverInfo* voice_receiver_info =
921 track_media_info_map.GetVoiceReceiverInfo(*track);
922 if (!voice_receiver_info) {
923 continue;
924 }
925 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
926 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:19927 timestamp_us, *track, *voice_receiver_info,
928 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34929 report->AddStats(std::move(audio_track_stats));
930 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
931 VideoTrackInterface* track =
932 static_cast<VideoTrackInterface*>(receiver->track().get());
933 const cricket::VideoReceiverInfo* video_receiver_info =
934 track_media_info_map.GetVideoReceiverInfo(*track);
935 if (!video_receiver_info) {
936 continue;
937 }
938 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
939 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 16:18:19940 timestamp_us, *track, *video_receiver_info,
941 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 13:08:34942 report->AddStats(std::move(video_track_stats));
943 } else {
944 RTC_NOTREACHED();
945 }
946 }
947}
948
Henrik Boström5b3541f2018-03-19 12:52:56949rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
950 bool filter_by_sender_selector,
951 rtc::scoped_refptr<const RTCStatsReport> report,
952 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
953 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
954 std::vector<std::string> rtpstream_ids;
955 if (filter_by_sender_selector) {
956 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
957 if (sender_selector) {
958 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
959 // reference the sender stats.
960 // Because we do not implement sender stats, we look at outbound-rtp(s)
961 // that reference the track attachment stats for the sender instead.
962 std::string track_id =
963 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
964 kSender, sender_selector->AttachmentId());
965 for (const auto& stats : *report) {
966 if (stats.type() != RTCOutboundRTPStreamStats::kType)
967 continue;
968 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
969 if (outbound_rtp.track_id.is_defined() &&
970 *outbound_rtp.track_id == track_id) {
971 rtpstream_ids.push_back(outbound_rtp.id());
972 }
973 }
974 }
975 } else {
976 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
977 if (receiver_selector) {
978 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
979 // reference the receiver stats.
980 // Because we do not implement receiver stats, we look at inbound-rtp(s)
981 // that reference the track attachment stats for the receiver instead.
982 std::string track_id =
983 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
984 kReceiver, receiver_selector->AttachmentId());
985 for (const auto& stats : *report) {
986 if (stats.type() != RTCInboundRTPStreamStats::kType)
987 continue;
988 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
989 if (inbound_rtp.track_id.is_defined() &&
990 *inbound_rtp.track_id == track_id) {
991 rtpstream_ids.push_back(inbound_rtp.id());
992 }
993 }
994 }
995 }
996 if (rtpstream_ids.empty())
997 return RTCStatsReport::Create(report->timestamp_us());
998 return TakeReferencedStats(report->Copy(), rtpstream_ids);
999}
1000
hboscc555c52016-10-18 19:48:311001} // namespace
1002
Henrik Boström5b3541f2018-03-19 12:52:561003RTCStatsCollector::RequestInfo::RequestInfo(
1004 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1005 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
1006
1007RTCStatsCollector::RequestInfo::RequestInfo(
1008 rtc::scoped_refptr<RtpSenderInternal> selector,
1009 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1010 : RequestInfo(FilterMode::kSenderSelector,
1011 std::move(callback),
1012 std::move(selector),
1013 nullptr) {}
1014
1015RTCStatsCollector::RequestInfo::RequestInfo(
1016 rtc::scoped_refptr<RtpReceiverInternal> selector,
1017 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
1018 : RequestInfo(FilterMode::kReceiverSelector,
1019 std::move(callback),
1020 nullptr,
1021 std::move(selector)) {}
1022
1023RTCStatsCollector::RequestInfo::RequestInfo(
1024 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
1025 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
1026 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
1027 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
1028 : filter_mode_(filter_mode),
1029 callback_(std::move(callback)),
1030 sender_selector_(std::move(sender_selector)),
1031 receiver_selector_(std::move(receiver_selector)) {
1032 RTC_DCHECK(callback_);
1033 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
1034}
1035
hbosc82f2e12016-09-05 08:36:501036rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-24 00:38:461037 PeerConnectionInternal* pc,
1038 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 08:36:501039 return rtc::scoped_refptr<RTCStatsCollector>(
1040 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
1041}
1042
Steve Anton2d8609c2018-01-24 00:38:461043RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 08:36:501044 int64_t cache_lifetime_us)
hbosd565b732016-08-30 21:04:351045 : pc_(pc),
Steve Anton978b8762017-09-29 19:15:021046 signaling_thread_(pc->signaling_thread()),
1047 worker_thread_(pc->worker_thread()),
1048 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 08:36:501049 num_pending_partial_reports_(0),
1050 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 08:49:311051 network_report_event_(true /* manual_reset */,
1052 true /* initially_signaled */),
hbos0e6758d2016-08-31 14:57:361053 cache_timestamp_us_(0),
1054 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 21:04:351055 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 08:36:501056 RTC_DCHECK(signaling_thread_);
1057 RTC_DCHECK(worker_thread_);
1058 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 14:57:361059 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Taylor Brandstetter3a034e12020-07-09 22:32:341060 pc_->SignalRtpDataChannelCreated().connect(
1061 this, &RTCStatsCollector::OnRtpDataChannelCreated);
1062 pc_->SignalSctpDataChannelCreated().connect(
1063 this, &RTCStatsCollector::OnSctpDataChannelCreated);
hbosd565b732016-08-30 21:04:351064}
1065
hbosb78306a2016-12-19 13:06:571066RTCStatsCollector::~RTCStatsCollector() {
1067 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1068}
1069
hbosc82f2e12016-09-05 08:36:501070void RTCStatsCollector::GetStatsReport(
1071 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 12:52:561072 GetStatsReportInternal(RequestInfo(std::move(callback)));
1073}
1074
1075void RTCStatsCollector::GetStatsReport(
1076 rtc::scoped_refptr<RtpSenderInternal> selector,
1077 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1078 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1079}
1080
1081void RTCStatsCollector::GetStatsReport(
1082 rtc::scoped_refptr<RtpReceiverInternal> selector,
1083 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
1084 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
1085}
1086
1087void RTCStatsCollector::GetStatsReportInternal(
1088 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 08:36:501089 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 12:52:561090 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 08:36:501091
hbos0e6758d2016-08-31 14:57:361092 // "Now" using a monotonically increasing timer.
1093 int64_t cache_now_us = rtc::TimeMicros();
1094 if (cached_report_ &&
1095 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 17:53:471096 // We have a fresh cached report to deliver. Deliver asynchronously, since
1097 // the caller may not be expecting a synchronous callback, and it avoids
1098 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 12:52:561099 std::vector<RequestInfo> requests;
1100 requests.swap(requests_);
Niels Möller4bab23f2021-01-18 08:24:331101
1102 // Task subclass to take ownership of the requests.
1103 // TODO(nisse): Delete when we can use C++14, and do lambda capture with
1104 // std::move.
1105 class DeliveryTask : public QueuedTask {
1106 public:
1107 DeliveryTask(rtc::scoped_refptr<RTCStatsCollector> collector,
1108 rtc::scoped_refptr<const RTCStatsReport> cached_report,
1109 std::vector<RequestInfo> requests)
1110 : collector_(collector),
1111 cached_report_(cached_report),
1112 requests_(std::move(requests)) {}
1113 bool Run() override {
1114 collector_->DeliverCachedReport(cached_report_, std::move(requests_));
1115 return true;
1116 }
1117
1118 private:
1119 rtc::scoped_refptr<RTCStatsCollector> collector_;
1120 rtc::scoped_refptr<const RTCStatsReport> cached_report_;
1121 std::vector<RequestInfo> requests_;
1122 };
1123 signaling_thread_->PostTask(std::make_unique<DeliveryTask>(
1124 this, cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 08:36:501125 } else if (!num_pending_partial_reports_) {
1126 // Only start gathering stats if we're not already gathering stats. In the
1127 // case of already gathering stats, |callback_| will be invoked when there
1128 // are no more pending partial reports.
1129
1130 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
1131 // UTC), in microseconds. The system clock could be modified and is not
1132 // necessarily monotonically increasing.
nissecdf37a92016-09-14 06:41:471133 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 08:36:501134
hbosf415f8a2017-01-02 12:28:511135 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 08:36:501136 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 12:58:021137
Henrik Boström180faeb2020-11-13 08:05:211138 // Prepare |transceiver_stats_infos_| and |call_stats_| for use in
hbos84abeb12017-01-16 14:16:441139 // |ProducePartialResultsOnNetworkThread| and
1140 // |ProducePartialResultsOnSignalingThread|.
Henrik Boström180faeb2020-11-13 08:05:211141 PrepareTransceiverStatsInfosAndCallStats_s_w();
Steve Anton7eca0932018-03-30 22:18:411142 // Prepare |transport_names_| for use in
1143 // |ProducePartialResultsOnNetworkThread|.
1144 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 18:34:401145
Henrik Boström40b030e2019-02-28 08:49:311146 // Don't touch |network_report_| on the signaling thread until
1147 // ProducePartialResultsOnNetworkThread() has signaled the
1148 // |network_report_event_|.
1149 network_report_event_.Reset();
Niels Möller4bab23f2021-01-18 08:24:331150 rtc::scoped_refptr<RTCStatsCollector> collector(this);
1151 network_thread_->PostTask(RTC_FROM_HERE, [collector, timestamp_us] {
1152 collector->ProducePartialResultsOnNetworkThread(timestamp_us);
1153 });
hbosf415f8a2017-01-02 12:28:511154 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 14:57:361155 }
hbosd565b732016-08-30 21:04:351156}
1157
1158void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 08:36:501159 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 21:04:351160 cached_report_ = nullptr;
1161}
1162
hbosb78306a2016-12-19 13:06:571163void RTCStatsCollector::WaitForPendingRequest() {
1164 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 08:49:311165 // If a request is pending, blocks until the |network_report_event_| is
1166 // signaled and then delivers the result. Otherwise this is a NO-OP.
1167 MergeNetworkReport_s();
hbosb78306a2016-12-19 13:06:571168}
1169
hbosc82f2e12016-09-05 08:36:501170void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1171 int64_t timestamp_us) {
1172 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501173 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1174
Henrik Boström40b030e2019-02-28 08:49:311175 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501176
Henrik Boström40b030e2019-02-28 08:49:311177 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1178 partial_report_.get());
hbosc82f2e12016-09-05 08:36:501179
Henrik Boström40b030e2019-02-28 08:49:311180 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1181 // signaling thread, so it is always the first partial result delivered on the
1182 // signaling thread. The request is not complete until MergeNetworkReport_s()
1183 // happens; we don't have to do anything here.
1184 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1185 --num_pending_partial_reports_;
1186}
1187
1188void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1189 int64_t timestamp_us,
1190 RTCStatsReport* partial_report) {
1191 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501192 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1193
Henrik Boström40b030e2019-02-28 08:49:311194 ProduceDataChannelStats_s(timestamp_us, partial_report);
1195 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1196 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 13:49:421197 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311198 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 08:36:501199}
1200
hbosc82f2e12016-09-05 08:36:501201void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
1202 int64_t timestamp_us) {
1203 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501204 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1205
Ivo Creusen8d8ffdb2019-04-30 07:45:211206 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 08:49:311207 // |network_report_event_| is reset before this method is invoked.
1208 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 08:36:501209
Steve Anton5dfde182018-02-06 18:34:401210 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 22:18:411211 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 18:34:401212 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1213 PrepareTransportCertificateStats_n(transport_stats_by_name);
1214
Henrik Boström40b030e2019-02-28 08:49:311215 ProducePartialResultsOnNetworkThreadImpl(
1216 timestamp_us, transport_stats_by_name, transport_cert_stats,
1217 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:401218
Henrik Boström40b030e2019-02-28 08:49:311219 // Signal that it is now safe to touch |network_report_| on the signaling
1220 // thread, and post a task to merge it into the final results.
1221 network_report_event_.Set();
Niels Möller4bab23f2021-01-18 08:24:331222 rtc::scoped_refptr<RTCStatsCollector> collector(this);
Henrik Boström40b030e2019-02-28 08:49:311223 signaling_thread_->PostTask(
Niels Möller4bab23f2021-01-18 08:24:331224 RTC_FROM_HERE, [collector] { collector->MergeNetworkReport_s(); });
Henrik Boström05d43c62019-02-15 09:23:081225}
1226
Henrik Boström40b030e2019-02-28 08:49:311227void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1228 int64_t timestamp_us,
1229 const std::map<std::string, cricket::TransportStats>&
1230 transport_stats_by_name,
1231 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1232 RTCStatsReport* partial_report) {
1233 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501234 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1235
Henrik Boström40b030e2019-02-28 08:49:311236 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1237 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1238 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1239 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 08:49:311240 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1241 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 11:40:251242 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1243 partial_report);
Henrik Boström40b030e2019-02-28 08:49:311244}
1245
1246void RTCStatsCollector::MergeNetworkReport_s() {
1247 RTC_DCHECK(signaling_thread_->IsCurrent());
1248 // The |network_report_event_| must be signaled for it to be safe to touch
1249 // |network_report_|. This is normally not blocking, but if
1250 // WaitForPendingRequest() is called while a request is pending, we might have
1251 // to wait until the network thread is done touching |network_report_|.
1252 network_report_event_.Wait(rtc::Event::kForever);
1253 if (!network_report_) {
1254 // Normally, MergeNetworkReport_s() is executed because it is posted from
1255 // the network thread. But if WaitForPendingRequest() is called while a
1256 // request is pending, an early call to MergeNetworkReport_s() is made,
1257 // merging the report and setting |network_report_| to null. If so, when the
1258 // previously posted MergeNetworkReport_s() is later executed, the report is
1259 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 08:36:501260 return;
1261 }
Mirko Bonadeica890ee2019-02-15 21:10:401262 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 08:49:311263 RTC_DCHECK(partial_report_);
1264 partial_report_->TakeMembersFrom(network_report_);
1265 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:401266 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 08:49:311267 // |network_report_| is currently the only partial report collected
1268 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1269 // ready to deliver the result.
1270 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1271 cache_timestamp_us_ = partial_report_timestamp_us_;
1272 cached_report_ = partial_report_;
1273 partial_report_ = nullptr;
1274 transceiver_stats_infos_.clear();
1275 // Trace WebRTC Stats when getStats is called on Javascript.
1276 // This allows access to WebRTC stats from trace logs. To enable them,
1277 // select the "webrtc_stats" category when recording traces.
1278 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1279 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:401280
Henrik Boström40b030e2019-02-28 08:49:311281 // Deliver report and clear |requests_|.
1282 std::vector<RequestInfo> requests;
1283 requests.swap(requests_);
1284 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 08:36:501285}
1286
Taylor Brandstetter25e022f2018-03-08 17:53:471287void RTCStatsCollector::DeliverCachedReport(
1288 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 12:52:561289 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 08:36:501290 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 12:52:561291 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 17:53:471292 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 17:42:251293
Henrik Boström5b3541f2018-03-19 12:52:561294 for (const RequestInfo& request : requests) {
1295 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1296 request.callback()->OnStatsDelivered(cached_report);
1297 } else {
1298 bool filter_by_sender_selector;
1299 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1300 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1301 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1302 filter_by_sender_selector = true;
1303 sender_selector = request.sender_selector();
1304 } else {
1305 RTC_DCHECK(request.filter_mode() ==
1306 RequestInfo::FilterMode::kReceiverSelector);
1307 filter_by_sender_selector = false;
1308 receiver_selector = request.receiver_selector();
1309 }
1310 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1311 filter_by_sender_selector, cached_report, sender_selector,
1312 receiver_selector));
1313 }
hbosc82f2e12016-09-05 08:36:501314 }
hbosd565b732016-08-30 21:04:351315}
1316
hbosdf6075a2016-12-19 12:58:021317void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 11:00:051318 int64_t timestamp_us,
1319 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce02016-10-03 21:16:561320 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021321 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501322 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1323
hbos02ba2112016-10-28 12:14:531324 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1325 if (transport_cert_stats_pair.second.local) {
1326 ProduceCertificateStatsFromSSLCertificateStats(
1327 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce02016-10-03 21:16:561328 }
hbos02ba2112016-10-28 12:14:531329 if (transport_cert_stats_pair.second.remote) {
1330 ProduceCertificateStatsFromSSLCertificateStats(
1331 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce02016-10-03 21:16:561332 }
1333 }
1334}
1335
hbosdf6075a2016-12-19 12:58:021336void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 23:19:501337 int64_t timestamp_us,
1338 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 10:32:061339 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021340 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501341 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1342
Steve Anton57858b32018-02-15 23:19:501343 for (const auto& stats : transceiver_stats_infos) {
1344 if (!stats.mid) {
1345 continue;
hbos0adb8282016-11-23 10:32:061346 }
Philipp Hancke95157a02020-11-16 19:08:271347 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1348 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1349
Steve Anton57858b32018-02-15 23:19:501350 const cricket::VoiceMediaInfo* voice_media_info =
1351 stats.track_media_info_map->voice_media_info();
1352 const cricket::VideoMediaInfo* video_media_info =
1353 stats.track_media_info_map->video_media_info();
1354 // Audio
1355 if (voice_media_info) {
1356 // Inbound
1357 for (const auto& pair : voice_media_info->receive_codecs) {
1358 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271359 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501360 }
1361 // Outbound
1362 for (const auto& pair : voice_media_info->send_codecs) {
1363 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271364 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501365 }
Guido Urdanetaee2388f2018-02-15 16:36:191366 }
Steve Anton57858b32018-02-15 23:19:501367 // Video
1368 if (video_media_info) {
1369 // Inbound
1370 for (const auto& pair : video_media_info->receive_codecs) {
1371 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271372 timestamp_us, *stats.mid, transport_id, true, pair.second));
Steve Anton57858b32018-02-15 23:19:501373 }
1374 // Outbound
1375 for (const auto& pair : video_media_info->send_codecs) {
1376 report->AddStats(CodecStatsFromRtpCodecParameters(
Philipp Hancke95157a02020-11-16 19:08:271377 timestamp_us, *stats.mid, transport_id, false, pair.second));
Steve Anton57858b32018-02-15 23:19:501378 }
hbos0adb8282016-11-23 10:32:061379 }
1380 }
1381}
1382
hboscc555c52016-10-18 19:48:311383void RTCStatsCollector::ProduceDataChannelStats_s(
Jonas Olssona4d87372019-07-05 17:08:331384 int64_t timestamp_us,
1385 RTCStatsReport* report) const {
Tomas Gunnarsson2e94de52020-06-16 14:54:101386 RTC_DCHECK_RUN_ON(signaling_thread_);
Henrik Boströme88c95e2020-07-08 09:18:501387 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Taylor Brandstetter3a034e12020-07-09 22:32:341388 std::vector<DataChannelStats> data_stats = pc_->GetDataChannelStats();
Tomas Gunnarsson2e94de52020-06-16 14:54:101389 for (const auto& stats : data_stats) {
hboscc555c52016-10-18 19:48:311390 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1391 new RTCDataChannelStats(
Tomas Gunnarsson2e94de52020-06-16 14:54:101392 "RTCDataChannel_" + rtc::ToString(stats.internal_id),
hboscc555c52016-10-18 19:48:311393 timestamp_us));
Tomas Gunnarsson2e94de52020-06-16 14:54:101394 data_channel_stats->label = std::move(stats.label);
1395 data_channel_stats->protocol = std::move(stats.protocol);
1396 data_channel_stats->data_channel_identifier = stats.id;
1397 data_channel_stats->state = DataStateToRTCDataChannelState(stats.state);
1398 data_channel_stats->messages_sent = stats.messages_sent;
1399 data_channel_stats->bytes_sent = stats.bytes_sent;
1400 data_channel_stats->messages_received = stats.messages_received;
1401 data_channel_stats->bytes_received = stats.bytes_received;
hboscc555c52016-10-18 19:48:311402 report->AddStats(std::move(data_channel_stats));
1403 }
1404}
1405
hbosdf6075a2016-12-19 12:58:021406void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 13:44:031407 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 18:34:401408 const std::map<std::string, cricket::TransportStats>&
1409 transport_stats_by_name,
stefanf79ade12017-06-02 13:44:031410 const Call::Stats& call_stats,
1411 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021412 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501413 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1414
Steve Anton5dfde182018-02-06 18:34:401415 for (const auto& entry : transport_stats_by_name) {
1416 const std::string& transport_name = entry.first;
1417 const cricket::TransportStats& transport_stats = entry.second;
1418 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 09:50:141419 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401420 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 21:54:491421 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:271422 channel_stats.ice_transport_stats.connection_infos) {
hbosc47a0c32016-10-11 21:54:491423 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 11:00:051424 new RTCIceCandidatePairStats(
1425 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1426 timestamp_us));
hbosc47a0c32016-10-11 21:54:491427
hbos0583b282016-11-30 09:50:141428 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 09:18:471429 // TODO(hbos): There could be other candidates that are not paired with
1430 // anything. We don't have a complete list. Local candidates come from
1431 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 13:08:341432 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 12:14:531433 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311434 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 12:14:531435 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 17:59:311436 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 16:08:181437 candidate_pair_stats->state =
1438 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1439 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 09:38:081440 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 21:54:491441 // TODO(hbos): This writable is different than the spec. It goes to
1442 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 13:08:341443 // https://crbug.com/633550
hbosc47a0c32016-10-11 21:54:491444 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 21:54:491445 candidate_pair_stats->bytes_sent =
1446 static_cast<uint64_t>(info.sent_total_bytes);
1447 candidate_pair_stats->bytes_received =
1448 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 14:34:471449 candidate_pair_stats->total_round_trip_time =
1450 static_cast<double>(info.total_round_trip_time_ms) /
1451 rtc::kNumMillisecsPerSec;
1452 if (info.current_round_trip_time_ms) {
1453 candidate_pair_stats->current_round_trip_time =
1454 static_cast<double>(*info.current_round_trip_time_ms) /
1455 rtc::kNumMillisecsPerSec;
1456 }
stefanf79ade12017-06-02 13:44:031457 if (info.best_connection) {
hbos338f78a2017-02-07 14:41:211458 // The bandwidth estimations we have are for the selected candidate
1459 // pair ("info.best_connection").
stefanf79ade12017-06-02 13:44:031460 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1461 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1462 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211463 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 13:44:031464 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211465 }
stefanf79ade12017-06-02 13:44:031466 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 14:41:211467 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 13:44:031468 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 14:41:211469 }
1470 }
hbosd82f5122016-12-09 12:12:391471 candidate_pair_stats->requests_received =
1472 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 09:22:531473 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1474 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491475 candidate_pair_stats->responses_received =
1476 static_cast<uint64_t>(info.recv_ping_responses);
1477 candidate_pair_stats->responses_sent =
1478 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 09:22:531479 RTC_DCHECK_GE(info.sent_ping_requests_total,
1480 info.sent_ping_requests_before_first_response);
1481 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1482 info.sent_ping_requests_total -
1483 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 21:54:491484
1485 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 09:18:471486 }
1487 }
1488 }
1489}
1490
Steve Anton57858b32018-02-15 23:19:501491void RTCStatsCollector::ProduceMediaStreamStats_s(
1492 int64_t timestamp_us,
1493 RTCStatsReport* report) const {
hbos09bc1282016-11-08 14:29:221494 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501495 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:501496
1497 std::map<std::string, std::vector<std::string>> track_ids;
1498
1499 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 16:29:421500 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501501 std::string track_id =
1502 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1503 kSender, sender->internal()->AttachmentId());
1504 for (auto& stream_id : sender->stream_ids()) {
1505 track_ids[stream_id].push_back(track_id);
1506 }
1507 }
Mirko Bonadei739baf02019-01-27 16:29:421508 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501509 std::string track_id =
1510 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1511 kReceiver, receiver->internal()->AttachmentId());
1512 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 23:05:281513 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 23:19:501514 }
1515 }
1516 }
1517
1518 // Build stats for each stream ID known.
1519 for (auto& it : track_ids) {
1520 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1521 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1522 stream_stats->stream_identifier = it.first;
1523 stream_stats->track_ids = it.second;
1524 report->AddStats(std::move(stream_stats));
1525 }
1526}
1527
1528void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1529 int64_t timestamp_us,
1530 RTCStatsReport* report) const {
1531 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501532 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1533
Steve Anton57858b32018-02-15 23:19:501534 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1535 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 16:29:421536 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 23:19:501537 senders.push_back(sender->internal());
1538 }
1539 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1540 senders, report);
1541
1542 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 16:29:421543 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 23:19:501544 receivers.push_back(receiver->internal());
1545 }
1546 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1547 receivers, report);
1548 }
hbos09bc1282016-11-08 14:29:221549}
1550
Henrik Boström646fda02019-05-22 13:49:421551void RTCStatsCollector::ProduceMediaSourceStats_s(
1552 int64_t timestamp_us,
1553 RTCStatsReport* report) const {
1554 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501555 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1556
Henrik Boström646fda02019-05-22 13:49:421557 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1558 transceiver_stats_infos_) {
1559 const auto& track_media_info_map =
1560 transceiver_stats_info.track_media_info_map;
1561 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1562 const auto& sender_internal = sender->internal();
1563 const auto& track = sender_internal->track();
1564 if (!track)
1565 continue;
Henrik Boströmd2c336f2019-07-03 15:11:101566 // TODO(https://crbug.com/webrtc/10771): The same track could be attached
1567 // to multiple senders which should result in multiple senders referencing
1568 // the same media-source stats. When all media source related metrics are
1569 // moved to the track's source (e.g. input frame rate is moved from
1570 // cricket::VideoSenderInfo to VideoTrackSourceInterface::Stats and audio
1571 // levels are moved to the corresponding audio track/source object), don't
1572 // create separate media source stats objects on a per-attachment basis.
Henrik Boström646fda02019-05-22 13:49:421573 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1574 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
Mirko Bonadei317a1f02019-09-17 15:06:181575 auto audio_source_stats = std::make_unique<RTCAudioSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421576 RTCMediaSourceStatsIDFromKindAndAttachment(
1577 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1578 timestamp_us);
Henrik Boströmd2c336f2019-07-03 15:11:101579 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1580 // SSRC assigned (there shouldn't need to exist a send-stream, created
1581 // by an O/A exchange) in order to read audio media-source stats.
1582 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1583 // value indicating no SSRC.
1584 if (sender_internal->ssrc() != 0) {
1585 auto* voice_sender_info =
1586 track_media_info_map->GetVoiceSenderInfoBySsrc(
1587 sender_internal->ssrc());
1588 if (voice_sender_info) {
1589 audio_source_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
1590 voice_sender_info->audio_level);
1591 audio_source_stats->total_audio_energy =
1592 voice_sender_info->total_input_energy;
1593 audio_source_stats->total_samples_duration =
1594 voice_sender_info->total_input_duration;
1595 }
1596 }
1597 media_source_stats = std::move(audio_source_stats);
Henrik Boström646fda02019-05-22 13:49:421598 } else {
1599 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
Mirko Bonadei317a1f02019-09-17 15:06:181600 auto video_source_stats = std::make_unique<RTCVideoSourceStats>(
Henrik Boström646fda02019-05-22 13:49:421601 RTCMediaSourceStatsIDFromKindAndAttachment(
1602 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1603 timestamp_us);
1604 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1605 auto* video_source = video_track->GetSource();
1606 VideoTrackSourceInterface::Stats source_stats;
1607 if (video_source && video_source->GetStats(&source_stats)) {
1608 video_source_stats->width = source_stats.input_width;
1609 video_source_stats->height = source_stats.input_height;
1610 }
Henrik Boströmd2c336f2019-07-03 15:11:101611 // TODO(https://crbug.com/webrtc/10771): We shouldn't need to have an
1612 // SSRC assigned (there shouldn't need to exist a send-stream, created
1613 // by an O/A exchange) in order to get framesPerSecond.
1614 // TODO(https://crbug.com/webrtc/8694): SSRC 0 shouldn't be a magic
1615 // value indicating no SSRC.
Henrik Boström646fda02019-05-22 13:49:421616 if (sender_internal->ssrc() != 0) {
Henrik Boströmd2c336f2019-07-03 15:11:101617 auto* video_sender_info =
1618 track_media_info_map->GetVideoSenderInfoBySsrc(
1619 sender_internal->ssrc());
1620 if (video_sender_info) {
Henrik Boström646fda02019-05-22 13:49:421621 video_source_stats->frames_per_second =
Henrik Boströmd2c336f2019-07-03 15:11:101622 video_sender_info->framerate_input;
Di Wu668dbf62021-02-27 08:29:151623 video_source_stats->frames = video_sender_info->frames;
Henrik Boström646fda02019-05-22 13:49:421624 }
1625 }
1626 media_source_stats = std::move(video_source_stats);
1627 }
1628 media_source_stats->track_identifier = track->id();
1629 media_source_stats->kind = track->kind();
1630 report->AddStats(std::move(media_source_stats));
1631 }
1632 }
1633}
1634
hbos6ab97ce02016-10-03 21:16:561635void RTCStatsCollector::ProducePeerConnectionStats_s(
Jonas Olssona4d87372019-07-05 17:08:331636 int64_t timestamp_us,
1637 RTCStatsReport* report) const {
hbosc82f2e12016-09-05 08:36:501638 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501639 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1640
hbosd565b732016-08-30 21:04:351641 std::unique_ptr<RTCPeerConnectionStats> stats(
Jonas Olssona4d87372019-07-05 17:08:331642 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 09:41:091643 stats->data_channels_opened = internal_record_.data_channels_opened;
1644 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce02016-10-03 21:16:561645 report->AddStats(std::move(stats));
hbosd565b732016-08-30 21:04:351646}
1647
hbosdf6075a2016-12-19 12:58:021648void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 19:44:481649 int64_t timestamp_us,
Steve Anton57858b32018-02-15 23:19:501650 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 14:16:441651 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021652 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501653 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
hbos6ded1902016-11-01 08:50:461654
Steve Anton57858b32018-02-15 23:19:501655 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1656 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1657 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1658 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1659 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1660 } else {
1661 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:191662 }
Steve Anton56bae8d2018-02-15 00:07:421663 }
Steve Anton57858b32018-02-15 23:19:501664}
1665
1666void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1667 int64_t timestamp_us,
1668 const RtpTransceiverStatsInfo& stats,
1669 RTCStatsReport* report) const {
Henrik Boströme88c95e2020-07-08 09:18:501670 RTC_DCHECK(network_thread_->IsCurrent());
1671 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1672
Steve Anton57858b32018-02-15 23:19:501673 if (!stats.mid || !stats.transport_name) {
1674 return;
1675 }
1676 RTC_DCHECK(stats.track_media_info_map);
1677 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1678 RTC_DCHECK(track_media_info_map.voice_media_info());
1679 std::string mid = *stats.mid;
1680 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1681 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1682 // Inbound
1683 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1684 track_media_info_map.voice_media_info()->receivers) {
1685 if (!voice_receiver_info.connected())
1686 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181687 auto inbound_audio = std::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501688 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1689 timestamp_us);
1690 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1691 inbound_audio.get());
1692 // TODO(hta): This lookup should look for the sender, not the track.
1693 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1694 track_media_info_map.GetAudioTrack(voice_receiver_info);
1695 if (audio_track) {
1696 inbound_audio->track_id =
1697 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1698 kReceiver,
1699 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 08:50:461700 }
Steve Anton57858b32018-02-15 23:19:501701 inbound_audio->transport_id = transport_id;
1702 report->AddStats(std::move(inbound_audio));
1703 }
1704 // Outbound
Henrik Boström4f40fa52019-12-19 12:27:271705 std::map<std::string, RTCOutboundRTPStreamStats*> audio_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501706 for (const cricket::VoiceSenderInfo& voice_sender_info :
1707 track_media_info_map.voice_media_info()->senders) {
1708 if (!voice_sender_info.connected())
1709 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181710 auto outbound_audio = std::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501711 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1712 timestamp_us);
1713 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1714 outbound_audio.get());
1715 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1716 track_media_info_map.GetAudioTrack(voice_sender_info);
1717 if (audio_track) {
Henrik Boström646fda02019-05-22 13:49:421718 int attachment_id =
1719 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 23:19:501720 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 13:49:421721 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1722 attachment_id);
1723 outbound_audio->media_source_id =
1724 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1725 attachment_id);
Steve Anton56bae8d2018-02-15 00:07:421726 }
Steve Anton57858b32018-02-15 23:19:501727 outbound_audio->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271728 audio_outbound_rtps.insert(
1729 std::make_pair(outbound_audio->id(), outbound_audio.get()));
Steve Anton57858b32018-02-15 23:19:501730 report->AddStats(std::move(outbound_audio));
1731 }
Henrik Boström883eefc2019-05-27 11:40:251732 // Remote-inbound
1733 // These are Report Block-based, information sent from the remote endpoint,
1734 // providing metrics about our Outbound streams. We take advantage of the fact
1735 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1736 // been added to the report.
1737 for (const cricket::VoiceSenderInfo& voice_sender_info :
1738 track_media_info_map.voice_media_info()->senders) {
1739 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1740 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071741 report_block_data, cricket::MEDIA_TYPE_AUDIO, audio_outbound_rtps,
1742 *report));
Henrik Boström883eefc2019-05-27 11:40:251743 }
1744 }
Steve Anton57858b32018-02-15 23:19:501745}
1746
1747void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1748 int64_t timestamp_us,
1749 const RtpTransceiverStatsInfo& stats,
1750 RTCStatsReport* report) const {
Henrik Boströme88c95e2020-07-08 09:18:501751 RTC_DCHECK(network_thread_->IsCurrent());
1752 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1753
Steve Anton57858b32018-02-15 23:19:501754 if (!stats.mid || !stats.transport_name) {
1755 return;
1756 }
1757 RTC_DCHECK(stats.track_media_info_map);
1758 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1759 RTC_DCHECK(track_media_info_map.video_media_info());
1760 std::string mid = *stats.mid;
1761 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1762 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1763 // Inbound
1764 for (const cricket::VideoReceiverInfo& video_receiver_info :
1765 track_media_info_map.video_media_info()->receivers) {
1766 if (!video_receiver_info.connected())
1767 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181768 auto inbound_video = std::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501769 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1770 timestamp_us);
1771 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1772 inbound_video.get());
1773 rtc::scoped_refptr<VideoTrackInterface> video_track =
1774 track_media_info_map.GetVideoTrack(video_receiver_info);
1775 if (video_track) {
1776 inbound_video->track_id =
1777 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1778 kReceiver,
1779 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1780 }
1781 inbound_video->transport_id = transport_id;
1782 report->AddStats(std::move(inbound_video));
1783 }
1784 // Outbound
Henrik Boström4f40fa52019-12-19 12:27:271785 std::map<std::string, RTCOutboundRTPStreamStats*> video_outbound_rtps;
Steve Anton57858b32018-02-15 23:19:501786 for (const cricket::VideoSenderInfo& video_sender_info :
Eldar Rello9276e2c2020-06-10 14:53:391787 track_media_info_map.video_media_info()->senders) {
Steve Anton57858b32018-02-15 23:19:501788 if (!video_sender_info.connected())
1789 continue;
Mirko Bonadei317a1f02019-09-17 15:06:181790 auto outbound_video = std::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 23:19:501791 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1792 timestamp_us);
Eldar Rello9276e2c2020-06-10 14:53:391793 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1794 outbound_video.get());
Steve Anton57858b32018-02-15 23:19:501795 rtc::scoped_refptr<VideoTrackInterface> video_track =
1796 track_media_info_map.GetVideoTrack(video_sender_info);
1797 if (video_track) {
Henrik Boström646fda02019-05-22 13:49:421798 int attachment_id =
1799 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 23:19:501800 outbound_video->track_id =
Henrik Boström646fda02019-05-22 13:49:421801 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1802 attachment_id);
1803 outbound_video->media_source_id =
1804 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1805 attachment_id);
Steve Anton57858b32018-02-15 23:19:501806 }
1807 outbound_video->transport_id = transport_id;
Henrik Boström4f40fa52019-12-19 12:27:271808 video_outbound_rtps.insert(
1809 std::make_pair(outbound_video->id(), outbound_video.get()));
Steve Anton57858b32018-02-15 23:19:501810 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 08:50:461811 }
Henrik Boström883eefc2019-05-27 11:40:251812 // Remote-inbound
1813 // These are Report Block-based, information sent from the remote endpoint,
1814 // providing metrics about our Outbound streams. We take advantage of the fact
1815 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1816 // been added to the report.
1817 for (const cricket::VideoSenderInfo& video_sender_info :
1818 track_media_info_map.video_media_info()->senders) {
1819 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1820 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
Eldar Relloc07e9042020-07-03 08:08:071821 report_block_data, cricket::MEDIA_TYPE_VIDEO, video_outbound_rtps,
1822 *report));
Henrik Boström883eefc2019-05-27 11:40:251823 }
1824 }
hbos6ded1902016-11-01 08:50:461825}
1826
hbosdf6075a2016-12-19 12:58:021827void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 18:34:401828 int64_t timestamp_us,
1829 const std::map<std::string, cricket::TransportStats>&
1830 transport_stats_by_name,
hbos2fa7c672016-10-24 11:00:051831 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1832 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 12:58:021833 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501834 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1835
Steve Anton5dfde182018-02-06 18:34:401836 for (const auto& entry : transport_stats_by_name) {
1837 const std::string& transport_name = entry.first;
1838 const cricket::TransportStats& transport_stats = entry.second;
1839
hbos2fa7c672016-10-24 11:00:051840 // Get reference to RTCP channel, if it exists.
1841 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 18:34:401842 for (const cricket::TransportChannelStats& channel_stats :
1843 transport_stats.channel_stats) {
Jonas Olssona4d87372019-07-05 17:08:331844 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
hbos2fa7c672016-10-24 11:00:051845 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 18:34:401846 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 11:00:051847 break;
1848 }
1849 }
1850
1851 // Get reference to local and remote certificates of this transport, if they
1852 // exist.
Steve Anton5dfde182018-02-06 18:34:401853 const auto& certificate_stats_it =
1854 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 11:00:051855 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1856 std::string local_certificate_id;
1857 if (certificate_stats_it->second.local) {
1858 local_certificate_id = RTCCertificateIDFromFingerprint(
1859 certificate_stats_it->second.local->fingerprint);
1860 }
1861 std::string remote_certificate_id;
1862 if (certificate_stats_it->second.remote) {
1863 remote_certificate_id = RTCCertificateIDFromFingerprint(
1864 certificate_stats_it->second.remote->fingerprint);
1865 }
1866
1867 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 18:34:401868 for (const cricket::TransportChannelStats& channel_stats :
1869 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 11:00:051870 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 18:34:401871 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1872 transport_name, channel_stats.component),
1873 timestamp_us));
hbos2fa7c672016-10-24 11:00:051874 transport_stats->bytes_sent = 0;
Artem Titovedacbd52020-07-06 14:06:371875 transport_stats->packets_sent = 0;
hbos2fa7c672016-10-24 11:00:051876 transport_stats->bytes_received = 0;
Artem Titovedacbd52020-07-06 14:06:371877 transport_stats->packets_received = 0;
Jonas Olssona4d87372019-07-05 17:08:331878 transport_stats->dtls_state =
1879 DtlsTransportStateToRTCDtlsTransportState(channel_stats.dtls_state);
Jonas Oreland149dc722019-08-28 06:10:271880 transport_stats->selected_candidate_pair_changes =
1881 channel_stats.ice_transport_stats.selected_candidate_pair_changes;
hbos2fa7c672016-10-24 11:00:051882 for (const cricket::ConnectionInfo& info :
Jonas Oreland149dc722019-08-28 06:10:271883 channel_stats.ice_transport_stats.connection_infos) {
hbos2fa7c672016-10-24 11:00:051884 *transport_stats->bytes_sent += info.sent_total_bytes;
Artem Titovedacbd52020-07-06 14:06:371885 *transport_stats->packets_sent +=
1886 info.sent_total_packets - info.sent_discarded_packets;
hbos2fa7c672016-10-24 11:00:051887 *transport_stats->bytes_received += info.recv_total_bytes;
Artem Titovedacbd52020-07-06 14:06:371888 *transport_stats->packets_received += info.packets_received;
hbos2fa7c672016-10-24 11:00:051889 if (info.best_connection) {
hbos2fa7c672016-10-24 11:00:051890 transport_stats->selected_candidate_pair_id =
1891 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1892 }
1893 }
1894 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1895 !rtcp_transport_stats_id.empty()) {
1896 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1897 }
1898 if (!local_certificate_id.empty())
1899 transport_stats->local_certificate_id = local_certificate_id;
1900 if (!remote_certificate_id.empty())
1901 transport_stats->remote_certificate_id = remote_certificate_id;
Harald Alvestrand5cb78072019-10-28 08:51:171902 // Crypto information
1903 if (channel_stats.ssl_version_bytes) {
1904 char bytes[5];
1905 snprintf(bytes, sizeof(bytes), "%04X", channel_stats.ssl_version_bytes);
1906 transport_stats->tls_version = bytes;
1907 }
1908 if (channel_stats.ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
1909 rtc::SSLStreamAdapter::SslCipherSuiteToName(
1910 channel_stats.ssl_cipher_suite)
1911 .length()) {
1912 transport_stats->dtls_cipher =
1913 rtc::SSLStreamAdapter::SslCipherSuiteToName(
1914 channel_stats.ssl_cipher_suite);
1915 }
1916 if (channel_stats.srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
1917 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite)
1918 .length()) {
1919 transport_stats->srtp_cipher =
1920 rtc::SrtpCryptoSuiteToName(channel_stats.srtp_crypto_suite);
1921 }
hbos2fa7c672016-10-24 11:00:051922 report->AddStats(std::move(transport_stats));
1923 }
1924 }
1925}
1926
1927std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 12:58:021928RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 18:34:401929 const std::map<std::string, cricket::TransportStats>&
1930 transport_stats_by_name) const {
hbosdf6075a2016-12-19 12:58:021931 RTC_DCHECK(network_thread_->IsCurrent());
Henrik Boströme88c95e2020-07-08 09:18:501932 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1933
hbos2fa7c672016-10-24 11:00:051934 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 18:34:401935 for (const auto& entry : transport_stats_by_name) {
1936 const std::string& transport_name = entry.first;
1937
hbos2fa7c672016-10-24 11:00:051938 CertificateStatsPair certificate_stats_pair;
1939 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 18:34:401940 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 11:00:051941 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 08:16:261942 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 11:00:051943 }
Steve Anton5dfde182018-02-06 18:34:401944
Taylor Brandstetterc3928662018-02-23 21:04:511945 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1946 pc_->GetRemoteSSLCertChain(transport_name);
1947 if (remote_cert_chain) {
1948 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 11:00:051949 }
Steve Anton5dfde182018-02-06 18:34:401950
hbos2fa7c672016-10-24 11:00:051951 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 18:34:401952 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 11:00:051953 }
1954 return transport_cert_stats;
1955}
1956
Henrik Boström180faeb2020-11-13 08:05:211957void RTCStatsCollector::PrepareTransceiverStatsInfosAndCallStats_s_w() {
Henrik Boströme88c95e2020-07-08 09:18:501958 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton56bae8d2018-02-15 00:07:421959
Henrik Boström180faeb2020-11-13 08:05:211960 transceiver_stats_infos_.clear();
Steve Anton57858b32018-02-15 23:19:501961 // These are used to invoke GetStats for all the media channels together in
1962 // one worker thread hop.
1963 std::map<cricket::VoiceMediaChannel*,
1964 std::unique_ptr<cricket::VoiceMediaInfo>>
1965 voice_stats;
1966 std::map<cricket::VideoMediaChannel*,
1967 std::unique_ptr<cricket::VideoMediaInfo>>
1968 video_stats;
1969
Henrik Boströme88c95e2020-07-08 09:18:501970 {
1971 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Anton57858b32018-02-15 23:19:501972
Henrik Boströme88c95e2020-07-08 09:18:501973 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1974 cricket::MediaType media_type = transceiver->media_type();
Steve Anton57858b32018-02-15 23:19:501975
Henrik Boströme88c95e2020-07-08 09:18:501976 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1977 // stats have been fetched on the worker thread.
Henrik Boström180faeb2020-11-13 08:05:211978 transceiver_stats_infos_.emplace_back();
1979 RtpTransceiverStatsInfo& stats = transceiver_stats_infos_.back();
Henrik Boströme88c95e2020-07-08 09:18:501980 stats.transceiver = transceiver->internal();
1981 stats.media_type = media_type;
Steve Anton57858b32018-02-15 23:19:501982
Henrik Boströme88c95e2020-07-08 09:18:501983 cricket::ChannelInterface* channel = transceiver->internal()->channel();
1984 if (!channel) {
1985 // The remaining fields require a BaseChannel.
1986 continue;
1987 }
Steve Anton57858b32018-02-15 23:19:501988
Henrik Boströme88c95e2020-07-08 09:18:501989 stats.mid = channel->content_name();
1990 stats.transport_name = channel->transport_name();
1991
1992 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1993 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1994 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1995 voice_stats.end());
1996 voice_stats[voice_channel->media_channel()] =
1997 std::make_unique<cricket::VoiceMediaInfo>();
1998 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1999 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
2000 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
2001 video_stats.end());
2002 video_stats[video_channel->media_channel()] =
2003 std::make_unique<cricket::VideoMediaInfo>();
2004 } else {
2005 RTC_NOTREACHED();
2006 }
Steve Anton57858b32018-02-15 23:19:502007 }
Guido Urdanetaee2388f2018-02-15 16:36:192008 }
Steve Anton57858b32018-02-15 23:19:502009
Henrik Boström180faeb2020-11-13 08:05:212010 // We jump to the worker thread and call GetStats() on each media channel as
2011 // well as GetCallStats(). At the same time we construct the
2012 // TrackMediaInfoMaps, which also needs info from the worker thread. This
2013 // minimizes the number of thread jumps.
Steve Anton57858b32018-02-15 23:19:502014 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
Henrik Boström6fafbe32020-07-08 08:37:002015 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2016
Steve Anton57858b32018-02-15 23:19:502017 for (const auto& entry : voice_stats) {
Niels Möller6b4d9622020-09-14 08:47:502018 if (!entry.first->GetStats(entry.second.get(),
2019 /*get_and_clear_legacy_stats=*/false)) {
Steve Anton57858b32018-02-15 23:19:502020 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
2021 }
2022 }
2023 for (const auto& entry : video_stats) {
2024 if (!entry.first->GetStats(entry.second.get())) {
2025 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
2026 }
2027 }
Steve Anton57858b32018-02-15 23:19:502028
Henrik Boström6fafbe32020-07-08 08:37:002029 // Create the TrackMediaInfoMap for each transceiver stats object.
Henrik Boström180faeb2020-11-13 08:05:212030 for (auto& stats : transceiver_stats_infos_) {
Henrik Boström6fafbe32020-07-08 08:37:002031 auto transceiver = stats.transceiver;
2032 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
2033 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
2034 if (transceiver->channel()) {
2035 cricket::MediaType media_type = transceiver->media_type();
2036 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
2037 auto* voice_channel =
2038 static_cast<cricket::VoiceChannel*>(transceiver->channel());
2039 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
2040 voice_media_info =
2041 std::move(voice_stats[voice_channel->media_channel()]);
2042 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
2043 auto* video_channel =
2044 static_cast<cricket::VideoChannel*>(transceiver->channel());
2045 RTC_DCHECK(video_stats[video_channel->media_channel()]);
2046 video_media_info =
2047 std::move(video_stats[video_channel->media_channel()]);
2048 }
Steve Anton57858b32018-02-15 23:19:502049 }
Henrik Boström6fafbe32020-07-08 08:37:002050 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
2051 for (const auto& sender : transceiver->senders()) {
2052 senders.push_back(sender->internal());
2053 }
2054 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
2055 for (const auto& receiver : transceiver->receivers()) {
2056 receivers.push_back(receiver->internal());
2057 }
2058 stats.track_media_info_map = std::make_unique<TrackMediaInfoMap>(
2059 std::move(voice_media_info), std::move(video_media_info), senders,
2060 receivers);
Steve Anton57858b32018-02-15 23:19:502061 }
Steve Anton57858b32018-02-15 23:19:502062
Henrik Boström180faeb2020-11-13 08:05:212063 call_stats_ = pc_->GetCallStats();
2064 });
hbos0adb8282016-11-23 10:32:062065}
2066
Steve Anton7eca0932018-03-30 22:18:412067std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
Henrik Boströme88c95e2020-07-08 09:18:502068 RTC_DCHECK(signaling_thread_->IsCurrent());
2069 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
2070
Steve Anton7eca0932018-03-30 22:18:412071 std::set<std::string> transport_names;
2072 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
2073 if (transceiver->internal()->channel()) {
2074 transport_names.insert(
2075 transceiver->internal()->channel()->transport_name());
2076 }
2077 }
2078 if (pc_->rtp_data_channel()) {
2079 transport_names.insert(pc_->rtp_data_channel()->transport_name());
2080 }
2081 if (pc_->sctp_transport_name()) {
2082 transport_names.insert(*pc_->sctp_transport_name());
2083 }
2084 return transport_names;
2085}
2086
Taylor Brandstetter3a034e12020-07-09 22:32:342087void RTCStatsCollector::OnRtpDataChannelCreated(RtpDataChannel* channel) {
hbos82ebe022016-11-14 09:41:092088 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
2089 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
2090}
2091
Taylor Brandstetter3a034e12020-07-09 22:32:342092void RTCStatsCollector::OnSctpDataChannelCreated(SctpDataChannel* channel) {
2093 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
2094 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
2095}
2096
2097void RTCStatsCollector::OnDataChannelOpened(DataChannelInterface* channel) {
hbos82ebe022016-11-14 09:41:092098 RTC_DCHECK(signaling_thread_->IsCurrent());
Jonas Olssona4d87372019-07-05 17:08:332099 bool result = internal_record_.opened_data_channels
2100 .insert(reinterpret_cast<uintptr_t>(channel))
2101 .second;
hbos82ebe022016-11-14 09:41:092102 ++internal_record_.data_channels_opened;
2103 RTC_DCHECK(result);
2104}
2105
Taylor Brandstetter3a034e12020-07-09 22:32:342106void RTCStatsCollector::OnDataChannelClosed(DataChannelInterface* channel) {
hbos82ebe022016-11-14 09:41:092107 RTC_DCHECK(signaling_thread_->IsCurrent());
2108 // Only channels that have been fully opened (and have increased the
2109 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 10:14:142110 if (internal_record_.opened_data_channels.erase(
2111 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 09:41:092112 ++internal_record_.data_channels_closed;
2113 }
2114}
2115
hboscc555c52016-10-18 19:48:312116const char* CandidateTypeToRTCIceCandidateTypeForTesting(
2117 const std::string& type) {
2118 return CandidateTypeToRTCIceCandidateType(type);
2119}
2120
2121const char* DataStateToRTCDataChannelStateForTesting(
2122 DataChannelInterface::DataState state) {
2123 return DataStateToRTCDataChannelState(state);
2124}
2125
hbosd565b732016-08-30 21:04:352126} // namespace webrtc