blob: a14967a5e3f227ec91ce7d4e11bb717d3506c0d2 [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 }
ivoc0d0b9122017-09-08 13:24:21 -0700243 if (info.ana_statistics.frame_length_increase_counter) {
244 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
245 *info.ana_statistics.frame_length_increase_counter);
246 }
247 if (info.ana_statistics.frame_length_decrease_counter) {
248 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
249 *info.ana_statistics.frame_length_decrease_counter);
250 }
251 if (info.ana_statistics.uplink_packet_loss_fraction) {
252 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
253 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255}
256
257void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700258 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100259 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
260 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000261 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700263 if (info.capture_start_ntp_time_ms >= 0) {
264 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
265 info.capture_start_ntp_time_ms);
266 }
sakalcc452e12017-02-09 04:53:45 -0800267 if (info.qp_sum)
268 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
269
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000270 const IntForAdd ints[] = {
271 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
272 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
273 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
274 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
275 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
276 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
277 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
278 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
279 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
280 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
281 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
282 info.min_playout_delay_ms },
283 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
284 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
285 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
286 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
287 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
288 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700289 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000290 };
291
292 for (const auto& i : ints)
293 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800294 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700295
ilnik2edc6842017-07-06 03:06:50 -0700296 if (info.timing_frame_info) {
297 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
298 info.timing_frame_info->ToString());
299 }
ilnikf04afde2017-07-07 01:26:24 -0700300
ilnika79cc282017-08-23 05:24:10 -0700301 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
302 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700303
304 report->AddString(
305 StatsReport::kStatsValueNameContentType,
306 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307}
308
309void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000310 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311
Peter Boströmb7d9a972015-12-18 16:01:11 +0100312 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
313 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000314 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
315 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000316 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
317 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700318 if (info.qp_sum)
319 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000320
321 const IntForAdd ints[] = {
322 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
323 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000324 { StatsReport::kStatsValueNameEncodeUsagePercent,
325 info.encode_usage_percent },
326 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000327 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
328 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
329 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000330 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
331 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
332 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
333 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
334 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700335 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000336 };
337
338 for (const auto& i : ints)
339 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800340 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700341 report->AddString(
342 StatsReport::kStatsValueNameContentType,
343 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344}
345
346void ExtractStats(const cricket::BandwidthEstimationInfo& info,
347 double stats_gathering_started,
348 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700349 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000351 report->set_timestamp(stats_gathering_started);
352 const IntForAdd ints[] = {
353 { StatsReport::kStatsValueNameAvailableSendBandwidth,
354 info.available_send_bandwidth },
355 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
356 info.available_recv_bandwidth },
357 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
358 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
359 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
360 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
361 };
362 for (const auto& i : ints)
363 report->AddInt(i.name, i.value);
364 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365}
366
wu@webrtc.org97077a32013-10-25 21:18:33 +0000367void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
368 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000369 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000370 // TODO(hta): Extract some stats here.
371}
372
373void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
374 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000375 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000376 // TODO(hta): Extract some stats here.
377}
378
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000380// In order to use the template, the functions that are called from it,
381// ExtractStats and ExtractRemoteStats, must be defined and overloaded
382// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383template<typename T>
384void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000385 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000386 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000387 StatsReport::Direction direction) {
388 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200389 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000390 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000391 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000392 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
393 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000394 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000395 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000396
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000397 if (!d.remote_stats.empty()) {
398 report = collector->PrepareReport(false, ssrc, transport_id, direction);
399 if (report)
400 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000401 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000403}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404
405} // namespace
406
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000407const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
408 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
409 return STATSREPORT_LOCAL_PORT_TYPE;
410 }
411 if (candidate_type == cricket::STUN_PORT_TYPE) {
412 return STATSREPORT_STUN_PORT_TYPE;
413 }
414 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
415 return STATSREPORT_PRFLX_PORT_TYPE;
416 }
417 if (candidate_type == cricket::RELAY_PORT_TYPE) {
418 return STATSREPORT_RELAY_PORT_TYPE;
419 }
nisseeb4ca4e2017-01-12 02:24:27 -0800420 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000421 return "unknown";
422}
423
424const char* AdapterTypeToStatsType(rtc::AdapterType type) {
425 switch (type) {
426 case rtc::ADAPTER_TYPE_UNKNOWN:
427 return "unknown";
428 case rtc::ADAPTER_TYPE_ETHERNET:
429 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
430 case rtc::ADAPTER_TYPE_WIFI:
431 return STATSREPORT_ADAPTER_TYPE_WIFI;
432 case rtc::ADAPTER_TYPE_CELLULAR:
433 return STATSREPORT_ADAPTER_TYPE_WWAN;
434 case rtc::ADAPTER_TYPE_VPN:
435 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000436 case rtc::ADAPTER_TYPE_LOOPBACK:
437 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000438 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800439 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000440 return "";
441 }
442}
443
deadbeefab9b2d12015-10-14 11:33:11 -0700444StatsCollector::StatsCollector(PeerConnection* pc)
445 : pc_(pc), stats_gathering_started_(0) {
446 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000447}
448
449StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700450 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
nissecdf37a92016-09-13 23:41:47 -0700453// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000454double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700455 return rtc::TimeUTCMicros() /
456 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000457}
458
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459// Adds a MediaStream with tracks that can be used as a |selector| in a call
460// to GetStats.
461void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700462 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700463 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700466 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700468 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000471void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200472 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700473 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700474 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700475#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000476 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700477 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000478#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000479
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000480 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000481
482 // Create the kStatsReportTypeTrack report for the new track if there is no
483 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000484 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
485 audio_track->id()));
486 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000487 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000488 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000489 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000490 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000491}
492
493void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200494 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700495 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700496 local_audio_tracks_.erase(
497 std::remove_if(
498 local_audio_tracks_.begin(), local_audio_tracks_.end(),
499 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
500 return track.first == audio_track && track.second == ssrc;
501 }),
502 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000503}
504
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000505void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700507 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700508 RTC_DCHECK(reports != NULL);
509 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000511 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
512
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000514 reports->reserve(reports_.size());
515 for (auto* r : reports_)
516 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000517 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 }
519
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000520 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700521 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000522 if (report)
523 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000525 report = reports_.Find(StatsReport::NewTypedId(
526 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000528 if (!report)
529 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000531 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
533 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000534 for (const auto* r : reports_) {
535 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000537
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000538 const StatsReport::Value* v =
539 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000540 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000541 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543}
544
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000545void
546StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700547 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 double time_now = GetTimeNow();
549 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
550 // ms apart will be ignored.
551 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000552 if (stats_gathering_started_ != 0 &&
553 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 return;
555 }
556 stats_gathering_started_ = time_now;
557
solenberg03d6d572016-03-01 12:42:03 -0800558 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
559 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700560 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000561 // TODO(tommi): All of these hop over to the worker thread to fetch
562 // information. We could use an AsyncInvoker to run all of these and post
563 // the information back to the signaling thread where we can create and
564 // update stats reports. That would also clean up the threading story a bit
565 // since we'd be creating/updating the stats report objects consistently on
566 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 ExtractSessionInfo();
stefanf79ade12017-06-02 06:44:03 -0700568 ExtractBweInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000570 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700571 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000572 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700573 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 }
575}
576
deadbeefab9b2d12015-10-14 11:33:11 -0700577StatsReport* StatsCollector::PrepareReport(
578 bool local,
579 uint32_t ssrc,
580 const StatsReport::Id& transport_id,
581 StatsReport::Direction direction) {
582 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000583 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200584 local ? StatsReport::kStatsReportTypeSsrc
585 : StatsReport::kStatsReportTypeRemoteSsrc,
586 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000587 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588
xians@webrtc.org01bda202014-07-09 07:38:38 +0000589 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000591 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000592 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000593 // The ssrc is not used by any track or existing report, return NULL
594 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000596 }
597
598 // The ssrc is not used by any existing track. Keeps the old track id
599 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000600 const StatsReport::Value* v =
601 report->FindValue(StatsReport::kStatsValueNameTrackId);
602 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000603 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 }
605
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000606 if (!report)
607 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000609 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000610 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000612 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000613 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000615 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 return report;
617}
618
zhihuange9e94c32016-11-04 11:38:15 -0700619bool StatsCollector::IsValidTrack(const std::string& track_id) {
620 return reports_.Find(StatsReport::NewTypedId(
621 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
622}
623
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000624StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000625 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700626 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700627 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000628
hbose29352b2016-08-25 03:52:38 -0700629 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
630 StatsReport* first_report = nullptr;
631 StatsReport* prev_report = nullptr;
632 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
633 stats = stats->issuer.get()) {
634 StatsReport::Id id(StatsReport::NewTypedId(
635 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
636
637 StatsReport* report = reports_.ReplaceOrAddNew(id);
638 report->set_timestamp(stats_gathering_started_);
639 report->AddString(StatsReport::kStatsValueNameFingerprint,
640 stats->fingerprint);
641 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
642 stats->fingerprint_algorithm);
643 report->AddString(StatsReport::kStatsValueNameDer,
644 stats->base64_certificate);
645 if (!first_report)
646 first_report = report;
647 else
648 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
649 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000650 }
hbose29352b2016-08-25 03:52:38 -0700651 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000652}
653
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000654StatsReport* StatsCollector::AddConnectionInfoReport(
655 const std::string& content_name, int component, int connection_id,
656 const StatsReport::Id& channel_report_id,
657 const cricket::ConnectionInfo& info) {
658 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
659 connection_id));
660 StatsReport* report = reports_.ReplaceOrAddNew(id);
661 report->set_timestamp(stats_gathering_started_);
662
663 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700664 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
665 {StatsReport::kStatsValueNameReceiving, info.receiving},
666 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000667 };
668 for (const auto& b : bools)
669 report->AddBoolean(b.name, b.value);
670
671 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
672 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
673 AddCandidateReport(info.local_candidate, true)->id());
674 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
675 AddCandidateReport(info.remote_candidate, false)->id());
676
677 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700678 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
679 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
680 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
681 {StatsReport::kStatsValueNameRtt, info.rtt},
682 {StatsReport::kStatsValueNameSendPacketsDiscarded,
683 info.sent_discarded_packets},
684 {StatsReport::kStatsValueNameSentPingRequestsTotal,
685 info.sent_ping_requests_total},
686 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
687 info.sent_ping_requests_before_first_response},
688 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
689 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
690 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000691 };
692 for (const auto& i : int64s)
693 report->AddInt64(i.name, i.value);
694
695 report->AddString(StatsReport::kStatsValueNameLocalAddress,
696 info.local_candidate.address().ToString());
697 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
698 info.local_candidate.type());
699 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
700 info.remote_candidate.address().ToString());
701 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
702 info.remote_candidate.type());
703 report->AddString(StatsReport::kStatsValueNameTransportType,
704 info.local_candidate.protocol());
705
706 return report;
707}
708
709StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000710 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000711 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000712 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
713 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000714 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000715 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000716 report->set_timestamp(stats_gathering_started_);
717 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000718 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
719 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000720 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000721 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
722 candidate.address().ipaddr().ToString());
723 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
724 candidate.address().PortAsString());
725 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
726 candidate.priority());
727 report->AddString(StatsReport::kStatsValueNameCandidateType,
728 IceCandidateTypeToStatsType(candidate.type()));
729 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
730 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000731 }
732
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000733 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000734}
735
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700737 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000738
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000740 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700741 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000742 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000743 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000744 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700745 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
hbosdf6075a2016-12-19 04:58:02 -0800747 std::unique_ptr<SessionStats> stats = pc_->session()->GetStats_s();
748 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000749 return;
750 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000752 // Store the proxy map away for use in SSRC reporting.
753 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
754 // signaling thread after fetching them on the worker thread, then just use
755 // the proxy map directly from the session stats.
756 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
757 // data.
hbosdf6075a2016-12-19 04:58:02 -0800758 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000759
hbosdf6075a2016-12-19 04:58:02 -0800760 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000761 // Attempt to get a copy of the certificates from the transport and
762 // expose them in stats reports. All channels in a transport share the
763 // same local and remote certificates.
764 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000765 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200766 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700767 if (pc_->session()->GetLocalCertificate(
768 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200769 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000770 if (r)
771 local_cert_report_id = r->id();
772 }
773
jbauch555604a2016-04-26 03:13:22 -0700774 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700775 pc_->session()->GetRemoteSSLCertificate(
776 transport_iter.second.transport_name);
777 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() {
831 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
832
833 if (pc_->session()->state() == WebRtcSession::State::STATE_CLOSED)
834 return;
835
836 webrtc::Call::Stats call_stats = pc_->session()->GetCallStats();
837 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.
Alex Narest42308f62017-06-19 17:58:12 +0200843 if (pc_->session()->video_channel()) {
844 pc_->session()->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() {
deadbeefab9b2d12015-10-14 11:33:11 -0700852 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000853
deadbeefab9b2d12015-10-14 11:33:11 -0700854 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 return;
856 }
857 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700858 if (!pc_->session()->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(
868 proxy_to_transport_, pc_->session()->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 "
deadbeefab9b2d12015-10-14 11:33:11 -0700871 << pc_->session()->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) {
deadbeefab9b2d12015-10-14 11:33:11 -0700885 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000886
deadbeefab9b2d12015-10-14 11:33:11 -0700887 if (!pc_->session()->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;
deadbeefab9b2d12015-10-14 11:33:11 -0700891 if (!pc_->session()->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(
901 proxy_to_transport_, pc_->session()->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 "
deadbeefab9b2d12015-10-14 11:33:11 -0700904 << pc_->session()->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() {
914 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
915
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() {
deadbeefab9b2d12015-10-14 11:33:11 -0700947 RTC_DCHECK(pc_->session()->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) {
deadbeefab9b2d12015-10-14 11:33:11 -0700970 RTC_DCHECK(pc_->session()->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() {
deadbeefab9b2d12015-10-14 11:33:11 -0700977 RTC_DCHECK(pc_->session()->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) {
deadbeefab9b2d12015-10-14 11:33:11 -07001005 RTC_DCHECK(pc_->session()->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) {
deadbeefab9b2d12015-10-14 11:33:11 -07001036 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001037 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -07001038 if (!pc_->session()->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);
deadbeefab9b2d12015-10-14 11:33:11 -07001045 if (!pc_->session()->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() {
deadbeefab9b2d12015-10-14 11:33:11 -07001056 RTC_DCHECK(pc_->session()->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