blob: b96428353aae280c83bdd301a204491dd1f60168 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/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
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010017#include "webrtc/pc/channel.h"
ossu7bb87ee2017-01-23 04:56:25 -080018#include "webrtc/pc/peerconnection.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/base64.h"
20#include "webrtc/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
deadbeefd59daf82015-10-14 15:02:44 -070053StatsReport::Id GetTransportIdFromProxy(const ProxyTransportMap& map,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000054 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070055 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070056 auto found = map.find(proxy);
57 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000058 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070059 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000060
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061 return StatsReport::NewComponentId(
62 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000063}
64
jbauchbe24c942015-06-22 15:06:43 -070065StatsReport* AddTrackReport(StatsCollection* reports,
66 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000067 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000068 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000069 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000070 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000071 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070072 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000073}
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075template <class TrackVector>
jbauchbe24c942015-06-22 15:06:43 -070076void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
77 TrackIdMap& track_ids) {
78 for (const auto& track : tracks) {
79 const std::string& track_id = track->id();
80 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070081 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -070082 track_ids[track_id] = report;
83 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084}
85
tommi@webrtc.org92f40182015-03-04 15:25:19 +000086void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
87 StatsReport* report) {
88 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
89 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070090 if (info.rtt_ms >= 0) {
91 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
92 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000093}
94
pbosf42376c2015-08-28 07:35:32 -070095void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
96 StatsReport* report) {
97 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
98}
99
andrew2fe1cb02015-11-27 17:27:35 -0800100void SetAudioProcessingStats(StatsReport* report,
101 bool typing_noise_detected,
102 int echo_return_loss,
103 int echo_return_loss_enhancement,
104 int echo_delay_median_ms,
105 float aec_quality_min,
ivoc8c63a822016-10-21 04:10:03 -0700106 int echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800107 float residual_echo_likelihood,
108 float residual_echo_likelihood_recent_max) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000109 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
110 typing_noise_detected);
zhihuang6ba3b192016-05-13 11:46:35 -0700111 if (aec_quality_min >= 0.0f) {
112 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
113 aec_quality_min);
114 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000115 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000116 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
117 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
118 };
zhihuang6ba3b192016-05-13 11:46:35 -0700119 for (const auto& i : ints) {
120 if (i.value >= 0) {
121 report->AddInt(i.name, i.value);
122 }
123 }
124 // These can take on valid negative values.
125 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss);
126 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
127 echo_return_loss_enhancement);
ivoc8c63a822016-10-21 04:10:03 -0700128 if (residual_echo_likelihood >= 0.0f) {
129 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
130 residual_echo_likelihood);
henrik.lundin04a057b2017-01-16 23:53:59 -0800131 }
132 if (residual_echo_likelihood_recent_max >= 0.0f) {
ivoc4e477a12017-01-15 08:29:46 -0800133 report->AddFloat(
134 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
135 residual_echo_likelihood_recent_max);
ivoc8c63a822016-10-21 04:10:03 -0700136 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700140 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000141 const FloatForAdd floats[] = {
142 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
143 { StatsReport::kStatsValueNameSecondaryDecodedRate,
144 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200145 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
146 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000147 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200148 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
149 { StatsReport::kStatsValueNamePreemptiveExpandRate,
150 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700151 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
152 { StatsReport::kStatsValueNameTotalSamplesDuration,
153 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000154 };
155
156 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000157 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
158 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
159 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
160 { StatsReport::kStatsValueNameDecodingCTSG,
161 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700162 { StatsReport::kStatsValueNameDecodingMutedOutput,
163 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000164 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
165 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
166 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
167 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
168 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
169 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
170 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
171 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
172 info.jitter_buffer_preferred_ms },
173 };
174
175 for (const auto& f : floats)
176 report->AddFloat(f.name, f.value);
177
178 for (const auto& i : ints)
179 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700180 if (info.audio_level >= 0) {
181 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
182 info.audio_level);
183 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184
185 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700187 if (info.capture_start_ntp_time_ms >= 0) {
188 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
189 info.capture_start_ntp_time_ms);
190 }
fippobec70ab2016-01-28 01:27:15 -0800191 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
194void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000195 ExtractCommonSendProperties(info, report);
196
andrew2fe1cb02015-11-27 17:27:35 -0800197 SetAudioProcessingStats(
198 report, info.typing_noise_detected, info.echo_return_loss,
199 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700200 info.aec_quality_min, info.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800201 info.residual_echo_likelihood, info.residual_echo_likelihood_recent_max);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000202
zsteine76bd3a2017-07-14 12:17:49 -0700203 const FloatForAdd floats[] = {
204 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
205 { StatsReport::kStatsValueNameTotalSamplesDuration,
206 info.total_input_duration }
207 };
208
andrew2fe1cb02015-11-27 17:27:35 -0800209 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000210 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800211 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000212 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
213 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
214 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
215 };
216
zsteine76bd3a2017-07-14 12:17:49 -0700217 for (const auto& f : floats) {
218 report->AddFloat(f.name, f.value);
219 }
220
zhihuang6ba3b192016-05-13 11:46:35 -0700221 for (const auto& i : ints) {
222 if (i.value >= 0) {
223 report->AddInt(i.name, i.value);
224 }
225 }
fippobec70ab2016-01-28 01:27:15 -0800226 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700227 if (info.ana_statistics.bitrate_action_counter) {
228 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
229 *info.ana_statistics.bitrate_action_counter);
230 }
231 if (info.ana_statistics.channel_action_counter) {
232 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
233 *info.ana_statistics.channel_action_counter);
234 }
235 if (info.ana_statistics.dtx_action_counter) {
236 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
237 *info.ana_statistics.dtx_action_counter);
238 }
239 if (info.ana_statistics.fec_action_counter) {
240 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
241 *info.ana_statistics.fec_action_counter);
242 }
243 if (info.ana_statistics.frame_length_action_counter) {
244 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthActionCounter,
245 *info.ana_statistics.frame_length_action_counter);
246 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247}
248
249void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700250 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100251 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
252 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000253 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700255 if (info.capture_start_ntp_time_ms >= 0) {
256 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
257 info.capture_start_ntp_time_ms);
258 }
sakalcc452e12017-02-09 04:53:45 -0800259 if (info.qp_sum)
260 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
261
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262 const IntForAdd ints[] = {
263 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
264 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
265 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
266 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
267 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
268 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
269 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
270 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
271 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
272 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
273 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
274 info.min_playout_delay_ms },
275 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
276 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
277 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
278 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
279 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
280 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700281 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000282 };
283
284 for (const auto& i : ints)
285 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800286 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700287
ilnik2edc6842017-07-06 03:06:50 -0700288 if (info.timing_frame_info) {
289 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
290 info.timing_frame_info->ToString());
291 }
ilnikf04afde2017-07-07 01:26:24 -0700292
ilnika79cc282017-08-23 05:24:10 -0700293 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
294 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700295
296 report->AddString(
297 StatsReport::kStatsValueNameContentType,
298 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299}
300
301void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000302 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
Peter Boströmb7d9a972015-12-18 16:01:11 +0100304 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
305 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000306 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
307 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000308 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
309 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700310 if (info.qp_sum)
311 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000312
313 const IntForAdd ints[] = {
314 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
315 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000316 { StatsReport::kStatsValueNameEncodeUsagePercent,
317 info.encode_usage_percent },
318 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000319 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
320 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
321 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000322 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
323 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
324 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
325 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
326 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700327 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000328 };
329
330 for (const auto& i : ints)
331 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800332 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700333 report->AddString(
334 StatsReport::kStatsValueNameContentType,
335 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336}
337
338void ExtractStats(const cricket::BandwidthEstimationInfo& info,
339 double stats_gathering_started,
340 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700341 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000343 report->set_timestamp(stats_gathering_started);
344 const IntForAdd ints[] = {
345 { StatsReport::kStatsValueNameAvailableSendBandwidth,
346 info.available_send_bandwidth },
347 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
348 info.available_recv_bandwidth },
349 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
350 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
351 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
352 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
353 };
354 for (const auto& i : ints)
355 report->AddInt(i.name, i.value);
356 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357}
358
wu@webrtc.org97077a32013-10-25 21:18:33 +0000359void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
360 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000361 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000362 // TODO(hta): Extract some stats here.
363}
364
365void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
366 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000367 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000368 // TODO(hta): Extract some stats here.
369}
370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000372// In order to use the template, the functions that are called from it,
373// ExtractStats and ExtractRemoteStats, must be defined and overloaded
374// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375template<typename T>
376void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000377 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000378 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000379 StatsReport::Direction direction) {
380 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200381 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000382 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000383 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000384 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
385 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000386 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000387 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000388
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000389 if (!d.remote_stats.empty()) {
390 report = collector->PrepareReport(false, ssrc, transport_id, direction);
391 if (report)
392 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000393 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000395}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396
397} // namespace
398
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000399const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
400 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
401 return STATSREPORT_LOCAL_PORT_TYPE;
402 }
403 if (candidate_type == cricket::STUN_PORT_TYPE) {
404 return STATSREPORT_STUN_PORT_TYPE;
405 }
406 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
407 return STATSREPORT_PRFLX_PORT_TYPE;
408 }
409 if (candidate_type == cricket::RELAY_PORT_TYPE) {
410 return STATSREPORT_RELAY_PORT_TYPE;
411 }
nisseeb4ca4e2017-01-12 02:24:27 -0800412 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000413 return "unknown";
414}
415
416const char* AdapterTypeToStatsType(rtc::AdapterType type) {
417 switch (type) {
418 case rtc::ADAPTER_TYPE_UNKNOWN:
419 return "unknown";
420 case rtc::ADAPTER_TYPE_ETHERNET:
421 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
422 case rtc::ADAPTER_TYPE_WIFI:
423 return STATSREPORT_ADAPTER_TYPE_WIFI;
424 case rtc::ADAPTER_TYPE_CELLULAR:
425 return STATSREPORT_ADAPTER_TYPE_WWAN;
426 case rtc::ADAPTER_TYPE_VPN:
427 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000428 case rtc::ADAPTER_TYPE_LOOPBACK:
429 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000430 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800431 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000432 return "";
433 }
434}
435
deadbeefab9b2d12015-10-14 11:33:11 -0700436StatsCollector::StatsCollector(PeerConnection* pc)
437 : pc_(pc), stats_gathering_started_(0) {
438 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000439}
440
441StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700442 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443}
444
nissecdf37a92016-09-13 23:41:47 -0700445// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000446double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700447 return rtc::TimeUTCMicros() /
448 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000449}
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451// Adds a MediaStream with tracks that can be used as a |selector| in a call
452// to GetStats.
453void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700454 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700455 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456
457 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700458 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700460 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461}
462
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000463void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200464 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700465 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700466 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700467#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000468 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700469 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000470#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000471
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000472 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000473
474 // Create the kStatsReportTypeTrack report for the new track if there is no
475 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000476 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
477 audio_track->id()));
478 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000479 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000480 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000481 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000482 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000483}
484
485void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200486 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700487 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700488 local_audio_tracks_.erase(
489 std::remove_if(
490 local_audio_tracks_.begin(), local_audio_tracks_.end(),
491 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
492 return track.first == audio_track && track.second == ssrc;
493 }),
494 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000495}
496
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000497void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700499 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700500 RTC_DCHECK(reports != NULL);
501 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000503 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
504
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000506 reports->reserve(reports_.size());
507 for (auto* r : reports_)
508 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000509 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 }
511
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000512 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700513 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000514 if (report)
515 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000517 report = reports_.Find(StatsReport::NewTypedId(
518 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000520 if (!report)
521 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000523 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
525 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000526 for (const auto* r : reports_) {
527 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000529
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000530 const StatsReport::Value* v =
531 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000532 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000533 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535}
536
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000537void
538StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700539 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 double time_now = GetTimeNow();
541 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
542 // ms apart will be ignored.
543 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000544 if (stats_gathering_started_ != 0 &&
545 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 return;
547 }
548 stats_gathering_started_ = time_now;
549
solenberg03d6d572016-03-01 12:42:03 -0800550 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
551 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700552 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000553 // TODO(tommi): All of these hop over to the worker thread to fetch
554 // information. We could use an AsyncInvoker to run all of these and post
555 // the information back to the signaling thread where we can create and
556 // update stats reports. That would also clean up the threading story a bit
557 // since we'd be creating/updating the stats report objects consistently on
558 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 ExtractSessionInfo();
stefanf79ade12017-06-02 06:44:03 -0700560 ExtractBweInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000562 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700563 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000564 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700565 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 }
567}
568
deadbeefab9b2d12015-10-14 11:33:11 -0700569StatsReport* StatsCollector::PrepareReport(
570 bool local,
571 uint32_t ssrc,
572 const StatsReport::Id& transport_id,
573 StatsReport::Direction direction) {
574 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000575 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200576 local ? StatsReport::kStatsReportTypeSsrc
577 : StatsReport::kStatsReportTypeRemoteSsrc,
578 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000579 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
xians@webrtc.org01bda202014-07-09 07:38:38 +0000581 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000583 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000584 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000585 // The ssrc is not used by any track or existing report, return NULL
586 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000588 }
589
590 // The ssrc is not used by any existing track. Keeps the old track id
591 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000592 const StatsReport::Value* v =
593 report->FindValue(StatsReport::kStatsValueNameTrackId);
594 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000595 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 }
597
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000598 if (!report)
599 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000601 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000602 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000604 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000605 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000607 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 return report;
609}
610
zhihuange9e94c32016-11-04 11:38:15 -0700611bool StatsCollector::IsValidTrack(const std::string& track_id) {
612 return reports_.Find(StatsReport::NewTypedId(
613 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
614}
615
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000616StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000617 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700618 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700619 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000620
hbose29352b2016-08-25 03:52:38 -0700621 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
622 StatsReport* first_report = nullptr;
623 StatsReport* prev_report = nullptr;
624 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
625 stats = stats->issuer.get()) {
626 StatsReport::Id id(StatsReport::NewTypedId(
627 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
628
629 StatsReport* report = reports_.ReplaceOrAddNew(id);
630 report->set_timestamp(stats_gathering_started_);
631 report->AddString(StatsReport::kStatsValueNameFingerprint,
632 stats->fingerprint);
633 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
634 stats->fingerprint_algorithm);
635 report->AddString(StatsReport::kStatsValueNameDer,
636 stats->base64_certificate);
637 if (!first_report)
638 first_report = report;
639 else
640 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
641 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000642 }
hbose29352b2016-08-25 03:52:38 -0700643 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000644}
645
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000646StatsReport* StatsCollector::AddConnectionInfoReport(
647 const std::string& content_name, int component, int connection_id,
648 const StatsReport::Id& channel_report_id,
649 const cricket::ConnectionInfo& info) {
650 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
651 connection_id));
652 StatsReport* report = reports_.ReplaceOrAddNew(id);
653 report->set_timestamp(stats_gathering_started_);
654
655 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700656 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
657 {StatsReport::kStatsValueNameReceiving, info.receiving},
658 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000659 };
660 for (const auto& b : bools)
661 report->AddBoolean(b.name, b.value);
662
663 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
664 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
665 AddCandidateReport(info.local_candidate, true)->id());
666 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
667 AddCandidateReport(info.remote_candidate, false)->id());
668
669 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700670 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
671 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
672 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
673 {StatsReport::kStatsValueNameRtt, info.rtt},
674 {StatsReport::kStatsValueNameSendPacketsDiscarded,
675 info.sent_discarded_packets},
676 {StatsReport::kStatsValueNameSentPingRequestsTotal,
677 info.sent_ping_requests_total},
678 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
679 info.sent_ping_requests_before_first_response},
680 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
681 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
682 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000683 };
684 for (const auto& i : int64s)
685 report->AddInt64(i.name, i.value);
686
687 report->AddString(StatsReport::kStatsValueNameLocalAddress,
688 info.local_candidate.address().ToString());
689 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
690 info.local_candidate.type());
691 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
692 info.remote_candidate.address().ToString());
693 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
694 info.remote_candidate.type());
695 report->AddString(StatsReport::kStatsValueNameTransportType,
696 info.local_candidate.protocol());
697
698 return report;
699}
700
701StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000702 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000703 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000704 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
705 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000706 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000707 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000708 report->set_timestamp(stats_gathering_started_);
709 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000710 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
711 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000712 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000713 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
714 candidate.address().ipaddr().ToString());
715 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
716 candidate.address().PortAsString());
717 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
718 candidate.priority());
719 report->AddString(StatsReport::kStatsValueNameCandidateType,
720 IceCandidateTypeToStatsType(candidate.type()));
721 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
722 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000723 }
724
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000725 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000726}
727
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700729 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000730
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000732 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700733 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000734 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000735 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000736 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700737 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738
hbosdf6075a2016-12-19 04:58:02 -0800739 std::unique_ptr<SessionStats> stats = pc_->session()->GetStats_s();
740 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000741 return;
742 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000744 // Store the proxy map away for use in SSRC reporting.
745 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
746 // signaling thread after fetching them on the worker thread, then just use
747 // the proxy map directly from the session stats.
748 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
749 // data.
hbosdf6075a2016-12-19 04:58:02 -0800750 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000751
hbosdf6075a2016-12-19 04:58:02 -0800752 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000753 // Attempt to get a copy of the certificates from the transport and
754 // expose them in stats reports. All channels in a transport share the
755 // same local and remote certificates.
756 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000757 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200758 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700759 if (pc_->session()->GetLocalCertificate(
760 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200761 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000762 if (r)
763 local_cert_report_id = r->id();
764 }
765
jbauch555604a2016-04-26 03:13:22 -0700766 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700767 pc_->session()->GetRemoteSSLCertificate(
768 transport_iter.second.transport_name);
769 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000770 StatsReport* r = AddCertificateReports(cert.get());
771 if (r)
772 remote_cert_report_id = r->id();
773 }
774
775 for (const auto& channel_iter : transport_iter.second.channel_stats) {
776 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700777 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000778 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
779 channel_report->set_timestamp(stats_gathering_started_);
780 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
781 channel_iter.component);
782 if (local_cert_report_id.get()) {
783 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
784 local_cert_report_id);
785 }
786 if (remote_cert_report_id.get()) {
787 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
788 remote_cert_report_id);
789 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800790 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
791 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
792 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
793 channel_report->AddString(
794 StatsReport::kStatsValueNameSrtpCipher,
795 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000796 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800797 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
798 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
799 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
800 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700801 channel_report->AddString(
802 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800803 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000804 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000805
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000806 int connection_id = 0;
807 for (const cricket::ConnectionInfo& info :
808 channel_iter.connection_infos) {
809 StatsReport* connection_report = AddConnectionInfoReport(
810 transport_iter.first, channel_iter.component, connection_id++,
811 channel_report->id(), info);
812 if (info.best_connection) {
813 channel_report->AddId(
814 StatsReport::kStatsValueNameSelectedCandidatePairId,
815 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817 }
818 }
819 }
820}
821
stefanf79ade12017-06-02 06:44:03 -0700822void StatsCollector::ExtractBweInfo() {
823 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
824
825 if (pc_->session()->state() == WebRtcSession::State::STATE_CLOSED)
826 return;
827
828 webrtc::Call::Stats call_stats = pc_->session()->GetCallStats();
829 cricket::BandwidthEstimationInfo bwe_info;
830 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
831 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
832 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
833 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
834 // TODO(holmer): Also fill this in for audio.
Alex Narest42308f62017-06-19 17:58:12 +0200835 if (pc_->session()->video_channel()) {
836 pc_->session()->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700837 }
stefanf79ade12017-06-02 06:44:03 -0700838 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
839 StatsReport* report = reports_.FindOrAddNew(report_id);
840 ExtractStats(bwe_info, stats_gathering_started_, report);
841}
842
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700844 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000845
deadbeefab9b2d12015-10-14 11:33:11 -0700846 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 return;
848 }
849 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700850 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 LOG(LS_ERROR) << "Failed to get voice channel stats.";
852 return;
853 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000854
855 // TODO(tommi): The above code should run on the worker thread and post the
856 // results back to the signaling thread, where we can add data to the reports.
857 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
858
deadbeefab9b2d12015-10-14 11:33:11 -0700859 StatsReport::Id transport_id(GetTransportIdFromProxy(
860 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000861 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700863 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 return;
865 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000866
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000867 ExtractStatsFromList(voice_info.receivers, transport_id, this,
868 StatsReport::kReceive);
869 ExtractStatsFromList(voice_info.senders, transport_id, this,
870 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000871
872 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873}
874
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000875void StatsCollector::ExtractVideoInfo(
876 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700877 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000878
deadbeefab9b2d12015-10-14 11:33:11 -0700879 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000881
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700883 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 LOG(LS_ERROR) << "Failed to get video channel stats.";
885 return;
886 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000887
888 // TODO(tommi): The above code should run on the worker thread and post the
889 // results back to the signaling thread, where we can add data to the reports.
890 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
891
deadbeefab9b2d12015-10-14 11:33:11 -0700892 StatsReport::Id transport_id(GetTransportIdFromProxy(
893 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000894 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700896 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 return;
898 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000899 ExtractStatsFromList(video_info.receivers, transport_id, this,
900 StatsReport::kReceive);
901 ExtractStatsFromList(video_info.senders, transport_id, this,
902 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903}
904
nissefcc640f2016-04-01 01:10:42 -0700905void StatsCollector::ExtractSenderInfo() {
906 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
907
908 for (const auto& sender : pc_->GetSenders()) {
909 // TODO(nisse): SSRC == 0 currently means none. Delete check when
910 // that is fixed.
911 if (!sender->ssrc()) {
912 continue;
913 }
914 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
915 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
916 continue;
917 }
918 // Safe, because kind() == kVideoKind implies a subclass of
919 // VideoTrackInterface; see mediastreaminterface.h.
920 VideoTrackSourceInterface* source =
921 static_cast<VideoTrackInterface*>(track.get())->GetSource();
922
923 VideoTrackSourceInterface::Stats stats;
924 if (!source->GetStats(&stats)) {
925 continue;
926 }
927 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
928 StatsReport::kStatsReportTypeSsrc,
929 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
930 StatsReport* report = reports_.FindOrAddNew(stats_id);
931 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
932 stats.input_width);
933 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
934 stats.input_height);
935 }
936}
937
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000938void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700939 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000940
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000941 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
942
deadbeefab9b2d12015-10-14 11:33:11 -0700943 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000944 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000945 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000946 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000947 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000948 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700949 // Filter out the initial id (-1).
950 if (dc->id() >= 0) {
951 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
952 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000953 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
954 report->AddString(StatsReport::kStatsValueNameState,
955 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000956 }
957}
958
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000959StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000960 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000961 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700962 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700963 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
964 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000965 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000966}
967
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000968void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700969 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000970 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000971 for (const auto& it : local_audio_tracks_) {
972 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200973 uint32_t ssrc = it.second;
974 StatsReport* report =
975 GetReport(StatsReport::kStatsReportTypeSsrc,
976 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000977 if (report == NULL) {
978 // This can happen if a local audio track is added to a stream on the
979 // fly and the report has not been set up yet. Do nothing in this case.
980 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
981 continue;
982 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000983
984 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000985 const StatsReport::Value* v =
986 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000987 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000988 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000989
jbauchbe24c942015-06-22 15:06:43 -0700990 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000991 UpdateReportFromAudioTrack(track, report);
992 }
993}
994
995void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
996 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700997 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700998 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000999
andrew2fe1cb02015-11-27 17:27:35 -08001000 // Don't overwrite report values if they're not available.
1001 int signal_level;
1002 if (track->GetSignalLevel(&signal_level)) {
1003 RTC_DCHECK_GE(signal_level, 0);
1004 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1005 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001006
andrew2fe1cb02015-11-27 17:27:35 -08001007 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001008
andrew2fe1cb02015-11-27 17:27:35 -08001009 if (audio_processor.get()) {
1010 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001011 audio_processor->GetStats(&stats);
1012
andrew2fe1cb02015-11-27 17:27:35 -08001013 SetAudioProcessingStats(
1014 report, stats.typing_noise_detected, stats.echo_return_loss,
1015 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -07001016 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -08001017 stats.residual_echo_likelihood,
1018 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +02001019
1020 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
1021 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -08001022 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001023}
1024
Peter Boström0c4e06b2015-10-07 12:23:21 +02001025bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1026 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001027 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -07001028 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001029 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -07001030 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001031 LOG(LS_WARNING) << "The SSRC " << ssrc
1032 << " is not associated with a sending track";
1033 return false;
1034 }
1035 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001036 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -07001037 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001038 LOG(LS_WARNING) << "The SSRC " << ssrc
1039 << " is not associated with a receiving track";
1040 return false;
1041 }
1042 }
1043
1044 return true;
1045}
1046
jbauchbe24c942015-06-22 15:06:43 -07001047void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -07001048 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001049
1050 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1051
1052 for (const auto& entry : track_ids_) {
1053 StatsReport* report = entry.second;
1054 report->set_timestamp(stats_gathering_started_);
1055 }
jbauchbe24c942015-06-22 15:06:43 -07001056}
1057
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001058void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001059 stats_gathering_started_ = 0;
1060}
1061
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062} // namespace webrtc