blob: 0344c988fabfb7fefb0e449d531490ac39d5e8dd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/statscollector.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <utility>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "pc/channel.h"
18#include "pc/peerconnection.h"
19#include "rtc_base/base64.h"
20#include "rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000025// The following is the enum RTCStatsIceCandidateType from
26// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
27// our stats report for ice candidate type could conform to that.
28const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
29const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
30const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
31const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
32
33// Strings used by the stats collector to report adapter types. This fits the
34// general stype of http://w3c.github.io/webrtc-stats than what
35// AdapterTypeToString does.
36const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
37const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
38const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
39const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000040const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000041
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000042template<typename ValueType>
43struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000044 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000046};
47
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000048typedef TypeForAdd<bool> BoolForAdd;
49typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020050typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000051typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000052
Steve Antond5585ca2017-10-23 14:49:26 -070053StatsReport::Id GetTransportIdFromProxy(
54 const std::map<std::string, std::string>& map,
55 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070056 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070057 auto found = map.find(proxy);
58 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000059 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070060 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000061
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000062 return StatsReport::NewComponentId(
63 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000064}
65
jbauchbe24c942015-06-22 15:06:43 -070066StatsReport* AddTrackReport(StatsCollection* reports,
67 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000068 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000069 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000070 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000071 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000072 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070073 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000074}
75
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076template <class TrackVector>
Steve Anton36b29d12017-10-30 09:57:42 -070077void CreateTrackReports(const TrackVector& tracks,
78 StatsCollection* reports,
79 TrackIdMap* track_ids) {
jbauchbe24c942015-06-22 15:06:43 -070080 for (const auto& track : tracks) {
81 const std::string& track_id = track->id();
82 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070083 RTC_DCHECK(report != nullptr);
Steve Anton36b29d12017-10-30 09:57:42 -070084 (*track_ids)[track_id] = report;
jbauchbe24c942015-06-22 15:06:43 -070085 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086}
87
tommi@webrtc.org92f40182015-03-04 15:25:19 +000088void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
89 StatsReport* report) {
90 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
91 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070092 if (info.rtt_ms >= 0) {
93 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
94 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000095}
96
pbosf42376c2015-08-28 07:35:32 -070097void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
98 StatsReport* report) {
99 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
100}
101
Ivo Creusen56d46092017-11-24 17:29:59 +0100102void SetAudioProcessingStats(StatsReport* report,
103 bool typing_noise_detected,
104 const AudioProcessingStats& apm_stats) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000105 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
106 typing_noise_detected);
Ivo Creusen56d46092017-11-24 17:29:59 +0100107 if (apm_stats.delay_median_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100108 report->AddInt(StatsReport::kStatsValueNameEchoDelayMedian,
Ivo Creusen56d46092017-11-24 17:29:59 +0100109 *apm_stats.delay_median_ms);
Ivo Creusenae026092017-11-20 13:07:16 +0100110 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100111 if (apm_stats.delay_standard_deviation_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100112 report->AddInt(StatsReport::kStatsValueNameEchoDelayStdDev,
Ivo Creusen56d46092017-11-24 17:29:59 +0100113 *apm_stats.delay_standard_deviation_ms);
zhihuang6ba3b192016-05-13 11:46:35 -0700114 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100115 if (apm_stats.echo_return_loss) {
Ivo Creusenae026092017-11-20 13:07:16 +0100116 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss,
Ivo Creusen56d46092017-11-24 17:29:59 +0100117 *apm_stats.echo_return_loss);
henrik.lundin04a057b2017-01-16 23:53:59 -0800118 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100119 if (apm_stats.echo_return_loss_enhancement) {
Ivo Creusenae026092017-11-20 13:07:16 +0100120 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
Ivo Creusen56d46092017-11-24 17:29:59 +0100121 *apm_stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +0100122 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100123 if (apm_stats.residual_echo_likelihood) {
Ivo Creusenae026092017-11-20 13:07:16 +0100124 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
Ivo Creusen56d46092017-11-24 17:29:59 +0100125 static_cast<float>(*apm_stats.residual_echo_likelihood));
Ivo Creusenae026092017-11-20 13:07:16 +0100126 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100127 if (apm_stats.residual_echo_likelihood_recent_max) {
ivoc4e477a12017-01-15 08:29:46 -0800128 report->AddFloat(
129 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
Ivo Creusen56d46092017-11-24 17:29:59 +0100130 static_cast<float>(*apm_stats.residual_echo_likelihood_recent_max));
131 }
132 if (apm_stats.divergent_filter_fraction) {
133 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
134 static_cast<float>(*apm_stats.divergent_filter_fraction));
ivoc8c63a822016-10-21 04:10:03 -0700135 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000136}
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700139 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000140 const FloatForAdd floats[] = {
141 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
142 { StatsReport::kStatsValueNameSecondaryDecodedRate,
143 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200144 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
145 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000146 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200147 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
148 { StatsReport::kStatsValueNamePreemptiveExpandRate,
149 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700150 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
151 { StatsReport::kStatsValueNameTotalSamplesDuration,
152 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000153 };
154
155 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000156 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
157 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
158 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
159 { StatsReport::kStatsValueNameDecodingCTSG,
160 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700161 { StatsReport::kStatsValueNameDecodingMutedOutput,
162 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000163 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
164 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
165 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
166 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
167 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
168 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
169 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
170 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
171 info.jitter_buffer_preferred_ms },
172 };
173
174 for (const auto& f : floats)
175 report->AddFloat(f.name, f.value);
176
177 for (const auto& i : ints)
178 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700179 if (info.audio_level >= 0) {
180 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
181 info.audio_level);
182 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000183
184 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700186 if (info.capture_start_ntp_time_ms >= 0) {
187 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
188 info.capture_start_ntp_time_ms);
189 }
fippobec70ab2016-01-28 01:27:15 -0800190 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191}
192
193void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000194 ExtractCommonSendProperties(info, report);
195
Ivo Creusen56d46092017-11-24 17:29:59 +0100196 SetAudioProcessingStats(report, info.typing_noise_detected,
197 info.apm_statistics);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000198
zsteine76bd3a2017-07-14 12:17:49 -0700199 const FloatForAdd floats[] = {
200 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
201 { StatsReport::kStatsValueNameTotalSamplesDuration,
202 info.total_input_duration }
203 };
204
andrew2fe1cb02015-11-27 17:27:35 -0800205 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000206 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800207 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000208 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
209 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
210 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
211 };
212
zsteine76bd3a2017-07-14 12:17:49 -0700213 for (const auto& f : floats) {
214 report->AddFloat(f.name, f.value);
215 }
216
zhihuang6ba3b192016-05-13 11:46:35 -0700217 for (const auto& i : ints) {
218 if (i.value >= 0) {
219 report->AddInt(i.name, i.value);
220 }
221 }
fippobec70ab2016-01-28 01:27:15 -0800222 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700223 if (info.ana_statistics.bitrate_action_counter) {
224 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
225 *info.ana_statistics.bitrate_action_counter);
226 }
227 if (info.ana_statistics.channel_action_counter) {
228 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
229 *info.ana_statistics.channel_action_counter);
230 }
231 if (info.ana_statistics.dtx_action_counter) {
232 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
233 *info.ana_statistics.dtx_action_counter);
234 }
235 if (info.ana_statistics.fec_action_counter) {
236 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
237 *info.ana_statistics.fec_action_counter);
238 }
ivoc0d0b9122017-09-08 13:24:21 -0700239 if (info.ana_statistics.frame_length_increase_counter) {
240 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
241 *info.ana_statistics.frame_length_increase_counter);
242 }
243 if (info.ana_statistics.frame_length_decrease_counter) {
244 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
245 *info.ana_statistics.frame_length_decrease_counter);
246 }
247 if (info.ana_statistics.uplink_packet_loss_fraction) {
248 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
249 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251}
252
253void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700254 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100255 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
256 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000257 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700259 if (info.capture_start_ntp_time_ms >= 0) {
260 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
261 info.capture_start_ntp_time_ms);
262 }
sakalcc452e12017-02-09 04:53:45 -0800263 if (info.qp_sum)
264 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
265
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000266 const IntForAdd ints[] = {
267 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
268 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
269 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
270 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
271 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
272 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
273 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
274 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
275 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
276 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
277 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
278 info.min_playout_delay_ms },
279 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
280 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
281 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
282 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
283 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
284 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700285 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000286 };
287
288 for (const auto& i : ints)
289 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800290 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700291
ilnik2edc6842017-07-06 03:06:50 -0700292 if (info.timing_frame_info) {
293 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
294 info.timing_frame_info->ToString());
295 }
ilnikf04afde2017-07-07 01:26:24 -0700296
ilnika79cc282017-08-23 05:24:10 -0700297 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
298 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700299
300 report->AddString(
301 StatsReport::kStatsValueNameContentType,
302 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303}
304
305void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000306 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307
Peter Boströmb7d9a972015-12-18 16:01:11 +0100308 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
309 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000310 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
311 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000312 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
313 (info.adapt_reason & 0x1) > 0);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100314 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
315 info.has_entered_low_resolution);
316
sakal87da4042016-10-31 06:53:47 -0700317 if (info.qp_sum)
318 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000319
320 const IntForAdd ints[] = {
321 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
322 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000323 { StatsReport::kStatsValueNameEncodeUsagePercent,
324 info.encode_usage_percent },
325 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000326 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
327 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
328 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000329 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
330 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
331 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
332 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
333 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700334 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000335 };
336
337 for (const auto& i : ints)
338 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800339 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700340 report->AddString(
341 StatsReport::kStatsValueNameContentType,
342 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343}
344
345void ExtractStats(const cricket::BandwidthEstimationInfo& info,
346 double stats_gathering_started,
347 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700348 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000350 report->set_timestamp(stats_gathering_started);
351 const IntForAdd ints[] = {
352 { StatsReport::kStatsValueNameAvailableSendBandwidth,
353 info.available_send_bandwidth },
354 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
355 info.available_recv_bandwidth },
356 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
357 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
358 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
359 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
360 };
361 for (const auto& i : ints)
362 report->AddInt(i.name, i.value);
363 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364}
365
wu@webrtc.org97077a32013-10-25 21:18:33 +0000366void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
367 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000368 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000369 // TODO(hta): Extract some stats here.
370}
371
372void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
373 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000374 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000375 // TODO(hta): Extract some stats here.
376}
377
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000379// In order to use the template, the functions that are called from it,
380// ExtractStats and ExtractRemoteStats, must be defined and overloaded
381// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382template<typename T>
383void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000384 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000385 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000386 StatsReport::Direction direction) {
387 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200388 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000389 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000390 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000391 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
392 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000393 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000394 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000395
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000396 if (!d.remote_stats.empty()) {
397 report = collector->PrepareReport(false, ssrc, transport_id, direction);
398 if (report)
399 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000400 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000402}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403
404} // namespace
405
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000406const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
407 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
408 return STATSREPORT_LOCAL_PORT_TYPE;
409 }
410 if (candidate_type == cricket::STUN_PORT_TYPE) {
411 return STATSREPORT_STUN_PORT_TYPE;
412 }
413 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
414 return STATSREPORT_PRFLX_PORT_TYPE;
415 }
416 if (candidate_type == cricket::RELAY_PORT_TYPE) {
417 return STATSREPORT_RELAY_PORT_TYPE;
418 }
nisseeb4ca4e2017-01-12 02:24:27 -0800419 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000420 return "unknown";
421}
422
423const char* AdapterTypeToStatsType(rtc::AdapterType type) {
424 switch (type) {
425 case rtc::ADAPTER_TYPE_UNKNOWN:
426 return "unknown";
427 case rtc::ADAPTER_TYPE_ETHERNET:
428 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
429 case rtc::ADAPTER_TYPE_WIFI:
430 return STATSREPORT_ADAPTER_TYPE_WIFI;
431 case rtc::ADAPTER_TYPE_CELLULAR:
432 return STATSREPORT_ADAPTER_TYPE_WWAN;
433 case rtc::ADAPTER_TYPE_VPN:
434 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000435 case rtc::ADAPTER_TYPE_LOOPBACK:
436 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000437 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800438 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000439 return "";
440 }
441}
442
deadbeefab9b2d12015-10-14 11:33:11 -0700443StatsCollector::StatsCollector(PeerConnection* pc)
444 : pc_(pc), stats_gathering_started_(0) {
445 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000446}
447
448StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700449 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450}
451
nissecdf37a92016-09-13 23:41:47 -0700452// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000453double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700454 return rtc::TimeUTCMicros() /
455 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000456}
457
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458// Adds a MediaStream with tracks that can be used as a |selector| in a call
459// to GetStats.
460void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700461 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700462 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463
Steve Anton36b29d12017-10-30 09:57:42 -0700464 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
465 &track_ids_);
466 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
467 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468}
469
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000470void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200471 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700472 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700473 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700474#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000475 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700476 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000477#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000478
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000479 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000480
481 // Create the kStatsReportTypeTrack report for the new track if there is no
482 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000483 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
484 audio_track->id()));
485 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000486 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000487 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000488 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000489 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000490}
491
492void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200493 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700494 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700495 local_audio_tracks_.erase(
496 std::remove_if(
497 local_audio_tracks_.begin(), local_audio_tracks_.end(),
498 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
499 return track.first == audio_track && track.second == ssrc;
500 }),
501 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000502}
503
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000504void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700506 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700507 RTC_DCHECK(reports != NULL);
508 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000510 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
511
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000513 reports->reserve(reports_.size());
514 for (auto* r : reports_)
515 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000516 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 }
518
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000519 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700520 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000521 if (report)
522 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000524 report = reports_.Find(StatsReport::NewTypedId(
525 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000527 if (!report)
528 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000530 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
532 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000533 for (const auto* r : reports_) {
534 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000536
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000537 const StatsReport::Value* v =
538 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000539 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000540 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542}
543
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000544void
545StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700546 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 double time_now = GetTimeNow();
548 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
549 // ms apart will be ignored.
550 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000551 if (stats_gathering_started_ != 0 &&
552 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 return;
554 }
555 stats_gathering_started_ = time_now;
556
Steve Anton75737c02017-11-06 10:37:17 -0800557 // TODO(tommi): All of these hop over to the worker thread to fetch
558 // information. We could use an AsyncInvoker to run all of these and post
559 // the information back to the signaling thread where we can create and
560 // update stats reports. That would also clean up the threading story a bit
561 // since we'd be creating/updating the stats report objects consistently on
562 // the same thread (this class has no locks right now).
563 ExtractSessionInfo();
564 ExtractBweInfo();
565 ExtractVoiceInfo();
566 ExtractVideoInfo(level);
567 ExtractSenderInfo();
568 ExtractDataInfo();
569 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570}
571
deadbeefab9b2d12015-10-14 11:33:11 -0700572StatsReport* StatsCollector::PrepareReport(
573 bool local,
574 uint32_t ssrc,
575 const StatsReport::Id& transport_id,
576 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700577 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000578 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200579 local ? StatsReport::kStatsReportTypeSsrc
580 : StatsReport::kStatsReportTypeRemoteSsrc,
581 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000582 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583
xians@webrtc.org01bda202014-07-09 07:38:38 +0000584 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000586 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000587 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000588 // The ssrc is not used by any track or existing report, return NULL
589 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000591 }
592
593 // The ssrc is not used by any existing track. Keeps the old track id
594 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000595 const StatsReport::Value* v =
596 report->FindValue(StatsReport::kStatsValueNameTrackId);
597 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000598 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 }
600
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000601 if (!report)
602 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000604 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000605 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000607 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000608 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000610 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 return report;
612}
613
zhihuange9e94c32016-11-04 11:38:15 -0700614bool StatsCollector::IsValidTrack(const std::string& track_id) {
615 return reports_.Find(StatsReport::NewTypedId(
616 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
617}
618
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000619StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000620 const rtc::SSLCertificate* cert) {
Steve Anton978b8762017-09-29 12:15:02 -0700621 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700622 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000623
hbose29352b2016-08-25 03:52:38 -0700624 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
625 StatsReport* first_report = nullptr;
626 StatsReport* prev_report = nullptr;
627 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
628 stats = stats->issuer.get()) {
629 StatsReport::Id id(StatsReport::NewTypedId(
630 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
631
632 StatsReport* report = reports_.ReplaceOrAddNew(id);
633 report->set_timestamp(stats_gathering_started_);
634 report->AddString(StatsReport::kStatsValueNameFingerprint,
635 stats->fingerprint);
636 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
637 stats->fingerprint_algorithm);
638 report->AddString(StatsReport::kStatsValueNameDer,
639 stats->base64_certificate);
640 if (!first_report)
641 first_report = report;
642 else
643 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
644 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000645 }
hbose29352b2016-08-25 03:52:38 -0700646 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000647}
648
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000649StatsReport* StatsCollector::AddConnectionInfoReport(
650 const std::string& content_name, int component, int connection_id,
651 const StatsReport::Id& channel_report_id,
652 const cricket::ConnectionInfo& info) {
653 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
654 connection_id));
655 StatsReport* report = reports_.ReplaceOrAddNew(id);
656 report->set_timestamp(stats_gathering_started_);
657
658 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700659 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
660 {StatsReport::kStatsValueNameReceiving, info.receiving},
661 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000662 };
663 for (const auto& b : bools)
664 report->AddBoolean(b.name, b.value);
665
666 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
667 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
668 AddCandidateReport(info.local_candidate, true)->id());
669 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
670 AddCandidateReport(info.remote_candidate, false)->id());
671
672 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700673 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
674 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
675 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
676 {StatsReport::kStatsValueNameRtt, info.rtt},
677 {StatsReport::kStatsValueNameSendPacketsDiscarded,
678 info.sent_discarded_packets},
679 {StatsReport::kStatsValueNameSentPingRequestsTotal,
680 info.sent_ping_requests_total},
681 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
682 info.sent_ping_requests_before_first_response},
683 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
684 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
685 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000686 };
687 for (const auto& i : int64s)
688 report->AddInt64(i.name, i.value);
689
690 report->AddString(StatsReport::kStatsValueNameLocalAddress,
691 info.local_candidate.address().ToString());
692 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
693 info.local_candidate.type());
694 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
695 info.remote_candidate.address().ToString());
696 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
697 info.remote_candidate.type());
698 report->AddString(StatsReport::kStatsValueNameTransportType,
699 info.local_candidate.protocol());
700
701 return report;
702}
703
704StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000705 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000706 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000707 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
708 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000709 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000710 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000711 report->set_timestamp(stats_gathering_started_);
712 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000713 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
714 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000715 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000716 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
717 candidate.address().ipaddr().ToString());
718 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
719 candidate.address().PortAsString());
720 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
721 candidate.priority());
722 report->AddString(StatsReport::kStatsValueNameCandidateType,
723 IceCandidateTypeToStatsType(candidate.type()));
724 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
725 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000726 }
727
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000728 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000729}
730
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700732 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000733
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000735 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700736 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000737 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000738 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000739 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700740 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741
Steve Anton978b8762017-09-29 12:15:02 -0700742 std::unique_ptr<SessionStats> stats = pc_->GetSessionStats_s();
hbosdf6075a2016-12-19 04:58:02 -0800743 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000744 return;
745 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000747 // Store the proxy map away for use in SSRC reporting.
748 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
749 // signaling thread after fetching them on the worker thread, then just use
750 // the proxy map directly from the session stats.
751 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
752 // data.
hbosdf6075a2016-12-19 04:58:02 -0800753 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000754
hbosdf6075a2016-12-19 04:58:02 -0800755 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000756 // Attempt to get a copy of the certificates from the transport and
757 // expose them in stats reports. All channels in a transport share the
758 // same local and remote certificates.
759 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000760 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200761 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton978b8762017-09-29 12:15:02 -0700762 if (pc_->GetLocalCertificate(transport_iter.second.transport_name,
763 &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200764 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000765 if (r)
766 local_cert_report_id = r->id();
767 }
768
jbauch555604a2016-04-26 03:13:22 -0700769 std::unique_ptr<rtc::SSLCertificate> cert =
Steve Anton978b8762017-09-29 12:15:02 -0700770 pc_->GetRemoteSSLCertificate(transport_iter.second.transport_name);
kwibergb4d01c42016-04-06 05:15:06 -0700771 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000772 StatsReport* r = AddCertificateReports(cert.get());
773 if (r)
774 remote_cert_report_id = r->id();
775 }
776
777 for (const auto& channel_iter : transport_iter.second.channel_stats) {
778 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700779 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000780 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
781 channel_report->set_timestamp(stats_gathering_started_);
782 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
783 channel_iter.component);
784 if (local_cert_report_id.get()) {
785 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
786 local_cert_report_id);
787 }
788 if (remote_cert_report_id.get()) {
789 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
790 remote_cert_report_id);
791 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800792 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
793 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
794 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
795 channel_report->AddString(
796 StatsReport::kStatsValueNameSrtpCipher,
797 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000798 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800799 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
800 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
801 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
802 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700803 channel_report->AddString(
804 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800805 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000806 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000807
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000808 int connection_id = 0;
809 for (const cricket::ConnectionInfo& info :
810 channel_iter.connection_infos) {
811 StatsReport* connection_report = AddConnectionInfoReport(
812 transport_iter.first, channel_iter.component, connection_id++,
813 channel_report->id(), info);
814 if (info.best_connection) {
815 channel_report->AddId(
816 StatsReport::kStatsValueNameSelectedCandidatePairId,
817 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 }
819 }
820 }
821 }
822}
823
stefanf79ade12017-06-02 06:44:03 -0700824void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700825 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700826
Steve Anton978b8762017-09-29 12:15:02 -0700827 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700828 return;
829
Steve Anton978b8762017-09-29 12:15:02 -0700830 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700831 cricket::BandwidthEstimationInfo bwe_info;
832 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
833 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
834 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
835 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
836 // TODO(holmer): Also fill this in for audio.
Steve Anton978b8762017-09-29 12:15:02 -0700837 if (pc_->video_channel()) {
838 pc_->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700839 }
stefanf79ade12017-06-02 06:44:03 -0700840 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
841 StatsReport* report = reports_.FindOrAddNew(report_id);
842 ExtractStats(bwe_info, stats_gathering_started_, report);
843}
844
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845void StatsCollector::ExtractVoiceInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700846 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000847
Steve Anton978b8762017-09-29 12:15:02 -0700848 if (!pc_->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 return;
850 }
851 cricket::VoiceMediaInfo voice_info;
Steve Anton978b8762017-09-29 12:15:02 -0700852 if (!pc_->voice_channel()->GetStats(&voice_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100853 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 return;
855 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000856
857 // TODO(tommi): The above code should run on the worker thread and post the
858 // results back to the signaling thread, where we can add data to the reports.
859 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
860
deadbeefab9b2d12015-10-14 11:33:11 -0700861 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700862 proxy_to_transport_, pc_->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000863 if (!transport_id.get()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100864 RTC_LOG(LS_ERROR) << "Failed to get transport name for proxy "
865 << pc_->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 return;
867 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000868
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000869 ExtractStatsFromList(voice_info.receivers, transport_id, this,
870 StatsReport::kReceive);
871 ExtractStatsFromList(voice_info.senders, transport_id, this,
872 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000873
Ivo Creusenae026092017-11-20 13:07:16 +0100874 UpdateStatsFromExistingLocalAudioTracks(voice_info.receivers.size() > 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875}
876
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000877void StatsCollector::ExtractVideoInfo(
878 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700879 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000880
Steve Anton978b8762017-09-29 12:15:02 -0700881 if (!pc_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000883
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 cricket::VideoMediaInfo video_info;
Steve Anton978b8762017-09-29 12:15:02 -0700885 if (!pc_->video_channel()->GetStats(&video_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100886 RTC_LOG(LS_ERROR) << "Failed to get video channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 return;
888 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000889
890 // TODO(tommi): The above code should run on the worker thread and post the
891 // results back to the signaling thread, where we can add data to the reports.
892 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
893
deadbeefab9b2d12015-10-14 11:33:11 -0700894 StatsReport::Id transport_id(GetTransportIdFromProxy(
Steve Anton978b8762017-09-29 12:15:02 -0700895 proxy_to_transport_, pc_->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000896 if (!transport_id.get()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100897 RTC_LOG(LS_ERROR) << "Failed to get transport name for proxy "
898 << pc_->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 return;
900 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000901 ExtractStatsFromList(video_info.receivers, transport_id, this,
902 StatsReport::kReceive);
903 ExtractStatsFromList(video_info.senders, transport_id, this,
904 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905}
906
nissefcc640f2016-04-01 01:10:42 -0700907void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700908 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700909
910 for (const auto& sender : pc_->GetSenders()) {
911 // TODO(nisse): SSRC == 0 currently means none. Delete check when
912 // that is fixed.
913 if (!sender->ssrc()) {
914 continue;
915 }
916 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
917 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
918 continue;
919 }
920 // Safe, because kind() == kVideoKind implies a subclass of
921 // VideoTrackInterface; see mediastreaminterface.h.
922 VideoTrackSourceInterface* source =
923 static_cast<VideoTrackInterface*>(track.get())->GetSource();
924
925 VideoTrackSourceInterface::Stats stats;
926 if (!source->GetStats(&stats)) {
927 continue;
928 }
929 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
930 StatsReport::kStatsReportTypeSsrc,
931 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
932 StatsReport* report = reports_.FindOrAddNew(stats_id);
933 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
934 stats.input_width);
935 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
936 stats.input_height);
937 }
938}
939
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000940void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700941 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000942
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000943 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
944
deadbeefab9b2d12015-10-14 11:33:11 -0700945 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000946 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000947 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000948 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000949 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000950 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700951 // Filter out the initial id (-1).
952 if (dc->id() >= 0) {
953 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
954 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000955 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
956 report->AddString(StatsReport::kStatsValueNameState,
957 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000958 }
959}
960
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000961StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000962 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000963 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700964 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700965 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
966 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000967 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000968}
969
Ivo Creusenae026092017-11-20 13:07:16 +0100970void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
971 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -0700972 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000973 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000974 for (const auto& it : local_audio_tracks_) {
975 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200976 uint32_t ssrc = it.second;
977 StatsReport* report =
978 GetReport(StatsReport::kStatsReportTypeSsrc,
979 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000980 if (report == NULL) {
981 // This can happen if a local audio track is added to a stream on the
982 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100983 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000984 continue;
985 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000986
987 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000988 const StatsReport::Value* v =
989 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000990 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000991 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000992
jbauchbe24c942015-06-22 15:06:43 -0700993 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +0100994 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000995 }
996}
997
998void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +0100999 StatsReport* report,
1000 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001001 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001002 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001003
andrew2fe1cb02015-11-27 17:27:35 -08001004 // Don't overwrite report values if they're not available.
1005 int signal_level;
1006 if (track->GetSignalLevel(&signal_level)) {
1007 RTC_DCHECK_GE(signal_level, 0);
1008 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1009 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001010
andrew2fe1cb02015-11-27 17:27:35 -08001011 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001012
andrew2fe1cb02015-11-27 17:27:35 -08001013 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001014 AudioProcessorInterface::AudioProcessorStatistics stats =
1015 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001016
Ivo Creusen56d46092017-11-24 17:29:59 +01001017 SetAudioProcessingStats(report, stats.typing_noise_detected,
1018 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001019 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001020}
1021
Peter Boström0c4e06b2015-10-07 12:23:21 +02001022bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1023 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001024 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001025 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001026 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001027 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001028 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1029 << " is not associated with a sending track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001030 return false;
1031 }
1032 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001033 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001034 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001035 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1036 << " is not associated with a receiving track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001037 return false;
1038 }
1039 }
1040
1041 return true;
1042}
1043
jbauchbe24c942015-06-22 15:06:43 -07001044void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001045 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001046
1047 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1048
1049 for (const auto& entry : track_ids_) {
1050 StatsReport* report = entry.second;
1051 report->set_timestamp(stats_gathering_started_);
1052 }
jbauchbe24c942015-06-22 15:06:43 -07001053}
1054
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001055void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001056 stats_gathering_started_ = 0;
1057}
1058
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059} // namespace webrtc