blob: a47e8d7af98d2512ade4f4986daf70c76be83286 [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
13#include <utility>
14#include <vector>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/peerconnection.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000017#include "talk/session/media/channel.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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000020#include "webrtc/base/scoped_ptr.h"
21#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000023using rtc::scoped_ptr;
24
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000028// The following is the enum RTCStatsIceCandidateType from
29// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
30// our stats report for ice candidate type could conform to that.
31const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
32const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
33const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
34const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
35
36// Strings used by the stats collector to report adapter types. This fits the
37// general stype of http://w3c.github.io/webrtc-stats than what
38// AdapterTypeToString does.
39const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
40const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
41const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
42const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000043const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000044
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045template<typename ValueType>
46struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000047 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000048 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000049};
50
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000051typedef TypeForAdd<bool> BoolForAdd;
52typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020053typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000054typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000055
deadbeefd59daf82015-10-14 15:02:44 -070056StatsReport::Id GetTransportIdFromProxy(const ProxyTransportMap& map,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000057 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070058 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070059 auto found = map.find(proxy);
60 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070062 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000063
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000064 return StatsReport::NewComponentId(
65 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000066}
67
jbauchbe24c942015-06-22 15:06:43 -070068StatsReport* AddTrackReport(StatsCollection* reports,
69 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000070 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000071 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000072 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000073 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000074 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070075 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000076}
77
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078template <class TrackVector>
jbauchbe24c942015-06-22 15:06:43 -070079void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
80 TrackIdMap& track_ids) {
81 for (const auto& track : tracks) {
82 const std::string& track_id = track->id();
83 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070084 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -070085 track_ids[track_id] = report;
86 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087}
88
tommi@webrtc.org92f40182015-03-04 15:25:19 +000089void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
90 StatsReport* report) {
91 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
92 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
93 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
94}
95
pbosf42376c2015-08-28 07:35:32 -070096void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
97 StatsReport* report) {
98 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
99}
100
andrew2fe1cb02015-11-27 17:27:35 -0800101void SetAudioProcessingStats(StatsReport* report,
102 bool typing_noise_detected,
103 int echo_return_loss,
104 int echo_return_loss_enhancement,
105 int echo_delay_median_ms,
106 float aec_quality_min,
107 int echo_delay_std_ms) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000108 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
109 typing_noise_detected);
110 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
111 aec_quality_min);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000112 const IntForAdd ints[] = {
113 { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss },
114 { StatsReport::kStatsValueNameEchoReturnLossEnhancement,
115 echo_return_loss_enhancement },
116 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
117 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
118 };
119 for (const auto& i : ints)
120 report->AddInt(i.name, i.value);
121}
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700124 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000125 const FloatForAdd floats[] = {
126 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
127 { StatsReport::kStatsValueNameSecondaryDecodedRate,
128 info.secondary_decoded_rate },
129 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200130 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
131 { StatsReport::kStatsValueNamePreemptiveExpandRate,
132 info.preemptive_expand_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000133 };
134
135 const IntForAdd ints[] = {
136 { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level },
137 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
138 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
139 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
140 { StatsReport::kStatsValueNameDecodingCTSG,
141 info.decoding_calls_to_silence_generator },
142 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
143 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
144 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
145 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
146 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
147 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
148 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
149 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
150 info.jitter_buffer_preferred_ms },
151 };
152
153 for (const auto& f : floats)
154 report->AddFloat(f.name, f.value);
155
156 for (const auto& i : ints)
157 report->AddInt(i.name, i.value);
158
159 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000161 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000162 info.capture_start_ntp_time_ms);
fippobec70ab2016-01-28 01:27:15 -0800163 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164}
165
166void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000167 ExtractCommonSendProperties(info, report);
168
andrew2fe1cb02015-11-27 17:27:35 -0800169 SetAudioProcessingStats(
170 report, info.typing_noise_detected, info.echo_return_loss,
171 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
172 info.aec_quality_min, info.echo_delay_std_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000173
andrew2fe1cb02015-11-27 17:27:35 -0800174 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000175 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800176 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000177 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
178 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
179 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
180 };
181
182 for (const auto& i : ints)
183 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800184 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185}
186
187void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700188 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100189 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
190 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000191 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000193 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000194 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000195 const IntForAdd ints[] = {
196 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
197 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
198 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
199 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
200 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
201 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
202 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
203 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
204 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
205 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
206 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
207 info.min_playout_delay_ms },
208 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
209 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
210 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
211 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
212 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
213 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
214 };
215
216 for (const auto& i : ints)
217 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800218 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219}
220
221void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000222 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
Peter Boströmb7d9a972015-12-18 16:01:11 +0100224 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
225 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000226 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
227 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000228 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
229 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000230 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
231 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000232
233 const IntForAdd ints[] = {
234 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
235 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000236 { StatsReport::kStatsValueNameEncodeUsagePercent,
237 info.encode_usage_percent },
238 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
239 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
240 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
241 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
242 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
243 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
244 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
245 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
246 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
247 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
248 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
249 };
250
251 for (const auto& i : ints)
252 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800253 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254}
255
256void ExtractStats(const cricket::BandwidthEstimationInfo& info,
257 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000258 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700260 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000262 report->set_timestamp(stats_gathering_started);
263 const IntForAdd ints[] = {
264 { StatsReport::kStatsValueNameAvailableSendBandwidth,
265 info.available_send_bandwidth },
266 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
267 info.available_recv_bandwidth },
268 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
269 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
270 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
271 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
272 };
273 for (const auto& i : ints)
274 report->AddInt(i.name, i.value);
275 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276}
277
wu@webrtc.org97077a32013-10-25 21:18:33 +0000278void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
279 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000280 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000281 // TODO(hta): Extract some stats here.
282}
283
284void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
285 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000286 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000287 // TODO(hta): Extract some stats here.
288}
289
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000291// In order to use the template, the functions that are called from it,
292// ExtractStats and ExtractRemoteStats, must be defined and overloaded
293// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294template<typename T>
295void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000296 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000297 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000298 StatsReport::Direction direction) {
299 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200300 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000301 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000302 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000303 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
304 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000305 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000306 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000307
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000308 if (!d.remote_stats.empty()) {
309 report = collector->PrepareReport(false, ssrc, transport_id, direction);
310 if (report)
311 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000312 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000314}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
316} // namespace
317
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000318const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
319 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
320 return STATSREPORT_LOCAL_PORT_TYPE;
321 }
322 if (candidate_type == cricket::STUN_PORT_TYPE) {
323 return STATSREPORT_STUN_PORT_TYPE;
324 }
325 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
326 return STATSREPORT_PRFLX_PORT_TYPE;
327 }
328 if (candidate_type == cricket::RELAY_PORT_TYPE) {
329 return STATSREPORT_RELAY_PORT_TYPE;
330 }
henrikg91d6ede2015-09-17 00:24:34 -0700331 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000332 return "unknown";
333}
334
335const char* AdapterTypeToStatsType(rtc::AdapterType type) {
336 switch (type) {
337 case rtc::ADAPTER_TYPE_UNKNOWN:
338 return "unknown";
339 case rtc::ADAPTER_TYPE_ETHERNET:
340 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
341 case rtc::ADAPTER_TYPE_WIFI:
342 return STATSREPORT_ADAPTER_TYPE_WIFI;
343 case rtc::ADAPTER_TYPE_CELLULAR:
344 return STATSREPORT_ADAPTER_TYPE_WWAN;
345 case rtc::ADAPTER_TYPE_VPN:
346 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000347 case rtc::ADAPTER_TYPE_LOOPBACK:
348 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000349 default:
henrikg91d6ede2015-09-17 00:24:34 -0700350 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000351 return "";
352 }
353}
354
deadbeefab9b2d12015-10-14 11:33:11 -0700355StatsCollector::StatsCollector(PeerConnection* pc)
356 : pc_(pc), stats_gathering_started_(0) {
357 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000358}
359
360StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700361 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000364double StatsCollector::GetTimeNow() {
365 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
366}
367
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368// Adds a MediaStream with tracks that can be used as a |selector| in a call
369// to GetStats.
370void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700371 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700372 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373
374 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700375 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700377 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378}
379
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000380void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200381 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700382 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700383 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000384#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000385 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700386 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000387#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000388
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000389 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000390
391 // Create the kStatsReportTypeTrack report for the new track if there is no
392 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000393 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
394 audio_track->id()));
395 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000396 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000397 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000398 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000399 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000400}
401
402void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700404 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000405 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
406 local_audio_tracks_.end(),
407 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
408 return track.first == audio_track && track.second == ssrc;
409 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000410}
411
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000412void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700414 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700415 RTC_DCHECK(reports != NULL);
416 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000418 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
419
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000421 reports->reserve(reports_.size());
422 for (auto* r : reports_)
423 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000424 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 }
426
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000427 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700428 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000429 if (report)
430 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000432 report = reports_.Find(StatsReport::NewTypedId(
433 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000435 if (!report)
436 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000438 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439
440 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000441 for (const auto* r : reports_) {
442 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000444
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000445 const StatsReport::Value* v =
446 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000447 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000448 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450}
451
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000452void
453StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700454 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 double time_now = GetTimeNow();
456 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
457 // ms apart will be ignored.
458 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000459 if (stats_gathering_started_ != 0 &&
460 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 return;
462 }
463 stats_gathering_started_ = time_now;
464
deadbeefab9b2d12015-10-14 11:33:11 -0700465 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000466 // TODO(tommi): All of these hop over to the worker thread to fetch
467 // information. We could use an AsyncInvoker to run all of these and post
468 // the information back to the signaling thread where we can create and
469 // update stats reports. That would also clean up the threading story a bit
470 // since we'd be creating/updating the stats report objects consistently on
471 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 ExtractSessionInfo();
473 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000474 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000475 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700476 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
478}
479
deadbeefab9b2d12015-10-14 11:33:11 -0700480StatsReport* StatsCollector::PrepareReport(
481 bool local,
482 uint32_t ssrc,
483 const StatsReport::Id& transport_id,
484 StatsReport::Direction direction) {
485 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000486 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200487 local ? StatsReport::kStatsReportTypeSsrc
488 : StatsReport::kStatsReportTypeRemoteSsrc,
489 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000490 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
xians@webrtc.org01bda202014-07-09 07:38:38 +0000492 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000494 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000495 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000496 // The ssrc is not used by any track or existing report, return NULL
497 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000499 }
500
501 // The ssrc is not used by any existing track. Keeps the old track id
502 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000503 const StatsReport::Value* v =
504 report->FindValue(StatsReport::kStatsValueNameTrackId);
505 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000506 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 }
508
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000509 if (!report)
510 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000512 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000513 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000515 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000516 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000518 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 return report;
520}
521
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000522StatsReport* StatsCollector::AddOneCertificateReport(
523 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
deadbeefab9b2d12015-10-14 11:33:11 -0700524 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000525
wu@webrtc.org4551b792013-10-09 15:37:36 +0000526 // TODO(bemasc): Move this computation to a helper class that caches these
527 // values to reduce CPU use in GetStats. This will require adding a fast
528 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000529
530 std::string digest_algorithm;
531 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000532 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000533
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000534 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
535 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000536
537 // SSLFingerprint::Create can fail if the algorithm returned by
538 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
539 // implementation of SSLCertificate::ComputeDigest. This currently happens
540 // with MD5- and SHA-224-signed certificates when linked to libNSS.
541 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000542 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000543
wu@webrtc.org4551b792013-10-09 15:37:36 +0000544 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
545
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000546 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000547 cert->ToDER(&der_buffer);
548 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000549 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
550 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000551
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000552 StatsReport::Id id(StatsReport::NewTypedId(
553 StatsReport::kStatsReportTypeCertificate, fingerprint));
554 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000555 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000556 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
557 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
558 digest_algorithm);
559 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000560 if (issuer)
561 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
562 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000563}
564
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000565StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000566 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700567 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000568 // Produces a chain of StatsReports representing this certificate and the rest
569 // of its chain, and adds those reports to |reports_|. The return value is
570 // the id of the leaf report. The provided cert must be non-null, so at least
571 // one report will always be provided and the returned string will never be
572 // empty.
henrikg91d6ede2015-09-17 00:24:34 -0700573 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000574
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000575 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000577 if (cert->GetChain(chain.accept())) {
578 // This loop runs in reverse, i.e. from root to leaf, so that each
579 // certificate's issuer's report ID is known before the child certificate's
580 // report is generated. The root certificate does not have an issuer ID
581 // value.
582 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000583 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000584 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000585 }
586 }
587 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000588 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000589}
590
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000591StatsReport* StatsCollector::AddConnectionInfoReport(
592 const std::string& content_name, int component, int connection_id,
593 const StatsReport::Id& channel_report_id,
594 const cricket::ConnectionInfo& info) {
595 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
596 connection_id));
597 StatsReport* report = reports_.ReplaceOrAddNew(id);
598 report->set_timestamp(stats_gathering_started_);
599
600 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700601 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
602 {StatsReport::kStatsValueNameReceiving, info.receiving},
603 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000604 };
605 for (const auto& b : bools)
606 report->AddBoolean(b.name, b.value);
607
608 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
609 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
610 AddCandidateReport(info.local_candidate, true)->id());
611 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
612 AddCandidateReport(info.remote_candidate, false)->id());
613
614 const Int64ForAdd int64s[] = {
615 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
616 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
617 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
618 { StatsReport::kStatsValueNameRtt, info.rtt },
619 { StatsReport::kStatsValueNameSendPacketsDiscarded,
620 info.sent_discarded_packets },
621 };
622 for (const auto& i : int64s)
623 report->AddInt64(i.name, i.value);
624
625 report->AddString(StatsReport::kStatsValueNameLocalAddress,
626 info.local_candidate.address().ToString());
627 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
628 info.local_candidate.type());
629 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
630 info.remote_candidate.address().ToString());
631 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
632 info.remote_candidate.type());
633 report->AddString(StatsReport::kStatsValueNameTransportType,
634 info.local_candidate.protocol());
635
636 return report;
637}
638
639StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000640 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000641 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000642 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
643 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000644 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000645 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000646 report->set_timestamp(stats_gathering_started_);
647 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000648 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
649 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000650 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000651 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
652 candidate.address().ipaddr().ToString());
653 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
654 candidate.address().PortAsString());
655 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
656 candidate.priority());
657 report->AddString(StatsReport::kStatsValueNameCandidateType,
658 IceCandidateTypeToStatsType(candidate.type()));
659 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
660 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000661 }
662
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000663 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000664}
665
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700667 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000668
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000670 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700671 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000673 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000674 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700675 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676
deadbeefd59daf82015-10-14 15:02:44 -0700677 SessionStats stats;
deadbeefab9b2d12015-10-14 11:33:11 -0700678 if (!pc_->session()->GetTransportStats(&stats)) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000679 return;
680 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000682 // Store the proxy map away for use in SSRC reporting.
683 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
684 // signaling thread after fetching them on the worker thread, then just use
685 // the proxy map directly from the session stats.
686 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
687 // data.
688 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000689
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000690 for (const auto& transport_iter : stats.transport_stats) {
691 // Attempt to get a copy of the certificates from the transport and
692 // expose them in stats reports. All channels in a transport share the
693 // same local and remote certificates.
694 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000695 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200696 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700697 if (pc_->session()->GetLocalCertificate(
698 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200699 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000700 if (r)
701 local_cert_report_id = r->id();
702 }
703
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000704 rtc::scoped_ptr<rtc::SSLCertificate> cert;
deadbeefab9b2d12015-10-14 11:33:11 -0700705 if (pc_->session()->GetRemoteSSLCertificate(
706 transport_iter.second.transport_name, cert.accept())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000707 StatsReport* r = AddCertificateReports(cert.get());
708 if (r)
709 remote_cert_report_id = r->id();
710 }
711
712 for (const auto& channel_iter : transport_iter.second.channel_stats) {
713 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700714 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000715 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
716 channel_report->set_timestamp(stats_gathering_started_);
717 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
718 channel_iter.component);
719 if (local_cert_report_id.get()) {
720 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
721 local_cert_report_id);
722 }
723 if (remote_cert_report_id.get()) {
724 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
725 remote_cert_report_id);
726 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800727 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
728 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
729 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
730 channel_report->AddString(
731 StatsReport::kStatsValueNameSrtpCipher,
732 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000733 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800734 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
735 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
736 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
737 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700738 channel_report->AddString(
739 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800740 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000741 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000742
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000743 int connection_id = 0;
744 for (const cricket::ConnectionInfo& info :
745 channel_iter.connection_infos) {
746 StatsReport* connection_report = AddConnectionInfoReport(
747 transport_iter.first, channel_iter.component, connection_id++,
748 channel_report->id(), info);
749 if (info.best_connection) {
750 channel_report->AddId(
751 StatsReport::kStatsValueNameSelectedCandidatePairId,
752 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 }
754 }
755 }
756 }
757}
758
759void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700760 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000761
deadbeefab9b2d12015-10-14 11:33:11 -0700762 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 return;
764 }
765 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700766 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 LOG(LS_ERROR) << "Failed to get voice channel stats.";
768 return;
769 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000770
771 // TODO(tommi): The above code should run on the worker thread and post the
772 // results back to the signaling thread, where we can add data to the reports.
773 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
774
deadbeefab9b2d12015-10-14 11:33:11 -0700775 StatsReport::Id transport_id(GetTransportIdFromProxy(
776 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000777 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700779 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 return;
781 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000782
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000783 ExtractStatsFromList(voice_info.receivers, transport_id, this,
784 StatsReport::kReceive);
785 ExtractStatsFromList(voice_info.senders, transport_id, this,
786 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000787
788 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789}
790
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000791void StatsCollector::ExtractVideoInfo(
792 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700793 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000794
deadbeefab9b2d12015-10-14 11:33:11 -0700795 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000797
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700799 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 LOG(LS_ERROR) << "Failed to get video channel stats.";
801 return;
802 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000803
804 // TODO(tommi): The above code should run on the worker thread and post the
805 // results back to the signaling thread, where we can add data to the reports.
806 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
807
deadbeefab9b2d12015-10-14 11:33:11 -0700808 StatsReport::Id transport_id(GetTransportIdFromProxy(
809 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000810 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700812 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 return;
814 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000815 ExtractStatsFromList(video_info.receivers, transport_id, this,
816 StatsReport::kReceive);
817 ExtractStatsFromList(video_info.senders, transport_id, this,
818 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 if (video_info.bw_estimations.size() != 1) {
820 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
821 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000822 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
823 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000825 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827}
828
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000829void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700830 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000831
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000832 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
833
deadbeefab9b2d12015-10-14 11:33:11 -0700834 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000835 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000836 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000837 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000838 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000839 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
840 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
841 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
842 report->AddString(StatsReport::kStatsValueNameState,
843 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000844 }
845}
846
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000847StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000848 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000849 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700850 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700851 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
852 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000853 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000854}
855
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000856void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700857 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000858 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000859 for (const auto& it : local_audio_tracks_) {
860 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200861 uint32_t ssrc = it.second;
862 StatsReport* report =
863 GetReport(StatsReport::kStatsReportTypeSsrc,
864 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000865 if (report == NULL) {
866 // This can happen if a local audio track is added to a stream on the
867 // fly and the report has not been set up yet. Do nothing in this case.
868 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
869 continue;
870 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000871
872 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000873 const StatsReport::Value* v =
874 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000875 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000876 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000877
jbauchbe24c942015-06-22 15:06:43 -0700878 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000879 UpdateReportFromAudioTrack(track, report);
880 }
881}
882
883void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
884 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700885 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700886 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000887
andrew2fe1cb02015-11-27 17:27:35 -0800888 // Don't overwrite report values if they're not available.
889 int signal_level;
890 if (track->GetSignalLevel(&signal_level)) {
891 RTC_DCHECK_GE(signal_level, 0);
892 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
893 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894
andrew2fe1cb02015-11-27 17:27:35 -0800895 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896
andrew2fe1cb02015-11-27 17:27:35 -0800897 if (audio_processor.get()) {
898 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000899 audio_processor->GetStats(&stats);
900
andrew2fe1cb02015-11-27 17:27:35 -0800901 SetAudioProcessingStats(
902 report, stats.typing_noise_detected, stats.echo_return_loss,
903 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
904 stats.aec_quality_min, stats.echo_delay_std_ms);
905 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000906}
907
Peter Boström0c4e06b2015-10-07 12:23:21 +0200908bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
909 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000910 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700911 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000912 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700913 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000914 LOG(LS_WARNING) << "The SSRC " << ssrc
915 << " is not associated with a sending track";
916 return false;
917 }
918 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700919 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700920 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000921 LOG(LS_WARNING) << "The SSRC " << ssrc
922 << " is not associated with a receiving track";
923 return false;
924 }
925 }
926
927 return true;
928}
929
jbauchbe24c942015-06-22 15:06:43 -0700930void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700931 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700932
933 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
934
935 for (const auto& entry : track_ids_) {
936 StatsReport* report = entry.second;
937 report->set_timestamp(stats_gathering_started_);
938 }
jbauchbe24c942015-06-22 15:06:43 -0700939}
940
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000941void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000942 stats_gathering_started_ = 0;
943}
944
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945} // namespace webrtc