blob: ec917aec72e53b9706438401768658f9a74ae923 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/rtc_stats_collector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <string>
hbosd565b732016-08-30 14:04:35 -070015#include <utility>
16#include <vector>
17
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "api/media_stream_interface.h"
21#include "api/peer_connection_interface.h"
Henrik Boström2e069262019-04-09 13:59:31 +020022#include "api/video/video_content_type.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "media/base/media_channel.h"
24#include "p2p/base/p2p_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "pc/peer_connection.h"
27#include "pc/rtc_stats_traversal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/checks.h"
Jonas Olsson43568dd2018-06-11 16:25:54 +020029#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/trace_event.h"
hbosd565b732016-08-30 14:04:35 -070032
33namespace webrtc {
34
hboscc555c52016-10-18 12:48:31 -070035namespace {
36
Henrik Boström646fda02019-05-22 15:49:42 +020037// TODO(https://crbug.com/webrtc/10656): Consider making IDs less predictable.
hbos2fa7c672016-10-24 04:00:05 -070038std::string RTCCertificateIDFromFingerprint(const std::string& fingerprint) {
39 return "RTCCertificate_" + fingerprint;
40}
41
Steve Anton57858b32018-02-15 15:19:50 -080042std::string RTCCodecStatsIDFromMidDirectionAndPayload(const std::string& mid,
43 bool inbound,
44 uint32_t payload_type) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020045 char buf[1024];
46 rtc::SimpleStringBuilder sb(buf);
47 sb << "RTCCodec_" << mid << (inbound ? "_Inbound_" : "_Outbound_")
48 << payload_type;
49 return sb.str();
hbos0adb8282016-11-23 02:32:06 -080050}
51
hbos2fa7c672016-10-24 04:00:05 -070052std::string RTCIceCandidatePairStatsIDFromConnectionInfo(
53 const cricket::ConnectionInfo& info) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020054 char buf[4096];
55 rtc::SimpleStringBuilder sb(buf);
56 sb << "RTCIceCandidatePair_" << info.local_candidate.id() << "_"
57 << info.remote_candidate.id();
58 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070059}
60
Harald Alvestranda3dab842018-01-14 09:18:58 +010061const char kSender[] = "sender";
62const char kReceiver[] = "receiver";
63
64std::string RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
65 const char* direction,
Harald Alvestrandc72af932018-01-11 17:18:19 +010066 int attachment_id) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020067 char buf[1024];
68 rtc::SimpleStringBuilder sb(buf);
69 sb << "RTCMediaStreamTrack_" << direction << "_" << attachment_id;
70 return sb.str();
hbos09bc1282016-11-08 06:29:22 -080071}
72
hbos2fa7c672016-10-24 04:00:05 -070073std::string RTCTransportStatsIDFromTransportChannel(
74 const std::string& transport_name, int channel_component) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020075 char buf[1024];
76 rtc::SimpleStringBuilder sb(buf);
77 sb << "RTCTransport_" << transport_name << "_" << channel_component;
78 return sb.str();
hbos2fa7c672016-10-24 04:00:05 -070079}
80
hboseeafe942016-11-01 03:00:17 -070081std::string RTCInboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020082 char buf[1024];
83 rtc::SimpleStringBuilder sb(buf);
84 sb << "RTCInboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
85 return sb.str();
hboseeafe942016-11-01 03:00:17 -070086}
87
hbos6ded1902016-11-01 01:50:46 -070088std::string RTCOutboundRTPStreamStatsIDFromSSRC(bool audio, uint32_t ssrc) {
Jonas Olsson43568dd2018-06-11 16:25:54 +020089 char buf[1024];
90 rtc::SimpleStringBuilder sb(buf);
91 sb << "RTCOutboundRTP" << (audio ? "Audio" : "Video") << "Stream_" << ssrc;
92 return sb.str();
hbos6ded1902016-11-01 01:50:46 -070093}
94
Henrik Boström8605fbf2019-06-24 16:44:51 +020095std::string RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(
Henrik Boström883eefc2019-05-27 13:40:25 +020096 cricket::MediaType media_type,
Henrik Boström883eefc2019-05-27 13:40:25 +020097 uint32_t source_ssrc) {
98 char buf[1024];
99 rtc::SimpleStringBuilder sb(buf);
100 sb << "RTCRemoteInboundRtp"
101 << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
Henrik Boström8605fbf2019-06-24 16:44:51 +0200102 << "Stream_" << source_ssrc;
Henrik Boström883eefc2019-05-27 13:40:25 +0200103 return sb.str();
104}
105
Henrik Boström646fda02019-05-22 15:49:42 +0200106std::string RTCMediaSourceStatsIDFromKindAndAttachment(
107 cricket::MediaType media_type,
108 int attachment_id) {
109 char buf[1024];
110 rtc::SimpleStringBuilder sb(buf);
111 sb << "RTC" << (media_type == cricket::MEDIA_TYPE_AUDIO ? "Audio" : "Video")
112 << "Source_" << attachment_id;
113 return sb.str();
114}
115
hbosab9f6e42016-10-07 02:18:47 -0700116const char* CandidateTypeToRTCIceCandidateType(const std::string& type) {
117 if (type == cricket::LOCAL_PORT_TYPE)
118 return RTCIceCandidateType::kHost;
119 if (type == cricket::STUN_PORT_TYPE)
120 return RTCIceCandidateType::kSrflx;
121 if (type == cricket::PRFLX_PORT_TYPE)
122 return RTCIceCandidateType::kPrflx;
123 if (type == cricket::RELAY_PORT_TYPE)
124 return RTCIceCandidateType::kRelay;
125 RTC_NOTREACHED();
126 return nullptr;
127}
128
hboscc555c52016-10-18 12:48:31 -0700129const char* DataStateToRTCDataChannelState(
130 DataChannelInterface::DataState state) {
131 switch (state) {
132 case DataChannelInterface::kConnecting:
133 return RTCDataChannelState::kConnecting;
134 case DataChannelInterface::kOpen:
135 return RTCDataChannelState::kOpen;
136 case DataChannelInterface::kClosing:
137 return RTCDataChannelState::kClosing;
138 case DataChannelInterface::kClosed:
139 return RTCDataChannelState::kClosed;
140 default:
141 RTC_NOTREACHED();
142 return nullptr;
143 }
144}
145
hbos06495bc2017-01-02 08:08:18 -0800146const char* IceCandidatePairStateToRTCStatsIceCandidatePairState(
147 cricket::IceCandidatePairState state) {
148 switch (state) {
149 case cricket::IceCandidatePairState::WAITING:
150 return RTCStatsIceCandidatePairState::kWaiting;
151 case cricket::IceCandidatePairState::IN_PROGRESS:
152 return RTCStatsIceCandidatePairState::kInProgress;
153 case cricket::IceCandidatePairState::SUCCEEDED:
154 return RTCStatsIceCandidatePairState::kSucceeded;
155 case cricket::IceCandidatePairState::FAILED:
156 return RTCStatsIceCandidatePairState::kFailed;
157 default:
158 RTC_NOTREACHED();
159 return nullptr;
160 }
161}
162
hbos7064d592017-01-16 07:38:02 -0800163const char* DtlsTransportStateToRTCDtlsTransportState(
164 cricket::DtlsTransportState state) {
165 switch (state) {
166 case cricket::DTLS_TRANSPORT_NEW:
167 return RTCDtlsTransportState::kNew;
168 case cricket::DTLS_TRANSPORT_CONNECTING:
169 return RTCDtlsTransportState::kConnecting;
170 case cricket::DTLS_TRANSPORT_CONNECTED:
171 return RTCDtlsTransportState::kConnected;
172 case cricket::DTLS_TRANSPORT_CLOSED:
173 return RTCDtlsTransportState::kClosed;
174 case cricket::DTLS_TRANSPORT_FAILED:
175 return RTCDtlsTransportState::kFailed;
176 default:
177 RTC_NOTREACHED();
178 return nullptr;
179 }
180}
181
Gary Liu37e489c2017-11-21 10:49:36 -0800182const char* NetworkAdapterTypeToStatsType(rtc::AdapterType type) {
183 switch (type) {
184 case rtc::ADAPTER_TYPE_CELLULAR:
185 return RTCNetworkType::kCellular;
186 case rtc::ADAPTER_TYPE_ETHERNET:
187 return RTCNetworkType::kEthernet;
188 case rtc::ADAPTER_TYPE_WIFI:
189 return RTCNetworkType::kWifi;
190 case rtc::ADAPTER_TYPE_VPN:
191 return RTCNetworkType::kVpn;
192 case rtc::ADAPTER_TYPE_UNKNOWN:
193 case rtc::ADAPTER_TYPE_LOOPBACK:
Qingsi Wang9f1de692018-06-28 15:38:09 -0700194 case rtc::ADAPTER_TYPE_ANY:
Gary Liu37e489c2017-11-21 10:49:36 -0800195 return RTCNetworkType::kUnknown;
196 }
197 RTC_NOTREACHED();
198 return nullptr;
199}
200
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200201const char* QualityLimitationReasonToRTCQualityLimitationReason(
202 QualityLimitationReason reason) {
203 switch (reason) {
204 case QualityLimitationReason::kNone:
205 return RTCQualityLimitationReason::kNone;
206 case QualityLimitationReason::kCpu:
207 return RTCQualityLimitationReason::kCpu;
208 case QualityLimitationReason::kBandwidth:
209 return RTCQualityLimitationReason::kBandwidth;
210 case QualityLimitationReason::kOther:
211 return RTCQualityLimitationReason::kOther;
212 }
213}
214
hbos9e302742017-01-20 02:47:10 -0800215double DoubleAudioLevelFromIntAudioLevel(int audio_level) {
216 RTC_DCHECK_GE(audio_level, 0);
217 RTC_DCHECK_LE(audio_level, 32767);
218 return audio_level / 32767.0;
219}
220
hbos0adb8282016-11-23 02:32:06 -0800221std::unique_ptr<RTCCodecStats> CodecStatsFromRtpCodecParameters(
Steve Anton57858b32018-02-15 15:19:50 -0800222 uint64_t timestamp_us,
223 const std::string& mid,
224 bool inbound,
hbos0adb8282016-11-23 02:32:06 -0800225 const RtpCodecParameters& codec_params) {
226 RTC_DCHECK_GE(codec_params.payload_type, 0);
227 RTC_DCHECK_LE(codec_params.payload_type, 127);
deadbeefe702b302017-02-04 12:09:01 -0800228 RTC_DCHECK(codec_params.clock_rate);
hbos0adb8282016-11-23 02:32:06 -0800229 uint32_t payload_type = static_cast<uint32_t>(codec_params.payload_type);
230 std::unique_ptr<RTCCodecStats> codec_stats(new RTCCodecStats(
Steve Anton57858b32018-02-15 15:19:50 -0800231 RTCCodecStatsIDFromMidDirectionAndPayload(mid, inbound, payload_type),
hbos0adb8282016-11-23 02:32:06 -0800232 timestamp_us));
233 codec_stats->payload_type = payload_type;
hbos13f54b22017-02-28 06:56:04 -0800234 codec_stats->mime_type = codec_params.mime_type();
deadbeefe702b302017-02-04 12:09:01 -0800235 if (codec_params.clock_rate) {
236 codec_stats->clock_rate = static_cast<uint32_t>(*codec_params.clock_rate);
237 }
hbos0adb8282016-11-23 02:32:06 -0800238 return codec_stats;
239}
240
hbos09bc1282016-11-08 06:29:22 -0800241void SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
242 const MediaStreamTrackInterface& track,
243 RTCMediaStreamTrackStats* track_stats) {
244 track_stats->track_identifier = track.id();
245 track_stats->ended = (track.state() == MediaStreamTrackInterface::kEnded);
246}
247
hbos820f5782016-11-22 03:16:50 -0800248// Provides the media independent counters (both audio and video).
hboseeafe942016-11-01 03:00:17 -0700249void SetInboundRTPStreamStatsFromMediaReceiverInfo(
250 const cricket::MediaReceiverInfo& media_receiver_info,
251 RTCInboundRTPStreamStats* inbound_stats) {
252 RTC_DCHECK(inbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800253 inbound_stats->ssrc = media_receiver_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100254 // TODO(hbos): Support the remote case. https://crbug.com/657855
hboseeafe942016-11-01 03:00:17 -0700255 inbound_stats->is_remote = false;
hboseeafe942016-11-01 03:00:17 -0700256 inbound_stats->packets_received =
257 static_cast<uint32_t>(media_receiver_info.packets_rcvd);
258 inbound_stats->bytes_received =
259 static_cast<uint64_t>(media_receiver_info.bytes_rcvd);
hbos02cd4d62016-12-09 04:19:44 -0800260 inbound_stats->packets_lost =
Harald Alvestrand719487e2017-12-13 12:26:04 +0100261 static_cast<int32_t>(media_receiver_info.packets_lost);
hboseeafe942016-11-01 03:00:17 -0700262}
263
264void SetInboundRTPStreamStatsFromVoiceReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800265 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700266 const cricket::VoiceReceiverInfo& voice_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800267 RTCInboundRTPStreamStats* inbound_audio) {
hboseeafe942016-11-01 03:00:17 -0700268 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800269 voice_receiver_info, inbound_audio);
270 inbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200271 inbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800272 if (voice_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800273 inbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
274 mid, true, *voice_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800275 }
hbos820f5782016-11-22 03:16:50 -0800276 inbound_audio->jitter =
hboseeafe942016-11-01 03:00:17 -0700277 static_cast<double>(voice_receiver_info.jitter_ms) /
278 rtc::kNumMillisecsPerSec;
hbos820f5782016-11-22 03:16:50 -0800279 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
280 // purposefully left undefined for audio.
Henrik Boström01738c62019-04-15 17:32:00 +0200281 if (voice_receiver_info.last_packet_received_timestamp_ms) {
282 inbound_audio->last_packet_received_timestamp =
283 static_cast<double>(
284 *voice_receiver_info.last_packet_received_timestamp_ms) /
285 rtc::kNumMillisecsPerSec;
286 }
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200287 inbound_audio->fec_packets_received =
288 voice_receiver_info.fec_packets_received;
289 inbound_audio->fec_packets_discarded =
290 voice_receiver_info.fec_packets_discarded;
hboseeafe942016-11-01 03:00:17 -0700291}
292
293void SetInboundRTPStreamStatsFromVideoReceiverInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800294 const std::string& mid,
hboseeafe942016-11-01 03:00:17 -0700295 const cricket::VideoReceiverInfo& video_receiver_info,
hbos820f5782016-11-22 03:16:50 -0800296 RTCInboundRTPStreamStats* inbound_video) {
hboseeafe942016-11-01 03:00:17 -0700297 SetInboundRTPStreamStatsFromMediaReceiverInfo(
hbos820f5782016-11-22 03:16:50 -0800298 video_receiver_info, inbound_video);
299 inbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200300 inbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800301 if (video_receiver_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800302 inbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
303 mid, true, *video_receiver_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800304 }
hbos820f5782016-11-22 03:16:50 -0800305 inbound_video->fir_count =
306 static_cast<uint32_t>(video_receiver_info.firs_sent);
307 inbound_video->pli_count =
308 static_cast<uint32_t>(video_receiver_info.plis_sent);
309 inbound_video->nack_count =
310 static_cast<uint32_t>(video_receiver_info.nacks_sent);
hbos6769c492017-01-02 08:35:13 -0800311 inbound_video->frames_decoded = video_receiver_info.frames_decoded;
Rasmus Brandt2efae772019-06-27 14:29:34 +0200312 inbound_video->key_frames_decoded = video_receiver_info.key_frames_decoded;
hbosa51d4f32017-02-16 05:34:48 -0800313 if (video_receiver_info.qp_sum)
314 inbound_video->qp_sum = *video_receiver_info.qp_sum;
Johannes Kronbfd343b2019-07-01 10:07:50 +0200315 inbound_video->total_decode_time =
316 static_cast<double>(video_receiver_info.total_decode_time_ms) /
317 rtc::kNumMillisecsPerSec;
Henrik Boström01738c62019-04-15 17:32:00 +0200318 if (video_receiver_info.last_packet_received_timestamp_ms) {
319 inbound_video->last_packet_received_timestamp =
320 static_cast<double>(
321 *video_receiver_info.last_packet_received_timestamp_ms) /
322 rtc::kNumMillisecsPerSec;
323 }
Henrik Boström2e069262019-04-09 13:59:31 +0200324 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
325 // optional, support the "unspecified" value.
326 if (video_receiver_info.content_type == VideoContentType::SCREENSHARE)
327 inbound_video->content_type = RTCContentType::kScreenshare;
hboseeafe942016-11-01 03:00:17 -0700328}
329
hbos820f5782016-11-22 03:16:50 -0800330// Provides the media independent counters (both audio and video).
hbos6ded1902016-11-01 01:50:46 -0700331void SetOutboundRTPStreamStatsFromMediaSenderInfo(
332 const cricket::MediaSenderInfo& media_sender_info,
333 RTCOutboundRTPStreamStats* outbound_stats) {
334 RTC_DCHECK(outbound_stats);
hbos3443bb72017-02-07 06:28:11 -0800335 outbound_stats->ssrc = media_sender_info.ssrc();
Harald Alvestrand89061872018-01-02 14:08:34 +0100336 // TODO(hbos): Support the remote case. https://crbug.com/657856
hbos6ded1902016-11-01 01:50:46 -0700337 outbound_stats->is_remote = false;
hbos6ded1902016-11-01 01:50:46 -0700338 outbound_stats->packets_sent =
339 static_cast<uint32_t>(media_sender_info.packets_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200340 outbound_stats->retransmitted_packets_sent =
341 media_sender_info.retransmitted_packets_sent;
hbos6ded1902016-11-01 01:50:46 -0700342 outbound_stats->bytes_sent =
343 static_cast<uint64_t>(media_sender_info.bytes_sent);
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200344 outbound_stats->retransmitted_bytes_sent =
345 media_sender_info.retransmitted_bytes_sent;
hbos6ded1902016-11-01 01:50:46 -0700346}
347
348void SetOutboundRTPStreamStatsFromVoiceSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800349 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700350 const cricket::VoiceSenderInfo& voice_sender_info,
351 RTCOutboundRTPStreamStats* outbound_audio) {
352 SetOutboundRTPStreamStatsFromMediaSenderInfo(
353 voice_sender_info, outbound_audio);
354 outbound_audio->media_type = "audio";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200355 outbound_audio->kind = "audio";
hbos585a9b12017-02-07 04:59:16 -0800356 if (voice_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800357 outbound_audio->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
358 mid, false, *voice_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800359 }
hbos6ded1902016-11-01 01:50:46 -0700360 // |fir_count|, |pli_count| and |sli_count| are only valid for video and are
361 // purposefully left undefined for audio.
362}
363
364void SetOutboundRTPStreamStatsFromVideoSenderInfo(
Steve Anton57858b32018-02-15 15:19:50 -0800365 const std::string& mid,
hbos6ded1902016-11-01 01:50:46 -0700366 const cricket::VideoSenderInfo& video_sender_info,
367 RTCOutboundRTPStreamStats* outbound_video) {
368 SetOutboundRTPStreamStatsFromMediaSenderInfo(
369 video_sender_info, outbound_video);
370 outbound_video->media_type = "video";
Philipp Hancke3bc01662018-08-28 14:55:03 +0200371 outbound_video->kind = "video";
hbos585a9b12017-02-07 04:59:16 -0800372 if (video_sender_info.codec_payload_type) {
Steve Anton57858b32018-02-15 15:19:50 -0800373 outbound_video->codec_id = RTCCodecStatsIDFromMidDirectionAndPayload(
374 mid, false, *video_sender_info.codec_payload_type);
hbos585a9b12017-02-07 04:59:16 -0800375 }
hbos6ded1902016-11-01 01:50:46 -0700376 outbound_video->fir_count =
377 static_cast<uint32_t>(video_sender_info.firs_rcvd);
378 outbound_video->pli_count =
379 static_cast<uint32_t>(video_sender_info.plis_rcvd);
380 outbound_video->nack_count =
381 static_cast<uint32_t>(video_sender_info.nacks_rcvd);
hbos6769c492017-01-02 08:35:13 -0800382 if (video_sender_info.qp_sum)
383 outbound_video->qp_sum = *video_sender_info.qp_sum;
384 outbound_video->frames_encoded = video_sender_info.frames_encoded;
Rasmus Brandt2efae772019-06-27 14:29:34 +0200385 outbound_video->key_frames_encoded = video_sender_info.key_frames_encoded;
Henrik Boströmf71362f2019-04-08 16:14:23 +0200386 outbound_video->total_encode_time =
387 static_cast<double>(video_sender_info.total_encode_time_ms) /
388 rtc::kNumMillisecsPerSec;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200389 outbound_video->total_encoded_bytes_target =
390 video_sender_info.total_encoded_bytes_target;
Henrik Boström9fe18342019-05-16 18:38:20 +0200391 outbound_video->total_packet_send_delay =
392 static_cast<double>(video_sender_info.total_packet_send_delay_ms) /
393 rtc::kNumMillisecsPerSec;
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200394 outbound_video->quality_limitation_reason =
395 QualityLimitationReasonToRTCQualityLimitationReason(
396 video_sender_info.quality_limitation_reason);
Henrik Boström2e069262019-04-09 13:59:31 +0200397 // TODO(https://crbug.com/webrtc/10529): When info's |content_info| is
398 // optional, support the "unspecified" value.
399 if (video_sender_info.content_type == VideoContentType::SCREENSHARE)
400 outbound_video->content_type = RTCContentType::kScreenshare;
hbos6ded1902016-11-01 01:50:46 -0700401}
402
Henrik Boström883eefc2019-05-27 13:40:25 +0200403std::unique_ptr<RTCRemoteInboundRtpStreamStats>
404ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
405 const ReportBlockData& report_block_data,
406 cricket::MediaType media_type,
407 const RTCStatsReport& report) {
408 const auto& report_block = report_block_data.report_block();
409 // RTCStats' timestamp generally refers to when the metric was sampled, but
410 // for "remote-[outbound/inbound]-rtp" it refers to the local time when the
411 // Report Block was received.
412 auto remote_inbound = absl::make_unique<RTCRemoteInboundRtpStreamStats>(
Henrik Boström8605fbf2019-06-24 16:44:51 +0200413 RTCRemoteInboundRtpStreamStatsIdFromSourceSsrc(media_type,
414 report_block.source_ssrc),
Henrik Boström883eefc2019-05-27 13:40:25 +0200415 /*timestamp=*/report_block_data.report_block_timestamp_utc_us());
Henrik Boström8605fbf2019-06-24 16:44:51 +0200416 remote_inbound->ssrc = report_block.source_ssrc;
Henrik Boström883eefc2019-05-27 13:40:25 +0200417 remote_inbound->kind =
418 media_type == cricket::MEDIA_TYPE_AUDIO ? "audio" : "video";
419 remote_inbound->packets_lost = report_block.packets_lost;
420 remote_inbound->round_trip_time =
421 static_cast<double>(report_block_data.last_rtt_ms()) /
422 rtc::kNumMillisecsPerSec;
423
424 std::string local_id = RTCOutboundRTPStreamStatsIDFromSSRC(
425 media_type == cricket::MEDIA_TYPE_AUDIO, report_block.source_ssrc);
426 const auto* local_id_stat = report.Get(local_id);
427 if (local_id_stat) {
428 remote_inbound->local_id = local_id;
429 const auto& outbound_rtp =
430 local_id_stat->cast_to<RTCOutboundRTPStreamStats>();
431 // The RTP/RTCP transport is obtained from the
432 // RTCOutboundRtpStreamStats's transport.
433 const auto* transport_from_id = outbound_rtp.transport_id.is_defined()
434 ? report.Get(*outbound_rtp.transport_id)
435 : nullptr;
436 if (transport_from_id) {
437 const auto& transport = transport_from_id->cast_to<RTCTransportStats>();
438 // If RTP and RTCP are not multiplexed, there is a separate RTCP
439 // transport paired with the RTP transport, otherwise the same
440 // transport is used for RTCP and RTP.
441 remote_inbound->transport_id =
442 transport.rtcp_transport_stats_id.is_defined()
443 ? *transport.rtcp_transport_stats_id
444 : *outbound_rtp.transport_id;
445 }
446 // We're assuming the same codec is used on both ends. However if the
447 // codec is switched out on the fly we may have received a Report Block
448 // based on the previous codec and there is no way to tell which point in
449 // time the codec changed for the remote end.
450 const auto* codec_from_id = outbound_rtp.codec_id.is_defined()
451 ? report.Get(*outbound_rtp.codec_id)
452 : nullptr;
453 if (codec_from_id) {
454 remote_inbound->codec_id = *outbound_rtp.codec_id;
455 const auto& codec = codec_from_id->cast_to<RTCCodecStats>();
456 if (codec.clock_rate.is_defined()) {
457 // The Report Block jitter is expressed in RTP timestamp units
458 // (https://tools.ietf.org/html/rfc3550#section-6.4.1). To convert this
459 // to seconds we divide by the codec's clock rate.
460 remote_inbound->jitter =
461 static_cast<double>(report_block.jitter) / *codec.clock_rate;
462 }
463 }
464 }
465 return remote_inbound;
466}
467
hbos02ba2112016-10-28 05:14:53 -0700468void ProduceCertificateStatsFromSSLCertificateStats(
469 int64_t timestamp_us, const rtc::SSLCertificateStats& certificate_stats,
470 RTCStatsReport* report) {
471 RTCCertificateStats* prev_certificate_stats = nullptr;
472 for (const rtc::SSLCertificateStats* s = &certificate_stats; s;
473 s = s->issuer.get()) {
hbos02d2a922016-12-21 01:29:05 -0800474 std::string certificate_stats_id =
475 RTCCertificateIDFromFingerprint(s->fingerprint);
476 // It is possible for the same certificate to show up multiple times, e.g.
477 // if local and remote side use the same certificate in a loopback call.
478 // If the report already contains stats for this certificate, skip it.
479 if (report->Get(certificate_stats_id)) {
480 RTC_DCHECK_EQ(s, &certificate_stats);
481 break;
482 }
hbos02ba2112016-10-28 05:14:53 -0700483 RTCCertificateStats* certificate_stats = new RTCCertificateStats(
hbos02d2a922016-12-21 01:29:05 -0800484 certificate_stats_id, timestamp_us);
hbos02ba2112016-10-28 05:14:53 -0700485 certificate_stats->fingerprint = s->fingerprint;
486 certificate_stats->fingerprint_algorithm = s->fingerprint_algorithm;
487 certificate_stats->base64_certificate = s->base64_certificate;
488 if (prev_certificate_stats)
489 prev_certificate_stats->issuer_certificate_id = certificate_stats->id();
490 report->AddStats(std::unique_ptr<RTCCertificateStats>(certificate_stats));
491 prev_certificate_stats = certificate_stats;
492 }
493}
494
495const std::string& ProduceIceCandidateStats(
496 int64_t timestamp_us, const cricket::Candidate& candidate, bool is_local,
hbosb4e426e2017-01-02 09:59:31 -0800497 const std::string& transport_id, RTCStatsReport* report) {
hbos02ba2112016-10-28 05:14:53 -0700498 const std::string& id = "RTCIceCandidate_" + candidate.id();
499 const RTCStats* stats = report->Get(id);
500 if (!stats) {
501 std::unique_ptr<RTCIceCandidateStats> candidate_stats;
502 if (is_local)
503 candidate_stats.reset(new RTCLocalIceCandidateStats(id, timestamp_us));
504 else
505 candidate_stats.reset(new RTCRemoteIceCandidateStats(id, timestamp_us));
hbosb4e426e2017-01-02 09:59:31 -0800506 candidate_stats->transport_id = transport_id;
Gary Liu37e489c2017-11-21 10:49:36 -0800507 if (is_local) {
508 candidate_stats->network_type =
509 NetworkAdapterTypeToStatsType(candidate.network_type());
Philipp Hancke95513752018-09-27 14:40:08 +0200510 if (candidate.type() == cricket::RELAY_PORT_TYPE) {
511 std::string relay_protocol = candidate.relay_protocol();
512 RTC_DCHECK(relay_protocol.compare("udp") == 0 ||
513 relay_protocol.compare("tcp") == 0 ||
514 relay_protocol.compare("tls") == 0);
515 candidate_stats->relay_protocol = relay_protocol;
516 }
Gary Liu37e489c2017-11-21 10:49:36 -0800517 } else {
518 // We don't expect to know the adapter type of remote candidates.
519 RTC_DCHECK_EQ(rtc::ADAPTER_TYPE_UNKNOWN, candidate.network_type());
520 }
hbos02ba2112016-10-28 05:14:53 -0700521 candidate_stats->ip = candidate.address().ipaddr().ToString();
522 candidate_stats->port = static_cast<int32_t>(candidate.address().port());
523 candidate_stats->protocol = candidate.protocol();
524 candidate_stats->candidate_type = CandidateTypeToRTCIceCandidateType(
525 candidate.type());
526 candidate_stats->priority = static_cast<int32_t>(candidate.priority());
527
528 stats = candidate_stats.get();
529 report->AddStats(std::move(candidate_stats));
530 }
531 RTC_DCHECK_EQ(stats->type(), is_local ? RTCLocalIceCandidateStats::kType
532 : RTCRemoteIceCandidateStats::kType);
533 return stats->id();
534}
535
hbos9e302742017-01-20 02:47:10 -0800536std::unique_ptr<RTCMediaStreamTrackStats>
537ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
538 int64_t timestamp_us,
539 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100540 const cricket::VoiceSenderInfo& voice_sender_info,
541 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800542 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
543 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100544 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
545 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100546 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800547 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
548 audio_track, audio_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200549 audio_track_stats->media_source_id =
550 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
551 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800552 audio_track_stats->remote_source = false;
553 audio_track_stats->detached = false;
554 if (voice_sender_info.audio_level >= 0) {
555 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
556 voice_sender_info.audio_level);
557 }
zsteine76bd3a2017-07-14 12:17:49 -0700558 audio_track_stats->total_audio_energy = voice_sender_info.total_input_energy;
559 audio_track_stats->total_samples_duration =
560 voice_sender_info.total_input_duration;
Ivo Creusen56d46092017-11-24 17:29:59 +0100561 if (voice_sender_info.apm_statistics.echo_return_loss) {
562 audio_track_stats->echo_return_loss =
563 *voice_sender_info.apm_statistics.echo_return_loss;
hbos9e302742017-01-20 02:47:10 -0800564 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100565 if (voice_sender_info.apm_statistics.echo_return_loss_enhancement) {
566 audio_track_stats->echo_return_loss_enhancement =
567 *voice_sender_info.apm_statistics.echo_return_loss_enhancement;
hbos9e302742017-01-20 02:47:10 -0800568 }
569 return audio_track_stats;
570}
571
572std::unique_ptr<RTCMediaStreamTrackStats>
573ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
574 int64_t timestamp_us,
575 const AudioTrackInterface& audio_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100576 const cricket::VoiceReceiverInfo& voice_receiver_info,
577 int attachment_id) {
578 // Since receiver tracks can't be reattached, we use the SSRC as
579 // an attachment identifier.
hbos9e302742017-01-20 02:47:10 -0800580 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats(
581 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100582 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
583 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100584 timestamp_us, RTCMediaStreamTrackKind::kAudio));
hbos9e302742017-01-20 02:47:10 -0800585 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
586 audio_track, audio_track_stats.get());
587 audio_track_stats->remote_source = true;
588 audio_track_stats->detached = false;
589 if (voice_receiver_info.audio_level >= 0) {
590 audio_track_stats->audio_level = DoubleAudioLevelFromIntAudioLevel(
591 voice_receiver_info.audio_level);
592 }
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200593 audio_track_stats->jitter_buffer_delay =
594 voice_receiver_info.jitter_buffer_delay_seconds;
Chen Xing0acffb52019-01-15 15:46:29 +0100595 audio_track_stats->jitter_buffer_emitted_count =
596 voice_receiver_info.jitter_buffer_emitted_count;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200597 audio_track_stats->inserted_samples_for_deceleration =
598 voice_receiver_info.inserted_samples_for_deceleration;
599 audio_track_stats->removed_samples_for_acceleration =
600 voice_receiver_info.removed_samples_for_acceleration;
zsteine76bd3a2017-07-14 12:17:49 -0700601 audio_track_stats->total_audio_energy =
602 voice_receiver_info.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700603 audio_track_stats->total_samples_received =
604 voice_receiver_info.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -0700605 audio_track_stats->total_samples_duration =
606 voice_receiver_info.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700607 audio_track_stats->concealed_samples = voice_receiver_info.concealed_samples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200608 audio_track_stats->silent_concealed_samples =
609 voice_receiver_info.silent_concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200610 audio_track_stats->concealment_events =
611 voice_receiver_info.concealment_events;
Ruslan Burakov8af88962018-11-22 17:21:10 +0100612 audio_track_stats->jitter_buffer_flushes =
613 voice_receiver_info.jitter_buffer_flushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100614 audio_track_stats->delayed_packet_outage_samples =
615 voice_receiver_info.delayed_packet_outage_samples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100616 audio_track_stats->relative_packet_arrival_delay =
617 voice_receiver_info.relative_packet_arrival_delay_seconds;
Henrik Lundin44125fa2019-04-29 17:00:46 +0200618 audio_track_stats->interruption_count =
619 voice_receiver_info.interruption_count >= 0
620 ? voice_receiver_info.interruption_count
621 : 0;
622 audio_track_stats->total_interruption_duration =
623 static_cast<double>(voice_receiver_info.total_interruption_duration_ms) /
624 rtc::kNumMillisecsPerSec;
hbos9e302742017-01-20 02:47:10 -0800625 return audio_track_stats;
626}
627
628std::unique_ptr<RTCMediaStreamTrackStats>
629ProduceMediaStreamTrackStatsFromVideoSenderInfo(
630 int64_t timestamp_us,
631 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100632 const cricket::VideoSenderInfo& video_sender_info,
633 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800634 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
635 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100636 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
Harald Alvestranda3dab842018-01-14 09:18:58 +0100637 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100638 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800639 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
640 video_track, video_track_stats.get());
Henrik Boström646fda02019-05-22 15:49:42 +0200641 video_track_stats->media_source_id =
642 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
643 attachment_id);
hbos9e302742017-01-20 02:47:10 -0800644 video_track_stats->remote_source = false;
645 video_track_stats->detached = false;
646 video_track_stats->frame_width = static_cast<uint32_t>(
647 video_sender_info.send_frame_width);
648 video_track_stats->frame_height = static_cast<uint32_t>(
649 video_sender_info.send_frame_height);
hbosfefe0762017-01-20 06:14:25 -0800650 // TODO(hbos): Will reduce this by frames dropped due to congestion control
Harald Alvestrand89061872018-01-02 14:08:34 +0100651 // when available. https://crbug.com/659137
hbosfefe0762017-01-20 06:14:25 -0800652 video_track_stats->frames_sent = video_sender_info.frames_encoded;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100653 video_track_stats->huge_frames_sent = video_sender_info.huge_frames_sent;
hbos9e302742017-01-20 02:47:10 -0800654 return video_track_stats;
655}
656
657std::unique_ptr<RTCMediaStreamTrackStats>
658ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
659 int64_t timestamp_us,
660 const VideoTrackInterface& video_track,
Harald Alvestrandc72af932018-01-11 17:18:19 +0100661 const cricket::VideoReceiverInfo& video_receiver_info,
662 int attachment_id) {
hbos9e302742017-01-20 02:47:10 -0800663 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats(
664 new RTCMediaStreamTrackStats(
Harald Alvestranda3dab842018-01-14 09:18:58 +0100665 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kReceiver,
666
667 attachment_id),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100668 timestamp_us, RTCMediaStreamTrackKind::kVideo));
hbos9e302742017-01-20 02:47:10 -0800669 SetMediaStreamTrackStatsFromMediaStreamTrackInterface(
670 video_track, video_track_stats.get());
671 video_track_stats->remote_source = true;
672 video_track_stats->detached = false;
673 if (video_receiver_info.frame_width > 0 &&
674 video_receiver_info.frame_height > 0) {
675 video_track_stats->frame_width = static_cast<uint32_t>(
676 video_receiver_info.frame_width);
677 video_track_stats->frame_height = static_cast<uint32_t>(
678 video_receiver_info.frame_height);
679 }
Guido Urdaneta67378412019-05-28 17:38:08 +0200680 video_track_stats->jitter_buffer_delay =
681 video_receiver_info.jitter_buffer_delay_seconds;
682 video_track_stats->jitter_buffer_emitted_count =
683 video_receiver_info.jitter_buffer_emitted_count;
hbos42f6d2f2017-01-20 03:56:50 -0800684 video_track_stats->frames_received = video_receiver_info.frames_received;
hbosf64941f2017-01-20 07:39:09 -0800685 // TODO(hbos): When we support receiving simulcast, this should be the total
686 // number of frames correctly decoded, independent of which SSRC it was
687 // received from. Since we don't support that, this is correct and is the same
Harald Alvestrand89061872018-01-02 14:08:34 +0100688 // value as "RTCInboundRTPStreamStats.framesDecoded". https://crbug.com/659137
hbosf64941f2017-01-20 07:39:09 -0800689 video_track_stats->frames_decoded = video_receiver_info.frames_decoded;
hbos50cfe1f2017-01-23 07:21:55 -0800690 RTC_DCHECK_GE(video_receiver_info.frames_received,
691 video_receiver_info.frames_rendered);
692 video_track_stats->frames_dropped = video_receiver_info.frames_received -
693 video_receiver_info.frames_rendered;
Sergey Silkin02371062019-01-31 16:45:42 +0100694 video_track_stats->freeze_count = video_receiver_info.freeze_count;
695 video_track_stats->pause_count = video_receiver_info.pause_count;
696 video_track_stats->total_freezes_duration =
697 static_cast<double>(video_receiver_info.total_freezes_duration_ms) /
698 rtc::kNumMillisecsPerSec;
699 video_track_stats->total_pauses_duration =
700 static_cast<double>(video_receiver_info.total_pauses_duration_ms) /
701 rtc::kNumMillisecsPerSec;
702 video_track_stats->total_frames_duration =
703 static_cast<double>(video_receiver_info.total_frames_duration_ms) /
704 rtc::kNumMillisecsPerSec;
705 video_track_stats->sum_squared_frame_durations =
706 video_receiver_info.sum_squared_frame_durations;
707
hbos9e302742017-01-20 02:47:10 -0800708 return video_track_stats;
709}
710
Harald Alvestrand89061872018-01-02 14:08:34 +0100711void ProduceSenderMediaTrackStats(
712 int64_t timestamp_us,
713 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800714 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders,
Harald Alvestrand89061872018-01-02 14:08:34 +0100715 RTCStatsReport* report) {
716 // This function iterates over the senders to generate outgoing track stats.
717
718 // TODO(hbos): Return stats of detached tracks. We have to perform stats
719 // gathering at the time of detachment to get accurate stats and timestamps.
720 // https://crbug.com/659137
Mirko Bonadei739baf02019-01-27 17:29:42 +0100721 for (const auto& sender : senders) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100722 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
723 AudioTrackInterface* track =
724 static_cast<AudioTrackInterface*>(sender->track().get());
725 if (!track)
726 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100727 cricket::VoiceSenderInfo null_sender_info;
728 const cricket::VoiceSenderInfo* voice_sender_info = &null_sender_info;
729 // TODO(hta): Checking on ssrc is not proper. There should be a way
730 // to see from a sender whether it's connected or not.
731 // Related to https://crbug.com/8694 (using ssrc 0 to indicate "none")
Steve Anton57858b32018-02-15 15:19:50 -0800732 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100733 // When pc.close is called, sender info is discarded, so
734 // we generate zeroes instead. Bug: It should be retained.
735 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800736 const cricket::VoiceSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100737 track_media_info_map.GetVoiceSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100738 if (sender_info) {
739 voice_sender_info = sender_info;
740 } else {
741 RTC_LOG(LS_INFO)
742 << "RTCStatsCollector: No voice sender info for sender with ssrc "
743 << sender->ssrc();
744 }
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100745 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100746 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100747 ProduceMediaStreamTrackStatsFromVoiceSenderInfo(
748 timestamp_us, *track, *voice_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100749 report->AddStats(std::move(audio_track_stats));
750 } else if (sender->media_type() == cricket::MEDIA_TYPE_VIDEO) {
751 VideoTrackInterface* track =
752 static_cast<VideoTrackInterface*>(sender->track().get());
753 if (!track)
754 continue;
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100755 cricket::VideoSenderInfo null_sender_info;
756 const cricket::VideoSenderInfo* video_sender_info = &null_sender_info;
757 // TODO(hta): Check on state not ssrc when state is available
Harald Alvestrand76d29522018-01-30 14:43:29 +0100758 // Related to https://bugs.webrtc.org/8694 (using ssrc 0 to indicate
759 // "none")
Steve Anton57858b32018-02-15 15:19:50 -0800760 if (sender->ssrc() != 0) {
Harald Alvestrand76d29522018-01-30 14:43:29 +0100761 // When pc.close is called, sender info is discarded, so
762 // we generate zeroes instead. Bug: It should be retained.
763 // https://crbug.com/807174
Steve Anton57858b32018-02-15 15:19:50 -0800764 const cricket::VideoSenderInfo* sender_info =
Harald Alvestrandb8e12012018-01-23 15:28:16 +0100765 track_media_info_map.GetVideoSenderInfoBySsrc(sender->ssrc());
Harald Alvestrand76d29522018-01-30 14:43:29 +0100766 if (sender_info) {
767 video_sender_info = sender_info;
768 } else {
769 RTC_LOG(LS_INFO) << "No video sender info for sender with ssrc "
770 << sender->ssrc();
771 }
772 }
Harald Alvestrand89061872018-01-02 14:08:34 +0100773 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
Harald Alvestrandc72af932018-01-11 17:18:19 +0100774 ProduceMediaStreamTrackStatsFromVideoSenderInfo(
775 timestamp_us, *track, *video_sender_info, sender->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100776 report->AddStats(std::move(video_track_stats));
777 } else {
778 RTC_NOTREACHED();
779 }
780 }
781}
782
783void ProduceReceiverMediaTrackStats(
784 int64_t timestamp_us,
785 const TrackMediaInfoMap& track_media_info_map,
Steve Anton57858b32018-02-15 15:19:50 -0800786 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers,
Harald Alvestrand89061872018-01-02 14:08:34 +0100787 RTCStatsReport* report) {
788 // This function iterates over the receivers to find the remote tracks.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100789 for (const auto& receiver : receivers) {
Harald Alvestrand89061872018-01-02 14:08:34 +0100790 if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
791 AudioTrackInterface* track =
792 static_cast<AudioTrackInterface*>(receiver->track().get());
793 const cricket::VoiceReceiverInfo* voice_receiver_info =
794 track_media_info_map.GetVoiceReceiverInfo(*track);
795 if (!voice_receiver_info) {
796 continue;
797 }
798 std::unique_ptr<RTCMediaStreamTrackStats> audio_track_stats =
799 ProduceMediaStreamTrackStatsFromVoiceReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100800 timestamp_us, *track, *voice_receiver_info,
801 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100802 report->AddStats(std::move(audio_track_stats));
803 } else if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
804 VideoTrackInterface* track =
805 static_cast<VideoTrackInterface*>(receiver->track().get());
806 const cricket::VideoReceiverInfo* video_receiver_info =
807 track_media_info_map.GetVideoReceiverInfo(*track);
808 if (!video_receiver_info) {
809 continue;
810 }
811 std::unique_ptr<RTCMediaStreamTrackStats> video_track_stats =
812 ProduceMediaStreamTrackStatsFromVideoReceiverInfo(
Harald Alvestrandc72af932018-01-11 17:18:19 +0100813 timestamp_us, *track, *video_receiver_info,
814 receiver->AttachmentId());
Harald Alvestrand89061872018-01-02 14:08:34 +0100815 report->AddStats(std::move(video_track_stats));
816 } else {
817 RTC_NOTREACHED();
818 }
819 }
820}
821
Henrik Boström5b3541f2018-03-19 13:52:56 +0100822rtc::scoped_refptr<RTCStatsReport> CreateReportFilteredBySelector(
823 bool filter_by_sender_selector,
824 rtc::scoped_refptr<const RTCStatsReport> report,
825 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
826 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector) {
827 std::vector<std::string> rtpstream_ids;
828 if (filter_by_sender_selector) {
829 // Filter mode: RTCStatsCollector::RequestInfo::kSenderSelector
830 if (sender_selector) {
831 // Find outbound-rtp(s) of the sender, i.e. the outbound-rtp(s) that
832 // reference the sender stats.
833 // Because we do not implement sender stats, we look at outbound-rtp(s)
834 // that reference the track attachment stats for the sender instead.
835 std::string track_id =
836 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
837 kSender, sender_selector->AttachmentId());
838 for (const auto& stats : *report) {
839 if (stats.type() != RTCOutboundRTPStreamStats::kType)
840 continue;
841 const auto& outbound_rtp = stats.cast_to<RTCOutboundRTPStreamStats>();
842 if (outbound_rtp.track_id.is_defined() &&
843 *outbound_rtp.track_id == track_id) {
844 rtpstream_ids.push_back(outbound_rtp.id());
845 }
846 }
847 }
848 } else {
849 // Filter mode: RTCStatsCollector::RequestInfo::kReceiverSelector
850 if (receiver_selector) {
851 // Find inbound-rtp(s) of the receiver, i.e. the inbound-rtp(s) that
852 // reference the receiver stats.
853 // Because we do not implement receiver stats, we look at inbound-rtp(s)
854 // that reference the track attachment stats for the receiver instead.
855 std::string track_id =
856 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
857 kReceiver, receiver_selector->AttachmentId());
858 for (const auto& stats : *report) {
859 if (stats.type() != RTCInboundRTPStreamStats::kType)
860 continue;
861 const auto& inbound_rtp = stats.cast_to<RTCInboundRTPStreamStats>();
862 if (inbound_rtp.track_id.is_defined() &&
863 *inbound_rtp.track_id == track_id) {
864 rtpstream_ids.push_back(inbound_rtp.id());
865 }
866 }
867 }
868 }
869 if (rtpstream_ids.empty())
870 return RTCStatsReport::Create(report->timestamp_us());
871 return TakeReferencedStats(report->Copy(), rtpstream_ids);
872}
873
hboscc555c52016-10-18 12:48:31 -0700874} // namespace
875
Henrik Boström5b3541f2018-03-19 13:52:56 +0100876RTCStatsCollector::RequestInfo::RequestInfo(
877 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
878 : RequestInfo(FilterMode::kAll, std::move(callback), nullptr, nullptr) {}
879
880RTCStatsCollector::RequestInfo::RequestInfo(
881 rtc::scoped_refptr<RtpSenderInternal> selector,
882 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
883 : RequestInfo(FilterMode::kSenderSelector,
884 std::move(callback),
885 std::move(selector),
886 nullptr) {}
887
888RTCStatsCollector::RequestInfo::RequestInfo(
889 rtc::scoped_refptr<RtpReceiverInternal> selector,
890 rtc::scoped_refptr<RTCStatsCollectorCallback> callback)
891 : RequestInfo(FilterMode::kReceiverSelector,
892 std::move(callback),
893 nullptr,
894 std::move(selector)) {}
895
896RTCStatsCollector::RequestInfo::RequestInfo(
897 RTCStatsCollector::RequestInfo::FilterMode filter_mode,
898 rtc::scoped_refptr<RTCStatsCollectorCallback> callback,
899 rtc::scoped_refptr<RtpSenderInternal> sender_selector,
900 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector)
901 : filter_mode_(filter_mode),
902 callback_(std::move(callback)),
903 sender_selector_(std::move(sender_selector)),
904 receiver_selector_(std::move(receiver_selector)) {
905 RTC_DCHECK(callback_);
906 RTC_DCHECK(!sender_selector_ || !receiver_selector_);
907}
908
hbosc82f2e12016-09-05 01:36:50 -0700909rtc::scoped_refptr<RTCStatsCollector> RTCStatsCollector::Create(
Steve Anton2d8609c2018-01-23 16:38:46 -0800910 PeerConnectionInternal* pc,
911 int64_t cache_lifetime_us) {
hbosc82f2e12016-09-05 01:36:50 -0700912 return rtc::scoped_refptr<RTCStatsCollector>(
913 new rtc::RefCountedObject<RTCStatsCollector>(pc, cache_lifetime_us));
914}
915
Steve Anton2d8609c2018-01-23 16:38:46 -0800916RTCStatsCollector::RTCStatsCollector(PeerConnectionInternal* pc,
hbosc82f2e12016-09-05 01:36:50 -0700917 int64_t cache_lifetime_us)
hbosd565b732016-08-30 14:04:35 -0700918 : pc_(pc),
Steve Anton978b8762017-09-29 12:15:02 -0700919 signaling_thread_(pc->signaling_thread()),
920 worker_thread_(pc->worker_thread()),
921 network_thread_(pc->network_thread()),
hbosc82f2e12016-09-05 01:36:50 -0700922 num_pending_partial_reports_(0),
923 partial_report_timestamp_us_(0),
Henrik Boström40b030e2019-02-28 09:49:31 +0100924 network_report_event_(true /* manual_reset */,
925 true /* initially_signaled */),
hbos0e6758d2016-08-31 07:57:36 -0700926 cache_timestamp_us_(0),
927 cache_lifetime_us_(cache_lifetime_us) {
hbosd565b732016-08-30 14:04:35 -0700928 RTC_DCHECK(pc_);
hbosc82f2e12016-09-05 01:36:50 -0700929 RTC_DCHECK(signaling_thread_);
930 RTC_DCHECK(worker_thread_);
931 RTC_DCHECK(network_thread_);
hbos0e6758d2016-08-31 07:57:36 -0700932 RTC_DCHECK_GE(cache_lifetime_us_, 0);
Steve Anton2d8609c2018-01-23 16:38:46 -0800933 pc_->SignalDataChannelCreated().connect(
hbos82ebe022016-11-14 01:41:09 -0800934 this, &RTCStatsCollector::OnDataChannelCreated);
hbosd565b732016-08-30 14:04:35 -0700935}
936
hbosb78306a2016-12-19 05:06:57 -0800937RTCStatsCollector::~RTCStatsCollector() {
938 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
939}
940
hbosc82f2e12016-09-05 01:36:50 -0700941void RTCStatsCollector::GetStatsReport(
942 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
Henrik Boström5b3541f2018-03-19 13:52:56 +0100943 GetStatsReportInternal(RequestInfo(std::move(callback)));
944}
945
946void RTCStatsCollector::GetStatsReport(
947 rtc::scoped_refptr<RtpSenderInternal> selector,
948 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
949 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
950}
951
952void RTCStatsCollector::GetStatsReport(
953 rtc::scoped_refptr<RtpReceiverInternal> selector,
954 rtc::scoped_refptr<RTCStatsCollectorCallback> callback) {
955 GetStatsReportInternal(RequestInfo(std::move(selector), std::move(callback)));
956}
957
958void RTCStatsCollector::GetStatsReportInternal(
959 RTCStatsCollector::RequestInfo request) {
hbosc82f2e12016-09-05 01:36:50 -0700960 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +0100961 requests_.push_back(std::move(request));
hbosc82f2e12016-09-05 01:36:50 -0700962
hbos0e6758d2016-08-31 07:57:36 -0700963 // "Now" using a monotonically increasing timer.
964 int64_t cache_now_us = rtc::TimeMicros();
965 if (cached_report_ &&
966 cache_now_us - cache_timestamp_us_ <= cache_lifetime_us_) {
Taylor Brandstetter25e022f2018-03-08 09:53:47 -0800967 // We have a fresh cached report to deliver. Deliver asynchronously, since
968 // the caller may not be expecting a synchronous callback, and it avoids
969 // reentrancy problems.
Henrik Boström5b3541f2018-03-19 13:52:56 +0100970 std::vector<RequestInfo> requests;
971 requests.swap(requests_);
Henrik Boström40b030e2019-02-28 09:49:31 +0100972 signaling_thread_->PostTask(
973 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::DeliverCachedReport, this,
974 cached_report_, std::move(requests)));
hbosc82f2e12016-09-05 01:36:50 -0700975 } else if (!num_pending_partial_reports_) {
976 // Only start gathering stats if we're not already gathering stats. In the
977 // case of already gathering stats, |callback_| will be invoked when there
978 // are no more pending partial reports.
979
980 // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970,
981 // UTC), in microseconds. The system clock could be modified and is not
982 // necessarily monotonically increasing.
nissecdf37a92016-09-13 23:41:47 -0700983 int64_t timestamp_us = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700984
hbosf415f8a2017-01-02 04:28:51 -0800985 num_pending_partial_reports_ = 2;
hbosc82f2e12016-09-05 01:36:50 -0700986 partial_report_timestamp_us_ = cache_now_us;
hbosdf6075a2016-12-19 04:58:02 -0800987
Steve Anton57858b32018-02-15 15:19:50 -0800988 // Prepare |transceiver_stats_infos_| for use in
hbos84abeb12017-01-16 06:16:44 -0800989 // |ProducePartialResultsOnNetworkThread| and
990 // |ProducePartialResultsOnSignalingThread|.
Steve Anton57858b32018-02-15 15:19:50 -0800991 transceiver_stats_infos_ = PrepareTransceiverStatsInfos_s();
Steve Anton7eca0932018-03-30 15:18:41 -0700992 // Prepare |transport_names_| for use in
993 // |ProducePartialResultsOnNetworkThread|.
994 transport_names_ = PrepareTransportNames_s();
Steve Anton5dfde182018-02-06 10:34:40 -0800995
stefanf79ade12017-06-02 06:44:03 -0700996 // Prepare |call_stats_| here since GetCallStats() will hop to the worker
997 // thread.
998 // TODO(holmer): To avoid the hop we could move BWE and BWE stats to the
999 // network thread, where it more naturally belongs.
Steve Anton978b8762017-09-29 12:15:02 -07001000 call_stats_ = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -07001001
Henrik Boström40b030e2019-02-28 09:49:31 +01001002 // Don't touch |network_report_| on the signaling thread until
1003 // ProducePartialResultsOnNetworkThread() has signaled the
1004 // |network_report_event_|.
1005 network_report_event_.Reset();
1006 network_thread_->PostTask(
1007 RTC_FROM_HERE,
hbosc82f2e12016-09-05 01:36:50 -07001008 rtc::Bind(&RTCStatsCollector::ProducePartialResultsOnNetworkThread,
Henrik Boström40b030e2019-02-28 09:49:31 +01001009 this, timestamp_us));
hbosf415f8a2017-01-02 04:28:51 -08001010 ProducePartialResultsOnSignalingThread(timestamp_us);
hbos0e6758d2016-08-31 07:57:36 -07001011 }
hbosd565b732016-08-30 14:04:35 -07001012}
1013
1014void RTCStatsCollector::ClearCachedStatsReport() {
hbosc82f2e12016-09-05 01:36:50 -07001015 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001016 cached_report_ = nullptr;
1017}
1018
hbosb78306a2016-12-19 05:06:57 -08001019void RTCStatsCollector::WaitForPendingRequest() {
1020 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +01001021 // If a request is pending, blocks until the |network_report_event_| is
1022 // signaled and then delivers the result. Otherwise this is a NO-OP.
1023 MergeNetworkReport_s();
hbosb78306a2016-12-19 05:06:57 -08001024}
1025
hbosc82f2e12016-09-05 01:36:50 -07001026void RTCStatsCollector::ProducePartialResultsOnSignalingThread(
1027 int64_t timestamp_us) {
1028 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström40b030e2019-02-28 09:49:31 +01001029 partial_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -07001030
Henrik Boström40b030e2019-02-28 09:49:31 +01001031 ProducePartialResultsOnSignalingThreadImpl(timestamp_us,
1032 partial_report_.get());
hbosc82f2e12016-09-05 01:36:50 -07001033
Henrik Boström40b030e2019-02-28 09:49:31 +01001034 // ProducePartialResultsOnSignalingThread() is running synchronously on the
1035 // signaling thread, so it is always the first partial result delivered on the
1036 // signaling thread. The request is not complete until MergeNetworkReport_s()
1037 // happens; we don't have to do anything here.
1038 RTC_DCHECK_GT(num_pending_partial_reports_, 1);
1039 --num_pending_partial_reports_;
1040}
1041
1042void RTCStatsCollector::ProducePartialResultsOnSignalingThreadImpl(
1043 int64_t timestamp_us,
1044 RTCStatsReport* partial_report) {
1045 RTC_DCHECK(signaling_thread_->IsCurrent());
1046 ProduceDataChannelStats_s(timestamp_us, partial_report);
1047 ProduceMediaStreamStats_s(timestamp_us, partial_report);
1048 ProduceMediaStreamTrackStats_s(timestamp_us, partial_report);
Henrik Boström646fda02019-05-22 15:49:42 +02001049 ProduceMediaSourceStats_s(timestamp_us, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001050 ProducePeerConnectionStats_s(timestamp_us, partial_report);
hbosc82f2e12016-09-05 01:36:50 -07001051}
1052
hbosc82f2e12016-09-05 01:36:50 -07001053void RTCStatsCollector::ProducePartialResultsOnNetworkThread(
1054 int64_t timestamp_us) {
1055 RTC_DCHECK(network_thread_->IsCurrent());
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +02001056 // Touching |network_report_| on this thread is safe by this method because
Henrik Boström40b030e2019-02-28 09:49:31 +01001057 // |network_report_event_| is reset before this method is invoked.
1058 network_report_ = RTCStatsReport::Create(timestamp_us);
hbosc82f2e12016-09-05 01:36:50 -07001059
Steve Anton5dfde182018-02-06 10:34:40 -08001060 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
Steve Anton7eca0932018-03-30 15:18:41 -07001061 pc_->GetTransportStatsByNames(transport_names_);
Steve Anton5dfde182018-02-06 10:34:40 -08001062 std::map<std::string, CertificateStatsPair> transport_cert_stats =
1063 PrepareTransportCertificateStats_n(transport_stats_by_name);
1064
Henrik Boström40b030e2019-02-28 09:49:31 +01001065 ProducePartialResultsOnNetworkThreadImpl(
1066 timestamp_us, transport_stats_by_name, transport_cert_stats,
1067 network_report_.get());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001068
Henrik Boström40b030e2019-02-28 09:49:31 +01001069 // Signal that it is now safe to touch |network_report_| on the signaling
1070 // thread, and post a task to merge it into the final results.
1071 network_report_event_.Set();
1072 signaling_thread_->PostTask(
1073 RTC_FROM_HERE, rtc::Bind(&RTCStatsCollector::MergeNetworkReport_s, this));
Henrik Boström05d43c62019-02-15 10:23:08 +01001074}
1075
Henrik Boström40b030e2019-02-28 09:49:31 +01001076void RTCStatsCollector::ProducePartialResultsOnNetworkThreadImpl(
1077 int64_t timestamp_us,
1078 const std::map<std::string, cricket::TransportStats>&
1079 transport_stats_by_name,
1080 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1081 RTCStatsReport* partial_report) {
1082 RTC_DCHECK(network_thread_->IsCurrent());
1083 ProduceCertificateStats_n(timestamp_us, transport_cert_stats, partial_report);
1084 ProduceCodecStats_n(timestamp_us, transceiver_stats_infos_, partial_report);
1085 ProduceIceCandidateAndPairStats_n(timestamp_us, transport_stats_by_name,
1086 call_stats_, partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001087 ProduceTransportStats_n(timestamp_us, transport_stats_by_name,
1088 transport_cert_stats, partial_report);
Henrik Boström883eefc2019-05-27 13:40:25 +02001089 ProduceRTPStreamStats_n(timestamp_us, transceiver_stats_infos_,
1090 partial_report);
Henrik Boström40b030e2019-02-28 09:49:31 +01001091}
1092
1093void RTCStatsCollector::MergeNetworkReport_s() {
1094 RTC_DCHECK(signaling_thread_->IsCurrent());
1095 // The |network_report_event_| must be signaled for it to be safe to touch
1096 // |network_report_|. This is normally not blocking, but if
1097 // WaitForPendingRequest() is called while a request is pending, we might have
1098 // to wait until the network thread is done touching |network_report_|.
1099 network_report_event_.Wait(rtc::Event::kForever);
1100 if (!network_report_) {
1101 // Normally, MergeNetworkReport_s() is executed because it is posted from
1102 // the network thread. But if WaitForPendingRequest() is called while a
1103 // request is pending, an early call to MergeNetworkReport_s() is made,
1104 // merging the report and setting |network_report_| to null. If so, when the
1105 // previously posted MergeNetworkReport_s() is later executed, the report is
1106 // already null and nothing needs to be done here.
hbosc82f2e12016-09-05 01:36:50 -07001107 return;
1108 }
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001109 RTC_DCHECK_GT(num_pending_partial_reports_, 0);
Henrik Boström40b030e2019-02-28 09:49:31 +01001110 RTC_DCHECK(partial_report_);
1111 partial_report_->TakeMembersFrom(network_report_);
1112 network_report_ = nullptr;
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001113 --num_pending_partial_reports_;
Henrik Boström40b030e2019-02-28 09:49:31 +01001114 // |network_report_| is currently the only partial report collected
1115 // asynchronously, so |num_pending_partial_reports_| must now be 0 and we are
1116 // ready to deliver the result.
1117 RTC_DCHECK_EQ(num_pending_partial_reports_, 0);
1118 cache_timestamp_us_ = partial_report_timestamp_us_;
1119 cached_report_ = partial_report_;
1120 partial_report_ = nullptr;
1121 transceiver_stats_infos_.clear();
1122 // Trace WebRTC Stats when getStats is called on Javascript.
1123 // This allows access to WebRTC stats from trace logs. To enable them,
1124 // select the "webrtc_stats" category when recording traces.
1125 TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
1126 cached_report_->ToJson());
Mirko Bonadeica890ee2019-02-15 21:10:40 +00001127
Henrik Boström40b030e2019-02-28 09:49:31 +01001128 // Deliver report and clear |requests_|.
1129 std::vector<RequestInfo> requests;
1130 requests.swap(requests_);
1131 DeliverCachedReport(cached_report_, std::move(requests));
hbosc82f2e12016-09-05 01:36:50 -07001132}
1133
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001134void RTCStatsCollector::DeliverCachedReport(
1135 rtc::scoped_refptr<const RTCStatsReport> cached_report,
Henrik Boström5b3541f2018-03-19 13:52:56 +01001136 std::vector<RTCStatsCollector::RequestInfo> requests) {
hbosc82f2e12016-09-05 01:36:50 -07001137 RTC_DCHECK(signaling_thread_->IsCurrent());
Henrik Boström5b3541f2018-03-19 13:52:56 +01001138 RTC_DCHECK(!requests.empty());
Taylor Brandstetter25e022f2018-03-08 09:53:47 -08001139 RTC_DCHECK(cached_report);
Taylor Brandstetter87d5a742018-03-06 09:42:25 -08001140
Henrik Boström5b3541f2018-03-19 13:52:56 +01001141 for (const RequestInfo& request : requests) {
1142 if (request.filter_mode() == RequestInfo::FilterMode::kAll) {
1143 request.callback()->OnStatsDelivered(cached_report);
1144 } else {
1145 bool filter_by_sender_selector;
1146 rtc::scoped_refptr<RtpSenderInternal> sender_selector;
1147 rtc::scoped_refptr<RtpReceiverInternal> receiver_selector;
1148 if (request.filter_mode() == RequestInfo::FilterMode::kSenderSelector) {
1149 filter_by_sender_selector = true;
1150 sender_selector = request.sender_selector();
1151 } else {
1152 RTC_DCHECK(request.filter_mode() ==
1153 RequestInfo::FilterMode::kReceiverSelector);
1154 filter_by_sender_selector = false;
1155 receiver_selector = request.receiver_selector();
1156 }
1157 request.callback()->OnStatsDelivered(CreateReportFilteredBySelector(
1158 filter_by_sender_selector, cached_report, sender_selector,
1159 receiver_selector));
1160 }
hbosc82f2e12016-09-05 01:36:50 -07001161 }
hbosd565b732016-08-30 14:04:35 -07001162}
1163
hbosdf6075a2016-12-19 04:58:02 -08001164void RTCStatsCollector::ProduceCertificateStats_n(
hbos2fa7c672016-10-24 04:00:05 -07001165 int64_t timestamp_us,
1166 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
hbos6ab97ce2016-10-03 14:16:56 -07001167 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001168 RTC_DCHECK(network_thread_->IsCurrent());
hbos02ba2112016-10-28 05:14:53 -07001169 for (const auto& transport_cert_stats_pair : transport_cert_stats) {
1170 if (transport_cert_stats_pair.second.local) {
1171 ProduceCertificateStatsFromSSLCertificateStats(
1172 timestamp_us, *transport_cert_stats_pair.second.local.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001173 }
hbos02ba2112016-10-28 05:14:53 -07001174 if (transport_cert_stats_pair.second.remote) {
1175 ProduceCertificateStatsFromSSLCertificateStats(
1176 timestamp_us, *transport_cert_stats_pair.second.remote.get(), report);
hbos6ab97ce2016-10-03 14:16:56 -07001177 }
1178 }
1179}
1180
hbosdf6075a2016-12-19 04:58:02 -08001181void RTCStatsCollector::ProduceCodecStats_n(
Steve Anton57858b32018-02-15 15:19:50 -08001182 int64_t timestamp_us,
1183 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos0adb8282016-11-23 02:32:06 -08001184 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001185 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001186 for (const auto& stats : transceiver_stats_infos) {
1187 if (!stats.mid) {
1188 continue;
hbos0adb8282016-11-23 02:32:06 -08001189 }
Steve Anton57858b32018-02-15 15:19:50 -08001190 const cricket::VoiceMediaInfo* voice_media_info =
1191 stats.track_media_info_map->voice_media_info();
1192 const cricket::VideoMediaInfo* video_media_info =
1193 stats.track_media_info_map->video_media_info();
1194 // Audio
1195 if (voice_media_info) {
1196 // Inbound
1197 for (const auto& pair : voice_media_info->receive_codecs) {
1198 report->AddStats(CodecStatsFromRtpCodecParameters(
1199 timestamp_us, *stats.mid, true, pair.second));
1200 }
1201 // Outbound
1202 for (const auto& pair : voice_media_info->send_codecs) {
1203 report->AddStats(CodecStatsFromRtpCodecParameters(
1204 timestamp_us, *stats.mid, false, pair.second));
1205 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001206 }
Steve Anton57858b32018-02-15 15:19:50 -08001207 // Video
1208 if (video_media_info) {
1209 // Inbound
1210 for (const auto& pair : video_media_info->receive_codecs) {
1211 report->AddStats(CodecStatsFromRtpCodecParameters(
1212 timestamp_us, *stats.mid, true, pair.second));
1213 }
1214 // Outbound
1215 for (const auto& pair : video_media_info->send_codecs) {
1216 report->AddStats(CodecStatsFromRtpCodecParameters(
1217 timestamp_us, *stats.mid, false, pair.second));
1218 }
hbos0adb8282016-11-23 02:32:06 -08001219 }
1220 }
1221}
1222
hboscc555c52016-10-18 12:48:31 -07001223void RTCStatsCollector::ProduceDataChannelStats_s(
1224 int64_t timestamp_us, RTCStatsReport* report) const {
1225 RTC_DCHECK(signaling_thread_->IsCurrent());
1226 for (const rtc::scoped_refptr<DataChannel>& data_channel :
1227 pc_->sctp_data_channels()) {
1228 std::unique_ptr<RTCDataChannelStats> data_channel_stats(
1229 new RTCDataChannelStats(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001230 "RTCDataChannel_" + rtc::ToString(data_channel->id()),
hboscc555c52016-10-18 12:48:31 -07001231 timestamp_us));
1232 data_channel_stats->label = data_channel->label();
1233 data_channel_stats->protocol = data_channel->protocol();
1234 data_channel_stats->datachannelid = data_channel->id();
1235 data_channel_stats->state =
1236 DataStateToRTCDataChannelState(data_channel->state());
1237 data_channel_stats->messages_sent = data_channel->messages_sent();
1238 data_channel_stats->bytes_sent = data_channel->bytes_sent();
1239 data_channel_stats->messages_received = data_channel->messages_received();
1240 data_channel_stats->bytes_received = data_channel->bytes_received();
1241 report->AddStats(std::move(data_channel_stats));
1242 }
1243}
1244
hbosdf6075a2016-12-19 04:58:02 -08001245void RTCStatsCollector::ProduceIceCandidateAndPairStats_n(
stefanf79ade12017-06-02 06:44:03 -07001246 int64_t timestamp_us,
Steve Anton5dfde182018-02-06 10:34:40 -08001247 const std::map<std::string, cricket::TransportStats>&
1248 transport_stats_by_name,
stefanf79ade12017-06-02 06:44:03 -07001249 const Call::Stats& call_stats,
1250 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001251 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001252 for (const auto& entry : transport_stats_by_name) {
1253 const std::string& transport_name = entry.first;
1254 const cricket::TransportStats& transport_stats = entry.second;
1255 for (const auto& channel_stats : transport_stats.channel_stats) {
hbos0583b282016-11-30 01:50:14 -08001256 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001257 transport_name, channel_stats.component);
hbosc47a0c32016-10-11 14:54:49 -07001258 for (const cricket::ConnectionInfo& info :
1259 channel_stats.connection_infos) {
hbosc47a0c32016-10-11 14:54:49 -07001260 std::unique_ptr<RTCIceCandidatePairStats> candidate_pair_stats(
hbos2fa7c672016-10-24 04:00:05 -07001261 new RTCIceCandidatePairStats(
1262 RTCIceCandidatePairStatsIDFromConnectionInfo(info),
1263 timestamp_us));
hbosc47a0c32016-10-11 14:54:49 -07001264
hbos0583b282016-11-30 01:50:14 -08001265 candidate_pair_stats->transport_id = transport_id;
hbosab9f6e42016-10-07 02:18:47 -07001266 // TODO(hbos): There could be other candidates that are not paired with
1267 // anything. We don't have a complete list. Local candidates come from
1268 // Port objects, and prflx candidates (both local and remote) are only
Harald Alvestrand89061872018-01-02 14:08:34 +01001269 // stored in candidate pairs. https://crbug.com/632723
hbos02ba2112016-10-28 05:14:53 -07001270 candidate_pair_stats->local_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001271 timestamp_us, info.local_candidate, true, transport_id, report);
hbos02ba2112016-10-28 05:14:53 -07001272 candidate_pair_stats->remote_candidate_id = ProduceIceCandidateStats(
hbosb4e426e2017-01-02 09:59:31 -08001273 timestamp_us, info.remote_candidate, false, transport_id, report);
hbos06495bc2017-01-02 08:08:18 -08001274 candidate_pair_stats->state =
1275 IceCandidatePairStateToRTCStatsIceCandidatePairState(info.state);
1276 candidate_pair_stats->priority = info.priority;
hbos92eaec62017-02-27 01:38:08 -08001277 candidate_pair_stats->nominated = info.nominated;
hbosc47a0c32016-10-11 14:54:49 -07001278 // TODO(hbos): This writable is different than the spec. It goes to
1279 // false after a certain amount of time without a response passes.
Harald Alvestrand89061872018-01-02 14:08:34 +01001280 // https://crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -07001281 candidate_pair_stats->writable = info.writable;
hbosc47a0c32016-10-11 14:54:49 -07001282 candidate_pair_stats->bytes_sent =
1283 static_cast<uint64_t>(info.sent_total_bytes);
1284 candidate_pair_stats->bytes_received =
1285 static_cast<uint64_t>(info.recv_total_bytes);
hbosbf8d3e52017-02-28 06:34:47 -08001286 candidate_pair_stats->total_round_trip_time =
1287 static_cast<double>(info.total_round_trip_time_ms) /
1288 rtc::kNumMillisecsPerSec;
1289 if (info.current_round_trip_time_ms) {
1290 candidate_pair_stats->current_round_trip_time =
1291 static_cast<double>(*info.current_round_trip_time_ms) /
1292 rtc::kNumMillisecsPerSec;
1293 }
stefanf79ade12017-06-02 06:44:03 -07001294 if (info.best_connection) {
hbos338f78a2017-02-07 06:41:21 -08001295 // The bandwidth estimations we have are for the selected candidate
1296 // pair ("info.best_connection").
stefanf79ade12017-06-02 06:44:03 -07001297 RTC_DCHECK_GE(call_stats.send_bandwidth_bps, 0);
1298 RTC_DCHECK_GE(call_stats.recv_bandwidth_bps, 0);
1299 if (call_stats.send_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001300 candidate_pair_stats->available_outgoing_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001301 static_cast<double>(call_stats.send_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001302 }
stefanf79ade12017-06-02 06:44:03 -07001303 if (call_stats.recv_bandwidth_bps > 0) {
hbos338f78a2017-02-07 06:41:21 -08001304 candidate_pair_stats->available_incoming_bitrate =
stefanf79ade12017-06-02 06:44:03 -07001305 static_cast<double>(call_stats.recv_bandwidth_bps);
hbos338f78a2017-02-07 06:41:21 -08001306 }
1307 }
hbosd82f5122016-12-09 04:12:39 -08001308 candidate_pair_stats->requests_received =
1309 static_cast<uint64_t>(info.recv_ping_requests);
hbose448dd52016-12-12 01:22:53 -08001310 candidate_pair_stats->requests_sent = static_cast<uint64_t>(
1311 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001312 candidate_pair_stats->responses_received =
1313 static_cast<uint64_t>(info.recv_ping_responses);
1314 candidate_pair_stats->responses_sent =
1315 static_cast<uint64_t>(info.sent_ping_responses);
hbose448dd52016-12-12 01:22:53 -08001316 RTC_DCHECK_GE(info.sent_ping_requests_total,
1317 info.sent_ping_requests_before_first_response);
1318 candidate_pair_stats->consent_requests_sent = static_cast<uint64_t>(
1319 info.sent_ping_requests_total -
1320 info.sent_ping_requests_before_first_response);
hbosc47a0c32016-10-11 14:54:49 -07001321
1322 report->AddStats(std::move(candidate_pair_stats));
hbosab9f6e42016-10-07 02:18:47 -07001323 }
1324 }
1325 }
1326}
1327
Steve Anton57858b32018-02-15 15:19:50 -08001328void RTCStatsCollector::ProduceMediaStreamStats_s(
1329 int64_t timestamp_us,
1330 RTCStatsReport* report) const {
hbos09bc1282016-11-08 06:29:22 -08001331 RTC_DCHECK(signaling_thread_->IsCurrent());
Steve Anton57858b32018-02-15 15:19:50 -08001332
1333 std::map<std::string, std::vector<std::string>> track_ids;
1334
1335 for (const auto& stats : transceiver_stats_infos_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +01001336 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001337 std::string track_id =
1338 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1339 kSender, sender->internal()->AttachmentId());
1340 for (auto& stream_id : sender->stream_ids()) {
1341 track_ids[stream_id].push_back(track_id);
1342 }
1343 }
Mirko Bonadei739baf02019-01-27 17:29:42 +01001344 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001345 std::string track_id =
1346 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1347 kReceiver, receiver->internal()->AttachmentId());
1348 for (auto& stream : receiver->streams()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -07001349 track_ids[stream->id()].push_back(track_id);
Steve Anton57858b32018-02-15 15:19:50 -08001350 }
1351 }
1352 }
1353
1354 // Build stats for each stream ID known.
1355 for (auto& it : track_ids) {
1356 std::unique_ptr<RTCMediaStreamStats> stream_stats(
1357 new RTCMediaStreamStats("RTCMediaStream_" + it.first, timestamp_us));
1358 stream_stats->stream_identifier = it.first;
1359 stream_stats->track_ids = it.second;
1360 report->AddStats(std::move(stream_stats));
1361 }
1362}
1363
1364void RTCStatsCollector::ProduceMediaStreamTrackStats_s(
1365 int64_t timestamp_us,
1366 RTCStatsReport* report) const {
1367 RTC_DCHECK(signaling_thread_->IsCurrent());
1368 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos_) {
1369 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001370 for (const auto& sender : stats.transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001371 senders.push_back(sender->internal());
1372 }
1373 ProduceSenderMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1374 senders, report);
1375
1376 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001377 for (const auto& receiver : stats.transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001378 receivers.push_back(receiver->internal());
1379 }
1380 ProduceReceiverMediaTrackStats(timestamp_us, *stats.track_media_info_map,
1381 receivers, report);
1382 }
hbos09bc1282016-11-08 06:29:22 -08001383}
1384
Henrik Boström646fda02019-05-22 15:49:42 +02001385void RTCStatsCollector::ProduceMediaSourceStats_s(
1386 int64_t timestamp_us,
1387 RTCStatsReport* report) const {
1388 RTC_DCHECK(signaling_thread_->IsCurrent());
1389 for (const RtpTransceiverStatsInfo& transceiver_stats_info :
1390 transceiver_stats_infos_) {
1391 const auto& track_media_info_map =
1392 transceiver_stats_info.track_media_info_map;
1393 for (const auto& sender : transceiver_stats_info.transceiver->senders()) {
1394 const auto& sender_internal = sender->internal();
1395 const auto& track = sender_internal->track();
1396 if (!track)
1397 continue;
1398 // TODO(hbos): The same track could be attached to multiple senders which
1399 // should result in multiple senders referencing the same media source
1400 // stats. When all media source related metrics are moved to the track's
1401 // source (e.g. input frame rate is moved from cricket::VideoSenderInfo to
1402 // VideoTrackSourceInterface::Stats), don't create separate media source
1403 // stats objects on a per-attachment basis.
1404 std::unique_ptr<RTCMediaSourceStats> media_source_stats;
1405 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
1406 media_source_stats = absl::make_unique<RTCAudioSourceStats>(
1407 RTCMediaSourceStatsIDFromKindAndAttachment(
1408 cricket::MEDIA_TYPE_AUDIO, sender_internal->AttachmentId()),
1409 timestamp_us);
1410 } else {
1411 RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind());
1412 auto video_source_stats = absl::make_unique<RTCVideoSourceStats>(
1413 RTCMediaSourceStatsIDFromKindAndAttachment(
1414 cricket::MEDIA_TYPE_VIDEO, sender_internal->AttachmentId()),
1415 timestamp_us);
1416 auto* video_track = static_cast<VideoTrackInterface*>(track.get());
1417 auto* video_source = video_track->GetSource();
1418 VideoTrackSourceInterface::Stats source_stats;
1419 if (video_source && video_source->GetStats(&source_stats)) {
1420 video_source_stats->width = source_stats.input_width;
1421 video_source_stats->height = source_stats.input_height;
1422 }
1423 // TODO(hbos): Source stats should not depend on whether or not we are
1424 // connected/have an SSRC assigned. Related to
1425 // https://crbug.com/webrtc/8694 (using ssrc 0 to indicate "none").
1426 if (sender_internal->ssrc() != 0) {
1427 auto* sender_info = track_media_info_map->GetVideoSenderInfoBySsrc(
1428 sender_internal->ssrc());
1429 if (sender_info) {
1430 video_source_stats->frames_per_second =
1431 sender_info->framerate_input;
1432 }
1433 }
1434 media_source_stats = std::move(video_source_stats);
1435 }
1436 media_source_stats->track_identifier = track->id();
1437 media_source_stats->kind = track->kind();
1438 report->AddStats(std::move(media_source_stats));
1439 }
1440 }
1441}
1442
hbos6ab97ce2016-10-03 14:16:56 -07001443void RTCStatsCollector::ProducePeerConnectionStats_s(
1444 int64_t timestamp_us, RTCStatsReport* report) const {
hbosc82f2e12016-09-05 01:36:50 -07001445 RTC_DCHECK(signaling_thread_->IsCurrent());
hbosd565b732016-08-30 14:04:35 -07001446 std::unique_ptr<RTCPeerConnectionStats> stats(
hbos0e6758d2016-08-31 07:57:36 -07001447 new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us));
hbos82ebe022016-11-14 01:41:09 -08001448 stats->data_channels_opened = internal_record_.data_channels_opened;
1449 stats->data_channels_closed = internal_record_.data_channels_closed;
hbos6ab97ce2016-10-03 14:16:56 -07001450 report->AddStats(std::move(stats));
hbosd565b732016-08-30 14:04:35 -07001451}
1452
hbosdf6075a2016-12-19 04:58:02 -08001453void RTCStatsCollector::ProduceRTPStreamStats_n(
Steve Anton593e3252017-12-15 11:44:48 -08001454 int64_t timestamp_us,
Steve Anton57858b32018-02-15 15:19:50 -08001455 const std::vector<RtpTransceiverStatsInfo>& transceiver_stats_infos,
hbos84abeb12017-01-16 06:16:44 -08001456 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001457 RTC_DCHECK(network_thread_->IsCurrent());
hbos6ded1902016-11-01 01:50:46 -07001458
Steve Anton57858b32018-02-15 15:19:50 -08001459 for (const RtpTransceiverStatsInfo& stats : transceiver_stats_infos) {
1460 if (stats.media_type == cricket::MEDIA_TYPE_AUDIO) {
1461 ProduceAudioRTPStreamStats_n(timestamp_us, stats, report);
1462 } else if (stats.media_type == cricket::MEDIA_TYPE_VIDEO) {
1463 ProduceVideoRTPStreamStats_n(timestamp_us, stats, report);
1464 } else {
1465 RTC_NOTREACHED();
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001466 }
Steve Anton56bae8d2018-02-14 16:07:42 -08001467 }
Steve Anton57858b32018-02-15 15:19:50 -08001468}
1469
1470void RTCStatsCollector::ProduceAudioRTPStreamStats_n(
1471 int64_t timestamp_us,
1472 const RtpTransceiverStatsInfo& stats,
1473 RTCStatsReport* report) const {
1474 if (!stats.mid || !stats.transport_name) {
1475 return;
1476 }
1477 RTC_DCHECK(stats.track_media_info_map);
1478 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1479 RTC_DCHECK(track_media_info_map.voice_media_info());
1480 std::string mid = *stats.mid;
1481 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1482 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1483 // Inbound
1484 for (const cricket::VoiceReceiverInfo& voice_receiver_info :
1485 track_media_info_map.voice_media_info()->receivers) {
1486 if (!voice_receiver_info.connected())
1487 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001488 auto inbound_audio = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001489 RTCInboundRTPStreamStatsIDFromSSRC(true, voice_receiver_info.ssrc()),
1490 timestamp_us);
1491 SetInboundRTPStreamStatsFromVoiceReceiverInfo(mid, voice_receiver_info,
1492 inbound_audio.get());
1493 // TODO(hta): This lookup should look for the sender, not the track.
1494 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1495 track_media_info_map.GetAudioTrack(voice_receiver_info);
1496 if (audio_track) {
1497 inbound_audio->track_id =
1498 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1499 kReceiver,
1500 track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
hbos6ded1902016-11-01 01:50:46 -07001501 }
Steve Anton57858b32018-02-15 15:19:50 -08001502 inbound_audio->transport_id = transport_id;
1503 report->AddStats(std::move(inbound_audio));
1504 }
1505 // Outbound
1506 for (const cricket::VoiceSenderInfo& voice_sender_info :
1507 track_media_info_map.voice_media_info()->senders) {
1508 if (!voice_sender_info.connected())
1509 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001510 auto outbound_audio = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001511 RTCOutboundRTPStreamStatsIDFromSSRC(true, voice_sender_info.ssrc()),
1512 timestamp_us);
1513 SetOutboundRTPStreamStatsFromVoiceSenderInfo(mid, voice_sender_info,
1514 outbound_audio.get());
1515 rtc::scoped_refptr<AudioTrackInterface> audio_track =
1516 track_media_info_map.GetAudioTrack(voice_sender_info);
1517 if (audio_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001518 int attachment_id =
1519 track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001520 outbound_audio->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001521 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1522 attachment_id);
1523 outbound_audio->media_source_id =
1524 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_AUDIO,
1525 attachment_id);
Steve Anton56bae8d2018-02-14 16:07:42 -08001526 }
Steve Anton57858b32018-02-15 15:19:50 -08001527 outbound_audio->transport_id = transport_id;
1528 report->AddStats(std::move(outbound_audio));
1529 }
Henrik Boström883eefc2019-05-27 13:40:25 +02001530 // Remote-inbound
1531 // These are Report Block-based, information sent from the remote endpoint,
1532 // providing metrics about our Outbound streams. We take advantage of the fact
1533 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1534 // been added to the report.
1535 for (const cricket::VoiceSenderInfo& voice_sender_info :
1536 track_media_info_map.voice_media_info()->senders) {
1537 for (const auto& report_block_data : voice_sender_info.report_block_datas) {
1538 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
1539 report_block_data, cricket::MEDIA_TYPE_AUDIO, *report));
1540 }
1541 }
Steve Anton57858b32018-02-15 15:19:50 -08001542}
1543
1544void RTCStatsCollector::ProduceVideoRTPStreamStats_n(
1545 int64_t timestamp_us,
1546 const RtpTransceiverStatsInfo& stats,
1547 RTCStatsReport* report) const {
1548 if (!stats.mid || !stats.transport_name) {
1549 return;
1550 }
1551 RTC_DCHECK(stats.track_media_info_map);
1552 const TrackMediaInfoMap& track_media_info_map = *stats.track_media_info_map;
1553 RTC_DCHECK(track_media_info_map.video_media_info());
1554 std::string mid = *stats.mid;
1555 std::string transport_id = RTCTransportStatsIDFromTransportChannel(
1556 *stats.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
1557 // Inbound
1558 for (const cricket::VideoReceiverInfo& video_receiver_info :
1559 track_media_info_map.video_media_info()->receivers) {
1560 if (!video_receiver_info.connected())
1561 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001562 auto inbound_video = absl::make_unique<RTCInboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001563 RTCInboundRTPStreamStatsIDFromSSRC(false, video_receiver_info.ssrc()),
1564 timestamp_us);
1565 SetInboundRTPStreamStatsFromVideoReceiverInfo(mid, video_receiver_info,
1566 inbound_video.get());
1567 rtc::scoped_refptr<VideoTrackInterface> video_track =
1568 track_media_info_map.GetVideoTrack(video_receiver_info);
1569 if (video_track) {
1570 inbound_video->track_id =
1571 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
1572 kReceiver,
1573 track_media_info_map.GetAttachmentIdByTrack(video_track).value());
1574 }
1575 inbound_video->transport_id = transport_id;
1576 report->AddStats(std::move(inbound_video));
1577 }
1578 // Outbound
1579 for (const cricket::VideoSenderInfo& video_sender_info :
1580 track_media_info_map.video_media_info()->senders) {
1581 if (!video_sender_info.connected())
1582 continue;
Karl Wiberg918f50c2018-07-05 11:40:33 +02001583 auto outbound_video = absl::make_unique<RTCOutboundRTPStreamStats>(
Steve Anton57858b32018-02-15 15:19:50 -08001584 RTCOutboundRTPStreamStatsIDFromSSRC(false, video_sender_info.ssrc()),
1585 timestamp_us);
1586 SetOutboundRTPStreamStatsFromVideoSenderInfo(mid, video_sender_info,
1587 outbound_video.get());
1588 rtc::scoped_refptr<VideoTrackInterface> video_track =
1589 track_media_info_map.GetVideoTrack(video_sender_info);
1590 if (video_track) {
Henrik Boström646fda02019-05-22 15:49:42 +02001591 int attachment_id =
1592 track_media_info_map.GetAttachmentIdByTrack(video_track).value();
Steve Anton57858b32018-02-15 15:19:50 -08001593 outbound_video->track_id =
Henrik Boström646fda02019-05-22 15:49:42 +02001594 RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
1595 attachment_id);
1596 outbound_video->media_source_id =
1597 RTCMediaSourceStatsIDFromKindAndAttachment(cricket::MEDIA_TYPE_VIDEO,
1598 attachment_id);
Steve Anton57858b32018-02-15 15:19:50 -08001599 }
1600 outbound_video->transport_id = transport_id;
1601 report->AddStats(std::move(outbound_video));
hbos6ded1902016-11-01 01:50:46 -07001602 }
Henrik Boström883eefc2019-05-27 13:40:25 +02001603 // Remote-inbound
1604 // These are Report Block-based, information sent from the remote endpoint,
1605 // providing metrics about our Outbound streams. We take advantage of the fact
1606 // that RTCOutboundRtpStreamStats, RTCCodecStats and RTCTransport have already
1607 // been added to the report.
1608 for (const cricket::VideoSenderInfo& video_sender_info :
1609 track_media_info_map.video_media_info()->senders) {
1610 for (const auto& report_block_data : video_sender_info.report_block_datas) {
1611 report->AddStats(ProduceRemoteInboundRtpStreamStatsFromReportBlockData(
1612 report_block_data, cricket::MEDIA_TYPE_VIDEO, *report));
1613 }
1614 }
hbos6ded1902016-11-01 01:50:46 -07001615}
1616
hbosdf6075a2016-12-19 04:58:02 -08001617void RTCStatsCollector::ProduceTransportStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001618 int64_t timestamp_us,
1619 const std::map<std::string, cricket::TransportStats>&
1620 transport_stats_by_name,
hbos2fa7c672016-10-24 04:00:05 -07001621 const std::map<std::string, CertificateStatsPair>& transport_cert_stats,
1622 RTCStatsReport* report) const {
hbosdf6075a2016-12-19 04:58:02 -08001623 RTC_DCHECK(network_thread_->IsCurrent());
Steve Anton5dfde182018-02-06 10:34:40 -08001624 for (const auto& entry : transport_stats_by_name) {
1625 const std::string& transport_name = entry.first;
1626 const cricket::TransportStats& transport_stats = entry.second;
1627
hbos2fa7c672016-10-24 04:00:05 -07001628 // Get reference to RTCP channel, if it exists.
1629 std::string rtcp_transport_stats_id;
Steve Anton5dfde182018-02-06 10:34:40 -08001630 for (const cricket::TransportChannelStats& channel_stats :
1631 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001632 if (channel_stats.component ==
1633 cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
1634 rtcp_transport_stats_id = RTCTransportStatsIDFromTransportChannel(
Steve Anton5dfde182018-02-06 10:34:40 -08001635 transport_name, channel_stats.component);
hbos2fa7c672016-10-24 04:00:05 -07001636 break;
1637 }
1638 }
1639
1640 // Get reference to local and remote certificates of this transport, if they
1641 // exist.
Steve Anton5dfde182018-02-06 10:34:40 -08001642 const auto& certificate_stats_it =
1643 transport_cert_stats.find(transport_name);
hbos2fa7c672016-10-24 04:00:05 -07001644 RTC_DCHECK(certificate_stats_it != transport_cert_stats.cend());
1645 std::string local_certificate_id;
1646 if (certificate_stats_it->second.local) {
1647 local_certificate_id = RTCCertificateIDFromFingerprint(
1648 certificate_stats_it->second.local->fingerprint);
1649 }
1650 std::string remote_certificate_id;
1651 if (certificate_stats_it->second.remote) {
1652 remote_certificate_id = RTCCertificateIDFromFingerprint(
1653 certificate_stats_it->second.remote->fingerprint);
1654 }
1655
1656 // There is one transport stats for each channel.
Steve Anton5dfde182018-02-06 10:34:40 -08001657 for (const cricket::TransportChannelStats& channel_stats :
1658 transport_stats.channel_stats) {
hbos2fa7c672016-10-24 04:00:05 -07001659 std::unique_ptr<RTCTransportStats> transport_stats(
Steve Anton5dfde182018-02-06 10:34:40 -08001660 new RTCTransportStats(RTCTransportStatsIDFromTransportChannel(
1661 transport_name, channel_stats.component),
1662 timestamp_us));
hbos2fa7c672016-10-24 04:00:05 -07001663 transport_stats->bytes_sent = 0;
1664 transport_stats->bytes_received = 0;
hbos7064d592017-01-16 07:38:02 -08001665 transport_stats->dtls_state = DtlsTransportStateToRTCDtlsTransportState(
1666 channel_stats.dtls_state);
hbos2fa7c672016-10-24 04:00:05 -07001667 for (const cricket::ConnectionInfo& info :
1668 channel_stats.connection_infos) {
1669 *transport_stats->bytes_sent += info.sent_total_bytes;
1670 *transport_stats->bytes_received += info.recv_total_bytes;
1671 if (info.best_connection) {
hbos2fa7c672016-10-24 04:00:05 -07001672 transport_stats->selected_candidate_pair_id =
1673 RTCIceCandidatePairStatsIDFromConnectionInfo(info);
1674 }
1675 }
1676 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
1677 !rtcp_transport_stats_id.empty()) {
1678 transport_stats->rtcp_transport_stats_id = rtcp_transport_stats_id;
1679 }
1680 if (!local_certificate_id.empty())
1681 transport_stats->local_certificate_id = local_certificate_id;
1682 if (!remote_certificate_id.empty())
1683 transport_stats->remote_certificate_id = remote_certificate_id;
1684 report->AddStats(std::move(transport_stats));
1685 }
1686 }
1687}
1688
1689std::map<std::string, RTCStatsCollector::CertificateStatsPair>
hbosdf6075a2016-12-19 04:58:02 -08001690RTCStatsCollector::PrepareTransportCertificateStats_n(
Steve Anton5dfde182018-02-06 10:34:40 -08001691 const std::map<std::string, cricket::TransportStats>&
1692 transport_stats_by_name) const {
hbosdf6075a2016-12-19 04:58:02 -08001693 RTC_DCHECK(network_thread_->IsCurrent());
hbos2fa7c672016-10-24 04:00:05 -07001694 std::map<std::string, CertificateStatsPair> transport_cert_stats;
Steve Anton5dfde182018-02-06 10:34:40 -08001695 for (const auto& entry : transport_stats_by_name) {
1696 const std::string& transport_name = entry.first;
1697
hbos2fa7c672016-10-24 04:00:05 -07001698 CertificateStatsPair certificate_stats_pair;
1699 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate;
Steve Anton5dfde182018-02-06 10:34:40 -08001700 if (pc_->GetLocalCertificate(transport_name, &local_certificate)) {
hbos2fa7c672016-10-24 04:00:05 -07001701 certificate_stats_pair.local =
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001702 local_certificate->GetSSLCertificateChain().GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001703 }
Steve Anton5dfde182018-02-06 10:34:40 -08001704
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001705 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
1706 pc_->GetRemoteSSLCertChain(transport_name);
1707 if (remote_cert_chain) {
1708 certificate_stats_pair.remote = remote_cert_chain->GetStats();
hbos2fa7c672016-10-24 04:00:05 -07001709 }
Steve Anton5dfde182018-02-06 10:34:40 -08001710
hbos2fa7c672016-10-24 04:00:05 -07001711 transport_cert_stats.insert(
Steve Anton5dfde182018-02-06 10:34:40 -08001712 std::make_pair(transport_name, std::move(certificate_stats_pair)));
hbos2fa7c672016-10-24 04:00:05 -07001713 }
1714 return transport_cert_stats;
1715}
1716
Steve Anton57858b32018-02-15 15:19:50 -08001717std::vector<RTCStatsCollector::RtpTransceiverStatsInfo>
1718RTCStatsCollector::PrepareTransceiverStatsInfos_s() const {
1719 std::vector<RtpTransceiverStatsInfo> transceiver_stats_infos;
Steve Anton56bae8d2018-02-14 16:07:42 -08001720
Steve Anton57858b32018-02-15 15:19:50 -08001721 // These are used to invoke GetStats for all the media channels together in
1722 // one worker thread hop.
1723 std::map<cricket::VoiceMediaChannel*,
1724 std::unique_ptr<cricket::VoiceMediaInfo>>
1725 voice_stats;
1726 std::map<cricket::VideoMediaChannel*,
1727 std::unique_ptr<cricket::VideoMediaInfo>>
1728 video_stats;
1729
Mirko Bonadei739baf02019-01-27 17:29:42 +01001730 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Anton57858b32018-02-15 15:19:50 -08001731 cricket::MediaType media_type = transceiver->media_type();
1732
1733 // Prepare stats entry. The TrackMediaInfoMap will be filled in after the
1734 // stats have been fetched on the worker thread.
1735 transceiver_stats_infos.emplace_back();
1736 RtpTransceiverStatsInfo& stats = transceiver_stats_infos.back();
1737 stats.transceiver = transceiver->internal();
1738 stats.media_type = media_type;
1739
Amit Hilbuchdd9390c2018-11-13 16:26:05 -08001740 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Steve Anton57858b32018-02-15 15:19:50 -08001741 if (!channel) {
1742 // The remaining fields require a BaseChannel.
1743 continue;
1744 }
1745
1746 stats.mid = channel->content_name();
1747 stats.transport_name = channel->transport_name();
1748
1749 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1750 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
1751 RTC_DCHECK(voice_stats.find(voice_channel->media_channel()) ==
1752 voice_stats.end());
1753 voice_stats[voice_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001754 absl::make_unique<cricket::VoiceMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001755 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1756 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
1757 RTC_DCHECK(video_stats.find(video_channel->media_channel()) ==
1758 video_stats.end());
1759 video_stats[video_channel->media_channel()] =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001760 absl::make_unique<cricket::VideoMediaInfo>();
Steve Anton57858b32018-02-15 15:19:50 -08001761 } else {
1762 RTC_NOTREACHED();
1763 }
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001764 }
Steve Anton57858b32018-02-15 15:19:50 -08001765
1766 // Call GetStats for all media channels together on the worker thread in one
1767 // hop.
1768 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
1769 for (const auto& entry : voice_stats) {
1770 if (!entry.first->GetStats(entry.second.get())) {
1771 RTC_LOG(LS_WARNING) << "Failed to get voice stats.";
1772 }
1773 }
1774 for (const auto& entry : video_stats) {
1775 if (!entry.first->GetStats(entry.second.get())) {
1776 RTC_LOG(LS_WARNING) << "Failed to get video stats.";
1777 }
1778 }
1779 });
1780
1781 // Create the TrackMediaInfoMap for each transceiver stats object.
1782 for (auto& stats : transceiver_stats_infos) {
1783 auto transceiver = stats.transceiver;
1784 std::unique_ptr<cricket::VoiceMediaInfo> voice_media_info;
1785 std::unique_ptr<cricket::VideoMediaInfo> video_media_info;
1786 if (transceiver->channel()) {
1787 cricket::MediaType media_type = transceiver->media_type();
1788 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
1789 auto* voice_channel =
1790 static_cast<cricket::VoiceChannel*>(transceiver->channel());
1791 RTC_DCHECK(voice_stats[voice_channel->media_channel()]);
1792 voice_media_info =
1793 std::move(voice_stats[voice_channel->media_channel()]);
1794 } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
1795 auto* video_channel =
1796 static_cast<cricket::VideoChannel*>(transceiver->channel());
1797 RTC_DCHECK(video_stats[video_channel->media_channel()]);
1798 video_media_info =
1799 std::move(video_stats[video_channel->media_channel()]);
1800 }
1801 }
1802 std::vector<rtc::scoped_refptr<RtpSenderInternal>> senders;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001803 for (const auto& sender : transceiver->senders()) {
Steve Anton57858b32018-02-15 15:19:50 -08001804 senders.push_back(sender->internal());
1805 }
1806 std::vector<rtc::scoped_refptr<RtpReceiverInternal>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001807 for (const auto& receiver : transceiver->receivers()) {
Steve Anton57858b32018-02-15 15:19:50 -08001808 receivers.push_back(receiver->internal());
1809 }
Karl Wiberg918f50c2018-07-05 11:40:33 +02001810 stats.track_media_info_map = absl::make_unique<TrackMediaInfoMap>(
Steve Anton57858b32018-02-15 15:19:50 -08001811 std::move(voice_media_info), std::move(video_media_info), senders,
1812 receivers);
Guido Urdanetaee2388f2018-02-15 16:36:19 +00001813 }
Steve Anton57858b32018-02-15 15:19:50 -08001814
1815 return transceiver_stats_infos;
hbos0adb8282016-11-23 02:32:06 -08001816}
1817
Steve Anton7eca0932018-03-30 15:18:41 -07001818std::set<std::string> RTCStatsCollector::PrepareTransportNames_s() const {
1819 std::set<std::string> transport_names;
1820 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
1821 if (transceiver->internal()->channel()) {
1822 transport_names.insert(
1823 transceiver->internal()->channel()->transport_name());
1824 }
1825 }
1826 if (pc_->rtp_data_channel()) {
1827 transport_names.insert(pc_->rtp_data_channel()->transport_name());
1828 }
1829 if (pc_->sctp_transport_name()) {
1830 transport_names.insert(*pc_->sctp_transport_name());
1831 }
1832 return transport_names;
1833}
1834
hbos82ebe022016-11-14 01:41:09 -08001835void RTCStatsCollector::OnDataChannelCreated(DataChannel* channel) {
1836 channel->SignalOpened.connect(this, &RTCStatsCollector::OnDataChannelOpened);
1837 channel->SignalClosed.connect(this, &RTCStatsCollector::OnDataChannelClosed);
1838}
1839
1840void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) {
1841 RTC_DCHECK(signaling_thread_->IsCurrent());
1842 bool result = internal_record_.opened_data_channels.insert(
1843 reinterpret_cast<uintptr_t>(channel)).second;
1844 ++internal_record_.data_channels_opened;
1845 RTC_DCHECK(result);
1846}
1847
1848void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) {
1849 RTC_DCHECK(signaling_thread_->IsCurrent());
1850 // Only channels that have been fully opened (and have increased the
1851 // |data_channels_opened_| counter) increase the closed counter.
hbos5bf9def2017-03-20 03:14:14 -07001852 if (internal_record_.opened_data_channels.erase(
1853 reinterpret_cast<uintptr_t>(channel))) {
hbos82ebe022016-11-14 01:41:09 -08001854 ++internal_record_.data_channels_closed;
1855 }
1856}
1857
hboscc555c52016-10-18 12:48:31 -07001858const char* CandidateTypeToRTCIceCandidateTypeForTesting(
1859 const std::string& type) {
1860 return CandidateTypeToRTCIceCandidateType(type);
1861}
1862
1863const char* DataStateToRTCDataChannelStateForTesting(
1864 DataChannelInterface::DataState state) {
1865 return DataStateToRTCDataChannelState(state);
1866}
1867
hbosd565b732016-08-30 14:04:35 -07001868} // namespace webrtc