blob: 7761d1079a88c2f3a9f58541f71891baedf3a5c3 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/statscollector.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <utility>
15#include <vector>
16
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/peerconnection.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/base64.h"
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +000019#include "webrtc/base/checks.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010020#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000025// The following is the enum RTCStatsIceCandidateType from
26// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
27// our stats report for ice candidate type could conform to that.
28const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
29const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
30const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
31const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
32
33// Strings used by the stats collector to report adapter types. This fits the
34// general stype of http://w3c.github.io/webrtc-stats than what
35// AdapterTypeToString does.
36const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
37const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
38const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
39const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000040const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000041
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000042template<typename ValueType>
43struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000044 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000046};
47
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000048typedef TypeForAdd<bool> BoolForAdd;
49typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020050typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000051typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000052
deadbeefd59daf82015-10-14 15:02:44 -070053StatsReport::Id GetTransportIdFromProxy(const ProxyTransportMap& map,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000054 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070055 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070056 auto found = map.find(proxy);
57 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000058 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070059 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000060
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061 return StatsReport::NewComponentId(
62 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000063}
64
jbauchbe24c942015-06-22 15:06:43 -070065StatsReport* AddTrackReport(StatsCollection* reports,
66 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000067 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000068 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000069 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000070 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000071 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070072 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000073}
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075template <class TrackVector>
jbauchbe24c942015-06-22 15:06:43 -070076void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
77 TrackIdMap& track_ids) {
78 for (const auto& track : tracks) {
79 const std::string& track_id = track->id();
80 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070081 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -070082 track_ids[track_id] = report;
83 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084}
85
tommi@webrtc.org92f40182015-03-04 15:25:19 +000086void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
87 StatsReport* report) {
88 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
89 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070090 if (info.rtt_ms >= 0) {
91 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
92 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000093}
94
pbosf42376c2015-08-28 07:35:32 -070095void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
96 StatsReport* report) {
97 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
98}
99
andrew2fe1cb02015-11-27 17:27:35 -0800100void SetAudioProcessingStats(StatsReport* report,
101 bool typing_noise_detected,
102 int echo_return_loss,
103 int echo_return_loss_enhancement,
104 int echo_delay_median_ms,
105 float aec_quality_min,
ivoc8c63a822016-10-21 04:10:03 -0700106 int echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800107 float residual_echo_likelihood,
108 float residual_echo_likelihood_recent_max) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000109 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
110 typing_noise_detected);
zhihuang6ba3b192016-05-13 11:46:35 -0700111 if (aec_quality_min >= 0.0f) {
112 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
113 aec_quality_min);
114 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000115 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000116 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
117 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
118 };
zhihuang6ba3b192016-05-13 11:46:35 -0700119 for (const auto& i : ints) {
120 if (i.value >= 0) {
121 report->AddInt(i.name, i.value);
122 }
123 }
124 // These can take on valid negative values.
125 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss);
126 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
127 echo_return_loss_enhancement);
ivoc8c63a822016-10-21 04:10:03 -0700128 if (residual_echo_likelihood >= 0.0f) {
129 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
130 residual_echo_likelihood);
henrik.lundin04a057b2017-01-16 23:53:59 -0800131 }
132 if (residual_echo_likelihood_recent_max >= 0.0f) {
ivoc4e477a12017-01-15 08:29:46 -0800133 report->AddFloat(
134 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
135 residual_echo_likelihood_recent_max);
ivoc8c63a822016-10-21 04:10:03 -0700136 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700140 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000141 const FloatForAdd floats[] = {
142 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
143 { StatsReport::kStatsValueNameSecondaryDecodedRate,
144 info.secondary_decoded_rate },
145 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200146 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
147 { StatsReport::kStatsValueNamePreemptiveExpandRate,
148 info.preemptive_expand_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000149 };
150
151 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000152 { 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 },
henrik.lundin63489782016-09-20 01:47:12 -0700157 { StatsReport::kStatsValueNameDecodingMutedOutput,
158 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000159 { 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 },
168 };
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
180 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700182 if (info.capture_start_ntp_time_ms >= 0) {
183 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
184 info.capture_start_ntp_time_ms);
185 }
fippobec70ab2016-01-28 01:27:15 -0800186 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187}
188
189void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000190 ExtractCommonSendProperties(info, report);
191
andrew2fe1cb02015-11-27 17:27:35 -0800192 SetAudioProcessingStats(
193 report, info.typing_noise_detected, info.echo_return_loss,
194 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700195 info.aec_quality_min, info.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800196 info.residual_echo_likelihood, info.residual_echo_likelihood_recent_max);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000197
andrew2fe1cb02015-11-27 17:27:35 -0800198 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000199 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800200 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000201 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
202 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
203 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
204 };
205
zhihuang6ba3b192016-05-13 11:46:35 -0700206 for (const auto& i : ints) {
207 if (i.value >= 0) {
208 report->AddInt(i.name, i.value);
209 }
210 }
fippobec70ab2016-01-28 01:27:15 -0800211 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212}
213
214void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700215 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100216 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
217 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000218 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700220 if (info.capture_start_ntp_time_ms >= 0) {
221 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
222 info.capture_start_ntp_time_ms);
223 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000224 const IntForAdd ints[] = {
225 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
226 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
227 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
228 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
229 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
230 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
231 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
232 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
233 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
234 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
235 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
236 info.min_playout_delay_ms },
237 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
238 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
239 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
240 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
241 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
242 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700243 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000244 };
245
246 for (const auto& i : ints)
247 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800248 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249}
250
251void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000252 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
Peter Boströmb7d9a972015-12-18 16:01:11 +0100254 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
255 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000256 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
257 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000258 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
259 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700260 if (info.qp_sum)
261 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262
263 const IntForAdd ints[] = {
264 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
265 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000266 { StatsReport::kStatsValueNameEncodeUsagePercent,
267 info.encode_usage_percent },
268 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000269 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
270 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
271 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000272 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
273 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
274 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
275 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
276 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700277 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000278 };
279
280 for (const auto& i : ints)
281 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800282 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283}
284
285void ExtractStats(const cricket::BandwidthEstimationInfo& info,
286 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000287 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700289 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000291 report->set_timestamp(stats_gathering_started);
292 const IntForAdd ints[] = {
293 { StatsReport::kStatsValueNameAvailableSendBandwidth,
294 info.available_send_bandwidth },
295 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
296 info.available_recv_bandwidth },
297 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
298 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
299 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
300 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
301 };
302 for (const auto& i : ints)
303 report->AddInt(i.name, i.value);
304 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305}
306
wu@webrtc.org97077a32013-10-25 21:18:33 +0000307void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
308 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000309 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000310 // TODO(hta): Extract some stats here.
311}
312
313void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
314 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000315 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000316 // TODO(hta): Extract some stats here.
317}
318
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000320// In order to use the template, the functions that are called from it,
321// ExtractStats and ExtractRemoteStats, must be defined and overloaded
322// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323template<typename T>
324void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000325 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000326 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000327 StatsReport::Direction direction) {
328 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200329 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000330 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000331 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000332 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
333 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000334 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000335 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000336
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000337 if (!d.remote_stats.empty()) {
338 report = collector->PrepareReport(false, ssrc, transport_id, direction);
339 if (report)
340 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000341 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000343}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344
345} // namespace
346
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000347const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
348 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
349 return STATSREPORT_LOCAL_PORT_TYPE;
350 }
351 if (candidate_type == cricket::STUN_PORT_TYPE) {
352 return STATSREPORT_STUN_PORT_TYPE;
353 }
354 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
355 return STATSREPORT_PRFLX_PORT_TYPE;
356 }
357 if (candidate_type == cricket::RELAY_PORT_TYPE) {
358 return STATSREPORT_RELAY_PORT_TYPE;
359 }
nisseeb4ca4e2017-01-12 02:24:27 -0800360 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000361 return "unknown";
362}
363
364const char* AdapterTypeToStatsType(rtc::AdapterType type) {
365 switch (type) {
366 case rtc::ADAPTER_TYPE_UNKNOWN:
367 return "unknown";
368 case rtc::ADAPTER_TYPE_ETHERNET:
369 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
370 case rtc::ADAPTER_TYPE_WIFI:
371 return STATSREPORT_ADAPTER_TYPE_WIFI;
372 case rtc::ADAPTER_TYPE_CELLULAR:
373 return STATSREPORT_ADAPTER_TYPE_WWAN;
374 case rtc::ADAPTER_TYPE_VPN:
375 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000376 case rtc::ADAPTER_TYPE_LOOPBACK:
377 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000378 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800379 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000380 return "";
381 }
382}
383
deadbeefab9b2d12015-10-14 11:33:11 -0700384StatsCollector::StatsCollector(PeerConnection* pc)
385 : pc_(pc), stats_gathering_started_(0) {
386 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000387}
388
389StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700390 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391}
392
nissecdf37a92016-09-13 23:41:47 -0700393// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000394double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700395 return rtc::TimeUTCMicros() /
396 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000397}
398
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399// Adds a MediaStream with tracks that can be used as a |selector| in a call
400// to GetStats.
401void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700402 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700403 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404
405 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700406 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700408 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409}
410
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000411void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200412 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700413 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700414 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700415#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000416 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700417 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000418#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000419
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000420 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000421
422 // Create the kStatsReportTypeTrack report for the new track if there is no
423 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000424 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
425 audio_track->id()));
426 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000427 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000428 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000429 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000430 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000431}
432
433void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200434 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700435 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000436 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
437 local_audio_tracks_.end(),
438 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
439 return track.first == audio_track && track.second == ssrc;
440 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000441}
442
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000443void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700445 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700446 RTC_DCHECK(reports != NULL);
447 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000449 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000452 reports->reserve(reports_.size());
453 for (auto* r : reports_)
454 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000455 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 }
457
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000458 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700459 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000460 if (report)
461 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000463 report = reports_.Find(StatsReport::NewTypedId(
464 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000466 if (!report)
467 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000469 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470
471 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000472 for (const auto* r : reports_) {
473 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000475
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000476 const StatsReport::Value* v =
477 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000478 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000479 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481}
482
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000483void
484StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700485 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 double time_now = GetTimeNow();
487 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
488 // ms apart will be ignored.
489 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000490 if (stats_gathering_started_ != 0 &&
491 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 return;
493 }
494 stats_gathering_started_ = time_now;
495
solenberg03d6d572016-03-01 12:42:03 -0800496 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
497 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700498 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000499 // TODO(tommi): All of these hop over to the worker thread to fetch
500 // information. We could use an AsyncInvoker to run all of these and post
501 // the information back to the signaling thread where we can create and
502 // update stats reports. That would also clean up the threading story a bit
503 // since we'd be creating/updating the stats report objects consistently on
504 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 ExtractSessionInfo();
506 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000507 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700508 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000509 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700510 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
512}
513
deadbeefab9b2d12015-10-14 11:33:11 -0700514StatsReport* StatsCollector::PrepareReport(
515 bool local,
516 uint32_t ssrc,
517 const StatsReport::Id& transport_id,
518 StatsReport::Direction direction) {
519 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000520 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200521 local ? StatsReport::kStatsReportTypeSsrc
522 : StatsReport::kStatsReportTypeRemoteSsrc,
523 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000524 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525
xians@webrtc.org01bda202014-07-09 07:38:38 +0000526 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000528 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000529 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000530 // The ssrc is not used by any track or existing report, return NULL
531 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000533 }
534
535 // The ssrc is not used by any existing track. Keeps the old track id
536 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000537 const StatsReport::Value* v =
538 report->FindValue(StatsReport::kStatsValueNameTrackId);
539 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000540 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 }
542
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000543 if (!report)
544 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000546 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000547 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000549 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000550 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000552 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 return report;
554}
555
zhihuange9e94c32016-11-04 11:38:15 -0700556bool StatsCollector::IsValidTrack(const std::string& track_id) {
557 return reports_.Find(StatsReport::NewTypedId(
558 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
559}
560
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000561StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000562 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700563 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700564 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000565
hbose29352b2016-08-25 03:52:38 -0700566 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
567 StatsReport* first_report = nullptr;
568 StatsReport* prev_report = nullptr;
569 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
570 stats = stats->issuer.get()) {
571 StatsReport::Id id(StatsReport::NewTypedId(
572 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
573
574 StatsReport* report = reports_.ReplaceOrAddNew(id);
575 report->set_timestamp(stats_gathering_started_);
576 report->AddString(StatsReport::kStatsValueNameFingerprint,
577 stats->fingerprint);
578 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
579 stats->fingerprint_algorithm);
580 report->AddString(StatsReport::kStatsValueNameDer,
581 stats->base64_certificate);
582 if (!first_report)
583 first_report = report;
584 else
585 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
586 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000587 }
hbose29352b2016-08-25 03:52:38 -0700588 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000589}
590
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000591StatsReport* StatsCollector::AddConnectionInfoReport(
592 const std::string& content_name, int component, int connection_id,
593 const StatsReport::Id& channel_report_id,
594 const cricket::ConnectionInfo& info) {
595 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
596 connection_id));
597 StatsReport* report = reports_.ReplaceOrAddNew(id);
598 report->set_timestamp(stats_gathering_started_);
599
600 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700601 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
602 {StatsReport::kStatsValueNameReceiving, info.receiving},
603 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000604 };
605 for (const auto& b : bools)
606 report->AddBoolean(b.name, b.value);
607
608 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
609 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
610 AddCandidateReport(info.local_candidate, true)->id());
611 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
612 AddCandidateReport(info.remote_candidate, false)->id());
613
614 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700615 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
616 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
617 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
618 {StatsReport::kStatsValueNameRtt, info.rtt},
619 {StatsReport::kStatsValueNameSendPacketsDiscarded,
620 info.sent_discarded_packets},
621 {StatsReport::kStatsValueNameSentPingRequestsTotal,
622 info.sent_ping_requests_total},
623 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
624 info.sent_ping_requests_before_first_response},
625 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
626 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
627 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000628 };
629 for (const auto& i : int64s)
630 report->AddInt64(i.name, i.value);
631
632 report->AddString(StatsReport::kStatsValueNameLocalAddress,
633 info.local_candidate.address().ToString());
634 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
635 info.local_candidate.type());
636 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
637 info.remote_candidate.address().ToString());
638 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
639 info.remote_candidate.type());
640 report->AddString(StatsReport::kStatsValueNameTransportType,
641 info.local_candidate.protocol());
642
643 return report;
644}
645
646StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000647 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000648 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000649 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
650 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000651 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000652 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000653 report->set_timestamp(stats_gathering_started_);
654 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000655 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
656 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000657 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000658 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
659 candidate.address().ipaddr().ToString());
660 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
661 candidate.address().PortAsString());
662 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
663 candidate.priority());
664 report->AddString(StatsReport::kStatsValueNameCandidateType,
665 IceCandidateTypeToStatsType(candidate.type()));
666 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
667 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000668 }
669
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000670 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000671}
672
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700674 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000675
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000677 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700678 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000679 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000680 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000681 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700682 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683
hbosdf6075a2016-12-19 04:58:02 -0800684 std::unique_ptr<SessionStats> stats = pc_->session()->GetStats_s();
685 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000686 return;
687 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000689 // Store the proxy map away for use in SSRC reporting.
690 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
691 // signaling thread after fetching them on the worker thread, then just use
692 // the proxy map directly from the session stats.
693 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
694 // data.
hbosdf6075a2016-12-19 04:58:02 -0800695 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000696
hbosdf6075a2016-12-19 04:58:02 -0800697 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000698 // Attempt to get a copy of the certificates from the transport and
699 // expose them in stats reports. All channels in a transport share the
700 // same local and remote certificates.
701 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000702 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200703 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700704 if (pc_->session()->GetLocalCertificate(
705 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200706 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000707 if (r)
708 local_cert_report_id = r->id();
709 }
710
jbauch555604a2016-04-26 03:13:22 -0700711 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700712 pc_->session()->GetRemoteSSLCertificate(
713 transport_iter.second.transport_name);
714 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000715 StatsReport* r = AddCertificateReports(cert.get());
716 if (r)
717 remote_cert_report_id = r->id();
718 }
719
720 for (const auto& channel_iter : transport_iter.second.channel_stats) {
721 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700722 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000723 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
724 channel_report->set_timestamp(stats_gathering_started_);
725 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
726 channel_iter.component);
727 if (local_cert_report_id.get()) {
728 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
729 local_cert_report_id);
730 }
731 if (remote_cert_report_id.get()) {
732 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
733 remote_cert_report_id);
734 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800735 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
736 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
737 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
738 channel_report->AddString(
739 StatsReport::kStatsValueNameSrtpCipher,
740 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000741 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800742 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
743 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
744 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
745 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700746 channel_report->AddString(
747 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800748 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000749 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000750
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000751 int connection_id = 0;
752 for (const cricket::ConnectionInfo& info :
753 channel_iter.connection_infos) {
754 StatsReport* connection_report = AddConnectionInfoReport(
755 transport_iter.first, channel_iter.component, connection_id++,
756 channel_report->id(), info);
757 if (info.best_connection) {
758 channel_report->AddId(
759 StatsReport::kStatsValueNameSelectedCandidatePairId,
760 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 }
762 }
763 }
764 }
765}
766
767void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700768 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000769
deadbeefab9b2d12015-10-14 11:33:11 -0700770 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 return;
772 }
773 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700774 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 LOG(LS_ERROR) << "Failed to get voice channel stats.";
776 return;
777 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000778
779 // TODO(tommi): The above code should run on the worker thread and post the
780 // results back to the signaling thread, where we can add data to the reports.
781 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
782
deadbeefab9b2d12015-10-14 11:33:11 -0700783 StatsReport::Id transport_id(GetTransportIdFromProxy(
784 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000785 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700787 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 return;
789 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000790
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000791 ExtractStatsFromList(voice_info.receivers, transport_id, this,
792 StatsReport::kReceive);
793 ExtractStatsFromList(voice_info.senders, transport_id, this,
794 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000795
796 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797}
798
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000799void StatsCollector::ExtractVideoInfo(
800 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700801 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000802
deadbeefab9b2d12015-10-14 11:33:11 -0700803 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000805
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700807 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 LOG(LS_ERROR) << "Failed to get video channel stats.";
809 return;
810 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000811
812 // TODO(tommi): The above code should run on the worker thread and post the
813 // results back to the signaling thread, where we can add data to the reports.
814 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
815
deadbeefab9b2d12015-10-14 11:33:11 -0700816 StatsReport::Id transport_id(GetTransportIdFromProxy(
817 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000818 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700820 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 return;
822 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000823 ExtractStatsFromList(video_info.receivers, transport_id, this,
824 StatsReport::kReceive);
825 ExtractStatsFromList(video_info.senders, transport_id, this,
826 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 if (video_info.bw_estimations.size() != 1) {
828 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
829 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000830 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
831 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000833 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 }
835}
836
nissefcc640f2016-04-01 01:10:42 -0700837void StatsCollector::ExtractSenderInfo() {
838 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
839
840 for (const auto& sender : pc_->GetSenders()) {
841 // TODO(nisse): SSRC == 0 currently means none. Delete check when
842 // that is fixed.
843 if (!sender->ssrc()) {
844 continue;
845 }
846 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
847 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
848 continue;
849 }
850 // Safe, because kind() == kVideoKind implies a subclass of
851 // VideoTrackInterface; see mediastreaminterface.h.
852 VideoTrackSourceInterface* source =
853 static_cast<VideoTrackInterface*>(track.get())->GetSource();
854
855 VideoTrackSourceInterface::Stats stats;
856 if (!source->GetStats(&stats)) {
857 continue;
858 }
859 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
860 StatsReport::kStatsReportTypeSsrc,
861 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
862 StatsReport* report = reports_.FindOrAddNew(stats_id);
863 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
864 stats.input_width);
865 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
866 stats.input_height);
867 }
868}
869
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000870void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700871 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000872
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000873 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
874
deadbeefab9b2d12015-10-14 11:33:11 -0700875 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000876 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000877 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000878 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000879 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000880 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700881 // Filter out the initial id (-1).
882 if (dc->id() >= 0) {
883 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
884 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000885 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
886 report->AddString(StatsReport::kStatsValueNameState,
887 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000888 }
889}
890
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000891StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000892 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000893 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700894 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700895 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
896 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000897 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000898}
899
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000900void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700901 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000902 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000903 for (const auto& it : local_audio_tracks_) {
904 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200905 uint32_t ssrc = it.second;
906 StatsReport* report =
907 GetReport(StatsReport::kStatsReportTypeSsrc,
908 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000909 if (report == NULL) {
910 // This can happen if a local audio track is added to a stream on the
911 // fly and the report has not been set up yet. Do nothing in this case.
912 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
913 continue;
914 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000915
916 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000917 const StatsReport::Value* v =
918 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000919 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000920 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000921
jbauchbe24c942015-06-22 15:06:43 -0700922 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000923 UpdateReportFromAudioTrack(track, report);
924 }
925}
926
927void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
928 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700929 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700930 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000931
andrew2fe1cb02015-11-27 17:27:35 -0800932 // Don't overwrite report values if they're not available.
933 int signal_level;
934 if (track->GetSignalLevel(&signal_level)) {
935 RTC_DCHECK_GE(signal_level, 0);
936 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
937 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000938
andrew2fe1cb02015-11-27 17:27:35 -0800939 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000940
andrew2fe1cb02015-11-27 17:27:35 -0800941 if (audio_processor.get()) {
942 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000943 audio_processor->GetStats(&stats);
944
andrew2fe1cb02015-11-27 17:27:35 -0800945 SetAudioProcessingStats(
946 report, stats.typing_noise_detected, stats.echo_return_loss,
947 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700948 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800949 stats.residual_echo_likelihood,
950 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +0200951
952 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
953 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -0800954 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000955}
956
Peter Boström0c4e06b2015-10-07 12:23:21 +0200957bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
958 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000959 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700960 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000961 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700962 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000963 LOG(LS_WARNING) << "The SSRC " << ssrc
964 << " is not associated with a sending track";
965 return false;
966 }
967 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700968 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700969 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000970 LOG(LS_WARNING) << "The SSRC " << ssrc
971 << " is not associated with a receiving track";
972 return false;
973 }
974 }
975
976 return true;
977}
978
jbauchbe24c942015-06-22 15:06:43 -0700979void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700980 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700981
982 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
983
984 for (const auto& entry : track_ids_) {
985 StatsReport* report = entry.second;
986 report->set_timestamp(stats_gathering_started_);
987 }
jbauchbe24c942015-06-22 15:06:43 -0700988}
989
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000990void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000991 stats_gathering_started_ = 0;
992}
993
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994} // namespace webrtc