blob: 919ea464f31eeb7e6efd4451a7bad7cd2910dfce [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>
jbauchbe24c942015-06-22 15:06:43 -070077void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
78 TrackIdMap& track_ids) {
79 for (const auto& track : tracks) {
80 const std::string& track_id = track->id();
81 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070082 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -070083 track_ids[track_id] = report;
84 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085}
86
tommi@webrtc.org92f40182015-03-04 15:25:19 +000087void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
88 StatsReport* report) {
89 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
90 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070091 if (info.rtt_ms >= 0) {
92 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
93 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000094}
95
pbosf42376c2015-08-28 07:35:32 -070096void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
97 StatsReport* report) {
98 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
99}
100
andrew2fe1cb02015-11-27 17:27:35 -0800101void SetAudioProcessingStats(StatsReport* report,
102 bool typing_noise_detected,
103 int echo_return_loss,
104 int echo_return_loss_enhancement,
105 int echo_delay_median_ms,
106 float aec_quality_min,
ivoc8c63a822016-10-21 04:10:03 -0700107 int echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800108 float residual_echo_likelihood,
109 float residual_echo_likelihood_recent_max) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000110 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
111 typing_noise_detected);
zhihuang6ba3b192016-05-13 11:46:35 -0700112 if (aec_quality_min >= 0.0f) {
113 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
114 aec_quality_min);
115 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000116 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000117 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
118 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
119 };
zhihuang6ba3b192016-05-13 11:46:35 -0700120 for (const auto& i : ints) {
121 if (i.value >= 0) {
122 report->AddInt(i.name, i.value);
123 }
124 }
125 // These can take on valid negative values.
126 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss);
127 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
128 echo_return_loss_enhancement);
ivoc8c63a822016-10-21 04:10:03 -0700129 if (residual_echo_likelihood >= 0.0f) {
130 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
131 residual_echo_likelihood);
henrik.lundin04a057b2017-01-16 23:53:59 -0800132 }
133 if (residual_echo_likelihood_recent_max >= 0.0f) {
ivoc4e477a12017-01-15 08:29:46 -0800134 report->AddFloat(
135 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
136 residual_echo_likelihood_recent_max);
ivoc8c63a822016-10-21 04:10:03 -0700137 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700141 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000142 const FloatForAdd floats[] = {
143 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
144 { StatsReport::kStatsValueNameSecondaryDecodedRate,
145 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200146 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
147 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000148 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200149 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
150 { StatsReport::kStatsValueNamePreemptiveExpandRate,
151 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700152 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
153 { StatsReport::kStatsValueNameTotalSamplesDuration,
154 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000155 };
156
157 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000158 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
159 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
160 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
161 { StatsReport::kStatsValueNameDecodingCTSG,
162 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700163 { StatsReport::kStatsValueNameDecodingMutedOutput,
164 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000165 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
166 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
167 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
168 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
169 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
170 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
171 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
172 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
173 info.jitter_buffer_preferred_ms },
174 };
175
176 for (const auto& f : floats)
177 report->AddFloat(f.name, f.value);
178
179 for (const auto& i : ints)
180 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700181 if (info.audio_level >= 0) {
182 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
183 info.audio_level);
184 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000185
186 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700188 if (info.capture_start_ntp_time_ms >= 0) {
189 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
190 info.capture_start_ntp_time_ms);
191 }
fippobec70ab2016-01-28 01:27:15 -0800192 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193}
194
195void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000196 ExtractCommonSendProperties(info, report);
197
andrew2fe1cb02015-11-27 17:27:35 -0800198 SetAudioProcessingStats(
199 report, info.typing_noise_detected, info.echo_return_loss,
200 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700201 info.aec_quality_min, info.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800202 info.residual_echo_likelihood, info.residual_echo_likelihood_recent_max);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203
zsteine76bd3a2017-07-14 12:17:49 -0700204 const FloatForAdd floats[] = {
205 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
206 { StatsReport::kStatsValueNameTotalSamplesDuration,
207 info.total_input_duration }
208 };
209
andrew2fe1cb02015-11-27 17:27:35 -0800210 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000211 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800212 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000213 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
214 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
215 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
216 };
217
zsteine76bd3a2017-07-14 12:17:49 -0700218 for (const auto& f : floats) {
219 report->AddFloat(f.name, f.value);
220 }
221
zhihuang6ba3b192016-05-13 11:46:35 -0700222 for (const auto& i : ints) {
223 if (i.value >= 0) {
224 report->AddInt(i.name, i.value);
225 }
226 }
fippobec70ab2016-01-28 01:27:15 -0800227 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700228 if (info.ana_statistics.bitrate_action_counter) {
229 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
230 *info.ana_statistics.bitrate_action_counter);
231 }
232 if (info.ana_statistics.channel_action_counter) {
233 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
234 *info.ana_statistics.channel_action_counter);
235 }
236 if (info.ana_statistics.dtx_action_counter) {
237 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
238 *info.ana_statistics.dtx_action_counter);
239 }
240 if (info.ana_statistics.fec_action_counter) {
241 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
242 *info.ana_statistics.fec_action_counter);
243 }
ivoc0d0b9122017-09-08 13:24:21 -0700244 if (info.ana_statistics.frame_length_increase_counter) {
245 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
246 *info.ana_statistics.frame_length_increase_counter);
247 }
248 if (info.ana_statistics.frame_length_decrease_counter) {
249 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
250 *info.ana_statistics.frame_length_decrease_counter);
251 }
252 if (info.ana_statistics.uplink_packet_loss_fraction) {
253 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
254 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256}
257
258void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700259 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100260 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
261 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700264 if (info.capture_start_ntp_time_ms >= 0) {
265 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
266 info.capture_start_ntp_time_ms);
267 }
sakalcc452e12017-02-09 04:53:45 -0800268 if (info.qp_sum)
269 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
270
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000271 const IntForAdd ints[] = {
272 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
273 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
274 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
275 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
276 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
277 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
278 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
279 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
280 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
281 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
282 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
283 info.min_playout_delay_ms },
284 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
285 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
286 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
287 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
288 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
289 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700290 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000291 };
292
293 for (const auto& i : ints)
294 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800295 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700296
ilnik2edc6842017-07-06 03:06:50 -0700297 if (info.timing_frame_info) {
298 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
299 info.timing_frame_info->ToString());
300 }
ilnikf04afde2017-07-07 01:26:24 -0700301
ilnika79cc282017-08-23 05:24:10 -0700302 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
303 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700304
305 report->AddString(
306 StatsReport::kStatsValueNameContentType,
307 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308}
309
310void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000311 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312
Peter Boströmb7d9a972015-12-18 16:01:11 +0100313 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
314 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000315 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
316 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000317 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
318 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700319 if (info.qp_sum)
320 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000321
322 const IntForAdd ints[] = {
323 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
324 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000325 { StatsReport::kStatsValueNameEncodeUsagePercent,
326 info.encode_usage_percent },
327 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000328 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
329 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
330 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000331 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
332 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
333 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
334 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
335 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700336 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000337 };
338
339 for (const auto& i : ints)
340 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800341 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700342 report->AddString(
343 StatsReport::kStatsValueNameContentType,
344 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345}
346
347void ExtractStats(const cricket::BandwidthEstimationInfo& info,
348 double stats_gathering_started,
349 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700350 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000352 report->set_timestamp(stats_gathering_started);
353 const IntForAdd ints[] = {
354 { StatsReport::kStatsValueNameAvailableSendBandwidth,
355 info.available_send_bandwidth },
356 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
357 info.available_recv_bandwidth },
358 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
359 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
360 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
361 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
362 };
363 for (const auto& i : ints)
364 report->AddInt(i.name, i.value);
365 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366}
367
wu@webrtc.org97077a32013-10-25 21:18:33 +0000368void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
369 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000370 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000371 // TODO(hta): Extract some stats here.
372}
373
374void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
375 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000376 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000377 // TODO(hta): Extract some stats here.
378}
379
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000381// In order to use the template, the functions that are called from it,
382// ExtractStats and ExtractRemoteStats, must be defined and overloaded
383// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384template<typename T>
385void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000386 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000387 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000388 StatsReport::Direction direction) {
389 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200390 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000391 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000392 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000393 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
394 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000395 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000396 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000397
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000398 if (!d.remote_stats.empty()) {
399 report = collector->PrepareReport(false, ssrc, transport_id, direction);
400 if (report)
401 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000402 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000404}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405
406} // namespace
407
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000408const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
409 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
410 return STATSREPORT_LOCAL_PORT_TYPE;
411 }
412 if (candidate_type == cricket::STUN_PORT_TYPE) {
413 return STATSREPORT_STUN_PORT_TYPE;
414 }
415 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
416 return STATSREPORT_PRFLX_PORT_TYPE;
417 }
418 if (candidate_type == cricket::RELAY_PORT_TYPE) {
419 return STATSREPORT_RELAY_PORT_TYPE;
420 }
nisseeb4ca4e2017-01-12 02:24:27 -0800421 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000422 return "unknown";
423}
424
425const char* AdapterTypeToStatsType(rtc::AdapterType type) {
426 switch (type) {
427 case rtc::ADAPTER_TYPE_UNKNOWN:
428 return "unknown";
429 case rtc::ADAPTER_TYPE_ETHERNET:
430 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
431 case rtc::ADAPTER_TYPE_WIFI:
432 return STATSREPORT_ADAPTER_TYPE_WIFI;
433 case rtc::ADAPTER_TYPE_CELLULAR:
434 return STATSREPORT_ADAPTER_TYPE_WWAN;
435 case rtc::ADAPTER_TYPE_VPN:
436 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000437 case rtc::ADAPTER_TYPE_LOOPBACK:
438 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000439 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800440 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000441 return "";
442 }
443}
444
deadbeefab9b2d12015-10-14 11:33:11 -0700445StatsCollector::StatsCollector(PeerConnection* pc)
446 : pc_(pc), stats_gathering_started_(0) {
447 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000448}
449
450StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700451 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452}
453
nissecdf37a92016-09-13 23:41:47 -0700454// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000455double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700456 return rtc::TimeUTCMicros() /
457 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000458}
459
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460// Adds a MediaStream with tracks that can be used as a |selector| in a call
461// to GetStats.
462void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700463 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700464 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
466 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700467 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700469 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470}
471
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000472void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200473 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700474 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700475 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700476#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000477 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700478 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000479#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000480
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000481 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000482
483 // Create the kStatsReportTypeTrack report for the new track if there is no
484 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000485 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
486 audio_track->id()));
487 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000488 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000489 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000490 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000491 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000492}
493
494void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200495 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700496 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700497 local_audio_tracks_.erase(
498 std::remove_if(
499 local_audio_tracks_.begin(), local_audio_tracks_.end(),
500 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
501 return track.first == audio_track && track.second == ssrc;
502 }),
503 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000504}
505
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000506void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700508 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700509 RTC_DCHECK(reports != NULL);
510 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000512 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
513
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000515 reports->reserve(reports_.size());
516 for (auto* r : reports_)
517 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000518 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 }
520
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000521 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700522 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000523 if (report)
524 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000526 report = reports_.Find(StatsReport::NewTypedId(
527 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000529 if (!report)
530 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000532 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
534 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000535 for (const auto* r : reports_) {
536 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000538
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000539 const StatsReport::Value* v =
540 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000541 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000542 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544}
545
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000546void
547StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700548 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 double time_now = GetTimeNow();
550 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
551 // ms apart will be ignored.
552 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000553 if (stats_gathering_started_ != 0 &&
554 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 return;
556 }
557 stats_gathering_started_ = time_now;
558
solenberg03d6d572016-03-01 12:42:03 -0800559 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
560 // pc_->session().
Steve Anton978b8762017-09-29 12:15:02 -0700561 if (pc_->session_created()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000562 // TODO(tommi): All of these hop over to the worker thread to fetch
563 // information. We could use an AsyncInvoker to run all of these and post
564 // the information back to the signaling thread where we can create and
565 // update stats reports. That would also clean up the threading story a bit
566 // since we'd be creating/updating the stats report objects consistently on
567 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 ExtractSessionInfo();
stefanf79ade12017-06-02 06:44:03 -0700569 ExtractBweInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000571 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700572 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000573 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700574 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 }
576}
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)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 LOG(LS_ERROR) << "Failed to get voice channel stats.";
860 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()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 LOG(LS_ERROR) << "Failed to get transport name for proxy "
Steve Anton978b8762017-09-29 12:15:02 -0700871 << 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)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 LOG(LS_ERROR) << "Failed to get video channel stats.";
893 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()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 LOG(LS_ERROR) << "Failed to get transport name for proxy "
Steve Anton978b8762017-09-29 12:15:02 -0700904 << 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.
988 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
989 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)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001039 LOG(LS_WARNING) << "The SSRC " << ssrc
1040 << " is not associated with a sending track";
1041 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)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001046 LOG(LS_WARNING) << "The SSRC " << ssrc
1047 << " is not associated with a receiving track";
1048 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