blob: d81689986d21086038bb8bdc81dff3333a404a31 [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>
Steve Anton5dfde182018-02-06 10:34:40 -080014#include <set>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <utility>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "pc/channel.h"
19#include "pc/peerconnection.h"
20#include "rtc_base/base64.h"
21#include "rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000026// The following is the enum RTCStatsIceCandidateType from
27// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
28// our stats report for ice candidate type could conform to that.
29const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
30const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
31const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
32const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
33
34// Strings used by the stats collector to report adapter types. This fits the
35// general stype of http://w3c.github.io/webrtc-stats than what
36// AdapterTypeToString does.
37const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
38const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
39const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
40const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000041const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000042
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000043template<typename ValueType>
44struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000045 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000046 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000047};
48
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000049typedef TypeForAdd<bool> BoolForAdd;
50typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020051typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000052typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000053
jbauchbe24c942015-06-22 15:06:43 -070054StatsReport* AddTrackReport(StatsCollection* reports,
55 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000056 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000057 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000058 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000059 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000060 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070061 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000062}
63
Harald Alvestrand75ceef22018-01-04 15:26:13 +010064template <class Track>
65void CreateTrackReport(const Track* track,
66 StatsCollection* reports,
67 TrackIdMap* track_ids) {
68 const std::string& track_id = track->id();
69 StatsReport* report = AddTrackReport(reports, track_id);
70 RTC_DCHECK(report != nullptr);
71 (*track_ids)[track_id] = report;
72}
73
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074template <class TrackVector>
Steve Anton36b29d12017-10-30 09:57:42 -070075void CreateTrackReports(const TrackVector& tracks,
76 StatsCollection* reports,
77 TrackIdMap* track_ids) {
jbauchbe24c942015-06-22 15:06:43 -070078 for (const auto& track : tracks) {
Harald Alvestrand75ceef22018-01-04 15:26:13 +010079 CreateTrackReport(track.get(), reports, track_ids);
jbauchbe24c942015-06-22 15:06:43 -070080 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081}
82
tommi@webrtc.org92f40182015-03-04 15:25:19 +000083void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
84 StatsReport* report) {
85 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
86 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070087 if (info.rtt_ms >= 0) {
88 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
89 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000090}
91
pbosf42376c2015-08-28 07:35:32 -070092void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
93 StatsReport* report) {
94 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
95}
96
Ivo Creusen56d46092017-11-24 17:29:59 +010097void SetAudioProcessingStats(StatsReport* report,
98 bool typing_noise_detected,
99 const AudioProcessingStats& apm_stats) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000100 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
101 typing_noise_detected);
Ivo Creusen56d46092017-11-24 17:29:59 +0100102 if (apm_stats.delay_median_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100103 report->AddInt(StatsReport::kStatsValueNameEchoDelayMedian,
Ivo Creusen56d46092017-11-24 17:29:59 +0100104 *apm_stats.delay_median_ms);
Ivo Creusenae026092017-11-20 13:07:16 +0100105 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100106 if (apm_stats.delay_standard_deviation_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100107 report->AddInt(StatsReport::kStatsValueNameEchoDelayStdDev,
Ivo Creusen56d46092017-11-24 17:29:59 +0100108 *apm_stats.delay_standard_deviation_ms);
zhihuang6ba3b192016-05-13 11:46:35 -0700109 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100110 if (apm_stats.echo_return_loss) {
Ivo Creusenae026092017-11-20 13:07:16 +0100111 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss,
Ivo Creusen56d46092017-11-24 17:29:59 +0100112 *apm_stats.echo_return_loss);
henrik.lundin04a057b2017-01-16 23:53:59 -0800113 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100114 if (apm_stats.echo_return_loss_enhancement) {
Ivo Creusenae026092017-11-20 13:07:16 +0100115 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
Ivo Creusen56d46092017-11-24 17:29:59 +0100116 *apm_stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +0100117 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100118 if (apm_stats.residual_echo_likelihood) {
Ivo Creusenae026092017-11-20 13:07:16 +0100119 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
Ivo Creusen56d46092017-11-24 17:29:59 +0100120 static_cast<float>(*apm_stats.residual_echo_likelihood));
Ivo Creusenae026092017-11-20 13:07:16 +0100121 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100122 if (apm_stats.residual_echo_likelihood_recent_max) {
ivoc4e477a12017-01-15 08:29:46 -0800123 report->AddFloat(
124 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
Ivo Creusen56d46092017-11-24 17:29:59 +0100125 static_cast<float>(*apm_stats.residual_echo_likelihood_recent_max));
126 }
127 if (apm_stats.divergent_filter_fraction) {
128 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
129 static_cast<float>(*apm_stats.divergent_filter_fraction));
ivoc8c63a822016-10-21 04:10:03 -0700130 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000131}
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700134 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000135 const FloatForAdd floats[] = {
136 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
137 { StatsReport::kStatsValueNameSecondaryDecodedRate,
138 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200139 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
140 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000141 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200142 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
143 { StatsReport::kStatsValueNamePreemptiveExpandRate,
144 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700145 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
146 { StatsReport::kStatsValueNameTotalSamplesDuration,
147 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000148 };
149
150 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000151 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
152 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
153 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
154 { StatsReport::kStatsValueNameDecodingCTSG,
155 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700156 { StatsReport::kStatsValueNameDecodingMutedOutput,
157 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000158 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
159 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
160 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
161 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
162 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
163 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
164 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
165 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
166 info.jitter_buffer_preferred_ms },
167 };
168
169 for (const auto& f : floats)
170 report->AddFloat(f.name, f.value);
171
172 for (const auto& i : ints)
173 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700174 if (info.audio_level >= 0) {
175 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
176 info.audio_level);
177 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000178
179 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700181 if (info.capture_start_ntp_time_ms >= 0) {
182 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
183 info.capture_start_ntp_time_ms);
184 }
fippobec70ab2016-01-28 01:27:15 -0800185 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186}
187
188void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000189 ExtractCommonSendProperties(info, report);
190
Ivo Creusen56d46092017-11-24 17:29:59 +0100191 SetAudioProcessingStats(report, info.typing_noise_detected,
192 info.apm_statistics);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000193
zsteine76bd3a2017-07-14 12:17:49 -0700194 const FloatForAdd floats[] = {
195 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
196 { StatsReport::kStatsValueNameTotalSamplesDuration,
197 info.total_input_duration }
198 };
199
andrew2fe1cb02015-11-27 17:27:35 -0800200 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000201 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800202 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
204 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
205 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
206 };
207
zsteine76bd3a2017-07-14 12:17:49 -0700208 for (const auto& f : floats) {
209 report->AddFloat(f.name, f.value);
210 }
211
zhihuang6ba3b192016-05-13 11:46:35 -0700212 for (const auto& i : ints) {
213 if (i.value >= 0) {
214 report->AddInt(i.name, i.value);
215 }
216 }
fippobec70ab2016-01-28 01:27:15 -0800217 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700218 if (info.ana_statistics.bitrate_action_counter) {
219 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
220 *info.ana_statistics.bitrate_action_counter);
221 }
222 if (info.ana_statistics.channel_action_counter) {
223 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
224 *info.ana_statistics.channel_action_counter);
225 }
226 if (info.ana_statistics.dtx_action_counter) {
227 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
228 *info.ana_statistics.dtx_action_counter);
229 }
230 if (info.ana_statistics.fec_action_counter) {
231 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
232 *info.ana_statistics.fec_action_counter);
233 }
ivoc0d0b9122017-09-08 13:24:21 -0700234 if (info.ana_statistics.frame_length_increase_counter) {
235 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
236 *info.ana_statistics.frame_length_increase_counter);
237 }
238 if (info.ana_statistics.frame_length_decrease_counter) {
239 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
240 *info.ana_statistics.frame_length_decrease_counter);
241 }
242 if (info.ana_statistics.uplink_packet_loss_fraction) {
243 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
244 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700245 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246}
247
248void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700249 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100250 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
251 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000252 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700254 if (info.capture_start_ntp_time_ms >= 0) {
255 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
256 info.capture_start_ntp_time_ms);
257 }
sakalcc452e12017-02-09 04:53:45 -0800258 if (info.qp_sum)
259 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
260
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000261 const IntForAdd ints[] = {
262 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
263 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
264 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
265 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
266 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
267 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
268 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
269 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
270 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
271 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
272 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
273 info.min_playout_delay_ms },
274 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
275 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
276 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
277 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
278 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
279 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700280 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000281 };
282
283 for (const auto& i : ints)
284 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800285 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700286
ilnik2edc6842017-07-06 03:06:50 -0700287 if (info.timing_frame_info) {
288 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
289 info.timing_frame_info->ToString());
290 }
ilnikf04afde2017-07-07 01:26:24 -0700291
ilnika79cc282017-08-23 05:24:10 -0700292 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
293 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700294
295 report->AddString(
296 StatsReport::kStatsValueNameContentType,
297 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298}
299
300void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000301 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
Peter Boströmb7d9a972015-12-18 16:01:11 +0100303 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
304 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000305 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
306 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000307 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
308 (info.adapt_reason & 0x1) > 0);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100309 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
310 info.has_entered_low_resolution);
311
sakal87da4042016-10-31 06:53:47 -0700312 if (info.qp_sum)
313 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000314
315 const IntForAdd ints[] = {
316 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
317 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000318 { StatsReport::kStatsValueNameEncodeUsagePercent,
319 info.encode_usage_percent },
320 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000321 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
322 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
323 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000324 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
325 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
326 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
327 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
328 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700329 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000330 };
331
332 for (const auto& i : ints)
333 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800334 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700335 report->AddString(
336 StatsReport::kStatsValueNameContentType,
337 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338}
339
340void ExtractStats(const cricket::BandwidthEstimationInfo& info,
341 double stats_gathering_started,
342 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700343 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000345 report->set_timestamp(stats_gathering_started);
346 const IntForAdd ints[] = {
347 { StatsReport::kStatsValueNameAvailableSendBandwidth,
348 info.available_send_bandwidth },
349 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
350 info.available_recv_bandwidth },
351 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
352 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
353 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
354 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
355 };
356 for (const auto& i : ints)
357 report->AddInt(i.name, i.value);
358 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359}
360
wu@webrtc.org97077a32013-10-25 21:18:33 +0000361void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
362 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000363 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000364 // TODO(hta): Extract some stats here.
365}
366
367void ExtractRemoteStats(const cricket::MediaReceiverInfo& 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000374// In order to use the template, the functions that are called from it,
375// ExtractStats and ExtractRemoteStats, must be defined and overloaded
376// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377template<typename T>
378void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000379 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000380 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000381 StatsReport::Direction direction) {
382 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200383 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000384 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000385 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000386 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
387 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000388 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000389 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000390
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000391 if (!d.remote_stats.empty()) {
392 report = collector->PrepareReport(false, ssrc, transport_id, direction);
393 if (report)
394 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000395 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000397}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398
399} // namespace
400
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000401const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
402 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
403 return STATSREPORT_LOCAL_PORT_TYPE;
404 }
405 if (candidate_type == cricket::STUN_PORT_TYPE) {
406 return STATSREPORT_STUN_PORT_TYPE;
407 }
408 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
409 return STATSREPORT_PRFLX_PORT_TYPE;
410 }
411 if (candidate_type == cricket::RELAY_PORT_TYPE) {
412 return STATSREPORT_RELAY_PORT_TYPE;
413 }
nisseeb4ca4e2017-01-12 02:24:27 -0800414 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000415 return "unknown";
416}
417
418const char* AdapterTypeToStatsType(rtc::AdapterType type) {
419 switch (type) {
420 case rtc::ADAPTER_TYPE_UNKNOWN:
421 return "unknown";
422 case rtc::ADAPTER_TYPE_ETHERNET:
423 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
424 case rtc::ADAPTER_TYPE_WIFI:
425 return STATSREPORT_ADAPTER_TYPE_WIFI;
426 case rtc::ADAPTER_TYPE_CELLULAR:
427 return STATSREPORT_ADAPTER_TYPE_WWAN;
428 case rtc::ADAPTER_TYPE_VPN:
429 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000430 case rtc::ADAPTER_TYPE_LOOPBACK:
431 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000432 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800433 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000434 return "";
435 }
436}
437
Steve Anton2d8609c2018-01-23 16:38:46 -0800438StatsCollector::StatsCollector(PeerConnectionInternal* pc)
deadbeefab9b2d12015-10-14 11:33:11 -0700439 : pc_(pc), stats_gathering_started_(0) {
440 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000441}
442
443StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700444 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445}
446
nissecdf37a92016-09-13 23:41:47 -0700447// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000448double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700449 return rtc::TimeUTCMicros() /
450 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000451}
452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453// Adds a MediaStream with tracks that can be used as a |selector| in a call
454// to GetStats.
455void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700456 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700457 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458
Steve Anton36b29d12017-10-30 09:57:42 -0700459 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
460 &track_ids_);
461 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
462 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463}
464
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100465void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
466 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
467 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
468 &track_ids_);
469 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
470 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
471 &track_ids_);
472 } else {
473 RTC_NOTREACHED() << "Illegal track kind";
474 }
475}
476
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000477void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200478 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700479 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700480 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700481#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000482 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700483 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000484#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000485
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000486 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000487
488 // Create the kStatsReportTypeTrack report for the new track if there is no
489 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000490 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
491 audio_track->id()));
492 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000493 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000494 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000495 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000496 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000497}
498
499void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200500 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700501 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700502 local_audio_tracks_.erase(
503 std::remove_if(
504 local_audio_tracks_.begin(), local_audio_tracks_.end(),
505 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
506 return track.first == audio_track && track.second == ssrc;
507 }),
508 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000509}
510
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000511void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700513 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700514 RTC_DCHECK(reports != NULL);
515 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000517 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
518
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000520 reports->reserve(reports_.size());
521 for (auto* r : reports_)
522 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000523 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
525
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000526 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700527 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000528 if (report)
529 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000531 report = reports_.Find(StatsReport::NewTypedId(
532 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000534 if (!report)
535 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000537 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
539 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000540 for (const auto* r : reports_) {
541 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000543
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000544 const StatsReport::Value* v =
545 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000546 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000547 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549}
550
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000551void
552StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700553 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 double time_now = GetTimeNow();
555 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
556 // ms apart will be ignored.
557 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000558 if (stats_gathering_started_ != 0 &&
559 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 return;
561 }
562 stats_gathering_started_ = time_now;
563
Steve Anton75737c02017-11-06 10:37:17 -0800564 // TODO(tommi): All of these hop over to the worker thread to fetch
565 // information. We could use an AsyncInvoker to run all of these and post
566 // the information back to the signaling thread where we can create and
567 // update stats reports. That would also clean up the threading story a bit
568 // since we'd be creating/updating the stats report objects consistently on
569 // the same thread (this class has no locks right now).
570 ExtractSessionInfo();
571 ExtractBweInfo();
572 ExtractVoiceInfo();
573 ExtractVideoInfo(level);
574 ExtractSenderInfo();
575 ExtractDataInfo();
576 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577}
578
deadbeefab9b2d12015-10-14 11:33:11 -0700579StatsReport* StatsCollector::PrepareReport(
580 bool local,
581 uint32_t ssrc,
582 const StatsReport::Id& transport_id,
583 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700584 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000585 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200586 local ? StatsReport::kStatsReportTypeSsrc
587 : StatsReport::kStatsReportTypeRemoteSsrc,
588 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000589 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590
xians@webrtc.org01bda202014-07-09 07:38:38 +0000591 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000593 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000594 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000595 // The ssrc is not used by any track or existing report, return NULL
596 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000598 }
599
600 // The ssrc is not used by any existing track. Keeps the old track id
601 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000602 const StatsReport::Value* v =
603 report->FindValue(StatsReport::kStatsValueNameTrackId);
604 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000605 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
607
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000608 if (!report)
609 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000611 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000612 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000614 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000615 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000617 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 return report;
619}
620
zhihuange9e94c32016-11-04 11:38:15 -0700621bool StatsCollector::IsValidTrack(const std::string& track_id) {
622 return reports_.Find(StatsReport::NewTypedId(
623 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
624}
625
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000626StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000627 const rtc::SSLCertificate* cert) {
Steve Anton978b8762017-09-29 12:15:02 -0700628 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700629 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000630
hbose29352b2016-08-25 03:52:38 -0700631 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
632 StatsReport* first_report = nullptr;
633 StatsReport* prev_report = nullptr;
634 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
635 stats = stats->issuer.get()) {
636 StatsReport::Id id(StatsReport::NewTypedId(
637 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
638
639 StatsReport* report = reports_.ReplaceOrAddNew(id);
640 report->set_timestamp(stats_gathering_started_);
641 report->AddString(StatsReport::kStatsValueNameFingerprint,
642 stats->fingerprint);
643 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
644 stats->fingerprint_algorithm);
645 report->AddString(StatsReport::kStatsValueNameDer,
646 stats->base64_certificate);
647 if (!first_report)
648 first_report = report;
649 else
650 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
651 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000652 }
hbose29352b2016-08-25 03:52:38 -0700653 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000654}
655
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000656StatsReport* StatsCollector::AddConnectionInfoReport(
657 const std::string& content_name, int component, int connection_id,
658 const StatsReport::Id& channel_report_id,
659 const cricket::ConnectionInfo& info) {
660 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
661 connection_id));
662 StatsReport* report = reports_.ReplaceOrAddNew(id);
663 report->set_timestamp(stats_gathering_started_);
664
665 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700666 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
667 {StatsReport::kStatsValueNameReceiving, info.receiving},
668 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000669 };
670 for (const auto& b : bools)
671 report->AddBoolean(b.name, b.value);
672
673 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
674 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
675 AddCandidateReport(info.local_candidate, true)->id());
676 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
677 AddCandidateReport(info.remote_candidate, false)->id());
678
679 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700680 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
681 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
682 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
683 {StatsReport::kStatsValueNameRtt, info.rtt},
684 {StatsReport::kStatsValueNameSendPacketsDiscarded,
685 info.sent_discarded_packets},
686 {StatsReport::kStatsValueNameSentPingRequestsTotal,
687 info.sent_ping_requests_total},
688 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
689 info.sent_ping_requests_before_first_response},
690 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
691 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
692 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000693 };
694 for (const auto& i : int64s)
695 report->AddInt64(i.name, i.value);
696
697 report->AddString(StatsReport::kStatsValueNameLocalAddress,
698 info.local_candidate.address().ToString());
699 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
700 info.local_candidate.type());
701 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
702 info.remote_candidate.address().ToString());
703 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
704 info.remote_candidate.type());
705 report->AddString(StatsReport::kStatsValueNameTransportType,
706 info.local_candidate.protocol());
707
708 return report;
709}
710
711StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000712 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000713 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000714 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
715 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000716 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000717 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000718 report->set_timestamp(stats_gathering_started_);
719 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000720 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
721 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000722 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000723 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
724 candidate.address().ipaddr().ToString());
725 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
726 candidate.address().PortAsString());
727 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
728 candidate.priority());
729 report->AddString(StatsReport::kStatsValueNameCandidateType,
730 IceCandidateTypeToStatsType(candidate.type()));
731 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
732 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000733 }
734
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000735 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000736}
737
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700739 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000742 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700743 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000744 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000745 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000746 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700747 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748
Steve Anton5dfde182018-02-06 10:34:40 -0800749 std::set<std::string> transport_names;
750 for (const auto& entry : pc_->GetTransportNamesByMid()) {
751 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
Steve Anton5dfde182018-02-06 10:34:40 -0800754 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
755 pc_->GetTransportStatsByNames(transport_names);
756
757 for (const auto& entry : transport_stats_by_name) {
758 const std::string& transport_name = entry.first;
759 const cricket::TransportStats& transport_stats = entry.second;
760
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;
Steve Anton5dfde182018-02-06 10:34:40 -0800767 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200768 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000769 if (r)
770 local_cert_report_id = r->id();
771 }
772
jbauch555604a2016-04-26 03:13:22 -0700773 std::unique_ptr<rtc::SSLCertificate> cert =
Steve Anton5dfde182018-02-06 10:34:40 -0800774 pc_->GetRemoteSSLCertificate(transport_name);
kwibergb4d01c42016-04-06 05:15:06 -0700775 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000776 StatsReport* r = AddCertificateReports(cert.get());
777 if (r)
778 remote_cert_report_id = r->id();
779 }
780
Steve Anton5dfde182018-02-06 10:34:40 -0800781 for (const auto& channel_iter : transport_stats.channel_stats) {
782 StatsReport::Id id(
783 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000784 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
785 channel_report->set_timestamp(stats_gathering_started_);
786 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
787 channel_iter.component);
788 if (local_cert_report_id.get()) {
789 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
790 local_cert_report_id);
791 }
792 if (remote_cert_report_id.get()) {
793 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
794 remote_cert_report_id);
795 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800796 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
797 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
798 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
799 channel_report->AddString(
800 StatsReport::kStatsValueNameSrtpCipher,
801 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000802 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800803 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
804 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
805 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
806 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700807 channel_report->AddString(
808 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800809 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000810 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000811
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000812 int connection_id = 0;
813 for (const cricket::ConnectionInfo& info :
814 channel_iter.connection_infos) {
815 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800816 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000817 channel_report->id(), info);
818 if (info.best_connection) {
819 channel_report->AddId(
820 StatsReport::kStatsValueNameSelectedCandidatePairId,
821 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 }
823 }
824 }
825 }
826}
827
stefanf79ade12017-06-02 06:44:03 -0700828void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700829 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700830
Steve Anton978b8762017-09-29 12:15:02 -0700831 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700832 return;
833
Steve Anton978b8762017-09-29 12:15:02 -0700834 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700835 cricket::BandwidthEstimationInfo bwe_info;
836 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
837 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
838 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
839 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
840 // TODO(holmer): Also fill this in for audio.
Steve Anton978b8762017-09-29 12:15:02 -0700841 if (pc_->video_channel()) {
842 pc_->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700843 }
stefanf79ade12017-06-02 06:44:03 -0700844 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
845 StatsReport* report = reports_.FindOrAddNew(report_id);
846 ExtractStats(bwe_info, stats_gathering_started_, report);
847}
848
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849void StatsCollector::ExtractVoiceInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700850 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000851
Steve Anton978b8762017-09-29 12:15:02 -0700852 if (!pc_->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 return;
854 }
855 cricket::VoiceMediaInfo voice_info;
Steve Anton978b8762017-09-29 12:15:02 -0700856 if (!pc_->voice_channel()->GetStats(&voice_info)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100857 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return;
859 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000860
861 // TODO(tommi): The above code should run on the worker thread and post the
862 // results back to the signaling thread, where we can add data to the reports.
863 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
864
Steve Anton74116482017-12-18 11:00:14 -0800865 StatsReport::Id transport_id =
866 StatsReport::NewComponentId(pc_->voice_channel()->transport_name(),
867 cricket::ICE_CANDIDATE_COMPONENT_RTP);
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 Anton74116482017-12-18 11:00:14 -0800881 if (!pc_->video_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 return;
Steve Anton74116482017-12-18 11:00:14 -0800883 }
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
Steve Anton74116482017-12-18 11:00:14 -0800894 StatsReport::Id transport_id =
895 StatsReport::NewComponentId(pc_->video_channel()->transport_name(),
896 cricket::ICE_CANDIDATE_COMPONENT_RTP);
897
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000898 ExtractStatsFromList(video_info.receivers, transport_id, this,
899 StatsReport::kReceive);
900 ExtractStatsFromList(video_info.senders, transport_id, this,
901 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902}
903
nissefcc640f2016-04-01 01:10:42 -0700904void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700905 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700906
907 for (const auto& sender : pc_->GetSenders()) {
908 // TODO(nisse): SSRC == 0 currently means none. Delete check when
909 // that is fixed.
910 if (!sender->ssrc()) {
911 continue;
912 }
913 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
914 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
915 continue;
916 }
917 // Safe, because kind() == kVideoKind implies a subclass of
918 // VideoTrackInterface; see mediastreaminterface.h.
919 VideoTrackSourceInterface* source =
920 static_cast<VideoTrackInterface*>(track.get())->GetSource();
921
922 VideoTrackSourceInterface::Stats stats;
923 if (!source->GetStats(&stats)) {
924 continue;
925 }
926 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
927 StatsReport::kStatsReportTypeSsrc,
928 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
929 StatsReport* report = reports_.FindOrAddNew(stats_id);
930 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
931 stats.input_width);
932 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
933 stats.input_height);
934 }
935}
936
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000937void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700938 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000939
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000940 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
941
deadbeefab9b2d12015-10-14 11:33:11 -0700942 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000943 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000944 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000945 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000946 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000947 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700948 // Filter out the initial id (-1).
949 if (dc->id() >= 0) {
950 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
951 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000952 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
953 report->AddString(StatsReport::kStatsValueNameState,
954 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000955 }
956}
957
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000958StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000959 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000960 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700961 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700962 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
963 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000964 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000965}
966
Ivo Creusenae026092017-11-20 13:07:16 +0100967void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
968 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -0700969 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000970 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000971 for (const auto& it : local_audio_tracks_) {
972 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200973 uint32_t ssrc = it.second;
974 StatsReport* report =
975 GetReport(StatsReport::kStatsReportTypeSsrc,
976 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000977 if (report == NULL) {
978 // This can happen if a local audio track is added to a stream on the
979 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100980 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000981 continue;
982 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000983
984 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000985 const StatsReport::Value* v =
986 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000987 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000988 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000989
jbauchbe24c942015-06-22 15:06:43 -0700990 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +0100991 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000992 }
993}
994
995void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +0100996 StatsReport* report,
997 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -0700998 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700999 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001000
andrew2fe1cb02015-11-27 17:27:35 -08001001 // Don't overwrite report values if they're not available.
1002 int signal_level;
1003 if (track->GetSignalLevel(&signal_level)) {
1004 RTC_DCHECK_GE(signal_level, 0);
1005 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1006 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001007
andrew2fe1cb02015-11-27 17:27:35 -08001008 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001009
andrew2fe1cb02015-11-27 17:27:35 -08001010 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001011 AudioProcessorInterface::AudioProcessorStatistics stats =
1012 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001013
Ivo Creusen56d46092017-11-24 17:29:59 +01001014 SetAudioProcessingStats(report, stats.typing_noise_detected,
1015 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001016 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001017}
1018
Peter Boström0c4e06b2015-10-07 12:23:21 +02001019bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1020 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001021 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001022 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001023 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001024 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001025 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1026 << " is not associated with a sending track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001027 return false;
1028 }
1029 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001030 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001031 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001032 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1033 << " is not associated with a receiving track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001034 return false;
1035 }
1036 }
1037
1038 return true;
1039}
1040
jbauchbe24c942015-06-22 15:06:43 -07001041void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001042 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001043
1044 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1045
1046 for (const auto& entry : track_ids_) {
1047 StatsReport* report = entry.second;
1048 report->set_timestamp(stats_gathering_started_);
1049 }
jbauchbe24c942015-06-22 15:06:43 -07001050}
1051
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001052void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001053 stats_gathering_started_ = 0;
1054}
1055
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056} // namespace webrtc