blob: a6a2e13ec063d71ff8e983a18272aecc6fb5480c [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));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
117 info.packets_rcvd);
118 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
119 info.packets_lost);
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000120 report->AddValue(StatsReport::kStatsValueNameDecodingCTSG,
121 info.decoding_calls_to_silence_generator);
122 report->AddValue(StatsReport::kStatsValueNameDecodingCTN,
123 info.decoding_calls_to_neteq);
124 report->AddValue(StatsReport::kStatsValueNameDecodingNormal,
125 info.decoding_normal);
126 report->AddValue(StatsReport::kStatsValueNameDecodingPLC,
127 info.decoding_plc);
128 report->AddValue(StatsReport::kStatsValueNameDecodingCNG,
129 info.decoding_cng);
130 report->AddValue(StatsReport::kStatsValueNameDecodingPLCCNG,
131 info.decoding_plc_cng);
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000132 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
133 info.capture_start_ntp_time_ms);
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000134 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135}
136
137void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
138 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
139 info.audio_level);
140 report->AddValue(StatsReport::kStatsValueNameBytesSent,
141 info.bytes_sent);
142 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
143 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000144 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
145 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
147 info.jitter_ms);
148 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
149 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000150 rtc::ToString<float>(info.aec_quality_min));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
152 info.echo_delay_median_ms);
153 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
154 info.echo_delay_std_ms);
155 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
156 info.echo_return_loss);
157 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
158 info.echo_return_loss_enhancement);
159 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000160 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
161 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162}
163
164void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
165 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
166 info.bytes_rcvd);
167 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
168 info.packets_rcvd);
169 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
170 info.packets_lost);
171
172 report->AddValue(StatsReport::kStatsValueNameFirsSent,
173 info.firs_sent);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000174 report->AddValue(StatsReport::kStatsValueNamePlisSent,
175 info.plis_sent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 report->AddValue(StatsReport::kStatsValueNameNacksSent,
177 info.nacks_sent);
178 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
179 info.frame_width);
180 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
181 info.frame_height);
182 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
183 info.framerate_rcvd);
184 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
185 info.framerate_decoded);
186 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
187 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000188
189 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
190 info.decode_ms);
191 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
192 info.max_decode_ms);
193 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
194 info.current_delay_ms);
195 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
196 info.target_delay_ms);
197 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
198 info.jitter_buffer_ms);
199 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
200 info.min_playout_delay_ms);
201 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
202 info.render_delay_ms);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000203
204 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
205 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206}
207
208void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
209 report->AddValue(StatsReport::kStatsValueNameBytesSent,
210 info.bytes_sent);
211 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
212 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000213 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
214 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
216 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
217 info.firs_rcvd);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000218 report->AddValue(StatsReport::kStatsValueNamePlisReceived,
219 info.plis_rcvd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
221 info.nacks_rcvd);
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000222 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
223 info.input_frame_width);
224 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
225 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000227 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000229 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
231 info.framerate_input);
232 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
233 info.framerate_sent);
234 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
235 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000236 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
237 (info.adapt_reason & 0x1) > 0);
238 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
239 (info.adapt_reason & 0x2) > 0);
240 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
241 (info.adapt_reason & 0x4) > 0);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000242 report->AddValue(StatsReport::kStatsValueNameAdaptationChanges,
243 info.adapt_changes);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000244 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
245 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
246 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000247 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
248 info.capture_queue_delay_ms_per_s);
249 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
250 info.encode_usage_percent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251}
252
253void ExtractStats(const cricket::BandwidthEstimationInfo& info,
254 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000255 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 StatsReport* report) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000257 ASSERT(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
259 // Clear out stats from previous GatherStats calls if any.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000260 if (report->timestamp() != stats_gathering_started) {
261 report->ResetValues();
262 report->set_timestamp(stats_gathering_started);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 }
264
265 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
266 info.available_send_bandwidth);
267 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
268 info.available_recv_bandwidth);
269 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
270 info.target_enc_bitrate);
271 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
272 info.actual_enc_bitrate);
273 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
274 info.retransmit_bitrate);
275 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
276 info.transmit_bitrate);
277 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
278 info.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000279 if (level >= PeerConnectionInterface::kStatsOutputLevelDebug) {
280 report->AddValue(
281 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug,
282 info.total_received_propagation_delta_ms);
283 if (info.recent_received_propagation_delta_ms.size() > 0) {
284 report->AddValue(
285 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug,
286 info.recent_received_propagation_delta_ms);
287 report->AddValue(
288 StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug,
289 info.recent_received_packet_group_arrival_time_ms);
290 }
291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292}
293
wu@webrtc.org97077a32013-10-25 21:18:33 +0000294void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
295 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000296 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000297 // TODO(hta): Extract some stats here.
298}
299
300void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
301 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000302 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000303 // TODO(hta): Extract some stats here.
304}
305
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000307// In order to use the template, the functions that are called from it,
308// ExtractStats and ExtractRemoteStats, must be defined and overloaded
309// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310template<typename T>
311void ExtractStatsFromList(const std::vector<T>& data,
312 const std::string& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000313 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000314 StatsReport::Direction direction) {
315 for (const auto& d : data) {
316 uint32 ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000317 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000318 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000319 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
320 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000321 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000322 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000323
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000324 if (!d.remote_stats.empty()) {
325 report = collector->PrepareReport(false, ssrc, transport_id, direction);
326 if (report)
327 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000328 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000330}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331
332} // namespace
333
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000334const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
335 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
336 return STATSREPORT_LOCAL_PORT_TYPE;
337 }
338 if (candidate_type == cricket::STUN_PORT_TYPE) {
339 return STATSREPORT_STUN_PORT_TYPE;
340 }
341 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
342 return STATSREPORT_PRFLX_PORT_TYPE;
343 }
344 if (candidate_type == cricket::RELAY_PORT_TYPE) {
345 return STATSREPORT_RELAY_PORT_TYPE;
346 }
347 ASSERT(false);
348 return "unknown";
349}
350
351const char* AdapterTypeToStatsType(rtc::AdapterType type) {
352 switch (type) {
353 case rtc::ADAPTER_TYPE_UNKNOWN:
354 return "unknown";
355 case rtc::ADAPTER_TYPE_ETHERNET:
356 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
357 case rtc::ADAPTER_TYPE_WIFI:
358 return STATSREPORT_ADAPTER_TYPE_WIFI;
359 case rtc::ADAPTER_TYPE_CELLULAR:
360 return STATSREPORT_ADAPTER_TYPE_WWAN;
361 case rtc::ADAPTER_TYPE_VPN:
362 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000363 case rtc::ADAPTER_TYPE_LOOPBACK:
364 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000365 default:
366 ASSERT(false);
367 return "";
368 }
369}
370
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000371StatsCollector::StatsCollector(WebRtcSession* session)
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000372 : session_(session),
373 stats_gathering_started_(0) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000374 ASSERT(session_);
375}
376
377StatsCollector::~StatsCollector() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000378 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000381double StatsCollector::GetTimeNow() {
382 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
383}
384
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385// Adds a MediaStream with tracks that can be used as a |selector| in a call
386// to GetStats.
387void StatsCollector::AddStream(MediaStreamInterface* stream) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000388 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 ASSERT(stream != NULL);
390
391 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
392 &reports_);
393 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
394 &reports_);
395}
396
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000397void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
398 uint32 ssrc) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000399 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000400 ASSERT(audio_track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000401 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
402 it != local_audio_tracks_.end(); ++it) {
403 ASSERT(it->first != audio_track || it->second != ssrc);
404 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000405
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000406 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000407
408 // Create the kStatsReportTypeTrack report for the new track if there is no
409 // report yet.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000410 rtc::scoped_ptr<StatsReport::Id> id(
411 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
412 audio_track->id()));
413 StatsReport* report = reports_.Find(*id.get());
414 if (!report) {
415 report = reports_.InsertNew(id.Pass());
416 report->AddValue(StatsReport::kStatsValueNameTrackId, audio_track->id());
417 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000418}
419
420void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
421 uint32 ssrc) {
422 ASSERT(audio_track != NULL);
423 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
424 it != local_audio_tracks_.end(); ++it) {
425 if (it->first == audio_track && it->second == ssrc) {
426 local_audio_tracks_.erase(it);
427 return;
428 }
429 }
430
431 ASSERT(false);
432}
433
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000434void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 StatsReports* reports) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000436 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 ASSERT(reports != NULL);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000438 ASSERT(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000441 reports->reserve(reports_.size());
442 for (auto* r : reports_)
443 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000444 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 }
446
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000447 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
448 StatsReport::kStatsReportTypeSession, session_->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000449 if (report)
450 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000452 report = reports_.Find(StatsReport::NewTypedId(
453 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000455 if (!report)
456 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000458 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459
460 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000461 for (const auto* r : reports_) {
462 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000464
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000465 const StatsReport::Value* v =
466 r->FindValue(StatsReport::kStatsValueNameTrackId);
467 if (v && v->value == track->id())
468 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470}
471
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000472void
473StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000474 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 double time_now = GetTimeNow();
476 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
477 // ms apart will be ignored.
478 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000479 if (stats_gathering_started_ != 0 &&
480 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 return;
482 }
483 stats_gathering_started_ = time_now;
484
485 if (session_) {
486 ExtractSessionInfo();
487 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000488 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000489 ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 }
491}
492
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000493StatsReport* StatsCollector::PrepareReport(
494 bool local,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000495 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000496 const std::string& transport_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000497 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000498 ASSERT(session_->signaling_thread()->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000499 const std::string ssrc_id = rtc::ToString<uint32>(ssrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000500 rtc::scoped_ptr<StatsReport::Id> id(StatsReport::NewIdWithDirection(
501 local ? StatsReport::kStatsReportTypeSsrc :
502 StatsReport::kStatsReportTypeRemoteSsrc,
503 ssrc_id, direction));
504 StatsReport* report = reports_.Find(*id.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505
xians@webrtc.org01bda202014-07-09 07:38:38 +0000506 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000508 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000509 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000510 // The ssrc is not used by any track or existing report, return NULL
511 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000513 }
514
515 // The ssrc is not used by any existing track. Keeps the old track id
516 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000517 const StatsReport::Value* v =
518 report->FindValue(StatsReport::kStatsValueNameTrackId);
519 if (v)
520 track_id = v->value;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 }
522
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000523 if (!report) {
524 report = reports_.InsertNew(id.Pass());
525 } else {
526 // Clear out stats from previous GatherStats calls if any.
527 // This is required since the report will be returned for the new values.
528 // Having the old values in the report will lead to multiple values with
529 // the same name.
530 // TODO(tommi): This seems to be pretty wasteful if some of these values
531 // have not changed (we basically throw them away just to recreate them).
532 // Figure out a way to not have to do this while not breaking the existing
533 // functionality.
534 report->ResetValues();
535 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000537 ASSERT(report->values().empty());
538 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000539 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540
541 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
542 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
543 // Add the mapping of SSRC to transport.
544 report->AddValue(StatsReport::kStatsValueNameTransportId,
545 transport_id);
546 return report;
547}
548
wu@webrtc.org4551b792013-10-09 15:37:36 +0000549std::string StatsCollector::AddOneCertificateReport(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 const rtc::SSLCertificate* cert, const std::string& issuer_id) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000551 ASSERT(session_->signaling_thread()->IsCurrent());
552
wu@webrtc.org4551b792013-10-09 15:37:36 +0000553 // TODO(bemasc): Move this computation to a helper class that caches these
554 // values to reduce CPU use in GetStats. This will require adding a fast
555 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000556
557 std::string digest_algorithm;
558 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
559 return std::string();
560
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
562 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000563
564 // SSLFingerprint::Create can fail if the algorithm returned by
565 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
566 // implementation of SSLCertificate::ComputeDigest. This currently happens
567 // with MD5- and SHA-224-signed certificates when linked to libNSS.
568 if (!ssl_fingerprint)
569 return std::string();
570
wu@webrtc.org4551b792013-10-09 15:37:36 +0000571 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
572
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000573 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000574 cert->ToDER(&der_buffer);
575 std::string der_base64;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 rtc::Base64::EncodeFromArray(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000577 der_buffer.data(), der_buffer.length(), &der_base64);
578
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000579 rtc::scoped_ptr<StatsReport::Id> id(
580 StatsReport::NewTypedId(
581 StatsReport::kStatsReportTypeCertificate, fingerprint));
582 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000583 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000584 report->AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
585 report->AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
586 digest_algorithm);
587 report->AddValue(StatsReport::kStatsValueNameDer, der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000588 if (!issuer_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000589 report->AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000590 // TODO(tommi): Can we avoid this?
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000591 return report->id().ToString();
wu@webrtc.org4551b792013-10-09 15:37:36 +0000592}
593
594std::string StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 const rtc::SSLCertificate* cert) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000596 ASSERT(session_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000597 // Produces a chain of StatsReports representing this certificate and the rest
598 // of its chain, and adds those reports to |reports_|. The return value is
599 // the id of the leaf report. The provided cert must be non-null, so at least
600 // one report will always be provided and the returned string will never be
601 // empty.
602 ASSERT(cert != NULL);
603
604 std::string issuer_id;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000605 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000606 if (cert->GetChain(chain.accept())) {
607 // This loop runs in reverse, i.e. from root to leaf, so that each
608 // certificate's issuer's report ID is known before the child certificate's
609 // report is generated. The root certificate does not have an issuer ID
610 // value.
611 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000612 const rtc::SSLCertificate& cert_i = chain->Get(i);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000613 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
614 }
615 }
616 // Add the leaf certificate.
617 return AddOneCertificateReport(cert, issuer_id);
618}
619
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000620std::string StatsCollector::AddCandidateReport(
621 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000622 bool local) {
623 scoped_ptr<StatsReport::Id> id(
624 StatsReport::NewCandidateId(local, candidate.id()));
625 StatsReport* report = reports_.Find(*id.get());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000626 if (!report) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000627 report = reports_.InsertNew(id.Pass());
628 report->set_timestamp(stats_gathering_started_);
629 if (local) {
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000630 report->AddValue(StatsReport::kStatsValueNameCandidateNetworkType,
631 AdapterTypeToStatsType(candidate.network_type()));
632 }
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000633 report->AddValue(StatsReport::kStatsValueNameCandidateIPAddress,
634 candidate.address().ipaddr().ToString());
635 report->AddValue(StatsReport::kStatsValueNameCandidatePortNumber,
636 candidate.address().PortAsString());
637 report->AddValue(StatsReport::kStatsValueNameCandidatePriority,
638 candidate.priority());
639 report->AddValue(StatsReport::kStatsValueNameCandidateType,
640 IceCandidateTypeToStatsType(candidate.type()));
641 report->AddValue(StatsReport::kStatsValueNameCandidateTransportType,
642 candidate.protocol());
643 }
644
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000645 // TODO(tommi): Necessary?
646 return report->id().ToString();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000647}
648
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649void StatsCollector::ExtractSessionInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000650 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 // Extract information from the base session.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000652 rtc::scoped_ptr<StatsReport::Id> id(
653 StatsReport::NewTypedId(
654 StatsReport::kStatsReportTypeSession, session_->id()));
655 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000656 report->set_timestamp(stats_gathering_started_);
657 report->ResetValues();
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000658 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
659 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660
661 cricket::SessionStats stats;
662 if (session_->GetStats(&stats)) {
663 // Store the proxy map away for use in SSRC reporting.
664 proxy_to_transport_ = stats.proxy_to_transport;
665
666 for (cricket::TransportStatsMap::iterator transport_iter
667 = stats.transport_stats.begin();
668 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000669 // Attempt to get a copy of the certificates from the transport and
670 // expose them in stats reports. All channels in a transport share the
671 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000672 //
673 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
674 // invoke method calls on the worker thread and block this thread, but
675 // messages are still processed on this thread, which may blow way the
676 // existing transports. So we cannot reuse |transport| after these calls.
wu@webrtc.org4551b792013-10-09 15:37:36 +0000677 std::string local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000678
wu@webrtc.org4551b792013-10-09 15:37:36 +0000679 cricket::Transport* transport =
680 session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000681 rtc::scoped_ptr<rtc::SSLIdentity> identity;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000682 if (transport && transport->GetIdentity(identity.accept())) {
683 local_cert_report_id =
684 AddCertificateReports(&(identity->certificate()));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000685 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000686
687 transport = session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000688 rtc::scoped_ptr<rtc::SSLCertificate> cert;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000689 if (transport && transport->GetRemoteCertificate(cert.accept())) {
690 remote_cert_report_id = AddCertificateReports(cert.get());
691 }
692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 for (cricket::TransportChannelStatsList::iterator channel_iter
694 = transport_iter->second.channel_stats.begin();
695 channel_iter != transport_iter->second.channel_stats.end();
696 ++channel_iter) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000697 rtc::scoped_ptr<StatsReport::Id> id(
698 StatsReport::NewComponentId(transport_iter->second.content_name,
699 channel_iter->component));
700 StatsReport* channel_report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000701 channel_report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000702 channel_report->AddValue(StatsReport::kStatsValueNameComponent,
703 channel_iter->component);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000704 if (!local_cert_report_id.empty()) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000705 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000706 StatsReport::kStatsValueNameLocalCertificateId,
707 local_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000708 }
709 if (!remote_cert_report_id.empty()) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000710 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000711 StatsReport::kStatsValueNameRemoteCertificateId,
712 remote_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000713 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 for (size_t i = 0;
715 i < channel_iter->connection_infos.size();
716 ++i) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000717 rtc::scoped_ptr<StatsReport::Id> id(
718 StatsReport::NewCandidatePairId(transport_iter->first,
719 channel_iter->component, static_cast<int>(i)));
720 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000721 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 // Link from connection to its containing channel.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000723 // TODO(tommi): Any way to avoid ToString here?
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000724 report->AddValue(StatsReport::kStatsValueNameChannelId,
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000725 channel_report->id().ToString());
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000726
727 const cricket::ConnectionInfo& info =
728 channel_iter->connection_infos[i];
729 report->AddValue(StatsReport::kStatsValueNameBytesSent,
730 info.sent_total_bytes);
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000731 report->AddValue(StatsReport::kStatsValueNameSendPacketsDiscarded,
732 info.sent_discarded_packets);
733 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
734 info.sent_total_packets);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000735 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
736 info.recv_total_bytes);
737 report->AddBoolean(StatsReport::kStatsValueNameWritable,
738 info.writable);
739 report->AddBoolean(StatsReport::kStatsValueNameReadable,
740 info.readable);
741 report->AddBoolean(StatsReport::kStatsValueNameActiveConnection,
742 info.best_connection);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000743 report->AddValue(StatsReport::kStatsValueNameLocalCandidateId,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000744 AddCandidateReport(info.local_candidate, true));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000745 report->AddValue(
746 StatsReport::kStatsValueNameRemoteCandidateId,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000747 AddCandidateReport(info.remote_candidate, false));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000748 report->AddValue(StatsReport::kStatsValueNameLocalAddress,
749 info.local_candidate.address().ToString());
750 report->AddValue(StatsReport::kStatsValueNameRemoteAddress,
751 info.remote_candidate.address().ToString());
752 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
753 report->AddValue(StatsReport::kStatsValueNameTransportType,
754 info.local_candidate.protocol());
755 report->AddValue(StatsReport::kStatsValueNameLocalCandidateType,
756 info.local_candidate.type());
757 report->AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
758 info.remote_candidate.type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 }
761 }
762 }
763}
764
765void StatsCollector::ExtractVoiceInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000766 ASSERT(session_->signaling_thread()->IsCurrent());
767
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 if (!session_->voice_channel()) {
769 return;
770 }
771 cricket::VoiceMediaInfo voice_info;
772 if (!session_->voice_channel()->GetStats(&voice_info)) {
773 LOG(LS_ERROR) << "Failed to get voice channel stats.";
774 return;
775 }
776 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000777 if (!GetTransportIdFromProxy(proxy_to_transport_,
778 session_->voice_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 &transport_id)) {
780 LOG(LS_ERROR) << "Failed to get transport name for proxy "
781 << session_->voice_channel()->content_name();
782 return;
783 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000784 ExtractStatsFromList(voice_info.receivers, transport_id, this,
785 StatsReport::kReceive);
786 ExtractStatsFromList(voice_info.senders, transport_id, this,
787 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000788
789 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790}
791
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000792void StatsCollector::ExtractVideoInfo(
793 PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000794 ASSERT(session_->signaling_thread()->IsCurrent());
795
796 if (!session_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000798
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000799 cricket::StatsOptions options;
800 options.include_received_propagation_stats =
801 (level >= PeerConnectionInterface::kStatsOutputLevelDebug) ?
802 true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 cricket::VideoMediaInfo video_info;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000804 if (!session_->video_channel()->GetStats(options, &video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 LOG(LS_ERROR) << "Failed to get video channel stats.";
806 return;
807 }
808 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000809 if (!GetTransportIdFromProxy(proxy_to_transport_,
810 session_->video_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 &transport_id)) {
812 LOG(LS_ERROR) << "Failed to get transport name for proxy "
813 << session_->video_channel()->content_name();
814 return;
815 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000816 ExtractStatsFromList(video_info.receivers, transport_id, this,
817 StatsReport::kReceive);
818 ExtractStatsFromList(video_info.senders, transport_id, this,
819 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 if (video_info.bw_estimations.size() != 1) {
821 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
822 } else {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000823 rtc::scoped_ptr<StatsReport::Id> report_id(
824 StatsReport::NewBandwidthEstimationId());
825 StatsReport* report = reports_.FindOrAddNew(report_id.Pass());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000827 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 }
829}
830
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000831void StatsCollector::ExtractDataInfo() {
832 ASSERT(session_->signaling_thread()->IsCurrent());
833
834 for (const auto& dc :
835 session_->mediastream_signaling()->sctp_data_channels()) {
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000836 rtc::scoped_ptr<StatsReport::Id> id(StatsReport::NewTypedIntId(
837 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000838 StatsReport* report = reports_.ReplaceOrAddNew(id.Pass());
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000839 report->set_timestamp(stats_gathering_started_);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000840 report->AddValue(StatsReport::kStatsValueNameLabel, dc->label());
841 report->AddValue(StatsReport::kStatsValueNameDataChannelId, dc->id());
842 report->AddValue(StatsReport::kStatsValueNameProtocol, dc->protocol());
843 report->AddValue(StatsReport::kStatsValueNameState,
844 DataChannelInterface::DataStateString(dc->state()));
845 }
846}
847
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000848StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000849 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000850 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000851 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000852 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
853 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000854 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000855}
856
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000857StatsReport* StatsCollector::GetOrCreateReport(
858 const StatsReport::StatsType& type,
859 const std::string& id,
860 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000861 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000862 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
863 type == StatsReport::kStatsReportTypeRemoteSsrc);
864 StatsReport* report = GetReport(type, id, direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000865 if (report == NULL) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000866 rtc::scoped_ptr<StatsReport::Id> report_id(
867 StatsReport::NewIdWithDirection(type, id, direction));
868 report = reports_.InsertNew(report_id.Pass());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000869 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000870
wu@webrtc.org97077a32013-10-25 21:18:33 +0000871 return report;
872}
873
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000874void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000875 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000876 // Loop through the existing local audio tracks.
877 for (LocalAudioTrackVector::const_iterator it = local_audio_tracks_.begin();
878 it != local_audio_tracks_.end(); ++it) {
879 AudioTrackInterface* track = it->first;
880 uint32 ssrc = it->second;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000881 std::string ssrc_id = rtc::ToString<uint32>(ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000882 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000883 ssrc_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000884 StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000885 if (report == NULL) {
886 // This can happen if a local audio track is added to a stream on the
887 // fly and the report has not been set up yet. Do nothing in this case.
888 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
889 continue;
890 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000891
892 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000893 const StatsReport::Value* v =
894 report->FindValue(StatsReport::kStatsValueNameTrackId);
895 if (!v || v->value != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000897
898 UpdateReportFromAudioTrack(track, report);
899 }
900}
901
902void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
903 StatsReport* report) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000904 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000905 ASSERT(track != NULL);
906 if (report == NULL)
907 return;
908
909 int signal_level = 0;
910 if (track->GetSignalLevel(&signal_level)) {
911 report->ReplaceValue(StatsReport::kStatsValueNameAudioInputLevel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000912 rtc::ToString<int>(signal_level));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000913 }
914
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000915 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000916 track->GetAudioProcessor());
917 if (audio_processor.get() == NULL)
918 return;
919
920 AudioProcessorInterface::AudioProcessorStats stats;
921 audio_processor->GetStats(&stats);
922 report->ReplaceValue(StatsReport::kStatsValueNameTypingNoiseState,
923 stats.typing_noise_detected ? "true" : "false");
924 report->ReplaceValue(StatsReport::kStatsValueNameEchoReturnLoss,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000925 rtc::ToString<int>(stats.echo_return_loss));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000926 report->ReplaceValue(
927 StatsReport::kStatsValueNameEchoReturnLossEnhancement,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000928 rtc::ToString<int>(stats.echo_return_loss_enhancement));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000929 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayMedian,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000930 rtc::ToString<int>(stats.echo_delay_median_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000931 report->ReplaceValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000932 rtc::ToString<float>(stats.aec_quality_min));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000933 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayStdDev,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000934 rtc::ToString<int>(stats.echo_delay_std_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000935}
936
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000937bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000938 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000939 ASSERT(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000940 if (direction == StatsReport::kSend) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000941 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000942 LOG(LS_WARNING) << "The SSRC " << ssrc
943 << " is not associated with a sending track";
944 return false;
945 }
946 } else {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000947 ASSERT(direction == StatsReport::kReceive);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000948 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000949 LOG(LS_WARNING) << "The SSRC " << ssrc
950 << " is not associated with a receiving track";
951 return false;
952 }
953 }
954
955 return true;
956}
957
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000958void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000959 stats_gathering_started_ = 0;
960}
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962} // namespace webrtc