blob: 6bfce676cac8dd9ce93d31ea13abfebfa361c6d2 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/stats_collector.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"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "pc/peer_connection.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 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800255 if (info.first_frame_received_to_decoded_ms >= 0) {
256 report->AddInt64(StatsReport::kStatsValueNameFirstFrameReceivedToDecodedMs,
257 info.first_frame_received_to_decoded_ms);
258 }
sakalcc452e12017-02-09 04:53:45 -0800259 if (info.qp_sum)
260 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
261
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200263 {StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms},
264 {StatsReport::kStatsValueNameDecodeMs, info.decode_ms},
265 {StatsReport::kStatsValueNameFirsSent, info.firs_sent},
266 {StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height},
267 {StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded},
268 {StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output},
269 {StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd},
270 {StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width},
271 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
272 {StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms},
273 {StatsReport::kStatsValueNameMinPlayoutDelayMs,
274 info.min_playout_delay_ms},
275 {StatsReport::kStatsValueNameNacksSent, info.nacks_sent},
276 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
277 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
278 {StatsReport::kStatsValueNamePlisSent, info.plis_sent},
279 {StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms},
280 {StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms},
281 {StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded},
Artem Titov8a3ab0e2018-07-27 14:52:57 +0000282 };
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000283
284 for (const auto& i : ints)
285 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800286 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700287
ilnik2edc6842017-07-06 03:06:50 -0700288 if (info.timing_frame_info) {
289 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
290 info.timing_frame_info->ToString());
291 }
ilnikf04afde2017-07-07 01:26:24 -0700292
ilnika79cc282017-08-23 05:24:10 -0700293 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
294 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700295
296 report->AddString(
297 StatsReport::kStatsValueNameContentType,
298 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299}
300
301void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000302 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
Peter Boströmb7d9a972015-12-18 16:01:11 +0100304 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
305 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000306 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
307 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000308 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
309 (info.adapt_reason & 0x1) > 0);
Ă…sa Perssonc3ed6302017-11-16 14:04:52 +0100310 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
311 info.has_entered_low_resolution);
312
sakal87da4042016-10-31 06:53:47 -0700313 if (info.qp_sum)
314 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000315
316 const IntForAdd ints[] = {
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100317 {StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes},
318 {StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms},
319 {StatsReport::kStatsValueNameEncodeUsagePercent,
320 info.encode_usage_percent},
321 {StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd},
322 {StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height},
323 {StatsReport::kStatsValueNameFrameRateInput, info.framerate_input},
324 {StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent},
325 {StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width},
326 {StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd},
327 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
328 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
329 {StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd},
330 {StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded},
331 {StatsReport::kStatsValueNameHugeFramesSent, info.huge_frames_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000332 };
333
334 for (const auto& i : ints)
335 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800336 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700337 report->AddString(
338 StatsReport::kStatsValueNameContentType,
339 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340}
341
342void ExtractStats(const cricket::BandwidthEstimationInfo& info,
343 double stats_gathering_started,
344 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700345 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000347 report->set_timestamp(stats_gathering_started);
348 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200349 {StatsReport::kStatsValueNameAvailableSendBandwidth,
350 info.available_send_bandwidth},
351 {StatsReport::kStatsValueNameAvailableReceiveBandwidth,
352 info.available_recv_bandwidth},
353 {StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate},
354 {StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate},
355 {StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate},
356 {StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000357 };
358 for (const auto& i : ints)
359 report->AddInt(i.name, i.value);
360 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361}
362
wu@webrtc.org97077a32013-10-25 21:18:33 +0000363void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
364 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000365 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000366 // TODO(hta): Extract some stats here.
367}
368
369void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
370 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000371 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000372 // TODO(hta): Extract some stats here.
373}
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000376// In order to use the template, the functions that are called from it,
377// ExtractStats and ExtractRemoteStats, must be defined and overloaded
378// for each type.
Yves Gerey665174f2018-06-19 15:03:05 +0200379template <typename T>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000381 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000382 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000383 StatsReport::Direction direction) {
384 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000386 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000387 // TODO(hta): Handle the case of multiple SSRCs per object.
Yves Gerey665174f2018-06-19 15:03:05 +0200388 StatsReport* report =
389 collector->PrepareReport(true, ssrc, transport_id, direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000390 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000391 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000392
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000393 if (!d.remote_stats.empty()) {
394 report = collector->PrepareReport(false, ssrc, transport_id, direction);
395 if (report)
396 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000397 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000399}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400
401} // namespace
402
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000403const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
404 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
405 return STATSREPORT_LOCAL_PORT_TYPE;
406 }
407 if (candidate_type == cricket::STUN_PORT_TYPE) {
408 return STATSREPORT_STUN_PORT_TYPE;
409 }
410 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
411 return STATSREPORT_PRFLX_PORT_TYPE;
412 }
413 if (candidate_type == cricket::RELAY_PORT_TYPE) {
414 return STATSREPORT_RELAY_PORT_TYPE;
415 }
nisseeb4ca4e2017-01-12 02:24:27 -0800416 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000417 return "unknown";
418}
419
420const char* AdapterTypeToStatsType(rtc::AdapterType type) {
421 switch (type) {
422 case rtc::ADAPTER_TYPE_UNKNOWN:
423 return "unknown";
424 case rtc::ADAPTER_TYPE_ETHERNET:
425 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
426 case rtc::ADAPTER_TYPE_WIFI:
427 return STATSREPORT_ADAPTER_TYPE_WIFI;
428 case rtc::ADAPTER_TYPE_CELLULAR:
429 return STATSREPORT_ADAPTER_TYPE_WWAN;
430 case rtc::ADAPTER_TYPE_VPN:
431 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000432 case rtc::ADAPTER_TYPE_LOOPBACK:
433 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
Qingsi Wang01560de2018-07-26 10:44:02 -0700434 case rtc::ADAPTER_TYPE_ANY:
435 return STATSREPORT_ADAPTER_TYPE_WILDCARD;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000436 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800437 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000438 return "";
439 }
440}
441
Steve Anton2d8609c2018-01-23 16:38:46 -0800442StatsCollector::StatsCollector(PeerConnectionInternal* pc)
deadbeefab9b2d12015-10-14 11:33:11 -0700443 : pc_(pc), stats_gathering_started_(0) {
444 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000445}
446
447StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700448 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
nissecdf37a92016-09-13 23:41:47 -0700451// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000452double StatsCollector::GetTimeNow() {
Minyue Li656d6092018-08-10 15:38:52 +0200453 return static_cast<double>(rtc::TimeUTCMillis());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000454}
455
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456// Adds a MediaStream with tracks that can be used as a |selector| in a call
457// to GetStats.
458void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700459 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700460 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
Steve Anton36b29d12017-10-30 09:57:42 -0700462 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
463 &track_ids_);
464 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
465 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466}
467
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100468void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
469 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
470 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
471 &track_ids_);
472 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
473 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
474 &track_ids_);
475 } else {
476 RTC_NOTREACHED() << "Illegal track kind";
477 }
478}
479
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000480void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200481 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700482 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700483 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700484#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000485 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700486 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000487#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000488
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000489 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000490
491 // Create the kStatsReportTypeTrack report for the new track if there is no
492 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000493 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
494 audio_track->id()));
495 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000496 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000497 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000498 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000499 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000500}
501
502void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200503 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700504 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700505 local_audio_tracks_.erase(
506 std::remove_if(
507 local_audio_tracks_.begin(), local_audio_tracks_.end(),
508 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
509 return track.first == audio_track && track.second == ssrc;
510 }),
511 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000512}
513
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000514void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700516 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700517 RTC_DCHECK(reports != NULL);
518 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000520 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
521
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000523 reports->reserve(reports_.size());
524 for (auto* r : reports_)
525 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000526 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 }
528
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000529 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700530 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000531 if (report)
532 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533
Yves Gerey665174f2018-06-19 15:03:05 +0200534 report = reports_.Find(
535 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000537 if (!report)
538 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000540 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
542 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000543 for (const auto* r : reports_) {
544 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000546
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000547 const StatsReport::Value* v =
548 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000549 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000550 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552}
553
Yves Gerey665174f2018-06-19 15:03:05 +0200554void StatsCollector::UpdateStats(
555 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700556 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 double time_now = GetTimeNow();
558 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
559 // ms apart will be ignored.
560 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000561 if (stats_gathering_started_ != 0 &&
562 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 return;
564 }
565 stats_gathering_started_ = time_now;
566
Steve Anton75737c02017-11-06 10:37:17 -0800567 // TODO(tommi): All of these hop over to the worker thread to fetch
568 // information. We could use an AsyncInvoker to run all of these and post
569 // the information back to the signaling thread where we can create and
570 // update stats reports. That would also clean up the threading story a bit
571 // since we'd be creating/updating the stats report objects consistently on
572 // the same thread (this class has no locks right now).
573 ExtractSessionInfo();
574 ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800575 ExtractMediaInfo();
Steve Anton75737c02017-11-06 10:37:17 -0800576 ExtractSenderInfo();
577 ExtractDataInfo();
578 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579}
580
Yves Gerey665174f2018-06-19 15:03:05 +0200581StatsReport* StatsCollector::PrepareReport(bool local,
582 uint32_t ssrc,
583 const StatsReport::Id& transport_id,
584 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700585 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000586 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200587 local ? StatsReport::kStatsReportTypeSsrc
588 : StatsReport::kStatsReportTypeRemoteSsrc,
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200589 rtc::ToString(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000590 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591
xians@webrtc.org01bda202014-07-09 07:38:38 +0000592 // Use the ID of the track that is currently mapped to the SSRC, if any.
Steve Antona41959e2018-11-28 11:15:33 -0800593 absl::string_view track_id = GetTrackIdBySsrc(ssrc, direction);
594 if (track_id.empty()) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700595 // The SSRC is not used by any existing track (or lookup failed since the
596 // SSRC wasn't signaled in SDP). Try copying the track ID from a previous
597 // report: if one exists.
598 if (report) {
599 const StatsReport::Value* v =
600 report->FindValue(StatsReport::kStatsValueNameTrackId);
Steve Antona41959e2018-11-28 11:15:33 -0800601 if (v) {
Taylor Brandstettera4653442018-06-19 09:44:26 -0700602 track_id = v->string_val();
Steve Antona41959e2018-11-28 11:15:33 -0800603 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606
Steve Antona41959e2018-11-28 11:15:33 -0800607 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000608 report = reports_.InsertNew(id);
Steve Antona41959e2018-11-28 11:15:33 -0800609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000611 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000612 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000614 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700615 if (!track_id.empty()) {
Steve Antona41959e2018-11-28 11:15:33 -0800616 report->AddString(StatsReport::kStatsValueNameTrackId,
617 std::string(track_id));
Taylor Brandstettera4653442018-06-19 09:44:26 -0700618 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000620 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 return report;
622}
623
zhihuange9e94c32016-11-04 11:38:15 -0700624bool StatsCollector::IsValidTrack(const std::string& track_id) {
625 return reports_.Find(StatsReport::NewTypedId(
626 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
627}
628
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000629StatsReport* StatsCollector::AddCertificateReports(
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800630 std::unique_ptr<rtc::SSLCertificateStats> cert_stats) {
Steve Anton978b8762017-09-29 12:15:02 -0700631 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000632
hbose29352b2016-08-25 03:52:38 -0700633 StatsReport* first_report = nullptr;
634 StatsReport* prev_report = nullptr;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800635 for (rtc::SSLCertificateStats* stats = cert_stats.get(); stats;
hbose29352b2016-08-25 03:52:38 -0700636 stats = stats->issuer.get()) {
637 StatsReport::Id id(StatsReport::NewTypedId(
638 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
639
640 StatsReport* report = reports_.ReplaceOrAddNew(id);
641 report->set_timestamp(stats_gathering_started_);
642 report->AddString(StatsReport::kStatsValueNameFingerprint,
643 stats->fingerprint);
644 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
645 stats->fingerprint_algorithm);
646 report->AddString(StatsReport::kStatsValueNameDer,
647 stats->base64_certificate);
648 if (!first_report)
649 first_report = report;
650 else
651 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
652 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000653 }
hbose29352b2016-08-25 03:52:38 -0700654 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000655}
656
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000657StatsReport* StatsCollector::AddConnectionInfoReport(
Yves Gerey665174f2018-06-19 15:03:05 +0200658 const std::string& content_name,
659 int component,
660 int connection_id,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000661 const StatsReport::Id& channel_report_id,
662 const cricket::ConnectionInfo& info) {
Yves Gerey665174f2018-06-19 15:03:05 +0200663 StatsReport::Id id(
664 StatsReport::NewCandidatePairId(content_name, component, connection_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000665 StatsReport* report = reports_.ReplaceOrAddNew(id);
666 report->set_timestamp(stats_gathering_started_);
667
668 const BoolForAdd bools[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200669 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
670 {StatsReport::kStatsValueNameReceiving, info.receiving},
671 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672 };
673 for (const auto& b : bools)
674 report->AddBoolean(b.name, b.value);
675
676 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
Qingsi Wang72a43a12018-02-20 16:03:18 -0800677 cricket::CandidateStats local_candidate_stats(info.local_candidate);
678 cricket::CandidateStats remote_candidate_stats(info.remote_candidate);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000679 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800680 AddCandidateReport(local_candidate_stats, true)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000681 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800682 AddCandidateReport(remote_candidate_stats, false)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000683
684 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700685 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
686 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
687 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
688 {StatsReport::kStatsValueNameRtt, info.rtt},
689 {StatsReport::kStatsValueNameSendPacketsDiscarded,
690 info.sent_discarded_packets},
691 {StatsReport::kStatsValueNameSentPingRequestsTotal,
692 info.sent_ping_requests_total},
693 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
694 info.sent_ping_requests_before_first_response},
695 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
696 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
697 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000698 };
699 for (const auto& i : int64s)
700 report->AddInt64(i.name, i.value);
701
702 report->AddString(StatsReport::kStatsValueNameLocalAddress,
703 info.local_candidate.address().ToString());
704 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
705 info.local_candidate.type());
706 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
707 info.remote_candidate.address().ToString());
708 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
709 info.remote_candidate.type());
710 report->AddString(StatsReport::kStatsValueNameTransportType,
711 info.local_candidate.protocol());
712
713 return report;
714}
715
716StatsReport* StatsCollector::AddCandidateReport(
Qingsi Wang72a43a12018-02-20 16:03:18 -0800717 const cricket::CandidateStats& candidate_stats,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000718 bool local) {
Qingsi Wang72a43a12018-02-20 16:03:18 -0800719 const auto& candidate = candidate_stats.candidate;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000720 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
721 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000722 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000723 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000724 report->set_timestamp(stats_gathering_started_);
725 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000726 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
727 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000728 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000729 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
730 candidate.address().ipaddr().ToString());
731 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
732 candidate.address().PortAsString());
733 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
734 candidate.priority());
735 report->AddString(StatsReport::kStatsValueNameCandidateType,
736 IceCandidateTypeToStatsType(candidate.type()));
737 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
738 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000739 }
Philipp Hanckefe0b4992019-03-22 09:00:50 +0100740 report->set_timestamp(stats_gathering_started_);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000741
Qingsi Wang4ff54432018-03-01 18:25:20 -0800742 if (local && candidate_stats.stun_stats.has_value()) {
743 const auto& stun_stats = candidate_stats.stun_stats.value();
744 report->AddInt64(StatsReport::kStatsValueNameSentStunKeepaliveRequests,
745 stun_stats.stun_binding_requests_sent);
746 report->AddInt64(StatsReport::kStatsValueNameRecvStunKeepaliveResponses,
747 stun_stats.stun_binding_responses_received);
748 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttTotal,
749 stun_stats.stun_binding_rtt_ms_total);
750 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttSquaredTotal,
751 stun_stats.stun_binding_rtt_ms_squared_total);
752 }
753
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000754 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000755}
756
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700758 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000759
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000761 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700762 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000763 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000764 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000765 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700766 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767
Qingsi Wang72a43a12018-02-20 16:03:18 -0800768 cricket::CandidateStatsList pooled_candidate_stats_list =
769 pc_->GetPooledCandidateStats();
770
771 for (const cricket::CandidateStats& stats : pooled_candidate_stats_list) {
772 AddCandidateReport(stats, true);
773 }
774
Steve Anton5dfde182018-02-06 10:34:40 -0800775 std::set<std::string> transport_names;
776 for (const auto& entry : pc_->GetTransportNamesByMid()) {
777 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000778 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779
Steve Anton5dfde182018-02-06 10:34:40 -0800780 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
781 pc_->GetTransportStatsByNames(transport_names);
782
783 for (const auto& entry : transport_stats_by_name) {
784 const std::string& transport_name = entry.first;
785 const cricket::TransportStats& transport_stats = entry.second;
786
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000787 // Attempt to get a copy of the certificates from the transport and
788 // expose them in stats reports. All channels in a transport share the
789 // same local and remote certificates.
790 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000791 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200792 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton5dfde182018-02-06 10:34:40 -0800793 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Benjamin Wright6c6c9df2018-10-25 01:16:26 -0700794 StatsReport* r = AddCertificateReports(
795 certificate->GetSSLCertificateChain().GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000796 if (r)
797 local_cert_report_id = r->id();
798 }
799
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800800 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
801 pc_->GetRemoteSSLCertChain(transport_name);
802 if (remote_cert_chain) {
803 StatsReport* r = AddCertificateReports(remote_cert_chain->GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000804 if (r)
805 remote_cert_report_id = r->id();
806 }
807
Steve Anton5dfde182018-02-06 10:34:40 -0800808 for (const auto& channel_iter : transport_stats.channel_stats) {
809 StatsReport::Id id(
810 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000811 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
812 channel_report->set_timestamp(stats_gathering_started_);
813 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
814 channel_iter.component);
815 if (local_cert_report_id.get()) {
816 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
817 local_cert_report_id);
818 }
819 if (remote_cert_report_id.get()) {
820 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
821 remote_cert_report_id);
822 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800823 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
824 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
825 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
826 channel_report->AddString(
827 StatsReport::kStatsValueNameSrtpCipher,
828 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000829 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800830 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
831 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
832 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
833 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700834 channel_report->AddString(
835 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800836 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000837 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000838
Qingsi Wang72a43a12018-02-20 16:03:18 -0800839 // Collect stats for non-pooled candidates. Note that the reports
840 // generated here supersedes the candidate reports generated in
841 // AddConnectionInfoReport below, and they may report candidates that are
842 // not paired. Also, the candidate report generated in
843 // AddConnectionInfoReport do not report port stats like StunStats.
844 for (const cricket::CandidateStats& stats :
845 channel_iter.candidate_stats_list) {
846 AddCandidateReport(stats, true);
847 }
848
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000849 int connection_id = 0;
850 for (const cricket::ConnectionInfo& info :
Yves Gerey665174f2018-06-19 15:03:05 +0200851 channel_iter.connection_infos) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000852 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800853 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000854 channel_report->id(), info);
855 if (info.best_connection) {
856 channel_report->AddId(
857 StatsReport::kStatsValueNameSelectedCandidatePairId,
858 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860 }
861 }
862 }
863}
864
stefanf79ade12017-06-02 06:44:03 -0700865void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700866 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700867
Steve Anton978b8762017-09-29 12:15:02 -0700868 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700869 return;
870
Steve Anton978b8762017-09-29 12:15:02 -0700871 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700872 cricket::BandwidthEstimationInfo bwe_info;
873 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
874 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
875 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
Steve Antonafb0bb72018-02-20 11:35:37 -0800876
stefanf79ade12017-06-02 06:44:03 -0700877 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
878 // TODO(holmer): Also fill this in for audio.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100879 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Antonafb0bb72018-02-20 11:35:37 -0800880 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) {
881 continue;
882 }
883 auto* video_channel =
884 static_cast<cricket::VideoChannel*>(transceiver->internal()->channel());
885 if (!video_channel) {
886 continue;
887 }
888 video_channel->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700889 }
Steve Antonafb0bb72018-02-20 11:35:37 -0800890
stefanf79ade12017-06-02 06:44:03 -0700891 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
892 StatsReport* report = reports_.FindOrAddNew(report_id);
893 ExtractStats(bwe_info, stats_gathering_started_, report);
894}
895
Steve Antonb8867112018-02-13 10:07:54 -0800896namespace {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000897
Steve Antonb8867112018-02-13 10:07:54 -0800898struct VoiceChannelStatsInfo {
899 std::string transport_name;
900 cricket::VoiceMediaChannel* voice_media_channel;
901 cricket::VoiceMediaInfo voice_media_info;
902};
903
904struct VideoChannelStatsInfo {
905 std::string transport_name;
906 cricket::VideoMediaChannel* video_media_channel;
907 cricket::VideoMediaInfo video_media_info;
908};
909
910} // namespace
911
912void StatsCollector::ExtractMediaInfo() {
913 RTC_DCHECK_RUN_ON(pc_->signaling_thread());
914
915 std::vector<VoiceChannelStatsInfo> voice_channel_infos;
916 std::vector<VideoChannelStatsInfo> video_channel_infos;
917
918 {
919 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100920 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Antonb8867112018-02-13 10:07:54 -0800921 if (!transceiver->internal()->channel()) {
922 continue;
923 }
924 cricket::MediaType media_type = transceiver->internal()->media_type();
925 if (media_type == cricket::MEDIA_TYPE_AUDIO) {
926 auto* voice_channel = static_cast<cricket::VoiceChannel*>(
927 transceiver->internal()->channel());
928 voice_channel_infos.emplace_back();
929 VoiceChannelStatsInfo& info = voice_channel_infos.back();
930 info.transport_name = voice_channel->transport_name();
931 info.voice_media_channel = voice_channel->media_channel();
932 } else {
933 RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
934 auto* video_channel = static_cast<cricket::VideoChannel*>(
935 transceiver->internal()->channel());
936 video_channel_infos.emplace_back();
937 VideoChannelStatsInfo& info = video_channel_infos.back();
938 info.transport_name = video_channel->transport_name();
939 info.video_media_channel = video_channel->media_channel();
940 }
941 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000943
Steve Antonb8867112018-02-13 10:07:54 -0800944 pc_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&] {
945 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
946 for (auto it = voice_channel_infos.begin(); it != voice_channel_infos.end();
947 /* incremented manually */) {
948 if (!it->voice_media_channel->GetStats(&it->voice_media_info)) {
949 RTC_LOG(LS_ERROR) << "Failed to get voice channel stats";
950 it = voice_channel_infos.erase(it);
951 continue;
952 }
953 ++it;
954 }
955 for (auto it = video_channel_infos.begin(); it != video_channel_infos.end();
956 /* incremented manually */) {
957 if (!it->video_media_channel->GetStats(&it->video_media_info)) {
958 RTC_LOG(LS_ERROR) << "Failed to get video channel stats";
959 it = video_channel_infos.erase(it);
960 continue;
961 }
962 ++it;
963 }
964 });
965
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000966 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
967
Steve Antonb8867112018-02-13 10:07:54 -0800968 bool has_remote_audio = false;
969 for (const auto& info : voice_channel_infos) {
970 StatsReport::Id transport_id = StatsReport::NewComponentId(
971 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
972 ExtractStatsFromList(info.voice_media_info.receivers, transport_id, this,
973 StatsReport::kReceive);
974 ExtractStatsFromList(info.voice_media_info.senders, transport_id, this,
975 StatsReport::kSend);
976 if (!info.voice_media_info.receivers.empty()) {
977 has_remote_audio = true;
978 }
Steve Anton74116482017-12-18 11:00:14 -0800979 }
Steve Antonb8867112018-02-13 10:07:54 -0800980 for (const auto& info : video_channel_infos) {
981 StatsReport::Id transport_id = StatsReport::NewComponentId(
982 info.transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
983 ExtractStatsFromList(info.video_media_info.receivers, transport_id, this,
984 StatsReport::kReceive);
985 ExtractStatsFromList(info.video_media_info.senders, transport_id, this,
986 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000988
Steve Antonb8867112018-02-13 10:07:54 -0800989 UpdateStatsFromExistingLocalAudioTracks(has_remote_audio);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990}
991
nissefcc640f2016-04-01 01:10:42 -0700992void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700993 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -0700994
995 for (const auto& sender : pc_->GetSenders()) {
996 // TODO(nisse): SSRC == 0 currently means none. Delete check when
997 // that is fixed.
998 if (!sender->ssrc()) {
999 continue;
1000 }
1001 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
1002 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
1003 continue;
1004 }
1005 // Safe, because kind() == kVideoKind implies a subclass of
1006 // VideoTrackInterface; see mediastreaminterface.h.
1007 VideoTrackSourceInterface* source =
1008 static_cast<VideoTrackInterface*>(track.get())->GetSource();
1009
1010 VideoTrackSourceInterface::Stats stats;
1011 if (!source->GetStats(&stats)) {
1012 continue;
1013 }
1014 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001015 StatsReport::kStatsReportTypeSsrc, rtc::ToString(sender->ssrc()),
1016 StatsReport::kSend);
nissefcc640f2016-04-01 01:10:42 -07001017 StatsReport* report = reports_.FindOrAddNew(stats_id);
1018 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
1019 stats.input_width);
1020 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
1021 stats.input_height);
1022 }
1023}
1024
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001025void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001026 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001027
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001028 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1029
deadbeefab9b2d12015-10-14 11:33:11 -07001030 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001031 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001032 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001033 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001034 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001035 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -07001036 // Filter out the initial id (-1).
1037 if (dc->id() >= 0) {
1038 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
1039 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001040 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
1041 report->AddString(StatsReport::kStatsValueNameState,
1042 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001043 }
1044}
1045
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001046StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001047 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001048 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001049 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001050 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
1051 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001052 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001053}
1054
Ivo Creusenae026092017-11-20 13:07:16 +01001055void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
1056 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001057 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001058 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001059 for (const auto& it : local_audio_tracks_) {
1060 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001061 uint32_t ssrc = it.second;
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001062 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
1063 rtc::ToString(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001064 if (report == NULL) {
1065 // This can happen if a local audio track is added to a stream on the
1066 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001067 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001068 continue;
1069 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001070
1071 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001072 const StatsReport::Value* v =
1073 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001074 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001075 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001076
jbauchbe24c942015-06-22 15:06:43 -07001077 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001078 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001079 }
1080}
1081
1082void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001083 StatsReport* report,
1084 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001085 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001086 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001087
andrew2fe1cb02015-11-27 17:27:35 -08001088 // Don't overwrite report values if they're not available.
1089 int signal_level;
1090 if (track->GetSignalLevel(&signal_level)) {
1091 RTC_DCHECK_GE(signal_level, 0);
1092 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1093 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001094
andrew2fe1cb02015-11-27 17:27:35 -08001095 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001096
andrew2fe1cb02015-11-27 17:27:35 -08001097 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001098 AudioProcessorInterface::AudioProcessorStatistics stats =
1099 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001100
Ivo Creusen56d46092017-11-24 17:29:59 +01001101 SetAudioProcessingStats(report, stats.typing_noise_detected,
1102 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001103 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001104}
1105
Steve Antona41959e2018-11-28 11:15:33 -08001106absl::string_view StatsCollector::GetTrackIdBySsrc(
1107 uint32_t ssrc,
1108 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001109 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001110 if (direction == StatsReport::kSend) {
Seth Hampson54fa0242018-12-19 16:12:11 -08001111 return pc_->GetLocalTrackIdBySsrc(ssrc);
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001112 }
Seth Hampson54fa0242018-12-19 16:12:11 -08001113 RTC_DCHECK_EQ(direction, StatsReport::kReceive);
1114 return pc_->GetRemoteTrackIdBySsrc(ssrc);
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001115}
1116
jbauchbe24c942015-06-22 15:06:43 -07001117void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001118 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001119
1120 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1121
1122 for (const auto& entry : track_ids_) {
1123 StatsReport* report = entry.second;
1124 report->set_timestamp(stats_gathering_started_);
1125 }
jbauchbe24c942015-06-22 15:06:43 -07001126}
1127
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001128void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001129 stats_gathering_started_ = 0;
1130}
1131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132} // namespace webrtc