blob: 1e3a1d92b1830ea1b5241cd531217b942aa9d167 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
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
38namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000041// The following is the enum RTCStatsIceCandidateType from
42// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
43// our stats report for ice candidate type could conform to that.
44const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
45const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
46const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
47const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
48
49// Strings used by the stats collector to report adapter types. This fits the
50// general stype of http://w3c.github.io/webrtc-stats than what
51// AdapterTypeToString does.
52const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
53const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
54const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
55const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
56
tommi@webrtc.org47218952014-07-15 19:22:37 +000057double GetTimeNow() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
tommi@webrtc.org47218952014-07-15 19:22:37 +000059}
60
61bool GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
62 const std::string& proxy,
63 std::string* transport) {
64 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
65 if (proxy.empty()) {
66 transport->clear();
67 return true;
68 }
69
70 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
71 if (found == map.end()) {
72 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
73 return false;
74 }
75
76 std::ostringstream ost;
77 // Component 1 is always used for RTP.
78 ost << "Channel-" << found->second << "-1";
79 *transport = ost.str();
80 return true;
81}
82
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083std::string StatsId(const std::string& type, const std::string& id) {
84 return type + "_" + id;
85}
86
xians@webrtc.org4cb01282014-06-12 14:57:05 +000087std::string StatsId(const std::string& type, const std::string& id,
88 StatsCollector::TrackDirection direction) {
89 ASSERT(direction == StatsCollector::kSending ||
90 direction == StatsCollector::kReceiving);
91
92 // Strings for the direction of the track.
93 const char kSendDirection[] = "send";
94 const char kRecvDirection[] = "recv";
95
96 const std::string direction_id = (direction == StatsCollector::kSending) ?
97 kSendDirection : kRecvDirection;
98 return type + "_" + id + "_" + direction_id;
99}
100
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101bool ExtractValueFromReport(
102 const StatsReport& report,
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000103 StatsReport::StatsValueName name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 std::string* value) {
105 StatsReport::Values::const_iterator it = report.values.begin();
106 for (; it != report.values.end(); ++it) {
107 if (it->name == name) {
108 *value = it->value;
109 return true;
110 }
111 }
112 return false;
113}
114
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000115void AddTrackReport(StatsSet* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000116 // Adds an empty track report.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000117 StatsReport* report = reports->ReplaceOrAddNew(
118 StatsId(StatsReport::kStatsReportTypeTrack, track_id));
119 report->type = StatsReport::kStatsReportTypeTrack;
120 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
xians@webrtc.org01bda202014-07-09 07:38:38 +0000121}
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123template <class TrackVector>
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000124void CreateTrackReports(const TrackVector& tracks, StatsSet* reports) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 for (size_t j = 0; j < tracks.size(); ++j) {
126 webrtc::MediaStreamTrackInterface* track = tracks[j];
xians@webrtc.org01bda202014-07-09 07:38:38 +0000127 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 }
129}
130
131void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
132 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
133 info.audio_level);
134 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
135 info.bytes_rcvd);
136 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
137 info.jitter_ms);
jiayl@webrtc.org11aab0e2014-03-07 18:56:26 +0000138 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
139 info.jitter_buffer_ms);
140 report->AddValue(StatsReport::kStatsValueNamePreferredJitterBufferMs,
141 info.jitter_buffer_preferred_ms);
142 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
143 info.delay_estimate_ms);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000144 report->AddValue(StatsReport::kStatsValueNameExpandRate,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000145 rtc::ToString<float>(info.expand_rate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
147 info.packets_rcvd);
148 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
149 info.packets_lost);
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000150 report->AddValue(StatsReport::kStatsValueNameDecodingCTSG,
151 info.decoding_calls_to_silence_generator);
152 report->AddValue(StatsReport::kStatsValueNameDecodingCTN,
153 info.decoding_calls_to_neteq);
154 report->AddValue(StatsReport::kStatsValueNameDecodingNormal,
155 info.decoding_normal);
156 report->AddValue(StatsReport::kStatsValueNameDecodingPLC,
157 info.decoding_plc);
158 report->AddValue(StatsReport::kStatsValueNameDecodingCNG,
159 info.decoding_cng);
160 report->AddValue(StatsReport::kStatsValueNameDecodingPLCCNG,
161 info.decoding_plc_cng);
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000162 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
163 info.capture_start_ntp_time_ms);
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000164 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165}
166
167void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
168 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
169 info.audio_level);
170 report->AddValue(StatsReport::kStatsValueNameBytesSent,
171 info.bytes_sent);
172 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
173 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000174 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
175 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
177 info.jitter_ms);
178 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
179 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000180 rtc::ToString<float>(info.aec_quality_min));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
182 info.echo_delay_median_ms);
183 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
184 info.echo_delay_std_ms);
185 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
186 info.echo_return_loss);
187 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
188 info.echo_return_loss_enhancement);
189 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000190 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
191 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
194void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
195 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
196 info.bytes_rcvd);
197 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
198 info.packets_rcvd);
199 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
200 info.packets_lost);
201
202 report->AddValue(StatsReport::kStatsValueNameFirsSent,
203 info.firs_sent);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000204 report->AddValue(StatsReport::kStatsValueNamePlisSent,
205 info.plis_sent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 report->AddValue(StatsReport::kStatsValueNameNacksSent,
207 info.nacks_sent);
208 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
209 info.frame_width);
210 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
211 info.frame_height);
212 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
213 info.framerate_rcvd);
214 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
215 info.framerate_decoded);
216 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
217 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000218
219 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
220 info.decode_ms);
221 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
222 info.max_decode_ms);
223 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
224 info.current_delay_ms);
225 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
226 info.target_delay_ms);
227 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
228 info.jitter_buffer_ms);
229 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
230 info.min_playout_delay_ms);
231 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
232 info.render_delay_ms);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000233
234 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
235 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236}
237
238void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
239 report->AddValue(StatsReport::kStatsValueNameBytesSent,
240 info.bytes_sent);
241 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
242 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000243 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
244 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245
246 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
247 info.firs_rcvd);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000248 report->AddValue(StatsReport::kStatsValueNamePlisReceived,
249 info.plis_rcvd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
251 info.nacks_rcvd);
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000252 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
253 info.input_frame_width);
254 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
255 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000257 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000259 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
261 info.framerate_input);
262 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
263 info.framerate_sent);
264 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
265 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000266 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
267 (info.adapt_reason & 0x1) > 0);
268 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
269 (info.adapt_reason & 0x2) > 0);
270 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
271 (info.adapt_reason & 0x4) > 0);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000272 report->AddValue(StatsReport::kStatsValueNameAdaptationChanges,
273 info.adapt_changes);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000274 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
275 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
276 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000277 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
278 info.capture_queue_delay_ms_per_s);
279 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
280 info.encode_usage_percent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281}
282
283void ExtractStats(const cricket::BandwidthEstimationInfo& info,
284 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000285 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 StatsReport* report) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000287 ASSERT(report->id == StatsReport::kStatsReportVideoBweId);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 report->type = StatsReport::kStatsReportTypeBwe;
289
290 // Clear out stats from previous GatherStats calls if any.
291 if (report->timestamp != stats_gathering_started) {
292 report->values.clear();
293 report->timestamp = stats_gathering_started;
294 }
295
296 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
297 info.available_send_bandwidth);
298 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
299 info.available_recv_bandwidth);
300 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
301 info.target_enc_bitrate);
302 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
303 info.actual_enc_bitrate);
304 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
305 info.retransmit_bitrate);
306 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
307 info.transmit_bitrate);
308 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
309 info.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000310 if (level >= PeerConnectionInterface::kStatsOutputLevelDebug) {
311 report->AddValue(
312 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug,
313 info.total_received_propagation_delta_ms);
314 if (info.recent_received_propagation_delta_ms.size() > 0) {
315 report->AddValue(
316 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug,
317 info.recent_received_propagation_delta_ms);
318 report->AddValue(
319 StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug,
320 info.recent_received_packet_group_arrival_time_ms);
321 }
322 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323}
324
wu@webrtc.org97077a32013-10-25 21:18:33 +0000325void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
326 StatsReport* report) {
327 report->timestamp = info.remote_stats[0].timestamp;
328 // TODO(hta): Extract some stats here.
329}
330
331void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
332 StatsReport* report) {
333 report->timestamp = info.remote_stats[0].timestamp;
334 // TODO(hta): Extract some stats here.
335}
336
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000338// In order to use the template, the functions that are called from it,
339// ExtractStats and ExtractRemoteStats, must be defined and overloaded
340// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341template<typename T>
342void ExtractStatsFromList(const std::vector<T>& data,
343 const std::string& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000344 StatsCollector* collector,
345 StatsCollector::TrackDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 typename std::vector<T>::const_iterator it = data.begin();
347 for (; it != data.end(); ++it) {
348 std::string id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000349 uint32 ssrc = it->ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000350 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000351 // TODO(hta): Handle the case of multiple SSRCs per object.
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000352 StatsReport* report = collector->PrepareLocalReport(ssrc, transport_id,
353 direction);
354 if (report)
355 ExtractStats(*it, report);
356
wu@webrtc.org97077a32013-10-25 21:18:33 +0000357 if (it->remote_stats.size() > 0) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000358 report = collector->PrepareRemoteReport(ssrc, transport_id,
359 direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000360 if (!report) {
361 continue;
362 }
363 ExtractRemoteStats(*it, report);
364 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000366}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367
368} // namespace
369
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000370const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
371 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
372 return STATSREPORT_LOCAL_PORT_TYPE;
373 }
374 if (candidate_type == cricket::STUN_PORT_TYPE) {
375 return STATSREPORT_STUN_PORT_TYPE;
376 }
377 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
378 return STATSREPORT_PRFLX_PORT_TYPE;
379 }
380 if (candidate_type == cricket::RELAY_PORT_TYPE) {
381 return STATSREPORT_RELAY_PORT_TYPE;
382 }
383 ASSERT(false);
384 return "unknown";
385}
386
387const char* AdapterTypeToStatsType(rtc::AdapterType type) {
388 switch (type) {
389 case rtc::ADAPTER_TYPE_UNKNOWN:
390 return "unknown";
391 case rtc::ADAPTER_TYPE_ETHERNET:
392 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
393 case rtc::ADAPTER_TYPE_WIFI:
394 return STATSREPORT_ADAPTER_TYPE_WIFI;
395 case rtc::ADAPTER_TYPE_CELLULAR:
396 return STATSREPORT_ADAPTER_TYPE_WWAN;
397 case rtc::ADAPTER_TYPE_VPN:
398 return STATSREPORT_ADAPTER_TYPE_VPN;
399 default:
400 ASSERT(false);
401 return "";
402 }
403}
404
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000405StatsCollector::StatsCollector(WebRtcSession* session)
406 : session_(session), stats_gathering_started_(0) {
407 ASSERT(session_);
408}
409
410StatsCollector::~StatsCollector() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000411 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412}
413
414// Adds a MediaStream with tracks that can be used as a |selector| in a call
415// to GetStats.
416void StatsCollector::AddStream(MediaStreamInterface* stream) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000417 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 ASSERT(stream != NULL);
419
420 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
421 &reports_);
422 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
423 &reports_);
424}
425
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000426void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
427 uint32 ssrc) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000428 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000429 ASSERT(audio_track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000430 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
431 it != local_audio_tracks_.end(); ++it) {
432 ASSERT(it->first != audio_track || it->second != ssrc);
433 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000434
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000435 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000436
437 // Create the kStatsReportTypeTrack report for the new track if there is no
438 // report yet.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000439 StatsReport* found = reports_.Find(
xians@webrtc.org01bda202014-07-09 07:38:38 +0000440 StatsId(StatsReport::kStatsReportTypeTrack, audio_track->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000441 if (!found)
xians@webrtc.org01bda202014-07-09 07:38:38 +0000442 AddTrackReport(&reports_, audio_track->id());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000443}
444
445void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
446 uint32 ssrc) {
447 ASSERT(audio_track != NULL);
448 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
449 it != local_audio_tracks_.end(); ++it) {
450 if (it->first == audio_track && it->second == ssrc) {
451 local_audio_tracks_.erase(it);
452 return;
453 }
454 }
455
456 ASSERT(false);
457}
458
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000459void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 StatsReports* reports) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000461 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 ASSERT(reports != NULL);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000463 ASSERT(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 if (!track) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000466 StatsSet::const_iterator it;
467 for (it = reports_.begin(); it != reports_.end(); ++it)
468 reports->push_back(&(*it));
469 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 }
471
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000472 StatsReport* report =
473 reports_.Find(StatsId(StatsReport::kStatsReportTypeSession,
474 session_->id()));
475 if (report)
476 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000478 report = reports_.Find(
479 StatsId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000481 if (!report)
482 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000484 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485
486 std::string track_id;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000487 for (StatsSet::const_iterator it = reports_.begin(); it != reports_.end();
488 ++it) {
489 if (it->type != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000491
492 if (ExtractValueFromReport(*it,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 StatsReport::kStatsValueNameTrackId,
494 &track_id)) {
495 if (track_id == track->id()) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000496 reports->push_back(&(*it));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498 }
499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500}
501
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000502void
503StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000504 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 double time_now = GetTimeNow();
506 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
507 // ms apart will be ignored.
508 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000509 if (stats_gathering_started_ != 0 &&
510 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 return;
512 }
513 stats_gathering_started_ = time_now;
514
515 if (session_) {
516 ExtractSessionInfo();
517 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000518 ExtractVideoInfo(level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 }
520}
521
wu@webrtc.org97077a32013-10-25 21:18:33 +0000522StatsReport* StatsCollector::PrepareLocalReport(
523 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000524 const std::string& transport_id,
525 TrackDirection direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000526 ASSERT(session_->signaling_thread()->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000527 const std::string ssrc_id = rtc::ToString<uint32>(ssrc);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000528 StatsReport* report = reports_.Find(
529 StatsId(StatsReport::kStatsReportTypeSsrc, ssrc_id, direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
xians@webrtc.org01bda202014-07-09 07:38:38 +0000531 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000533 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000534 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000535 // The ssrc is not used by any track or existing report, return NULL
536 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000538 }
539
540 // The ssrc is not used by any existing track. Keeps the old track id
541 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000542 ExtractValueFromReport(*report,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 StatsReport::kStatsValueNameTrackId,
544 &track_id);
545 }
546
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000547 report = GetOrCreateReport(
548 StatsReport::kStatsReportTypeSsrc, ssrc_id, direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549
550 // Clear out stats from previous GatherStats calls if any.
xians@webrtc.org01bda202014-07-09 07:38:38 +0000551 // This is required since the report will be returned for the new values.
552 // Having the old values in the report will lead to multiple values with
553 // the same name.
554 // TODO(xians): Consider changing StatsReport to use map instead of vector.
555 report->values.clear();
556 report->timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
558 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
559 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
560 // Add the mapping of SSRC to transport.
561 report->AddValue(StatsReport::kStatsValueNameTransportId,
562 transport_id);
563 return report;
564}
565
wu@webrtc.org97077a32013-10-25 21:18:33 +0000566StatsReport* StatsCollector::PrepareRemoteReport(
567 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000568 const std::string& transport_id,
569 TrackDirection direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000570 ASSERT(session_->signaling_thread()->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000571 const std::string ssrc_id = rtc::ToString<uint32>(ssrc);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000572 StatsReport* report = reports_.Find(
573 StatsId(StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction));
wu@webrtc.org97077a32013-10-25 21:18:33 +0000574
xians@webrtc.org01bda202014-07-09 07:38:38 +0000575 // Use the ID of the track that is currently mapped to the SSRC, if any.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000576 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000577 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000578 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000579 // The ssrc is not used by any track or existing report, return NULL
580 // in such case to indicate no report is prepared for the ssrc.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000581 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000582 }
583
584 // The ssrc is not used by any existing track. Keeps the old track id
585 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000586 ExtractValueFromReport(*report,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000587 StatsReport::kStatsValueNameTrackId,
588 &track_id);
589 }
590
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000591 report = GetOrCreateReport(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000592 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000593
594 // Clear out stats from previous GatherStats calls if any.
595 // The timestamp will be added later. Zero it for debugging.
596 report->values.clear();
597 report->timestamp = 0;
598
599 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
600 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
601 // Add the mapping of SSRC to transport.
602 report->AddValue(StatsReport::kStatsValueNameTransportId,
603 transport_id);
604 return report;
605}
606
wu@webrtc.org4551b792013-10-09 15:37:36 +0000607std::string StatsCollector::AddOneCertificateReport(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000608 const rtc::SSLCertificate* cert, const std::string& issuer_id) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000609 // TODO(bemasc): Move this computation to a helper class that caches these
610 // values to reduce CPU use in GetStats. This will require adding a fast
611 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000612
613 std::string digest_algorithm;
614 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
615 return std::string();
616
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000617 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
618 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000619
620 // SSLFingerprint::Create can fail if the algorithm returned by
621 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
622 // implementation of SSLCertificate::ComputeDigest. This currently happens
623 // with MD5- and SHA-224-signed certificates when linked to libNSS.
624 if (!ssl_fingerprint)
625 return std::string();
626
wu@webrtc.org4551b792013-10-09 15:37:36 +0000627 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
628
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000629 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000630 cert->ToDER(&der_buffer);
631 std::string der_base64;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000632 rtc::Base64::EncodeFromArray(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000633 der_buffer.data(), der_buffer.length(), &der_base64);
634
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000635 StatsReport* report = reports_.ReplaceOrAddNew(
636 StatsId(StatsReport::kStatsReportTypeCertificate, fingerprint));
637 report->type = StatsReport::kStatsReportTypeCertificate;
638 report->timestamp = stats_gathering_started_;
639 report->AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
640 report->AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
641 digest_algorithm);
642 report->AddValue(StatsReport::kStatsValueNameDer, der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000643 if (!issuer_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000644 report->AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
645 return report->id;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000646}
647
648std::string StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000649 const rtc::SSLCertificate* cert) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000650 // Produces a chain of StatsReports representing this certificate and the rest
651 // of its chain, and adds those reports to |reports_|. The return value is
652 // the id of the leaf report. The provided cert must be non-null, so at least
653 // one report will always be provided and the returned string will never be
654 // empty.
655 ASSERT(cert != NULL);
656
657 std::string issuer_id;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000658 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000659 if (cert->GetChain(chain.accept())) {
660 // This loop runs in reverse, i.e. from root to leaf, so that each
661 // certificate's issuer's report ID is known before the child certificate's
662 // report is generated. The root certificate does not have an issuer ID
663 // value.
664 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000665 const rtc::SSLCertificate& cert_i = chain->Get(i);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000666 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
667 }
668 }
669 // Add the leaf certificate.
670 return AddOneCertificateReport(cert, issuer_id);
671}
672
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000673std::string StatsCollector::AddCandidateReport(
674 const cricket::Candidate& candidate,
675 const std::string& report_type) {
676 std::ostringstream ost;
677 ost << "Cand-" << candidate.id();
678 StatsReport* report = reports_.Find(ost.str());
679 if (!report) {
680 report = reports_.InsertNew(ost.str());
681 DCHECK(StatsReport::kStatsReportTypeIceLocalCandidate == report_type ||
682 StatsReport::kStatsReportTypeIceRemoteCandidate == report_type);
683 report->type = report_type;
684 if (report_type == StatsReport::kStatsReportTypeIceLocalCandidate) {
685 report->AddValue(StatsReport::kStatsValueNameCandidateNetworkType,
686 AdapterTypeToStatsType(candidate.network_type()));
687 }
688 report->timestamp = stats_gathering_started_;
689 report->AddValue(StatsReport::kStatsValueNameCandidateIPAddress,
690 candidate.address().ipaddr().ToString());
691 report->AddValue(StatsReport::kStatsValueNameCandidatePortNumber,
692 candidate.address().PortAsString());
693 report->AddValue(StatsReport::kStatsValueNameCandidatePriority,
694 candidate.priority());
695 report->AddValue(StatsReport::kStatsValueNameCandidateType,
696 IceCandidateTypeToStatsType(candidate.type()));
697 report->AddValue(StatsReport::kStatsValueNameCandidateTransportType,
698 candidate.protocol());
699 }
700
701 return ost.str();
702}
703
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704void StatsCollector::ExtractSessionInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000705 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 // Extract information from the base session.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000707 StatsReport* report = reports_.ReplaceOrAddNew(
708 StatsId(StatsReport::kStatsReportTypeSession, session_->id()));
709 report->type = StatsReport::kStatsReportTypeSession;
710 report->timestamp = stats_gathering_started_;
711 report->values.clear();
712 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
713 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714
715 cricket::SessionStats stats;
716 if (session_->GetStats(&stats)) {
717 // Store the proxy map away for use in SSRC reporting.
718 proxy_to_transport_ = stats.proxy_to_transport;
719
720 for (cricket::TransportStatsMap::iterator transport_iter
721 = stats.transport_stats.begin();
722 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000723 // Attempt to get a copy of the certificates from the transport and
724 // expose them in stats reports. All channels in a transport share the
725 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000726 //
727 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
728 // invoke method calls on the worker thread and block this thread, but
729 // messages are still processed on this thread, which may blow way the
730 // existing transports. So we cannot reuse |transport| after these calls.
wu@webrtc.org4551b792013-10-09 15:37:36 +0000731 std::string local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000732
wu@webrtc.org4551b792013-10-09 15:37:36 +0000733 cricket::Transport* transport =
734 session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000735 rtc::scoped_ptr<rtc::SSLIdentity> identity;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000736 if (transport && transport->GetIdentity(identity.accept())) {
737 local_cert_report_id =
738 AddCertificateReports(&(identity->certificate()));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000739 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000740
741 transport = session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000742 rtc::scoped_ptr<rtc::SSLCertificate> cert;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000743 if (transport && transport->GetRemoteCertificate(cert.accept())) {
744 remote_cert_report_id = AddCertificateReports(cert.get());
745 }
746
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 for (cricket::TransportChannelStatsList::iterator channel_iter
748 = transport_iter->second.channel_stats.begin();
749 channel_iter != transport_iter->second.channel_stats.end();
750 ++channel_iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 std::ostringstream ostc;
752 ostc << "Channel-" << transport_iter->second.content_name
753 << "-" << channel_iter->component;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000754 StatsReport* channel_report = reports_.ReplaceOrAddNew(ostc.str());
755 channel_report->type = StatsReport::kStatsReportTypeComponent;
756 channel_report->timestamp = stats_gathering_started_;
757 channel_report->AddValue(StatsReport::kStatsValueNameComponent,
758 channel_iter->component);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000759 if (!local_cert_report_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000760 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000761 StatsReport::kStatsValueNameLocalCertificateId,
762 local_cert_report_id);
763 if (!remote_cert_report_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000764 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000765 StatsReport::kStatsValueNameRemoteCertificateId,
766 remote_cert_report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 for (size_t i = 0;
768 i < channel_iter->connection_infos.size();
769 ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 std::ostringstream ost;
771 ost << "Conn-" << transport_iter->first << "-"
772 << channel_iter->component << "-" << i;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000773 StatsReport* report = reports_.ReplaceOrAddNew(ost.str());
774 report->type = StatsReport::kStatsReportTypeCandidatePair;
775 report->timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 // Link from connection to its containing channel.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000777 report->AddValue(StatsReport::kStatsValueNameChannelId,
778 channel_report->id);
779
780 const cricket::ConnectionInfo& info =
781 channel_iter->connection_infos[i];
782 report->AddValue(StatsReport::kStatsValueNameBytesSent,
783 info.sent_total_bytes);
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000784 report->AddValue(StatsReport::kStatsValueNameSendPacketsDiscarded,
785 info.sent_discarded_packets);
786 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
787 info.sent_total_packets);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000788 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
789 info.recv_total_bytes);
790 report->AddBoolean(StatsReport::kStatsValueNameWritable,
791 info.writable);
792 report->AddBoolean(StatsReport::kStatsValueNameReadable,
793 info.readable);
794 report->AddBoolean(StatsReport::kStatsValueNameActiveConnection,
795 info.best_connection);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000796 report->AddValue(StatsReport::kStatsValueNameLocalCandidateId,
797 AddCandidateReport(
798 info.local_candidate,
799 StatsReport::kStatsReportTypeIceLocalCandidate));
800 report->AddValue(
801 StatsReport::kStatsValueNameRemoteCandidateId,
802 AddCandidateReport(
803 info.remote_candidate,
804 StatsReport::kStatsReportTypeIceRemoteCandidate));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000805 report->AddValue(StatsReport::kStatsValueNameLocalAddress,
806 info.local_candidate.address().ToString());
807 report->AddValue(StatsReport::kStatsValueNameRemoteAddress,
808 info.remote_candidate.address().ToString());
809 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
810 report->AddValue(StatsReport::kStatsValueNameTransportType,
811 info.local_candidate.protocol());
812 report->AddValue(StatsReport::kStatsValueNameLocalCandidateType,
813 info.local_candidate.type());
814 report->AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
815 info.remote_candidate.type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817 }
818 }
819 }
820}
821
822void StatsCollector::ExtractVoiceInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000823 ASSERT(session_->signaling_thread()->IsCurrent());
824
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 if (!session_->voice_channel()) {
826 return;
827 }
828 cricket::VoiceMediaInfo voice_info;
829 if (!session_->voice_channel()->GetStats(&voice_info)) {
830 LOG(LS_ERROR) << "Failed to get voice channel stats.";
831 return;
832 }
833 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000834 if (!GetTransportIdFromProxy(proxy_to_transport_,
835 session_->voice_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 &transport_id)) {
837 LOG(LS_ERROR) << "Failed to get transport name for proxy "
838 << session_->voice_channel()->content_name();
839 return;
840 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000841 ExtractStatsFromList(voice_info.receivers, transport_id, this, kReceiving);
842 ExtractStatsFromList(voice_info.senders, transport_id, this, kSending);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000843
844 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845}
846
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000847void StatsCollector::ExtractVideoInfo(
848 PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000849 ASSERT(session_->signaling_thread()->IsCurrent());
850
851 if (!session_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000853
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000854 cricket::StatsOptions options;
855 options.include_received_propagation_stats =
856 (level >= PeerConnectionInterface::kStatsOutputLevelDebug) ?
857 true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 cricket::VideoMediaInfo video_info;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000859 if (!session_->video_channel()->GetStats(options, &video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 LOG(LS_ERROR) << "Failed to get video channel stats.";
861 return;
862 }
863 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000864 if (!GetTransportIdFromProxy(proxy_to_transport_,
865 session_->video_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 &transport_id)) {
867 LOG(LS_ERROR) << "Failed to get transport name for proxy "
868 << session_->video_channel()->content_name();
869 return;
870 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000871 ExtractStatsFromList(video_info.receivers, transport_id, this, kReceiving);
872 ExtractStatsFromList(video_info.senders, transport_id, this, kSending);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 if (video_info.bw_estimations.size() != 1) {
874 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
875 } else {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000876 StatsReport* report =
877 reports_.FindOrAddNew(StatsReport::kStatsReportVideoBweId);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000879 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881}
882
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000883StatsReport* StatsCollector::GetReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000884 const std::string& id,
885 TrackDirection direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000886 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000887 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
888 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000889 return reports_.Find(StatsId(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000890}
891
892StatsReport* StatsCollector::GetOrCreateReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000893 const std::string& id,
894 TrackDirection direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000895 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000896 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
897 type == StatsReport::kStatsReportTypeRemoteSsrc);
898 StatsReport* report = GetReport(type, id, direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000899 if (report == NULL) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000900 std::string statsid = StatsId(type, id, direction);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000901 report = reports_.FindOrAddNew(statsid);
902 ASSERT(report->id == statsid);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000903 report->type = type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000904 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000905
wu@webrtc.org97077a32013-10-25 21:18:33 +0000906 return report;
907}
908
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000909void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000910 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000911 // Loop through the existing local audio tracks.
912 for (LocalAudioTrackVector::const_iterator it = local_audio_tracks_.begin();
913 it != local_audio_tracks_.end(); ++it) {
914 AudioTrackInterface* track = it->first;
915 uint32 ssrc = it->second;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000916 std::string ssrc_id = rtc::ToString<uint32>(ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000917 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000918 ssrc_id,
919 kSending);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000920 if (report == NULL) {
921 // This can happen if a local audio track is added to a stream on the
922 // fly and the report has not been set up yet. Do nothing in this case.
923 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
924 continue;
925 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000926
927 // The same ssrc can be used by both local and remote audio tracks.
928 std::string track_id;
929 if (!ExtractValueFromReport(*report,
930 StatsReport::kStatsValueNameTrackId,
931 &track_id) ||
932 track_id != track->id()) {
933 continue;
934 }
935
936 UpdateReportFromAudioTrack(track, report);
937 }
938}
939
940void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
941 StatsReport* report) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000942 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000943 ASSERT(track != NULL);
944 if (report == NULL)
945 return;
946
947 int signal_level = 0;
948 if (track->GetSignalLevel(&signal_level)) {
949 report->ReplaceValue(StatsReport::kStatsValueNameAudioInputLevel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000950 rtc::ToString<int>(signal_level));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000951 }
952
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000953 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000954 track->GetAudioProcessor());
955 if (audio_processor.get() == NULL)
956 return;
957
958 AudioProcessorInterface::AudioProcessorStats stats;
959 audio_processor->GetStats(&stats);
960 report->ReplaceValue(StatsReport::kStatsValueNameTypingNoiseState,
961 stats.typing_noise_detected ? "true" : "false");
962 report->ReplaceValue(StatsReport::kStatsValueNameEchoReturnLoss,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000963 rtc::ToString<int>(stats.echo_return_loss));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000964 report->ReplaceValue(
965 StatsReport::kStatsValueNameEchoReturnLossEnhancement,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000966 rtc::ToString<int>(stats.echo_return_loss_enhancement));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000967 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayMedian,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000968 rtc::ToString<int>(stats.echo_delay_median_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000969 report->ReplaceValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000970 rtc::ToString<float>(stats.aec_quality_min));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000971 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayStdDev,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000972 rtc::ToString<int>(stats.echo_delay_std_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000973}
974
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000975bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
976 TrackDirection direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000977 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000978 if (direction == kSending) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000979 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000980 LOG(LS_WARNING) << "The SSRC " << ssrc
981 << " is not associated with a sending track";
982 return false;
983 }
984 } else {
985 ASSERT(direction == kReceiving);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000986 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000987 LOG(LS_WARNING) << "The SSRC " << ssrc
988 << " is not associated with a receiving track";
989 return false;
990 }
991 }
992
993 return true;
994}
995
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000996void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000997 stats_gathering_started_ = 0;
998}
999
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000} // namespace webrtc