blob: 76ac76d7d2b953ff556039151373733d388bff2d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/statscollector.h"
29
30#include <utility>
31#include <vector>
32
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/base64.h"
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +000035#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/scoped_ptr.h"
37#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000039using rtc::scoped_ptr;
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000044// The following is the enum RTCStatsIceCandidateType from
45// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
46// our stats report for ice candidate type could conform to that.
47const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
48const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
49const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
50const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
51
52// Strings used by the stats collector to report adapter types. This fits the
53// general stype of http://w3c.github.io/webrtc-stats than what
54// AdapterTypeToString does.
55const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
56const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
57const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
58const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000059const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000060
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061template<typename ValueType>
62struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000063 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000064 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000065};
66
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000067typedef TypeForAdd<bool> BoolForAdd;
68typedef TypeForAdd<float> FloatForAdd;
69typedef TypeForAdd<int64> Int64ForAdd;
70typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000071
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000072StatsReport::Id GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
73 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070074 RTC_DCHECK(!proxy.empty());
tommi@webrtc.org47218952014-07-15 19:22:37 +000075 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000076 if (found == map.end())
77 return StatsReport::Id();
tommi@webrtc.org47218952014-07-15 19:22:37 +000078
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000079 return StatsReport::NewComponentId(
80 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000081}
82
jbauchbe24c942015-06-22 15:06:43 -070083StatsReport* AddTrackReport(StatsCollection* reports,
84 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000085 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000086 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000087 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000088 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000089 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070090 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000091}
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093template <class TrackVector>
jbauchbe24c942015-06-22 15:06:43 -070094void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
95 TrackIdMap& track_ids) {
96 for (const auto& track : tracks) {
97 const std::string& track_id = track->id();
98 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070099 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -0700100 track_ids[track_id] = report;
101 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102}
103
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000104void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
105 StatsReport* report) {
106 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
107 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
108 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
109}
110
pbosf42376c2015-08-28 07:35:32 -0700111void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
112 StatsReport* report) {
113 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
114}
115
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000116void SetAudioProcessingStats(StatsReport* report, int signal_level,
117 bool typing_noise_detected, int echo_return_loss,
118 int echo_return_loss_enhancement, int echo_delay_median_ms,
119 float aec_quality_min, int echo_delay_std_ms) {
120 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
121 typing_noise_detected);
122 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
123 aec_quality_min);
124 // Don't overwrite the previous signal level if it's not available now.
125 if (signal_level >= 0)
126 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
127 const IntForAdd ints[] = {
128 { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss },
129 { StatsReport::kStatsValueNameEchoReturnLossEnhancement,
130 echo_return_loss_enhancement },
131 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
132 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
133 };
134 for (const auto& i : ints)
135 report->AddInt(i.name, i.value);
136}
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700139 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000140 const FloatForAdd floats[] = {
141 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
142 { StatsReport::kStatsValueNameSecondaryDecodedRate,
143 info.secondary_decoded_rate },
144 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200145 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
146 { StatsReport::kStatsValueNamePreemptiveExpandRate,
147 info.preemptive_expand_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000148 };
149
150 const IntForAdd ints[] = {
151 { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level },
152 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
153 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
154 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
155 { StatsReport::kStatsValueNameDecodingCTSG,
156 info.decoding_calls_to_silence_generator },
157 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
158 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
159 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
160 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
161 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
162 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
163 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
164 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
165 info.jitter_buffer_preferred_ms },
166 };
167
168 for (const auto& f : floats)
169 report->AddFloat(f.name, f.value);
170
171 for (const auto& i : ints)
172 report->AddInt(i.name, i.value);
173
174 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000176 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000177 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178}
179
180void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000181 ExtractCommonSendProperties(info, report);
182
183 SetAudioProcessingStats(report, info.audio_level, info.typing_noise_detected,
184 info.echo_return_loss, info.echo_return_loss_enhancement,
185 info.echo_delay_median_ms, info.aec_quality_min, info.echo_delay_std_ms);
186
187 const IntForAdd ints[] = {
188 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
189 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
190 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
191 };
192
193 for (const auto& i : ints)
194 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195}
196
197void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700198 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000199 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000201 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000202 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203 const IntForAdd ints[] = {
204 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
205 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
206 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
207 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
208 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
209 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
210 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
211 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
212 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
213 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
214 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
215 info.min_playout_delay_ms },
216 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
217 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
218 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
219 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
220 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
221 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
222 };
223
224 for (const auto& i : ints)
225 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226}
227
228void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000229 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000231 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
232 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000233 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
234 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000235 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
236 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000237
238 const IntForAdd ints[] = {
239 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
240 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000241 { StatsReport::kStatsValueNameEncodeUsagePercent,
242 info.encode_usage_percent },
243 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
244 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
245 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
246 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
247 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
248 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
249 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
250 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
251 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
252 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
253 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
254 };
255
256 for (const auto& i : ints)
257 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258}
259
260void ExtractStats(const cricket::BandwidthEstimationInfo& info,
261 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000262 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700264 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000266 report->set_timestamp(stats_gathering_started);
267 const IntForAdd ints[] = {
268 { StatsReport::kStatsValueNameAvailableSendBandwidth,
269 info.available_send_bandwidth },
270 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
271 info.available_recv_bandwidth },
272 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
273 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
274 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
275 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
276 };
277 for (const auto& i : ints)
278 report->AddInt(i.name, i.value);
279 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280}
281
wu@webrtc.org97077a32013-10-25 21:18:33 +0000282void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
283 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000284 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000285 // TODO(hta): Extract some stats here.
286}
287
288void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
289 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000290 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000291 // TODO(hta): Extract some stats here.
292}
293
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000295// In order to use the template, the functions that are called from it,
296// ExtractStats and ExtractRemoteStats, must be defined and overloaded
297// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298template<typename T>
299void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000300 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000301 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000302 StatsReport::Direction direction) {
303 for (const auto& d : data) {
304 uint32 ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000305 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000306 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000307 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
308 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000309 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000310 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000311
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000312 if (!d.remote_stats.empty()) {
313 report = collector->PrepareReport(false, ssrc, transport_id, direction);
314 if (report)
315 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000316 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000318}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319
320} // namespace
321
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000322const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
323 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
324 return STATSREPORT_LOCAL_PORT_TYPE;
325 }
326 if (candidate_type == cricket::STUN_PORT_TYPE) {
327 return STATSREPORT_STUN_PORT_TYPE;
328 }
329 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
330 return STATSREPORT_PRFLX_PORT_TYPE;
331 }
332 if (candidate_type == cricket::RELAY_PORT_TYPE) {
333 return STATSREPORT_RELAY_PORT_TYPE;
334 }
henrikg91d6ede2015-09-17 00:24:34 -0700335 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000336 return "unknown";
337}
338
339const char* AdapterTypeToStatsType(rtc::AdapterType type) {
340 switch (type) {
341 case rtc::ADAPTER_TYPE_UNKNOWN:
342 return "unknown";
343 case rtc::ADAPTER_TYPE_ETHERNET:
344 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
345 case rtc::ADAPTER_TYPE_WIFI:
346 return STATSREPORT_ADAPTER_TYPE_WIFI;
347 case rtc::ADAPTER_TYPE_CELLULAR:
348 return STATSREPORT_ADAPTER_TYPE_WWAN;
349 case rtc::ADAPTER_TYPE_VPN:
350 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000351 case rtc::ADAPTER_TYPE_LOOPBACK:
352 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000353 default:
henrikg91d6ede2015-09-17 00:24:34 -0700354 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000355 return "";
356 }
357}
358
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000359StatsCollector::StatsCollector(WebRtcSession* session)
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000360 : session_(session),
361 stats_gathering_started_(0) {
henrikg91d6ede2015-09-17 00:24:34 -0700362 RTC_DCHECK(session_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000363}
364
365StatsCollector::~StatsCollector() {
henrikg91d6ede2015-09-17 00:24:34 -0700366 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367}
368
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000369double StatsCollector::GetTimeNow() {
370 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
371}
372
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373// Adds a MediaStream with tracks that can be used as a |selector| in a call
374// to GetStats.
375void StatsCollector::AddStream(MediaStreamInterface* stream) {
henrikg91d6ede2015-09-17 00:24:34 -0700376 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
377 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378
379 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700380 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700382 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383}
384
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000385void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
386 uint32 ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700387 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
388 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000389#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000390 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700391 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000392#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000393
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000394 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000395
396 // Create the kStatsReportTypeTrack report for the new track if there is no
397 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000398 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
399 audio_track->id()));
400 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000401 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000402 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000403 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000404 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000405}
406
407void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
408 uint32 ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700409 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000410 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
411 local_audio_tracks_.end(),
412 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
413 return track.first == audio_track && track.second == ssrc;
414 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000415}
416
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000417void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 StatsReports* reports) {
henrikg91d6ede2015-09-17 00:24:34 -0700419 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
420 RTC_DCHECK(reports != NULL);
421 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000423 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
424
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000426 reports->reserve(reports_.size());
427 for (auto* r : reports_)
428 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000429 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
431
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000432 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
433 StatsReport::kStatsReportTypeSession, session_->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000434 if (report)
435 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000437 report = reports_.Find(StatsReport::NewTypedId(
438 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000440 if (!report)
441 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000443 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444
445 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000446 for (const auto* r : reports_) {
447 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000449
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000450 const StatsReport::Value* v =
451 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000452 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000453 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455}
456
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000457void
458StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
henrikg91d6ede2015-09-17 00:24:34 -0700459 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 double time_now = GetTimeNow();
461 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
462 // ms apart will be ignored.
463 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000464 if (stats_gathering_started_ != 0 &&
465 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 return;
467 }
468 stats_gathering_started_ = time_now;
469
470 if (session_) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000471 // TODO(tommi): All of these hop over to the worker thread to fetch
472 // information. We could use an AsyncInvoker to run all of these and post
473 // the information back to the signaling thread where we can create and
474 // update stats reports. That would also clean up the threading story a bit
475 // since we'd be creating/updating the stats report objects consistently on
476 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 ExtractSessionInfo();
478 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000479 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000480 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700481 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 }
483}
484
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000485StatsReport* StatsCollector::PrepareReport(
486 bool local,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000487 uint32 ssrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000488 const StatsReport::Id& transport_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000489 StatsReport::Direction direction) {
henrikg91d6ede2015-09-17 00:24:34 -0700490 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000491 StatsReport::Id id(StatsReport::NewIdWithDirection(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000492 local ? StatsReport::kStatsReportTypeSsrc :
493 StatsReport::kStatsReportTypeRemoteSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000494 rtc::ToString<uint32>(ssrc), direction));
495 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496
xians@webrtc.org01bda202014-07-09 07:38:38 +0000497 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000499 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000500 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000501 // The ssrc is not used by any track or existing report, return NULL
502 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000504 }
505
506 // The ssrc is not used by any existing track. Keeps the old track id
507 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000508 const StatsReport::Value* v =
509 report->FindValue(StatsReport::kStatsValueNameTrackId);
510 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000511 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 }
513
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000514 if (!report)
515 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000517 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000518 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000520 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000521 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000523 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 return report;
525}
526
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000527StatsReport* StatsCollector::AddOneCertificateReport(
528 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
henrikg91d6ede2015-09-17 00:24:34 -0700529 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000530
wu@webrtc.org4551b792013-10-09 15:37:36 +0000531 // TODO(bemasc): Move this computation to a helper class that caches these
532 // values to reduce CPU use in GetStats. This will require adding a fast
533 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000534
535 std::string digest_algorithm;
536 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000537 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000538
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000539 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
540 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000541
542 // SSLFingerprint::Create can fail if the algorithm returned by
543 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
544 // implementation of SSLCertificate::ComputeDigest. This currently happens
545 // with MD5- and SHA-224-signed certificates when linked to libNSS.
546 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000547 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000548
wu@webrtc.org4551b792013-10-09 15:37:36 +0000549 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
550
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000551 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000552 cert->ToDER(&der_buffer);
553 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000554 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
555 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000556
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000557 StatsReport::Id id(StatsReport::NewTypedId(
558 StatsReport::kStatsReportTypeCertificate, fingerprint));
559 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000560 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000561 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
562 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
563 digest_algorithm);
564 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000565 if (issuer)
566 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
567 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000568}
569
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000570StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000571 const rtc::SSLCertificate* cert) {
henrikg91d6ede2015-09-17 00:24:34 -0700572 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000573 // Produces a chain of StatsReports representing this certificate and the rest
574 // of its chain, and adds those reports to |reports_|. The return value is
575 // the id of the leaf report. The provided cert must be non-null, so at least
576 // one report will always be provided and the returned string will never be
577 // empty.
henrikg91d6ede2015-09-17 00:24:34 -0700578 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000579
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000580 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000581 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000582 if (cert->GetChain(chain.accept())) {
583 // This loop runs in reverse, i.e. from root to leaf, so that each
584 // certificate's issuer's report ID is known before the child certificate's
585 // report is generated. The root certificate does not have an issuer ID
586 // value.
587 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000588 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000589 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000590 }
591 }
592 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000593 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000594}
595
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000596StatsReport* StatsCollector::AddConnectionInfoReport(
597 const std::string& content_name, int component, int connection_id,
598 const StatsReport::Id& channel_report_id,
599 const cricket::ConnectionInfo& info) {
600 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
601 connection_id));
602 StatsReport* report = reports_.ReplaceOrAddNew(id);
603 report->set_timestamp(stats_gathering_started_);
604
605 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700606 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
607 {StatsReport::kStatsValueNameReceiving, info.receiving},
608 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000609 };
610 for (const auto& b : bools)
611 report->AddBoolean(b.name, b.value);
612
613 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
614 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
615 AddCandidateReport(info.local_candidate, true)->id());
616 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
617 AddCandidateReport(info.remote_candidate, false)->id());
618
619 const Int64ForAdd int64s[] = {
620 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
621 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
622 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
623 { StatsReport::kStatsValueNameRtt, info.rtt },
624 { StatsReport::kStatsValueNameSendPacketsDiscarded,
625 info.sent_discarded_packets },
626 };
627 for (const auto& i : int64s)
628 report->AddInt64(i.name, i.value);
629
630 report->AddString(StatsReport::kStatsValueNameLocalAddress,
631 info.local_candidate.address().ToString());
632 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
633 info.local_candidate.type());
634 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
635 info.remote_candidate.address().ToString());
636 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
637 info.remote_candidate.type());
638 report->AddString(StatsReport::kStatsValueNameTransportType,
639 info.local_candidate.protocol());
640
641 return report;
642}
643
644StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000645 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000646 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000647 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
648 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000649 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000650 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000651 report->set_timestamp(stats_gathering_started_);
652 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000653 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
654 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000655 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000656 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
657 candidate.address().ipaddr().ToString());
658 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
659 candidate.address().PortAsString());
660 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
661 candidate.priority());
662 report->AddString(StatsReport::kStatsValueNameCandidateType,
663 IceCandidateTypeToStatsType(candidate.type()));
664 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
665 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000666 }
667
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000668 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000669}
670
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671void StatsCollector::ExtractSessionInfo() {
henrikg91d6ede2015-09-17 00:24:34 -0700672 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000673
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000675 StatsReport::Id id(StatsReport::NewTypedId(
676 StatsReport::kStatsReportTypeSession, session_->id()));
677 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000678 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000679 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
680 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681
682 cricket::SessionStats stats;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000683 if (!session_->GetTransportStats(&stats)) {
684 return;
685 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000687 // Store the proxy map away for use in SSRC reporting.
688 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
689 // signaling thread after fetching them on the worker thread, then just use
690 // the proxy map directly from the session stats.
691 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
692 // data.
693 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000694
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000695 for (const auto& transport_iter : stats.transport_stats) {
696 // Attempt to get a copy of the certificates from the transport and
697 // expose them in stats reports. All channels in a transport share the
698 // same local and remote certificates.
699 //
Guo-wei Shieh89024332015-09-18 13:50:22 -0700700 // Note that Transport::GetCertificate and Transport::GetRemoteCertificate
701 // invoke method calls on the worker thread and block this thread, but
702 // messages are still processed on this thread, which may blow way the
703 // existing transports. So we cannot reuse |transport| after these calls.
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000704 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Guo-wei Shieh89024332015-09-18 13:50:22 -0700705
706 cricket::Transport* transport =
707 session_->GetTransport(transport_iter.second.content_name);
Henrik Boströmd8281982015-08-27 10:12:24 +0200708 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
Guo-wei Shieh89024332015-09-18 13:50:22 -0700709 if (transport && transport->GetCertificate(&certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200710 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000711 if (r)
712 local_cert_report_id = r->id();
713 }
714
Guo-wei Shieh89024332015-09-18 13:50:22 -0700715 transport = session_->GetTransport(transport_iter.second.content_name);
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000716 rtc::scoped_ptr<rtc::SSLCertificate> cert;
Guo-wei Shieh89024332015-09-18 13:50:22 -0700717 if (transport && transport->GetRemoteSSLCertificate(cert.accept())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000718 StatsReport* r = AddCertificateReports(cert.get());
719 if (r)
720 remote_cert_report_id = r->id();
721 }
722
723 for (const auto& channel_iter : transport_iter.second.channel_stats) {
724 StatsReport::Id id(StatsReport::NewComponentId(
Guo-wei Shieh89024332015-09-18 13:50:22 -0700725 transport_iter.second.content_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000726 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
727 channel_report->set_timestamp(stats_gathering_started_);
728 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
729 channel_iter.component);
730 if (local_cert_report_id.get()) {
731 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
732 local_cert_report_id);
733 }
734 if (remote_cert_report_id.get()) {
735 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
736 remote_cert_report_id);
737 }
738 const std::string& srtp_cipher = channel_iter.srtp_cipher;
739 if (!srtp_cipher.empty()) {
740 channel_report->AddString(StatsReport::kStatsValueNameSrtpCipher,
741 srtp_cipher);
742 }
743 const std::string& ssl_cipher = channel_iter.ssl_cipher;
744 if (!ssl_cipher.empty()) {
745 channel_report->AddString(StatsReport::kStatsValueNameDtlsCipher,
746 ssl_cipher);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000747 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000748
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000749 int connection_id = 0;
750 for (const cricket::ConnectionInfo& info :
751 channel_iter.connection_infos) {
752 StatsReport* connection_report = AddConnectionInfoReport(
753 transport_iter.first, channel_iter.component, connection_id++,
754 channel_report->id(), info);
755 if (info.best_connection) {
756 channel_report->AddId(
757 StatsReport::kStatsValueNameSelectedCandidatePairId,
758 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 }
761 }
762 }
763}
764
765void StatsCollector::ExtractVoiceInfo() {
henrikg91d6ede2015-09-17 00:24:34 -0700766 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000767
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 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000776
777 // TODO(tommi): The above code should run on the worker thread and post the
778 // results back to the signaling thread, where we can add data to the reports.
779 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
780
781 StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_,
782 session_->voice_channel()->content_name()));
783 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 LOG(LS_ERROR) << "Failed to get transport name for proxy "
785 << session_->voice_channel()->content_name();
786 return;
787 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000788
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000789 ExtractStatsFromList(voice_info.receivers, transport_id, this,
790 StatsReport::kReceive);
791 ExtractStatsFromList(voice_info.senders, transport_id, this,
792 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000793
794 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795}
796
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000797void StatsCollector::ExtractVideoInfo(
798 PeerConnectionInterface::StatsOutputLevel level) {
henrikg91d6ede2015-09-17 00:24:34 -0700799 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000800
801 if (!session_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000803
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 cricket::VideoMediaInfo video_info;
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000805 if (!session_->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 LOG(LS_ERROR) << "Failed to get video channel stats.";
807 return;
808 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000809
810 // TODO(tommi): The above code should run on the worker thread and post the
811 // results back to the signaling thread, where we can add data to the reports.
812 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
813
814 StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_,
815 session_->video_channel()->content_name()));
816 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 LOG(LS_ERROR) << "Failed to get transport name for proxy "
818 << session_->video_channel()->content_name();
819 return;
820 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000821 ExtractStatsFromList(video_info.receivers, transport_id, this,
822 StatsReport::kReceive);
823 ExtractStatsFromList(video_info.senders, transport_id, this,
824 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 if (video_info.bw_estimations.size() != 1) {
826 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
827 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000828 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
829 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000831 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833}
834
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000835void StatsCollector::ExtractDataInfo() {
henrikg91d6ede2015-09-17 00:24:34 -0700836 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000837
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000838 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
839
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000840 for (const auto& dc :
841 session_->mediastream_signaling()->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000842 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000843 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000844 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000845 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000846 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
847 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
848 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
849 report->AddString(StatsReport::kStatsValueNameState,
850 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000851 }
852}
853
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000854StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000855 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000856 StatsReport::Direction direction) {
henrikg91d6ede2015-09-17 00:24:34 -0700857 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
858 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
859 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000860 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000861}
862
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000863void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
henrikg91d6ede2015-09-17 00:24:34 -0700864 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000865 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000866 for (const auto& it : local_audio_tracks_) {
867 AudioTrackInterface* track = it.first;
868 uint32 ssrc = it.second;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000869 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000870 rtc::ToString<uint32>(ssrc),
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000871 StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000872 if (report == NULL) {
873 // This can happen if a local audio track is added to a stream on the
874 // fly and the report has not been set up yet. Do nothing in this case.
875 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
876 continue;
877 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000878
879 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000880 const StatsReport::Value* v =
881 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000882 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000883 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000884
jbauchbe24c942015-06-22 15:06:43 -0700885 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000886 UpdateReportFromAudioTrack(track, report);
887 }
888}
889
890void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
891 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700892 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
893 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894
895 int signal_level = 0;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000896 if (!track->GetSignalLevel(&signal_level))
897 signal_level = -1;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000898
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000899 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000900 track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000901
902 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000903 if (audio_processor.get())
904 audio_processor->GetStats(&stats);
905
906 SetAudioProcessingStats(report, signal_level, stats.typing_noise_detected,
907 stats.echo_return_loss, stats.echo_return_loss_enhancement,
908 stats.echo_delay_median_ms, stats.aec_quality_min,
909 stats.echo_delay_std_ms);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000910}
911
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000912bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000913 StatsReport::Direction direction) {
henrikg91d6ede2015-09-17 00:24:34 -0700914 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000915 if (direction == StatsReport::kSend) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000916 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000917 LOG(LS_WARNING) << "The SSRC " << ssrc
918 << " is not associated with a sending track";
919 return false;
920 }
921 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700922 RTC_DCHECK(direction == StatsReport::kReceive);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000923 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000924 LOG(LS_WARNING) << "The SSRC " << ssrc
925 << " is not associated with a receiving track";
926 return false;
927 }
928 }
929
930 return true;
931}
932
jbauchbe24c942015-06-22 15:06:43 -0700933void StatsCollector::UpdateTrackReports() {
henrikg91d6ede2015-09-17 00:24:34 -0700934 RTC_DCHECK(session_->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700935
936 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
937
938 for (const auto& entry : track_ids_) {
939 StatsReport* report = entry.second;
940 report->set_timestamp(stats_gathering_started_);
941 }
Guo-wei Shieh89024332015-09-18 13:50:22 -0700942
jbauchbe24c942015-06-22 15:06:43 -0700943}
944
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000945void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000946 stats_gathering_started_ = 0;
947}
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949} // namespace webrtc