blob: 15fe173082e57bdc59c4746c7d6abe3e70957971 [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);
sakal87da4042016-10-31 06:53:47 -0700320 if (info.qp_sum)
321 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000322
323 const IntForAdd ints[] = {
324 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
325 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000326 { StatsReport::kStatsValueNameEncodeUsagePercent,
327 info.encode_usage_percent },
328 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000329 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
330 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
331 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000332 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
333 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
334 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
335 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
336 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700337 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000338 };
339
340 for (const auto& i : ints)
341 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800342 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700343 report->AddString(
344 StatsReport::kStatsValueNameContentType,
345 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346}
347
348void ExtractStats(const cricket::BandwidthEstimationInfo& info,
349 double stats_gathering_started,
350 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700351 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000353 report->set_timestamp(stats_gathering_started);
354 const IntForAdd ints[] = {
355 { StatsReport::kStatsValueNameAvailableSendBandwidth,
356 info.available_send_bandwidth },
357 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
358 info.available_recv_bandwidth },
359 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
360 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
361 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
362 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
363 };
364 for (const auto& i : ints)
365 report->AddInt(i.name, i.value);
366 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367}
368
wu@webrtc.org97077a32013-10-25 21:18:33 +0000369void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
370 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000371 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000372 // TODO(hta): Extract some stats here.
373}
374
375void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
376 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000377 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000378 // TODO(hta): Extract some stats here.
379}
380
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000382// In order to use the template, the functions that are called from it,
383// ExtractStats and ExtractRemoteStats, must be defined and overloaded
384// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385template<typename T>
386void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000387 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000388 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000389 StatsReport::Direction direction) {
390 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200391 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000392 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000393 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000394 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
395 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000396 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000397 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000398
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000399 if (!d.remote_stats.empty()) {
400 report = collector->PrepareReport(false, ssrc, transport_id, direction);
401 if (report)
402 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000403 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000405}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406
407} // namespace
408
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000409const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
410 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
411 return STATSREPORT_LOCAL_PORT_TYPE;
412 }
413 if (candidate_type == cricket::STUN_PORT_TYPE) {
414 return STATSREPORT_STUN_PORT_TYPE;
415 }
416 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
417 return STATSREPORT_PRFLX_PORT_TYPE;
418 }
419 if (candidate_type == cricket::RELAY_PORT_TYPE) {
420 return STATSREPORT_RELAY_PORT_TYPE;
421 }
nisseeb4ca4e2017-01-12 02:24:27 -0800422 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000423 return "unknown";
424}
425
426const char* AdapterTypeToStatsType(rtc::AdapterType type) {
427 switch (type) {
428 case rtc::ADAPTER_TYPE_UNKNOWN:
429 return "unknown";
430 case rtc::ADAPTER_TYPE_ETHERNET:
431 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
432 case rtc::ADAPTER_TYPE_WIFI:
433 return STATSREPORT_ADAPTER_TYPE_WIFI;
434 case rtc::ADAPTER_TYPE_CELLULAR:
435 return STATSREPORT_ADAPTER_TYPE_WWAN;
436 case rtc::ADAPTER_TYPE_VPN:
437 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000438 case rtc::ADAPTER_TYPE_LOOPBACK:
439 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000440 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800441 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000442 return "";
443 }
444}
445
deadbeefab9b2d12015-10-14 11:33:11 -0700446StatsCollector::StatsCollector(PeerConnection* pc)
447 : pc_(pc), stats_gathering_started_(0) {
448 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000449}
450
451StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700452 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453}
454
nissecdf37a92016-09-13 23:41:47 -0700455// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000456double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700457 return rtc::TimeUTCMicros() /
458 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000459}
460
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461// Adds a MediaStream with tracks that can be used as a |selector| in a call
462// to GetStats.
463void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700464 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700465 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466
Steve Anton36b29d12017-10-30 09:57:42 -0700467 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
468 &track_ids_);
469 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
470 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471}
472
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000473void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200474 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700475 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700476 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700477#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000478 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700479 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000480#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000481
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000482 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000483
484 // Create the kStatsReportTypeTrack report for the new track if there is no
485 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000486 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
487 audio_track->id()));
488 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000489 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000490 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000491 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000492 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000493}
494
495void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200496 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700497 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700498 local_audio_tracks_.erase(
499 std::remove_if(
500 local_audio_tracks_.begin(), local_audio_tracks_.end(),
501 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
502 return track.first == audio_track && track.second == ssrc;
503 }),
504 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000505}
506
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000507void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700509 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700510 RTC_DCHECK(reports != NULL);
511 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000513 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
514
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000516 reports->reserve(reports_.size());
517 for (auto* r : reports_)
518 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000519 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 }
521
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000522 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700523 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000524 if (report)
525 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000527 report = reports_.Find(StatsReport::NewTypedId(
528 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000530 if (!report)
531 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000533 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534
535 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000536 for (const auto* r : reports_) {
537 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000539
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000540 const StatsReport::Value* v =
541 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000542 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000543 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000547void
548StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700549 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 double time_now = GetTimeNow();
551 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
552 // ms apart will be ignored.
553 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000554 if (stats_gathering_started_ != 0 &&
555 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 return;
557 }
558 stats_gathering_started_ = time_now;
559
solenberg03d6d572016-03-01 12:42:03 -0800560 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
561 // pc_->session().
Steve Anton978b8762017-09-29 12:15:02 -0700562 if (pc_->session_created()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000563 // 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).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 ExtractSessionInfo();
stefanf79ade12017-06-02 06:44:03 -0700570 ExtractBweInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000572 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700573 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000574 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700575 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 }
577}
578
deadbeefab9b2d12015-10-14 11:33:11 -0700579StatsReport* StatsCollector::PrepareReport(
580 bool local,
581 uint32_t ssrc,
582 const StatsReport::Id& transport_id,
583 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700584 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000585 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200586 local ? StatsReport::kStatsReportTypeSsrc
587 : StatsReport::kStatsReportTypeRemoteSsrc,
588 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000589 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
xians@webrtc.org01bda202014-07-09 07:38:38 +0000591 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000593 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000594 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000595 // The ssrc is not used by any track or existing report, return NULL
596 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000598 }
599
600 // The ssrc is not used by any existing track. Keeps the old track id
601 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000602 const StatsReport::Value* v =
603 report->FindValue(StatsReport::kStatsValueNameTrackId);
604 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000605 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
607
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000608 if (!report)
609 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000611 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000612 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000614 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000615 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000617 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 return report;
619}
620
zhihuange9e94c32016-11-04 11:38:15 -0700621bool StatsCollector::IsValidTrack(const std::string& track_id) {
622 return reports_.Find(StatsReport::NewTypedId(
623 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
624}
625
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000626StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000627 const rtc::SSLCertificate* cert) {
Steve Anton978b8762017-09-29 12:15:02 -0700628 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700629 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000630
hbose29352b2016-08-25 03:52:38 -0700631 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
632 StatsReport* first_report = nullptr;
633 StatsReport* prev_report = nullptr;
634 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
635 stats = stats->issuer.get()) {
636 StatsReport::Id id(StatsReport::NewTypedId(
637 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
638
639 StatsReport* report = reports_.ReplaceOrAddNew(id);
640 report->set_timestamp(stats_gathering_started_);
641 report->AddString(StatsReport::kStatsValueNameFingerprint,
642 stats->fingerprint);
643 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
644 stats->fingerprint_algorithm);
645 report->AddString(StatsReport::kStatsValueNameDer,
646 stats->base64_certificate);
647 if (!first_report)
648 first_report = report;
649 else
650 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
651 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000652 }
hbose29352b2016-08-25 03:52:38 -0700653 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000654}
655
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000656StatsReport* StatsCollector::AddConnectionInfoReport(
657 const std::string& content_name, int component, int connection_id,
658 const StatsReport::Id& channel_report_id,
659 const cricket::ConnectionInfo& info) {
660 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
661 connection_id));
662 StatsReport* report = reports_.ReplaceOrAddNew(id);
663 report->set_timestamp(stats_gathering_started_);
664
665 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700666 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
667 {StatsReport::kStatsValueNameReceiving, info.receiving},
668 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000669 };
670 for (const auto& b : bools)
671 report->AddBoolean(b.name, b.value);
672
673 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
674 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
675 AddCandidateReport(info.local_candidate, true)->id());
676 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
677 AddCandidateReport(info.remote_candidate, false)->id());
678
679 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700680 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
681 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
682 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
683 {StatsReport::kStatsValueNameRtt, info.rtt},
684 {StatsReport::kStatsValueNameSendPacketsDiscarded,
685 info.sent_discarded_packets},
686 {StatsReport::kStatsValueNameSentPingRequestsTotal,
687 info.sent_ping_requests_total},
688 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
689 info.sent_ping_requests_before_first_response},
690 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
691 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
692 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000693 };
694 for (const auto& i : int64s)
695 report->AddInt64(i.name, i.value);
696
697 report->AddString(StatsReport::kStatsValueNameLocalAddress,
698 info.local_candidate.address().ToString());
699 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
700 info.local_candidate.type());
701 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
702 info.remote_candidate.address().ToString());
703 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
704 info.remote_candidate.type());
705 report->AddString(StatsReport::kStatsValueNameTransportType,
706 info.local_candidate.protocol());
707
708 return report;
709}
710
711StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000712 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000713 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000714 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
715 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000716 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000717 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000718 report->set_timestamp(stats_gathering_started_);
719 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000720 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
721 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000722 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000723 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
724 candidate.address().ipaddr().ToString());
725 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
726 candidate.address().PortAsString());
727 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
728 candidate.priority());
729 report->AddString(StatsReport::kStatsValueNameCandidateType,
730 IceCandidateTypeToStatsType(candidate.type()));
731 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
732 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000733 }
734
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000735 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000736}
737
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700739 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000742 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700743 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000744 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000745 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000746 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700747 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748
Steve Anton978b8762017-09-29 12:15:02 -0700749 std::unique_ptr<SessionStats> stats = pc_->GetSessionStats_s();
hbosdf6075a2016-12-19 04:58:02 -0800750 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000751 return;
752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000754 // Store the proxy map away for use in SSRC reporting.
755 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
756 // signaling thread after fetching them on the worker thread, then just use
757 // the proxy map directly from the session stats.
758 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
759 // data.
hbosdf6075a2016-12-19 04:58:02 -0800760 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000761
hbosdf6075a2016-12-19 04:58:02 -0800762 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000763 // Attempt to get a copy of the certificates from the transport and
764 // expose them in stats reports. All channels in a transport share the
765 // same local and remote certificates.
766 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000767 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200768 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton978b8762017-09-29 12:15:02 -0700769 if (pc_->GetLocalCertificate(transport_iter.second.transport_name,
770 &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200771 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000772 if (r)
773 local_cert_report_id = r->id();
774 }
775
jbauch555604a2016-04-26 03:13:22 -0700776 std::unique_ptr<rtc::SSLCertificate> cert =
Steve Anton978b8762017-09-29 12:15:02 -0700777 pc_->GetRemoteSSLCertificate(transport_iter.second.transport_name);
kwibergb4d01c42016-04-06 05:15:06 -0700778 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000779 StatsReport* r = AddCertificateReports(cert.get());
780 if (r)
781 remote_cert_report_id = r->id();
782 }
783
784 for (const auto& channel_iter : transport_iter.second.channel_stats) {
785 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700786 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000787 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
788 channel_report->set_timestamp(stats_gathering_started_);
789 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
790 channel_iter.component);
791 if (local_cert_report_id.get()) {
792 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
793 local_cert_report_id);
794 }
795 if (remote_cert_report_id.get()) {
796 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
797 remote_cert_report_id);
798 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800799 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
800 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
801 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
802 channel_report->AddString(
803 StatsReport::kStatsValueNameSrtpCipher,
804 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000805 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800806 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
807 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
808 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
809 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700810 channel_report->AddString(
811 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800812 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000813 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000814
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000815 int connection_id = 0;
816 for (const cricket::ConnectionInfo& info :
817 channel_iter.connection_infos) {
818 StatsReport* connection_report = AddConnectionInfoReport(
819 transport_iter.first, channel_iter.component, connection_id++,
820 channel_report->id(), info);
821 if (info.best_connection) {
822 channel_report->AddId(
823 StatsReport::kStatsValueNameSelectedCandidatePairId,
824 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 }
826 }
827 }
828 }
829}
830
stefanf79ade12017-06-02 06:44:03 -0700831void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700832 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700833
Steve Anton978b8762017-09-29 12:15:02 -0700834 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700835 return;
836
Steve Anton978b8762017-09-29 12:15:02 -0700837 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700838 cricket::BandwidthEstimationInfo bwe_info;
839 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
840 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
841 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
842 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
843 // TODO(holmer): Also fill this in for audio.
Steve Anton978b8762017-09-29 12:15:02 -0700844 if (pc_->video_channel()) {
845 pc_->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700846 }
stefanf79ade12017-06-02 06:44:03 -0700847 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
848 StatsReport* report = reports_.FindOrAddNew(report_id);
849 ExtractStats(bwe_info, stats_gathering_started_, report);
850}
851
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852void StatsCollector::ExtractVoiceInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700853 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000854
Steve Anton978b8762017-09-29 12:15:02 -0700855 if (!pc_->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 return;
857 }
858 cricket::VoiceMediaInfo voice_info;
Steve Anton978b8762017-09-29 12:15:02 -0700859 if (!pc_->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 LOG(LS_ERROR) << "Failed to get voice channel stats.";
861 return;
862 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000863
864 // TODO(tommi): The above code should run on the worker thread and post the
865 // results back to the signaling thread, where we can add data to the reports.
866 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
867
deadbeefab9b2d12015-10-14 11:33:11 -0700868 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700869 proxy_to_transport_, pc_->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000870 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 LOG(LS_ERROR) << "Failed to get transport name for proxy "
Steve Anton978b8762017-09-29 12:15:02 -0700872 << pc_->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 return;
874 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000875
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000876 ExtractStatsFromList(voice_info.receivers, transport_id, this,
877 StatsReport::kReceive);
878 ExtractStatsFromList(voice_info.senders, transport_id, this,
879 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000880
881 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882}
883
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000884void StatsCollector::ExtractVideoInfo(
885 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700886 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000887
Steve Anton978b8762017-09-29 12:15:02 -0700888 if (!pc_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000890
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 cricket::VideoMediaInfo video_info;
Steve Anton978b8762017-09-29 12:15:02 -0700892 if (!pc_->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 LOG(LS_ERROR) << "Failed to get video channel stats.";
894 return;
895 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000896
897 // TODO(tommi): The above code should run on the worker thread and post the
898 // results back to the signaling thread, where we can add data to the reports.
899 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
900
deadbeefab9b2d12015-10-14 11:33:11 -0700901 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700902 proxy_to_transport_, pc_->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000903 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 LOG(LS_ERROR) << "Failed to get transport name for proxy "
Steve Anton978b8762017-09-29 12:15:02 -0700905 << pc_->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 return;
907 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000908 ExtractStatsFromList(video_info.receivers, transport_id, this,
909 StatsReport::kReceive);
910 ExtractStatsFromList(video_info.senders, transport_id, this,
911 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912}
913
nissefcc640f2016-04-01 01:10:42 -0700914void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700915 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700916
917 for (const auto& sender : pc_->GetSenders()) {
918 // TODO(nisse): SSRC == 0 currently means none. Delete check when
919 // that is fixed.
920 if (!sender->ssrc()) {
921 continue;
922 }
923 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
924 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
925 continue;
926 }
927 // Safe, because kind() == kVideoKind implies a subclass of
928 // VideoTrackInterface; see mediastreaminterface.h.
929 VideoTrackSourceInterface* source =
930 static_cast<VideoTrackInterface*>(track.get())->GetSource();
931
932 VideoTrackSourceInterface::Stats stats;
933 if (!source->GetStats(&stats)) {
934 continue;
935 }
936 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
937 StatsReport::kStatsReportTypeSsrc,
938 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
939 StatsReport* report = reports_.FindOrAddNew(stats_id);
940 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
941 stats.input_width);
942 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
943 stats.input_height);
944 }
945}
946
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000947void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700948 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000949
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000950 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
951
deadbeefab9b2d12015-10-14 11:33:11 -0700952 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000953 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000954 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000955 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000956 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000957 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700958 // Filter out the initial id (-1).
959 if (dc->id() >= 0) {
960 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
961 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000962 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
963 report->AddString(StatsReport::kStatsValueNameState,
964 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000965 }
966}
967
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000968StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000969 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000970 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700971 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700972 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
973 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000974 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000975}
976
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000977void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
Steve Anton978b8762017-09-29 12:15:02 -0700978 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000979 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000980 for (const auto& it : local_audio_tracks_) {
981 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200982 uint32_t ssrc = it.second;
983 StatsReport* report =
984 GetReport(StatsReport::kStatsReportTypeSsrc,
985 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000986 if (report == NULL) {
987 // This can happen if a local audio track is added to a stream on the
988 // fly and the report has not been set up yet. Do nothing in this case.
989 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
990 continue;
991 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000992
993 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000994 const StatsReport::Value* v =
995 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000996 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000997 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000998
jbauchbe24c942015-06-22 15:06:43 -0700999 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001000 UpdateReportFromAudioTrack(track, report);
1001 }
1002}
1003
1004void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
1005 StatsReport* report) {
Steve Anton978b8762017-09-29 12:15:02 -07001006 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001007 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001008
andrew2fe1cb02015-11-27 17:27:35 -08001009 // Don't overwrite report values if they're not available.
1010 int signal_level;
1011 if (track->GetSignalLevel(&signal_level)) {
1012 RTC_DCHECK_GE(signal_level, 0);
1013 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1014 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001015
andrew2fe1cb02015-11-27 17:27:35 -08001016 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001017
andrew2fe1cb02015-11-27 17:27:35 -08001018 if (audio_processor.get()) {
1019 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001020 audio_processor->GetStats(&stats);
1021
andrew2fe1cb02015-11-27 17:27:35 -08001022 SetAudioProcessingStats(
1023 report, stats.typing_noise_detected, stats.echo_return_loss,
1024 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -07001025 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -08001026 stats.residual_echo_likelihood,
1027 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +02001028
1029 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
1030 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -08001031 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001032}
1033
Peter Boström0c4e06b2015-10-07 12:23:21 +02001034bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1035 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001036 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001037 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001038 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001039 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001040 LOG(LS_WARNING) << "The SSRC " << ssrc
1041 << " is not associated with a sending track";
1042 return false;
1043 }
1044 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001045 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001046 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001047 LOG(LS_WARNING) << "The SSRC " << ssrc
1048 << " is not associated with a receiving track";
1049 return false;
1050 }
1051 }
1052
1053 return true;
1054}
1055
jbauchbe24c942015-06-22 15:06:43 -07001056void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001057 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001058
1059 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1060
1061 for (const auto& entry : track_ids_) {
1062 StatsReport* report = entry.second;
1063 report->set_timestamp(stats_gathering_started_);
1064 }
jbauchbe24c942015-06-22 15:06:43 -07001065}
1066
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001067void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001068 stats_gathering_started_ = 0;
1069}
1070
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071} // namespace webrtc