blob: 7dfc3ea5a520e8f51cb129c137aa12cf29b5f760 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/statscollector.h"
29
30#include <utility>
31#include <vector>
32
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/base64.h"
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +000035#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/scoped_ptr.h"
37#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000039using rtc::scoped_ptr;
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000044// The following is the enum RTCStatsIceCandidateType from
45// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
46// our stats report for ice candidate type could conform to that.
47const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
48const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
49const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
50const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
51
52// Strings used by the stats collector to report adapter types. This fits the
53// general stype of http://w3c.github.io/webrtc-stats than what
54// AdapterTypeToString does.
55const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
56const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
57const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
58const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000059const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000060
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061template<typename ValueType>
62struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000063 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000064 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000065};
66
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000067typedef TypeForAdd<bool> BoolForAdd;
68typedef TypeForAdd<float> FloatForAdd;
69typedef TypeForAdd<int64> Int64ForAdd;
70typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000071
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000072StatsReport::Id GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
73 const std::string& proxy) {
74 DCHECK(!proxy.empty());
tommi@webrtc.org47218952014-07-15 19:22:37 +000075 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000076 if (found == map.end())
77 return StatsReport::Id();
tommi@webrtc.org47218952014-07-15 19:22:37 +000078
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000079 return StatsReport::NewComponentId(
80 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000081}
82
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000083void AddTrackReport(StatsCollection* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000084 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000085 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000086 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000087 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000088 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
xians@webrtc.org01bda202014-07-09 07:38:38 +000089}
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091template <class TrackVector>
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000092void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000093 for (const auto& track : tracks)
xians@webrtc.org01bda202014-07-09 07:38:38 +000094 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095}
96
tommi@webrtc.org92f40182015-03-04 15:25:19 +000097void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
98 StatsReport* report) {
99 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
100 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
101 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
102}
103
104void SetAudioProcessingStats(StatsReport* report, int signal_level,
105 bool typing_noise_detected, int echo_return_loss,
106 int echo_return_loss_enhancement, int echo_delay_median_ms,
107 float aec_quality_min, int echo_delay_std_ms) {
108 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
109 typing_noise_detected);
110 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
111 aec_quality_min);
112 // Don't overwrite the previous signal level if it's not available now.
113 if (signal_level >= 0)
114 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
115 const IntForAdd ints[] = {
116 { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss },
117 { StatsReport::kStatsValueNameEchoReturnLossEnhancement,
118 echo_return_loss_enhancement },
119 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
120 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
121 };
122 for (const auto& i : ints)
123 report->AddInt(i.name, i.value);
124}
125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000127 const FloatForAdd floats[] = {
128 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
129 { StatsReport::kStatsValueNameSecondaryDecodedRate,
130 info.secondary_decoded_rate },
131 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
132 };
133
134 const IntForAdd ints[] = {
135 { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level },
136 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
137 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
138 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
139 { StatsReport::kStatsValueNameDecodingCTSG,
140 info.decoding_calls_to_silence_generator },
141 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
142 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
143 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
144 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
145 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
146 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
147 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
148 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
149 info.jitter_buffer_preferred_ms },
150 };
151
152 for (const auto& f : floats)
153 report->AddFloat(f.name, f.value);
154
155 for (const auto& i : ints)
156 report->AddInt(i.name, i.value);
157
158 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000160 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000161 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000162
163 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
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
169 SetAudioProcessingStats(report, info.audio_level, info.typing_noise_detected,
170 info.echo_return_loss, info.echo_return_loss_enhancement,
171 info.echo_delay_median_ms, info.aec_quality_min, info.echo_delay_std_ms);
172
173 const IntForAdd ints[] = {
174 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
175 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
176 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
177 };
178
179 for (const auto& i : ints)
180 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181}
182
183void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000186 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000187 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000188 const IntForAdd ints[] = {
189 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
190 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
191 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
192 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
193 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
194 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
195 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
196 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
197 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
198 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
199 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
200 info.min_playout_delay_ms },
201 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
202 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
203 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
204 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
205 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
206 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
207 };
208
209 for (const auto& i : ints)
210 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
213void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000214 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000216 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
217 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000218 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
219 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000220 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
221 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000222
223 const IntForAdd ints[] = {
224 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
225 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
226 { StatsReport::kStatsValueNameCaptureJitterMs, info.capture_jitter_ms },
227 { StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
228 info.capture_queue_delay_ms_per_s },
229 { StatsReport::kStatsValueNameEncodeUsagePercent,
230 info.encode_usage_percent },
231 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
232 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
233 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
234 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
235 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
236 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
237 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
238 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
239 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
240 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
241 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
242 };
243
244 for (const auto& i : ints)
245 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246}
247
248void ExtractStats(const cricket::BandwidthEstimationInfo& info,
249 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000250 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 StatsReport* report) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000252 DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000254 report->set_timestamp(stats_gathering_started);
255 const IntForAdd ints[] = {
256 { StatsReport::kStatsValueNameAvailableSendBandwidth,
257 info.available_send_bandwidth },
258 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
259 info.available_recv_bandwidth },
260 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
261 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
262 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
263 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
264 };
265 for (const auto& i : ints)
266 report->AddInt(i.name, i.value);
267 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268}
269
wu@webrtc.org97077a32013-10-25 21:18:33 +0000270void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
271 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000272 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000273 // TODO(hta): Extract some stats here.
274}
275
276void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
277 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000278 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000279 // TODO(hta): Extract some stats here.
280}
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000283// In order to use the template, the functions that are called from it,
284// ExtractStats and ExtractRemoteStats, must be defined and overloaded
285// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286template<typename T>
287void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000288 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000289 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000290 StatsReport::Direction direction) {
291 for (const auto& d : data) {
292 uint32 ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000293 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000294 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000295 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
296 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000297 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000298 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000299
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000300 if (!d.remote_stats.empty()) {
301 report = collector->PrepareReport(false, ssrc, transport_id, direction);
302 if (report)
303 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000304 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000306}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307
308} // namespace
309
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000310const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
311 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
312 return STATSREPORT_LOCAL_PORT_TYPE;
313 }
314 if (candidate_type == cricket::STUN_PORT_TYPE) {
315 return STATSREPORT_STUN_PORT_TYPE;
316 }
317 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
318 return STATSREPORT_PRFLX_PORT_TYPE;
319 }
320 if (candidate_type == cricket::RELAY_PORT_TYPE) {
321 return STATSREPORT_RELAY_PORT_TYPE;
322 }
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000323 DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000324 return "unknown";
325}
326
327const char* AdapterTypeToStatsType(rtc::AdapterType type) {
328 switch (type) {
329 case rtc::ADAPTER_TYPE_UNKNOWN:
330 return "unknown";
331 case rtc::ADAPTER_TYPE_ETHERNET:
332 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
333 case rtc::ADAPTER_TYPE_WIFI:
334 return STATSREPORT_ADAPTER_TYPE_WIFI;
335 case rtc::ADAPTER_TYPE_CELLULAR:
336 return STATSREPORT_ADAPTER_TYPE_WWAN;
337 case rtc::ADAPTER_TYPE_VPN:
338 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000339 case rtc::ADAPTER_TYPE_LOOPBACK:
340 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000341 default:
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000342 DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000343 return "";
344 }
345}
346
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000347StatsCollector::StatsCollector(WebRtcSession* session)
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000348 : session_(session),
349 stats_gathering_started_(0) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000350 DCHECK(session_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000351}
352
353StatsCollector::~StatsCollector() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000354 DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355}
356
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000357double StatsCollector::GetTimeNow() {
358 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
359}
360
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361// Adds a MediaStream with tracks that can be used as a |selector| in a call
362// to GetStats.
363void StatsCollector::AddStream(MediaStreamInterface* stream) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000364 DCHECK(session_->signaling_thread()->IsCurrent());
365 DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366
367 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
368 &reports_);
369 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
370 &reports_);
371}
372
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000373void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
374 uint32 ssrc) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000375 DCHECK(session_->signaling_thread()->IsCurrent());
376 DCHECK(audio_track != NULL);
377#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000378 for (const auto& track : local_audio_tracks_)
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000379 DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000380#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000381
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000382 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000383
384 // Create the kStatsReportTypeTrack report for the new track if there is no
385 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000386 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
387 audio_track->id()));
388 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000389 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000390 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000391 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000392 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000393}
394
395void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
396 uint32 ssrc) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000397 DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000398 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
399 local_audio_tracks_.end(),
400 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
401 return track.first == audio_track && track.second == ssrc;
402 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000403}
404
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000405void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 StatsReports* reports) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000407 DCHECK(session_->signaling_thread()->IsCurrent());
408 DCHECK(reports != NULL);
409 DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000411 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
412
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000414 reports->reserve(reports_.size());
415 for (auto* r : reports_)
416 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000417 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 }
419
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000420 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
421 StatsReport::kStatsReportTypeSession, session_->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000422 if (report)
423 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000425 report = reports_.Find(StatsReport::NewTypedId(
426 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000428 if (!report)
429 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000431 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432
433 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000434 for (const auto* r : reports_) {
435 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000437
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000438 const StatsReport::Value* v =
439 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000440 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000441 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443}
444
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000445void
446StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000447 DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 double time_now = GetTimeNow();
449 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
450 // ms apart will be ignored.
451 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000452 if (stats_gathering_started_ != 0 &&
453 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 return;
455 }
456 stats_gathering_started_ = time_now;
457
458 if (session_) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000459 // TODO(tommi): All of these hop over to the worker thread to fetch
460 // information. We could use an AsyncInvoker to run all of these and post
461 // the information back to the signaling thread where we can create and
462 // update stats reports. That would also clean up the threading story a bit
463 // since we'd be creating/updating the stats report objects consistently on
464 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 ExtractSessionInfo();
466 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000467 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000468 ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 }
470}
471
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000472StatsReport* StatsCollector::PrepareReport(
473 bool local,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000474 uint32 ssrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000475 const StatsReport::Id& transport_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000476 StatsReport::Direction direction) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000477 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000478 StatsReport::Id id(StatsReport::NewIdWithDirection(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000479 local ? StatsReport::kStatsReportTypeSsrc :
480 StatsReport::kStatsReportTypeRemoteSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000481 rtc::ToString<uint32>(ssrc), direction));
482 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
xians@webrtc.org01bda202014-07-09 07:38:38 +0000484 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000486 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000487 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000488 // The ssrc is not used by any track or existing report, return NULL
489 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000491 }
492
493 // The ssrc is not used by any existing track. Keeps the old track id
494 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000495 const StatsReport::Value* v =
496 report->FindValue(StatsReport::kStatsValueNameTrackId);
497 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000498 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000501 if (!report)
502 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000504 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000505 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000507 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000508 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000510 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 return report;
512}
513
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000514StatsReport* StatsCollector::AddOneCertificateReport(
515 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000516 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000517
wu@webrtc.org4551b792013-10-09 15:37:36 +0000518 // TODO(bemasc): Move this computation to a helper class that caches these
519 // values to reduce CPU use in GetStats. This will require adding a fast
520 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000521
522 std::string digest_algorithm;
523 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000524 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000525
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000526 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
527 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000528
529 // SSLFingerprint::Create can fail if the algorithm returned by
530 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
531 // implementation of SSLCertificate::ComputeDigest. This currently happens
532 // with MD5- and SHA-224-signed certificates when linked to libNSS.
533 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000534 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000535
wu@webrtc.org4551b792013-10-09 15:37:36 +0000536 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
537
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000539 cert->ToDER(&der_buffer);
540 std::string der_base64;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000541 rtc::Base64::EncodeFromArray(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000542 der_buffer.data(), der_buffer.length(), &der_base64);
543
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000544 StatsReport::Id id(StatsReport::NewTypedId(
545 StatsReport::kStatsReportTypeCertificate, fingerprint));
546 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000547 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000548 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
549 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
550 digest_algorithm);
551 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000552 if (issuer)
553 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
554 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000555}
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) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000559 DCHECK(session_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000560 // Produces a chain of StatsReports representing this certificate and the rest
561 // of its chain, and adds those reports to |reports_|. The return value is
562 // the id of the leaf report. The provided cert must be non-null, so at least
563 // one report will always be provided and the returned string will never be
564 // empty.
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000565 DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000566
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000567 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000568 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000569 if (cert->GetChain(chain.accept())) {
570 // This loop runs in reverse, i.e. from root to leaf, so that each
571 // certificate's issuer's report ID is known before the child certificate's
572 // report is generated. The root certificate does not have an issuer ID
573 // value.
574 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000576 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000577 }
578 }
579 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000580 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000581}
582
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000583StatsReport* StatsCollector::AddConnectionInfoReport(
584 const std::string& content_name, int component, int connection_id,
585 const StatsReport::Id& channel_report_id,
586 const cricket::ConnectionInfo& info) {
587 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
588 connection_id));
589 StatsReport* report = reports_.ReplaceOrAddNew(id);
590 report->set_timestamp(stats_gathering_started_);
591
592 const BoolForAdd bools[] = {
593 { StatsReport::kStatsValueNameActiveConnection, info.best_connection },
594 { StatsReport::kStatsValueNameReadable, info.readable },
595 { StatsReport::kStatsValueNameWritable, info.writable },
596 };
597 for (const auto& b : bools)
598 report->AddBoolean(b.name, b.value);
599
600 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
601 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
602 AddCandidateReport(info.local_candidate, true)->id());
603 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
604 AddCandidateReport(info.remote_candidate, false)->id());
605
606 const Int64ForAdd int64s[] = {
607 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
608 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
609 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
610 { StatsReport::kStatsValueNameRtt, info.rtt },
611 { StatsReport::kStatsValueNameSendPacketsDiscarded,
612 info.sent_discarded_packets },
613 };
614 for (const auto& i : int64s)
615 report->AddInt64(i.name, i.value);
616
617 report->AddString(StatsReport::kStatsValueNameLocalAddress,
618 info.local_candidate.address().ToString());
619 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
620 info.local_candidate.type());
621 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
622 info.remote_candidate.address().ToString());
623 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
624 info.remote_candidate.type());
625 report->AddString(StatsReport::kStatsValueNameTransportType,
626 info.local_candidate.protocol());
627
628 return report;
629}
630
631StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000632 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000633 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000634 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
635 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000636 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000637 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000638 report->set_timestamp(stats_gathering_started_);
639 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000640 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
641 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000642 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000643 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
644 candidate.address().ipaddr().ToString());
645 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
646 candidate.address().PortAsString());
647 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
648 candidate.priority());
649 report->AddString(StatsReport::kStatsValueNameCandidateType,
650 IceCandidateTypeToStatsType(candidate.type()));
651 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
652 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000653 }
654
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000655 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000656}
657
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658void StatsCollector::ExtractSessionInfo() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000659 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000660
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000662 StatsReport::Id id(StatsReport::NewTypedId(
663 StatsReport::kStatsReportTypeSession, session_->id()));
664 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000665 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000666 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
667 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668
669 cricket::SessionStats stats;
670 if (session_->GetStats(&stats)) {
671 // Store the proxy map away for use in SSRC reporting.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
673 // signaling thread after fetching them on the worker thread, then just use
674 // the proxy map directly from the session stats.
675 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
676 // data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 proxy_to_transport_ = stats.proxy_to_transport;
678
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000679 for (const auto& transport_iter : stats.transport_stats) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000680 // Attempt to get a copy of the certificates from the transport and
681 // expose them in stats reports. All channels in a transport share the
682 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000683 //
684 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
685 // invoke method calls on the worker thread and block this thread, but
686 // messages are still processed on this thread, which may blow way the
687 // existing transports. So we cannot reuse |transport| after these calls.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000688 StatsReport::Id local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000689
wu@webrtc.org4551b792013-10-09 15:37:36 +0000690 cricket::Transport* transport =
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000691 session_->GetTransport(transport_iter.second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692 rtc::scoped_ptr<rtc::SSLIdentity> identity;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000693 if (transport && transport->GetIdentity(identity.accept())) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000694 StatsReport* r = AddCertificateReports(&(identity->certificate()));
695 if (r)
696 local_cert_report_id = r->id();
wu@webrtc.org4551b792013-10-09 15:37:36 +0000697 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000698
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000699 transport = session_->GetTransport(transport_iter.second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000700 rtc::scoped_ptr<rtc::SSLCertificate> cert;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000701 if (transport && transport->GetRemoteCertificate(cert.accept())) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000702 StatsReport* r = AddCertificateReports(cert.get());
703 if (r)
704 remote_cert_report_id = r->id();
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000705 }
706
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000707 for (const auto& channel_iter : transport_iter.second.channel_stats) {
708 StatsReport::Id id(StatsReport::NewComponentId(
709 transport_iter.second.content_name, channel_iter.component));
710 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000711 channel_report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000712 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000713 channel_iter.component);
714 if (local_cert_report_id.get()) {
715 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
716 local_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000717 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000718 if (remote_cert_report_id.get()) {
719 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
720 remote_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000721 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000722 const std::string& srtp_cipher = channel_iter.srtp_cipher;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000723 if (!srtp_cipher.empty()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000724 channel_report->AddString(StatsReport::kStatsValueNameSrtpCipher,
725 srtp_cipher);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000726 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000727 const std::string& ssl_cipher = channel_iter.ssl_cipher;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000728 if (!ssl_cipher.empty()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000729 channel_report->AddString(StatsReport::kStatsValueNameDtlsCipher,
730 ssl_cipher);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000731 }
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000732
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000733 int connection_id = 0;
734 for (const cricket::ConnectionInfo& info :
735 channel_iter.connection_infos) {
736 StatsReport* connection_report = AddConnectionInfoReport(
737 transport_iter.first, channel_iter.component, connection_id++,
738 channel_report->id(), info);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000739 if (info.best_connection) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000740 channel_report->AddId(
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000741 StatsReport::kStatsValueNameSelectedCandidatePairId,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000742 connection_report->id());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000743 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745 }
746 }
747 }
748}
749
750void StatsCollector::ExtractVoiceInfo() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000751 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000752
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 if (!session_->voice_channel()) {
754 return;
755 }
756 cricket::VoiceMediaInfo voice_info;
757 if (!session_->voice_channel()->GetStats(&voice_info)) {
758 LOG(LS_ERROR) << "Failed to get voice channel stats.";
759 return;
760 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000761
762 // TODO(tommi): The above code should run on the worker thread and post the
763 // results back to the signaling thread, where we can add data to the reports.
764 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
765
766 StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_,
767 session_->voice_channel()->content_name()));
768 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 LOG(LS_ERROR) << "Failed to get transport name for proxy "
770 << session_->voice_channel()->content_name();
771 return;
772 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000773
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000774 ExtractStatsFromList(voice_info.receivers, transport_id, this,
775 StatsReport::kReceive);
776 ExtractStatsFromList(voice_info.senders, transport_id, this,
777 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000778
779 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780}
781
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000782void StatsCollector::ExtractVideoInfo(
783 PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000784 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000785
786 if (!session_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000788
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 cricket::VideoMediaInfo video_info;
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000790 if (!session_->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 LOG(LS_ERROR) << "Failed to get video channel stats.";
792 return;
793 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000794
795 // TODO(tommi): The above code should run on the worker thread and post the
796 // results back to the signaling thread, where we can add data to the reports.
797 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
798
799 StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_,
800 session_->video_channel()->content_name()));
801 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 LOG(LS_ERROR) << "Failed to get transport name for proxy "
803 << session_->video_channel()->content_name();
804 return;
805 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000806 ExtractStatsFromList(video_info.receivers, transport_id, this,
807 StatsReport::kReceive);
808 ExtractStatsFromList(video_info.senders, transport_id, this,
809 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 if (video_info.bw_estimations.size() != 1) {
811 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
812 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000813 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
814 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000816 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 }
818}
819
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000820void StatsCollector::ExtractDataInfo() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000821 DCHECK(session_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000822
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000823 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
824
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000825 for (const auto& dc :
826 session_->mediastream_signaling()->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000827 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000828 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000829 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000830 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000831 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
832 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
833 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
834 report->AddString(StatsReport::kStatsValueNameState,
835 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000836 }
837}
838
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000839StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000840 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000841 StatsReport::Direction direction) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000842 DCHECK(session_->signaling_thread()->IsCurrent());
843 DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000844 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000845 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000846}
847
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000848void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000849 DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000850 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000851 for (const auto& it : local_audio_tracks_) {
852 AudioTrackInterface* track = it.first;
853 uint32 ssrc = it.second;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000854 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000855 rtc::ToString<uint32>(ssrc),
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000856 StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000857 if (report == NULL) {
858 // This can happen if a local audio track is added to a stream on the
859 // fly and the report has not been set up yet. Do nothing in this case.
860 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
861 continue;
862 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000863
864 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000865 const StatsReport::Value* v =
866 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000867 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000868 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000869
870 UpdateReportFromAudioTrack(track, report);
871 }
872}
873
874void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
875 StatsReport* report) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000876 DCHECK(session_->signaling_thread()->IsCurrent());
877 DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000878
879 int signal_level = 0;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000880 if (!track->GetSignalLevel(&signal_level))
881 signal_level = -1;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000882
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000883 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000884 track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000885
886 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000887 if (audio_processor.get())
888 audio_processor->GetStats(&stats);
889
890 SetAudioProcessingStats(report, signal_level, stats.typing_noise_detected,
891 stats.echo_return_loss, stats.echo_return_loss_enhancement,
892 stats.echo_delay_median_ms, stats.aec_quality_min,
893 stats.echo_delay_std_ms);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894}
895
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000896bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000897 StatsReport::Direction direction) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000898 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000899 if (direction == StatsReport::kSend) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000900 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000901 LOG(LS_WARNING) << "The SSRC " << ssrc
902 << " is not associated with a sending track";
903 return false;
904 }
905 } else {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000906 DCHECK(direction == StatsReport::kReceive);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000907 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000908 LOG(LS_WARNING) << "The SSRC " << ssrc
909 << " is not associated with a receiving track";
910 return false;
911 }
912 }
913
914 return true;
915}
916
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000917void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000918 stats_gathering_started_ = 0;
919}
920
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921} // namespace webrtc