blob: c08633b48ee0447ee53be2c35a70dd73ed08d814 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/statscollector.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
Steve Anton5dfde182018-02-06 10:34:40 -080014#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <utility>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "pc/channel.h"
19#include "pc/peerconnection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Artem Titova76af0c2018-07-23 17:38:12 +020021#include "rtc_base/third_party/base64/base64.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000026// The following is the enum RTCStatsIceCandidateType from
27// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
28// our stats report for ice candidate type could conform to that.
29const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
30const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
31const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
32const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
33
34// Strings used by the stats collector to report adapter types. This fits the
35// general stype of http://w3c.github.io/webrtc-stats than what
36// AdapterTypeToString does.
37const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
38const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
39const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
40const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000041const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
Qingsi Wang01560de2018-07-26 10:44:02 -070042const char* STATSREPORT_ADAPTER_TYPE_WILDCARD = "wildcard";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000043
Yves Gerey665174f2018-06-19 15:03:05 +020044template <typename ValueType>
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000046 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000047 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000048};
49
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000050typedef TypeForAdd<bool> BoolForAdd;
51typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020052typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000053typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000054
jbauchbe24c942015-06-22 15:06:43 -070055StatsReport* AddTrackReport(StatsCollection* reports,
56 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000057 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000058 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000059 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000060 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000061 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070062 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000063}
64
Harald Alvestrand75ceef22018-01-04 15:26:13 +010065template <class Track>
66void CreateTrackReport(const Track* track,
67 StatsCollection* reports,
68 TrackIdMap* track_ids) {
69 const std::string& track_id = track->id();
70 StatsReport* report = AddTrackReport(reports, track_id);
71 RTC_DCHECK(report != nullptr);
72 (*track_ids)[track_id] = report;
73}
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075template <class TrackVector>
Steve Anton36b29d12017-10-30 09:57:42 -070076void CreateTrackReports(const TrackVector& tracks,
77 StatsCollection* reports,
78 TrackIdMap* track_ids) {
jbauchbe24c942015-06-22 15:06:43 -070079 for (const auto& track : tracks) {
Harald Alvestrand75ceef22018-01-04 15:26:13 +010080 CreateTrackReport(track.get(), reports, track_ids);
jbauchbe24c942015-06-22 15:06:43 -070081 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082}
83
tommi@webrtc.org92f40182015-03-04 15:25:19 +000084void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
85 StatsReport* report) {
86 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
87 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070088 if (info.rtt_ms >= 0) {
89 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
90 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000091}
92
pbosf42376c2015-08-28 07:35:32 -070093void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
94 StatsReport* report) {
95 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
96}
97
Ivo Creusen56d46092017-11-24 17:29:59 +010098void SetAudioProcessingStats(StatsReport* report,
99 bool typing_noise_detected,
100 const AudioProcessingStats& apm_stats) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000101 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
102 typing_noise_detected);
Ivo Creusen56d46092017-11-24 17:29:59 +0100103 if (apm_stats.delay_median_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100104 report->AddInt(StatsReport::kStatsValueNameEchoDelayMedian,
Ivo Creusen56d46092017-11-24 17:29:59 +0100105 *apm_stats.delay_median_ms);
Ivo Creusenae026092017-11-20 13:07:16 +0100106 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100107 if (apm_stats.delay_standard_deviation_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100108 report->AddInt(StatsReport::kStatsValueNameEchoDelayStdDev,
Ivo Creusen56d46092017-11-24 17:29:59 +0100109 *apm_stats.delay_standard_deviation_ms);
zhihuang6ba3b192016-05-13 11:46:35 -0700110 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100111 if (apm_stats.echo_return_loss) {
Ivo Creusenae026092017-11-20 13:07:16 +0100112 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss,
Ivo Creusen56d46092017-11-24 17:29:59 +0100113 *apm_stats.echo_return_loss);
henrik.lundin04a057b2017-01-16 23:53:59 -0800114 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100115 if (apm_stats.echo_return_loss_enhancement) {
Ivo Creusenae026092017-11-20 13:07:16 +0100116 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
Ivo Creusen56d46092017-11-24 17:29:59 +0100117 *apm_stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +0100118 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100119 if (apm_stats.residual_echo_likelihood) {
Ivo Creusenae026092017-11-20 13:07:16 +0100120 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
Ivo Creusen56d46092017-11-24 17:29:59 +0100121 static_cast<float>(*apm_stats.residual_echo_likelihood));
Ivo Creusenae026092017-11-20 13:07:16 +0100122 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100123 if (apm_stats.residual_echo_likelihood_recent_max) {
ivoc4e477a12017-01-15 08:29:46 -0800124 report->AddFloat(
125 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
Ivo Creusen56d46092017-11-24 17:29:59 +0100126 static_cast<float>(*apm_stats.residual_echo_likelihood_recent_max));
127 }
128 if (apm_stats.divergent_filter_fraction) {
129 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
130 static_cast<float>(*apm_stats.divergent_filter_fraction));
ivoc8c63a822016-10-21 04:10:03 -0700131 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000132}
133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700135 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000136 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200137 {StatsReport::kStatsValueNameExpandRate, info.expand_rate},
138 {StatsReport::kStatsValueNameSecondaryDecodedRate,
139 info.secondary_decoded_rate},
140 {StatsReport::kStatsValueNameSecondaryDiscardedRate,
141 info.secondary_discarded_rate},
142 {StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate},
143 {StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate},
144 {StatsReport::kStatsValueNamePreemptiveExpandRate,
145 info.preemptive_expand_rate},
146 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy},
147 {StatsReport::kStatsValueNameTotalSamplesDuration,
148 info.total_output_duration}};
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000149
150 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200151 {StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms},
152 {StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng},
153 {StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq},
154 {StatsReport::kStatsValueNameDecodingCTSG,
155 info.decoding_calls_to_silence_generator},
156 {StatsReport::kStatsValueNameDecodingMutedOutput,
157 info.decoding_muted_output},
158 {StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal},
159 {StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc},
160 {StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng},
161 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
162 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
163 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
164 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
165 {StatsReport::kStatsValueNamePreferredJitterBufferMs,
166 info.jitter_buffer_preferred_ms},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000167 };
168
169 for (const auto& f : floats)
170 report->AddFloat(f.name, f.value);
171
172 for (const auto& i : ints)
173 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700174 if (info.audio_level >= 0) {
175 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
176 info.audio_level);
177 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000178
Yves Gerey665174f2018-06-19 15:03:05 +0200179 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700180 if (info.capture_start_ntp_time_ms >= 0) {
181 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
182 info.capture_start_ntp_time_ms);
183 }
fippobec70ab2016-01-28 01:27:15 -0800184 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185}
186
187void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000188 ExtractCommonSendProperties(info, report);
189
Ivo Creusen56d46092017-11-24 17:29:59 +0100190 SetAudioProcessingStats(report, info.typing_noise_detected,
191 info.apm_statistics);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000192
zsteine76bd3a2017-07-14 12:17:49 -0700193 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200194 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy},
195 {StatsReport::kStatsValueNameTotalSamplesDuration,
196 info.total_input_duration}};
zsteine76bd3a2017-07-14 12:17:49 -0700197
andrew2fe1cb02015-11-27 17:27:35 -0800198 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000199 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200200 {StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
201 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
202 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
203 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000204 };
205
zsteine76bd3a2017-07-14 12:17:49 -0700206 for (const auto& f : floats) {
207 report->AddFloat(f.name, f.value);
208 }
209
zhihuang6ba3b192016-05-13 11:46:35 -0700210 for (const auto& i : ints) {
211 if (i.value >= 0) {
212 report->AddInt(i.name, i.value);
213 }
214 }
fippobec70ab2016-01-28 01:27:15 -0800215 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700216 if (info.ana_statistics.bitrate_action_counter) {
217 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
218 *info.ana_statistics.bitrate_action_counter);
219 }
220 if (info.ana_statistics.channel_action_counter) {
221 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
222 *info.ana_statistics.channel_action_counter);
223 }
224 if (info.ana_statistics.dtx_action_counter) {
225 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
226 *info.ana_statistics.dtx_action_counter);
227 }
228 if (info.ana_statistics.fec_action_counter) {
229 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
230 *info.ana_statistics.fec_action_counter);
231 }
ivoc0d0b9122017-09-08 13:24:21 -0700232 if (info.ana_statistics.frame_length_increase_counter) {
233 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
234 *info.ana_statistics.frame_length_increase_counter);
235 }
236 if (info.ana_statistics.frame_length_decrease_counter) {
237 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
238 *info.ana_statistics.frame_length_decrease_counter);
239 }
240 if (info.ana_statistics.uplink_packet_loss_fraction) {
241 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
242 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700243 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244}
245
246void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700247 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100248 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
249 info.decoder_implementation_name);
Yves Gerey665174f2018-06-19 15:03:05 +0200250 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700251 if (info.capture_start_ntp_time_ms >= 0) {
252 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
253 info.capture_start_ntp_time_ms);
254 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800255 if (info.first_frame_received_to_decoded_ms >= 0) {
256 report->AddInt64(StatsReport::kStatsValueNameFirstFrameReceivedToDecodedMs,
257 info.first_frame_received_to_decoded_ms);
258 }
sakalcc452e12017-02-09 04:53:45 -0800259 if (info.qp_sum)
260 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
261
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200263 {StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms},
264 {StatsReport::kStatsValueNameDecodeMs, info.decode_ms},
265 {StatsReport::kStatsValueNameFirsSent, info.firs_sent},
266 {StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height},
267 {StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded},
268 {StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output},
269 {StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd},
270 {StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width},
271 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
272 {StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms},
273 {StatsReport::kStatsValueNameMinPlayoutDelayMs,
274 info.min_playout_delay_ms},
275 {StatsReport::kStatsValueNameNacksSent, info.nacks_sent},
276 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
277 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
278 {StatsReport::kStatsValueNamePlisSent, info.plis_sent},
279 {StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms},
280 {StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms},
281 {StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded},
Artem Titov8a3ab0e2018-07-27 14:52:57 +0000282 };
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000283
284 for (const auto& i : ints)
285 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800286 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700287
ilnik2edc6842017-07-06 03:06:50 -0700288 if (info.timing_frame_info) {
289 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
290 info.timing_frame_info->ToString());
291 }
ilnikf04afde2017-07-07 01:26:24 -0700292
ilnika79cc282017-08-23 05:24:10 -0700293 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
294 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700295
296 report->AddString(
297 StatsReport::kStatsValueNameContentType,
298 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299}
300
301void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000302 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
Peter Boströmb7d9a972015-12-18 16:01:11 +0100304 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
305 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000306 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
307 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000308 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
309 (info.adapt_reason & 0x1) > 0);
Ă…sa Perssonc3ed6302017-11-16 14:04:52 +0100310 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
311 info.has_entered_low_resolution);
312
sakal87da4042016-10-31 06:53:47 -0700313 if (info.qp_sum)
314 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000315
316 const IntForAdd ints[] = {
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100317 {StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes},
318 {StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms},
319 {StatsReport::kStatsValueNameEncodeUsagePercent,
320 info.encode_usage_percent},
321 {StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd},
322 {StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height},
323 {StatsReport::kStatsValueNameFrameRateInput, info.framerate_input},
324 {StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent},
325 {StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width},
326 {StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd},
327 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
328 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
329 {StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd},
330 {StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded},
331 {StatsReport::kStatsValueNameHugeFramesSent, info.huge_frames_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000332 };
333
334 for (const auto& i : ints)
335 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800336 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700337 report->AddString(
338 StatsReport::kStatsValueNameContentType,
339 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340}
341
342void ExtractStats(const cricket::BandwidthEstimationInfo& info,
343 double stats_gathering_started,
344 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700345 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000347 report->set_timestamp(stats_gathering_started);
348 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200349 {StatsReport::kStatsValueNameAvailableSendBandwidth,
350 info.available_send_bandwidth},
351 {StatsReport::kStatsValueNameAvailableReceiveBandwidth,
352 info.available_recv_bandwidth},
353 {StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate},
354 {StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate},
355 {StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate},
356 {StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000357 };
358 for (const auto& i : ints)
359 report->AddInt(i.name, i.value);
360 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361}
362
wu@webrtc.org97077a32013-10-25 21:18:33 +0000363void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
364 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000365 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000366 // TODO(hta): Extract some stats here.
367}
368
369void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
370 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000371 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000372 // TODO(hta): Extract some stats here.
373}
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000376// In order to use the template, the functions that are called from it,
377// ExtractStats and ExtractRemoteStats, must be defined and overloaded
378// for each type.
Yves Gerey665174f2018-06-19 15:03:05 +0200379template <typename T>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000381 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000382 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000383 StatsReport::Direction direction) {
384 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000386 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000387 // TODO(hta): Handle the case of multiple SSRCs per object.
Yves Gerey665174f2018-06-19 15:03:05 +0200388 StatsReport* report =
389 collector->PrepareReport(true, ssrc, transport_id, direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000390 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000391 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000392
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000393 if (!d.remote_stats.empty()) {
394 report = collector->PrepareReport(false, ssrc, transport_id, direction);
395 if (report)
396 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000397 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000399}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400
401} // namespace
402
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000403const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
404 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
405 return STATSREPORT_LOCAL_PORT_TYPE;
406 }
407 if (candidate_type == cricket::STUN_PORT_TYPE) {
408 return STATSREPORT_STUN_PORT_TYPE;
409 }
410 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
411 return STATSREPORT_PRFLX_PORT_TYPE;
412 }
413 if (candidate_type == cricket::RELAY_PORT_TYPE) {
414 return STATSREPORT_RELAY_PORT_TYPE;
415 }
nisseeb4ca4e2017-01-12 02:24:27 -0800416 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000417 return "unknown";
418}
419
420const char* AdapterTypeToStatsType(rtc::AdapterType type) {
421 switch (type) {
422 case rtc::ADAPTER_TYPE_UNKNOWN:
423 return "unknown";
424 case rtc::ADAPTER_TYPE_ETHERNET:
425 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
426 case rtc::ADAPTER_TYPE_WIFI:
427 return STATSREPORT_ADAPTER_TYPE_WIFI;
428 case rtc::ADAPTER_TYPE_CELLULAR:
429 return STATSREPORT_ADAPTER_TYPE_WWAN;
430 case rtc::ADAPTER_TYPE_VPN:
431 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000432 case rtc::ADAPTER_TYPE_LOOPBACK:
433 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
Qingsi Wang01560de2018-07-26 10:44:02 -0700434 case rtc::ADAPTER_TYPE_ANY:
435 return STATSREPORT_ADAPTER_TYPE_WILDCARD;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000436 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800437 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000438 return "";
439 }
440}
441
Steve Anton2d8609c2018-01-23 16:38:46 -0800442StatsCollector::StatsCollector(PeerConnectionInternal* pc)
deadbeefab9b2d12015-10-14 11:33:11 -0700443 : pc_(pc), stats_gathering_started_(0) {
444 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000445}
446
447StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700448 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
nissecdf37a92016-09-13 23:41:47 -0700451// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000452double StatsCollector::GetTimeNow() {
Minyue Li656d6092018-08-10 15:38:52 +0200453 return static_cast<double>(rtc::TimeUTCMillis());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000454}
455
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456// Adds a MediaStream with tracks that can be used as a |selector| in a call
457// to GetStats.
458void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700459 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700460 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
Steve Anton36b29d12017-10-30 09:57:42 -0700462 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
463 &track_ids_);
464 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
465 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466}
467
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100468void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
469 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
470 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
471 &track_ids_);
472 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
473 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
474 &track_ids_);
475 } else {
476 RTC_NOTREACHED() << "Illegal track kind";
477 }
478}
479
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000480void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200481 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700482 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700483 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700484#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000485 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700486 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000487#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000488
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000489 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000490
491 // Create the kStatsReportTypeTrack report for the new track if there is no
492 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000493 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
494 audio_track->id()));
495 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000496 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000497 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000498 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000499 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000500}
501
502void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200503 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700504 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700505 local_audio_tracks_.erase(
506 std::remove_if(
507 local_audio_tracks_.begin(), local_audio_tracks_.end(),
508 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
509 return track.first == audio_track && track.second == ssrc;
510 }),
511 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000512}
513
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000514void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700516 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700517 RTC_DCHECK(reports != NULL);
518 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000520 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
521
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000523 reports->reserve(reports_.size());
524 for (auto* r : reports_)
525 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000526 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 }
528
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000529 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700530 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000531 if (report)
532 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
Yves Gerey665174f2018-06-19 15:03:05 +0200534 report = reports_.Find(
535 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000537 if (!report)
538 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000540 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
542 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000543 for (const auto* r : reports_) {
544 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000546
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000547 const StatsReport::Value* v =
548 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000549 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000550 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552}
553
Yves Gerey665174f2018-06-19 15:03:05 +0200554void StatsCollector::UpdateStats(
555 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700556 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 double time_now = GetTimeNow();
558 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
559 // ms apart will be ignored.
560 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000561 if (stats_gathering_started_ != 0 &&
562 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 return;
564 }
565 stats_gathering_started_ = time_now;
566
Steve Anton75737c02017-11-06 10:37:17 -0800567 // TODO(tommi): All of these hop over to the worker thread to fetch
568 // information. We could use an AsyncInvoker to run all of these and post
569 // the information back to the signaling thread where we can create and
570 // update stats reports. That would also clean up the threading story a bit
571 // since we'd be creating/updating the stats report objects consistently on
572 // the same thread (this class has no locks right now).
573 ExtractSessionInfo();
574 ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800575 ExtractMediaInfo();
Steve Anton75737c02017-11-06 10:37:17 -0800576 ExtractSenderInfo();
577 ExtractDataInfo();
578 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579}
580
Yves Gerey665174f2018-06-19 15:03:05 +0200581StatsReport* StatsCollector::PrepareReport(bool local,
582 uint32_t ssrc,
583 const StatsReport::Id& transport_id,
584 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700585 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000586 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200587 local ? StatsReport::kStatsReportTypeSsrc
588 : StatsReport::kStatsReportTypeRemoteSsrc,
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200589 rtc::ToString(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000590 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591
xians@webrtc.org01bda202014-07-09 07:38:38 +0000592 // Use the ID of the track that is currently mapped to the SSRC, if any.
Steve Antona41959e2018-11-28 11:15:33 -0800593 absl::string_view track_id = GetTrackIdBySsrc(ssrc, direction);
594 if (track_id.empty()) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700595 // The SSRC is not used by any existing track (or lookup failed since the
596 // SSRC wasn't signaled in SDP). Try copying the track ID from a previous
597 // report: if one exists.
598 if (report) {
599 const StatsReport::Value* v =
600 report->FindValue(StatsReport::kStatsValueNameTrackId);
Steve Antona41959e2018-11-28 11:15:33 -0800601 if (v) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700602 track_id = v->string_val();
Steve Antona41959e2018-11-28 11:15:33 -0800603 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606
Steve Antona41959e2018-11-28 11:15:33 -0800607 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000608 report = reports_.InsertNew(id);
Steve Antona41959e2018-11-28 11:15:33 -0800609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000611 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000612 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000614 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700615 if (!track_id.empty()) {
Steve Antona41959e2018-11-28 11:15:33 -0800616 report->AddString(StatsReport::kStatsValueNameTrackId,
617 std::string(track_id));
Taylor Brandstettera4653442018-06-19 09:44:26 -0700618 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000620 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 return report;
622}
623
zhihuange9e94c32016-11-04 11:38:15 -0700624bool StatsCollector::IsValidTrack(const std::string& track_id) {
625 return reports_.Find(StatsReport::NewTypedId(
626 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
627}
628
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000629StatsReport* StatsCollector::AddCertificateReports(
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800630 std::unique_ptr<rtc::SSLCertificateStats> cert_stats) {
Steve Anton978b8762017-09-29 12:15:02 -0700631 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000632
hbose29352b2016-08-25 03:52:38 -0700633 StatsReport* first_report = nullptr;
634 StatsReport* prev_report = nullptr;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800635 for (rtc::SSLCertificateStats* stats = cert_stats.get(); stats;
hbose29352b2016-08-25 03:52:38 -0700636 stats = stats->issuer.get()) {
637 StatsReport::Id id(StatsReport::NewTypedId(
638 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
639
640 StatsReport* report = reports_.ReplaceOrAddNew(id);
641 report->set_timestamp(stats_gathering_started_);
642 report->AddString(StatsReport::kStatsValueNameFingerprint,
643 stats->fingerprint);
644 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
645 stats->fingerprint_algorithm);
646 report->AddString(StatsReport::kStatsValueNameDer,
647 stats->base64_certificate);
648 if (!first_report)
649 first_report = report;
650 else
651 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
652 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000653 }
hbose29352b2016-08-25 03:52:38 -0700654 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000655}
656
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000657StatsReport* StatsCollector::AddConnectionInfoReport(
Yves Gerey665174f2018-06-19 15:03:05 +0200658 const std::string& content_name,
659 int component,
660 int connection_id,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000661 const StatsReport::Id& channel_report_id,
662 const cricket::ConnectionInfo& info) {
Yves Gerey665174f2018-06-19 15:03:05 +0200663 StatsReport::Id id(
664 StatsReport::NewCandidatePairId(content_name, component, connection_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000665 StatsReport* report = reports_.ReplaceOrAddNew(id);
666 report->set_timestamp(stats_gathering_started_);
667
668 const BoolForAdd bools[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200669 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
670 {StatsReport::kStatsValueNameReceiving, info.receiving},
671 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672 };
673 for (const auto& b : bools)
674 report->AddBoolean(b.name, b.value);
675
676 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
Qingsi Wang72a43a12018-02-20 16:03:18 -0800677 cricket::CandidateStats local_candidate_stats(info.local_candidate);
678 cricket::CandidateStats remote_candidate_stats(info.remote_candidate);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000679 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800680 AddCandidateReport(local_candidate_stats, true)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000681 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800682 AddCandidateReport(remote_candidate_stats, false)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000683
684 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700685 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
686 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
687 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
688 {StatsReport::kStatsValueNameRtt, info.rtt},
689 {StatsReport::kStatsValueNameSendPacketsDiscarded,
690 info.sent_discarded_packets},
691 {StatsReport::kStatsValueNameSentPingRequestsTotal,
692 info.sent_ping_requests_total},
693 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
694 info.sent_ping_requests_before_first_response},
695 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
696 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
697 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000698 };
699 for (const auto& i : int64s)
700 report->AddInt64(i.name, i.value);
701
702 report->AddString(StatsReport::kStatsValueNameLocalAddress,
703 info.local_candidate.address().ToString());
704 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
705 info.local_candidate.type());
706 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
707 info.remote_candidate.address().ToString());
708 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
709 info.remote_candidate.type());
710 report->AddString(StatsReport::kStatsValueNameTransportType,
711 info.local_candidate.protocol());
712
713 return report;
714}
715
716StatsReport* StatsCollector::AddCandidateReport(
Qingsi Wang72a43a12018-02-20 16:03:18 -0800717 const cricket::CandidateStats& candidate_stats,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000718 bool local) {
Qingsi Wang72a43a12018-02-20 16:03:18 -0800719 const auto& candidate = candidate_stats.candidate;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000720 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
721 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000722 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000723 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000724 report->set_timestamp(stats_gathering_started_);
725 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000726 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
727 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000728 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000729 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
730 candidate.address().ipaddr().ToString());
731 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
732 candidate.address().PortAsString());
733 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
734 candidate.priority());
735 report->AddString(StatsReport::kStatsValueNameCandidateType,
736 IceCandidateTypeToStatsType(candidate.type()));
737 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
738 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000739 }
740
Qingsi Wang4ff54432018-03-01 18:25:20 -0800741 if (local && candidate_stats.stun_stats.has_value()) {
742 const auto& stun_stats = candidate_stats.stun_stats.value();
743 report->AddInt64(StatsReport::kStatsValueNameSentStunKeepaliveRequests,
744 stun_stats.stun_binding_requests_sent);
745 report->AddInt64(StatsReport::kStatsValueNameRecvStunKeepaliveResponses,
746 stun_stats.stun_binding_responses_received);
747 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttTotal,
748 stun_stats.stun_binding_rtt_ms_total);
749 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttSquaredTotal,
750 stun_stats.stun_binding_rtt_ms_squared_total);
751 }
752
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000753 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000754}
755
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700757 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000758
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000760 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700761 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000762 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000763 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000764 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700765 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766
Qingsi Wang72a43a12018-02-20 16:03:18 -0800767 cricket::CandidateStatsList pooled_candidate_stats_list =
768 pc_->GetPooledCandidateStats();
769
770 for (const cricket::CandidateStats& stats : pooled_candidate_stats_list) {
771 AddCandidateReport(stats, true);
772 }
773
Steve Anton5dfde182018-02-06 10:34:40 -0800774 std::set<std::string> transport_names;
775 for (const auto& entry : pc_->GetTransportNamesByMid()) {
776 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000777 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778
Steve Anton5dfde182018-02-06 10:34:40 -0800779 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
780 pc_->GetTransportStatsByNames(transport_names);
781
782 for (const auto& entry : transport_stats_by_name) {
783 const std::string& transport_name = entry.first;
784 const cricket::TransportStats& transport_stats = entry.second;
785
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000786 // Attempt to get a copy of the certificates from the transport and
787 // expose them in stats reports. All channels in a transport share the
788 // same local and remote certificates.
789 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000790 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200791 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton5dfde182018-02-06 10:34:40 -0800792 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Benjamin Wright6c6c9df2018-10-25 01:16:26 -0700793 StatsReport* r = AddCertificateReports(
794 certificate->GetSSLCertificateChain().GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000795 if (r)
796 local_cert_report_id = r->id();
797 }
798
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800799 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
800 pc_->GetRemoteSSLCertChain(transport_name);
801 if (remote_cert_chain) {
802 StatsReport* r = AddCertificateReports(remote_cert_chain->GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000803 if (r)
804 remote_cert_report_id = r->id();
805 }
806
Steve Anton5dfde182018-02-06 10:34:40 -0800807 for (const auto& channel_iter : transport_stats.channel_stats) {
808 StatsReport::Id id(
809 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000810 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
811 channel_report->set_timestamp(stats_gathering_started_);
812 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
813 channel_iter.component);
814 if (local_cert_report_id.get()) {
815 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
816 local_cert_report_id);
817 }
818 if (remote_cert_report_id.get()) {
819 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
820 remote_cert_report_id);
821 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800822 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
823 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
824 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
825 channel_report->AddString(
826 StatsReport::kStatsValueNameSrtpCipher,
827 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000828 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800829 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
830 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
831 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
832 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700833 channel_report->AddString(
834 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800835 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000836 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000837
Qingsi Wang72a43a12018-02-20 16:03:18 -0800838 // Collect stats for non-pooled candidates. Note that the reports
839 // generated here supersedes the candidate reports generated in
840 // AddConnectionInfoReport below, and they may report candidates that are
841 // not paired. Also, the candidate report generated in
842 // AddConnectionInfoReport do not report port stats like StunStats.
843 for (const cricket::CandidateStats& stats :
844 channel_iter.candidate_stats_list) {
845 AddCandidateReport(stats, true);
846 }
847
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000848 int connection_id = 0;
849 for (const cricket::ConnectionInfo& info :
Yves Gerey665174f2018-06-19 15:03:05 +0200850 channel_iter.connection_infos) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000851 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800852 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000853 channel_report->id(), info);
854 if (info.best_connection) {
855 channel_report->AddId(
856 StatsReport::kStatsValueNameSelectedCandidatePairId,
857 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859 }
860 }
861 }
862}
863
stefanf79ade12017-06-02 06:44:03 -0700864void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700865 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700866
Steve Anton978b8762017-09-29 12:15:02 -0700867 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700868 return;
869
Steve Anton978b8762017-09-29 12:15:02 -0700870 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700871 cricket::BandwidthEstimationInfo bwe_info;
872 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
873 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
874 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
Steve Antonafb0bb72018-02-20 11:35:37 -0800875
stefanf79ade12017-06-02 06:44:03 -0700876 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
877 // TODO(holmer): Also fill this in for audio.
Steve Antonafb0bb72018-02-20 11:35:37 -0800878 for (auto transceiver : pc_->GetTransceiversInternal()) {
879 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) {
880 continue;
881 }
882 auto* video_channel =
883 static_cast<cricket::VideoChannel*>(transceiver->internal()->channel());
884 if (!video_channel) {
885 continue;
886 }
887 video_channel->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700888 }
Steve Antonafb0bb72018-02-20 11:35:37 -0800889
stefanf79ade12017-06-02 06:44:03 -0700890 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
891 StatsReport* report = reports_.FindOrAddNew(report_id);
892 ExtractStats(bwe_info, stats_gathering_started_, report);
893}
894
Steve Antonb8867112018-02-13 10:07:54 -0800895namespace {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000896
Steve Antonb8867112018-02-13 10:07:54 -0800897struct VoiceChannelStatsInfo {
898 std::string transport_name;
899 cricket::VoiceMediaChannel* voice_media_channel;
900 cricket::VoiceMediaInfo voice_media_info;
901};
902
903struct VideoChannelStatsInfo {
904 std::string transport_name;
905 cricket::VideoMediaChannel* video_media_channel;
906 cricket::VideoMediaInfo video_media_info;
907};
908
909} // namespace
910
911void StatsCollector::ExtractMediaInfo() {
912 RTC_DCHECK_RUN_ON(pc_->signaling_thread());
913
914 std::vector<VoiceChannelStatsInfo> voice_channel_infos;
915 std::vector<VideoChannelStatsInfo> video_channel_infos;
916
917 {
918 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
919 for (auto transceiver : pc_->GetTransceiversInternal()) {
920 if (!transceiver->internal()->channel()) {
921 continue;
922 }
923 cricket::MediaType media_type = transceiver->internal()->media_type();
924 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
925 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
926 transceiver->internal()->channel());
927 voice_channel_infos.emplace_back();
928 VoiceChannelStatsInfo& info = voice_channel_infos.back();
929 info.transport_name = voice_channel->transport_name();
930 info.voice_media_channel = voice_channel->media_channel();
931 } else {
932 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
933 auto* video_channel = static_cast<cricket::VideoChannel*>(
934 transceiver->internal()->channel());
935 video_channel_infos.emplace_back();
936 VideoChannelStatsInfo& info = video_channel_infos.back();
937 info.transport_name = video_channel->transport_name();
938 info.video_media_channel = video_channel->media_channel();
939 }
940 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000942
Steve Antonb8867112018-02-13 10:07:54 -0800943 pc_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&] {
944 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
945 for (auto it = voice_channel_infos.begin(); it != voice_channel_infos.end();
946 /* incremented manually */) {
947 if (!it->voice_media_channel->GetStats(&it->voice_media_info)) {
948 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats";
949 it = voice_channel_infos.erase(it);
950 continue;
951 }
952 ++it;
953 }
954 for (auto it = video_channel_infos.begin(); it != video_channel_infos.end();
955 /* incremented manually */) {
956 if (!it->video_media_channel->GetStats(&it->video_media_info)) {
957 RTC_LOG(LS_ERROR) << "Failed to get video channel stats";
958 it = video_channel_infos.erase(it);
959 continue;
960 }
961 ++it;
962 }
963 });
964
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000965 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
966
Steve Antonb8867112018-02-13 10:07:54 -0800967 bool has_remote_audio = false;
968 for (const auto& info : voice_channel_infos) {
969 StatsReport::Id transport_id = StatsReport::NewComponentId(
970 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
971 ExtractStatsFromList(info.voice_media_info.receivers, transport_id, this,
972 StatsReport::kReceive);
973 ExtractStatsFromList(info.voice_media_info.senders, transport_id, this,
974 StatsReport::kSend);
975 if (!info.voice_media_info.receivers.empty()) {
976 has_remote_audio = true;
977 }
Steve Anton74116482017-12-18 11:00:14 -0800978 }
Steve Antonb8867112018-02-13 10:07:54 -0800979 for (const auto& info : video_channel_infos) {
980 StatsReport::Id transport_id = StatsReport::NewComponentId(
981 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
982 ExtractStatsFromList(info.video_media_info.receivers, transport_id, this,
983 StatsReport::kReceive);
984 ExtractStatsFromList(info.video_media_info.senders, transport_id, this,
985 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000987
Steve Antonb8867112018-02-13 10:07:54 -0800988 UpdateStatsFromExistingLocalAudioTracks(has_remote_audio);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989}
990
nissefcc640f2016-04-01 01:10:42 -0700991void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700992 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700993
994 for (const auto& sender : pc_->GetSenders()) {
995 // TODO(nisse): SSRC == 0 currently means none. Delete check when
996 // that is fixed.
997 if (!sender->ssrc()) {
998 continue;
999 }
1000 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
1001 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
1002 continue;
1003 }
1004 // Safe, because kind() == kVideoKind implies a subclass of
1005 // VideoTrackInterface; see mediastreaminterface.h.
1006 VideoTrackSourceInterface* source =
1007 static_cast<VideoTrackInterface*>(track.get())->GetSource();
1008
1009 VideoTrackSourceInterface::Stats stats;
1010 if (!source->GetStats(&stats)) {
1011 continue;
1012 }
1013 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001014 StatsReport::kStatsReportTypeSsrc, rtc::ToString(sender->ssrc()),
1015 StatsReport::kSend);
nissefcc640f2016-04-01 01:10:42 -07001016 StatsReport* report = reports_.FindOrAddNew(stats_id);
1017 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
1018 stats.input_width);
1019 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
1020 stats.input_height);
1021 }
1022}
1023
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001024void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001025 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001026
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001027 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1028
deadbeefab9b2d12015-10-14 11:33:11 -07001029 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001030 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001031 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001032 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001033 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001034 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -07001035 // Filter out the initial id (-1).
1036 if (dc->id() >= 0) {
1037 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
1038 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001039 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
1040 report->AddString(StatsReport::kStatsValueNameState,
1041 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001042 }
1043}
1044
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001045StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001046 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001047 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001048 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001049 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
1050 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001051 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001052}
1053
Ivo Creusenae026092017-11-20 13:07:16 +01001054void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
1055 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001056 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001057 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001058 for (const auto& it : local_audio_tracks_) {
1059 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001060 uint32_t ssrc = it.second;
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001061 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
1062 rtc::ToString(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001063 if (report == NULL) {
1064 // This can happen if a local audio track is added to a stream on the
1065 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001066 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001067 continue;
1068 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001069
1070 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001071 const StatsReport::Value* v =
1072 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001073 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001074 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001075
jbauchbe24c942015-06-22 15:06:43 -07001076 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001077 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001078 }
1079}
1080
1081void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001082 StatsReport* report,
1083 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001084 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001085 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001086
andrew2fe1cb02015-11-27 17:27:35 -08001087 // Don't overwrite report values if they're not available.
1088 int signal_level;
1089 if (track->GetSignalLevel(&signal_level)) {
1090 RTC_DCHECK_GE(signal_level, 0);
1091 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1092 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001093
andrew2fe1cb02015-11-27 17:27:35 -08001094 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001095
andrew2fe1cb02015-11-27 17:27:35 -08001096 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001097 AudioProcessorInterface::AudioProcessorStatistics stats =
1098 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001099
Ivo Creusen56d46092017-11-24 17:29:59 +01001100 SetAudioProcessingStats(report, stats.typing_noise_detected,
1101 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001102 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001103}
1104
Steve Antona41959e2018-11-28 11:15:33 -08001105absl::string_view StatsCollector::GetTrackIdBySsrc(
1106 uint32_t ssrc,
1107 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001108 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
Steve Antona41959e2018-11-28 11:15:33 -08001109 absl::string_view track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001110 if (direction == StatsReport::kSend) {
Steve Antona41959e2018-11-28 11:15:33 -08001111 track_id = pc_->GetLocalTrackIdBySsrc(ssrc);
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001112 } else {
Steve Antona41959e2018-11-28 11:15:33 -08001113 RTC_DCHECK_EQ(direction, StatsReport::kReceive);
1114 track_id = pc_->GetRemoteTrackIdBySsrc(ssrc);
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001115 }
Steve Antona41959e2018-11-28 11:15:33 -08001116 if (track_id.empty()) {
1117 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc << " is not associated with a "
1118 << (direction == StatsReport::kSend ? "sending"
1119 : "receiving")
1120 << " track";
1121 }
1122 return track_id;
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001123}
1124
jbauchbe24c942015-06-22 15:06:43 -07001125void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001126 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001127
1128 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1129
1130 for (const auto& entry : track_ids_) {
1131 StatsReport* report = entry.second;
1132 report->set_timestamp(stats_gathering_started_);
1133 }
jbauchbe24c942015-06-22 15:06:43 -07001134}
1135
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001136void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001137 stats_gathering_started_ = 0;
1138}
1139
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140} // namespace webrtc