blob: 7dc17da1595f7fc3ac968bdb1909d7e6fff5c37f [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);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000254 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
255 (info.adapt_reason & 0x4) > 0);
sakal87da4042016-10-31 06:53:47 -0700256 if (info.qp_sum)
257 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000258
259 const IntForAdd ints[] = {
260 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
261 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262 { StatsReport::kStatsValueNameEncodeUsagePercent,
263 info.encode_usage_percent },
264 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000265 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
266 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
267 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000268 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
269 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
270 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
271 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
272 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700273 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000274 };
275
276 for (const auto& i : ints)
277 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800278 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279}
280
281void ExtractStats(const cricket::BandwidthEstimationInfo& info,
282 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000283 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700285 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000287 report->set_timestamp(stats_gathering_started);
288 const IntForAdd ints[] = {
289 { StatsReport::kStatsValueNameAvailableSendBandwidth,
290 info.available_send_bandwidth },
291 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
292 info.available_recv_bandwidth },
293 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
294 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
295 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
296 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
297 };
298 for (const auto& i : ints)
299 report->AddInt(i.name, i.value);
300 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301}
302
wu@webrtc.org97077a32013-10-25 21:18:33 +0000303void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
304 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000305 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000306 // TODO(hta): Extract some stats here.
307}
308
309void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
310 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000311 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000312 // TODO(hta): Extract some stats here.
313}
314
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000316// In order to use the template, the functions that are called from it,
317// ExtractStats and ExtractRemoteStats, must be defined and overloaded
318// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319template<typename T>
320void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000321 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000322 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000323 StatsReport::Direction direction) {
324 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200325 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000326 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000327 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000328 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
329 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000330 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000331 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000332
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000333 if (!d.remote_stats.empty()) {
334 report = collector->PrepareReport(false, ssrc, transport_id, direction);
335 if (report)
336 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000337 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000339}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340
341} // namespace
342
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000343const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
344 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
345 return STATSREPORT_LOCAL_PORT_TYPE;
346 }
347 if (candidate_type == cricket::STUN_PORT_TYPE) {
348 return STATSREPORT_STUN_PORT_TYPE;
349 }
350 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
351 return STATSREPORT_PRFLX_PORT_TYPE;
352 }
353 if (candidate_type == cricket::RELAY_PORT_TYPE) {
354 return STATSREPORT_RELAY_PORT_TYPE;
355 }
henrikg91d6ede2015-09-17 00:24:34 -0700356 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000357 return "unknown";
358}
359
360const char* AdapterTypeToStatsType(rtc::AdapterType type) {
361 switch (type) {
362 case rtc::ADAPTER_TYPE_UNKNOWN:
363 return "unknown";
364 case rtc::ADAPTER_TYPE_ETHERNET:
365 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
366 case rtc::ADAPTER_TYPE_WIFI:
367 return STATSREPORT_ADAPTER_TYPE_WIFI;
368 case rtc::ADAPTER_TYPE_CELLULAR:
369 return STATSREPORT_ADAPTER_TYPE_WWAN;
370 case rtc::ADAPTER_TYPE_VPN:
371 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000372 case rtc::ADAPTER_TYPE_LOOPBACK:
373 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000374 default:
henrikg91d6ede2015-09-17 00:24:34 -0700375 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000376 return "";
377 }
378}
379
deadbeefab9b2d12015-10-14 11:33:11 -0700380StatsCollector::StatsCollector(PeerConnection* pc)
381 : pc_(pc), stats_gathering_started_(0) {
382 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000383}
384
385StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700386 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387}
388
nissecdf37a92016-09-13 23:41:47 -0700389// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000390double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700391 return rtc::TimeUTCMicros() /
392 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000393}
394
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395// Adds a MediaStream with tracks that can be used as a |selector| in a call
396// to GetStats.
397void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700398 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700399 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400
401 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700402 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700404 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000407void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200408 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700409 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700410 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700411#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000412 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700413 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000414#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000415
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000416 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000417
418 // Create the kStatsReportTypeTrack report for the new track if there is no
419 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000420 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
421 audio_track->id()));
422 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000423 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000424 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000425 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000426 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000427}
428
429void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200430 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700431 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000432 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
433 local_audio_tracks_.end(),
434 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
435 return track.first == audio_track && track.second == ssrc;
436 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000437}
438
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000439void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700441 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700442 RTC_DCHECK(reports != NULL);
443 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000445 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
446
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000448 reports->reserve(reports_.size());
449 for (auto* r : reports_)
450 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000451 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 }
453
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000454 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700455 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000456 if (report)
457 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000459 report = reports_.Find(StatsReport::NewTypedId(
460 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000462 if (!report)
463 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000465 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466
467 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000468 for (const auto* r : reports_) {
469 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000471
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000472 const StatsReport::Value* v =
473 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000474 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000475 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477}
478
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000479void
480StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700481 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 double time_now = GetTimeNow();
483 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
484 // ms apart will be ignored.
485 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000486 if (stats_gathering_started_ != 0 &&
487 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 return;
489 }
490 stats_gathering_started_ = time_now;
491
solenberg03d6d572016-03-01 12:42:03 -0800492 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
493 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700494 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000495 // TODO(tommi): All of these hop over to the worker thread to fetch
496 // information. We could use an AsyncInvoker to run all of these and post
497 // the information back to the signaling thread where we can create and
498 // update stats reports. That would also clean up the threading story a bit
499 // since we'd be creating/updating the stats report objects consistently on
500 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 ExtractSessionInfo();
502 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000503 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700504 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000505 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700506 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 }
508}
509
deadbeefab9b2d12015-10-14 11:33:11 -0700510StatsReport* StatsCollector::PrepareReport(
511 bool local,
512 uint32_t ssrc,
513 const StatsReport::Id& transport_id,
514 StatsReport::Direction direction) {
515 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000516 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200517 local ? StatsReport::kStatsReportTypeSsrc
518 : StatsReport::kStatsReportTypeRemoteSsrc,
519 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000520 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521
xians@webrtc.org01bda202014-07-09 07:38:38 +0000522 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000524 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000525 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000526 // The ssrc is not used by any track or existing report, return NULL
527 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000529 }
530
531 // The ssrc is not used by any existing track. Keeps the old track id
532 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000533 const StatsReport::Value* v =
534 report->FindValue(StatsReport::kStatsValueNameTrackId);
535 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000536 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
538
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000539 if (!report)
540 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000542 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000543 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000545 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000546 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000548 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 return report;
550}
551
zhihuange9e94c32016-11-04 11:38:15 -0700552bool StatsCollector::IsValidTrack(const std::string& track_id) {
553 return reports_.Find(StatsReport::NewTypedId(
554 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
555}
556
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000557StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700559 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700560 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000561
hbose29352b2016-08-25 03:52:38 -0700562 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
563 StatsReport* first_report = nullptr;
564 StatsReport* prev_report = nullptr;
565 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
566 stats = stats->issuer.get()) {
567 StatsReport::Id id(StatsReport::NewTypedId(
568 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
569
570 StatsReport* report = reports_.ReplaceOrAddNew(id);
571 report->set_timestamp(stats_gathering_started_);
572 report->AddString(StatsReport::kStatsValueNameFingerprint,
573 stats->fingerprint);
574 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
575 stats->fingerprint_algorithm);
576 report->AddString(StatsReport::kStatsValueNameDer,
577 stats->base64_certificate);
578 if (!first_report)
579 first_report = report;
580 else
581 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
582 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000583 }
hbose29352b2016-08-25 03:52:38 -0700584 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000585}
586
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000587StatsReport* StatsCollector::AddConnectionInfoReport(
588 const std::string& content_name, int component, int connection_id,
589 const StatsReport::Id& channel_report_id,
590 const cricket::ConnectionInfo& info) {
591 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
592 connection_id));
593 StatsReport* report = reports_.ReplaceOrAddNew(id);
594 report->set_timestamp(stats_gathering_started_);
595
596 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700597 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
598 {StatsReport::kStatsValueNameReceiving, info.receiving},
599 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000600 };
601 for (const auto& b : bools)
602 report->AddBoolean(b.name, b.value);
603
604 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
605 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
606 AddCandidateReport(info.local_candidate, true)->id());
607 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
608 AddCandidateReport(info.remote_candidate, false)->id());
609
610 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700611 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
612 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
613 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
614 {StatsReport::kStatsValueNameRtt, info.rtt},
615 {StatsReport::kStatsValueNameSendPacketsDiscarded,
616 info.sent_discarded_packets},
617 {StatsReport::kStatsValueNameSentPingRequestsTotal,
618 info.sent_ping_requests_total},
619 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
620 info.sent_ping_requests_before_first_response},
621 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
622 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
623 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000624 };
625 for (const auto& i : int64s)
626 report->AddInt64(i.name, i.value);
627
628 report->AddString(StatsReport::kStatsValueNameLocalAddress,
629 info.local_candidate.address().ToString());
630 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
631 info.local_candidate.type());
632 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
633 info.remote_candidate.address().ToString());
634 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
635 info.remote_candidate.type());
636 report->AddString(StatsReport::kStatsValueNameTransportType,
637 info.local_candidate.protocol());
638
639 return report;
640}
641
642StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000643 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000644 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000645 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
646 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000647 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000648 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000649 report->set_timestamp(stats_gathering_started_);
650 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000651 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
652 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000653 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000654 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
655 candidate.address().ipaddr().ToString());
656 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
657 candidate.address().PortAsString());
658 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
659 candidate.priority());
660 report->AddString(StatsReport::kStatsValueNameCandidateType,
661 IceCandidateTypeToStatsType(candidate.type()));
662 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
663 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000664 }
665
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000666 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000667}
668
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700670 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000671
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000673 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700674 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000675 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000676 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000677 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700678 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679
deadbeefd59daf82015-10-14 15:02:44 -0700680 SessionStats stats;
deadbeefab9b2d12015-10-14 11:33:11 -0700681 if (!pc_->session()->GetTransportStats(&stats)) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000682 return;
683 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000685 // Store the proxy map away for use in SSRC reporting.
686 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
687 // signaling thread after fetching them on the worker thread, then just use
688 // the proxy map directly from the session stats.
689 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
690 // data.
691 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000692
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000693 for (const auto& transport_iter : stats.transport_stats) {
694 // Attempt to get a copy of the certificates from the transport and
695 // expose them in stats reports. All channels in a transport share the
696 // same local and remote certificates.
697 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000698 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200699 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700700 if (pc_->session()->GetLocalCertificate(
701 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200702 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000703 if (r)
704 local_cert_report_id = r->id();
705 }
706
jbauch555604a2016-04-26 03:13:22 -0700707 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700708 pc_->session()->GetRemoteSSLCertificate(
709 transport_iter.second.transport_name);
710 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000711 StatsReport* r = AddCertificateReports(cert.get());
712 if (r)
713 remote_cert_report_id = r->id();
714 }
715
716 for (const auto& channel_iter : transport_iter.second.channel_stats) {
717 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700718 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000719 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
720 channel_report->set_timestamp(stats_gathering_started_);
721 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
722 channel_iter.component);
723 if (local_cert_report_id.get()) {
724 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
725 local_cert_report_id);
726 }
727 if (remote_cert_report_id.get()) {
728 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
729 remote_cert_report_id);
730 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800731 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
732 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
733 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
734 channel_report->AddString(
735 StatsReport::kStatsValueNameSrtpCipher,
736 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000737 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800738 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
739 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
740 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
741 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700742 channel_report->AddString(
743 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800744 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000745 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000746
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000747 int connection_id = 0;
748 for (const cricket::ConnectionInfo& info :
749 channel_iter.connection_infos) {
750 StatsReport* connection_report = AddConnectionInfoReport(
751 transport_iter.first, channel_iter.component, connection_id++,
752 channel_report->id(), info);
753 if (info.best_connection) {
754 channel_report->AddId(
755 StatsReport::kStatsValueNameSelectedCandidatePairId,
756 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758 }
759 }
760 }
761}
762
763void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700764 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000765
deadbeefab9b2d12015-10-14 11:33:11 -0700766 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 return;
768 }
769 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700770 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 LOG(LS_ERROR) << "Failed to get voice channel stats.";
772 return;
773 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000774
775 // TODO(tommi): The above code should run on the worker thread and post the
776 // results back to the signaling thread, where we can add data to the reports.
777 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
778
deadbeefab9b2d12015-10-14 11:33:11 -0700779 StatsReport::Id transport_id(GetTransportIdFromProxy(
780 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000781 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700783 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 return;
785 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000786
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000787 ExtractStatsFromList(voice_info.receivers, transport_id, this,
788 StatsReport::kReceive);
789 ExtractStatsFromList(voice_info.senders, transport_id, this,
790 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000791
792 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793}
794
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000795void StatsCollector::ExtractVideoInfo(
796 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700797 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000798
deadbeefab9b2d12015-10-14 11:33:11 -0700799 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000801
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700803 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 LOG(LS_ERROR) << "Failed to get video channel stats.";
805 return;
806 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000807
808 // TODO(tommi): The above code should run on the worker thread and post the
809 // results back to the signaling thread, where we can add data to the reports.
810 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
811
deadbeefab9b2d12015-10-14 11:33:11 -0700812 StatsReport::Id transport_id(GetTransportIdFromProxy(
813 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000814 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700816 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 return;
818 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000819 ExtractStatsFromList(video_info.receivers, transport_id, this,
820 StatsReport::kReceive);
821 ExtractStatsFromList(video_info.senders, transport_id, this,
822 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 if (video_info.bw_estimations.size() != 1) {
824 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
825 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000826 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
827 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000829 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
831}
832
nissefcc640f2016-04-01 01:10:42 -0700833void StatsCollector::ExtractSenderInfo() {
834 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
835
836 for (const auto& sender : pc_->GetSenders()) {
837 // TODO(nisse): SSRC == 0 currently means none. Delete check when
838 // that is fixed.
839 if (!sender->ssrc()) {
840 continue;
841 }
842 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
843 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
844 continue;
845 }
846 // Safe, because kind() == kVideoKind implies a subclass of
847 // VideoTrackInterface; see mediastreaminterface.h.
848 VideoTrackSourceInterface* source =
849 static_cast<VideoTrackInterface*>(track.get())->GetSource();
850
851 VideoTrackSourceInterface::Stats stats;
852 if (!source->GetStats(&stats)) {
853 continue;
854 }
855 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
856 StatsReport::kStatsReportTypeSsrc,
857 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
858 StatsReport* report = reports_.FindOrAddNew(stats_id);
859 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
860 stats.input_width);
861 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
862 stats.input_height);
863 }
864}
865
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000866void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700867 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000868
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000869 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
870
deadbeefab9b2d12015-10-14 11:33:11 -0700871 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000872 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000873 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000874 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000875 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000876 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700877 // Filter out the initial id (-1).
878 if (dc->id() >= 0) {
879 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
880 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000881 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
882 report->AddString(StatsReport::kStatsValueNameState,
883 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000884 }
885}
886
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000887StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000888 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000889 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700890 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700891 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
892 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000893 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894}
895
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700897 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000898 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000899 for (const auto& it : local_audio_tracks_) {
900 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200901 uint32_t ssrc = it.second;
902 StatsReport* report =
903 GetReport(StatsReport::kStatsReportTypeSsrc,
904 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000905 if (report == NULL) {
906 // This can happen if a local audio track is added to a stream on the
907 // fly and the report has not been set up yet. Do nothing in this case.
908 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
909 continue;
910 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000911
912 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000913 const StatsReport::Value* v =
914 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000915 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000916 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000917
jbauchbe24c942015-06-22 15:06:43 -0700918 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000919 UpdateReportFromAudioTrack(track, report);
920 }
921}
922
923void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
924 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700925 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700926 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000927
andrew2fe1cb02015-11-27 17:27:35 -0800928 // Don't overwrite report values if they're not available.
929 int signal_level;
930 if (track->GetSignalLevel(&signal_level)) {
931 RTC_DCHECK_GE(signal_level, 0);
932 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
933 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000934
andrew2fe1cb02015-11-27 17:27:35 -0800935 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000936
andrew2fe1cb02015-11-27 17:27:35 -0800937 if (audio_processor.get()) {
938 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000939 audio_processor->GetStats(&stats);
940
andrew2fe1cb02015-11-27 17:27:35 -0800941 SetAudioProcessingStats(
942 report, stats.typing_noise_detected, stats.echo_return_loss,
943 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700944 stats.aec_quality_min, stats.echo_delay_std_ms,
945 stats.residual_echo_likelihood);
Minyue2a8a78c2016-04-07 16:48:15 +0200946
947 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
948 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -0800949 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000950}
951
Peter Boström0c4e06b2015-10-07 12:23:21 +0200952bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
953 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000954 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700955 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000956 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700957 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000958 LOG(LS_WARNING) << "The SSRC " << ssrc
959 << " is not associated with a sending track";
960 return false;
961 }
962 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700963 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700964 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000965 LOG(LS_WARNING) << "The SSRC " << ssrc
966 << " is not associated with a receiving track";
967 return false;
968 }
969 }
970
971 return true;
972}
973
jbauchbe24c942015-06-22 15:06:43 -0700974void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700975 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700976
977 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
978
979 for (const auto& entry : track_ids_) {
980 StatsReport* report = entry.second;
981 report->set_timestamp(stats_gathering_started_);
982 }
jbauchbe24c942015-06-22 15:06:43 -0700983}
984
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000985void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000986 stats_gathering_started_ = 0;
987}
988
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989} // namespace webrtc