blob: 95201769a64f9c5e349b642da2a1d2f85a398299 [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
andrew2fe1cb02015-11-27 17:27:35 -0800102void SetAudioProcessingStats(StatsReport* report,
103 bool typing_noise_detected,
104 int echo_return_loss,
105 int echo_return_loss_enhancement,
106 int echo_delay_median_ms,
107 float aec_quality_min,
ivoc8c63a822016-10-21 04:10:03 -0700108 int echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800109 float residual_echo_likelihood,
110 float residual_echo_likelihood_recent_max) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000111 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
112 typing_noise_detected);
zhihuang6ba3b192016-05-13 11:46:35 -0700113 if (aec_quality_min >= 0.0f) {
114 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
115 aec_quality_min);
116 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000117 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000118 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
119 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
120 };
zhihuang6ba3b192016-05-13 11:46:35 -0700121 for (const auto& i : ints) {
122 if (i.value >= 0) {
123 report->AddInt(i.name, i.value);
124 }
125 }
126 // These can take on valid negative values.
127 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss);
128 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
129 echo_return_loss_enhancement);
ivoc8c63a822016-10-21 04:10:03 -0700130 if (residual_echo_likelihood >= 0.0f) {
131 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
132 residual_echo_likelihood);
henrik.lundin04a057b2017-01-16 23:53:59 -0800133 }
134 if (residual_echo_likelihood_recent_max >= 0.0f) {
ivoc4e477a12017-01-15 08:29:46 -0800135 report->AddFloat(
136 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
137 residual_echo_likelihood_recent_max);
ivoc8c63a822016-10-21 04:10:03 -0700138 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000139}
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700142 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000143 const FloatForAdd floats[] = {
144 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
145 { StatsReport::kStatsValueNameSecondaryDecodedRate,
146 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200147 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
148 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000149 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200150 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
151 { StatsReport::kStatsValueNamePreemptiveExpandRate,
152 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700153 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
154 { StatsReport::kStatsValueNameTotalSamplesDuration,
155 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000156 };
157
158 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000159 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
160 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
161 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
162 { StatsReport::kStatsValueNameDecodingCTSG,
163 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700164 { StatsReport::kStatsValueNameDecodingMutedOutput,
165 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000166 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
167 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
168 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
169 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
170 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
171 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
172 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
173 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
174 info.jitter_buffer_preferred_ms },
175 };
176
177 for (const auto& f : floats)
178 report->AddFloat(f.name, f.value);
179
180 for (const auto& i : ints)
181 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700182 if (info.audio_level >= 0) {
183 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
184 info.audio_level);
185 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000186
187 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700189 if (info.capture_start_ntp_time_ms >= 0) {
190 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
191 info.capture_start_ntp_time_ms);
192 }
fippobec70ab2016-01-28 01:27:15 -0800193 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194}
195
196void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000197 ExtractCommonSendProperties(info, report);
198
andrew2fe1cb02015-11-27 17:27:35 -0800199 SetAudioProcessingStats(
200 report, info.typing_noise_detected, info.echo_return_loss,
201 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700202 info.aec_quality_min, info.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800203 info.residual_echo_likelihood, info.residual_echo_likelihood_recent_max);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000204
zsteine76bd3a2017-07-14 12:17:49 -0700205 const FloatForAdd floats[] = {
206 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
207 { StatsReport::kStatsValueNameTotalSamplesDuration,
208 info.total_input_duration }
209 };
210
andrew2fe1cb02015-11-27 17:27:35 -0800211 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000212 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800213 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000214 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
215 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
216 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
217 };
218
zsteine76bd3a2017-07-14 12:17:49 -0700219 for (const auto& f : floats) {
220 report->AddFloat(f.name, f.value);
221 }
222
zhihuang6ba3b192016-05-13 11:46:35 -0700223 for (const auto& i : ints) {
224 if (i.value >= 0) {
225 report->AddInt(i.name, i.value);
226 }
227 }
fippobec70ab2016-01-28 01:27:15 -0800228 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700229 if (info.ana_statistics.bitrate_action_counter) {
230 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
231 *info.ana_statistics.bitrate_action_counter);
232 }
233 if (info.ana_statistics.channel_action_counter) {
234 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
235 *info.ana_statistics.channel_action_counter);
236 }
237 if (info.ana_statistics.dtx_action_counter) {
238 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
239 *info.ana_statistics.dtx_action_counter);
240 }
241 if (info.ana_statistics.fec_action_counter) {
242 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
243 *info.ana_statistics.fec_action_counter);
244 }
ivoc0d0b9122017-09-08 13:24:21 -0700245 if (info.ana_statistics.frame_length_increase_counter) {
246 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
247 *info.ana_statistics.frame_length_increase_counter);
248 }
249 if (info.ana_statistics.frame_length_decrease_counter) {
250 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
251 *info.ana_statistics.frame_length_decrease_counter);
252 }
253 if (info.ana_statistics.uplink_packet_loss_fraction) {
254 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
255 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700256 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257}
258
259void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700260 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100261 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
262 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000263 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700265 if (info.capture_start_ntp_time_ms >= 0) {
266 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
267 info.capture_start_ntp_time_ms);
268 }
sakalcc452e12017-02-09 04:53:45 -0800269 if (info.qp_sum)
270 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
271
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000272 const IntForAdd ints[] = {
273 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
274 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
275 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
276 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
277 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
278 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
279 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
280 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
281 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
282 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
283 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
284 info.min_playout_delay_ms },
285 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
286 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
287 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
288 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
289 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
290 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700291 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000292 };
293
294 for (const auto& i : ints)
295 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800296 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700297
ilnik2edc6842017-07-06 03:06:50 -0700298 if (info.timing_frame_info) {
299 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
300 info.timing_frame_info->ToString());
301 }
ilnikf04afde2017-07-07 01:26:24 -0700302
ilnika79cc282017-08-23 05:24:10 -0700303 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
304 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700305
306 report->AddString(
307 StatsReport::kStatsValueNameContentType,
308 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309}
310
311void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000312 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313
Peter Boströmb7d9a972015-12-18 16:01:11 +0100314 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
315 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000316 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
317 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000318 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
319 (info.adapt_reason & 0x1) > 0);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100320 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
321 info.has_entered_low_resolution);
322
sakal87da4042016-10-31 06:53:47 -0700323 if (info.qp_sum)
324 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000325
326 const IntForAdd ints[] = {
327 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
328 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000329 { StatsReport::kStatsValueNameEncodeUsagePercent,
330 info.encode_usage_percent },
331 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000332 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
333 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
334 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000335 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
336 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
337 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
338 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
339 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700340 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000341 };
342
343 for (const auto& i : ints)
344 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800345 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700346 report->AddString(
347 StatsReport::kStatsValueNameContentType,
348 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349}
350
351void ExtractStats(const cricket::BandwidthEstimationInfo& info,
352 double stats_gathering_started,
353 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700354 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000356 report->set_timestamp(stats_gathering_started);
357 const IntForAdd ints[] = {
358 { StatsReport::kStatsValueNameAvailableSendBandwidth,
359 info.available_send_bandwidth },
360 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
361 info.available_recv_bandwidth },
362 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
363 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
364 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
365 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
366 };
367 for (const auto& i : ints)
368 report->AddInt(i.name, i.value);
369 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
wu@webrtc.org97077a32013-10-25 21:18:33 +0000372void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
373 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000374 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000375 // TODO(hta): Extract some stats here.
376}
377
378void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
379 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000380 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000381 // TODO(hta): Extract some stats here.
382}
383
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000385// In order to use the template, the functions that are called from it,
386// ExtractStats and ExtractRemoteStats, must be defined and overloaded
387// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388template<typename T>
389void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000390 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000391 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000392 StatsReport::Direction direction) {
393 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200394 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000395 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000396 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000397 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
398 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000399 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000400 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000401
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000402 if (!d.remote_stats.empty()) {
403 report = collector->PrepareReport(false, ssrc, transport_id, direction);
404 if (report)
405 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000406 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000408}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409
410} // namespace
411
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000412const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
413 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
414 return STATSREPORT_LOCAL_PORT_TYPE;
415 }
416 if (candidate_type == cricket::STUN_PORT_TYPE) {
417 return STATSREPORT_STUN_PORT_TYPE;
418 }
419 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
420 return STATSREPORT_PRFLX_PORT_TYPE;
421 }
422 if (candidate_type == cricket::RELAY_PORT_TYPE) {
423 return STATSREPORT_RELAY_PORT_TYPE;
424 }
nisseeb4ca4e2017-01-12 02:24:27 -0800425 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000426 return "unknown";
427}
428
429const char* AdapterTypeToStatsType(rtc::AdapterType type) {
430 switch (type) {
431 case rtc::ADAPTER_TYPE_UNKNOWN:
432 return "unknown";
433 case rtc::ADAPTER_TYPE_ETHERNET:
434 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
435 case rtc::ADAPTER_TYPE_WIFI:
436 return STATSREPORT_ADAPTER_TYPE_WIFI;
437 case rtc::ADAPTER_TYPE_CELLULAR:
438 return STATSREPORT_ADAPTER_TYPE_WWAN;
439 case rtc::ADAPTER_TYPE_VPN:
440 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000441 case rtc::ADAPTER_TYPE_LOOPBACK:
442 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000443 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800444 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000445 return "";
446 }
447}
448
deadbeefab9b2d12015-10-14 11:33:11 -0700449StatsCollector::StatsCollector(PeerConnection* pc)
450 : pc_(pc), stats_gathering_started_(0) {
451 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000452}
453
454StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700455 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
nissecdf37a92016-09-13 23:41:47 -0700458// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000459double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700460 return rtc::TimeUTCMicros() /
461 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000462}
463
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464// Adds a MediaStream with tracks that can be used as a |selector| in a call
465// to GetStats.
466void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700467 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700468 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469
Steve Anton36b29d12017-10-30 09:57:42 -0700470 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
471 &track_ids_);
472 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
473 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474}
475
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000476void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200477 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700478 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700479 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700480#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000481 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700482 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000483#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000484
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000485 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000486
487 // Create the kStatsReportTypeTrack report for the new track if there is no
488 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000489 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
490 audio_track->id()));
491 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000492 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000493 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000494 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000495 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000496}
497
498void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200499 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700500 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700501 local_audio_tracks_.erase(
502 std::remove_if(
503 local_audio_tracks_.begin(), local_audio_tracks_.end(),
504 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
505 return track.first == audio_track && track.second == ssrc;
506 }),
507 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000508}
509
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000510void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700512 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700513 RTC_DCHECK(reports != NULL);
514 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000516 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
517
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000519 reports->reserve(reports_.size());
520 for (auto* r : reports_)
521 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000522 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
524
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000525 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700526 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000527 if (report)
528 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000530 report = reports_.Find(StatsReport::NewTypedId(
531 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000533 if (!report)
534 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000536 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537
538 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000539 for (const auto* r : reports_) {
540 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000542
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000543 const StatsReport::Value* v =
544 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000545 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000546 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548}
549
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000550void
551StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700552 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 double time_now = GetTimeNow();
554 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
555 // ms apart will be ignored.
556 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000557 if (stats_gathering_started_ != 0 &&
558 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 return;
560 }
561 stats_gathering_started_ = time_now;
562
Steve Anton75737c02017-11-06 10:37:17 -0800563 // TODO(tommi): All of these hop over to the worker thread to fetch
564 // information. We could use an AsyncInvoker to run all of these and post
565 // the information back to the signaling thread where we can create and
566 // update stats reports. That would also clean up the threading story a bit
567 // since we'd be creating/updating the stats report objects consistently on
568 // the same thread (this class has no locks right now).
569 ExtractSessionInfo();
570 ExtractBweInfo();
571 ExtractVoiceInfo();
572 ExtractVideoInfo(level);
573 ExtractSenderInfo();
574 ExtractDataInfo();
575 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576}
577
deadbeefab9b2d12015-10-14 11:33:11 -0700578StatsReport* StatsCollector::PrepareReport(
579 bool local,
580 uint32_t ssrc,
581 const StatsReport::Id& transport_id,
582 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700583 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000584 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200585 local ? StatsReport::kStatsReportTypeSsrc
586 : StatsReport::kStatsReportTypeRemoteSsrc,
587 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000588 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589
xians@webrtc.org01bda202014-07-09 07:38:38 +0000590 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000592 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000593 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000594 // The ssrc is not used by any track or existing report, return NULL
595 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000597 }
598
599 // The ssrc is not used by any existing track. Keeps the old track id
600 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000601 const StatsReport::Value* v =
602 report->FindValue(StatsReport::kStatsValueNameTrackId);
603 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000604 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000607 if (!report)
608 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000610 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000611 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000613 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000614 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000616 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 return report;
618}
619
zhihuange9e94c32016-11-04 11:38:15 -0700620bool StatsCollector::IsValidTrack(const std::string& track_id) {
621 return reports_.Find(StatsReport::NewTypedId(
622 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
623}
624
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000625StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000626 const rtc::SSLCertificate* cert) {
Steve Anton978b8762017-09-29 12:15:02 -0700627 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700628 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000629
hbose29352b2016-08-25 03:52:38 -0700630 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
631 StatsReport* first_report = nullptr;
632 StatsReport* prev_report = nullptr;
633 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
634 stats = stats->issuer.get()) {
635 StatsReport::Id id(StatsReport::NewTypedId(
636 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
637
638 StatsReport* report = reports_.ReplaceOrAddNew(id);
639 report->set_timestamp(stats_gathering_started_);
640 report->AddString(StatsReport::kStatsValueNameFingerprint,
641 stats->fingerprint);
642 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
643 stats->fingerprint_algorithm);
644 report->AddString(StatsReport::kStatsValueNameDer,
645 stats->base64_certificate);
646 if (!first_report)
647 first_report = report;
648 else
649 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
650 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000651 }
hbose29352b2016-08-25 03:52:38 -0700652 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000653}
654
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000655StatsReport* StatsCollector::AddConnectionInfoReport(
656 const std::string& content_name, int component, int connection_id,
657 const StatsReport::Id& channel_report_id,
658 const cricket::ConnectionInfo& info) {
659 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
660 connection_id));
661 StatsReport* report = reports_.ReplaceOrAddNew(id);
662 report->set_timestamp(stats_gathering_started_);
663
664 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700665 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
666 {StatsReport::kStatsValueNameReceiving, info.receiving},
667 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000668 };
669 for (const auto& b : bools)
670 report->AddBoolean(b.name, b.value);
671
672 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
673 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
674 AddCandidateReport(info.local_candidate, true)->id());
675 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
676 AddCandidateReport(info.remote_candidate, false)->id());
677
678 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700679 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
680 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
681 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
682 {StatsReport::kStatsValueNameRtt, info.rtt},
683 {StatsReport::kStatsValueNameSendPacketsDiscarded,
684 info.sent_discarded_packets},
685 {StatsReport::kStatsValueNameSentPingRequestsTotal,
686 info.sent_ping_requests_total},
687 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
688 info.sent_ping_requests_before_first_response},
689 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
690 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
691 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000692 };
693 for (const auto& i : int64s)
694 report->AddInt64(i.name, i.value);
695
696 report->AddString(StatsReport::kStatsValueNameLocalAddress,
697 info.local_candidate.address().ToString());
698 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
699 info.local_candidate.type());
700 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
701 info.remote_candidate.address().ToString());
702 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
703 info.remote_candidate.type());
704 report->AddString(StatsReport::kStatsValueNameTransportType,
705 info.local_candidate.protocol());
706
707 return report;
708}
709
710StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000711 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000712 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000713 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
714 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000715 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000716 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000717 report->set_timestamp(stats_gathering_started_);
718 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000719 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
720 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000721 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000722 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
723 candidate.address().ipaddr().ToString());
724 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
725 candidate.address().PortAsString());
726 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
727 candidate.priority());
728 report->AddString(StatsReport::kStatsValueNameCandidateType,
729 IceCandidateTypeToStatsType(candidate.type()));
730 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
731 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000732 }
733
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000734 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000735}
736
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700738 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000739
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000741 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700742 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000743 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000744 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000745 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700746 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747
Steve Anton978b8762017-09-29 12:15:02 -0700748 std::unique_ptr<SessionStats> stats = pc_->GetSessionStats_s();
hbosdf6075a2016-12-19 04:58:02 -0800749 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000750 return;
751 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000753 // Store the proxy map away for use in SSRC reporting.
754 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
755 // signaling thread after fetching them on the worker thread, then just use
756 // the proxy map directly from the session stats.
757 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
758 // data.
hbosdf6075a2016-12-19 04:58:02 -0800759 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000760
hbosdf6075a2016-12-19 04:58:02 -0800761 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000762 // Attempt to get a copy of the certificates from the transport and
763 // expose them in stats reports. All channels in a transport share the
764 // same local and remote certificates.
765 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000766 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200767 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton978b8762017-09-29 12:15:02 -0700768 if (pc_->GetLocalCertificate(transport_iter.second.transport_name,
769 &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200770 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000771 if (r)
772 local_cert_report_id = r->id();
773 }
774
jbauch555604a2016-04-26 03:13:22 -0700775 std::unique_ptr<rtc::SSLCertificate> cert =
Steve Anton978b8762017-09-29 12:15:02 -0700776 pc_->GetRemoteSSLCertificate(transport_iter.second.transport_name);
kwibergb4d01c42016-04-06 05:15:06 -0700777 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000778 StatsReport* r = AddCertificateReports(cert.get());
779 if (r)
780 remote_cert_report_id = r->id();
781 }
782
783 for (const auto& channel_iter : transport_iter.second.channel_stats) {
784 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700785 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000786 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
787 channel_report->set_timestamp(stats_gathering_started_);
788 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
789 channel_iter.component);
790 if (local_cert_report_id.get()) {
791 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
792 local_cert_report_id);
793 }
794 if (remote_cert_report_id.get()) {
795 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
796 remote_cert_report_id);
797 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800798 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
799 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
800 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
801 channel_report->AddString(
802 StatsReport::kStatsValueNameSrtpCipher,
803 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000804 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800805 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
806 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
807 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
808 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700809 channel_report->AddString(
810 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800811 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000812 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000813
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000814 int connection_id = 0;
815 for (const cricket::ConnectionInfo& info :
816 channel_iter.connection_infos) {
817 StatsReport* connection_report = AddConnectionInfoReport(
818 transport_iter.first, channel_iter.component, connection_id++,
819 channel_report->id(), info);
820 if (info.best_connection) {
821 channel_report->AddId(
822 StatsReport::kStatsValueNameSelectedCandidatePairId,
823 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 }
825 }
826 }
827 }
828}
829
stefanf79ade12017-06-02 06:44:03 -0700830void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700831 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700832
Steve Anton978b8762017-09-29 12:15:02 -0700833 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700834 return;
835
Steve Anton978b8762017-09-29 12:15:02 -0700836 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700837 cricket::BandwidthEstimationInfo bwe_info;
838 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
839 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
840 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
841 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
842 // TODO(holmer): Also fill this in for audio.
Steve Anton978b8762017-09-29 12:15:02 -0700843 if (pc_->video_channel()) {
844 pc_->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700845 }
stefanf79ade12017-06-02 06:44:03 -0700846 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
847 StatsReport* report = reports_.FindOrAddNew(report_id);
848 ExtractStats(bwe_info, stats_gathering_started_, report);
849}
850
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851void StatsCollector::ExtractVoiceInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700852 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000853
Steve Anton978b8762017-09-29 12:15:02 -0700854 if (!pc_->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 return;
856 }
857 cricket::VoiceMediaInfo voice_info;
Steve Anton978b8762017-09-29 12:15:02 -0700858 if (!pc_->voice_channel()->GetStats(&voice_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100859 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 return;
861 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000862
863 // TODO(tommi): The above code should run on the worker thread and post the
864 // results back to the signaling thread, where we can add data to the reports.
865 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
866
deadbeefab9b2d12015-10-14 11:33:11 -0700867 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700868 proxy_to_transport_, pc_->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000869 if (!transport_id.get()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100870 RTC_LOG(LS_ERROR) << "Failed to get transport name for proxy "
871 << pc_->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 return;
873 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000874
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000875 ExtractStatsFromList(voice_info.receivers, transport_id, this,
876 StatsReport::kReceive);
877 ExtractStatsFromList(voice_info.senders, transport_id, this,
878 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000879
880 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881}
882
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000883void StatsCollector::ExtractVideoInfo(
884 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700885 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000886
Steve Anton978b8762017-09-29 12:15:02 -0700887 if (!pc_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000889
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 cricket::VideoMediaInfo video_info;
Steve Anton978b8762017-09-29 12:15:02 -0700891 if (!pc_->video_channel()->GetStats(&video_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100892 RTC_LOG(LS_ERROR) << "Failed to get video channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 return;
894 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000895
896 // TODO(tommi): The above code should run on the worker thread and post the
897 // results back to the signaling thread, where we can add data to the reports.
898 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
899
deadbeefab9b2d12015-10-14 11:33:11 -0700900 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700901 proxy_to_transport_, pc_->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000902 if (!transport_id.get()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100903 RTC_LOG(LS_ERROR) << "Failed to get transport name for proxy "
904 << pc_->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 return;
906 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000907 ExtractStatsFromList(video_info.receivers, transport_id, this,
908 StatsReport::kReceive);
909 ExtractStatsFromList(video_info.senders, transport_id, this,
910 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911}
912
nissefcc640f2016-04-01 01:10:42 -0700913void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700914 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700915
916 for (const auto& sender : pc_->GetSenders()) {
917 // TODO(nisse): SSRC == 0 currently means none. Delete check when
918 // that is fixed.
919 if (!sender->ssrc()) {
920 continue;
921 }
922 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
923 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
924 continue;
925 }
926 // Safe, because kind() == kVideoKind implies a subclass of
927 // VideoTrackInterface; see mediastreaminterface.h.
928 VideoTrackSourceInterface* source =
929 static_cast<VideoTrackInterface*>(track.get())->GetSource();
930
931 VideoTrackSourceInterface::Stats stats;
932 if (!source->GetStats(&stats)) {
933 continue;
934 }
935 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
936 StatsReport::kStatsReportTypeSsrc,
937 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
938 StatsReport* report = reports_.FindOrAddNew(stats_id);
939 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
940 stats.input_width);
941 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
942 stats.input_height);
943 }
944}
945
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000946void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700947 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000948
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000949 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
950
deadbeefab9b2d12015-10-14 11:33:11 -0700951 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000952 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000953 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000954 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000955 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000956 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700957 // Filter out the initial id (-1).
958 if (dc->id() >= 0) {
959 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
960 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000961 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
962 report->AddString(StatsReport::kStatsValueNameState,
963 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000964 }
965}
966
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000967StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000968 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000969 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700970 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700971 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
972 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000973 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000974}
975
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000976void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
Steve Anton978b8762017-09-29 12:15:02 -0700977 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000978 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000979 for (const auto& it : local_audio_tracks_) {
980 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200981 uint32_t ssrc = it.second;
982 StatsReport* report =
983 GetReport(StatsReport::kStatsReportTypeSsrc,
984 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000985 if (report == NULL) {
986 // This can happen if a local audio track is added to a stream on the
987 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100988 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000989 continue;
990 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000991
992 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000993 const StatsReport::Value* v =
994 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000995 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000996 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000997
jbauchbe24c942015-06-22 15:06:43 -0700998 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000999 UpdateReportFromAudioTrack(track, report);
1000 }
1001}
1002
1003void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
1004 StatsReport* report) {
Steve Anton978b8762017-09-29 12:15:02 -07001005 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001006 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001007
andrew2fe1cb02015-11-27 17:27:35 -08001008 // Don't overwrite report values if they're not available.
1009 int signal_level;
1010 if (track->GetSignalLevel(&signal_level)) {
1011 RTC_DCHECK_GE(signal_level, 0);
1012 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1013 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001014
andrew2fe1cb02015-11-27 17:27:35 -08001015 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001016
andrew2fe1cb02015-11-27 17:27:35 -08001017 if (audio_processor.get()) {
1018 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001019 audio_processor->GetStats(&stats);
1020
andrew2fe1cb02015-11-27 17:27:35 -08001021 SetAudioProcessingStats(
1022 report, stats.typing_noise_detected, stats.echo_return_loss,
1023 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -07001024 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -08001025 stats.residual_echo_likelihood,
1026 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +02001027
1028 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
1029 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -08001030 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001031}
1032
Peter Boström0c4e06b2015-10-07 12:23:21 +02001033bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1034 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001035 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001036 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001037 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001038 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001039 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1040 << " is not associated with a sending track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001041 return false;
1042 }
1043 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001044 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001045 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001046 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1047 << " is not associated with a receiving track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001048 return false;
1049 }
1050 }
1051
1052 return true;
1053}
1054
jbauchbe24c942015-06-22 15:06:43 -07001055void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001056 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001057
1058 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1059
1060 for (const auto& entry : track_ids_) {
1061 StatsReport* report = entry.second;
1062 report->set_timestamp(stats_gathering_started_);
1063 }
jbauchbe24c942015-06-22 15:06:43 -07001064}
1065
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001066void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001067 stats_gathering_started_ = 0;
1068}
1069
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070} // namespace webrtc