blob: 0567c3fe347b74a027dc445da8ca5b5cb504f670 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/statscollector.h"
29
30#include <utility>
31#include <vector>
32
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/base64.h"
35#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000038using rtc::scoped_ptr;
39
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000043// The following is the enum RTCStatsIceCandidateType from
44// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
45// our stats report for ice candidate type could conform to that.
46const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
47const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
48const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
49const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
50
51// Strings used by the stats collector to report adapter types. This fits the
52// general stype of http://w3c.github.io/webrtc-stats than what
53// AdapterTypeToString does.
54const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
55const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
56const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
57const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000058const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000059
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000060template<typename ValueType>
61struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000062 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000063 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000064};
65
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000066typedef TypeForAdd<bool> BoolForAdd;
67typedef TypeForAdd<float> FloatForAdd;
68typedef TypeForAdd<int64> Int64ForAdd;
69typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000070
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000071StatsReport::Id GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
72 const std::string& proxy) {
73 DCHECK(!proxy.empty());
tommi@webrtc.org47218952014-07-15 19:22:37 +000074 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000075 if (found == map.end())
76 return StatsReport::Id();
tommi@webrtc.org47218952014-07-15 19:22:37 +000077
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000078 return StatsReport::NewComponentId(
79 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000080}
81
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000082void AddTrackReport(StatsCollection* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000083 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000084 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000085 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000086 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000087 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
xians@webrtc.org01bda202014-07-09 07:38:38 +000088}
89
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090template <class TrackVector>
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000091void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000092 for (const auto& track : tracks)
xians@webrtc.org01bda202014-07-09 07:38:38 +000093 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094}
95
tommi@webrtc.org92f40182015-03-04 15:25:19 +000096void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
97 StatsReport* report) {
98 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
99 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
100 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
101}
102
103void SetAudioProcessingStats(StatsReport* report, int signal_level,
104 bool typing_noise_detected, int echo_return_loss,
105 int echo_return_loss_enhancement, int echo_delay_median_ms,
106 float aec_quality_min, int echo_delay_std_ms) {
107 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
108 typing_noise_detected);
109 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
110 aec_quality_min);
111 // Don't overwrite the previous signal level if it's not available now.
112 if (signal_level >= 0)
113 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
114 const IntForAdd ints[] = {
115 { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss },
116 { StatsReport::kStatsValueNameEchoReturnLossEnhancement,
117 echo_return_loss_enhancement },
118 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
119 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
120 };
121 for (const auto& i : ints)
122 report->AddInt(i.name, i.value);
123}
124
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000126 const FloatForAdd floats[] = {
127 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
128 { StatsReport::kStatsValueNameSecondaryDecodedRate,
129 info.secondary_decoded_rate },
130 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
131 };
132
133 const IntForAdd ints[] = {
134 { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level },
135 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
136 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
137 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
138 { StatsReport::kStatsValueNameDecodingCTSG,
139 info.decoding_calls_to_silence_generator },
140 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
141 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
142 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
143 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
144 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
145 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
146 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
147 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
148 info.jitter_buffer_preferred_ms },
149 };
150
151 for (const auto& f : floats)
152 report->AddFloat(f.name, f.value);
153
154 for (const auto& i : ints)
155 report->AddInt(i.name, i.value);
156
157 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000159 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000160 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000161
162 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163}
164
165void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000166 ExtractCommonSendProperties(info, report);
167
168 SetAudioProcessingStats(report, info.audio_level, info.typing_noise_detected,
169 info.echo_return_loss, info.echo_return_loss_enhancement,
170 info.echo_delay_median_ms, info.aec_quality_min, info.echo_delay_std_ms);
171
172 const IntForAdd ints[] = {
173 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
174 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
175 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
176 };
177
178 for (const auto& i : ints)
179 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180}
181
182void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000183 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000185 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000186 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000187 const IntForAdd ints[] = {
188 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
189 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
190 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
191 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
192 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
193 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
194 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
195 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
196 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
197 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
198 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
199 info.min_playout_delay_ms },
200 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
201 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
202 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
203 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
204 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
205 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
206 };
207
208 for (const auto& i : ints)
209 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210}
211
212void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000213 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000215 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
216 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000217 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
218 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000219 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
220 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000221
222 const IntForAdd ints[] = {
223 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
224 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
225 { StatsReport::kStatsValueNameCaptureJitterMs, info.capture_jitter_ms },
226 { StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
227 info.capture_queue_delay_ms_per_s },
228 { StatsReport::kStatsValueNameEncodeUsagePercent,
229 info.encode_usage_percent },
230 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
231 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
232 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
233 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
234 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
235 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
236 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
237 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
238 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
239 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
240 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
241 };
242
243 for (const auto& i : ints)
244 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245}
246
247void ExtractStats(const cricket::BandwidthEstimationInfo& info,
248 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000249 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 StatsReport* report) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000251 ASSERT(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000253 report->set_timestamp(stats_gathering_started);
254 const IntForAdd ints[] = {
255 { StatsReport::kStatsValueNameAvailableSendBandwidth,
256 info.available_send_bandwidth },
257 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
258 info.available_recv_bandwidth },
259 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
260 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
261 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
262 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
263 };
264 for (const auto& i : ints)
265 report->AddInt(i.name, i.value);
266 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267}
268
wu@webrtc.org97077a32013-10-25 21:18:33 +0000269void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
270 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000271 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000272 // TODO(hta): Extract some stats here.
273}
274
275void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
276 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000277 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000278 // TODO(hta): Extract some stats here.
279}
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000282// In order to use the template, the functions that are called from it,
283// ExtractStats and ExtractRemoteStats, must be defined and overloaded
284// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285template<typename T>
286void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000287 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000288 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000289 StatsReport::Direction direction) {
290 for (const auto& d : data) {
291 uint32 ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000292 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000293 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000294 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
295 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000296 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000297 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000298
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000299 if (!d.remote_stats.empty()) {
300 report = collector->PrepareReport(false, ssrc, transport_id, direction);
301 if (report)
302 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000303 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000305}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306
307} // namespace
308
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000309const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
310 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
311 return STATSREPORT_LOCAL_PORT_TYPE;
312 }
313 if (candidate_type == cricket::STUN_PORT_TYPE) {
314 return STATSREPORT_STUN_PORT_TYPE;
315 }
316 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
317 return STATSREPORT_PRFLX_PORT_TYPE;
318 }
319 if (candidate_type == cricket::RELAY_PORT_TYPE) {
320 return STATSREPORT_RELAY_PORT_TYPE;
321 }
322 ASSERT(false);
323 return "unknown";
324}
325
326const char* AdapterTypeToStatsType(rtc::AdapterType type) {
327 switch (type) {
328 case rtc::ADAPTER_TYPE_UNKNOWN:
329 return "unknown";
330 case rtc::ADAPTER_TYPE_ETHERNET:
331 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
332 case rtc::ADAPTER_TYPE_WIFI:
333 return STATSREPORT_ADAPTER_TYPE_WIFI;
334 case rtc::ADAPTER_TYPE_CELLULAR:
335 return STATSREPORT_ADAPTER_TYPE_WWAN;
336 case rtc::ADAPTER_TYPE_VPN:
337 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000338 case rtc::ADAPTER_TYPE_LOOPBACK:
339 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000340 default:
341 ASSERT(false);
342 return "";
343 }
344}
345
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000346StatsCollector::StatsCollector(WebRtcSession* session)
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000347 : session_(session),
348 stats_gathering_started_(0) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000349 ASSERT(session_);
350}
351
352StatsCollector::~StatsCollector() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000353 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354}
355
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000356double StatsCollector::GetTimeNow() {
357 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
358}
359
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360// Adds a MediaStream with tracks that can be used as a |selector| in a call
361// to GetStats.
362void StatsCollector::AddStream(MediaStreamInterface* stream) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000363 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 ASSERT(stream != NULL);
365
366 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
367 &reports_);
368 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
369 &reports_);
370}
371
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000372void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
373 uint32 ssrc) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000374 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000375 ASSERT(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000376#if ENABLE_DEBUG
377 for (const auto& track : local_audio_tracks_)
378 ASSERT(track.first != audio_track || track.second != ssrc);
379#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000380
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000381 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000382
383 // Create the kStatsReportTypeTrack report for the new track if there is no
384 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000385 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
386 audio_track->id()));
387 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000388 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000389 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000390 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000391 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000392}
393
394void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
395 uint32 ssrc) {
396 ASSERT(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000397 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
398 local_audio_tracks_.end(),
399 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
400 return track.first == audio_track && track.second == ssrc;
401 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000402}
403
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000404void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 StatsReports* reports) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000406 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 ASSERT(reports != NULL);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000408 ASSERT(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000410 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
411
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000413 reports->reserve(reports_.size());
414 for (auto* r : reports_)
415 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000416 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 }
418
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000419 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
420 StatsReport::kStatsReportTypeSession, session_->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000421 if (report)
422 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000424 report = reports_.Find(StatsReport::NewTypedId(
425 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000427 if (!report)
428 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000430 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431
432 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000433 for (const auto* r : reports_) {
434 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000436
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000437 const StatsReport::Value* v =
438 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000439 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000440 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442}
443
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000444void
445StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000446 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 double time_now = GetTimeNow();
448 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
449 // ms apart will be ignored.
450 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000451 if (stats_gathering_started_ != 0 &&
452 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 return;
454 }
455 stats_gathering_started_ = time_now;
456
457 if (session_) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000458 // TODO(tommi): All of these hop over to the worker thread to fetch
459 // information. We could use an AsyncInvoker to run all of these and post
460 // the information back to the signaling thread where we can create and
461 // update stats reports. That would also clean up the threading story a bit
462 // since we'd be creating/updating the stats report objects consistently on
463 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 ExtractSessionInfo();
465 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000466 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000467 ExtractDataInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 }
469}
470
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000471StatsReport* StatsCollector::PrepareReport(
472 bool local,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000473 uint32 ssrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000474 const StatsReport::Id& transport_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000475 StatsReport::Direction direction) {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000476 ASSERT(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000477 StatsReport::Id id(StatsReport::NewIdWithDirection(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000478 local ? StatsReport::kStatsReportTypeSsrc :
479 StatsReport::kStatsReportTypeRemoteSsrc,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000480 rtc::ToString<uint32>(ssrc), direction));
481 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482
xians@webrtc.org01bda202014-07-09 07:38:38 +0000483 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000485 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000486 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000487 // The ssrc is not used by any track or existing report, return NULL
488 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000490 }
491
492 // The ssrc is not used by any existing track. Keeps the old track id
493 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000494 const StatsReport::Value* v =
495 report->FindValue(StatsReport::kStatsValueNameTrackId);
496 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000497 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 }
499
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000500 if (!report)
501 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000503 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000504 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000506 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000507 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000509 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 return report;
511}
512
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000513StatsReport* StatsCollector::AddOneCertificateReport(
514 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000515 ASSERT(session_->signaling_thread()->IsCurrent());
516
wu@webrtc.org4551b792013-10-09 15:37:36 +0000517 // TODO(bemasc): Move this computation to a helper class that caches these
518 // values to reduce CPU use in GetStats. This will require adding a fast
519 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000520
521 std::string digest_algorithm;
522 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000523 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000524
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000525 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
526 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000527
528 // SSLFingerprint::Create can fail if the algorithm returned by
529 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
530 // implementation of SSLCertificate::ComputeDigest. This currently happens
531 // with MD5- and SHA-224-signed certificates when linked to libNSS.
532 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000533 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000534
wu@webrtc.org4551b792013-10-09 15:37:36 +0000535 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
536
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000537 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000538 cert->ToDER(&der_buffer);
539 std::string der_base64;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540 rtc::Base64::EncodeFromArray(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000541 der_buffer.data(), der_buffer.length(), &der_base64);
542
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000543 StatsReport::Id id(StatsReport::NewTypedId(
544 StatsReport::kStatsReportTypeCertificate, fingerprint));
545 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000546 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000547 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
548 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
549 digest_algorithm);
550 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000551 if (issuer)
552 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
553 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000554}
555
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000556StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557 const rtc::SSLCertificate* cert) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000558 ASSERT(session_->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000559 // Produces a chain of StatsReports representing this certificate and the rest
560 // of its chain, and adds those reports to |reports_|. The return value is
561 // the id of the leaf report. The provided cert must be non-null, so at least
562 // one report will always be provided and the returned string will never be
563 // empty.
564 ASSERT(cert != NULL);
565
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000566 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000567 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000568 if (cert->GetChain(chain.accept())) {
569 // This loop runs in reverse, i.e. from root to leaf, so that each
570 // certificate's issuer's report ID is known before the child certificate's
571 // report is generated. The root certificate does not have an issuer ID
572 // value.
573 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000574 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000575 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000576 }
577 }
578 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000579 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000580}
581
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000582StatsReport* StatsCollector::AddConnectionInfoReport(
583 const std::string& content_name, int component, int connection_id,
584 const StatsReport::Id& channel_report_id,
585 const cricket::ConnectionInfo& info) {
586 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
587 connection_id));
588 StatsReport* report = reports_.ReplaceOrAddNew(id);
589 report->set_timestamp(stats_gathering_started_);
590
591 const BoolForAdd bools[] = {
592 { StatsReport::kStatsValueNameActiveConnection, info.best_connection },
593 { StatsReport::kStatsValueNameReadable, info.readable },
594 { StatsReport::kStatsValueNameWritable, info.writable },
595 };
596 for (const auto& b : bools)
597 report->AddBoolean(b.name, b.value);
598
599 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
600 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
601 AddCandidateReport(info.local_candidate, true)->id());
602 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
603 AddCandidateReport(info.remote_candidate, false)->id());
604
605 const Int64ForAdd int64s[] = {
606 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
607 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
608 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
609 { StatsReport::kStatsValueNameRtt, info.rtt },
610 { StatsReport::kStatsValueNameSendPacketsDiscarded,
611 info.sent_discarded_packets },
612 };
613 for (const auto& i : int64s)
614 report->AddInt64(i.name, i.value);
615
616 report->AddString(StatsReport::kStatsValueNameLocalAddress,
617 info.local_candidate.address().ToString());
618 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
619 info.local_candidate.type());
620 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
621 info.remote_candidate.address().ToString());
622 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
623 info.remote_candidate.type());
624 report->AddString(StatsReport::kStatsValueNameTransportType,
625 info.local_candidate.protocol());
626
627 return report;
628}
629
630StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000631 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000632 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000633 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
634 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000635 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000636 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000637 report->set_timestamp(stats_gathering_started_);
638 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000639 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
640 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000641 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000642 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
643 candidate.address().ipaddr().ToString());
644 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
645 candidate.address().PortAsString());
646 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
647 candidate.priority());
648 report->AddString(StatsReport::kStatsValueNameCandidateType,
649 IceCandidateTypeToStatsType(candidate.type()));
650 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
651 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000652 }
653
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000654 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000655}
656
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657void StatsCollector::ExtractSessionInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000658 ASSERT(session_->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000659
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000661 StatsReport::Id id(StatsReport::NewTypedId(
662 StatsReport::kStatsReportTypeSession, session_->id()));
663 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000664 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000665 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
666 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667
668 cricket::SessionStats stats;
669 if (session_->GetStats(&stats)) {
670 // Store the proxy map away for use in SSRC reporting.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000671 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
672 // signaling thread after fetching them on the worker thread, then just use
673 // the proxy map directly from the session stats.
674 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
675 // data.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 proxy_to_transport_ = stats.proxy_to_transport;
677
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000678 for (const auto& transport_iter : stats.transport_stats) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000679 // Attempt to get a copy of the certificates from the transport and
680 // expose them in stats reports. All channels in a transport share the
681 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000682 //
683 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
684 // invoke method calls on the worker thread and block this thread, but
685 // messages are still processed on this thread, which may blow way the
686 // existing transports. So we cannot reuse |transport| after these calls.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000687 StatsReport::Id local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000688
wu@webrtc.org4551b792013-10-09 15:37:36 +0000689 cricket::Transport* transport =
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000690 session_->GetTransport(transport_iter.second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000691 rtc::scoped_ptr<rtc::SSLIdentity> identity;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000692 if (transport && transport->GetIdentity(identity.accept())) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000693 StatsReport* r = AddCertificateReports(&(identity->certificate()));
694 if (r)
695 local_cert_report_id = r->id();
wu@webrtc.org4551b792013-10-09 15:37:36 +0000696 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000697
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000698 transport = session_->GetTransport(transport_iter.second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000699 rtc::scoped_ptr<rtc::SSLCertificate> cert;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000700 if (transport && transport->GetRemoteCertificate(cert.accept())) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000701 StatsReport* r = AddCertificateReports(cert.get());
702 if (r)
703 remote_cert_report_id = r->id();
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000704 }
705
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000706 for (const auto& channel_iter : transport_iter.second.channel_stats) {
707 StatsReport::Id id(StatsReport::NewComponentId(
708 transport_iter.second.content_name, channel_iter.component));
709 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000710 channel_report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000711 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000712 channel_iter.component);
713 if (local_cert_report_id.get()) {
714 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
715 local_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000716 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000717 if (remote_cert_report_id.get()) {
718 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
719 remote_cert_report_id);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000720 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000721 const std::string& srtp_cipher = channel_iter.srtp_cipher;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000722 if (!srtp_cipher.empty()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000723 channel_report->AddString(StatsReport::kStatsValueNameSrtpCipher,
724 srtp_cipher);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000725 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000726 const std::string& ssl_cipher = channel_iter.ssl_cipher;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000727 if (!ssl_cipher.empty()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000728 channel_report->AddString(StatsReport::kStatsValueNameDtlsCipher,
729 ssl_cipher);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000730 }
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000731
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000732 int connection_id = 0;
733 for (const cricket::ConnectionInfo& info :
734 channel_iter.connection_infos) {
735 StatsReport* connection_report = AddConnectionInfoReport(
736 transport_iter.first, channel_iter.component, connection_id++,
737 channel_report->id(), info);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000738 if (info.best_connection) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000739 channel_report->AddId(
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000740 StatsReport::kStatsValueNameSelectedCandidatePairId,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000741 connection_report->id());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000742 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744 }
745 }
746 }
747}
748
749void StatsCollector::ExtractVoiceInfo() {
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000750 ASSERT(session_->signaling_thread()->IsCurrent());
751
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.org69bc5a32014-12-15 09:44:48 +0000783 ASSERT(session_->signaling_thread()->IsCurrent());
784
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() {
820 ASSERT(session_->signaling_thread()->IsCurrent());
821
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.org69bc5a32014-12-15 09:44:48 +0000841 ASSERT(session_->signaling_thread()->IsCurrent());
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000842 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
843 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.org69bc5a32014-12-15 09:44:48 +0000848 ASSERT(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.org69bc5a32014-12-15 09:44:48 +0000875 ASSERT(session_->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000876 ASSERT(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.org69bc5a32014-12-15 09:44:48 +0000897 ASSERT(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.org4fb7e252015-01-21 11:36:18 +0000905 ASSERT(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