blob: 8347424b2f103e91944d33a28cd233fda3fd87f9 [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,
107 float residual_echo_likelihood) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000108 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
109 typing_noise_detected);
zhihuang6ba3b192016-05-13 11:46:35 -0700110 if (aec_quality_min >= 0.0f) {
111 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
112 aec_quality_min);
113 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000114 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000115 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
116 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
117 };
zhihuang6ba3b192016-05-13 11:46:35 -0700118 for (const auto& i : ints) {
119 if (i.value >= 0) {
120 report->AddInt(i.name, i.value);
121 }
122 }
123 // These can take on valid negative values.
124 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss);
125 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
126 echo_return_loss_enhancement);
ivoc8c63a822016-10-21 04:10:03 -0700127 if (residual_echo_likelihood >= 0.0f) {
128 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
129 residual_echo_likelihood);
130 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000131}
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700134 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000135 const FloatForAdd floats[] = {
136 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
137 { StatsReport::kStatsValueNameSecondaryDecodedRate,
138 info.secondary_decoded_rate },
139 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200140 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
141 { StatsReport::kStatsValueNamePreemptiveExpandRate,
142 info.preemptive_expand_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000143 };
144
145 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000146 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
147 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
148 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
149 { StatsReport::kStatsValueNameDecodingCTSG,
150 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700151 { StatsReport::kStatsValueNameDecodingMutedOutput,
152 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000153 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
154 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
155 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
156 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
157 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
158 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
159 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
160 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
161 info.jitter_buffer_preferred_ms },
162 };
163
164 for (const auto& f : floats)
165 report->AddFloat(f.name, f.value);
166
167 for (const auto& i : ints)
168 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700169 if (info.audio_level >= 0) {
170 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
171 info.audio_level);
172 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000173
174 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700176 if (info.capture_start_ntp_time_ms >= 0) {
177 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
178 info.capture_start_ntp_time_ms);
179 }
fippobec70ab2016-01-28 01:27:15 -0800180 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181}
182
183void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184 ExtractCommonSendProperties(info, report);
185
andrew2fe1cb02015-11-27 17:27:35 -0800186 SetAudioProcessingStats(
187 report, info.typing_noise_detected, info.echo_return_loss,
188 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700189 info.aec_quality_min, info.echo_delay_std_ms,
190 info.residual_echo_likelihood);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000191
andrew2fe1cb02015-11-27 17:27:35 -0800192 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000193 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800194 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000195 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
196 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
197 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
198 };
199
zhihuang6ba3b192016-05-13 11:46:35 -0700200 for (const auto& i : ints) {
201 if (i.value >= 0) {
202 report->AddInt(i.name, i.value);
203 }
204 }
fippobec70ab2016-01-28 01:27:15 -0800205 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206}
207
208void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700209 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100210 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
211 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000212 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700214 if (info.capture_start_ntp_time_ms >= 0) {
215 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
216 info.capture_start_ntp_time_ms);
217 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000218 const IntForAdd ints[] = {
219 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
220 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
221 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
222 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
223 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
224 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
225 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
226 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
227 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
228 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
229 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
230 info.min_playout_delay_ms },
231 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
232 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
233 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
234 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
235 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
236 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700237 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000238 };
239
240 for (const auto& i : ints)
241 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800242 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243}
244
245void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000246 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247
Peter Boströmb7d9a972015-12-18 16:01:11 +0100248 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
249 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000250 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
251 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000252 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
253 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700254 if (info.qp_sum)
255 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000256
257 const IntForAdd ints[] = {
258 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
259 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000260 { StatsReport::kStatsValueNameEncodeUsagePercent,
261 info.encode_usage_percent },
262 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000263 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
264 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
265 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000266 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
267 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
268 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
269 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
270 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700271 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000272 };
273
274 for (const auto& i : ints)
275 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800276 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277}
278
279void ExtractStats(const cricket::BandwidthEstimationInfo& info,
280 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000281 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700283 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000285 report->set_timestamp(stats_gathering_started);
286 const IntForAdd ints[] = {
287 { StatsReport::kStatsValueNameAvailableSendBandwidth,
288 info.available_send_bandwidth },
289 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
290 info.available_recv_bandwidth },
291 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
292 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
293 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
294 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
295 };
296 for (const auto& i : ints)
297 report->AddInt(i.name, i.value);
298 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299}
300
wu@webrtc.org97077a32013-10-25 21:18:33 +0000301void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
302 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000303 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000304 // TODO(hta): Extract some stats here.
305}
306
307void ExtractRemoteStats(const cricket::MediaReceiverInfo& 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000314// In order to use the template, the functions that are called from it,
315// ExtractStats and ExtractRemoteStats, must be defined and overloaded
316// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317template<typename T>
318void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000319 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000320 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000321 StatsReport::Direction direction) {
322 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200323 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000324 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000325 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000326 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
327 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000328 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000329 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000330
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000331 if (!d.remote_stats.empty()) {
332 report = collector->PrepareReport(false, ssrc, transport_id, direction);
333 if (report)
334 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000335 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000337}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
339} // namespace
340
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000341const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
342 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
343 return STATSREPORT_LOCAL_PORT_TYPE;
344 }
345 if (candidate_type == cricket::STUN_PORT_TYPE) {
346 return STATSREPORT_STUN_PORT_TYPE;
347 }
348 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
349 return STATSREPORT_PRFLX_PORT_TYPE;
350 }
351 if (candidate_type == cricket::RELAY_PORT_TYPE) {
352 return STATSREPORT_RELAY_PORT_TYPE;
353 }
nisseeb4ca4e2017-01-12 02:24:27 -0800354 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000355 return "unknown";
356}
357
358const char* AdapterTypeToStatsType(rtc::AdapterType type) {
359 switch (type) {
360 case rtc::ADAPTER_TYPE_UNKNOWN:
361 return "unknown";
362 case rtc::ADAPTER_TYPE_ETHERNET:
363 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
364 case rtc::ADAPTER_TYPE_WIFI:
365 return STATSREPORT_ADAPTER_TYPE_WIFI;
366 case rtc::ADAPTER_TYPE_CELLULAR:
367 return STATSREPORT_ADAPTER_TYPE_WWAN;
368 case rtc::ADAPTER_TYPE_VPN:
369 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000370 case rtc::ADAPTER_TYPE_LOOPBACK:
371 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000372 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800373 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000374 return "";
375 }
376}
377
deadbeefab9b2d12015-10-14 11:33:11 -0700378StatsCollector::StatsCollector(PeerConnection* pc)
379 : pc_(pc), stats_gathering_started_(0) {
380 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000381}
382
383StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700384 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385}
386
nissecdf37a92016-09-13 23:41:47 -0700387// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000388double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700389 return rtc::TimeUTCMicros() /
390 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000391}
392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393// Adds a MediaStream with tracks that can be used as a |selector| in a call
394// to GetStats.
395void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700396 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700397 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398
399 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700400 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700402 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403}
404
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000405void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200406 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700407 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700408 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700409#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000410 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700411 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000412#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000413
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000414 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000415
416 // Create the kStatsReportTypeTrack report for the new track if there is no
417 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000418 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
419 audio_track->id()));
420 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000421 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000422 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000423 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000424 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000425}
426
427void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200428 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700429 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000430 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
431 local_audio_tracks_.end(),
432 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
433 return track.first == audio_track && track.second == ssrc;
434 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000435}
436
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000437void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700439 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700440 RTC_DCHECK(reports != NULL);
441 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000443 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
444
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000446 reports->reserve(reports_.size());
447 for (auto* r : reports_)
448 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000449 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 }
451
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000452 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700453 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000454 if (report)
455 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000457 report = reports_.Find(StatsReport::NewTypedId(
458 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000460 if (!report)
461 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000463 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000466 for (const auto* r : reports_) {
467 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000469
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000470 const StatsReport::Value* v =
471 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000472 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000473 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475}
476
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000477void
478StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700479 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 double time_now = GetTimeNow();
481 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
482 // ms apart will be ignored.
483 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000484 if (stats_gathering_started_ != 0 &&
485 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 return;
487 }
488 stats_gathering_started_ = time_now;
489
solenberg03d6d572016-03-01 12:42:03 -0800490 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
491 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700492 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000493 // TODO(tommi): All of these hop over to the worker thread to fetch
494 // information. We could use an AsyncInvoker to run all of these and post
495 // the information back to the signaling thread where we can create and
496 // update stats reports. That would also clean up the threading story a bit
497 // since we'd be creating/updating the stats report objects consistently on
498 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 ExtractSessionInfo();
500 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000501 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700502 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000503 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700504 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 }
506}
507
deadbeefab9b2d12015-10-14 11:33:11 -0700508StatsReport* StatsCollector::PrepareReport(
509 bool local,
510 uint32_t ssrc,
511 const StatsReport::Id& transport_id,
512 StatsReport::Direction direction) {
513 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000514 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200515 local ? StatsReport::kStatsReportTypeSsrc
516 : StatsReport::kStatsReportTypeRemoteSsrc,
517 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000518 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
xians@webrtc.org01bda202014-07-09 07:38:38 +0000520 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000522 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000523 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000524 // The ssrc is not used by any track or existing report, return NULL
525 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000527 }
528
529 // The ssrc is not used by any existing track. Keeps the old track id
530 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000531 const StatsReport::Value* v =
532 report->FindValue(StatsReport::kStatsValueNameTrackId);
533 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000534 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
536
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000537 if (!report)
538 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000540 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000541 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000543 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000544 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000546 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 return report;
548}
549
zhihuange9e94c32016-11-04 11:38:15 -0700550bool StatsCollector::IsValidTrack(const std::string& track_id) {
551 return reports_.Find(StatsReport::NewTypedId(
552 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
553}
554
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000555StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000556 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700557 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700558 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000559
hbose29352b2016-08-25 03:52:38 -0700560 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
561 StatsReport* first_report = nullptr;
562 StatsReport* prev_report = nullptr;
563 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
564 stats = stats->issuer.get()) {
565 StatsReport::Id id(StatsReport::NewTypedId(
566 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
567
568 StatsReport* report = reports_.ReplaceOrAddNew(id);
569 report->set_timestamp(stats_gathering_started_);
570 report->AddString(StatsReport::kStatsValueNameFingerprint,
571 stats->fingerprint);
572 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
573 stats->fingerprint_algorithm);
574 report->AddString(StatsReport::kStatsValueNameDer,
575 stats->base64_certificate);
576 if (!first_report)
577 first_report = report;
578 else
579 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
580 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000581 }
hbose29352b2016-08-25 03:52:38 -0700582 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000583}
584
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000585StatsReport* StatsCollector::AddConnectionInfoReport(
586 const std::string& content_name, int component, int connection_id,
587 const StatsReport::Id& channel_report_id,
588 const cricket::ConnectionInfo& info) {
589 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
590 connection_id));
591 StatsReport* report = reports_.ReplaceOrAddNew(id);
592 report->set_timestamp(stats_gathering_started_);
593
594 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700595 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
596 {StatsReport::kStatsValueNameReceiving, info.receiving},
597 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000598 };
599 for (const auto& b : bools)
600 report->AddBoolean(b.name, b.value);
601
602 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
603 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
604 AddCandidateReport(info.local_candidate, true)->id());
605 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
606 AddCandidateReport(info.remote_candidate, false)->id());
607
608 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700609 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
610 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
611 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
612 {StatsReport::kStatsValueNameRtt, info.rtt},
613 {StatsReport::kStatsValueNameSendPacketsDiscarded,
614 info.sent_discarded_packets},
615 {StatsReport::kStatsValueNameSentPingRequestsTotal,
616 info.sent_ping_requests_total},
617 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
618 info.sent_ping_requests_before_first_response},
619 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
620 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
621 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000622 };
623 for (const auto& i : int64s)
624 report->AddInt64(i.name, i.value);
625
626 report->AddString(StatsReport::kStatsValueNameLocalAddress,
627 info.local_candidate.address().ToString());
628 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
629 info.local_candidate.type());
630 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
631 info.remote_candidate.address().ToString());
632 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
633 info.remote_candidate.type());
634 report->AddString(StatsReport::kStatsValueNameTransportType,
635 info.local_candidate.protocol());
636
637 return report;
638}
639
640StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000641 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000642 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000643 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
644 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000645 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000646 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000647 report->set_timestamp(stats_gathering_started_);
648 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000649 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
650 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000651 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000652 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
653 candidate.address().ipaddr().ToString());
654 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
655 candidate.address().PortAsString());
656 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
657 candidate.priority());
658 report->AddString(StatsReport::kStatsValueNameCandidateType,
659 IceCandidateTypeToStatsType(candidate.type()));
660 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
661 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000662 }
663
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000664 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000665}
666
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700668 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000669
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000671 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700672 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000673 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000674 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000675 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700676 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677
hbosdf6075a2016-12-19 04:58:02 -0800678 std::unique_ptr<SessionStats> stats = pc_->session()->GetStats_s();
679 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000680 return;
681 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000683 // Store the proxy map away for use in SSRC reporting.
684 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
685 // signaling thread after fetching them on the worker thread, then just use
686 // the proxy map directly from the session stats.
687 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
688 // data.
hbosdf6075a2016-12-19 04:58:02 -0800689 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000690
hbosdf6075a2016-12-19 04:58:02 -0800691 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000692 // Attempt to get a copy of the certificates from the transport and
693 // expose them in stats reports. All channels in a transport share the
694 // same local and remote certificates.
695 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000696 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200697 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700698 if (pc_->session()->GetLocalCertificate(
699 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200700 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000701 if (r)
702 local_cert_report_id = r->id();
703 }
704
jbauch555604a2016-04-26 03:13:22 -0700705 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700706 pc_->session()->GetRemoteSSLCertificate(
707 transport_iter.second.transport_name);
708 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000709 StatsReport* r = AddCertificateReports(cert.get());
710 if (r)
711 remote_cert_report_id = r->id();
712 }
713
714 for (const auto& channel_iter : transport_iter.second.channel_stats) {
715 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700716 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000717 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
718 channel_report->set_timestamp(stats_gathering_started_);
719 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
720 channel_iter.component);
721 if (local_cert_report_id.get()) {
722 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
723 local_cert_report_id);
724 }
725 if (remote_cert_report_id.get()) {
726 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
727 remote_cert_report_id);
728 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800729 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
730 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
731 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
732 channel_report->AddString(
733 StatsReport::kStatsValueNameSrtpCipher,
734 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000735 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800736 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
737 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
738 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
739 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700740 channel_report->AddString(
741 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800742 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000743 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000744
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000745 int connection_id = 0;
746 for (const cricket::ConnectionInfo& info :
747 channel_iter.connection_infos) {
748 StatsReport* connection_report = AddConnectionInfoReport(
749 transport_iter.first, channel_iter.component, connection_id++,
750 channel_report->id(), info);
751 if (info.best_connection) {
752 channel_report->AddId(
753 StatsReport::kStatsValueNameSelectedCandidatePairId,
754 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 }
756 }
757 }
758 }
759}
760
761void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700762 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000763
deadbeefab9b2d12015-10-14 11:33:11 -0700764 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 return;
766 }
767 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700768 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 LOG(LS_ERROR) << "Failed to get voice channel stats.";
770 return;
771 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000772
773 // TODO(tommi): The above code should run on the worker thread and post the
774 // results back to the signaling thread, where we can add data to the reports.
775 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
776
deadbeefab9b2d12015-10-14 11:33:11 -0700777 StatsReport::Id transport_id(GetTransportIdFromProxy(
778 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000779 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700781 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 return;
783 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000784
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000785 ExtractStatsFromList(voice_info.receivers, transport_id, this,
786 StatsReport::kReceive);
787 ExtractStatsFromList(voice_info.senders, transport_id, this,
788 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000789
790 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791}
792
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000793void StatsCollector::ExtractVideoInfo(
794 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700795 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000796
deadbeefab9b2d12015-10-14 11:33:11 -0700797 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000799
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700801 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 LOG(LS_ERROR) << "Failed to get video channel stats.";
803 return;
804 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000805
806 // TODO(tommi): The above code should run on the worker thread and post the
807 // results back to the signaling thread, where we can add data to the reports.
808 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
809
deadbeefab9b2d12015-10-14 11:33:11 -0700810 StatsReport::Id transport_id(GetTransportIdFromProxy(
811 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000812 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700814 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 return;
816 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000817 ExtractStatsFromList(video_info.receivers, transport_id, this,
818 StatsReport::kReceive);
819 ExtractStatsFromList(video_info.senders, transport_id, this,
820 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 if (video_info.bw_estimations.size() != 1) {
822 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
823 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000824 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
825 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000827 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
829}
830
nissefcc640f2016-04-01 01:10:42 -0700831void StatsCollector::ExtractSenderInfo() {
832 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
833
834 for (const auto& sender : pc_->GetSenders()) {
835 // TODO(nisse): SSRC == 0 currently means none. Delete check when
836 // that is fixed.
837 if (!sender->ssrc()) {
838 continue;
839 }
840 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
841 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
842 continue;
843 }
844 // Safe, because kind() == kVideoKind implies a subclass of
845 // VideoTrackInterface; see mediastreaminterface.h.
846 VideoTrackSourceInterface* source =
847 static_cast<VideoTrackInterface*>(track.get())->GetSource();
848
849 VideoTrackSourceInterface::Stats stats;
850 if (!source->GetStats(&stats)) {
851 continue;
852 }
853 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
854 StatsReport::kStatsReportTypeSsrc,
855 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
856 StatsReport* report = reports_.FindOrAddNew(stats_id);
857 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
858 stats.input_width);
859 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
860 stats.input_height);
861 }
862}
863
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000864void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700865 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000866
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000867 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
868
deadbeefab9b2d12015-10-14 11:33:11 -0700869 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000870 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000871 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000872 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000873 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000874 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700875 // Filter out the initial id (-1).
876 if (dc->id() >= 0) {
877 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
878 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000879 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
880 report->AddString(StatsReport::kStatsValueNameState,
881 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000882 }
883}
884
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000885StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000886 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000887 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700888 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700889 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
890 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000891 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000892}
893
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700895 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000897 for (const auto& it : local_audio_tracks_) {
898 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200899 uint32_t ssrc = it.second;
900 StatsReport* report =
901 GetReport(StatsReport::kStatsReportTypeSsrc,
902 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000903 if (report == NULL) {
904 // This can happen if a local audio track is added to a stream on the
905 // fly and the report has not been set up yet. Do nothing in this case.
906 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
907 continue;
908 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000909
910 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000911 const StatsReport::Value* v =
912 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000913 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000914 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000915
jbauchbe24c942015-06-22 15:06:43 -0700916 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000917 UpdateReportFromAudioTrack(track, report);
918 }
919}
920
921void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
922 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700923 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700924 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000925
andrew2fe1cb02015-11-27 17:27:35 -0800926 // Don't overwrite report values if they're not available.
927 int signal_level;
928 if (track->GetSignalLevel(&signal_level)) {
929 RTC_DCHECK_GE(signal_level, 0);
930 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
931 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000932
andrew2fe1cb02015-11-27 17:27:35 -0800933 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000934
andrew2fe1cb02015-11-27 17:27:35 -0800935 if (audio_processor.get()) {
936 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000937 audio_processor->GetStats(&stats);
938
andrew2fe1cb02015-11-27 17:27:35 -0800939 SetAudioProcessingStats(
940 report, stats.typing_noise_detected, stats.echo_return_loss,
941 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700942 stats.aec_quality_min, stats.echo_delay_std_ms,
943 stats.residual_echo_likelihood);
Minyue2a8a78c2016-04-07 16:48:15 +0200944
945 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
946 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -0800947 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000948}
949
Peter Boström0c4e06b2015-10-07 12:23:21 +0200950bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
951 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000952 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700953 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000954 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700955 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000956 LOG(LS_WARNING) << "The SSRC " << ssrc
957 << " is not associated with a sending track";
958 return false;
959 }
960 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700961 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700962 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000963 LOG(LS_WARNING) << "The SSRC " << ssrc
964 << " is not associated with a receiving track";
965 return false;
966 }
967 }
968
969 return true;
970}
971
jbauchbe24c942015-06-22 15:06:43 -0700972void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700973 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700974
975 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
976
977 for (const auto& entry : track_ids_) {
978 StatsReport* report = entry.second;
979 report->set_timestamp(stats_gathering_started_);
980 }
jbauchbe24c942015-06-22 15:06:43 -0700981}
982
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000983void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000984 stats_gathering_started_ = 0;
985}
986
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987} // namespace webrtc