blob: 96120966156d4d7cbd03b714f774bf4c98446926 [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"
35#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000038using rtc::scoped_ptr;
39
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000043// The following is the enum RTCStatsIceCandidateType from
44// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
45// our stats report for ice candidate type could conform to that.
46const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
47const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
48const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
49const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
50
51// Strings used by the stats collector to report adapter types. This fits the
52// general stype of http://w3c.github.io/webrtc-stats than what
53// AdapterTypeToString does.
54const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
55const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
56const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
57const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000058const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000059
tommi@webrtc.org47218952014-07-15 19:22:37 +000060bool GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
61 const std::string& proxy,
62 std::string* transport) {
63 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
64 if (proxy.empty()) {
65 transport->clear();
66 return true;
67 }
68
69 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
70 if (found == map.end()) {
71 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
72 return false;
73 }
74
tommi@webrtc.org47218952014-07-15 19:22:37 +000075 // Component 1 is always used for RTP.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000076 scoped_ptr<StatsReport::Id> id(
77 StatsReport::NewComponentId(found->second, 1));
78 // TODO(tommi): Should |transport| simply be of type StatsReport::Id?
79 // When we support more value types than string (e.g. int, double, vector etc)
80 // we should also support a value type for Id.
81 *transport = id->ToString();
tommi@webrtc.org47218952014-07-15 19:22:37 +000082 return true;
83}
84
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000085void AddTrackReport(StatsCollection* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000086 // Adds an empty track report.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000087 rtc::scoped_ptr<StatsReport::Id> id(
88 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
89 StatsReport* report = reports->ReplaceOrAddNew(id.Pass());
tommi@webrtc.org5b06b062014-08-15 08:38:30 +000090 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
xians@webrtc.org01bda202014-07-09 07:38:38 +000091}
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093template <class TrackVector>
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000094void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 for (size_t j = 0; j < tracks.size(); ++j) {
96 webrtc::MediaStreamTrackInterface* track = tracks[j];
xians@webrtc.org01bda202014-07-09 07:38:38 +000097 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 }
99}
100
101void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
102 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
103 info.audio_level);
104 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
105 info.bytes_rcvd);
106 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
107 info.jitter_ms);
jiayl@webrtc.org11aab0e2014-03-07 18:56:26 +0000108 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
109 info.jitter_buffer_ms);
110 report->AddValue(StatsReport::kStatsValueNamePreferredJitterBufferMs,
111 info.jitter_buffer_preferred_ms);
112 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
113 info.delay_estimate_ms);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000114 report->AddValue(StatsReport::kStatsValueNameExpandRate,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 rtc::ToString<float>(info.expand_rate));
minyue@webrtc.org652bc372015-02-18 23:50:46 +0000116 report->AddValue(StatsReport::kStatsValueNameSpeechExpandRate,
117 rtc::ToString<float>(info.speech_expand_rate));
118 report->AddValue(StatsReport::kStatsValueNameSecondaryDecodedRate,
119 rtc::ToString<float>(info.secondary_decoded_rate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
121 info.packets_rcvd);
122 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
123 info.packets_lost);
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000124 report->AddValue(StatsReport::kStatsValueNameDecodingCTSG,
125 info.decoding_calls_to_silence_generator);
126 report->AddValue(StatsReport::kStatsValueNameDecodingCTN,
127 info.decoding_calls_to_neteq);
128 report->AddValue(StatsReport::kStatsValueNameDecodingNormal,
129 info.decoding_normal);
130 report->AddValue(StatsReport::kStatsValueNameDecodingPLC,
131 info.decoding_plc);
132 report->AddValue(StatsReport::kStatsValueNameDecodingCNG,
133 info.decoding_cng);
134 report->AddValue(StatsReport::kStatsValueNameDecodingPLCCNG,
135 info.decoding_plc_cng);
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000136 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
137 info.capture_start_ntp_time_ms);
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000138 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139}
140
141void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
142 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
143 info.audio_level);
144 report->AddValue(StatsReport::kStatsValueNameBytesSent,
145 info.bytes_sent);
146 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
147 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000148 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
149 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
151 info.jitter_ms);
152 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
153 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000154 rtc::ToString<float>(info.aec_quality_min));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
156 info.echo_delay_median_ms);
157 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
158 info.echo_delay_std_ms);
159 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
160 info.echo_return_loss);
161 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
162 info.echo_return_loss_enhancement);
163 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000164 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
165 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166}
167
168void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
169 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
170 info.bytes_rcvd);
171 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
172 info.packets_rcvd);
173 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
174 info.packets_lost);
175
176 report->AddValue(StatsReport::kStatsValueNameFirsSent,
177 info.firs_sent);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000178 report->AddValue(StatsReport::kStatsValueNamePlisSent,
179 info.plis_sent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 report->AddValue(StatsReport::kStatsValueNameNacksSent,
181 info.nacks_sent);
182 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
183 info.frame_width);
184 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
185 info.frame_height);
186 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
187 info.framerate_rcvd);
188 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
189 info.framerate_decoded);
190 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
191 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000192
193 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
194 info.decode_ms);
195 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
196 info.max_decode_ms);
197 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
198 info.current_delay_ms);
199 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
200 info.target_delay_ms);
201 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
202 info.jitter_buffer_ms);
203 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
204 info.min_playout_delay_ms);
205 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
206 info.render_delay_ms);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000207
208 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
209 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210}
211
212void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
213 report->AddValue(StatsReport::kStatsValueNameBytesSent,
214 info.bytes_sent);
215 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
216 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000217 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
218 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219
220 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
221 info.firs_rcvd);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000222 report->AddValue(StatsReport::kStatsValueNamePlisReceived,
223 info.plis_rcvd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
225 info.nacks_rcvd);
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000226 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
227 info.input_frame_width);
228 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
229 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000231 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000233 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
235 info.framerate_input);
236 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
237 info.framerate_sent);
238 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
239 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000240 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
241 (info.adapt_reason & 0x1) > 0);
242 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
243 (info.adapt_reason & 0x2) > 0);
244 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
245 (info.adapt_reason & 0x4) > 0);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000246 report->AddValue(StatsReport::kStatsValueNameAdaptationChanges,
247 info.adapt_changes);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000248 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
249 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
250 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000251 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
252 info.capture_queue_delay_ms_per_s);
253 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
254 info.encode_usage_percent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255}
256
257void ExtractStats(const cricket::BandwidthEstimationInfo& info,
258 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000259 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 StatsReport* report) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000261 ASSERT(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262
263 // Clear out stats from previous GatherStats calls if any.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000264 if (report->timestamp() != stats_gathering_started) {
265 report->ResetValues();
266 report->set_timestamp(stats_gathering_started);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 }
268
269 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
270 info.available_send_bandwidth);
271 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
272 info.available_recv_bandwidth);
273 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
274 info.target_enc_bitrate);
275 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
276 info.actual_enc_bitrate);
277 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
278 info.retransmit_bitrate);
279 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
280 info.transmit_bitrate);
281 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
282 info.bucket_delay);
283}
284
wu@webrtc.org97077a32013-10-25 21:18:33 +0000285void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
286 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000287 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000288 // TODO(hta): Extract some stats here.
289}
290
291void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
292 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000293 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000294 // TODO(hta): Extract some stats here.
295}
296
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000298// In order to use the template, the functions that are called from it,
299// ExtractStats and ExtractRemoteStats, must be defined and overloaded
300// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301template<typename T>
302void ExtractStatsFromList(const std::vector<T>& data,
303 const std::string& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000304 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000305 StatsReport::Direction direction) {
306 for (const auto& d : data) {
307 uint32 ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000308 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000309 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000310 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
311 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000312 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000313 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000314
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000315 if (!d.remote_stats.empty()) {
316 report = collector->PrepareReport(false, ssrc, transport_id, direction);
317 if (report)
318 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000321}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322
323} // namespace
324
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000325const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
326 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
327 return STATSREPORT_LOCAL_PORT_TYPE;
328 }
329 if (candidate_type == cricket::STUN_PORT_TYPE) {
330 return STATSREPORT_STUN_PORT_TYPE;
331 }
332 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
333 return STATSREPORT_PRFLX_PORT_TYPE;
334 }
335 if (candidate_type == cricket::RELAY_PORT_TYPE) {
336 return STATSREPORT_RELAY_PORT_TYPE;
337 }
338 ASSERT(false);
339 return "unknown";
340}
341
342const char* AdapterTypeToStatsType(rtc::AdapterType type) {
343 switch (type) {
344 case rtc::ADAPTER_TYPE_UNKNOWN:
345 return "unknown";
346 case rtc::ADAPTER_TYPE_ETHERNET:
347 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
348 case rtc::ADAPTER_TYPE_WIFI:
349 return STATSREPORT_ADAPTER_TYPE_WIFI;
350 case rtc::ADAPTER_TYPE_CELLULAR:
351 return STATSREPORT_ADAPTER_TYPE_WWAN;
352 case rtc::ADAPTER_TYPE_VPN:
353 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000354 case rtc::ADAPTER_TYPE_LOOPBACK:
355 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000356 default:
357 ASSERT(false);
358 return "";
359 }
360}
361
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000362StatsCollector::StatsCollector(WebRtcSession* session)
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000363 : session_(session),
364 stats_gathering_started_(0) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000365 ASSERT(session_);
366}
367
368StatsCollector::~StatsCollector() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000369 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000372double StatsCollector::GetTimeNow() {
373 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
374}
375
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376// Adds a MediaStream with tracks that can be used as a |selector| in a call
377// to GetStats.
378void StatsCollector::AddStream(MediaStreamInterface* stream) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000379 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 ASSERT(stream != NULL);
381
382 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
383 &reports_);
384 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
385 &reports_);
386}
387
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000388void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
389 uint32 ssrc) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000390 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000391 ASSERT(audio_track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000392 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
393 it != local_audio_tracks_.end(); ++it) {
394 ASSERT(it->first != audio_track || it->second != ssrc);
395 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000396
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000397 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000398
399 // Create the kStatsReportTypeTrack report for the new track if there is no
400 // report yet.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000401 rtc::scoped_ptr<StatsReport::Id> id(
402 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
403 audio_track->id()));
404 StatsReport* report = reports_.Find(*id.get());
405 if (!report) {
406 report = reports_.InsertNew(id.Pass());
407 report->AddValue(StatsReport::kStatsValueNameTrackId, audio_track->id());
408 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000409}
410
411void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
412 uint32 ssrc) {
413 ASSERT(audio_track != NULL);
414 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
415 it != local_audio_tracks_.end(); ++it) {
416 if (it->first == audio_track && it->second == ssrc) {
417 local_audio_tracks_.erase(it);
418 return;
419 }
420 }
421
422 ASSERT(false);
423}
424
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000425void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 StatsReports* reports) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000427 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 ASSERT(reports != NULL);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000429 ASSERT(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000432 reports->reserve(reports_.size());
433 for (auto* r : reports_)
434 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000435 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 }
437
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000438 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
439 StatsReport::kStatsReportTypeSession, session_->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000440 if (report)
441 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000443 report = reports_.Find(StatsReport::NewTypedId(
444 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000446 if (!report)
447 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000449 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450
451 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000452 for (const auto* r : reports_) {
453 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000455
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000456 const StatsReport::Value* v =
457 r->FindValue(StatsReport::kStatsValueNameTrackId);
458 if (v && v->value == track->id())
459 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461}
462
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000463void
464StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000465 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 double time_now = GetTimeNow();
467 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
468 // ms apart will be ignored.
469 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000470 if (stats_gathering_started_ != 0 &&
471 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 return;
473 }
474 stats_gathering_started_ = time_now;
475
476 if (session_) {
477 ExtractSessionInfo();
478 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000479 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000480 ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 }
482}
483
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000484StatsReport* StatsCollector::PrepareReport(
485 bool local,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000486 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000487 const std::string& transport_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000488 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000489 ASSERT(session_->signaling_thread()->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000490 const std::string ssrc_id = rtc::ToString<uint32>(ssrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000491 rtc::scoped_ptr<StatsReport::Id> id(StatsReport::NewIdWithDirection(
492 local ? StatsReport::kStatsReportTypeSsrc :
493 StatsReport::kStatsReportTypeRemoteSsrc,
494 ssrc_id, direction));
495 StatsReport* report = reports_.Find(*id.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496
xians@webrtc.org01bda202014-07-09 07:38:38 +0000497 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000499 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000500 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000501 // The ssrc is not used by any track or existing report, return NULL
502 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000504 }
505
506 // The ssrc is not used by any existing track. Keeps the old track id
507 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000508 const StatsReport::Value* v =
509 report->FindValue(StatsReport::kStatsValueNameTrackId);
510 if (v)
511 track_id = v->value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 }
513
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000514 if (!report) {
515 report = reports_.InsertNew(id.Pass());
516 } else {
517 // Clear out stats from previous GatherStats calls if any.
518 // This is required since the report will be returned for the new values.
519 // Having the old values in the report will lead to multiple values with
520 // the same name.
521 // TODO(tommi): This seems to be pretty wasteful if some of these values
522 // have not changed (we basically throw them away just to recreate them).
523 // Figure out a way to not have to do this while not breaking the existing
524 // functionality.
525 report->ResetValues();
526 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000528 ASSERT(report->values().empty());
529 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000530 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
532 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
533 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
534 // Add the mapping of SSRC to transport.
535 report->AddValue(StatsReport::kStatsValueNameTransportId,
536 transport_id);
537 return report;
538}
539
wu@webrtc.org4551b792013-10-09 15:37:36 +0000540std::string StatsCollector::AddOneCertificateReport(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000541 const rtc::SSLCertificate* cert, const std::string& issuer_id) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000542 ASSERT(session_->signaling_thread()->IsCurrent());
543
wu@webrtc.org4551b792013-10-09 15:37:36 +0000544 // TODO(bemasc): Move this computation to a helper class that caches these
545 // values to reduce CPU use in GetStats. This will require adding a fast
546 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000547
548 std::string digest_algorithm;
549 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
550 return std::string();
551
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
553 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000554
555 // SSLFingerprint::Create can fail if the algorithm returned by
556 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
557 // implementation of SSLCertificate::ComputeDigest. This currently happens
558 // with MD5- and SHA-224-signed certificates when linked to libNSS.
559 if (!ssl_fingerprint)
560 return std::string();
561
wu@webrtc.org4551b792013-10-09 15:37:36 +0000562 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
563
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000564 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000565 cert->ToDER(&der_buffer);
566 std::string der_base64;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000567 rtc::Base64::EncodeFromArray(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000568 der_buffer.data(), der_buffer.length(), &der_base64);
569
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000570 rtc::scoped_ptr<StatsReport::Id> id(
571 StatsReport::NewTypedId(
572 StatsReport::kStatsReportTypeCertificate, fingerprint));
573 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000574 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000575 report->AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
576 report->AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
577 digest_algorithm);
578 report->AddValue(StatsReport::kStatsValueNameDer, der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000579 if (!issuer_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000580 report->AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000581 // TODO(tommi): Can we avoid this?
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000582 return report->id().ToString();
wu@webrtc.org4551b792013-10-09 15:37:36 +0000583}
584
585std::string StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000586 const rtc::SSLCertificate* cert) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000587 ASSERT(session_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000588 // Produces a chain of StatsReports representing this certificate and the rest
589 // of its chain, and adds those reports to |reports_|. The return value is
590 // the id of the leaf report. The provided cert must be non-null, so at least
591 // one report will always be provided and the returned string will never be
592 // empty.
593 ASSERT(cert != NULL);
594
595 std::string issuer_id;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000596 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000597 if (cert->GetChain(chain.accept())) {
598 // This loop runs in reverse, i.e. from root to leaf, so that each
599 // certificate's issuer's report ID is known before the child certificate's
600 // report is generated. The root certificate does not have an issuer ID
601 // value.
602 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 const rtc::SSLCertificate& cert_i = chain->Get(i);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000604 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
605 }
606 }
607 // Add the leaf certificate.
608 return AddOneCertificateReport(cert, issuer_id);
609}
610
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000611std::string StatsCollector::AddCandidateReport(
612 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000613 bool local) {
614 scoped_ptr<StatsReport::Id> id(
615 StatsReport::NewCandidateId(local, candidate.id()));
616 StatsReport* report = reports_.Find(*id.get());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000617 if (!report) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000618 report = reports_.InsertNew(id.Pass());
619 report->set_timestamp(stats_gathering_started_);
620 if (local) {
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000621 report->AddValue(StatsReport::kStatsValueNameCandidateNetworkType,
622 AdapterTypeToStatsType(candidate.network_type()));
623 }
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000624 report->AddValue(StatsReport::kStatsValueNameCandidateIPAddress,
625 candidate.address().ipaddr().ToString());
626 report->AddValue(StatsReport::kStatsValueNameCandidatePortNumber,
627 candidate.address().PortAsString());
628 report->AddValue(StatsReport::kStatsValueNameCandidatePriority,
629 candidate.priority());
630 report->AddValue(StatsReport::kStatsValueNameCandidateType,
631 IceCandidateTypeToStatsType(candidate.type()));
632 report->AddValue(StatsReport::kStatsValueNameCandidateTransportType,
633 candidate.protocol());
634 }
635
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000636 // TODO(tommi): Necessary?
637 return report->id().ToString();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000638}
639
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640void StatsCollector::ExtractSessionInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000641 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 // Extract information from the base session.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000643 rtc::scoped_ptr<StatsReport::Id> id(
644 StatsReport::NewTypedId(
645 StatsReport::kStatsReportTypeSession, session_->id()));
646 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000647 report->set_timestamp(stats_gathering_started_);
648 report->ResetValues();
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000649 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
650 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
652 cricket::SessionStats stats;
653 if (session_->GetStats(&stats)) {
654 // Store the proxy map away for use in SSRC reporting.
655 proxy_to_transport_ = stats.proxy_to_transport;
656
657 for (cricket::TransportStatsMap::iterator transport_iter
658 = stats.transport_stats.begin();
659 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000660 // Attempt to get a copy of the certificates from the transport and
661 // expose them in stats reports. All channels in a transport share the
662 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000663 //
664 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
665 // invoke method calls on the worker thread and block this thread, but
666 // messages are still processed on this thread, which may blow way the
667 // existing transports. So we cannot reuse |transport| after these calls.
wu@webrtc.org4551b792013-10-09 15:37:36 +0000668 std::string local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000669
wu@webrtc.org4551b792013-10-09 15:37:36 +0000670 cricket::Transport* transport =
671 session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672 rtc::scoped_ptr<rtc::SSLIdentity> identity;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000673 if (transport && transport->GetIdentity(identity.accept())) {
674 local_cert_report_id =
675 AddCertificateReports(&(identity->certificate()));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000676 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000677
678 transport = session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000679 rtc::scoped_ptr<rtc::SSLCertificate> cert;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000680 if (transport && transport->GetRemoteCertificate(cert.accept())) {
681 remote_cert_report_id = AddCertificateReports(cert.get());
682 }
683
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 for (cricket::TransportChannelStatsList::iterator channel_iter
685 = transport_iter->second.channel_stats.begin();
686 channel_iter != transport_iter->second.channel_stats.end();
687 ++channel_iter) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000688 rtc::scoped_ptr<StatsReport::Id> id(
689 StatsReport::NewComponentId(transport_iter->second.content_name,
690 channel_iter->component));
691 StatsReport* channel_report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000692 channel_report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000693 channel_report->AddValue(StatsReport::kStatsValueNameComponent,
694 channel_iter->component);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000695 if (!local_cert_report_id.empty()) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000696 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000697 StatsReport::kStatsValueNameLocalCertificateId,
698 local_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000699 }
700 if (!remote_cert_report_id.empty()) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000701 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000702 StatsReport::kStatsValueNameRemoteCertificateId,
703 remote_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000704 }
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000705 const std::string& srtp_cipher = channel_iter->srtp_cipher;
706 if (!srtp_cipher.empty()) {
707 channel_report->AddValue(
708 StatsReport::kStatsValueNameSrtpCipher,
709 srtp_cipher);
710 }
711 const std::string& ssl_cipher = channel_iter->ssl_cipher;
712 if (!ssl_cipher.empty()) {
713 channel_report->AddValue(
714 StatsReport::kStatsValueNameDtlsCipher,
715 ssl_cipher);
716 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 for (size_t i = 0;
718 i < channel_iter->connection_infos.size();
719 ++i) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000720 rtc::scoped_ptr<StatsReport::Id> id(
721 StatsReport::NewCandidatePairId(transport_iter->first,
722 channel_iter->component, static_cast<int>(i)));
723 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000724 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 // Link from connection to its containing channel.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000726 // TODO(tommi): Any way to avoid ToString here?
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000727 report->AddValue(StatsReport::kStatsValueNameChannelId,
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000728 channel_report->id().ToString());
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000729
730 const cricket::ConnectionInfo& info =
731 channel_iter->connection_infos[i];
732 report->AddValue(StatsReport::kStatsValueNameBytesSent,
733 info.sent_total_bytes);
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000734 report->AddValue(StatsReport::kStatsValueNameSendPacketsDiscarded,
735 info.sent_discarded_packets);
736 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
737 info.sent_total_packets);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000738 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
739 info.recv_total_bytes);
740 report->AddBoolean(StatsReport::kStatsValueNameWritable,
741 info.writable);
742 report->AddBoolean(StatsReport::kStatsValueNameReadable,
743 info.readable);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000744 report->AddValue(StatsReport::kStatsValueNameLocalCandidateId,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000745 AddCandidateReport(info.local_candidate, true));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000746 report->AddValue(
747 StatsReport::kStatsValueNameRemoteCandidateId,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000748 AddCandidateReport(info.remote_candidate, false));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000749 report->AddValue(StatsReport::kStatsValueNameLocalAddress,
750 info.local_candidate.address().ToString());
751 report->AddValue(StatsReport::kStatsValueNameRemoteAddress,
752 info.remote_candidate.address().ToString());
753 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
754 report->AddValue(StatsReport::kStatsValueNameTransportType,
755 info.local_candidate.protocol());
756 report->AddValue(StatsReport::kStatsValueNameLocalCandidateType,
757 info.local_candidate.type());
758 report->AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
759 info.remote_candidate.type());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000760 report->AddBoolean(StatsReport::kStatsValueNameActiveConnection,
761 info.best_connection);
762 if (info.best_connection) {
763 channel_report->AddValue(
764 StatsReport::kStatsValueNameSelectedCandidatePairId,
765 report->id().ToString());
766 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
768 }
769 }
770 }
771}
772
773void StatsCollector::ExtractVoiceInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000774 ASSERT(session_->signaling_thread()->IsCurrent());
775
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 if (!session_->voice_channel()) {
777 return;
778 }
779 cricket::VoiceMediaInfo voice_info;
780 if (!session_->voice_channel()->GetStats(&voice_info)) {
781 LOG(LS_ERROR) << "Failed to get voice channel stats.";
782 return;
783 }
784 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000785 if (!GetTransportIdFromProxy(proxy_to_transport_,
786 session_->voice_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 &transport_id)) {
788 LOG(LS_ERROR) << "Failed to get transport name for proxy "
789 << session_->voice_channel()->content_name();
790 return;
791 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000792 ExtractStatsFromList(voice_info.receivers, transport_id, this,
793 StatsReport::kReceive);
794 ExtractStatsFromList(voice_info.senders, transport_id, this,
795 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000796
797 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798}
799
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000800void StatsCollector::ExtractVideoInfo(
801 PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000802 ASSERT(session_->signaling_thread()->IsCurrent());
803
804 if (!session_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000806
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 cricket::VideoMediaInfo video_info;
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000808 if (!session_->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 LOG(LS_ERROR) << "Failed to get video channel stats.";
810 return;
811 }
812 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000813 if (!GetTransportIdFromProxy(proxy_to_transport_,
814 session_->video_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 &transport_id)) {
816 LOG(LS_ERROR) << "Failed to get transport name for proxy "
817 << session_->video_channel()->content_name();
818 return;
819 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000820 ExtractStatsFromList(video_info.receivers, transport_id, this,
821 StatsReport::kReceive);
822 ExtractStatsFromList(video_info.senders, transport_id, this,
823 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 if (video_info.bw_estimations.size() != 1) {
825 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
826 } else {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000827 rtc::scoped_ptr<StatsReport::Id> report_id(
828 StatsReport::NewBandwidthEstimationId());
829 StatsReport* report = reports_.FindOrAddNew(report_id.Pass());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000831 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833}
834
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000835void StatsCollector::ExtractDataInfo() {
836 ASSERT(session_->signaling_thread()->IsCurrent());
837
838 for (const auto& dc :
839 session_->mediastream_signaling()->sctp_data_channels()) {
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000840 rtc::scoped_ptr<StatsReport::Id> id(StatsReport::NewTypedIntId(
841 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000842 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000843 report->set_timestamp(stats_gathering_started_);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000844 report->AddValue(StatsReport::kStatsValueNameLabel, dc->label());
845 report->AddValue(StatsReport::kStatsValueNameDataChannelId, dc->id());
846 report->AddValue(StatsReport::kStatsValueNameProtocol, dc->protocol());
847 report->AddValue(StatsReport::kStatsValueNameState,
848 DataChannelInterface::DataStateString(dc->state()));
849 }
850}
851
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000852StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000853 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000854 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000855 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000856 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
857 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000858 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000859}
860
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000861StatsReport* StatsCollector::GetOrCreateReport(
862 const StatsReport::StatsType& type,
863 const std::string& id,
864 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000865 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000866 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
867 type == StatsReport::kStatsReportTypeRemoteSsrc);
868 StatsReport* report = GetReport(type, id, direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000869 if (report == NULL) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000870 rtc::scoped_ptr<StatsReport::Id> report_id(
871 StatsReport::NewIdWithDirection(type, id, direction));
872 report = reports_.InsertNew(report_id.Pass());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000873 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000874
wu@webrtc.org97077a32013-10-25 21:18:33 +0000875 return report;
876}
877
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000878void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000879 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000880 // Loop through the existing local audio tracks.
881 for (LocalAudioTrackVector::const_iterator it = local_audio_tracks_.begin();
882 it != local_audio_tracks_.end(); ++it) {
883 AudioTrackInterface* track = it->first;
884 uint32 ssrc = it->second;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000885 std::string ssrc_id = rtc::ToString<uint32>(ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000886 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000887 ssrc_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000888 StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000889 if (report == NULL) {
890 // This can happen if a local audio track is added to a stream on the
891 // fly and the report has not been set up yet. Do nothing in this case.
892 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
893 continue;
894 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000895
896 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000897 const StatsReport::Value* v =
898 report->FindValue(StatsReport::kStatsValueNameTrackId);
899 if (!v || v->value != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000900 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000901
902 UpdateReportFromAudioTrack(track, report);
903 }
904}
905
906void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
907 StatsReport* report) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000908 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000909 ASSERT(track != NULL);
910 if (report == NULL)
911 return;
912
913 int signal_level = 0;
914 if (track->GetSignalLevel(&signal_level)) {
915 report->ReplaceValue(StatsReport::kStatsValueNameAudioInputLevel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000916 rtc::ToString<int>(signal_level));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000917 }
918
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000919 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000920 track->GetAudioProcessor());
921 if (audio_processor.get() == NULL)
922 return;
923
924 AudioProcessorInterface::AudioProcessorStats stats;
925 audio_processor->GetStats(&stats);
926 report->ReplaceValue(StatsReport::kStatsValueNameTypingNoiseState,
927 stats.typing_noise_detected ? "true" : "false");
928 report->ReplaceValue(StatsReport::kStatsValueNameEchoReturnLoss,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000929 rtc::ToString<int>(stats.echo_return_loss));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000930 report->ReplaceValue(
931 StatsReport::kStatsValueNameEchoReturnLossEnhancement,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000932 rtc::ToString<int>(stats.echo_return_loss_enhancement));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000933 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayMedian,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000934 rtc::ToString<int>(stats.echo_delay_median_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000935 report->ReplaceValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000936 rtc::ToString<float>(stats.aec_quality_min));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000937 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayStdDev,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000938 rtc::ToString<int>(stats.echo_delay_std_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000939}
940
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000941bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000942 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000943 ASSERT(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000944 if (direction == StatsReport::kSend) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000945 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000946 LOG(LS_WARNING) << "The SSRC " << ssrc
947 << " is not associated with a sending track";
948 return false;
949 }
950 } else {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000951 ASSERT(direction == StatsReport::kReceive);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000952 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000953 LOG(LS_WARNING) << "The SSRC " << ssrc
954 << " is not associated with a receiving track";
955 return false;
956 }
957 }
958
959 return true;
960}
961
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000962void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000963 stats_gathering_started_ = 0;
964}
965
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966} // namespace webrtc