blob: fb1595236b1a196a9e54889827bc07016df446a2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/statscollector.h"
29
30#include <utility>
31#include <vector>
32
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/base64.h"
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +000035#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/scoped_ptr.h"
37#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000039using rtc::scoped_ptr;
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000044// The following is the enum RTCStatsIceCandidateType from
45// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
46// our stats report for ice candidate type could conform to that.
47const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
48const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
49const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
50const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
51
52// Strings used by the stats collector to report adapter types. This fits the
53// general stype of http://w3c.github.io/webrtc-stats than what
54// AdapterTypeToString does.
55const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
56const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
57const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
58const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000059const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000060
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061template<typename ValueType>
62struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000063 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000064 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000065};
66
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000067typedef TypeForAdd<bool> BoolForAdd;
68typedef TypeForAdd<float> FloatForAdd;
69typedef TypeForAdd<int64> Int64ForAdd;
70typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000071
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000072StatsReport::Id GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
73 const std::string& proxy) {
74 DCHECK(!proxy.empty());
tommi@webrtc.org47218952014-07-15 19:22:37 +000075 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000076 if (found == map.end())
77 return StatsReport::Id();
tommi@webrtc.org47218952014-07-15 19:22:37 +000078
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000079 return StatsReport::NewComponentId(
80 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000081}
82
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000083void AddTrackReport(StatsCollection* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000084 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000085 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000086 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000087 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000088 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
xians@webrtc.org01bda202014-07-09 07:38:38 +000089}
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091template <class TrackVector>
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000092void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000093 for (const auto& track : tracks)
xians@webrtc.org01bda202014-07-09 07:38:38 +000094 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095}
96
tommi@webrtc.org92f40182015-03-04 15:25:19 +000097void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
98 StatsReport* report) {
99 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
100 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
101 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
102}
103
104void SetAudioProcessingStats(StatsReport* report, int signal_level,
105 bool typing_noise_detected, int echo_return_loss,
106 int echo_return_loss_enhancement, int echo_delay_median_ms,
107 float aec_quality_min, int echo_delay_std_ms) {
108 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
109 typing_noise_detected);
110 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
111 aec_quality_min);
112 // Don't overwrite the previous signal level if it's not available now.
113 if (signal_level >= 0)
114 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
115 const IntForAdd ints[] = {
116 { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss },
117 { StatsReport::kStatsValueNameEchoReturnLossEnhancement,
118 echo_return_loss_enhancement },
119 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
120 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
121 };
122 for (const auto& i : ints)
123 report->AddInt(i.name, i.value);
124}
125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000127 const FloatForAdd floats[] = {
128 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
129 { StatsReport::kStatsValueNameSecondaryDecodedRate,
130 info.secondary_decoded_rate },
131 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
132 };
133
134 const IntForAdd ints[] = {
135 { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level },
136 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
137 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
138 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
139 { StatsReport::kStatsValueNameDecodingCTSG,
140 info.decoding_calls_to_silence_generator },
141 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
142 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
143 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
144 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
145 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
146 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
147 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
148 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
149 info.jitter_buffer_preferred_ms },
150 };
151
152 for (const auto& f : floats)
153 report->AddFloat(f.name, f.value);
154
155 for (const auto& i : ints)
156 report->AddInt(i.name, i.value);
157
158 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000160 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000161 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000162
163 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164}
165
166void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000167 ExtractCommonSendProperties(info, report);
168
169 SetAudioProcessingStats(report, info.audio_level, info.typing_noise_detected,
170 info.echo_return_loss, info.echo_return_loss_enhancement,
171 info.echo_delay_median_ms, info.aec_quality_min, info.echo_delay_std_ms);
172
173 const IntForAdd ints[] = {
174 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
175 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
176 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
177 };
178
179 for (const auto& i : ints)
180 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181}
182
183void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000186 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000187 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000188 const IntForAdd ints[] = {
189 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
190 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
191 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
192 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
193 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
194 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
195 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
196 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
197 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
198 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
199 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
200 info.min_playout_delay_ms },
201 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
202 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
203 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
204 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
205 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
206 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
207 };
208
209 for (const auto& i : ints)
210 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
213void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000214 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000216 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
217 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000218 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
219 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000220 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
221 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000222
223 const IntForAdd ints[] = {
224 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
225 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000226 { StatsReport::kStatsValueNameEncodeUsagePercent,
227 info.encode_usage_percent },
228 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
229 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
230 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
231 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
232 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
233 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
234 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
235 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
236 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
237 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
238 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
239 };
240
241 for (const auto& i : ints)
242 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243}
244
245void ExtractStats(const cricket::BandwidthEstimationInfo& info,
246 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000247 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 StatsReport* report) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000249 DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000251 report->set_timestamp(stats_gathering_started);
252 const IntForAdd ints[] = {
253 { StatsReport::kStatsValueNameAvailableSendBandwidth,
254 info.available_send_bandwidth },
255 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
256 info.available_recv_bandwidth },
257 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
258 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
259 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
260 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
261 };
262 for (const auto& i : ints)
263 report->AddInt(i.name, i.value);
264 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265}
266
wu@webrtc.org97077a32013-10-25 21:18:33 +0000267void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
268 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000269 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000270 // TODO(hta): Extract some stats here.
271}
272
273void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
274 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000275 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000276 // TODO(hta): Extract some stats here.
277}
278
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000280// In order to use the template, the functions that are called from it,
281// ExtractStats and ExtractRemoteStats, must be defined and overloaded
282// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283template<typename T>
284void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000285 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000286 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000287 StatsReport::Direction direction) {
288 for (const auto& d : data) {
289 uint32 ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000290 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000291 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000292 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
293 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000294 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000295 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000296
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000297 if (!d.remote_stats.empty()) {
298 report = collector->PrepareReport(false, ssrc, transport_id, direction);
299 if (report)
300 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000301 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000303}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304
305} // namespace
306
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000307const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
308 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
309 return STATSREPORT_LOCAL_PORT_TYPE;
310 }
311 if (candidate_type == cricket::STUN_PORT_TYPE) {
312 return STATSREPORT_STUN_PORT_TYPE;
313 }
314 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
315 return STATSREPORT_PRFLX_PORT_TYPE;
316 }
317 if (candidate_type == cricket::RELAY_PORT_TYPE) {
318 return STATSREPORT_RELAY_PORT_TYPE;
319 }
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000320 DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000321 return "unknown";
322}
323
324const char* AdapterTypeToStatsType(rtc::AdapterType type) {
325 switch (type) {
326 case rtc::ADAPTER_TYPE_UNKNOWN:
327 return "unknown";
328 case rtc::ADAPTER_TYPE_ETHERNET:
329 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
330 case rtc::ADAPTER_TYPE_WIFI:
331 return STATSREPORT_ADAPTER_TYPE_WIFI;
332 case rtc::ADAPTER_TYPE_CELLULAR:
333 return STATSREPORT_ADAPTER_TYPE_WWAN;
334 case rtc::ADAPTER_TYPE_VPN:
335 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000336 case rtc::ADAPTER_TYPE_LOOPBACK:
337 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000338 default:
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000339 DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000340 return "";
341 }
342}
343
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000344StatsCollector::StatsCollector(WebRtcSession* session)
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000345 : session_(session),
346 stats_gathering_started_(0) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000347 DCHECK(session_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000348}
349
350StatsCollector::~StatsCollector() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000351 DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352}
353
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000354double StatsCollector::GetTimeNow() {
355 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
356}
357
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358// Adds a MediaStream with tracks that can be used as a |selector| in a call
359// to GetStats.
360void StatsCollector::AddStream(MediaStreamInterface* stream) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000361 DCHECK(session_->signaling_thread()->IsCurrent());
362 DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363
364 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
365 &reports_);
366 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
367 &reports_);
368}
369
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000370void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
371 uint32 ssrc) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000372 DCHECK(session_->signaling_thread()->IsCurrent());
373 DCHECK(audio_track != NULL);
374#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000375 for (const auto& track : local_audio_tracks_)
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000376 DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000377#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000378
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000379 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000380
381 // Create the kStatsReportTypeTrack report for the new track if there is no
382 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000383 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
384 audio_track->id()));
385 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000386 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000387 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000388 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000389 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000390}
391
392void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
393 uint32 ssrc) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000394 DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000395 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
396 local_audio_tracks_.end(),
397 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
398 return track.first == audio_track && track.second == ssrc;
399 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000400}
401
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000402void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 StatsReports* reports) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000404 DCHECK(session_->signaling_thread()->IsCurrent());
405 DCHECK(reports != NULL);
406 DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000408 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
409
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000411 reports->reserve(reports_.size());
412 for (auto* r : reports_)
413 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000414 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 }
416
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000417 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
418 StatsReport::kStatsReportTypeSession, session_->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000419 if (report)
420 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000422 report = reports_.Find(StatsReport::NewTypedId(
423 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000425 if (!report)
426 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000428 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429
430 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000431 for (const auto* r : reports_) {
432 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000434
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000435 const StatsReport::Value* v =
436 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000437 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000438 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000442void
443StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000444 DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 double time_now = GetTimeNow();
446 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
447 // ms apart will be ignored.
448 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000449 if (stats_gathering_started_ != 0 &&
450 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 return;
452 }
453 stats_gathering_started_ = time_now;
454
455 if (session_) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000456 // TODO(tommi): All of these hop over to the worker thread to fetch
457 // information. We could use an AsyncInvoker to run all of these and post
458 // the information back to the signaling thread where we can create and
459 // update stats reports. That would also clean up the threading story a bit
460 // since we'd be creating/updating the stats report objects consistently on
461 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 ExtractSessionInfo();
463 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000464 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000465 ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 }
467}
468
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000469StatsReport* StatsCollector::PrepareReport(
470 bool local,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000471 uint32 ssrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000472 const StatsReport::Id& transport_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000473 StatsReport::Direction direction) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000474 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000475 StatsReport::Id id(StatsReport::NewIdWithDirection(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000476 local ? StatsReport::kStatsReportTypeSsrc :
477 StatsReport::kStatsReportTypeRemoteSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000478 rtc::ToString<uint32>(ssrc), direction));
479 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480
xians@webrtc.org01bda202014-07-09 07:38:38 +0000481 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000483 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000484 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000485 // The ssrc is not used by any track or existing report, return NULL
486 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000488 }
489
490 // The ssrc is not used by any existing track. Keeps the old track id
491 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000492 const StatsReport::Value* v =
493 report->FindValue(StatsReport::kStatsValueNameTrackId);
494 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000495 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
497
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000498 if (!report)
499 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000501 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000502 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000504 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000505 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000507 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 return report;
509}
510
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000511StatsReport* StatsCollector::AddOneCertificateReport(
512 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000513 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000514
wu@webrtc.org4551b792013-10-09 15:37:36 +0000515 // TODO(bemasc): Move this computation to a helper class that caches these
516 // values to reduce CPU use in GetStats. This will require adding a fast
517 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000518
519 std::string digest_algorithm;
520 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000521 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000522
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000523 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
524 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000525
526 // SSLFingerprint::Create can fail if the algorithm returned by
527 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
528 // implementation of SSLCertificate::ComputeDigest. This currently happens
529 // with MD5- and SHA-224-signed certificates when linked to libNSS.
530 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000531 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000532
wu@webrtc.org4551b792013-10-09 15:37:36 +0000533 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
534
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000536 cert->ToDER(&der_buffer);
537 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000538 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
539 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000540
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000541 StatsReport::Id id(StatsReport::NewTypedId(
542 StatsReport::kStatsReportTypeCertificate, fingerprint));
543 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000544 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000545 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
546 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
547 digest_algorithm);
548 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000549 if (issuer)
550 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
551 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000552}
553
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000554StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 const rtc::SSLCertificate* cert) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000556 DCHECK(session_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000557 // Produces a chain of StatsReports representing this certificate and the rest
558 // of its chain, and adds those reports to |reports_|. The return value is
559 // the id of the leaf report. The provided cert must be non-null, so at least
560 // one report will always be provided and the returned string will never be
561 // empty.
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000562 DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000563
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000564 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000565 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000566 if (cert->GetChain(chain.accept())) {
567 // This loop runs in reverse, i.e. from root to leaf, so that each
568 // certificate's issuer's report ID is known before the child certificate's
569 // report is generated. The root certificate does not have an issuer ID
570 // value.
571 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000572 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000573 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000574 }
575 }
576 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000577 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000578}
579
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000580StatsReport* StatsCollector::AddConnectionInfoReport(
581 const std::string& content_name, int component, int connection_id,
582 const StatsReport::Id& channel_report_id,
583 const cricket::ConnectionInfo& info) {
584 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
585 connection_id));
586 StatsReport* report = reports_.ReplaceOrAddNew(id);
587 report->set_timestamp(stats_gathering_started_);
588
589 const BoolForAdd bools[] = {
590 { StatsReport::kStatsValueNameActiveConnection, info.best_connection },
591 { StatsReport::kStatsValueNameReadable, info.readable },
592 { StatsReport::kStatsValueNameWritable, info.writable },
593 };
594 for (const auto& b : bools)
595 report->AddBoolean(b.name, b.value);
596
597 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
598 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
599 AddCandidateReport(info.local_candidate, true)->id());
600 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
601 AddCandidateReport(info.remote_candidate, false)->id());
602
603 const Int64ForAdd int64s[] = {
604 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
605 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
606 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
607 { StatsReport::kStatsValueNameRtt, info.rtt },
608 { StatsReport::kStatsValueNameSendPacketsDiscarded,
609 info.sent_discarded_packets },
610 };
611 for (const auto& i : int64s)
612 report->AddInt64(i.name, i.value);
613
614 report->AddString(StatsReport::kStatsValueNameLocalAddress,
615 info.local_candidate.address().ToString());
616 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
617 info.local_candidate.type());
618 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
619 info.remote_candidate.address().ToString());
620 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
621 info.remote_candidate.type());
622 report->AddString(StatsReport::kStatsValueNameTransportType,
623 info.local_candidate.protocol());
624
625 return report;
626}
627
628StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000629 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000630 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000631 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
632 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000633 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000634 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000635 report->set_timestamp(stats_gathering_started_);
636 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000637 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
638 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000639 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000640 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
641 candidate.address().ipaddr().ToString());
642 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
643 candidate.address().PortAsString());
644 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
645 candidate.priority());
646 report->AddString(StatsReport::kStatsValueNameCandidateType,
647 IceCandidateTypeToStatsType(candidate.type()));
648 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
649 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000650 }
651
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000652 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000653}
654
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655void StatsCollector::ExtractSessionInfo() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000656 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000657
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000659 StatsReport::Id id(StatsReport::NewTypedId(
660 StatsReport::kStatsReportTypeSession, session_->id()));
661 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000662 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000663 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
664 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665
666 cricket::SessionStats stats;
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000667 if (!session_->GetTransportStats(&stats)) {
668 return;
669 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000671 // Store the proxy map away for use in SSRC reporting.
672 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
673 // signaling thread after fetching them on the worker thread, then just use
674 // the proxy map directly from the session stats.
675 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
676 // data.
677 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000678
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000679 for (const auto& transport_iter : stats.transport_stats) {
680 // Attempt to get a copy of the certificates from the transport and
681 // expose them in stats reports. All channels in a transport share the
682 // same local and remote certificates.
683 //
684 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
685 // invoke method calls on the worker thread and block this thread, but
686 // messages are still processed on this thread, which may blow way the
687 // existing transports. So we cannot reuse |transport| after these calls.
688 StatsReport::Id local_cert_report_id, remote_cert_report_id;
689
690 cricket::Transport* transport =
691 session_->GetTransport(transport_iter.second.content_name);
692 rtc::scoped_ptr<rtc::SSLIdentity> identity;
693 if (transport && transport->GetIdentity(identity.accept())) {
694 StatsReport* r = AddCertificateReports(&(identity->certificate()));
695 if (r)
696 local_cert_report_id = r->id();
697 }
698
699 transport = session_->GetTransport(transport_iter.second.content_name);
700 rtc::scoped_ptr<rtc::SSLCertificate> cert;
701 if (transport && transport->GetRemoteCertificate(cert.accept())) {
702 StatsReport* r = AddCertificateReports(cert.get());
703 if (r)
704 remote_cert_report_id = r->id();
705 }
706
707 for (const auto& channel_iter : transport_iter.second.channel_stats) {
708 StatsReport::Id id(StatsReport::NewComponentId(
709 transport_iter.second.content_name, channel_iter.component));
710 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
711 channel_report->set_timestamp(stats_gathering_started_);
712 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
713 channel_iter.component);
714 if (local_cert_report_id.get()) {
715 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
716 local_cert_report_id);
717 }
718 if (remote_cert_report_id.get()) {
719 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
720 remote_cert_report_id);
721 }
722 const std::string& srtp_cipher = channel_iter.srtp_cipher;
723 if (!srtp_cipher.empty()) {
724 channel_report->AddString(StatsReport::kStatsValueNameSrtpCipher,
725 srtp_cipher);
726 }
727 const std::string& ssl_cipher = channel_iter.ssl_cipher;
728 if (!ssl_cipher.empty()) {
729 channel_report->AddString(StatsReport::kStatsValueNameDtlsCipher,
730 ssl_cipher);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000731 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000732
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000733 int connection_id = 0;
734 for (const cricket::ConnectionInfo& info :
735 channel_iter.connection_infos) {
736 StatsReport* connection_report = AddConnectionInfoReport(
737 transport_iter.first, channel_iter.component, connection_id++,
738 channel_report->id(), info);
739 if (info.best_connection) {
740 channel_report->AddId(
741 StatsReport::kStatsValueNameSelectedCandidatePairId,
742 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744 }
745 }
746 }
747}
748
749void StatsCollector::ExtractVoiceInfo() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000750 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000751
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 if (!session_->voice_channel()) {
753 return;
754 }
755 cricket::VoiceMediaInfo voice_info;
756 if (!session_->voice_channel()->GetStats(&voice_info)) {
757 LOG(LS_ERROR) << "Failed to get voice channel stats.";
758 return;
759 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000760
761 // TODO(tommi): The above code should run on the worker thread and post the
762 // results back to the signaling thread, where we can add data to the reports.
763 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
764
765 StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_,
766 session_->voice_channel()->content_name()));
767 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 LOG(LS_ERROR) << "Failed to get transport name for proxy "
769 << session_->voice_channel()->content_name();
770 return;
771 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000772
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000773 ExtractStatsFromList(voice_info.receivers, transport_id, this,
774 StatsReport::kReceive);
775 ExtractStatsFromList(voice_info.senders, transport_id, this,
776 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000777
778 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779}
780
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000781void StatsCollector::ExtractVideoInfo(
782 PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000783 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000784
785 if (!session_->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000787
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 cricket::VideoMediaInfo video_info;
pbos@webrtc.org058b1f12015-03-04 08:54:32 +0000789 if (!session_->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 LOG(LS_ERROR) << "Failed to get video channel stats.";
791 return;
792 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000793
794 // TODO(tommi): The above code should run on the worker thread and post the
795 // results back to the signaling thread, where we can add data to the reports.
796 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
797
798 StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_,
799 session_->video_channel()->content_name()));
800 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 LOG(LS_ERROR) << "Failed to get transport name for proxy "
802 << session_->video_channel()->content_name();
803 return;
804 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000805 ExtractStatsFromList(video_info.receivers, transport_id, this,
806 StatsReport::kReceive);
807 ExtractStatsFromList(video_info.senders, transport_id, this,
808 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 if (video_info.bw_estimations.size() != 1) {
810 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
811 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000812 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
813 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000815 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
817}
818
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000819void StatsCollector::ExtractDataInfo() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000820 DCHECK(session_->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000821
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000822 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
823
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000824 for (const auto& dc :
825 session_->mediastream_signaling()->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000826 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000827 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000828 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000829 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000830 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
831 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
832 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
833 report->AddString(StatsReport::kStatsValueNameState,
834 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000835 }
836}
837
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000838StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000839 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000840 StatsReport::Direction direction) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000841 DCHECK(session_->signaling_thread()->IsCurrent());
842 DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000843 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000844 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000845}
846
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000847void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000848 DCHECK(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000849 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000850 for (const auto& it : local_audio_tracks_) {
851 AudioTrackInterface* track = it.first;
852 uint32 ssrc = it.second;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000853 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000854 rtc::ToString<uint32>(ssrc),
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000855 StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000856 if (report == NULL) {
857 // This can happen if a local audio track is added to a stream on the
858 // fly and the report has not been set up yet. Do nothing in this case.
859 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
860 continue;
861 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000862
863 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000864 const StatsReport::Value* v =
865 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000866 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000867 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000868
869 UpdateReportFromAudioTrack(track, report);
870 }
871}
872
873void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
874 StatsReport* report) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000875 DCHECK(session_->signaling_thread()->IsCurrent());
876 DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000877
878 int signal_level = 0;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000879 if (!track->GetSignalLevel(&signal_level))
880 signal_level = -1;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000881
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000882 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000883 track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000884
885 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000886 if (audio_processor.get())
887 audio_processor->GetStats(&stats);
888
889 SetAudioProcessingStats(report, signal_level, stats.typing_noise_detected,
890 stats.echo_return_loss, stats.echo_return_loss_enhancement,
891 stats.echo_delay_median_ms, stats.aec_quality_min,
892 stats.echo_delay_std_ms);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000893}
894
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000895bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000896 StatsReport::Direction direction) {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000897 DCHECK(session_->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000898 if (direction == StatsReport::kSend) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000899 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000900 LOG(LS_WARNING) << "The SSRC " << ssrc
901 << " is not associated with a sending track";
902 return false;
903 }
904 } else {
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000905 DCHECK(direction == StatsReport::kReceive);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000906 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000907 LOG(LS_WARNING) << "The SSRC " << ssrc
908 << " is not associated with a receiving track";
909 return false;
910 }
911 }
912
913 return true;
914}
915
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000916void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000917 stats_gathering_started_ = 0;
918}
919
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920} // namespace webrtc