blob: e1930a1fc3c8aecd1a9e91cc470c3b8d9bbc9e8d [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 }
396 RTC_LOG(LS_INFO) << "Missing track ID for "
397 << (direction == StatsReport::kSend ? "send" : "recv")
398 << " SSRC=" << ssrc << ".";
399 return "";
400}
401
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000403// In order to use the template, the functions that are called from it,
404// ExtractStats and ExtractRemoteStats, must be defined and overloaded
405// for each type.
Yves Gerey665174f2018-06-19 15:03:05 +0200406template <typename T>
Steve Antonefe4c922019-03-27 10:26:06 -0700407void ExtractStatsFromList(
408 const std::vector<T>& data,
409 const StatsReport::Id& transport_id,
410 StatsCollector* collector,
411 StatsReport::Direction direction,
412 const std::map<uint32_t, std::string>& track_id_by_ssrc) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000413 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200414 uint32_t ssrc = d.ssrc();
Steve Antonefe4c922019-03-27 10:26:06 -0700415 std::string track_id = GetTrackIdBySsrc(ssrc, direction, track_id_by_ssrc);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000416 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000417 // TODO(hta): Handle the case of multiple SSRCs per object.
Yves Gerey665174f2018-06-19 15:03:05 +0200418 StatsReport* report =
Steve Antonefe4c922019-03-27 10:26:06 -0700419 collector->PrepareReport(true, ssrc, track_id, transport_id, direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000420 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000421 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000422
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000423 if (!d.remote_stats.empty()) {
Steve Antonefe4c922019-03-27 10:26:06 -0700424 report = collector->PrepareReport(false, ssrc, track_id, transport_id,
425 direction);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000426 if (report)
427 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000428 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000430}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431
432} // namespace
433
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000434const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
435 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
436 return STATSREPORT_LOCAL_PORT_TYPE;
437 }
438 if (candidate_type == cricket::STUN_PORT_TYPE) {
439 return STATSREPORT_STUN_PORT_TYPE;
440 }
441 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
442 return STATSREPORT_PRFLX_PORT_TYPE;
443 }
444 if (candidate_type == cricket::RELAY_PORT_TYPE) {
445 return STATSREPORT_RELAY_PORT_TYPE;
446 }
nisseeb4ca4e2017-01-12 02:24:27 -0800447 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000448 return "unknown";
449}
450
451const char* AdapterTypeToStatsType(rtc::AdapterType type) {
452 switch (type) {
453 case rtc::ADAPTER_TYPE_UNKNOWN:
454 return "unknown";
455 case rtc::ADAPTER_TYPE_ETHERNET:
456 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
457 case rtc::ADAPTER_TYPE_WIFI:
458 return STATSREPORT_ADAPTER_TYPE_WIFI;
459 case rtc::ADAPTER_TYPE_CELLULAR:
460 return STATSREPORT_ADAPTER_TYPE_WWAN;
461 case rtc::ADAPTER_TYPE_VPN:
462 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000463 case rtc::ADAPTER_TYPE_LOOPBACK:
464 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
Qingsi Wang01560de2018-07-26 10:44:02 -0700465 case rtc::ADAPTER_TYPE_ANY:
466 return STATSREPORT_ADAPTER_TYPE_WILDCARD;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000467 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800468 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000469 return "";
470 }
471}
472
Steve Anton2d8609c2018-01-23 16:38:46 -0800473StatsCollector::StatsCollector(PeerConnectionInternal* pc)
deadbeefab9b2d12015-10-14 11:33:11 -0700474 : pc_(pc), stats_gathering_started_(0) {
475 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000476}
477
478StatsCollector::~StatsCollector() {
Steve Anton978b8762017-09-29 12:15:02 -0700479 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480}
481
nissecdf37a92016-09-13 23:41:47 -0700482// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000483double StatsCollector::GetTimeNow() {
Minyue Li656d6092018-08-10 15:38:52 +0200484 return static_cast<double>(rtc::TimeUTCMillis());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000485}
486
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487// Adds a MediaStream with tracks that can be used as a |selector| in a call
488// to GetStats.
489void StatsCollector::AddStream(MediaStreamInterface* stream) {
Steve Anton978b8762017-09-29 12:15:02 -0700490 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700491 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
Steve Anton36b29d12017-10-30 09:57:42 -0700493 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), &reports_,
494 &track_ids_);
495 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), &reports_,
496 &track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497}
498
Harald Alvestrand75ceef22018-01-04 15:26:13 +0100499void StatsCollector::AddTrack(MediaStreamTrackInterface* track) {
500 if (track->kind() == MediaStreamTrackInterface::kAudioKind) {
501 CreateTrackReport(static_cast<AudioTrackInterface*>(track), &reports_,
502 &track_ids_);
503 } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) {
504 CreateTrackReport(static_cast<VideoTrackInterface*>(track), &reports_,
505 &track_ids_);
506 } else {
507 RTC_NOTREACHED() << "Illegal track kind";
508 }
509}
510
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000511void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200512 uint32_t ssrc) {
Steve Anton978b8762017-09-29 12:15:02 -0700513 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700514 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700515#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000516 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700517 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000518#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000519
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000520 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000521
522 // Create the kStatsReportTypeTrack report for the new track if there is no
523 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000524 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
525 audio_track->id()));
526 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000527 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000528 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000529 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000530 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000531}
532
533void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200534 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700535 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700536 local_audio_tracks_.erase(
537 std::remove_if(
538 local_audio_tracks_.begin(), local_audio_tracks_.end(),
539 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
540 return track.first == audio_track && track.second == ssrc;
541 }),
542 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000543}
544
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000545void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 StatsReports* reports) {
Steve Anton978b8762017-09-29 12:15:02 -0700547 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700548 RTC_DCHECK(reports != NULL);
549 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000551 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
552
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000554 reports->reserve(reports_.size());
555 for (auto* r : reports_)
556 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000557 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 }
559
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000560 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700561 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000562 if (report)
563 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564
Yves Gerey665174f2018-06-19 15:03:05 +0200565 report = reports_.Find(
566 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000568 if (!report)
569 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000571 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
573 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000574 for (const auto* r : reports_) {
575 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000577
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000578 const StatsReport::Value* v =
579 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000580 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000581 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583}
584
Yves Gerey665174f2018-06-19 15:03:05 +0200585void StatsCollector::UpdateStats(
586 PeerConnectionInterface::StatsOutputLevel level) {
Steve Anton978b8762017-09-29 12:15:02 -0700587 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 double time_now = GetTimeNow();
589 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
590 // ms apart will be ignored.
591 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000592 if (stats_gathering_started_ != 0 &&
593 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 return;
595 }
596 stats_gathering_started_ = time_now;
597
Steve Anton75737c02017-11-06 10:37:17 -0800598 // TODO(tommi): All of these hop over to the worker thread to fetch
599 // information. We could use an AsyncInvoker to run all of these and post
600 // the information back to the signaling thread where we can create and
601 // update stats reports. That would also clean up the threading story a bit
602 // since we'd be creating/updating the stats report objects consistently on
603 // the same thread (this class has no locks right now).
604 ExtractSessionInfo();
605 ExtractBweInfo();
Steve Antonb8867112018-02-13 10:07:54 -0800606 ExtractMediaInfo();
Steve Anton75737c02017-11-06 10:37:17 -0800607 ExtractSenderInfo();
608 ExtractDataInfo();
609 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610}
611
Yves Gerey665174f2018-06-19 15:03:05 +0200612StatsReport* StatsCollector::PrepareReport(bool local,
613 uint32_t ssrc,
Steve Antonefe4c922019-03-27 10:26:06 -0700614 const std::string& track_id,
Yves Gerey665174f2018-06-19 15:03:05 +0200615 const StatsReport::Id& transport_id,
616 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -0700617 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000618 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200619 local ? StatsReport::kStatsReportTypeSsrc
620 : StatsReport::kStatsReportTypeRemoteSsrc,
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200621 rtc::ToString(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000622 StatsReport* report = reports_.Find(id);
Steve Antona41959e2018-11-28 11:15:33 -0800623 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000624 report = reports_.InsertNew(id);
Steve Antona41959e2018-11-28 11:15:33 -0800625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000627 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000628 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000630 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700631 if (!track_id.empty()) {
Steve Antonefe4c922019-03-27 10:26:06 -0700632 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
Taylor Brandstettera4653442018-06-19 09:44:26 -0700633 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000635 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 return report;
637}
638
zhihuange9e94c32016-11-04 11:38:15 -0700639bool StatsCollector::IsValidTrack(const std::string& track_id) {
640 return reports_.Find(StatsReport::NewTypedId(
641 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
642}
643
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000644StatsReport* StatsCollector::AddCertificateReports(
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800645 std::unique_ptr<rtc::SSLCertificateStats> cert_stats) {
Steve Anton978b8762017-09-29 12:15:02 -0700646 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000647
hbose29352b2016-08-25 03:52:38 -0700648 StatsReport* first_report = nullptr;
649 StatsReport* prev_report = nullptr;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800650 for (rtc::SSLCertificateStats* stats = cert_stats.get(); stats;
hbose29352b2016-08-25 03:52:38 -0700651 stats = stats->issuer.get()) {
652 StatsReport::Id id(StatsReport::NewTypedId(
653 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
654
655 StatsReport* report = reports_.ReplaceOrAddNew(id);
656 report->set_timestamp(stats_gathering_started_);
657 report->AddString(StatsReport::kStatsValueNameFingerprint,
658 stats->fingerprint);
659 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
660 stats->fingerprint_algorithm);
661 report->AddString(StatsReport::kStatsValueNameDer,
662 stats->base64_certificate);
663 if (!first_report)
664 first_report = report;
665 else
666 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
667 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000668 }
hbose29352b2016-08-25 03:52:38 -0700669 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000670}
671
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672StatsReport* StatsCollector::AddConnectionInfoReport(
Yves Gerey665174f2018-06-19 15:03:05 +0200673 const std::string& content_name,
674 int component,
675 int connection_id,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000676 const StatsReport::Id& channel_report_id,
677 const cricket::ConnectionInfo& info) {
Yves Gerey665174f2018-06-19 15:03:05 +0200678 StatsReport::Id id(
679 StatsReport::NewCandidatePairId(content_name, component, connection_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000680 StatsReport* report = reports_.ReplaceOrAddNew(id);
681 report->set_timestamp(stats_gathering_started_);
682
683 const BoolForAdd bools[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200684 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
685 {StatsReport::kStatsValueNameReceiving, info.receiving},
686 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000687 };
688 for (const auto& b : bools)
689 report->AddBoolean(b.name, b.value);
690
691 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
Qingsi Wang72a43a12018-02-20 16:03:18 -0800692 cricket::CandidateStats local_candidate_stats(info.local_candidate);
693 cricket::CandidateStats remote_candidate_stats(info.remote_candidate);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000694 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800695 AddCandidateReport(local_candidate_stats, true)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000696 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
Qingsi Wang72a43a12018-02-20 16:03:18 -0800697 AddCandidateReport(remote_candidate_stats, false)->id());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000698
699 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700700 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
701 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
702 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
703 {StatsReport::kStatsValueNameRtt, info.rtt},
704 {StatsReport::kStatsValueNameSendPacketsDiscarded,
705 info.sent_discarded_packets},
706 {StatsReport::kStatsValueNameSentPingRequestsTotal,
707 info.sent_ping_requests_total},
708 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
709 info.sent_ping_requests_before_first_response},
710 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
711 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
712 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000713 };
714 for (const auto& i : int64s)
715 report->AddInt64(i.name, i.value);
716
717 report->AddString(StatsReport::kStatsValueNameLocalAddress,
718 info.local_candidate.address().ToString());
719 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
720 info.local_candidate.type());
721 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
722 info.remote_candidate.address().ToString());
723 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
724 info.remote_candidate.type());
725 report->AddString(StatsReport::kStatsValueNameTransportType,
726 info.local_candidate.protocol());
727
728 return report;
729}
730
731StatsReport* StatsCollector::AddCandidateReport(
Qingsi Wang72a43a12018-02-20 16:03:18 -0800732 const cricket::CandidateStats& candidate_stats,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000733 bool local) {
Qingsi Wang72a43a12018-02-20 16:03:18 -0800734 const auto& candidate = candidate_stats.candidate;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000735 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
736 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000737 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000738 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000739 report->set_timestamp(stats_gathering_started_);
740 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000741 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
742 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000743 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000744 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
745 candidate.address().ipaddr().ToString());
746 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
747 candidate.address().PortAsString());
748 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
749 candidate.priority());
750 report->AddString(StatsReport::kStatsValueNameCandidateType,
751 IceCandidateTypeToStatsType(candidate.type()));
752 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
753 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000754 }
Philipp Hanckefe0b4992019-03-22 09:00:50 +0100755 report->set_timestamp(stats_gathering_started_);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000756
Qingsi Wang4ff54432018-03-01 18:25:20 -0800757 if (local && candidate_stats.stun_stats.has_value()) {
758 const auto& stun_stats = candidate_stats.stun_stats.value();
759 report->AddInt64(StatsReport::kStatsValueNameSentStunKeepaliveRequests,
760 stun_stats.stun_binding_requests_sent);
761 report->AddInt64(StatsReport::kStatsValueNameRecvStunKeepaliveResponses,
762 stun_stats.stun_binding_responses_received);
763 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttTotal,
764 stun_stats.stun_binding_rtt_ms_total);
765 report->AddFloat(StatsReport::kStatsValueNameStunKeepaliveRttSquaredTotal,
766 stun_stats.stun_binding_rtt_ms_squared_total);
767 }
768
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000769 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000770}
771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772void StatsCollector::ExtractSessionInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700773 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000774
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000776 StatsReport::Id id(StatsReport::NewTypedId(
Steve Anton978b8762017-09-29 12:15:02 -0700777 StatsReport::kStatsReportTypeSession, pc_->session_id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000778 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000779 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000780 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
Steve Anton978b8762017-09-29 12:15:02 -0700781 pc_->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782
Qingsi Wang72a43a12018-02-20 16:03:18 -0800783 cricket::CandidateStatsList pooled_candidate_stats_list =
784 pc_->GetPooledCandidateStats();
785
786 for (const cricket::CandidateStats& stats : pooled_candidate_stats_list) {
787 AddCandidateReport(stats, true);
788 }
789
Steve Anton5dfde182018-02-06 10:34:40 -0800790 std::set<std::string> transport_names;
791 for (const auto& entry : pc_->GetTransportNamesByMid()) {
792 transport_names.insert(entry.second);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000793 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794
Steve Anton5dfde182018-02-06 10:34:40 -0800795 std::map<std::string, cricket::TransportStats> transport_stats_by_name =
796 pc_->GetTransportStatsByNames(transport_names);
797
798 for (const auto& entry : transport_stats_by_name) {
799 const std::string& transport_name = entry.first;
800 const cricket::TransportStats& transport_stats = entry.second;
801
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000802 // Attempt to get a copy of the certificates from the transport and
803 // expose them in stats reports. All channels in a transport share the
804 // same local and remote certificates.
805 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000806 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200807 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Steve Anton5dfde182018-02-06 10:34:40 -0800808 if (pc_->GetLocalCertificate(transport_name, &certificate)) {
Benjamin Wright6c6c9df2018-10-25 01:16:26 -0700809 StatsReport* r = AddCertificateReports(
810 certificate->GetSSLCertificateChain().GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000811 if (r)
812 local_cert_report_id = r->id();
813 }
814
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800815 std::unique_ptr<rtc::SSLCertChain> remote_cert_chain =
816 pc_->GetRemoteSSLCertChain(transport_name);
817 if (remote_cert_chain) {
818 StatsReport* r = AddCertificateReports(remote_cert_chain->GetStats());
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000819 if (r)
820 remote_cert_report_id = r->id();
821 }
822
Steve Anton5dfde182018-02-06 10:34:40 -0800823 for (const auto& channel_iter : transport_stats.channel_stats) {
824 StatsReport::Id id(
825 StatsReport::NewComponentId(transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000826 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
827 channel_report->set_timestamp(stats_gathering_started_);
828 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
829 channel_iter.component);
830 if (local_cert_report_id.get()) {
831 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
832 local_cert_report_id);
833 }
834 if (remote_cert_report_id.get()) {
835 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
836 remote_cert_report_id);
837 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800838 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
839 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
840 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
841 channel_report->AddString(
842 StatsReport::kStatsValueNameSrtpCipher,
843 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000844 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800845 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
846 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
847 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
848 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700849 channel_report->AddString(
850 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800851 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000852 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000853
Qingsi Wang72a43a12018-02-20 16:03:18 -0800854 // Collect stats for non-pooled candidates. Note that the reports
855 // generated here supersedes the candidate reports generated in
856 // AddConnectionInfoReport below, and they may report candidates that are
857 // not paired. Also, the candidate report generated in
858 // AddConnectionInfoReport do not report port stats like StunStats.
859 for (const cricket::CandidateStats& stats :
860 channel_iter.candidate_stats_list) {
861 AddCandidateReport(stats, true);
862 }
863
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000864 int connection_id = 0;
865 for (const cricket::ConnectionInfo& info :
Yves Gerey665174f2018-06-19 15:03:05 +0200866 channel_iter.connection_infos) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000867 StatsReport* connection_report = AddConnectionInfoReport(
Steve Anton5dfde182018-02-06 10:34:40 -0800868 transport_name, channel_iter.component, connection_id++,
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000869 channel_report->id(), info);
870 if (info.best_connection) {
871 channel_report->AddId(
872 StatsReport::kStatsValueNameSelectedCandidatePairId,
873 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 }
875 }
876 }
877 }
878}
879
stefanf79ade12017-06-02 06:44:03 -0700880void StatsCollector::ExtractBweInfo() {
Steve Anton978b8762017-09-29 12:15:02 -0700881 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
stefanf79ade12017-06-02 06:44:03 -0700882
Steve Anton978b8762017-09-29 12:15:02 -0700883 if (pc_->signaling_state() == PeerConnectionInterface::kClosed)
stefanf79ade12017-06-02 06:44:03 -0700884 return;
885
Steve Anton978b8762017-09-29 12:15:02 -0700886 webrtc::Call::Stats call_stats = pc_->GetCallStats();
stefanf79ade12017-06-02 06:44:03 -0700887 cricket::BandwidthEstimationInfo bwe_info;
888 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
889 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
890 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
Steve Antonafb0bb72018-02-20 11:35:37 -0800891
stefanf79ade12017-06-02 06:44:03 -0700892 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
893 // TODO(holmer): Also fill this in for audio.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100894 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Antonafb0bb72018-02-20 11:35:37 -0800895 if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO) {
896 continue;
897 }
898 auto* video_channel =
899 static_cast<cricket::VideoChannel*>(transceiver->internal()->channel());
900 if (!video_channel) {
901 continue;
902 }
903 video_channel->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700904 }
Steve Antonafb0bb72018-02-20 11:35:37 -0800905
stefanf79ade12017-06-02 06:44:03 -0700906 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
907 StatsReport* report = reports_.FindOrAddNew(report_id);
908 ExtractStats(bwe_info, stats_gathering_started_, report);
909}
910
Steve Antonb8867112018-02-13 10:07:54 -0800911namespace {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000912
Steve Antonefe4c922019-03-27 10:26:06 -0700913class MediaChannelStatsGatherer {
914 public:
915 virtual ~MediaChannelStatsGatherer() = default;
916
917 virtual bool GetStatsOnWorkerThread() = 0;
918
919 virtual void ExtractStats(StatsCollector* collector) const = 0;
920
921 virtual bool HasRemoteAudio() const = 0;
922
923 std::string mid;
Steve Antonb8867112018-02-13 10:07:54 -0800924 std::string transport_name;
Steve Antonefe4c922019-03-27 10:26:06 -0700925 std::map<uint32_t, std::string> sender_track_id_by_ssrc;
926 std::map<uint32_t, std::string> receiver_track_id_by_ssrc;
927
928 protected:
929 template <typename ReceiverT, typename SenderT>
930 void ExtractSenderReceiverStats(
931 StatsCollector* collector,
932 const std::vector<ReceiverT>& receiver_data,
933 const std::vector<SenderT>& sender_data) const {
934 RTC_DCHECK(collector);
935 StatsReport::Id transport_id = StatsReport::NewComponentId(
936 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP);
937 ExtractStatsFromList(receiver_data, transport_id, collector,
938 StatsReport::kReceive, receiver_track_id_by_ssrc);
939 ExtractStatsFromList(sender_data, transport_id, collector,
940 StatsReport::kSend, sender_track_id_by_ssrc);
941 }
942};
943
944class VoiceMediaChannelStatsGatherer final : public MediaChannelStatsGatherer {
945 public:
946 VoiceMediaChannelStatsGatherer(
947 cricket::VoiceMediaChannel* voice_media_channel)
948 : voice_media_channel_(voice_media_channel) {
949 RTC_DCHECK(voice_media_channel_);
950 }
951
952 bool GetStatsOnWorkerThread() override {
953 return voice_media_channel_->GetStats(&voice_media_info);
954 }
955
956 void ExtractStats(StatsCollector* collector) const override {
957 ExtractSenderReceiverStats(collector, voice_media_info.receivers,
958 voice_media_info.senders);
959 }
960
961 bool HasRemoteAudio() const override {
962 return !voice_media_info.receivers.empty();
963 }
964
965 private:
966 cricket::VoiceMediaChannel* voice_media_channel_;
Steve Antonb8867112018-02-13 10:07:54 -0800967 cricket::VoiceMediaInfo voice_media_info;
968};
969
Steve Antonefe4c922019-03-27 10:26:06 -0700970class VideoMediaChannelStatsGatherer final : public MediaChannelStatsGatherer {
971 public:
972 VideoMediaChannelStatsGatherer(
973 cricket::VideoMediaChannel* video_media_channel)
974 : video_media_channel_(video_media_channel) {
975 RTC_DCHECK(video_media_channel_);
976 }
977
978 bool GetStatsOnWorkerThread() override {
979 return video_media_channel_->GetStats(&video_media_info);
980 }
981
982 void ExtractStats(StatsCollector* collector) const override {
983 ExtractSenderReceiverStats(collector, video_media_info.receivers,
984 video_media_info.senders);
985 }
986
987 bool HasRemoteAudio() const override { return false; }
988
989 private:
990 cricket::VideoMediaChannel* video_media_channel_;
Steve Antonb8867112018-02-13 10:07:54 -0800991 cricket::VideoMediaInfo video_media_info;
992};
993
Steve Antonefe4c922019-03-27 10:26:06 -0700994std::unique_ptr<MediaChannelStatsGatherer> CreateMediaChannelStatsGatherer(
995 cricket::MediaChannel* channel) {
996 RTC_DCHECK(channel);
997 if (channel->media_type() == cricket::MEDIA_TYPE_AUDIO) {
998 return absl::make_unique<VoiceMediaChannelStatsGatherer>(
999 static_cast<cricket::VoiceMediaChannel*>(channel));
1000 } else {
1001 RTC_DCHECK_EQ(channel->media_type(), cricket::MEDIA_TYPE_VIDEO);
1002 return absl::make_unique<VideoMediaChannelStatsGatherer>(
1003 static_cast<cricket::VideoMediaChannel*>(channel));
1004 }
1005}
1006
Steve Antonb8867112018-02-13 10:07:54 -08001007} // namespace
1008
1009void StatsCollector::ExtractMediaInfo() {
1010 RTC_DCHECK_RUN_ON(pc_->signaling_thread());
1011
Steve Antonefe4c922019-03-27 10:26:06 -07001012 std::vector<std::unique_ptr<MediaChannelStatsGatherer>> gatherers;
Steve Antonb8867112018-02-13 10:07:54 -08001013
1014 {
1015 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Mirko Bonadei739baf02019-01-27 17:29:42 +01001016 for (const auto& transceiver : pc_->GetTransceiversInternal()) {
Steve Antonefe4c922019-03-27 10:26:06 -07001017 cricket::ChannelInterface* channel = transceiver->internal()->channel();
1018 if (!channel) {
Steve Antonb8867112018-02-13 10:07:54 -08001019 continue;
1020 }
Steve Antonefe4c922019-03-27 10:26:06 -07001021 std::unique_ptr<MediaChannelStatsGatherer> gatherer =
1022 CreateMediaChannelStatsGatherer(channel->media_channel());
1023 gatherer->mid = channel->content_name();
1024 gatherer->transport_name = channel->transport_name();
1025 for (const auto& sender : transceiver->internal()->senders()) {
1026 std::string track_id = (sender->track() ? sender->track()->id() : "");
1027 gatherer->sender_track_id_by_ssrc.insert(
1028 std::make_pair(sender->ssrc(), track_id));
Steve Antonb8867112018-02-13 10:07:54 -08001029 }
Steve Antonefe4c922019-03-27 10:26:06 -07001030 for (const auto& receiver : transceiver->internal()->receivers()) {
1031 gatherer->receiver_track_id_by_ssrc.insert(std::make_pair(
1032 receiver->internal()->ssrc(), receiver->track()->id()));
1033 }
1034 gatherers.push_back(std::move(gatherer));
Steve Antonb8867112018-02-13 10:07:54 -08001035 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001037
Steve Antonb8867112018-02-13 10:07:54 -08001038 pc_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&] {
1039 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
Steve Antonefe4c922019-03-27 10:26:06 -07001040 for (auto it = gatherers.begin(); it != gatherers.end();
Steve Antonb8867112018-02-13 10:07:54 -08001041 /* incremented manually */) {
Steve Antonefe4c922019-03-27 10:26:06 -07001042 MediaChannelStatsGatherer* gatherer = it->get();
1043 if (!gatherer->GetStatsOnWorkerThread()) {
1044 RTC_LOG(LS_ERROR) << "Failed to get media channel stats for mid="
1045 << gatherer->mid;
1046 it = gatherers.erase(it);
Steve Antonb8867112018-02-13 10:07:54 -08001047 continue;
1048 }
1049 ++it;
1050 }
1051 });
1052
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001053 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1054
Steve Antonb8867112018-02-13 10:07:54 -08001055 bool has_remote_audio = false;
Steve Antonefe4c922019-03-27 10:26:06 -07001056 for (const auto& gatherer : gatherers) {
1057 gatherer->ExtractStats(this);
1058 has_remote_audio |= gatherer->HasRemoteAudio();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001060
Steve Antonb8867112018-02-13 10:07:54 -08001061 UpdateStatsFromExistingLocalAudioTracks(has_remote_audio);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062}
1063
nissefcc640f2016-04-01 01:10:42 -07001064void StatsCollector::ExtractSenderInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001065 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
nissefcc640f2016-04-01 01:10:42 -07001066
1067 for (const auto& sender : pc_->GetSenders()) {
1068 // TODO(nisse): SSRC == 0 currently means none. Delete check when
1069 // that is fixed.
1070 if (!sender->ssrc()) {
1071 continue;
1072 }
1073 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
1074 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
1075 continue;
1076 }
1077 // Safe, because kind() == kVideoKind implies a subclass of
1078 // VideoTrackInterface; see mediastreaminterface.h.
1079 VideoTrackSourceInterface* source =
1080 static_cast<VideoTrackInterface*>(track.get())->GetSource();
1081
1082 VideoTrackSourceInterface::Stats stats;
1083 if (!source->GetStats(&stats)) {
1084 continue;
1085 }
1086 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001087 StatsReport::kStatsReportTypeSsrc, rtc::ToString(sender->ssrc()),
1088 StatsReport::kSend);
nissefcc640f2016-04-01 01:10:42 -07001089 StatsReport* report = reports_.FindOrAddNew(stats_id);
1090 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
1091 stats.input_width);
1092 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
1093 stats.input_height);
1094 }
1095}
1096
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001097void StatsCollector::ExtractDataInfo() {
Steve Anton978b8762017-09-29 12:15:02 -07001098 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001099
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001100 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1101
deadbeefab9b2d12015-10-14 11:33:11 -07001102 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001103 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001104 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001105 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +00001106 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001107 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -07001108 // Filter out the initial id (-1).
1109 if (dc->id() >= 0) {
1110 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
1111 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001112 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
1113 report->AddString(StatsReport::kStatsValueNameState,
1114 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +00001115 }
1116}
1117
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001118StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001119 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001120 StatsReport::Direction direction) {
Steve Anton978b8762017-09-29 12:15:02 -07001121 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001122 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
1123 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001124 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001125}
1126
Ivo Creusenae026092017-11-20 13:07:16 +01001127void StatsCollector::UpdateStatsFromExistingLocalAudioTracks(
1128 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001129 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001130 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001131 for (const auto& it : local_audio_tracks_) {
1132 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001133 uint32_t ssrc = it.second;
Jonas Olsson6b1985d2018-07-05 11:59:48 +02001134 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
1135 rtc::ToString(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001136 if (report == NULL) {
1137 // This can happen if a local audio track is added to a stream on the
1138 // fly and the report has not been set up yet. Do nothing in this case.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001139 RTC_LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001140 continue;
1141 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001142
1143 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001144 const StatsReport::Value* v =
1145 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +00001146 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001147 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001148
jbauchbe24c942015-06-22 15:06:43 -07001149 report->set_timestamp(stats_gathering_started_);
Ivo Creusenae026092017-11-20 13:07:16 +01001150 UpdateReportFromAudioTrack(track, report, has_remote_tracks);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001151 }
1152}
1153
1154void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
Ivo Creusenae026092017-11-20 13:07:16 +01001155 StatsReport* report,
1156 bool has_remote_tracks) {
Steve Anton978b8762017-09-29 12:15:02 -07001157 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -07001158 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001159
andrew2fe1cb02015-11-27 17:27:35 -08001160 // Don't overwrite report values if they're not available.
1161 int signal_level;
1162 if (track->GetSignalLevel(&signal_level)) {
1163 RTC_DCHECK_GE(signal_level, 0);
1164 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
1165 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001166
andrew2fe1cb02015-11-27 17:27:35 -08001167 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001168
andrew2fe1cb02015-11-27 17:27:35 -08001169 if (audio_processor.get()) {
Ivo Creusenae026092017-11-20 13:07:16 +01001170 AudioProcessorInterface::AudioProcessorStatistics stats =
1171 audio_processor->GetStats(has_remote_tracks);
tommi@webrtc.org92f40182015-03-04 15:25:19 +00001172
Ivo Creusen56d46092017-11-24 17:29:59 +01001173 SetAudioProcessingStats(report, stats.typing_noise_detected,
1174 stats.apm_statistics);
andrew2fe1cb02015-11-27 17:27:35 -08001175 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001176}
1177
jbauchbe24c942015-06-22 15:06:43 -07001178void StatsCollector::UpdateTrackReports() {
Steve Anton978b8762017-09-29 12:15:02 -07001179 RTC_DCHECK(pc_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001180
1181 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1182
1183 for (const auto& entry : track_ids_) {
1184 StatsReport* report = entry.second;
1185 report->set_timestamp(stats_gathering_started_);
1186 }
jbauchbe24c942015-06-22 15:06:43 -07001187}
1188
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001189void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001190 stats_gathering_started_ = 0;
1191}
1192
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193} // namespace webrtc