blob: 0018b10c8b40d907a397cc0dbc8c7c0474361387 [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
Yves Gerey665174f2018-06-19 15:03:05 +020043template <typename ValueType>
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000044struct 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[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200136 {StatsReport::kStatsValueNameExpandRate, info.expand_rate},
137 {StatsReport::kStatsValueNameSecondaryDecodedRate,
138 info.secondary_decoded_rate},
139 {StatsReport::kStatsValueNameSecondaryDiscardedRate,
140 info.secondary_discarded_rate},
141 {StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate},
142 {StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate},
143 {StatsReport::kStatsValueNamePreemptiveExpandRate,
144 info.preemptive_expand_rate},
145 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy},
146 {StatsReport::kStatsValueNameTotalSamplesDuration,
147 info.total_output_duration}};
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000148
149 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200150 {StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms},
151 {StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng},
152 {StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq},
153 {StatsReport::kStatsValueNameDecodingCTSG,
154 info.decoding_calls_to_silence_generator},
155 {StatsReport::kStatsValueNameDecodingMutedOutput,
156 info.decoding_muted_output},
157 {StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal},
158 {StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc},
159 {StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng},
160 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
161 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
162 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
163 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
164 {StatsReport::kStatsValueNamePreferredJitterBufferMs,
165 info.jitter_buffer_preferred_ms},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000166 };
167
168 for (const auto& f : floats)
169 report->AddFloat(f.name, f.value);
170
171 for (const auto& i : ints)
172 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700173 if (info.audio_level >= 0) {
174 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
175 info.audio_level);
176 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000177
Yves Gerey665174f2018-06-19 15:03:05 +0200178 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700179 if (info.capture_start_ntp_time_ms >= 0) {
180 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
181 info.capture_start_ntp_time_ms);
182 }
fippobec70ab2016-01-28 01:27:15 -0800183 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184}
185
186void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000187 ExtractCommonSendProperties(info, report);
188
Ivo Creusen56d46092017-11-24 17:29:59 +0100189 SetAudioProcessingStats(report, info.typing_noise_detected,
190 info.apm_statistics);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000191
zsteine76bd3a2017-07-14 12:17:49 -0700192 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200193 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy},
194 {StatsReport::kStatsValueNameTotalSamplesDuration,
195 info.total_input_duration}};
zsteine76bd3a2017-07-14 12:17:49 -0700196
andrew2fe1cb02015-11-27 17:27:35 -0800197 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000198 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200199 {StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
200 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
201 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
202 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203 };
204
zsteine76bd3a2017-07-14 12:17:49 -0700205 for (const auto& f : floats) {
206 report->AddFloat(f.name, f.value);
207 }
208
zhihuang6ba3b192016-05-13 11:46:35 -0700209 for (const auto& i : ints) {
210 if (i.value >= 0) {
211 report->AddInt(i.name, i.value);
212 }
213 }
fippobec70ab2016-01-28 01:27:15 -0800214 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700215 if (info.ana_statistics.bitrate_action_counter) {
216 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
217 *info.ana_statistics.bitrate_action_counter);
218 }
219 if (info.ana_statistics.channel_action_counter) {
220 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
221 *info.ana_statistics.channel_action_counter);
222 }
223 if (info.ana_statistics.dtx_action_counter) {
224 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
225 *info.ana_statistics.dtx_action_counter);
226 }
227 if (info.ana_statistics.fec_action_counter) {
228 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
229 *info.ana_statistics.fec_action_counter);
230 }
ivoc0d0b9122017-09-08 13:24:21 -0700231 if (info.ana_statistics.frame_length_increase_counter) {
232 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
233 *info.ana_statistics.frame_length_increase_counter);
234 }
235 if (info.ana_statistics.frame_length_decrease_counter) {
236 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
237 *info.ana_statistics.frame_length_decrease_counter);
238 }
239 if (info.ana_statistics.uplink_packet_loss_fraction) {
240 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
241 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243}
244
245void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700246 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100247 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
248 info.decoder_implementation_name);
Yves Gerey665174f2018-06-19 15:03:05 +0200249 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700250 if (info.capture_start_ntp_time_ms >= 0) {
251 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
252 info.capture_start_ntp_time_ms);
253 }
sakalcc452e12017-02-09 04:53:45 -0800254 if (info.qp_sum)
255 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
256
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000257 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200258 {StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms},
259 {StatsReport::kStatsValueNameDecodeMs, info.decode_ms},
260 {StatsReport::kStatsValueNameFirsSent, info.firs_sent},
261 {StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height},
262 {StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded},
263 {StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output},
264 {StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd},
265 {StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width},
266 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
267 {StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms},
268 {StatsReport::kStatsValueNameMinPlayoutDelayMs,
269 info.min_playout_delay_ms},
270 {StatsReport::kStatsValueNameNacksSent, info.nacks_sent},
271 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
272 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
273 {StatsReport::kStatsValueNamePlisSent, info.plis_sent},
274 {StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms},
275 {StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms},
276 {StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000277 };
278
279 for (const auto& i : ints)
280 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800281 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700282
ilnik2edc6842017-07-06 03:06:50 -0700283 if (info.timing_frame_info) {
284 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
285 info.timing_frame_info->ToString());
286 }
ilnikf04afde2017-07-07 01:26:24 -0700287
ilnika79cc282017-08-23 05:24:10 -0700288 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
289 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700290
291 report->AddString(
292 StatsReport::kStatsValueNameContentType,
293 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294}
295
296void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000297 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298
Peter Boströmb7d9a972015-12-18 16:01:11 +0100299 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
300 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000301 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
302 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000303 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
304 (info.adapt_reason & 0x1) > 0);
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100305 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
306 info.has_entered_low_resolution);
307
sakal87da4042016-10-31 06:53:47 -0700308 if (info.qp_sum)
309 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000310
311 const IntForAdd ints[] = {
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100312 {StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes},
313 {StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms},
314 {StatsReport::kStatsValueNameEncodeUsagePercent,
315 info.encode_usage_percent},
316 {StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd},
317 {StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height},
318 {StatsReport::kStatsValueNameFrameRateInput, info.framerate_input},
319 {StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent},
320 {StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width},
321 {StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd},
322 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
323 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
324 {StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd},
325 {StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded},
326 {StatsReport::kStatsValueNameHugeFramesSent, info.huge_frames_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000327 };
328
329 for (const auto& i : ints)
330 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800331 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700332 report->AddString(
333 StatsReport::kStatsValueNameContentType,
334 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335}
336
337void ExtractStats(const cricket::BandwidthEstimationInfo& info,
338 double stats_gathering_started,
339 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700340 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000342 report->set_timestamp(stats_gathering_started);
343 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200344 {StatsReport::kStatsValueNameAvailableSendBandwidth,
345 info.available_send_bandwidth},
346 {StatsReport::kStatsValueNameAvailableReceiveBandwidth,
347 info.available_recv_bandwidth},
348 {StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate},
349 {StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate},
350 {StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate},
351 {StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000352 };
353 for (const auto& i : ints)
354 report->AddInt(i.name, i.value);
355 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356}
357
wu@webrtc.org97077a32013-10-25 21:18:33 +0000358void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
359 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000360 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000361 // TODO(hta): Extract some stats here.
362}
363
364void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
365 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000366 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000367 // TODO(hta): Extract some stats here.
368}
369
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000371// In order to use the template, the functions that are called from it,
372// ExtractStats and ExtractRemoteStats, must be defined and overloaded
373// for each type.
Yves Gerey665174f2018-06-19 15:03:05 +0200374template <typename T>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000376 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000377 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000378 StatsReport::Direction direction) {
379 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200380 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000381 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000382 // TODO(hta): Handle the case of multiple SSRCs per object.
Yves Gerey665174f2018-06-19 15:03:05 +0200383 StatsReport* report =
384 collector->PrepareReport(true, ssrc, transport_id, direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000385 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000386 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000387
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000388 if (!d.remote_stats.empty()) {
389 report = collector->PrepareReport(false, ssrc, transport_id, direction);
390 if (report)
391 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000392 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000394}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395
396} // namespace
397
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000398const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
399 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
400 return STATSREPORT_LOCAL_PORT_TYPE;
401 }
402 if (candidate_type == cricket::STUN_PORT_TYPE) {
403 return STATSREPORT_STUN_PORT_TYPE;
404 }
405 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
406 return STATSREPORT_PRFLX_PORT_TYPE;
407 }
408 if (candidate_type == cricket::RELAY_PORT_TYPE) {
409 return STATSREPORT_RELAY_PORT_TYPE;
410 }
nisseeb4ca4e2017-01-12 02:24:27 -0800411 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000412 return "unknown";
413}
414
415const char* AdapterTypeToStatsType(rtc::AdapterType type) {
416 switch (type) {
Qingsi Wang9f1de692018-06-28 15:38:09 -0700417 case rtc::ADAPTER_TYPE_ANY:
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000418 case rtc::ADAPTER_TYPE_UNKNOWN:
419 return "unknown";
420 case rtc::ADAPTER_TYPE_ETHERNET:
421 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
422 case rtc::ADAPTER_TYPE_WIFI:
423 return STATSREPORT_ADAPTER_TYPE_WIFI;
424 case rtc::ADAPTER_TYPE_CELLULAR:
425 return STATSREPORT_ADAPTER_TYPE_WWAN;
426 case rtc::ADAPTER_TYPE_VPN:
427 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000428 case rtc::ADAPTER_TYPE_LOOPBACK:
429 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000430 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800431 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000432 return "";
433 }
434}
435
Steve Anton2d8609c2018-01-23 16:38:46 -0800436StatsCollector::StatsCollector(PeerConnectionInternal* pc)
deadbeefab9b2d12015-10-14 11:33:11 -0700437 : pc_(pc), stats_gathering_started_(0) {
438 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000439}
440
441StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700442 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443}
444
nissecdf37a92016-09-13 23:41:47 -0700445// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000446double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700447 return rtc::TimeUTCMicros() /
448 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000449}
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451// Adds a MediaStream with tracks that can be used as a |selector| in a call
452// to GetStats.
453void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700454 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700455 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456
Steve Anton36b29d12017-10-30 09:57:42 -0700457 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
458 &track_ids_);
459 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
460 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461}
462
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100463void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
464 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
465 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
466 &track_ids_);
467 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
468 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
469 &track_ids_);
470 } else {
471 RTC_NOTREACHED() << "Illegal track kind";
472 }
473}
474
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000475void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200476 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700477 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700478 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700479#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000480 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700481 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000482#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000483
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000484 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000485
486 // Create the kStatsReportTypeTrack report for the new track if there is no
487 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000488 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
489 audio_track->id()));
490 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000491 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000492 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000493 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000494 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000495}
496
497void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200498 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700499 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700500 local_audio_tracks_.erase(
501 std::remove_if(
502 local_audio_tracks_.begin(), local_audio_tracks_.end(),
503 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
504 return track.first == audio_track && track.second == ssrc;
505 }),
506 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000507}
508
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000509void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700511 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700512 RTC_DCHECK(reports != NULL);
513 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000515 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
516
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000518 reports->reserve(reports_.size());
519 for (auto* r : reports_)
520 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000521 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 }
523
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000524 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700525 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000526 if (report)
527 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528
Yves Gerey665174f2018-06-19 15:03:05 +0200529 report = reports_.Find(
530 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000532 if (!report)
533 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000535 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536
537 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000538 for (const auto* r : reports_) {
539 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000541
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000542 const StatsReport::Value* v =
543 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000544 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000545 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547}
548
Yves Gerey665174f2018-06-19 15:03:05 +0200549void StatsCollector::UpdateStats(
550 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700551 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 double time_now = GetTimeNow();
553 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
554 // ms apart will be ignored.
555 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000556 if (stats_gathering_started_ != 0 &&
557 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700558 RTC_LOG(LS_INFO)
559 << "Not updating stats again, since they were updated within "
560 << kMinGatherStatsPeriod << "ms.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 return;
562 }
563 stats_gathering_started_ = time_now;
564
Steve Anton75737c02017-11-06 10:37:17 -0800565 // TODO(tommi): All of these hop over to the worker thread to fetch
566 // information. We could use an AsyncInvoker to run all of these and post
567 // the information back to the signaling thread where we can create and
568 // update stats reports. That would also clean up the threading story a bit
569 // since we'd be creating/updating the stats report objects consistently on
570 // the same thread (this class has no locks right now).
571 ExtractSessionInfo();
572 ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800573 ExtractMediaInfo();
Steve Anton75737c02017-11-06 10:37:17 -0800574 ExtractSenderInfo();
575 ExtractDataInfo();
576 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577}
578
Yves Gerey665174f2018-06-19 15:03:05 +0200579StatsReport* StatsCollector::PrepareReport(bool local,
580 uint32_t ssrc,
581 const StatsReport::Id& transport_id,
582 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700583 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000584 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200585 local ? StatsReport::kStatsReportTypeSsrc
586 : StatsReport::kStatsReportTypeRemoteSsrc,
587 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000588 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589
xians@webrtc.org01bda202014-07-09 07:38:38 +0000590 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000592 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700593 // The SSRC is not used by any existing track (or lookup failed since the
594 // SSRC wasn't signaled in SDP). Try copying the track ID from a previous
595 // report: if one exists.
596 if (report) {
597 const StatsReport::Value* v =
598 report->FindValue(StatsReport::kStatsValueNameTrackId);
599 if (v)
600 track_id = v->string_val();
xians@webrtc.org01bda202014-07-09 07:38:38 +0000601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 }
603
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000604 if (!report)
605 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000607 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000608 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000610 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700611 if (!track_id.empty()) {
612 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
613 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000615 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 return report;
617}
618
zhihuange9e94c32016-11-04 11:38:15 -0700619bool StatsCollector::IsValidTrack(const std::string& track_id) {
620 return reports_.Find(StatsReport::NewTypedId(
621 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
622}
623
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000624StatsReport* StatsCollector::AddCertificateReports(
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800625 std::unique_ptr<rtc::SSLCertificateStats> cert_stats) {
Steve Anton978b8762017-09-29 12:15:02 -0700626 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000627
hbose29352b2016-08-25 03:52:38 -0700628 StatsReport* first_report = nullptr;
629 StatsReport* prev_report = nullptr;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800630 for (rtc::SSLCertificateStats* stats = cert_stats.get(); stats;
hbose29352b2016-08-25 03:52:38 -0700631 stats = stats->issuer.get()) {
632 StatsReport::Id id(StatsReport::NewTypedId(
633 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
634
635 StatsReport* report = reports_.ReplaceOrAddNew(id);
636 report->set_timestamp(stats_gathering_started_);
637 report->AddString(StatsReport::kStatsValueNameFingerprint,
638 stats->fingerprint);
639 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
640 stats->fingerprint_algorithm);
641 report->AddString(StatsReport::kStatsValueNameDer,
642 stats->base64_certificate);
643 if (!first_report)
644 first_report = report;
645 else
646 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
647 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000648 }
hbose29352b2016-08-25 03:52:38 -0700649 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000650}
651
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000652StatsReport* StatsCollector::AddConnectionInfoReport(
Yves Gerey665174f2018-06-19 15:03:05 +0200653 const std::string& content_name,
654 int component,
655 int connection_id,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000656 const StatsReport::Id& channel_report_id,
657 const cricket::ConnectionInfo& info) {
Yves Gerey665174f2018-06-19 15:03:05 +0200658 StatsReport::Id id(
659 StatsReport::NewCandidatePairId(content_name, component, connection_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000660 StatsReport* report = reports_.ReplaceOrAddNew(id);
661 report->set_timestamp(stats_gathering_started_);
662
663 const BoolForAdd bools[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200664 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
665 {StatsReport::kStatsValueNameReceiving, info.receiving},
666 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000667 };
668 for (const auto& b : bools)
669 report->AddBoolean(b.name, b.value);
670
671 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
Qingsi Wang72a43a12018-02-20 16:03:18 -0800672 cricket::CandidateStats local_candidate_stats(info.local_candidate);
673 cricket::CandidateStats remote_candidate_stats(info.remote_candidate);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000674 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800675 AddCandidateReport(local_candidate_stats, true)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000676 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800677 AddCandidateReport(remote_candidate_stats, false)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000678
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(
Qingsi Wang72a43a12018-02-20 16:03:18 -0800712 const cricket::CandidateStats& candidate_stats,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000713 bool local) {
Qingsi Wang72a43a12018-02-20 16:03:18 -0800714 const auto& candidate = candidate_stats.candidate;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000715 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
716 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000717 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000718 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000719 report->set_timestamp(stats_gathering_started_);
720 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000721 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
722 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000723 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000724 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
725 candidate.address().ipaddr().ToString());
726 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
727 candidate.address().PortAsString());
728 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
729 candidate.priority());
730 report->AddString(StatsReport::kStatsValueNameCandidateType,
731 IceCandidateTypeToStatsType(candidate.type()));
732 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
733 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000734 }
735
Qingsi Wang4ff54432018-03-01 18:25:20 -0800736 if (local && candidate_stats.stun_stats.has_value()) {
737 const auto& stun_stats = candidate_stats.stun_stats.value();
738 report->AddInt64(StatsReport::kStatsValueNameSentStunKeepaliveRequests,
739 stun_stats.stun_binding_requests_sent);
740 report->AddInt64(StatsReport::kStatsValueNameRecvStunKeepaliveResponses,
741 stun_stats.stun_binding_responses_received);
742 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttTotal,
743 stun_stats.stun_binding_rtt_ms_total);
744 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttSquaredTotal,
745 stun_stats.stun_binding_rtt_ms_squared_total);
746 }
747
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000748 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000749}
750
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700752 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000753
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000755 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700756 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000757 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000758 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000759 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700760 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761
Qingsi Wang72a43a12018-02-20 16:03:18 -0800762 cricket::CandidateStatsList pooled_candidate_stats_list =
763 pc_->GetPooledCandidateStats();
764
765 for (const cricket::CandidateStats& stats : pooled_candidate_stats_list) {
766 AddCandidateReport(stats, true);
767 }
768
Steve Anton5dfde182018-02-06 10:34:40 -0800769 std::set<std::string> transport_names;
770 for (const auto& entry : pc_->GetTransportNamesByMid()) {
771 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000772 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773
Steve Anton5dfde182018-02-06 10:34:40 -0800774 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
775 pc_->GetTransportStatsByNames(transport_names);
776
777 for (const auto& entry : transport_stats_by_name) {
778 const std::string& transport_name = entry.first;
779 const cricket::TransportStats& transport_stats = entry.second;
780
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000781 // Attempt to get a copy of the certificates from the transport and
782 // expose them in stats reports. All channels in a transport share the
783 // same local and remote certificates.
784 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000785 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200786 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton5dfde182018-02-06 10:34:40 -0800787 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800788 StatsReport* r =
789 AddCertificateReports(certificate->ssl_cert_chain().GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000790 if (r)
791 local_cert_report_id = r->id();
792 }
793
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800794 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
795 pc_->GetRemoteSSLCertChain(transport_name);
796 if (remote_cert_chain) {
797 StatsReport* r = AddCertificateReports(remote_cert_chain->GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000798 if (r)
799 remote_cert_report_id = r->id();
800 }
801
Steve Anton5dfde182018-02-06 10:34:40 -0800802 for (const auto& channel_iter : transport_stats.channel_stats) {
803 StatsReport::Id id(
804 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000805 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
806 channel_report->set_timestamp(stats_gathering_started_);
807 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
808 channel_iter.component);
809 if (local_cert_report_id.get()) {
810 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
811 local_cert_report_id);
812 }
813 if (remote_cert_report_id.get()) {
814 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
815 remote_cert_report_id);
816 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800817 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
818 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
819 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
820 channel_report->AddString(
821 StatsReport::kStatsValueNameSrtpCipher,
822 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000823 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800824 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
825 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
826 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
827 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700828 channel_report->AddString(
829 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800830 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000831 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000832
Qingsi Wang72a43a12018-02-20 16:03:18 -0800833 // Collect stats for non-pooled candidates. Note that the reports
834 // generated here supersedes the candidate reports generated in
835 // AddConnectionInfoReport below, and they may report candidates that are
836 // not paired. Also, the candidate report generated in
837 // AddConnectionInfoReport do not report port stats like StunStats.
838 for (const cricket::CandidateStats& stats :
839 channel_iter.candidate_stats_list) {
840 AddCandidateReport(stats, true);
841 }
842
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000843 int connection_id = 0;
844 for (const cricket::ConnectionInfo& info :
Yves Gerey665174f2018-06-19 15:03:05 +0200845 channel_iter.connection_infos) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000846 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800847 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000848 channel_report->id(), info);
849 if (info.best_connection) {
850 channel_report->AddId(
851 StatsReport::kStatsValueNameSelectedCandidatePairId,
852 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854 }
855 }
856 }
857}
858
stefanf79ade12017-06-02 06:44:03 -0700859void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700860 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700861
Steve Anton978b8762017-09-29 12:15:02 -0700862 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700863 return;
864
Steve Anton978b8762017-09-29 12:15:02 -0700865 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700866 cricket::BandwidthEstimationInfo bwe_info;
867 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
868 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
869 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
Steve Antonafb0bb72018-02-20 11:35:37 -0800870
stefanf79ade12017-06-02 06:44:03 -0700871 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
872 // TODO(holmer): Also fill this in for audio.
Steve Antonafb0bb72018-02-20 11:35:37 -0800873 for (auto transceiver : pc_->GetTransceiversInternal()) {
874 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) {
875 continue;
876 }
877 auto* video_channel =
878 static_cast<cricket::VideoChannel*>(transceiver->internal()->channel());
879 if (!video_channel) {
880 continue;
881 }
882 video_channel->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700883 }
Steve Antonafb0bb72018-02-20 11:35:37 -0800884
stefanf79ade12017-06-02 06:44:03 -0700885 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
886 StatsReport* report = reports_.FindOrAddNew(report_id);
887 ExtractStats(bwe_info, stats_gathering_started_, report);
888}
889
Steve Antonb8867112018-02-13 10:07:54 -0800890namespace {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000891
Steve Antonb8867112018-02-13 10:07:54 -0800892struct VoiceChannelStatsInfo {
893 std::string transport_name;
894 cricket::VoiceMediaChannel* voice_media_channel;
895 cricket::VoiceMediaInfo voice_media_info;
896};
897
898struct VideoChannelStatsInfo {
899 std::string transport_name;
900 cricket::VideoMediaChannel* video_media_channel;
901 cricket::VideoMediaInfo video_media_info;
902};
903
904} // namespace
905
906void StatsCollector::ExtractMediaInfo() {
907 RTC_DCHECK_RUN_ON(pc_->signaling_thread());
908
909 std::vector<VoiceChannelStatsInfo> voice_channel_infos;
910 std::vector<VideoChannelStatsInfo> video_channel_infos;
911
912 {
913 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
914 for (auto transceiver : pc_->GetTransceiversInternal()) {
915 if (!transceiver->internal()->channel()) {
916 continue;
917 }
918 cricket::MediaType media_type = transceiver->internal()->media_type();
919 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
920 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
921 transceiver->internal()->channel());
922 voice_channel_infos.emplace_back();
923 VoiceChannelStatsInfo& info = voice_channel_infos.back();
924 info.transport_name = voice_channel->transport_name();
925 info.voice_media_channel = voice_channel->media_channel();
926 } else {
927 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
928 auto* video_channel = static_cast<cricket::VideoChannel*>(
929 transceiver->internal()->channel());
930 video_channel_infos.emplace_back();
931 VideoChannelStatsInfo& info = video_channel_infos.back();
932 info.transport_name = video_channel->transport_name();
933 info.video_media_channel = video_channel->media_channel();
934 }
935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000937
Steve Antonb8867112018-02-13 10:07:54 -0800938 pc_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&] {
939 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
940 for (auto it = voice_channel_infos.begin(); it != voice_channel_infos.end();
941 /* incremented manually */) {
942 if (!it->voice_media_channel->GetStats(&it->voice_media_info)) {
943 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats";
944 it = voice_channel_infos.erase(it);
945 continue;
946 }
947 ++it;
948 }
949 for (auto it = video_channel_infos.begin(); it != video_channel_infos.end();
950 /* incremented manually */) {
951 if (!it->video_media_channel->GetStats(&it->video_media_info)) {
952 RTC_LOG(LS_ERROR) << "Failed to get video channel stats";
953 it = video_channel_infos.erase(it);
954 continue;
955 }
956 ++it;
957 }
958 });
959
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000960 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
961
Steve Antonb8867112018-02-13 10:07:54 -0800962 bool has_remote_audio = false;
963 for (const auto& info : voice_channel_infos) {
964 StatsReport::Id transport_id = StatsReport::NewComponentId(
965 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
966 ExtractStatsFromList(info.voice_media_info.receivers, transport_id, this,
967 StatsReport::kReceive);
968 ExtractStatsFromList(info.voice_media_info.senders, transport_id, this,
969 StatsReport::kSend);
970 if (!info.voice_media_info.receivers.empty()) {
971 has_remote_audio = true;
972 }
Steve Anton74116482017-12-18 11:00:14 -0800973 }
Steve Antonb8867112018-02-13 10:07:54 -0800974 for (const auto& info : video_channel_infos) {
975 StatsReport::Id transport_id = StatsReport::NewComponentId(
976 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
977 ExtractStatsFromList(info.video_media_info.receivers, transport_id, this,
978 StatsReport::kReceive);
979 ExtractStatsFromList(info.video_media_info.senders, transport_id, this,
980 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000982
Steve Antonb8867112018-02-13 10:07:54 -0800983 UpdateStatsFromExistingLocalAudioTracks(has_remote_audio);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984}
985
nissefcc640f2016-04-01 01:10:42 -0700986void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700987 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700988
989 for (const auto& sender : pc_->GetSenders()) {
990 // TODO(nisse): SSRC == 0 currently means none. Delete check when
991 // that is fixed.
992 if (!sender->ssrc()) {
993 continue;
994 }
995 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
996 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
997 continue;
998 }
999 // Safe, because kind() == kVideoKind implies a subclass of
1000 // VideoTrackInterface; see mediastreaminterface.h.
1001 VideoTrackSourceInterface* source =
1002 static_cast<VideoTrackInterface*>(track.get())->GetSource();
1003
1004 VideoTrackSourceInterface::Stats stats;
1005 if (!source->GetStats(&stats)) {
1006 continue;
1007 }
1008 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
1009 StatsReport::kStatsReportTypeSsrc,
1010 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
1011 StatsReport* report = reports_.FindOrAddNew(stats_id);
1012 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
1013 stats.input_width);
1014 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
1015 stats.input_height);
1016 }
1017}
1018
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001019void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001020 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001021
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001022 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1023
deadbeefab9b2d12015-10-14 11:33:11 -07001024 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001025 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001026 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001027 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001028 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001029 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -07001030 // Filter out the initial id (-1).
1031 if (dc->id() >= 0) {
1032 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
1033 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001034 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
1035 report->AddString(StatsReport::kStatsValueNameState,
1036 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001037 }
1038}
1039
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001040StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001041 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001042 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001043 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001044 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
1045 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001046 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001047}
1048
Ivo Creusenae026092017-11-20 13:07:16 +01001049void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
1050 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001051 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001052 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001053 for (const auto& it : local_audio_tracks_) {
1054 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001055 uint32_t ssrc = it.second;
1056 StatsReport* report =
1057 GetReport(StatsReport::kStatsReportTypeSsrc,
1058 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001059 if (report == NULL) {
1060 // This can happen if a local audio track is added to a stream on the
1061 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001062 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001063 continue;
1064 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001065
1066 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001067 const StatsReport::Value* v =
1068 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001069 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001070 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001071
jbauchbe24c942015-06-22 15:06:43 -07001072 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001073 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001074 }
1075}
1076
1077void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001078 StatsReport* report,
1079 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001080 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001081 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001082
andrew2fe1cb02015-11-27 17:27:35 -08001083 // Don't overwrite report values if they're not available.
1084 int signal_level;
1085 if (track->GetSignalLevel(&signal_level)) {
1086 RTC_DCHECK_GE(signal_level, 0);
1087 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1088 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001089
andrew2fe1cb02015-11-27 17:27:35 -08001090 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001091
andrew2fe1cb02015-11-27 17:27:35 -08001092 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001093 AudioProcessorInterface::AudioProcessorStatistics stats =
1094 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001095
Ivo Creusen56d46092017-11-24 17:29:59 +01001096 SetAudioProcessingStats(report, stats.typing_noise_detected,
1097 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001098 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001099}
1100
Peter Boström0c4e06b2015-10-07 12:23:21 +02001101bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1102 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001103 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001104 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001105 if (direction == StatsReport::kSend) {
Steve Anton978b8762017-09-29 12:15:02 -07001106 if (!pc_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001107 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1108 << " is not associated with a sending track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001109 return false;
1110 }
1111 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001112 RTC_DCHECK(direction == StatsReport::kReceive);
Steve Anton978b8762017-09-29 12:15:02 -07001113 if (!pc_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001114 RTC_LOG(LS_WARNING) << "The SSRC " << ssrc
1115 << " is not associated with a receiving track";
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001116 return false;
1117 }
1118 }
1119
1120 return true;
1121}
1122
jbauchbe24c942015-06-22 15:06:43 -07001123void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001124 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001125
1126 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1127
1128 for (const auto& entry : track_ids_) {
1129 StatsReport* report = entry.second;
1130 report->set_timestamp(stats_gathering_started_);
1131 }
jbauchbe24c942015-06-22 15:06:43 -07001132}
1133
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001134void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001135 stats_gathering_started_ = 0;
1136}
1137
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138} // namespace webrtc