blob: 6b27dfd290ffd7a33c798ea7c5236da3d71794d7 [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>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <utility>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "pc/channel.h"
18#include "pc/peerconnection.h"
19#include "rtc_base/base64.h"
20#include "rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000025// The following is the enum RTCStatsIceCandidateType from
26// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
27// our stats report for ice candidate type could conform to that.
28const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
29const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
30const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
31const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
32
33// Strings used by the stats collector to report adapter types. This fits the
34// general stype of http://w3c.github.io/webrtc-stats than what
35// AdapterTypeToString does.
36const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
37const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
38const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
39const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000040const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000041
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000042template<typename ValueType>
43struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000044 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000046};
47
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000048typedef TypeForAdd<bool> BoolForAdd;
49typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020050typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000051typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000052
Steve Antond5585ca2017-10-23 14:49:26 -070053StatsReport::Id GetTransportIdFromProxy(
54 const std::map<std::string, std::string>& map,
55 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070056 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070057 auto found = map.find(proxy);
58 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000059 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070060 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000061
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000062 return StatsReport::NewComponentId(
63 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000064}
65
jbauchbe24c942015-06-22 15:06:43 -070066StatsReport* AddTrackReport(StatsCollection* reports,
67 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000068 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000069 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000070 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000071 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000072 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070073 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000074}
75
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076template <class TrackVector>
Steve Anton36b29d12017-10-30 09:57:42 -070077void CreateTrackReports(const TrackVector& tracks,
78 StatsCollection* reports,
79 TrackIdMap* track_ids) {
jbauchbe24c942015-06-22 15:06:43 -070080 for (const auto& track : tracks) {
81 const std::string& track_id = track->id();
82 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070083 RTC_DCHECK(report != nullptr);
Steve Anton36b29d12017-10-30 09:57:42 -070084 (*track_ids)[track_id] = report;
jbauchbe24c942015-06-22 15:06:43 -070085 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086}
87
tommi@webrtc.org92f40182015-03-04 15:25:19 +000088void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
89 StatsReport* report) {
90 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
91 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070092 if (info.rtt_ms >= 0) {
93 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
94 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000095}
96
pbosf42376c2015-08-28 07:35:32 -070097void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
98 StatsReport* report) {
99 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
100}
101
Ivo Creusenae026092017-11-20 13:07:16 +0100102void SetAudioProcessingStats(
103 StatsReport* report,
104 bool typing_noise_detected,
105 rtc::Optional<double> echo_return_loss,
106 rtc::Optional<double> echo_return_loss_enhancement,
107 rtc::Optional<int32_t> echo_delay_median_ms,
108 rtc::Optional<double> aec_quality_min,
109 rtc::Optional<int32_t> echo_delay_std_ms,
110 rtc::Optional<double> residual_echo_likelihood,
111 rtc::Optional<double> residual_echo_likelihood_recent_max) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000112 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
113 typing_noise_detected);
Ivo Creusenae026092017-11-20 13:07:16 +0100114 // TODO(ivoc): Remove the checks for default values once the whole stat
115 // chain uses optionals.
116 if (aec_quality_min && *aec_quality_min >= 0.0) {
zhihuang6ba3b192016-05-13 11:46:35 -0700117 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
Ivo Creusenae026092017-11-20 13:07:16 +0100118 *aec_quality_min);
zhihuang6ba3b192016-05-13 11:46:35 -0700119 }
Ivo Creusenae026092017-11-20 13:07:16 +0100120 if (echo_delay_median_ms && *echo_delay_median_ms >= 0) {
121 report->AddInt(StatsReport::kStatsValueNameEchoDelayMedian,
122 *echo_delay_median_ms);
123 }
124 if (echo_delay_std_ms && *echo_delay_std_ms >= 0) {
125 report->AddInt(StatsReport::kStatsValueNameEchoDelayStdDev,
126 *echo_delay_std_ms);
zhihuang6ba3b192016-05-13 11:46:35 -0700127 }
128 // These can take on valid negative values.
Ivo Creusenae026092017-11-20 13:07:16 +0100129 if (echo_return_loss) {
130 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss,
131 static_cast<int32_t>(*echo_return_loss));
henrik.lundin04a057b2017-01-16 23:53:59 -0800132 }
Ivo Creusenae026092017-11-20 13:07:16 +0100133 if (echo_return_loss_enhancement) {
134 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
135 static_cast<int32_t>(*echo_return_loss_enhancement));
136 }
137 if (residual_echo_likelihood && *residual_echo_likelihood >= 0.0) {
138 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
139 *residual_echo_likelihood);
140 }
141 if (residual_echo_likelihood_recent_max &&
142 *residual_echo_likelihood_recent_max >= 0.0) {
ivoc4e477a12017-01-15 08:29:46 -0800143 report->AddFloat(
144 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
Ivo Creusenae026092017-11-20 13:07:16 +0100145 *residual_echo_likelihood_recent_max);
ivoc8c63a822016-10-21 04:10:03 -0700146 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000147}
148
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700150 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000151 const FloatForAdd floats[] = {
152 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
153 { StatsReport::kStatsValueNameSecondaryDecodedRate,
154 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200155 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
156 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000157 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200158 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
159 { StatsReport::kStatsValueNamePreemptiveExpandRate,
160 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700161 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
162 { StatsReport::kStatsValueNameTotalSamplesDuration,
163 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000164 };
165
166 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000167 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
168 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
169 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
170 { StatsReport::kStatsValueNameDecodingCTSG,
171 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700172 { StatsReport::kStatsValueNameDecodingMutedOutput,
173 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000174 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
175 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
176 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
177 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
178 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
179 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
180 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
181 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
182 info.jitter_buffer_preferred_ms },
183 };
184
185 for (const auto& f : floats)
186 report->AddFloat(f.name, f.value);
187
188 for (const auto& i : ints)
189 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700190 if (info.audio_level >= 0) {
191 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
192 info.audio_level);
193 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000194
195 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700197 if (info.capture_start_ntp_time_ms >= 0) {
198 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
199 info.capture_start_ntp_time_ms);
200 }
fippobec70ab2016-01-28 01:27:15 -0800201 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202}
203
204void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000205 ExtractCommonSendProperties(info, report);
206
Ivo Creusenae026092017-11-20 13:07:16 +0100207 // TODO(ivoc): Update VoiceSenderInfo to pass Optionals all the way from APM.
andrew2fe1cb02015-11-27 17:27:35 -0800208 SetAudioProcessingStats(
Ivo Creusenae026092017-11-20 13:07:16 +0100209 report, info.typing_noise_detected,
210 rtc::Optional<double>(info.echo_return_loss),
211 rtc::Optional<double>(info.echo_return_loss_enhancement),
212 rtc::Optional<int32_t>(info.echo_delay_median_ms),
213 rtc::Optional<double>(info.aec_quality_min),
214 rtc::Optional<int32_t>(info.echo_delay_std_ms),
215 rtc::Optional<double>(info.residual_echo_likelihood),
216 rtc::Optional<double>(info.residual_echo_likelihood_recent_max));
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000217
zsteine76bd3a2017-07-14 12:17:49 -0700218 const FloatForAdd floats[] = {
219 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
220 { StatsReport::kStatsValueNameTotalSamplesDuration,
221 info.total_input_duration }
222 };
223
andrew2fe1cb02015-11-27 17:27:35 -0800224 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000225 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800226 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000227 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
228 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
229 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
230 };
231
zsteine76bd3a2017-07-14 12:17:49 -0700232 for (const auto& f : floats) {
233 report->AddFloat(f.name, f.value);
234 }
235
zhihuang6ba3b192016-05-13 11:46:35 -0700236 for (const auto& i : ints) {
237 if (i.value >= 0) {
238 report->AddInt(i.name, i.value);
239 }
240 }
fippobec70ab2016-01-28 01:27:15 -0800241 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700242 if (info.ana_statistics.bitrate_action_counter) {
243 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
244 *info.ana_statistics.bitrate_action_counter);
245 }
246 if (info.ana_statistics.channel_action_counter) {
247 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
248 *info.ana_statistics.channel_action_counter);
249 }
250 if (info.ana_statistics.dtx_action_counter) {
251 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
252 *info.ana_statistics.dtx_action_counter);
253 }
254 if (info.ana_statistics.fec_action_counter) {
255 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
256 *info.ana_statistics.fec_action_counter);
257 }
ivoc0d0b9122017-09-08 13:24:21 -0700258 if (info.ana_statistics.frame_length_increase_counter) {
259 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
260 *info.ana_statistics.frame_length_increase_counter);
261 }
262 if (info.ana_statistics.frame_length_decrease_counter) {
263 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
264 *info.ana_statistics.frame_length_decrease_counter);
265 }
266 if (info.ana_statistics.uplink_packet_loss_fraction) {
267 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
268 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700269 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270}
271
272void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700273 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100274 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
275 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000276 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700278 if (info.capture_start_ntp_time_ms >= 0) {
279 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
280 info.capture_start_ntp_time_ms);
281 }
sakalcc452e12017-02-09 04:53:45 -0800282 if (info.qp_sum)
283 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
284
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000285 const IntForAdd ints[] = {
286 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
287 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
288 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
289 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
290 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
291 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
292 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
293 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
294 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
295 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
296 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
297 info.min_playout_delay_ms },
298 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
299 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
300 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
301 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
302 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
303 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700304 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000305 };
306
307 for (const auto& i : ints)
308 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800309 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700310
ilnik2edc6842017-07-06 03:06:50 -0700311 if (info.timing_frame_info) {
312 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
313 info.timing_frame_info->ToString());
314 }
ilnikf04afde2017-07-07 01:26:24 -0700315
ilnika79cc282017-08-23 05:24:10 -0700316 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
317 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700318
319 report->AddString(
320 StatsReport::kStatsValueNameContentType,
321 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322}
323
324void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000325 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326
Peter Boströmb7d9a972015-12-18 16:01:11 +0100327 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
328 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000329 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
330 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000331 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
332 (info.adapt_reason & 0x1) > 0);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100333 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
334 info.has_entered_low_resolution);
335
sakal87da4042016-10-31 06:53:47 -0700336 if (info.qp_sum)
337 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000338
339 const IntForAdd ints[] = {
340 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
341 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000342 { StatsReport::kStatsValueNameEncodeUsagePercent,
343 info.encode_usage_percent },
344 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000345 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
346 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
347 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000348 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
349 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
350 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
351 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
352 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700353 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000354 };
355
356 for (const auto& i : ints)
357 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800358 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700359 report->AddString(
360 StatsReport::kStatsValueNameContentType,
361 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
364void ExtractStats(const cricket::BandwidthEstimationInfo& info,
365 double stats_gathering_started,
366 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700367 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000369 report->set_timestamp(stats_gathering_started);
370 const IntForAdd ints[] = {
371 { StatsReport::kStatsValueNameAvailableSendBandwidth,
372 info.available_send_bandwidth },
373 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
374 info.available_recv_bandwidth },
375 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
376 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
377 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
378 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
379 };
380 for (const auto& i : ints)
381 report->AddInt(i.name, i.value);
382 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383}
384
wu@webrtc.org97077a32013-10-25 21:18:33 +0000385void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
386 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000387 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000388 // TODO(hta): Extract some stats here.
389}
390
391void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
392 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000393 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000394 // TODO(hta): Extract some stats here.
395}
396
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000398// In order to use the template, the functions that are called from it,
399// ExtractStats and ExtractRemoteStats, must be defined and overloaded
400// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401template<typename T>
402void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000403 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000404 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000405 StatsReport::Direction direction) {
406 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200407 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000408 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000409 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000410 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
411 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000412 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000413 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000414
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000415 if (!d.remote_stats.empty()) {
416 report = collector->PrepareReport(false, ssrc, transport_id, direction);
417 if (report)
418 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000419 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000421}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422
423} // namespace
424
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000425const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
426 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
427 return STATSREPORT_LOCAL_PORT_TYPE;
428 }
429 if (candidate_type == cricket::STUN_PORT_TYPE) {
430 return STATSREPORT_STUN_PORT_TYPE;
431 }
432 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
433 return STATSREPORT_PRFLX_PORT_TYPE;
434 }
435 if (candidate_type == cricket::RELAY_PORT_TYPE) {
436 return STATSREPORT_RELAY_PORT_TYPE;
437 }
nisseeb4ca4e2017-01-12 02:24:27 -0800438 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000439 return "unknown";
440}
441
442const char* AdapterTypeToStatsType(rtc::AdapterType type) {
443 switch (type) {
444 case rtc::ADAPTER_TYPE_UNKNOWN:
445 return "unknown";
446 case rtc::ADAPTER_TYPE_ETHERNET:
447 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
448 case rtc::ADAPTER_TYPE_WIFI:
449 return STATSREPORT_ADAPTER_TYPE_WIFI;
450 case rtc::ADAPTER_TYPE_CELLULAR:
451 return STATSREPORT_ADAPTER_TYPE_WWAN;
452 case rtc::ADAPTER_TYPE_VPN:
453 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000454 case rtc::ADAPTER_TYPE_LOOPBACK:
455 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000456 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800457 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000458 return "";
459 }
460}
461
deadbeefab9b2d12015-10-14 11:33:11 -0700462StatsCollector::StatsCollector(PeerConnection* pc)
463 : pc_(pc), stats_gathering_started_(0) {
464 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000465}
466
467StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700468 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
nissecdf37a92016-09-13 23:41:47 -0700471// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000472double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700473 return rtc::TimeUTCMicros() /
474 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000475}
476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477// Adds a MediaStream with tracks that can be used as a |selector| in a call
478// to GetStats.
479void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700480 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700481 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482
Steve Anton36b29d12017-10-30 09:57:42 -0700483 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
484 &track_ids_);
485 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
486 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487}
488
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000489void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200490 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700491 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700492 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700493#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000494 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700495 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000496#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000497
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000498 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000499
500 // Create the kStatsReportTypeTrack report for the new track if there is no
501 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000502 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
503 audio_track->id()));
504 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000505 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000506 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000507 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000508 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000509}
510
511void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200512 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700513 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700514 local_audio_tracks_.erase(
515 std::remove_if(
516 local_audio_tracks_.begin(), local_audio_tracks_.end(),
517 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
518 return track.first == audio_track && track.second == ssrc;
519 }),
520 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000521}
522
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000523void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700525 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700526 RTC_DCHECK(reports != NULL);
527 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000529 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000532 reports->reserve(reports_.size());
533 for (auto* r : reports_)
534 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000535 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
537
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000538 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700539 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000540 if (report)
541 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000543 report = reports_.Find(StatsReport::NewTypedId(
544 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000546 if (!report)
547 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000549 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550
551 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000552 for (const auto* r : reports_) {
553 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000555
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000556 const StatsReport::Value* v =
557 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000558 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000559 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561}
562
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000563void
564StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700565 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 double time_now = GetTimeNow();
567 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
568 // ms apart will be ignored.
569 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000570 if (stats_gathering_started_ != 0 &&
571 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 return;
573 }
574 stats_gathering_started_ = time_now;
575
Steve Anton75737c02017-11-06 10:37:17 -0800576 // TODO(tommi): All of these hop over to the worker thread to fetch
577 // information. We could use an AsyncInvoker to run all of these and post
578 // the information back to the signaling thread where we can create and
579 // update stats reports. That would also clean up the threading story a bit
580 // since we'd be creating/updating the stats report objects consistently on
581 // the same thread (this class has no locks right now).
582 ExtractSessionInfo();
583 ExtractBweInfo();
584 ExtractVoiceInfo();
585 ExtractVideoInfo(level);
586 ExtractSenderInfo();
587 ExtractDataInfo();
588 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589}
590
deadbeefab9b2d12015-10-14 11:33:11 -0700591StatsReport* StatsCollector::PrepareReport(
592 bool local,
593 uint32_t ssrc,
594 const StatsReport::Id& transport_id,
595 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700596 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000597 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200598 local ? StatsReport::kStatsReportTypeSsrc
599 : StatsReport::kStatsReportTypeRemoteSsrc,
600 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000601 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602
xians@webrtc.org01bda202014-07-09 07:38:38 +0000603 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000605 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000606 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000607 // The ssrc is not used by any track or existing report, return NULL
608 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000610 }
611
612 // The ssrc is not used by any existing track. Keeps the old track id
613 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000614 const StatsReport::Value* v =
615 report->FindValue(StatsReport::kStatsValueNameTrackId);
616 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000617 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000620 if (!report)
621 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000623 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000624 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000626 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000627 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000629 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 return report;
631}
632
zhihuange9e94c32016-11-04 11:38:15 -0700633bool StatsCollector::IsValidTrack(const std::string& track_id) {
634 return reports_.Find(StatsReport::NewTypedId(
635 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
636}
637
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000638StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000639 const rtc::SSLCertificate* cert) {
Steve Anton978b8762017-09-29 12:15:02 -0700640 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700641 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000642
hbose29352b2016-08-25 03:52:38 -0700643 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
644 StatsReport* first_report = nullptr;
645 StatsReport* prev_report = nullptr;
646 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
647 stats = stats->issuer.get()) {
648 StatsReport::Id id(StatsReport::NewTypedId(
649 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
650
651 StatsReport* report = reports_.ReplaceOrAddNew(id);
652 report->set_timestamp(stats_gathering_started_);
653 report->AddString(StatsReport::kStatsValueNameFingerprint,
654 stats->fingerprint);
655 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
656 stats->fingerprint_algorithm);
657 report->AddString(StatsReport::kStatsValueNameDer,
658 stats->base64_certificate);
659 if (!first_report)
660 first_report = report;
661 else
662 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
663 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000664 }
hbose29352b2016-08-25 03:52:38 -0700665 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000666}
667
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000668StatsReport* StatsCollector::AddConnectionInfoReport(
669 const std::string& content_name, int component, int connection_id,
670 const StatsReport::Id& channel_report_id,
671 const cricket::ConnectionInfo& info) {
672 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
673 connection_id));
674 StatsReport* report = reports_.ReplaceOrAddNew(id);
675 report->set_timestamp(stats_gathering_started_);
676
677 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700678 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
679 {StatsReport::kStatsValueNameReceiving, info.receiving},
680 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000681 };
682 for (const auto& b : bools)
683 report->AddBoolean(b.name, b.value);
684
685 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
686 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
687 AddCandidateReport(info.local_candidate, true)->id());
688 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
689 AddCandidateReport(info.remote_candidate, false)->id());
690
691 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700692 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
693 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
694 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
695 {StatsReport::kStatsValueNameRtt, info.rtt},
696 {StatsReport::kStatsValueNameSendPacketsDiscarded,
697 info.sent_discarded_packets},
698 {StatsReport::kStatsValueNameSentPingRequestsTotal,
699 info.sent_ping_requests_total},
700 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
701 info.sent_ping_requests_before_first_response},
702 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
703 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
704 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000705 };
706 for (const auto& i : int64s)
707 report->AddInt64(i.name, i.value);
708
709 report->AddString(StatsReport::kStatsValueNameLocalAddress,
710 info.local_candidate.address().ToString());
711 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
712 info.local_candidate.type());
713 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
714 info.remote_candidate.address().ToString());
715 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
716 info.remote_candidate.type());
717 report->AddString(StatsReport::kStatsValueNameTransportType,
718 info.local_candidate.protocol());
719
720 return report;
721}
722
723StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000724 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000725 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000726 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
727 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000728 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000729 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000730 report->set_timestamp(stats_gathering_started_);
731 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000732 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
733 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000734 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000735 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
736 candidate.address().ipaddr().ToString());
737 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
738 candidate.address().PortAsString());
739 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
740 candidate.priority());
741 report->AddString(StatsReport::kStatsValueNameCandidateType,
742 IceCandidateTypeToStatsType(candidate.type()));
743 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
744 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000745 }
746
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000747 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000748}
749
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700751 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000752
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000754 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700755 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000756 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000757 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000758 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700759 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760
Steve Anton978b8762017-09-29 12:15:02 -0700761 std::unique_ptr<SessionStats> stats = pc_->GetSessionStats_s();
hbosdf6075a2016-12-19 04:58:02 -0800762 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000763 return;
764 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000766 // Store the proxy map away for use in SSRC reporting.
767 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
768 // signaling thread after fetching them on the worker thread, then just use
769 // the proxy map directly from the session stats.
770 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
771 // data.
hbosdf6075a2016-12-19 04:58:02 -0800772 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000773
hbosdf6075a2016-12-19 04:58:02 -0800774 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000775 // Attempt to get a copy of the certificates from the transport and
776 // expose them in stats reports. All channels in a transport share the
777 // same local and remote certificates.
778 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000779 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200780 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton978b8762017-09-29 12:15:02 -0700781 if (pc_->GetLocalCertificate(transport_iter.second.transport_name,
782 &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200783 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000784 if (r)
785 local_cert_report_id = r->id();
786 }
787
jbauch555604a2016-04-26 03:13:22 -0700788 std::unique_ptr<rtc::SSLCertificate> cert =
Steve Anton978b8762017-09-29 12:15:02 -0700789 pc_->GetRemoteSSLCertificate(transport_iter.second.transport_name);
kwibergb4d01c42016-04-06 05:15:06 -0700790 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000791 StatsReport* r = AddCertificateReports(cert.get());
792 if (r)
793 remote_cert_report_id = r->id();
794 }
795
796 for (const auto& channel_iter : transport_iter.second.channel_stats) {
797 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700798 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000799 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
800 channel_report->set_timestamp(stats_gathering_started_);
801 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
802 channel_iter.component);
803 if (local_cert_report_id.get()) {
804 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
805 local_cert_report_id);
806 }
807 if (remote_cert_report_id.get()) {
808 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
809 remote_cert_report_id);
810 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800811 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
812 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
813 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
814 channel_report->AddString(
815 StatsReport::kStatsValueNameSrtpCipher,
816 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000817 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800818 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
819 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
820 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
821 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700822 channel_report->AddString(
823 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800824 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000825 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000826
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000827 int connection_id = 0;
828 for (const cricket::ConnectionInfo& info :
829 channel_iter.connection_infos) {
830 StatsReport* connection_report = AddConnectionInfoReport(
831 transport_iter.first, channel_iter.component, connection_id++,
832 channel_report->id(), info);
833 if (info.best_connection) {
834 channel_report->AddId(
835 StatsReport::kStatsValueNameSelectedCandidatePairId,
836 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
838 }
839 }
840 }
841}
842
stefanf79ade12017-06-02 06:44:03 -0700843void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700844 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700845
Steve Anton978b8762017-09-29 12:15:02 -0700846 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700847 return;
848
Steve Anton978b8762017-09-29 12:15:02 -0700849 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700850 cricket::BandwidthEstimationInfo bwe_info;
851 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
852 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
853 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
854 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
855 // TODO(holmer): Also fill this in for audio.
Steve Anton978b8762017-09-29 12:15:02 -0700856 if (pc_->video_channel()) {
857 pc_->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700858 }
stefanf79ade12017-06-02 06:44:03 -0700859 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
860 StatsReport* report = reports_.FindOrAddNew(report_id);
861 ExtractStats(bwe_info, stats_gathering_started_, report);
862}
863
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864void StatsCollector::ExtractVoiceInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700865 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000866
Steve Anton978b8762017-09-29 12:15:02 -0700867 if (!pc_->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 return;
869 }
870 cricket::VoiceMediaInfo voice_info;
Steve Anton978b8762017-09-29 12:15:02 -0700871 if (!pc_->voice_channel()->GetStats(&voice_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100872 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 return;
874 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000875
876 // TODO(tommi): The above code should run on the worker thread and post the
877 // results back to the signaling thread, where we can add data to the reports.
878 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
879
deadbeefab9b2d12015-10-14 11:33:11 -0700880 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700881 proxy_to_transport_, pc_->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000882 if (!transport_id.get()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100883 RTC_LOG(LS_ERROR) << "Failed to get transport name for proxy "
884 << pc_->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 return;
886 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000887
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000888 ExtractStatsFromList(voice_info.receivers, transport_id, this,
889 StatsReport::kReceive);
890 ExtractStatsFromList(voice_info.senders, transport_id, this,
891 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000892
Ivo Creusenae026092017-11-20 13:07:16 +0100893 UpdateStatsFromExistingLocalAudioTracks(voice_info.receivers.size() > 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894}
895
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000896void StatsCollector::ExtractVideoInfo(
897 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700898 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000899
Steve Anton978b8762017-09-29 12:15:02 -0700900 if (!pc_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000902
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 cricket::VideoMediaInfo video_info;
Steve Anton978b8762017-09-29 12:15:02 -0700904 if (!pc_->video_channel()->GetStats(&video_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100905 RTC_LOG(LS_ERROR) << "Failed to get video channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 return;
907 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000908
909 // TODO(tommi): The above code should run on the worker thread and post the
910 // results back to the signaling thread, where we can add data to the reports.
911 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
912
deadbeefab9b2d12015-10-14 11:33:11 -0700913 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700914 proxy_to_transport_, pc_->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000915 if (!transport_id.get()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100916 RTC_LOG(LS_ERROR) << "Failed to get transport name for proxy "
917 << pc_->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 return;
919 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000920 ExtractStatsFromList(video_info.receivers, transport_id, this,
921 StatsReport::kReceive);
922 ExtractStatsFromList(video_info.senders, transport_id, this,
923 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924}
925
nissefcc640f2016-04-01 01:10:42 -0700926void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700927 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700928
929 for (const auto& sender : pc_->GetSenders()) {
930 // TODO(nisse): SSRC == 0 currently means none. Delete check when
931 // that is fixed.
932 if (!sender->ssrc()) {
933 continue;
934 }
935 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
936 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
937 continue;
938 }
939 // Safe, because kind() == kVideoKind implies a subclass of
940 // VideoTrackInterface; see mediastreaminterface.h.
941 VideoTrackSourceInterface* source =
942 static_cast<VideoTrackInterface*>(track.get())->GetSource();
943
944 VideoTrackSourceInterface::Stats stats;
945 if (!source->GetStats(&stats)) {
946 continue;
947 }
948 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
949 StatsReport::kStatsReportTypeSsrc,
950 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
951 StatsReport* report = reports_.FindOrAddNew(stats_id);
952 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
953 stats.input_width);
954 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
955 stats.input_height);
956 }
957}
958
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000959void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700960 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000961
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000962 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
963
deadbeefab9b2d12015-10-14 11:33:11 -0700964 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000965 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000966 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000967 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000968 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000969 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700970 // Filter out the initial id (-1).
971 if (dc->id() >= 0) {
972 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
973 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000974 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
975 report->AddString(StatsReport::kStatsValueNameState,
976 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000977 }
978}
979
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000980StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000981 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000982 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700983 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700984 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
985 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000986 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000987}
988
Ivo Creusenae026092017-11-20 13:07:16 +0100989void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
990 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -0700991 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000992 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000993 for (const auto& it : local_audio_tracks_) {
994 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200995 uint32_t ssrc = it.second;
996 StatsReport* report =
997 GetReport(StatsReport::kStatsReportTypeSsrc,
998 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000999 if (report == NULL) {
1000 // This can happen if a local audio track is added to a stream on the
1001 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001002 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001003 continue;
1004 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001005
1006 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001007 const StatsReport::Value* v =
1008 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001009 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001010 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001011
jbauchbe24c942015-06-22 15:06:43 -07001012 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001013 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001014 }
1015}
1016
1017void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001018 StatsReport* report,
1019 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001020 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001021 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001022
andrew2fe1cb02015-11-27 17:27:35 -08001023 // Don't overwrite report values if they're not available.
1024 int signal_level;
1025 if (track->GetSignalLevel(&signal_level)) {
1026 RTC_DCHECK_GE(signal_level, 0);
1027 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1028 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001029
andrew2fe1cb02015-11-27 17:27:35 -08001030 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001031
andrew2fe1cb02015-11-27 17:27:35 -08001032 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001033 AudioProcessorInterface::AudioProcessorStatistics stats =
1034 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001035
andrew2fe1cb02015-11-27 17:27:35 -08001036 SetAudioProcessingStats(
1037 report, stats.typing_noise_detected, stats.echo_return_loss,
1038 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -07001039 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -08001040 stats.residual_echo_likelihood,
1041 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +02001042
Ivo Creusenae026092017-11-20 13:07:16 +01001043 if (stats.aec_divergent_filter_fraction) {
1044 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
1045 *stats.aec_divergent_filter_fraction);
1046 }
andrew2fe1cb02015-11-27 17:27:35 -08001047 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001048}
1049
Peter Boström0c4e06b2015-10-07 12:23:21 +02001050bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1051 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001052 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001053 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001054 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001055 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001056 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1057 << " is not associated with a sending track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001058 return false;
1059 }
1060 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001061 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001062 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001063 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1064 << " is not associated with a receiving track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001065 return false;
1066 }
1067 }
1068
1069 return true;
1070}
1071
jbauchbe24c942015-06-22 15:06:43 -07001072void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001073 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001074
1075 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1076
1077 for (const auto& entry : track_ids_) {
1078 StatsReport* report = entry.second;
1079 report->set_timestamp(stats_gathering_started_);
1080 }
jbauchbe24c942015-06-22 15:06:43 -07001081}
1082
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001083void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001084 stats_gathering_started_ = 0;
1085}
1086
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087} // namespace webrtc