blob: 9a77a21275d4af27eeb088019af04e8d966e5402 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Artem Titova76af0c2018-07-23 17:38:12 +020021#include "rtc_base/third_party/base64/base64.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";
Qingsi Wang01560de2018-07-26 10:44:02 -070042const char* STATSREPORT_ADAPTER_TYPE_WILDCARD = "wildcard";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000043
Yves Gerey665174f2018-06-19 15:03:05 +020044template <typename ValueType>
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000046 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000047 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000048};
49
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000050typedef TypeForAdd<bool> BoolForAdd;
51typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020052typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000053typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000054
jbauchbe24c942015-06-22 15:06:43 -070055StatsReport* AddTrackReport(StatsCollection* reports,
56 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000057 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000058 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000059 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000060 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000061 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070062 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000063}
64
Harald Alvestrand75ceef22018-01-04 15:26:13 +010065template <class Track>
66void CreateTrackReport(const Track* track,
67 StatsCollection* reports,
68 TrackIdMap* track_ids) {
69 const std::string& track_id = track->id();
70 StatsReport* report = AddTrackReport(reports, track_id);
71 RTC_DCHECK(report != nullptr);
72 (*track_ids)[track_id] = report;
73}
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075template <class TrackVector>
Steve Anton36b29d12017-10-30 09:57:42 -070076void CreateTrackReports(const TrackVector& tracks,
77 StatsCollection* reports,
78 TrackIdMap* track_ids) {
jbauchbe24c942015-06-22 15:06:43 -070079 for (const auto& track : tracks) {
Harald Alvestrand75ceef22018-01-04 15:26:13 +010080 CreateTrackReport(track.get(), reports, track_ids);
jbauchbe24c942015-06-22 15:06:43 -070081 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082}
83
tommi@webrtc.org92f40182015-03-04 15:25:19 +000084void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
85 StatsReport* report) {
86 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
87 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070088 if (info.rtt_ms >= 0) {
89 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
90 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000091}
92
pbosf42376c2015-08-28 07:35:32 -070093void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
94 StatsReport* report) {
95 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
96}
97
Ivo Creusen56d46092017-11-24 17:29:59 +010098void SetAudioProcessingStats(StatsReport* report,
99 bool typing_noise_detected,
100 const AudioProcessingStats& apm_stats) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000101 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
102 typing_noise_detected);
Ivo Creusen56d46092017-11-24 17:29:59 +0100103 if (apm_stats.delay_median_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100104 report->AddInt(StatsReport::kStatsValueNameEchoDelayMedian,
Ivo Creusen56d46092017-11-24 17:29:59 +0100105 *apm_stats.delay_median_ms);
Ivo Creusenae026092017-11-20 13:07:16 +0100106 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100107 if (apm_stats.delay_standard_deviation_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100108 report->AddInt(StatsReport::kStatsValueNameEchoDelayStdDev,
Ivo Creusen56d46092017-11-24 17:29:59 +0100109 *apm_stats.delay_standard_deviation_ms);
zhihuang6ba3b192016-05-13 11:46:35 -0700110 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100111 if (apm_stats.echo_return_loss) {
Ivo Creusenae026092017-11-20 13:07:16 +0100112 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss,
Ivo Creusen56d46092017-11-24 17:29:59 +0100113 *apm_stats.echo_return_loss);
henrik.lundin04a057b2017-01-16 23:53:59 -0800114 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100115 if (apm_stats.echo_return_loss_enhancement) {
Ivo Creusenae026092017-11-20 13:07:16 +0100116 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
Ivo Creusen56d46092017-11-24 17:29:59 +0100117 *apm_stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +0100118 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100119 if (apm_stats.residual_echo_likelihood) {
Ivo Creusenae026092017-11-20 13:07:16 +0100120 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
Ivo Creusen56d46092017-11-24 17:29:59 +0100121 static_cast<float>(*apm_stats.residual_echo_likelihood));
Ivo Creusenae026092017-11-20 13:07:16 +0100122 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100123 if (apm_stats.residual_echo_likelihood_recent_max) {
ivoc4e477a12017-01-15 08:29:46 -0800124 report->AddFloat(
125 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
Ivo Creusen56d46092017-11-24 17:29:59 +0100126 static_cast<float>(*apm_stats.residual_echo_likelihood_recent_max));
127 }
128 if (apm_stats.divergent_filter_fraction) {
129 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
130 static_cast<float>(*apm_stats.divergent_filter_fraction));
ivoc8c63a822016-10-21 04:10:03 -0700131 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000132}
133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700135 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000136 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200137 {StatsReport::kStatsValueNameExpandRate, info.expand_rate},
138 {StatsReport::kStatsValueNameSecondaryDecodedRate,
139 info.secondary_decoded_rate},
140 {StatsReport::kStatsValueNameSecondaryDiscardedRate,
141 info.secondary_discarded_rate},
142 {StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate},
143 {StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate},
144 {StatsReport::kStatsValueNamePreemptiveExpandRate,
145 info.preemptive_expand_rate},
146 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy},
147 {StatsReport::kStatsValueNameTotalSamplesDuration,
148 info.total_output_duration}};
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000149
150 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200151 {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},
156 {StatsReport::kStatsValueNameDecodingMutedOutput,
157 info.decoding_muted_output},
158 {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},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000167 };
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
Yves Gerey665174f2018-06-19 15:03:05 +0200179 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700180 if (info.capture_start_ntp_time_ms >= 0) {
181 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
182 info.capture_start_ntp_time_ms);
183 }
fippobec70ab2016-01-28 01:27:15 -0800184 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185}
186
187void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000188 ExtractCommonSendProperties(info, report);
189
Ivo Creusen56d46092017-11-24 17:29:59 +0100190 SetAudioProcessingStats(report, info.typing_noise_detected,
191 info.apm_statistics);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000192
zsteine76bd3a2017-07-14 12:17:49 -0700193 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200194 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy},
195 {StatsReport::kStatsValueNameTotalSamplesDuration,
196 info.total_input_duration}};
zsteine76bd3a2017-07-14 12:17:49 -0700197
andrew2fe1cb02015-11-27 17:27:35 -0800198 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000199 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200200 {StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
201 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
202 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
203 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000204 };
205
zsteine76bd3a2017-07-14 12:17:49 -0700206 for (const auto& f : floats) {
207 report->AddFloat(f.name, f.value);
208 }
209
zhihuang6ba3b192016-05-13 11:46:35 -0700210 for (const auto& i : ints) {
211 if (i.value >= 0) {
212 report->AddInt(i.name, i.value);
213 }
214 }
fippobec70ab2016-01-28 01:27:15 -0800215 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700216 if (info.ana_statistics.bitrate_action_counter) {
217 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
218 *info.ana_statistics.bitrate_action_counter);
219 }
220 if (info.ana_statistics.channel_action_counter) {
221 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
222 *info.ana_statistics.channel_action_counter);
223 }
224 if (info.ana_statistics.dtx_action_counter) {
225 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
226 *info.ana_statistics.dtx_action_counter);
227 }
228 if (info.ana_statistics.fec_action_counter) {
229 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
230 *info.ana_statistics.fec_action_counter);
231 }
ivoc0d0b9122017-09-08 13:24:21 -0700232 if (info.ana_statistics.frame_length_increase_counter) {
233 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
234 *info.ana_statistics.frame_length_increase_counter);
235 }
236 if (info.ana_statistics.frame_length_decrease_counter) {
237 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
238 *info.ana_statistics.frame_length_decrease_counter);
239 }
240 if (info.ana_statistics.uplink_packet_loss_fraction) {
241 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
242 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700243 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244}
245
246void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700247 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100248 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
249 info.decoder_implementation_name);
Yves Gerey665174f2018-06-19 15:03:05 +0200250 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700251 if (info.capture_start_ntp_time_ms >= 0) {
252 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
253 info.capture_start_ntp_time_ms);
254 }
sakalcc452e12017-02-09 04:53:45 -0800255 if (info.qp_sum)
256 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
257
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000258 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200259 {StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms},
260 {StatsReport::kStatsValueNameDecodeMs, info.decode_ms},
261 {StatsReport::kStatsValueNameFirsSent, info.firs_sent},
262 {StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height},
263 {StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded},
264 {StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output},
265 {StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd},
266 {StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width},
267 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
268 {StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms},
269 {StatsReport::kStatsValueNameMinPlayoutDelayMs,
270 info.min_playout_delay_ms},
271 {StatsReport::kStatsValueNameNacksSent, info.nacks_sent},
272 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
273 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
274 {StatsReport::kStatsValueNamePlisSent, info.plis_sent},
275 {StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms},
276 {StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms},
277 {StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded},
Artem Titov8a3ab0e2018-07-27 14:52:57 +0000278 };
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000279
280 for (const auto& i : ints)
281 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800282 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700283
ilnik2edc6842017-07-06 03:06:50 -0700284 if (info.timing_frame_info) {
285 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
286 info.timing_frame_info->ToString());
287 }
ilnikf04afde2017-07-07 01:26:24 -0700288
ilnika79cc282017-08-23 05:24:10 -0700289 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
290 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700291
292 report->AddString(
293 StatsReport::kStatsValueNameContentType,
294 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295}
296
297void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000298 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
Peter Boströmb7d9a972015-12-18 16:01:11 +0100300 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
301 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000302 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
303 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000304 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
305 (info.adapt_reason & 0x1) > 0);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100306 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
307 info.has_entered_low_resolution);
308
sakal87da4042016-10-31 06:53:47 -0700309 if (info.qp_sum)
310 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000311
312 const IntForAdd ints[] = {
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100313 {StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes},
314 {StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms},
315 {StatsReport::kStatsValueNameEncodeUsagePercent,
316 info.encode_usage_percent},
317 {StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd},
318 {StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height},
319 {StatsReport::kStatsValueNameFrameRateInput, info.framerate_input},
320 {StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent},
321 {StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width},
322 {StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd},
323 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
324 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
325 {StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd},
326 {StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded},
327 {StatsReport::kStatsValueNameHugeFramesSent, info.huge_frames_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000328 };
329
330 for (const auto& i : ints)
331 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800332 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700333 report->AddString(
334 StatsReport::kStatsValueNameContentType,
335 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336}
337
338void ExtractStats(const cricket::BandwidthEstimationInfo& info,
339 double stats_gathering_started,
340 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700341 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000343 report->set_timestamp(stats_gathering_started);
344 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200345 {StatsReport::kStatsValueNameAvailableSendBandwidth,
346 info.available_send_bandwidth},
347 {StatsReport::kStatsValueNameAvailableReceiveBandwidth,
348 info.available_recv_bandwidth},
349 {StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate},
350 {StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate},
351 {StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate},
352 {StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000353 };
354 for (const auto& i : ints)
355 report->AddInt(i.name, i.value);
356 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357}
358
wu@webrtc.org97077a32013-10-25 21:18:33 +0000359void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
360 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000361 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000362 // TODO(hta): Extract some stats here.
363}
364
365void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
366 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000367 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000368 // TODO(hta): Extract some stats here.
369}
370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000372// In order to use the template, the functions that are called from it,
373// ExtractStats and ExtractRemoteStats, must be defined and overloaded
374// for each type.
Yves Gerey665174f2018-06-19 15:03:05 +0200375template <typename T>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000377 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000378 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000379 StatsReport::Direction direction) {
380 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200381 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000382 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000383 // TODO(hta): Handle the case of multiple SSRCs per object.
Yves Gerey665174f2018-06-19 15:03:05 +0200384 StatsReport* report =
385 collector->PrepareReport(true, ssrc, transport_id, direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000386 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000387 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000388
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000389 if (!d.remote_stats.empty()) {
390 report = collector->PrepareReport(false, ssrc, transport_id, direction);
391 if (report)
392 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000393 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000395}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396
397} // namespace
398
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000399const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
400 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
401 return STATSREPORT_LOCAL_PORT_TYPE;
402 }
403 if (candidate_type == cricket::STUN_PORT_TYPE) {
404 return STATSREPORT_STUN_PORT_TYPE;
405 }
406 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
407 return STATSREPORT_PRFLX_PORT_TYPE;
408 }
409 if (candidate_type == cricket::RELAY_PORT_TYPE) {
410 return STATSREPORT_RELAY_PORT_TYPE;
411 }
nisseeb4ca4e2017-01-12 02:24:27 -0800412 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000413 return "unknown";
414}
415
416const char* AdapterTypeToStatsType(rtc::AdapterType type) {
417 switch (type) {
418 case rtc::ADAPTER_TYPE_UNKNOWN:
419 return "unknown";
420 case rtc::ADAPTER_TYPE_ETHERNET:
421 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
422 case rtc::ADAPTER_TYPE_WIFI:
423 return STATSREPORT_ADAPTER_TYPE_WIFI;
424 case rtc::ADAPTER_TYPE_CELLULAR:
425 return STATSREPORT_ADAPTER_TYPE_WWAN;
426 case rtc::ADAPTER_TYPE_VPN:
427 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000428 case rtc::ADAPTER_TYPE_LOOPBACK:
429 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
Qingsi Wang01560de2018-07-26 10:44:02 -0700430 case rtc::ADAPTER_TYPE_ANY:
431 return STATSREPORT_ADAPTER_TYPE_WILDCARD;
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() {
Minyue Li656d6092018-08-10 15:38:52 +0200449 return static_cast<double>(rtc::TimeUTCMillis());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000450}
451
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452// Adds a MediaStream with tracks that can be used as a |selector| in a call
453// to GetStats.
454void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700455 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700456 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
Steve Anton36b29d12017-10-30 09:57:42 -0700458 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
459 &track_ids_);
460 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
461 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462}
463
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100464void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
465 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
466 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
467 &track_ids_);
468 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
469 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
470 &track_ids_);
471 } else {
472 RTC_NOTREACHED() << "Illegal track kind";
473 }
474}
475
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000476void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200477 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700478 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700479 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700480#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000481 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700482 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000483#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000484
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000485 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000486
487 // Create the kStatsReportTypeTrack report for the new track if there is no
488 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000489 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
490 audio_track->id()));
491 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000492 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000493 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000494 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000495 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000496}
497
498void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200499 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700500 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700501 local_audio_tracks_.erase(
502 std::remove_if(
503 local_audio_tracks_.begin(), local_audio_tracks_.end(),
504 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
505 return track.first == audio_track && track.second == ssrc;
506 }),
507 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000508}
509
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000510void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700512 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700513 RTC_DCHECK(reports != NULL);
514 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000516 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
517
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000519 reports->reserve(reports_.size());
520 for (auto* r : reports_)
521 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000522 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
524
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000525 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700526 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000527 if (report)
528 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
Yves Gerey665174f2018-06-19 15:03:05 +0200530 report = reports_.Find(
531 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000533 if (!report)
534 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000536 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537
538 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000539 for (const auto* r : reports_) {
540 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000542
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000543 const StatsReport::Value* v =
544 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000545 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000546 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548}
549
Yves Gerey665174f2018-06-19 15:03:05 +0200550void StatsCollector::UpdateStats(
551 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700552 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 double time_now = GetTimeNow();
554 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
555 // ms apart will be ignored.
556 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000557 if (stats_gathering_started_ != 0 &&
558 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 return;
560 }
561 stats_gathering_started_ = time_now;
562
Steve Anton75737c02017-11-06 10:37:17 -0800563 // TODO(tommi): All of these hop over to the worker thread to fetch
564 // information. We could use an AsyncInvoker to run all of these and post
565 // the information back to the signaling thread where we can create and
566 // update stats reports. That would also clean up the threading story a bit
567 // since we'd be creating/updating the stats report objects consistently on
568 // the same thread (this class has no locks right now).
569 ExtractSessionInfo();
570 ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800571 ExtractMediaInfo();
Steve Anton75737c02017-11-06 10:37:17 -0800572 ExtractSenderInfo();
573 ExtractDataInfo();
574 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575}
576
Yves Gerey665174f2018-06-19 15:03:05 +0200577StatsReport* StatsCollector::PrepareReport(bool local,
578 uint32_t ssrc,
579 const StatsReport::Id& transport_id,
580 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700581 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000582 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200583 local ? StatsReport::kStatsReportTypeSsrc
584 : StatsReport::kStatsReportTypeRemoteSsrc,
585 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000586 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587
xians@webrtc.org01bda202014-07-09 07:38:38 +0000588 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000590 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700591 // The SSRC is not used by any existing track (or lookup failed since the
592 // SSRC wasn't signaled in SDP). Try copying the track ID from a previous
593 // report: if one exists.
594 if (report) {
595 const StatsReport::Value* v =
596 report->FindValue(StatsReport::kStatsValueNameTrackId);
597 if (v)
598 track_id = v->string_val();
xians@webrtc.org01bda202014-07-09 07:38:38 +0000599 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 }
601
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000602 if (!report)
603 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000605 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000606 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000608 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700609 if (!track_id.empty()) {
610 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
611 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000613 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 return report;
615}
616
zhihuange9e94c32016-11-04 11:38:15 -0700617bool StatsCollector::IsValidTrack(const std::string& track_id) {
618 return reports_.Find(StatsReport::NewTypedId(
619 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
620}
621
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000622StatsReport* StatsCollector::AddCertificateReports(
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800623 std::unique_ptr<rtc::SSLCertificateStats> cert_stats) {
Steve Anton978b8762017-09-29 12:15:02 -0700624 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000625
hbose29352b2016-08-25 03:52:38 -0700626 StatsReport* first_report = nullptr;
627 StatsReport* prev_report = nullptr;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800628 for (rtc::SSLCertificateStats* stats = cert_stats.get(); stats;
hbose29352b2016-08-25 03:52:38 -0700629 stats = stats->issuer.get()) {
630 StatsReport::Id id(StatsReport::NewTypedId(
631 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
632
633 StatsReport* report = reports_.ReplaceOrAddNew(id);
634 report->set_timestamp(stats_gathering_started_);
635 report->AddString(StatsReport::kStatsValueNameFingerprint,
636 stats->fingerprint);
637 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
638 stats->fingerprint_algorithm);
639 report->AddString(StatsReport::kStatsValueNameDer,
640 stats->base64_certificate);
641 if (!first_report)
642 first_report = report;
643 else
644 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
645 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000646 }
hbose29352b2016-08-25 03:52:38 -0700647 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000648}
649
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000650StatsReport* StatsCollector::AddConnectionInfoReport(
Yves Gerey665174f2018-06-19 15:03:05 +0200651 const std::string& content_name,
652 int component,
653 int connection_id,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000654 const StatsReport::Id& channel_report_id,
655 const cricket::ConnectionInfo& info) {
Yves Gerey665174f2018-06-19 15:03:05 +0200656 StatsReport::Id id(
657 StatsReport::NewCandidatePairId(content_name, component, connection_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000658 StatsReport* report = reports_.ReplaceOrAddNew(id);
659 report->set_timestamp(stats_gathering_started_);
660
661 const BoolForAdd bools[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200662 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
663 {StatsReport::kStatsValueNameReceiving, info.receiving},
664 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000665 };
666 for (const auto& b : bools)
667 report->AddBoolean(b.name, b.value);
668
669 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
Qingsi Wang72a43a12018-02-20 16:03:18 -0800670 cricket::CandidateStats local_candidate_stats(info.local_candidate);
671 cricket::CandidateStats remote_candidate_stats(info.remote_candidate);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800673 AddCandidateReport(local_candidate_stats, true)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000674 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800675 AddCandidateReport(remote_candidate_stats, false)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000676
677 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700678 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
679 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
680 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
681 {StatsReport::kStatsValueNameRtt, info.rtt},
682 {StatsReport::kStatsValueNameSendPacketsDiscarded,
683 info.sent_discarded_packets},
684 {StatsReport::kStatsValueNameSentPingRequestsTotal,
685 info.sent_ping_requests_total},
686 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
687 info.sent_ping_requests_before_first_response},
688 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
689 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
690 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000691 };
692 for (const auto& i : int64s)
693 report->AddInt64(i.name, i.value);
694
695 report->AddString(StatsReport::kStatsValueNameLocalAddress,
696 info.local_candidate.address().ToString());
697 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
698 info.local_candidate.type());
699 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
700 info.remote_candidate.address().ToString());
701 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
702 info.remote_candidate.type());
703 report->AddString(StatsReport::kStatsValueNameTransportType,
704 info.local_candidate.protocol());
705
706 return report;
707}
708
709StatsReport* StatsCollector::AddCandidateReport(
Qingsi Wang72a43a12018-02-20 16:03:18 -0800710 const cricket::CandidateStats& candidate_stats,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000711 bool local) {
Qingsi Wang72a43a12018-02-20 16:03:18 -0800712 const auto& candidate = candidate_stats.candidate;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000713 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
714 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000715 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000716 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000717 report->set_timestamp(stats_gathering_started_);
718 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000719 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
720 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000721 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000722 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
723 candidate.address().ipaddr().ToString());
724 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
725 candidate.address().PortAsString());
726 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
727 candidate.priority());
728 report->AddString(StatsReport::kStatsValueNameCandidateType,
729 IceCandidateTypeToStatsType(candidate.type()));
730 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
731 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000732 }
733
Qingsi Wang4ff54432018-03-01 18:25:20 -0800734 if (local && candidate_stats.stun_stats.has_value()) {
735 const auto& stun_stats = candidate_stats.stun_stats.value();
736 report->AddInt64(StatsReport::kStatsValueNameSentStunKeepaliveRequests,
737 stun_stats.stun_binding_requests_sent);
738 report->AddInt64(StatsReport::kStatsValueNameRecvStunKeepaliveResponses,
739 stun_stats.stun_binding_responses_received);
740 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttTotal,
741 stun_stats.stun_binding_rtt_ms_total);
742 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttSquaredTotal,
743 stun_stats.stun_binding_rtt_ms_squared_total);
744 }
745
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000746 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000747}
748
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700750 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000751
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000753 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700754 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000755 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000756 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000757 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700758 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759
Qingsi Wang72a43a12018-02-20 16:03:18 -0800760 cricket::CandidateStatsList pooled_candidate_stats_list =
761 pc_->GetPooledCandidateStats();
762
763 for (const cricket::CandidateStats& stats : pooled_candidate_stats_list) {
764 AddCandidateReport(stats, true);
765 }
766
Steve Anton5dfde182018-02-06 10:34:40 -0800767 std::set<std::string> transport_names;
768 for (const auto& entry : pc_->GetTransportNamesByMid()) {
769 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000770 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771
Steve Anton5dfde182018-02-06 10:34:40 -0800772 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
773 pc_->GetTransportStatsByNames(transport_names);
774
775 for (const auto& entry : transport_stats_by_name) {
776 const std::string& transport_name = entry.first;
777 const cricket::TransportStats& transport_stats = entry.second;
778
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000779 // Attempt to get a copy of the certificates from the transport and
780 // expose them in stats reports. All channels in a transport share the
781 // same local and remote certificates.
782 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000783 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200784 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton5dfde182018-02-06 10:34:40 -0800785 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800786 StatsReport* r =
787 AddCertificateReports(certificate->ssl_cert_chain().GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000788 if (r)
789 local_cert_report_id = r->id();
790 }
791
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800792 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
793 pc_->GetRemoteSSLCertChain(transport_name);
794 if (remote_cert_chain) {
795 StatsReport* r = AddCertificateReports(remote_cert_chain->GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000796 if (r)
797 remote_cert_report_id = r->id();
798 }
799
Steve Anton5dfde182018-02-06 10:34:40 -0800800 for (const auto& channel_iter : transport_stats.channel_stats) {
801 StatsReport::Id id(
802 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000803 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
804 channel_report->set_timestamp(stats_gathering_started_);
805 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
806 channel_iter.component);
807 if (local_cert_report_id.get()) {
808 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
809 local_cert_report_id);
810 }
811 if (remote_cert_report_id.get()) {
812 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
813 remote_cert_report_id);
814 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800815 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
816 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
817 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
818 channel_report->AddString(
819 StatsReport::kStatsValueNameSrtpCipher,
820 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000821 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800822 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
823 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
824 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
825 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700826 channel_report->AddString(
827 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800828 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000829 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000830
Qingsi Wang72a43a12018-02-20 16:03:18 -0800831 // Collect stats for non-pooled candidates. Note that the reports
832 // generated here supersedes the candidate reports generated in
833 // AddConnectionInfoReport below, and they may report candidates that are
834 // not paired. Also, the candidate report generated in
835 // AddConnectionInfoReport do not report port stats like StunStats.
836 for (const cricket::CandidateStats& stats :
837 channel_iter.candidate_stats_list) {
838 AddCandidateReport(stats, true);
839 }
840
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000841 int connection_id = 0;
842 for (const cricket::ConnectionInfo& info :
Yves Gerey665174f2018-06-19 15:03:05 +0200843 channel_iter.connection_infos) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000844 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800845 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000846 channel_report->id(), info);
847 if (info.best_connection) {
848 channel_report->AddId(
849 StatsReport::kStatsValueNameSelectedCandidatePairId,
850 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 }
852 }
853 }
854 }
855}
856
stefanf79ade12017-06-02 06:44:03 -0700857void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700858 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700859
Steve Anton978b8762017-09-29 12:15:02 -0700860 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700861 return;
862
Steve Anton978b8762017-09-29 12:15:02 -0700863 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700864 cricket::BandwidthEstimationInfo bwe_info;
865 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
866 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
867 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
Steve Antonafb0bb72018-02-20 11:35:37 -0800868
stefanf79ade12017-06-02 06:44:03 -0700869 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
870 // TODO(holmer): Also fill this in for audio.
Steve Antonafb0bb72018-02-20 11:35:37 -0800871 for (auto transceiver : pc_->GetTransceiversInternal()) {
872 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) {
873 continue;
874 }
875 auto* video_channel =
876 static_cast<cricket::VideoChannel*>(transceiver->internal()->channel());
877 if (!video_channel) {
878 continue;
879 }
880 video_channel->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700881 }
Steve Antonafb0bb72018-02-20 11:35:37 -0800882
stefanf79ade12017-06-02 06:44:03 -0700883 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
884 StatsReport* report = reports_.FindOrAddNew(report_id);
885 ExtractStats(bwe_info, stats_gathering_started_, report);
886}
887
Steve Antonb8867112018-02-13 10:07:54 -0800888namespace {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000889
Steve Antonb8867112018-02-13 10:07:54 -0800890struct VoiceChannelStatsInfo {
891 std::string transport_name;
892 cricket::VoiceMediaChannel* voice_media_channel;
893 cricket::VoiceMediaInfo voice_media_info;
894};
895
896struct VideoChannelStatsInfo {
897 std::string transport_name;
898 cricket::VideoMediaChannel* video_media_channel;
899 cricket::VideoMediaInfo video_media_info;
900};
901
902} // namespace
903
904void StatsCollector::ExtractMediaInfo() {
905 RTC_DCHECK_RUN_ON(pc_->signaling_thread());
906
907 std::vector<VoiceChannelStatsInfo> voice_channel_infos;
908 std::vector<VideoChannelStatsInfo> video_channel_infos;
909
910 {
911 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
912 for (auto transceiver : pc_->GetTransceiversInternal()) {
913 if (!transceiver->internal()->channel()) {
914 continue;
915 }
916 cricket::MediaType media_type = transceiver->internal()->media_type();
917 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
918 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
919 transceiver->internal()->channel());
920 voice_channel_infos.emplace_back();
921 VoiceChannelStatsInfo& info = voice_channel_infos.back();
922 info.transport_name = voice_channel->transport_name();
923 info.voice_media_channel = voice_channel->media_channel();
924 } else {
925 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
926 auto* video_channel = static_cast<cricket::VideoChannel*>(
927 transceiver->internal()->channel());
928 video_channel_infos.emplace_back();
929 VideoChannelStatsInfo& info = video_channel_infos.back();
930 info.transport_name = video_channel->transport_name();
931 info.video_media_channel = video_channel->media_channel();
932 }
933 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000935
Steve Antonb8867112018-02-13 10:07:54 -0800936 pc_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&] {
937 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
938 for (auto it = voice_channel_infos.begin(); it != voice_channel_infos.end();
939 /* incremented manually */) {
940 if (!it->voice_media_channel->GetStats(&it->voice_media_info)) {
941 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats";
942 it = voice_channel_infos.erase(it);
943 continue;
944 }
945 ++it;
946 }
947 for (auto it = video_channel_infos.begin(); it != video_channel_infos.end();
948 /* incremented manually */) {
949 if (!it->video_media_channel->GetStats(&it->video_media_info)) {
950 RTC_LOG(LS_ERROR) << "Failed to get video channel stats";
951 it = video_channel_infos.erase(it);
952 continue;
953 }
954 ++it;
955 }
956 });
957
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000958 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
959
Steve Antonb8867112018-02-13 10:07:54 -0800960 bool has_remote_audio = false;
961 for (const auto& info : voice_channel_infos) {
962 StatsReport::Id transport_id = StatsReport::NewComponentId(
963 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
964 ExtractStatsFromList(info.voice_media_info.receivers, transport_id, this,
965 StatsReport::kReceive);
966 ExtractStatsFromList(info.voice_media_info.senders, transport_id, this,
967 StatsReport::kSend);
968 if (!info.voice_media_info.receivers.empty()) {
969 has_remote_audio = true;
970 }
Steve Anton74116482017-12-18 11:00:14 -0800971 }
Steve Antonb8867112018-02-13 10:07:54 -0800972 for (const auto& info : video_channel_infos) {
973 StatsReport::Id transport_id = StatsReport::NewComponentId(
974 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
975 ExtractStatsFromList(info.video_media_info.receivers, transport_id, this,
976 StatsReport::kReceive);
977 ExtractStatsFromList(info.video_media_info.senders, transport_id, this,
978 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000980
Steve Antonb8867112018-02-13 10:07:54 -0800981 UpdateStatsFromExistingLocalAudioTracks(has_remote_audio);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982}
983
nissefcc640f2016-04-01 01:10:42 -0700984void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700985 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700986
987 for (const auto& sender : pc_->GetSenders()) {
988 // TODO(nisse): SSRC == 0 currently means none. Delete check when
989 // that is fixed.
990 if (!sender->ssrc()) {
991 continue;
992 }
993 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
994 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
995 continue;
996 }
997 // Safe, because kind() == kVideoKind implies a subclass of
998 // VideoTrackInterface; see mediastreaminterface.h.
999 VideoTrackSourceInterface* source =
1000 static_cast<VideoTrackInterface*>(track.get())->GetSource();
1001
1002 VideoTrackSourceInterface::Stats stats;
1003 if (!source->GetStats(&stats)) {
1004 continue;
1005 }
1006 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
1007 StatsReport::kStatsReportTypeSsrc,
1008 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
1009 StatsReport* report = reports_.FindOrAddNew(stats_id);
1010 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
1011 stats.input_width);
1012 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
1013 stats.input_height);
1014 }
1015}
1016
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001017void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001018 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001019
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001020 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1021
deadbeefab9b2d12015-10-14 11:33:11 -07001022 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001023 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001024 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001025 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001026 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001027 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -07001028 // Filter out the initial id (-1).
1029 if (dc->id() >= 0) {
1030 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
1031 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001032 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
1033 report->AddString(StatsReport::kStatsValueNameState,
1034 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001035 }
1036}
1037
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001038StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001039 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001040 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001041 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001042 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
1043 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001044 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001045}
1046
Ivo Creusenae026092017-11-20 13:07:16 +01001047void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
1048 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001049 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001050 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001051 for (const auto& it : local_audio_tracks_) {
1052 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001053 uint32_t ssrc = it.second;
1054 StatsReport* report =
1055 GetReport(StatsReport::kStatsReportTypeSsrc,
1056 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001057 if (report == NULL) {
1058 // This can happen if a local audio track is added to a stream on the
1059 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001060 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001061 continue;
1062 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001063
1064 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001065 const StatsReport::Value* v =
1066 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001067 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001068 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001069
jbauchbe24c942015-06-22 15:06:43 -07001070 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001071 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001072 }
1073}
1074
1075void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001076 StatsReport* report,
1077 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001078 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001079 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001080
andrew2fe1cb02015-11-27 17:27:35 -08001081 // Don't overwrite report values if they're not available.
1082 int signal_level;
1083 if (track->GetSignalLevel(&signal_level)) {
1084 RTC_DCHECK_GE(signal_level, 0);
1085 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1086 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001087
andrew2fe1cb02015-11-27 17:27:35 -08001088 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001089
andrew2fe1cb02015-11-27 17:27:35 -08001090 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001091 AudioProcessorInterface::AudioProcessorStatistics stats =
1092 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001093
Ivo Creusen56d46092017-11-24 17:29:59 +01001094 SetAudioProcessingStats(report, stats.typing_noise_detected,
1095 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001096 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001097}
1098
Peter Boström0c4e06b2015-10-07 12:23:21 +02001099bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1100 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001101 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001102 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001103 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001104 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001105 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1106 << " is not associated with a sending track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001107 return false;
1108 }
1109 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001110 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001111 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001112 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1113 << " is not associated with a receiving track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001114 return false;
1115 }
1116 }
1117
1118 return true;
1119}
1120
jbauchbe24c942015-06-22 15:06:43 -07001121void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001122 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001123
1124 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1125
1126 for (const auto& entry : track_ids_) {
1127 StatsReport* report = entry.second;
1128 report->set_timestamp(stats_gathering_started_);
1129 }
jbauchbe24c942015-06-22 15:06:43 -07001130}
1131
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001132void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001133 stats_gathering_started_ = 0;
1134}
1135
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136} // namespace webrtc