blob: e91f873824a245ee7284f5a6e154908be627e258 [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
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/base64.h"
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +000018#include "webrtc/base/checks.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010019#include "webrtc/pc/channel.h"
ossu7bb87ee2017-01-23 04:56:25 -080020#include "webrtc/pc/peerconnection.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 }
sakalcc452e12017-02-09 04:53:45 -0800224 if (info.qp_sum)
225 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
226
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000227 const IntForAdd ints[] = {
228 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
229 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
230 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
231 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
232 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
233 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
234 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
235 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
236 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
237 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
238 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
239 info.min_playout_delay_ms },
240 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
241 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
242 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
243 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
244 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
245 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700246 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000247 };
248
249 for (const auto& i : ints)
250 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800251 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252}
253
254void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000255 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
Peter Boströmb7d9a972015-12-18 16:01:11 +0100257 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
258 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000259 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
260 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000261 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
262 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700263 if (info.qp_sum)
264 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000265
266 const IntForAdd ints[] = {
267 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
268 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000269 { StatsReport::kStatsValueNameEncodeUsagePercent,
270 info.encode_usage_percent },
271 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000272 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
273 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
274 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000275 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
276 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
277 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
278 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
279 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700280 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000281 };
282
283 for (const auto& i : ints)
284 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800285 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286}
287
288void ExtractStats(const cricket::BandwidthEstimationInfo& info,
289 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000290 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700292 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000294 report->set_timestamp(stats_gathering_started);
295 const IntForAdd ints[] = {
296 { StatsReport::kStatsValueNameAvailableSendBandwidth,
297 info.available_send_bandwidth },
298 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
299 info.available_recv_bandwidth },
300 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
301 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
302 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
303 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
304 };
305 for (const auto& i : ints)
306 report->AddInt(i.name, i.value);
307 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308}
309
wu@webrtc.org97077a32013-10-25 21:18:33 +0000310void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
311 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000312 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000313 // TODO(hta): Extract some stats here.
314}
315
316void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
317 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000318 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000319 // TODO(hta): Extract some stats here.
320}
321
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000323// In order to use the template, the functions that are called from it,
324// ExtractStats and ExtractRemoteStats, must be defined and overloaded
325// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326template<typename T>
327void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000328 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000329 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000330 StatsReport::Direction direction) {
331 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200332 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000333 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000334 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000335 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
336 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000337 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000338 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000339
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000340 if (!d.remote_stats.empty()) {
341 report = collector->PrepareReport(false, ssrc, transport_id, direction);
342 if (report)
343 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000344 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000346}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347
348} // namespace
349
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000350const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
351 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
352 return STATSREPORT_LOCAL_PORT_TYPE;
353 }
354 if (candidate_type == cricket::STUN_PORT_TYPE) {
355 return STATSREPORT_STUN_PORT_TYPE;
356 }
357 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
358 return STATSREPORT_PRFLX_PORT_TYPE;
359 }
360 if (candidate_type == cricket::RELAY_PORT_TYPE) {
361 return STATSREPORT_RELAY_PORT_TYPE;
362 }
nisseeb4ca4e2017-01-12 02:24:27 -0800363 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000364 return "unknown";
365}
366
367const char* AdapterTypeToStatsType(rtc::AdapterType type) {
368 switch (type) {
369 case rtc::ADAPTER_TYPE_UNKNOWN:
370 return "unknown";
371 case rtc::ADAPTER_TYPE_ETHERNET:
372 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
373 case rtc::ADAPTER_TYPE_WIFI:
374 return STATSREPORT_ADAPTER_TYPE_WIFI;
375 case rtc::ADAPTER_TYPE_CELLULAR:
376 return STATSREPORT_ADAPTER_TYPE_WWAN;
377 case rtc::ADAPTER_TYPE_VPN:
378 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000379 case rtc::ADAPTER_TYPE_LOOPBACK:
380 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000381 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800382 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000383 return "";
384 }
385}
386
deadbeefab9b2d12015-10-14 11:33:11 -0700387StatsCollector::StatsCollector(PeerConnection* pc)
388 : pc_(pc), stats_gathering_started_(0) {
389 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000390}
391
392StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700393 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394}
395
nissecdf37a92016-09-13 23:41:47 -0700396// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000397double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700398 return rtc::TimeUTCMicros() /
399 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000400}
401
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402// Adds a MediaStream with tracks that can be used as a |selector| in a call
403// to GetStats.
404void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700405 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700406 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407
408 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700409 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700411 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412}
413
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000414void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200415 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700416 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700417 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700418#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000419 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700420 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000421#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000422
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000423 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000424
425 // Create the kStatsReportTypeTrack report for the new track if there is no
426 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000427 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
428 audio_track->id()));
429 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000430 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000431 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000432 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000433 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000434}
435
436void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200437 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700438 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000439 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
440 local_audio_tracks_.end(),
441 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
442 return track.first == audio_track && track.second == ssrc;
443 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000444}
445
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000446void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700448 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700449 RTC_DCHECK(reports != NULL);
450 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000452 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
453
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000455 reports->reserve(reports_.size());
456 for (auto* r : reports_)
457 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000458 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 }
460
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000461 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700462 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000463 if (report)
464 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000466 report = reports_.Find(StatsReport::NewTypedId(
467 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000469 if (!report)
470 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000472 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473
474 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000475 for (const auto* r : reports_) {
476 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000478
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000479 const StatsReport::Value* v =
480 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000481 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000482 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484}
485
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000486void
487StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700488 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 double time_now = GetTimeNow();
490 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
491 // ms apart will be ignored.
492 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000493 if (stats_gathering_started_ != 0 &&
494 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 return;
496 }
497 stats_gathering_started_ = time_now;
498
solenberg03d6d572016-03-01 12:42:03 -0800499 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
500 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700501 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000502 // TODO(tommi): All of these hop over to the worker thread to fetch
503 // information. We could use an AsyncInvoker to run all of these and post
504 // the information back to the signaling thread where we can create and
505 // update stats reports. That would also clean up the threading story a bit
506 // since we'd be creating/updating the stats report objects consistently on
507 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 ExtractSessionInfo();
509 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000510 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700511 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000512 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700513 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 }
515}
516
deadbeefab9b2d12015-10-14 11:33:11 -0700517StatsReport* StatsCollector::PrepareReport(
518 bool local,
519 uint32_t ssrc,
520 const StatsReport::Id& transport_id,
521 StatsReport::Direction direction) {
522 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000523 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200524 local ? StatsReport::kStatsReportTypeSsrc
525 : StatsReport::kStatsReportTypeRemoteSsrc,
526 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000527 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528
xians@webrtc.org01bda202014-07-09 07:38:38 +0000529 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000531 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000532 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000533 // The ssrc is not used by any track or existing report, return NULL
534 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000536 }
537
538 // The ssrc is not used by any existing track. Keeps the old track id
539 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000540 const StatsReport::Value* v =
541 report->FindValue(StatsReport::kStatsValueNameTrackId);
542 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000543 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 }
545
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000546 if (!report)
547 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000549 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000550 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000552 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000553 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000555 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 return report;
557}
558
zhihuange9e94c32016-11-04 11:38:15 -0700559bool StatsCollector::IsValidTrack(const std::string& track_id) {
560 return reports_.Find(StatsReport::NewTypedId(
561 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
562}
563
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000564StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000565 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700566 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700567 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000568
hbose29352b2016-08-25 03:52:38 -0700569 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
570 StatsReport* first_report = nullptr;
571 StatsReport* prev_report = nullptr;
572 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
573 stats = stats->issuer.get()) {
574 StatsReport::Id id(StatsReport::NewTypedId(
575 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
576
577 StatsReport* report = reports_.ReplaceOrAddNew(id);
578 report->set_timestamp(stats_gathering_started_);
579 report->AddString(StatsReport::kStatsValueNameFingerprint,
580 stats->fingerprint);
581 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
582 stats->fingerprint_algorithm);
583 report->AddString(StatsReport::kStatsValueNameDer,
584 stats->base64_certificate);
585 if (!first_report)
586 first_report = report;
587 else
588 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
589 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000590 }
hbose29352b2016-08-25 03:52:38 -0700591 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000592}
593
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000594StatsReport* StatsCollector::AddConnectionInfoReport(
595 const std::string& content_name, int component, int connection_id,
596 const StatsReport::Id& channel_report_id,
597 const cricket::ConnectionInfo& info) {
598 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
599 connection_id));
600 StatsReport* report = reports_.ReplaceOrAddNew(id);
601 report->set_timestamp(stats_gathering_started_);
602
603 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700604 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
605 {StatsReport::kStatsValueNameReceiving, info.receiving},
606 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000607 };
608 for (const auto& b : bools)
609 report->AddBoolean(b.name, b.value);
610
611 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
612 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
613 AddCandidateReport(info.local_candidate, true)->id());
614 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
615 AddCandidateReport(info.remote_candidate, false)->id());
616
617 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700618 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
619 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
620 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
621 {StatsReport::kStatsValueNameRtt, info.rtt},
622 {StatsReport::kStatsValueNameSendPacketsDiscarded,
623 info.sent_discarded_packets},
624 {StatsReport::kStatsValueNameSentPingRequestsTotal,
625 info.sent_ping_requests_total},
626 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
627 info.sent_ping_requests_before_first_response},
628 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
629 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
630 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000631 };
632 for (const auto& i : int64s)
633 report->AddInt64(i.name, i.value);
634
635 report->AddString(StatsReport::kStatsValueNameLocalAddress,
636 info.local_candidate.address().ToString());
637 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
638 info.local_candidate.type());
639 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
640 info.remote_candidate.address().ToString());
641 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
642 info.remote_candidate.type());
643 report->AddString(StatsReport::kStatsValueNameTransportType,
644 info.local_candidate.protocol());
645
646 return report;
647}
648
649StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000650 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000651 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000652 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
653 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000654 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000655 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000656 report->set_timestamp(stats_gathering_started_);
657 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000658 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
659 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000660 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000661 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
662 candidate.address().ipaddr().ToString());
663 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
664 candidate.address().PortAsString());
665 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
666 candidate.priority());
667 report->AddString(StatsReport::kStatsValueNameCandidateType,
668 IceCandidateTypeToStatsType(candidate.type()));
669 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
670 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000671 }
672
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000673 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000674}
675
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700677 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000678
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000680 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700681 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000682 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000683 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000684 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700685 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686
hbosdf6075a2016-12-19 04:58:02 -0800687 std::unique_ptr<SessionStats> stats = pc_->session()->GetStats_s();
688 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000689 return;
690 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000692 // Store the proxy map away for use in SSRC reporting.
693 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
694 // signaling thread after fetching them on the worker thread, then just use
695 // the proxy map directly from the session stats.
696 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
697 // data.
hbosdf6075a2016-12-19 04:58:02 -0800698 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000699
hbosdf6075a2016-12-19 04:58:02 -0800700 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000701 // Attempt to get a copy of the certificates from the transport and
702 // expose them in stats reports. All channels in a transport share the
703 // same local and remote certificates.
704 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000705 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200706 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700707 if (pc_->session()->GetLocalCertificate(
708 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200709 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000710 if (r)
711 local_cert_report_id = r->id();
712 }
713
jbauch555604a2016-04-26 03:13:22 -0700714 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700715 pc_->session()->GetRemoteSSLCertificate(
716 transport_iter.second.transport_name);
717 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000718 StatsReport* r = AddCertificateReports(cert.get());
719 if (r)
720 remote_cert_report_id = r->id();
721 }
722
723 for (const auto& channel_iter : transport_iter.second.channel_stats) {
724 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700725 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000726 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
727 channel_report->set_timestamp(stats_gathering_started_);
728 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
729 channel_iter.component);
730 if (local_cert_report_id.get()) {
731 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
732 local_cert_report_id);
733 }
734 if (remote_cert_report_id.get()) {
735 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
736 remote_cert_report_id);
737 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800738 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
739 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
740 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
741 channel_report->AddString(
742 StatsReport::kStatsValueNameSrtpCipher,
743 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000744 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800745 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
746 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
747 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
748 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700749 channel_report->AddString(
750 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800751 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000752 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000753
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000754 int connection_id = 0;
755 for (const cricket::ConnectionInfo& info :
756 channel_iter.connection_infos) {
757 StatsReport* connection_report = AddConnectionInfoReport(
758 transport_iter.first, channel_iter.component, connection_id++,
759 channel_report->id(), info);
760 if (info.best_connection) {
761 channel_report->AddId(
762 StatsReport::kStatsValueNameSelectedCandidatePairId,
763 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765 }
766 }
767 }
768}
769
770void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700771 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000772
deadbeefab9b2d12015-10-14 11:33:11 -0700773 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 return;
775 }
776 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700777 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 LOG(LS_ERROR) << "Failed to get voice channel stats.";
779 return;
780 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000781
782 // TODO(tommi): The above code should run on the worker thread and post the
783 // results back to the signaling thread, where we can add data to the reports.
784 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
785
deadbeefab9b2d12015-10-14 11:33:11 -0700786 StatsReport::Id transport_id(GetTransportIdFromProxy(
787 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000788 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700790 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 return;
792 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000793
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000794 ExtractStatsFromList(voice_info.receivers, transport_id, this,
795 StatsReport::kReceive);
796 ExtractStatsFromList(voice_info.senders, transport_id, this,
797 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000798
799 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800}
801
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000802void StatsCollector::ExtractVideoInfo(
803 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700804 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000805
deadbeefab9b2d12015-10-14 11:33:11 -0700806 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000808
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700810 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 LOG(LS_ERROR) << "Failed to get video channel stats.";
812 return;
813 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000814
815 // TODO(tommi): The above code should run on the worker thread and post the
816 // results back to the signaling thread, where we can add data to the reports.
817 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
818
deadbeefab9b2d12015-10-14 11:33:11 -0700819 StatsReport::Id transport_id(GetTransportIdFromProxy(
820 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000821 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700823 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 return;
825 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000826 ExtractStatsFromList(video_info.receivers, transport_id, this,
827 StatsReport::kReceive);
828 ExtractStatsFromList(video_info.senders, transport_id, this,
829 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 if (video_info.bw_estimations.size() != 1) {
831 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
832 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000833 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
834 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000836 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
838}
839
nissefcc640f2016-04-01 01:10:42 -0700840void StatsCollector::ExtractSenderInfo() {
841 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
842
843 for (const auto& sender : pc_->GetSenders()) {
844 // TODO(nisse): SSRC == 0 currently means none. Delete check when
845 // that is fixed.
846 if (!sender->ssrc()) {
847 continue;
848 }
849 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
850 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
851 continue;
852 }
853 // Safe, because kind() == kVideoKind implies a subclass of
854 // VideoTrackInterface; see mediastreaminterface.h.
855 VideoTrackSourceInterface* source =
856 static_cast<VideoTrackInterface*>(track.get())->GetSource();
857
858 VideoTrackSourceInterface::Stats stats;
859 if (!source->GetStats(&stats)) {
860 continue;
861 }
862 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
863 StatsReport::kStatsReportTypeSsrc,
864 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
865 StatsReport* report = reports_.FindOrAddNew(stats_id);
866 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
867 stats.input_width);
868 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
869 stats.input_height);
870 }
871}
872
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000873void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700874 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000875
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000876 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
877
deadbeefab9b2d12015-10-14 11:33:11 -0700878 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000879 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000880 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000881 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000882 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000883 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700884 // Filter out the initial id (-1).
885 if (dc->id() >= 0) {
886 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
887 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000888 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
889 report->AddString(StatsReport::kStatsValueNameState,
890 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000891 }
892}
893
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000894StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000895 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000896 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700897 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700898 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
899 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000900 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000901}
902
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000903void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700904 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000905 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000906 for (const auto& it : local_audio_tracks_) {
907 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200908 uint32_t ssrc = it.second;
909 StatsReport* report =
910 GetReport(StatsReport::kStatsReportTypeSsrc,
911 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000912 if (report == NULL) {
913 // This can happen if a local audio track is added to a stream on the
914 // fly and the report has not been set up yet. Do nothing in this case.
915 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
916 continue;
917 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000918
919 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000920 const StatsReport::Value* v =
921 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000922 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000923 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000924
jbauchbe24c942015-06-22 15:06:43 -0700925 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000926 UpdateReportFromAudioTrack(track, report);
927 }
928}
929
930void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
931 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700932 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700933 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000934
andrew2fe1cb02015-11-27 17:27:35 -0800935 // Don't overwrite report values if they're not available.
936 int signal_level;
937 if (track->GetSignalLevel(&signal_level)) {
938 RTC_DCHECK_GE(signal_level, 0);
939 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
940 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000941
andrew2fe1cb02015-11-27 17:27:35 -0800942 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000943
andrew2fe1cb02015-11-27 17:27:35 -0800944 if (audio_processor.get()) {
945 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000946 audio_processor->GetStats(&stats);
947
andrew2fe1cb02015-11-27 17:27:35 -0800948 SetAudioProcessingStats(
949 report, stats.typing_noise_detected, stats.echo_return_loss,
950 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700951 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800952 stats.residual_echo_likelihood,
953 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +0200954
955 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
956 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -0800957 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000958}
959
Peter Boström0c4e06b2015-10-07 12:23:21 +0200960bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
961 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000962 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700963 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000964 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700965 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000966 LOG(LS_WARNING) << "The SSRC " << ssrc
967 << " is not associated with a sending track";
968 return false;
969 }
970 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700971 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700972 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000973 LOG(LS_WARNING) << "The SSRC " << ssrc
974 << " is not associated with a receiving track";
975 return false;
976 }
977 }
978
979 return true;
980}
981
jbauchbe24c942015-06-22 15:06:43 -0700982void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700983 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700984
985 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
986
987 for (const auto& entry : track_ids_) {
988 StatsReport* report = entry.second;
989 report->set_timestamp(stats_gathering_started_);
990 }
jbauchbe24c942015-06-22 15:06:43 -0700991}
992
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000993void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000994 stats_gathering_started_ = 0;
995}
996
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997} // namespace webrtc