blob: 25386f0abd95d160a80a0319a4aab086680b19aa [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
Steve Antonefe4c922019-03-27 10:26:06 -070018#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "pc/peer_connection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Artem Titova76af0c2018-07-23 17:38:12 +020022#include "rtc_base/third_party/base64/base64.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
24namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000027// The following is the enum RTCStatsIceCandidateType from
28// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
29// our stats report for ice candidate type could conform to that.
30const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
31const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
32const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
33const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
34
35// Strings used by the stats collector to report adapter types. This fits the
36// general stype of http://w3c.github.io/webrtc-stats than what
37// AdapterTypeToString does.
38const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
39const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
40const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
41const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000042const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
Qingsi Wang01560de2018-07-26 10:44:02 -070043const char* STATSREPORT_ADAPTER_TYPE_WILDCARD = "wildcard";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000044
Yves Gerey665174f2018-06-19 15:03:05 +020045template <typename ValueType>
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000046struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000047 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000048 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000049};
50
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000051typedef TypeForAdd<bool> BoolForAdd;
52typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020053typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000054typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000055
jbauchbe24c942015-06-22 15:06:43 -070056StatsReport* AddTrackReport(StatsCollection* reports,
57 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000058 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000059 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000060 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000062 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070063 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000064}
65
Harald Alvestrand75ceef22018-01-04 15:26:13 +010066template <class Track>
67void CreateTrackReport(const Track* track,
68 StatsCollection* reports,
69 TrackIdMap* track_ids) {
70 const std::string& track_id = track->id();
71 StatsReport* report = AddTrackReport(reports, track_id);
72 RTC_DCHECK(report != nullptr);
73 (*track_ids)[track_id] = report;
74}
75
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076template <class TrackVector>
Steve Anton36b29d12017-10-30 09:57:42 -070077void CreateTrackReports(const TrackVector& tracks,
78 StatsCollection* reports,
79 TrackIdMap* track_ids) {
jbauchbe24c942015-06-22 15:06:43 -070080 for (const auto& track : tracks) {
Harald Alvestrand75ceef22018-01-04 15:26:13 +010081 CreateTrackReport(track.get(), reports, track_ids);
jbauchbe24c942015-06-22 15:06:43 -070082 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083}
84
tommi@webrtc.org92f40182015-03-04 15:25:19 +000085void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
86 StatsReport* report) {
87 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
88 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070089 if (info.rtt_ms >= 0) {
90 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
91 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000092}
93
pbosf42376c2015-08-28 07:35:32 -070094void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
95 StatsReport* report) {
96 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
97}
98
Ivo Creusen56d46092017-11-24 17:29:59 +010099void SetAudioProcessingStats(StatsReport* report,
100 bool typing_noise_detected,
101 const AudioProcessingStats& apm_stats) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000102 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
103 typing_noise_detected);
Ivo Creusen56d46092017-11-24 17:29:59 +0100104 if (apm_stats.delay_median_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100105 report->AddInt(StatsReport::kStatsValueNameEchoDelayMedian,
Ivo Creusen56d46092017-11-24 17:29:59 +0100106 *apm_stats.delay_median_ms);
Ivo Creusenae026092017-11-20 13:07:16 +0100107 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100108 if (apm_stats.delay_standard_deviation_ms) {
Ivo Creusenae026092017-11-20 13:07:16 +0100109 report->AddInt(StatsReport::kStatsValueNameEchoDelayStdDev,
Ivo Creusen56d46092017-11-24 17:29:59 +0100110 *apm_stats.delay_standard_deviation_ms);
zhihuang6ba3b192016-05-13 11:46:35 -0700111 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100112 if (apm_stats.echo_return_loss) {
Ivo Creusenae026092017-11-20 13:07:16 +0100113 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss,
Ivo Creusen56d46092017-11-24 17:29:59 +0100114 *apm_stats.echo_return_loss);
henrik.lundin04a057b2017-01-16 23:53:59 -0800115 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100116 if (apm_stats.echo_return_loss_enhancement) {
Ivo Creusenae026092017-11-20 13:07:16 +0100117 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
Ivo Creusen56d46092017-11-24 17:29:59 +0100118 *apm_stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +0100119 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100120 if (apm_stats.residual_echo_likelihood) {
Ivo Creusenae026092017-11-20 13:07:16 +0100121 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
Ivo Creusen56d46092017-11-24 17:29:59 +0100122 static_cast<float>(*apm_stats.residual_echo_likelihood));
Ivo Creusenae026092017-11-20 13:07:16 +0100123 }
Ivo Creusen56d46092017-11-24 17:29:59 +0100124 if (apm_stats.residual_echo_likelihood_recent_max) {
ivoc4e477a12017-01-15 08:29:46 -0800125 report->AddFloat(
126 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
Ivo Creusen56d46092017-11-24 17:29:59 +0100127 static_cast<float>(*apm_stats.residual_echo_likelihood_recent_max));
128 }
129 if (apm_stats.divergent_filter_fraction) {
130 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
131 static_cast<float>(*apm_stats.divergent_filter_fraction));
ivoc8c63a822016-10-21 04:10:03 -0700132 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000133}
134
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700136 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000137 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200138 {StatsReport::kStatsValueNameExpandRate, info.expand_rate},
139 {StatsReport::kStatsValueNameSecondaryDecodedRate,
140 info.secondary_decoded_rate},
141 {StatsReport::kStatsValueNameSecondaryDiscardedRate,
142 info.secondary_discarded_rate},
143 {StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate},
144 {StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate},
145 {StatsReport::kStatsValueNamePreemptiveExpandRate,
146 info.preemptive_expand_rate},
147 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy},
148 {StatsReport::kStatsValueNameTotalSamplesDuration,
149 info.total_output_duration}};
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000150
151 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200152 {StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms},
153 {StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng},
154 {StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq},
155 {StatsReport::kStatsValueNameDecodingCTSG,
156 info.decoding_calls_to_silence_generator},
157 {StatsReport::kStatsValueNameDecodingMutedOutput,
158 info.decoding_muted_output},
159 {StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal},
160 {StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc},
161 {StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng},
162 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
163 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
164 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
165 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
166 {StatsReport::kStatsValueNamePreferredJitterBufferMs,
167 info.jitter_buffer_preferred_ms},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000168 };
169
170 for (const auto& f : floats)
171 report->AddFloat(f.name, f.value);
172
173 for (const auto& i : ints)
174 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700175 if (info.audio_level >= 0) {
176 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
177 info.audio_level);
178 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000179
Yves Gerey665174f2018-06-19 15:03:05 +0200180 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700181 if (info.capture_start_ntp_time_ms >= 0) {
182 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
183 info.capture_start_ntp_time_ms);
184 }
fippobec70ab2016-01-28 01:27:15 -0800185 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186}
187
188void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000189 ExtractCommonSendProperties(info, report);
190
Ivo Creusen56d46092017-11-24 17:29:59 +0100191 SetAudioProcessingStats(report, info.typing_noise_detected,
192 info.apm_statistics);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000193
zsteine76bd3a2017-07-14 12:17:49 -0700194 const FloatForAdd floats[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200195 {StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy},
196 {StatsReport::kStatsValueNameTotalSamplesDuration,
197 info.total_input_duration}};
zsteine76bd3a2017-07-14 12:17:49 -0700198
andrew2fe1cb02015-11-27 17:27:35 -0800199 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000200 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200201 {StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
202 {StatsReport::kStatsValueNameJitterReceived, info.jitter_ms},
203 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
204 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000205 };
206
zsteine76bd3a2017-07-14 12:17:49 -0700207 for (const auto& f : floats) {
208 report->AddFloat(f.name, f.value);
209 }
210
zhihuang6ba3b192016-05-13 11:46:35 -0700211 for (const auto& i : ints) {
212 if (i.value >= 0) {
213 report->AddInt(i.name, i.value);
214 }
215 }
fippobec70ab2016-01-28 01:27:15 -0800216 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
ivoce1198e02017-09-08 08:13:19 -0700217 if (info.ana_statistics.bitrate_action_counter) {
218 report->AddInt(StatsReport::kStatsValueNameAnaBitrateActionCounter,
219 *info.ana_statistics.bitrate_action_counter);
220 }
221 if (info.ana_statistics.channel_action_counter) {
222 report->AddInt(StatsReport::kStatsValueNameAnaChannelActionCounter,
223 *info.ana_statistics.channel_action_counter);
224 }
225 if (info.ana_statistics.dtx_action_counter) {
226 report->AddInt(StatsReport::kStatsValueNameAnaDtxActionCounter,
227 *info.ana_statistics.dtx_action_counter);
228 }
229 if (info.ana_statistics.fec_action_counter) {
230 report->AddInt(StatsReport::kStatsValueNameAnaFecActionCounter,
231 *info.ana_statistics.fec_action_counter);
232 }
ivoc0d0b9122017-09-08 13:24:21 -0700233 if (info.ana_statistics.frame_length_increase_counter) {
234 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthIncreaseCounter,
235 *info.ana_statistics.frame_length_increase_counter);
236 }
237 if (info.ana_statistics.frame_length_decrease_counter) {
238 report->AddInt(StatsReport::kStatsValueNameAnaFrameLengthDecreaseCounter,
239 *info.ana_statistics.frame_length_decrease_counter);
240 }
241 if (info.ana_statistics.uplink_packet_loss_fraction) {
242 report->AddFloat(StatsReport::kStatsValueNameAnaUplinkPacketLossFraction,
243 *info.ana_statistics.uplink_packet_loss_fraction);
ivoce1198e02017-09-08 08:13:19 -0700244 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245}
246
247void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700248 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100249 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
250 info.decoder_implementation_name);
Yves Gerey665174f2018-06-19 15:03:05 +0200251 report->AddInt64(StatsReport::kStatsValueNameBytesReceived, info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700252 if (info.capture_start_ntp_time_ms >= 0) {
253 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
254 info.capture_start_ntp_time_ms);
255 }
Benjamin Wright514f0842018-12-10 09:55:17 -0800256 if (info.first_frame_received_to_decoded_ms >= 0) {
257 report->AddInt64(StatsReport::kStatsValueNameFirstFrameReceivedToDecodedMs,
258 info.first_frame_received_to_decoded_ms);
259 }
sakalcc452e12017-02-09 04:53:45 -0800260 if (info.qp_sum)
261 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
262
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000263 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200264 {StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms},
265 {StatsReport::kStatsValueNameDecodeMs, info.decode_ms},
266 {StatsReport::kStatsValueNameFirsSent, info.firs_sent},
267 {StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height},
268 {StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded},
269 {StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output},
270 {StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd},
271 {StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width},
272 {StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms},
273 {StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms},
274 {StatsReport::kStatsValueNameMinPlayoutDelayMs,
275 info.min_playout_delay_ms},
276 {StatsReport::kStatsValueNameNacksSent, info.nacks_sent},
277 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
278 {StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd},
279 {StatsReport::kStatsValueNamePlisSent, info.plis_sent},
280 {StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms},
281 {StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms},
282 {StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded},
Artem Titov8a3ab0e2018-07-27 14:52:57 +0000283 };
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000284
285 for (const auto& i : ints)
286 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800287 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700288
ilnik2edc6842017-07-06 03:06:50 -0700289 if (info.timing_frame_info) {
290 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
291 info.timing_frame_info->ToString());
292 }
ilnikf04afde2017-07-07 01:26:24 -0700293
ilnika79cc282017-08-23 05:24:10 -0700294 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
295 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700296
297 report->AddString(
298 StatsReport::kStatsValueNameContentType,
299 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300}
301
302void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000303 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304
Peter Boströmb7d9a972015-12-18 16:01:11 +0100305 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
306 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000307 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
308 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000309 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
310 (info.adapt_reason & 0x1) > 0);
Ă…sa Perssonc3ed6302017-11-16 14:04:52 +0100311 report->AddBoolean(StatsReport::kStatsValueNameHasEnteredLowResolution,
312 info.has_entered_low_resolution);
313
sakal87da4042016-10-31 06:53:47 -0700314 if (info.qp_sum)
315 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000316
317 const IntForAdd ints[] = {
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100318 {StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes},
319 {StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms},
320 {StatsReport::kStatsValueNameEncodeUsagePercent,
321 info.encode_usage_percent},
322 {StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd},
323 {StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height},
324 {StatsReport::kStatsValueNameFrameRateInput, info.framerate_input},
325 {StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent},
326 {StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width},
327 {StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd},
328 {StatsReport::kStatsValueNamePacketsLost, info.packets_lost},
329 {StatsReport::kStatsValueNamePacketsSent, info.packets_sent},
330 {StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd},
331 {StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded},
332 {StatsReport::kStatsValueNameHugeFramesSent, info.huge_frames_sent},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000333 };
334
335 for (const auto& i : ints)
336 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800337 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnik50864a82017-09-06 12:32:35 -0700338 report->AddString(
339 StatsReport::kStatsValueNameContentType,
340 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341}
342
343void ExtractStats(const cricket::BandwidthEstimationInfo& info,
344 double stats_gathering_started,
345 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700346 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000348 report->set_timestamp(stats_gathering_started);
349 const IntForAdd ints[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200350 {StatsReport::kStatsValueNameAvailableSendBandwidth,
351 info.available_send_bandwidth},
352 {StatsReport::kStatsValueNameAvailableReceiveBandwidth,
353 info.available_recv_bandwidth},
354 {StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate},
355 {StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate},
356 {StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate},
357 {StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000358 };
359 for (const auto& i : ints)
360 report->AddInt(i.name, i.value);
361 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
wu@webrtc.org97077a32013-10-25 21:18:33 +0000364void ExtractRemoteStats(const cricket::MediaSenderInfo& 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
370void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
371 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000372 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000373 // TODO(hta): Extract some stats here.
374}
375
Steve Antonefe4c922019-03-27 10:26:06 -0700376std::string GetTrackIdBySsrc(
377 uint32_t ssrc,
378 StatsReport::Direction direction,
379 const std::map<uint32_t, std::string>& track_id_by_ssrc) {
380 auto it = track_id_by_ssrc.find(ssrc);
381 if (it != track_id_by_ssrc.end()) {
382 return it->second;
383 }
384 if (direction == StatsReport::kReceive) {
385 // If the track ID was not found, this might be an unsignaled receive
386 // SSRC, so try looking up by the special SSRC 0.
387 it = track_id_by_ssrc.find(0);
388 if (it != track_id_by_ssrc.end()) {
389 RTC_LOG(LS_INFO) << "Assuming SSRC=" << ssrc
390 << " is an unsignalled receive stream corresponding "
391 "to the RtpReceiver with track ID \""
392 << it->second << "\".";
393 return it->second;
394 }
395 }
Steve Antonefe4c922019-03-27 10:26:06 -0700396 return "";
397}
398
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000400// In order to use the template, the functions that are called from it,
401// ExtractStats and ExtractRemoteStats, must be defined and overloaded
402// for each type.
Yves Gerey665174f2018-06-19 15:03:05 +0200403template <typename T>
Steve Antonefe4c922019-03-27 10:26:06 -0700404void ExtractStatsFromList(
405 const std::vector<T>& data,
406 const StatsReport::Id& transport_id,
407 StatsCollector* collector,
408 StatsReport::Direction direction,
409 const std::map<uint32_t, std::string>& track_id_by_ssrc) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000410 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200411 uint32_t ssrc = d.ssrc();
Steve Antonefe4c922019-03-27 10:26:06 -0700412 std::string track_id = GetTrackIdBySsrc(ssrc, direction, track_id_by_ssrc);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000413 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000414 // TODO(hta): Handle the case of multiple SSRCs per object.
Yves Gerey665174f2018-06-19 15:03:05 +0200415 StatsReport* report =
Steve Antonefe4c922019-03-27 10:26:06 -0700416 collector->PrepareReport(true, ssrc, track_id, transport_id, direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000417 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000418 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000419
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000420 if (!d.remote_stats.empty()) {
Steve Antonefe4c922019-03-27 10:26:06 -0700421 report = collector->PrepareReport(false, ssrc, track_id, transport_id,
422 direction);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000423 if (report)
424 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000427}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428
429} // namespace
430
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000431const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
432 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
433 return STATSREPORT_LOCAL_PORT_TYPE;
434 }
435 if (candidate_type == cricket::STUN_PORT_TYPE) {
436 return STATSREPORT_STUN_PORT_TYPE;
437 }
438 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
439 return STATSREPORT_PRFLX_PORT_TYPE;
440 }
441 if (candidate_type == cricket::RELAY_PORT_TYPE) {
442 return STATSREPORT_RELAY_PORT_TYPE;
443 }
nisseeb4ca4e2017-01-12 02:24:27 -0800444 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000445 return "unknown";
446}
447
448const char* AdapterTypeToStatsType(rtc::AdapterType type) {
449 switch (type) {
450 case rtc::ADAPTER_TYPE_UNKNOWN:
451 return "unknown";
452 case rtc::ADAPTER_TYPE_ETHERNET:
453 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
454 case rtc::ADAPTER_TYPE_WIFI:
455 return STATSREPORT_ADAPTER_TYPE_WIFI;
456 case rtc::ADAPTER_TYPE_CELLULAR:
457 return STATSREPORT_ADAPTER_TYPE_WWAN;
458 case rtc::ADAPTER_TYPE_VPN:
459 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000460 case rtc::ADAPTER_TYPE_LOOPBACK:
461 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
Qingsi Wang01560de2018-07-26 10:44:02 -0700462 case rtc::ADAPTER_TYPE_ANY:
463 return STATSREPORT_ADAPTER_TYPE_WILDCARD;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000464 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800465 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000466 return "";
467 }
468}
469
Steve Anton2d8609c2018-01-23 16:38:46 -0800470StatsCollector::StatsCollector(PeerConnectionInternal* pc)
deadbeefab9b2d12015-10-14 11:33:11 -0700471 : pc_(pc), stats_gathering_started_(0) {
472 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000473}
474
475StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700476 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477}
478
nissecdf37a92016-09-13 23:41:47 -0700479// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000480double StatsCollector::GetTimeNow() {
Minyue Li656d6092018-08-10 15:38:52 +0200481 return static_cast<double>(rtc::TimeUTCMillis());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000482}
483
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484// Adds a MediaStream with tracks that can be used as a |selector| in a call
485// to GetStats.
486void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700487 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700488 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489
Steve Anton36b29d12017-10-30 09:57:42 -0700490 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
491 &track_ids_);
492 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
493 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494}
495
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100496void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
497 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
498 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
499 &track_ids_);
500 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
501 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
502 &track_ids_);
503 } else {
504 RTC_NOTREACHED() << "Illegal track kind";
505 }
506}
507
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000508void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200509 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700510 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700511 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700512#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000513 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700514 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000515#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000516
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000517 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000518
519 // Create the kStatsReportTypeTrack report for the new track if there is no
520 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000521 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
522 audio_track->id()));
523 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000524 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000525 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000526 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000527 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000528}
529
530void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200531 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700532 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700533 local_audio_tracks_.erase(
534 std::remove_if(
535 local_audio_tracks_.begin(), local_audio_tracks_.end(),
536 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
537 return track.first == audio_track && track.second == ssrc;
538 }),
539 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000540}
541
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000542void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700544 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700545 RTC_DCHECK(reports != NULL);
546 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000548 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
549
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000551 reports->reserve(reports_.size());
552 for (auto* r : reports_)
553 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000554 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 }
556
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000557 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700558 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000559 if (report)
560 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561
Yves Gerey665174f2018-06-19 15:03:05 +0200562 report = reports_.Find(
563 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000565 if (!report)
566 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000568 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569
570 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000571 for (const auto* r : reports_) {
572 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000574
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000575 const StatsReport::Value* v =
576 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000577 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000578 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580}
581
Yves Gerey665174f2018-06-19 15:03:05 +0200582void StatsCollector::UpdateStats(
583 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700584 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 double time_now = GetTimeNow();
586 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
587 // ms apart will be ignored.
588 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000589 if (stats_gathering_started_ != 0 &&
590 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 return;
592 }
593 stats_gathering_started_ = time_now;
594
Steve Anton75737c02017-11-06 10:37:17 -0800595 // TODO(tommi): All of these hop over to the worker thread to fetch
596 // information. We could use an AsyncInvoker to run all of these and post
597 // the information back to the signaling thread where we can create and
598 // update stats reports. That would also clean up the threading story a bit
599 // since we'd be creating/updating the stats report objects consistently on
600 // the same thread (this class has no locks right now).
601 ExtractSessionInfo();
602 ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800603 ExtractMediaInfo();
Steve Anton75737c02017-11-06 10:37:17 -0800604 ExtractSenderInfo();
605 ExtractDataInfo();
606 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607}
608
Yves Gerey665174f2018-06-19 15:03:05 +0200609StatsReport* StatsCollector::PrepareReport(bool local,
610 uint32_t ssrc,
Steve Antonefe4c922019-03-27 10:26:06 -0700611 const std::string& track_id,
Yves Gerey665174f2018-06-19 15:03:05 +0200612 const StatsReport::Id& transport_id,
613 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700614 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000615 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200616 local ? StatsReport::kStatsReportTypeSsrc
617 : StatsReport::kStatsReportTypeRemoteSsrc,
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200618 rtc::ToString(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000619 StatsReport* report = reports_.Find(id);
Steve Antona41959e2018-11-28 11:15:33 -0800620 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000621 report = reports_.InsertNew(id);
Steve Antona41959e2018-11-28 11:15:33 -0800622 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000624 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000625 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000627 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700628 if (!track_id.empty()) {
Steve Antonefe4c922019-03-27 10:26:06 -0700629 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700630 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000632 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 return report;
634}
635
zhihuange9e94c32016-11-04 11:38:15 -0700636bool StatsCollector::IsValidTrack(const std::string& track_id) {
637 return reports_.Find(StatsReport::NewTypedId(
638 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
639}
640
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000641StatsReport* StatsCollector::AddCertificateReports(
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800642 std::unique_ptr<rtc::SSLCertificateStats> cert_stats) {
Steve Anton978b8762017-09-29 12:15:02 -0700643 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000644
hbose29352b2016-08-25 03:52:38 -0700645 StatsReport* first_report = nullptr;
646 StatsReport* prev_report = nullptr;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800647 for (rtc::SSLCertificateStats* stats = cert_stats.get(); stats;
hbose29352b2016-08-25 03:52:38 -0700648 stats = stats->issuer.get()) {
649 StatsReport::Id id(StatsReport::NewTypedId(
650 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
651
652 StatsReport* report = reports_.ReplaceOrAddNew(id);
653 report->set_timestamp(stats_gathering_started_);
654 report->AddString(StatsReport::kStatsValueNameFingerprint,
655 stats->fingerprint);
656 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
657 stats->fingerprint_algorithm);
658 report->AddString(StatsReport::kStatsValueNameDer,
659 stats->base64_certificate);
660 if (!first_report)
661 first_report = report;
662 else
663 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
664 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000665 }
hbose29352b2016-08-25 03:52:38 -0700666 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000667}
668
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000669StatsReport* StatsCollector::AddConnectionInfoReport(
Yves Gerey665174f2018-06-19 15:03:05 +0200670 const std::string& content_name,
671 int component,
672 int connection_id,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000673 const StatsReport::Id& channel_report_id,
674 const cricket::ConnectionInfo& info) {
Yves Gerey665174f2018-06-19 15:03:05 +0200675 StatsReport::Id id(
676 StatsReport::NewCandidatePairId(content_name, component, connection_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000677 StatsReport* report = reports_.ReplaceOrAddNew(id);
678 report->set_timestamp(stats_gathering_started_);
679
680 const BoolForAdd bools[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200681 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
682 {StatsReport::kStatsValueNameReceiving, info.receiving},
683 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000684 };
685 for (const auto& b : bools)
686 report->AddBoolean(b.name, b.value);
687
688 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
Qingsi Wang72a43a12018-02-20 16:03:18 -0800689 cricket::CandidateStats local_candidate_stats(info.local_candidate);
690 cricket::CandidateStats remote_candidate_stats(info.remote_candidate);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000691 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800692 AddCandidateReport(local_candidate_stats, true)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000693 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800694 AddCandidateReport(remote_candidate_stats, false)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000695
696 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700697 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
698 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
699 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
700 {StatsReport::kStatsValueNameRtt, info.rtt},
701 {StatsReport::kStatsValueNameSendPacketsDiscarded,
702 info.sent_discarded_packets},
703 {StatsReport::kStatsValueNameSentPingRequestsTotal,
704 info.sent_ping_requests_total},
705 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
706 info.sent_ping_requests_before_first_response},
707 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
708 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
709 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000710 };
711 for (const auto& i : int64s)
712 report->AddInt64(i.name, i.value);
713
714 report->AddString(StatsReport::kStatsValueNameLocalAddress,
715 info.local_candidate.address().ToString());
716 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
717 info.local_candidate.type());
718 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
719 info.remote_candidate.address().ToString());
720 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
721 info.remote_candidate.type());
722 report->AddString(StatsReport::kStatsValueNameTransportType,
723 info.local_candidate.protocol());
724
725 return report;
726}
727
728StatsReport* StatsCollector::AddCandidateReport(
Qingsi Wang72a43a12018-02-20 16:03:18 -0800729 const cricket::CandidateStats& candidate_stats,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000730 bool local) {
Qingsi Wang72a43a12018-02-20 16:03:18 -0800731 const auto& candidate = candidate_stats.candidate;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000732 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
733 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000734 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000735 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000736 report->set_timestamp(stats_gathering_started_);
737 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000738 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
739 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000740 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000741 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
742 candidate.address().ipaddr().ToString());
743 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
744 candidate.address().PortAsString());
745 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
746 candidate.priority());
747 report->AddString(StatsReport::kStatsValueNameCandidateType,
748 IceCandidateTypeToStatsType(candidate.type()));
749 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
750 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000751 }
Philipp Hanckefe0b4992019-03-22 09:00:50 +0100752 report->set_timestamp(stats_gathering_started_);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000753
Qingsi Wang4ff54432018-03-01 18:25:20 -0800754 if (local && candidate_stats.stun_stats.has_value()) {
755 const auto& stun_stats = candidate_stats.stun_stats.value();
756 report->AddInt64(StatsReport::kStatsValueNameSentStunKeepaliveRequests,
757 stun_stats.stun_binding_requests_sent);
758 report->AddInt64(StatsReport::kStatsValueNameRecvStunKeepaliveResponses,
759 stun_stats.stun_binding_responses_received);
760 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttTotal,
761 stun_stats.stun_binding_rtt_ms_total);
762 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttSquaredTotal,
763 stun_stats.stun_binding_rtt_ms_squared_total);
764 }
765
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000766 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000767}
768
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700770 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000773 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700774 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000775 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000776 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000777 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700778 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779
Qingsi Wang72a43a12018-02-20 16:03:18 -0800780 cricket::CandidateStatsList pooled_candidate_stats_list =
781 pc_->GetPooledCandidateStats();
782
783 for (const cricket::CandidateStats& stats : pooled_candidate_stats_list) {
784 AddCandidateReport(stats, true);
785 }
786
Steve Anton5dfde182018-02-06 10:34:40 -0800787 std::set<std::string> transport_names;
788 for (const auto& entry : pc_->GetTransportNamesByMid()) {
789 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000790 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791
Steve Anton5dfde182018-02-06 10:34:40 -0800792 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
793 pc_->GetTransportStatsByNames(transport_names);
794
795 for (const auto& entry : transport_stats_by_name) {
796 const std::string& transport_name = entry.first;
797 const cricket::TransportStats& transport_stats = entry.second;
798
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000799 // Attempt to get a copy of the certificates from the transport and
800 // expose them in stats reports. All channels in a transport share the
801 // same local and remote certificates.
802 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000803 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200804 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton5dfde182018-02-06 10:34:40 -0800805 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Benjamin Wright6c6c9df2018-10-25 01:16:26 -0700806 StatsReport* r = AddCertificateReports(
807 certificate->GetSSLCertificateChain().GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000808 if (r)
809 local_cert_report_id = r->id();
810 }
811
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800812 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
813 pc_->GetRemoteSSLCertChain(transport_name);
814 if (remote_cert_chain) {
815 StatsReport* r = AddCertificateReports(remote_cert_chain->GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000816 if (r)
817 remote_cert_report_id = r->id();
818 }
819
Steve Anton5dfde182018-02-06 10:34:40 -0800820 for (const auto& channel_iter : transport_stats.channel_stats) {
821 StatsReport::Id id(
822 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000823 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
824 channel_report->set_timestamp(stats_gathering_started_);
825 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
826 channel_iter.component);
827 if (local_cert_report_id.get()) {
828 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
829 local_cert_report_id);
830 }
831 if (remote_cert_report_id.get()) {
832 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
833 remote_cert_report_id);
834 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800835 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
836 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
837 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
838 channel_report->AddString(
839 StatsReport::kStatsValueNameSrtpCipher,
840 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000841 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800842 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
843 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
844 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
845 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700846 channel_report->AddString(
847 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800848 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000849 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000850
Qingsi Wang72a43a12018-02-20 16:03:18 -0800851 // Collect stats for non-pooled candidates. Note that the reports
852 // generated here supersedes the candidate reports generated in
853 // AddConnectionInfoReport below, and they may report candidates that are
854 // not paired. Also, the candidate report generated in
855 // AddConnectionInfoReport do not report port stats like StunStats.
856 for (const cricket::CandidateStats& stats :
857 channel_iter.candidate_stats_list) {
858 AddCandidateReport(stats, true);
859 }
860
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000861 int connection_id = 0;
862 for (const cricket::ConnectionInfo& info :
Yves Gerey665174f2018-06-19 15:03:05 +0200863 channel_iter.connection_infos) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000864 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800865 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000866 channel_report->id(), info);
867 if (info.best_connection) {
868 channel_report->AddId(
869 StatsReport::kStatsValueNameSelectedCandidatePairId,
870 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 }
872 }
873 }
874 }
875}
876
stefanf79ade12017-06-02 06:44:03 -0700877void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700878 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700879
Steve Anton978b8762017-09-29 12:15:02 -0700880 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700881 return;
882
Steve Anton978b8762017-09-29 12:15:02 -0700883 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700884 cricket::BandwidthEstimationInfo bwe_info;
885 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
886 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
887 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
Steve Antonafb0bb72018-02-20 11:35:37 -0800888
stefanf79ade12017-06-02 06:44:03 -0700889 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
890 // TODO(holmer): Also fill this in for audio.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100891 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Antonafb0bb72018-02-20 11:35:37 -0800892 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) {
893 continue;
894 }
895 auto* video_channel =
896 static_cast<cricket::VideoChannel*>(transceiver->internal()->channel());
897 if (!video_channel) {
898 continue;
899 }
900 video_channel->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700901 }
Steve Antonafb0bb72018-02-20 11:35:37 -0800902
stefanf79ade12017-06-02 06:44:03 -0700903 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
904 StatsReport* report = reports_.FindOrAddNew(report_id);
905 ExtractStats(bwe_info, stats_gathering_started_, report);
906}
907
Steve Antonb8867112018-02-13 10:07:54 -0800908namespace {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000909
Steve Antonefe4c922019-03-27 10:26:06 -0700910class MediaChannelStatsGatherer {
911 public:
912 virtual ~MediaChannelStatsGatherer() = default;
913
914 virtual bool GetStatsOnWorkerThread() = 0;
915
916 virtual void ExtractStats(StatsCollector* collector) const = 0;
917
918 virtual bool HasRemoteAudio() const = 0;
919
920 std::string mid;
Steve Antonb8867112018-02-13 10:07:54 -0800921 std::string transport_name;
Steve Antonefe4c922019-03-27 10:26:06 -0700922 std::map<uint32_t, std::string> sender_track_id_by_ssrc;
923 std::map<uint32_t, std::string> receiver_track_id_by_ssrc;
924
925 protected:
926 template <typename ReceiverT, typename SenderT>
927 void ExtractSenderReceiverStats(
928 StatsCollector* collector,
929 const std::vector<ReceiverT>& receiver_data,
930 const std::vector<SenderT>& sender_data) const {
931 RTC_DCHECK(collector);
932 StatsReport::Id transport_id = StatsReport::NewComponentId(
933 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
934 ExtractStatsFromList(receiver_data, transport_id, collector,
935 StatsReport::kReceive, receiver_track_id_by_ssrc);
936 ExtractStatsFromList(sender_data, transport_id, collector,
937 StatsReport::kSend, sender_track_id_by_ssrc);
938 }
939};
940
941class VoiceMediaChannelStatsGatherer final : public MediaChannelStatsGatherer {
942 public:
943 VoiceMediaChannelStatsGatherer(
944 cricket::VoiceMediaChannel* voice_media_channel)
945 : voice_media_channel_(voice_media_channel) {
946 RTC_DCHECK(voice_media_channel_);
947 }
948
949 bool GetStatsOnWorkerThread() override {
950 return voice_media_channel_->GetStats(&voice_media_info);
951 }
952
953 void ExtractStats(StatsCollector* collector) const override {
954 ExtractSenderReceiverStats(collector, voice_media_info.receivers,
955 voice_media_info.senders);
956 }
957
958 bool HasRemoteAudio() const override {
959 return !voice_media_info.receivers.empty();
960 }
961
962 private:
963 cricket::VoiceMediaChannel* voice_media_channel_;
Steve Antonb8867112018-02-13 10:07:54 -0800964 cricket::VoiceMediaInfo voice_media_info;
965};
966
Steve Antonefe4c922019-03-27 10:26:06 -0700967class VideoMediaChannelStatsGatherer final : public MediaChannelStatsGatherer {
968 public:
969 VideoMediaChannelStatsGatherer(
970 cricket::VideoMediaChannel* video_media_channel)
971 : video_media_channel_(video_media_channel) {
972 RTC_DCHECK(video_media_channel_);
973 }
974
975 bool GetStatsOnWorkerThread() override {
976 return video_media_channel_->GetStats(&video_media_info);
977 }
978
979 void ExtractStats(StatsCollector* collector) const override {
980 ExtractSenderReceiverStats(collector, video_media_info.receivers,
981 video_media_info.senders);
982 }
983
984 bool HasRemoteAudio() const override { return false; }
985
986 private:
987 cricket::VideoMediaChannel* video_media_channel_;
Steve Antonb8867112018-02-13 10:07:54 -0800988 cricket::VideoMediaInfo video_media_info;
989};
990
Steve Antonefe4c922019-03-27 10:26:06 -0700991std::unique_ptr<MediaChannelStatsGatherer> CreateMediaChannelStatsGatherer(
992 cricket::MediaChannel* channel) {
993 RTC_DCHECK(channel);
994 if (channel->media_type() == cricket::MEDIA_TYPE_AUDIO) {
995 return absl::make_unique<VoiceMediaChannelStatsGatherer>(
996 static_cast<cricket::VoiceMediaChannel*>(channel));
997 } else {
998 RTC_DCHECK_EQ(channel->media_type(), cricket::MEDIA_TYPE_VIDEO);
999 return absl::make_unique<VideoMediaChannelStatsGatherer>(
1000 static_cast<cricket::VideoMediaChannel*>(channel));
1001 }
1002}
1003
Steve Antonb8867112018-02-13 10:07:54 -08001004} // namespace
1005
1006void StatsCollector::ExtractMediaInfo() {
1007 RTC_DCHECK_RUN_ON(pc_->signaling_thread());
1008
Steve Antonefe4c922019-03-27 10:26:06 -07001009 std::vector<std::unique_ptr<MediaChannelStatsGatherer>> gatherers;
Steve Antonb8867112018-02-13 10:07:54 -08001010
1011 {
1012 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001013 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Antonefe4c922019-03-27 10:26:06 -07001014 cricket::ChannelInterface* channel = transceiver->internal()->channel();
1015 if (!channel) {
Steve Antonb8867112018-02-13 10:07:54 -08001016 continue;
1017 }
Steve Antonefe4c922019-03-27 10:26:06 -07001018 std::unique_ptr<MediaChannelStatsGatherer> gatherer =
1019 CreateMediaChannelStatsGatherer(channel->media_channel());
1020 gatherer->mid = channel->content_name();
1021 gatherer->transport_name = channel->transport_name();
1022 for (const auto& sender : transceiver->internal()->senders()) {
1023 std::string track_id = (sender->track() ? sender->track()->id() : "");
1024 gatherer->sender_track_id_by_ssrc.insert(
1025 std::make_pair(sender->ssrc(), track_id));
Steve Antonb8867112018-02-13 10:07:54 -08001026 }
Steve Antonefe4c922019-03-27 10:26:06 -07001027 for (const auto& receiver : transceiver->internal()->receivers()) {
1028 gatherer->receiver_track_id_by_ssrc.insert(std::make_pair(
1029 receiver->internal()->ssrc(), receiver->track()->id()));
1030 }
1031 gatherers.push_back(std::move(gatherer));
Steve Antonb8867112018-02-13 10:07:54 -08001032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001034
Steve Antonb8867112018-02-13 10:07:54 -08001035 pc_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&] {
1036 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Antonefe4c922019-03-27 10:26:06 -07001037 for (auto it = gatherers.begin(); it != gatherers.end();
Steve Antonb8867112018-02-13 10:07:54 -08001038 /* incremented manually */) {
Steve Antonefe4c922019-03-27 10:26:06 -07001039 MediaChannelStatsGatherer* gatherer = it->get();
1040 if (!gatherer->GetStatsOnWorkerThread()) {
1041 RTC_LOG(LS_ERROR) << "Failed to get media channel stats for mid="
1042 << gatherer->mid;
1043 it = gatherers.erase(it);
Steve Antonb8867112018-02-13 10:07:54 -08001044 continue;
1045 }
1046 ++it;
1047 }
1048 });
1049
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001050 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1051
Steve Antonb8867112018-02-13 10:07:54 -08001052 bool has_remote_audio = false;
Steve Antonefe4c922019-03-27 10:26:06 -07001053 for (const auto& gatherer : gatherers) {
1054 gatherer->ExtractStats(this);
1055 has_remote_audio |= gatherer->HasRemoteAudio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001057
Steve Antonb8867112018-02-13 10:07:54 -08001058 UpdateStatsFromExistingLocalAudioTracks(has_remote_audio);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059}
1060
nissefcc640f2016-04-01 01:10:42 -07001061void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001062 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -07001063
1064 for (const auto& sender : pc_->GetSenders()) {
1065 // TODO(nisse): SSRC == 0 currently means none. Delete check when
1066 // that is fixed.
1067 if (!sender->ssrc()) {
1068 continue;
1069 }
1070 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
1071 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
1072 continue;
1073 }
1074 // Safe, because kind() == kVideoKind implies a subclass of
1075 // VideoTrackInterface; see mediastreaminterface.h.
1076 VideoTrackSourceInterface* source =
1077 static_cast<VideoTrackInterface*>(track.get())->GetSource();
1078
1079 VideoTrackSourceInterface::Stats stats;
1080 if (!source->GetStats(&stats)) {
1081 continue;
1082 }
1083 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001084 StatsReport::kStatsReportTypeSsrc, rtc::ToString(sender->ssrc()),
1085 StatsReport::kSend);
nissefcc640f2016-04-01 01:10:42 -07001086 StatsReport* report = reports_.FindOrAddNew(stats_id);
1087 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
1088 stats.input_width);
1089 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
1090 stats.input_height);
1091 }
1092}
1093
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001094void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001095 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001096
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001097 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1098
deadbeefab9b2d12015-10-14 11:33:11 -07001099 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001100 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001101 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001102 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001103 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001104 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -07001105 // Filter out the initial id (-1).
1106 if (dc->id() >= 0) {
1107 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
1108 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001109 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
1110 report->AddString(StatsReport::kStatsValueNameState,
1111 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001112 }
1113}
1114
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001115StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001116 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001117 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001118 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001119 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
1120 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001121 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001122}
1123
Ivo Creusenae026092017-11-20 13:07:16 +01001124void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
1125 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001126 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001127 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001128 for (const auto& it : local_audio_tracks_) {
1129 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001130 uint32_t ssrc = it.second;
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001131 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
1132 rtc::ToString(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001133 if (report == NULL) {
1134 // This can happen if a local audio track is added to a stream on the
1135 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001136 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001137 continue;
1138 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001139
1140 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001141 const StatsReport::Value* v =
1142 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001143 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001144 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001145
jbauchbe24c942015-06-22 15:06:43 -07001146 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001147 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001148 }
1149}
1150
1151void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001152 StatsReport* report,
1153 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001154 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001155 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001156
andrew2fe1cb02015-11-27 17:27:35 -08001157 // Don't overwrite report values if they're not available.
1158 int signal_level;
1159 if (track->GetSignalLevel(&signal_level)) {
1160 RTC_DCHECK_GE(signal_level, 0);
1161 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1162 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001163
andrew2fe1cb02015-11-27 17:27:35 -08001164 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001165
andrew2fe1cb02015-11-27 17:27:35 -08001166 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001167 AudioProcessorInterface::AudioProcessorStatistics stats =
1168 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001169
Ivo Creusen56d46092017-11-24 17:29:59 +01001170 SetAudioProcessingStats(report, stats.typing_noise_detected,
1171 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001172 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001173}
1174
jbauchbe24c942015-06-22 15:06:43 -07001175void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001176 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001177
1178 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1179
1180 for (const auto& entry : track_ids_) {
1181 StatsReport* report = entry.second;
1182 report->set_timestamp(stats_gathering_started_);
1183 }
jbauchbe24c942015-06-22 15:06:43 -07001184}
1185
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001186void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001187 stats_gathering_started_ = 0;
1188}
1189
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190} // namespace webrtc