blob: 347a84640cf9973a72d86e5133d5b4bc038dbb72 [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
deadbeefab9b2d12015-10-14 11:33:11 -070033#include "talk/app/webrtc/peerconnection.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000034#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/base64.h"
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +000036#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037#include "webrtc/base/scoped_ptr.h"
38#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000040using rtc::scoped_ptr;
41
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000045// The following is the enum RTCStatsIceCandidateType from
46// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
47// our stats report for ice candidate type could conform to that.
48const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
49const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
50const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
51const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
52
53// Strings used by the stats collector to report adapter types. This fits the
54// general stype of http://w3c.github.io/webrtc-stats than what
55// AdapterTypeToString does.
56const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
57const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
58const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
59const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000060const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000061
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000062template<typename ValueType>
63struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000064 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000065 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000066};
67
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000068typedef TypeForAdd<bool> BoolForAdd;
69typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020070typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000071typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000072
deadbeefd59daf82015-10-14 15:02:44 -070073StatsReport::Id GetTransportIdFromProxy(const ProxyTransportMap& map,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000074 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070075 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070076 auto found = map.find(proxy);
77 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000078 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070079 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000080
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000081 return StatsReport::NewComponentId(
82 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000083}
84
jbauchbe24c942015-06-22 15:06:43 -070085StatsReport* AddTrackReport(StatsCollection* reports,
86 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000087 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000088 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000089 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000090 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000091 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070092 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000093}
94
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095template <class TrackVector>
jbauchbe24c942015-06-22 15:06:43 -070096void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
97 TrackIdMap& track_ids) {
98 for (const auto& track : tracks) {
99 const std::string& track_id = track->id();
100 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -0700101 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -0700102 track_ids[track_id] = report;
103 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104}
105
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000106void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
107 StatsReport* report) {
108 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
109 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
110 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
111}
112
pbosf42376c2015-08-28 07:35:32 -0700113void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
114 StatsReport* report) {
115 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
116}
117
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000118void SetAudioProcessingStats(StatsReport* report, int signal_level,
119 bool typing_noise_detected, int echo_return_loss,
120 int echo_return_loss_enhancement, int echo_delay_median_ms,
121 float aec_quality_min, int echo_delay_std_ms) {
122 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
123 typing_noise_detected);
124 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
125 aec_quality_min);
126 // Don't overwrite the previous signal level if it's not available now.
127 if (signal_level >= 0)
128 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
129 const IntForAdd ints[] = {
130 { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss },
131 { StatsReport::kStatsValueNameEchoReturnLossEnhancement,
132 echo_return_loss_enhancement },
133 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
134 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
135 };
136 for (const auto& i : ints)
137 report->AddInt(i.name, i.value);
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700141 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000142 const FloatForAdd floats[] = {
143 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
144 { StatsReport::kStatsValueNameSecondaryDecodedRate,
145 info.secondary_decoded_rate },
146 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200147 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
148 { StatsReport::kStatsValueNamePreemptiveExpandRate,
149 info.preemptive_expand_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000150 };
151
152 const IntForAdd ints[] = {
153 { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level },
154 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
155 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
156 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
157 { StatsReport::kStatsValueNameDecodingCTSG,
158 info.decoding_calls_to_silence_generator },
159 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
160 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
161 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
162 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
163 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
164 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
165 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
166 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
167 info.jitter_buffer_preferred_ms },
168 };
169
170 for (const auto& f : floats)
171 report->AddFloat(f.name, f.value);
172
173 for (const auto& i : ints)
174 report->AddInt(i.name, i.value);
175
176 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000178 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000179 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180}
181
182void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000183 ExtractCommonSendProperties(info, report);
184
185 SetAudioProcessingStats(report, info.audio_level, info.typing_noise_detected,
186 info.echo_return_loss, info.echo_return_loss_enhancement,
187 info.echo_delay_median_ms, info.aec_quality_min, info.echo_delay_std_ms);
188
189 const IntForAdd ints[] = {
190 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
191 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
192 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
193 };
194
195 for (const auto& i : ints)
196 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197}
198
199void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700200 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000201 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000203 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000204 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000205 const IntForAdd ints[] = {
206 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
207 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
208 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
209 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
210 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
211 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
212 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
213 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
214 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
215 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
216 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
217 info.min_playout_delay_ms },
218 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
219 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
220 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
221 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
222 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
223 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
224 };
225
226 for (const auto& i : ints)
227 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228}
229
230void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000231 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000233 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
234 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000235 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
236 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000237 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
238 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000239
240 const IntForAdd ints[] = {
241 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
242 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000243 { StatsReport::kStatsValueNameEncodeUsagePercent,
244 info.encode_usage_percent },
245 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
246 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
247 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
248 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
249 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
250 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
251 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
252 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
253 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
254 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
255 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
256 };
257
258 for (const auto& i : ints)
259 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260}
261
262void ExtractStats(const cricket::BandwidthEstimationInfo& info,
263 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000264 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700266 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000268 report->set_timestamp(stats_gathering_started);
269 const IntForAdd ints[] = {
270 { StatsReport::kStatsValueNameAvailableSendBandwidth,
271 info.available_send_bandwidth },
272 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
273 info.available_recv_bandwidth },
274 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
275 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
276 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
277 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
278 };
279 for (const auto& i : ints)
280 report->AddInt(i.name, i.value);
281 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282}
283
wu@webrtc.org97077a32013-10-25 21:18:33 +0000284void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
285 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000286 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000287 // TODO(hta): Extract some stats here.
288}
289
290void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
291 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000292 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000293 // TODO(hta): Extract some stats here.
294}
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000297// In order to use the template, the functions that are called from it,
298// ExtractStats and ExtractRemoteStats, must be defined and overloaded
299// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300template<typename T>
301void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000302 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000303 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000304 StatsReport::Direction direction) {
305 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200306 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000307 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000308 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000309 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
310 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000311 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000312 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000313
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000314 if (!d.remote_stats.empty()) {
315 report = collector->PrepareReport(false, ssrc, transport_id, direction);
316 if (report)
317 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000318 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000320}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321
322} // namespace
323
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000324const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
325 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
326 return STATSREPORT_LOCAL_PORT_TYPE;
327 }
328 if (candidate_type == cricket::STUN_PORT_TYPE) {
329 return STATSREPORT_STUN_PORT_TYPE;
330 }
331 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
332 return STATSREPORT_PRFLX_PORT_TYPE;
333 }
334 if (candidate_type == cricket::RELAY_PORT_TYPE) {
335 return STATSREPORT_RELAY_PORT_TYPE;
336 }
henrikg91d6ede2015-09-17 00:24:34 -0700337 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000338 return "unknown";
339}
340
341const char* AdapterTypeToStatsType(rtc::AdapterType type) {
342 switch (type) {
343 case rtc::ADAPTER_TYPE_UNKNOWN:
344 return "unknown";
345 case rtc::ADAPTER_TYPE_ETHERNET:
346 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
347 case rtc::ADAPTER_TYPE_WIFI:
348 return STATSREPORT_ADAPTER_TYPE_WIFI;
349 case rtc::ADAPTER_TYPE_CELLULAR:
350 return STATSREPORT_ADAPTER_TYPE_WWAN;
351 case rtc::ADAPTER_TYPE_VPN:
352 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000353 case rtc::ADAPTER_TYPE_LOOPBACK:
354 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000355 default:
henrikg91d6ede2015-09-17 00:24:34 -0700356 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000357 return "";
358 }
359}
360
deadbeefab9b2d12015-10-14 11:33:11 -0700361StatsCollector::StatsCollector(PeerConnection* pc)
362 : pc_(pc), stats_gathering_started_(0) {
363 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000364}
365
366StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700367 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368}
369
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000370double StatsCollector::GetTimeNow() {
371 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
372}
373
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374// Adds a MediaStream with tracks that can be used as a |selector| in a call
375// to GetStats.
376void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700377 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700378 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379
380 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700381 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700383 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384}
385
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000386void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200387 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700388 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700389 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000390#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000391 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700392 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000393#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000394
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000395 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000396
397 // Create the kStatsReportTypeTrack report for the new track if there is no
398 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000399 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
400 audio_track->id()));
401 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000402 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000403 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000404 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000405 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000406}
407
408void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200409 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700410 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000411 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
412 local_audio_tracks_.end(),
413 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
414 return track.first == audio_track && track.second == ssrc;
415 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000416}
417
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000418void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700420 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700421 RTC_DCHECK(reports != NULL);
422 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000424 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
425
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000427 reports->reserve(reports_.size());
428 for (auto* r : reports_)
429 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000430 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 }
432
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000433 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700434 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000435 if (report)
436 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000438 report = reports_.Find(StatsReport::NewTypedId(
439 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000441 if (!report)
442 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000444 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445
446 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000447 for (const auto* r : reports_) {
448 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000450
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000451 const StatsReport::Value* v =
452 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000453 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000454 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000458void
459StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700460 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 double time_now = GetTimeNow();
462 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
463 // ms apart will be ignored.
464 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000465 if (stats_gathering_started_ != 0 &&
466 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 return;
468 }
469 stats_gathering_started_ = time_now;
470
deadbeefab9b2d12015-10-14 11:33:11 -0700471 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000472 // TODO(tommi): All of these hop over to the worker thread to fetch
473 // information. We could use an AsyncInvoker to run all of these and post
474 // the information back to the signaling thread where we can create and
475 // update stats reports. That would also clean up the threading story a bit
476 // since we'd be creating/updating the stats report objects consistently on
477 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 ExtractSessionInfo();
479 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000480 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000481 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700482 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 }
484}
485
deadbeefab9b2d12015-10-14 11:33:11 -0700486StatsReport* StatsCollector::PrepareReport(
487 bool local,
488 uint32_t ssrc,
489 const StatsReport::Id& transport_id,
490 StatsReport::Direction direction) {
491 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000492 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200493 local ? StatsReport::kStatsReportTypeSsrc
494 : StatsReport::kStatsReportTypeRemoteSsrc,
495 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000496 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497
xians@webrtc.org01bda202014-07-09 07:38:38 +0000498 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000500 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000501 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000502 // The ssrc is not used by any track or existing report, return NULL
503 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000505 }
506
507 // The ssrc is not used by any existing track. Keeps the old track id
508 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000509 const StatsReport::Value* v =
510 report->FindValue(StatsReport::kStatsValueNameTrackId);
511 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000512 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 }
514
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000515 if (!report)
516 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000518 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000519 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000521 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000522 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000524 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 return report;
526}
527
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000528StatsReport* StatsCollector::AddOneCertificateReport(
529 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
deadbeefab9b2d12015-10-14 11:33:11 -0700530 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000531
wu@webrtc.org4551b792013-10-09 15:37:36 +0000532 // TODO(bemasc): Move this computation to a helper class that caches these
533 // values to reduce CPU use in GetStats. This will require adding a fast
534 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000535
536 std::string digest_algorithm;
537 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000538 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000539
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
541 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000542
543 // SSLFingerprint::Create can fail if the algorithm returned by
544 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
545 // implementation of SSLCertificate::ComputeDigest. This currently happens
546 // with MD5- and SHA-224-signed certificates when linked to libNSS.
547 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000548 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000549
wu@webrtc.org4551b792013-10-09 15:37:36 +0000550 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
551
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000553 cert->ToDER(&der_buffer);
554 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000555 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
556 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000557
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000558 StatsReport::Id id(StatsReport::NewTypedId(
559 StatsReport::kStatsReportTypeCertificate, fingerprint));
560 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000561 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000562 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
563 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
564 digest_algorithm);
565 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000566 if (issuer)
567 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
568 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000569}
570
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000571StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000572 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700573 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000574 // Produces a chain of StatsReports representing this certificate and the rest
575 // of its chain, and adds those reports to |reports_|. The return value is
576 // the id of the leaf report. The provided cert must be non-null, so at least
577 // one report will always be provided and the returned string will never be
578 // empty.
henrikg91d6ede2015-09-17 00:24:34 -0700579 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000580
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000581 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000582 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000583 if (cert->GetChain(chain.accept())) {
584 // This loop runs in reverse, i.e. from root to leaf, so that each
585 // certificate's issuer's report ID is known before the child certificate's
586 // report is generated. The root certificate does not have an issuer ID
587 // value.
588 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000590 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000591 }
592 }
593 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000594 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000595}
596
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000597StatsReport* StatsCollector::AddConnectionInfoReport(
598 const std::string& content_name, int component, int connection_id,
599 const StatsReport::Id& channel_report_id,
600 const cricket::ConnectionInfo& info) {
601 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
602 connection_id));
603 StatsReport* report = reports_.ReplaceOrAddNew(id);
604 report->set_timestamp(stats_gathering_started_);
605
606 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700607 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
608 {StatsReport::kStatsValueNameReceiving, info.receiving},
609 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000610 };
611 for (const auto& b : bools)
612 report->AddBoolean(b.name, b.value);
613
614 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
615 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
616 AddCandidateReport(info.local_candidate, true)->id());
617 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
618 AddCandidateReport(info.remote_candidate, false)->id());
619
620 const Int64ForAdd int64s[] = {
621 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
622 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
623 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
624 { StatsReport::kStatsValueNameRtt, info.rtt },
625 { StatsReport::kStatsValueNameSendPacketsDiscarded,
626 info.sent_discarded_packets },
627 };
628 for (const auto& i : int64s)
629 report->AddInt64(i.name, i.value);
630
631 report->AddString(StatsReport::kStatsValueNameLocalAddress,
632 info.local_candidate.address().ToString());
633 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
634 info.local_candidate.type());
635 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
636 info.remote_candidate.address().ToString());
637 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
638 info.remote_candidate.type());
639 report->AddString(StatsReport::kStatsValueNameTransportType,
640 info.local_candidate.protocol());
641
642 return report;
643}
644
645StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000646 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000647 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000648 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
649 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000650 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000651 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000652 report->set_timestamp(stats_gathering_started_);
653 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000654 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
655 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000656 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000657 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
658 candidate.address().ipaddr().ToString());
659 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
660 candidate.address().PortAsString());
661 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
662 candidate.priority());
663 report->AddString(StatsReport::kStatsValueNameCandidateType,
664 IceCandidateTypeToStatsType(candidate.type()));
665 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
666 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000667 }
668
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000669 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000670}
671
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700673 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000674
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000676 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700677 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000678 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000679 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000680 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700681 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682
deadbeefd59daf82015-10-14 15:02:44 -0700683 SessionStats stats;
deadbeefab9b2d12015-10-14 11:33:11 -0700684 if (!pc_->session()->GetTransportStats(&stats)) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000685 return;
686 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000688 // Store the proxy map away for use in SSRC reporting.
689 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
690 // signaling thread after fetching them on the worker thread, then just use
691 // the proxy map directly from the session stats.
692 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
693 // data.
694 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000695
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000696 for (const auto& transport_iter : stats.transport_stats) {
697 // Attempt to get a copy of the certificates from the transport and
698 // expose them in stats reports. All channels in a transport share the
699 // same local and remote certificates.
700 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000701 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200702 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700703 if (pc_->session()->GetLocalCertificate(
704 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200705 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000706 if (r)
707 local_cert_report_id = r->id();
708 }
709
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000710 rtc::scoped_ptr<rtc::SSLCertificate> cert;
deadbeefab9b2d12015-10-14 11:33:11 -0700711 if (pc_->session()->GetRemoteSSLCertificate(
712 transport_iter.second.transport_name, cert.accept())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000713 StatsReport* r = AddCertificateReports(cert.get());
714 if (r)
715 remote_cert_report_id = r->id();
716 }
717
718 for (const auto& channel_iter : transport_iter.second.channel_stats) {
719 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700720 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000721 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
722 channel_report->set_timestamp(stats_gathering_started_);
723 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
724 channel_iter.component);
725 if (local_cert_report_id.get()) {
726 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
727 local_cert_report_id);
728 }
729 if (remote_cert_report_id.get()) {
730 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
731 remote_cert_report_id);
732 }
733 const std::string& srtp_cipher = channel_iter.srtp_cipher;
734 if (!srtp_cipher.empty()) {
735 channel_report->AddString(StatsReport::kStatsValueNameSrtpCipher,
736 srtp_cipher);
737 }
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700738 int ssl_cipher = channel_iter.ssl_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700739 if (ssl_cipher &&
740 rtc::SSLStreamAdapter::GetSslCipherSuiteName(ssl_cipher).length()) {
741 channel_report->AddString(
742 StatsReport::kStatsValueNameDtlsCipher,
743 rtc::SSLStreamAdapter::GetSslCipherSuiteName(ssl_cipher));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000744 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000745
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000746 int connection_id = 0;
747 for (const cricket::ConnectionInfo& info :
748 channel_iter.connection_infos) {
749 StatsReport* connection_report = AddConnectionInfoReport(
750 transport_iter.first, channel_iter.component, connection_id++,
751 channel_report->id(), info);
752 if (info.best_connection) {
753 channel_report->AddId(
754 StatsReport::kStatsValueNameSelectedCandidatePairId,
755 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 }
757 }
758 }
759 }
760}
761
762void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700763 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000764
deadbeefab9b2d12015-10-14 11:33:11 -0700765 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 return;
767 }
768 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700769 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 LOG(LS_ERROR) << "Failed to get voice channel stats.";
771 return;
772 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000773
774 // TODO(tommi): The above code should run on the worker thread and post the
775 // results back to the signaling thread, where we can add data to the reports.
776 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
777
deadbeefab9b2d12015-10-14 11:33:11 -0700778 StatsReport::Id transport_id(GetTransportIdFromProxy(
779 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000780 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700782 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 return;
784 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000785
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000786 ExtractStatsFromList(voice_info.receivers, transport_id, this,
787 StatsReport::kReceive);
788 ExtractStatsFromList(voice_info.senders, transport_id, this,
789 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000790
791 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792}
793
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000794void StatsCollector::ExtractVideoInfo(
795 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700796 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000797
deadbeefab9b2d12015-10-14 11:33:11 -0700798 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000800
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700802 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 LOG(LS_ERROR) << "Failed to get video channel stats.";
804 return;
805 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000806
807 // TODO(tommi): The above code should run on the worker thread and post the
808 // results back to the signaling thread, where we can add data to the reports.
809 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
810
deadbeefab9b2d12015-10-14 11:33:11 -0700811 StatsReport::Id transport_id(GetTransportIdFromProxy(
812 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000813 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700815 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 return;
817 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000818 ExtractStatsFromList(video_info.receivers, transport_id, this,
819 StatsReport::kReceive);
820 ExtractStatsFromList(video_info.senders, transport_id, this,
821 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 if (video_info.bw_estimations.size() != 1) {
823 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
824 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000825 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
826 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000828 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 }
830}
831
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000832void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700833 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000834
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000835 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
836
deadbeefab9b2d12015-10-14 11:33:11 -0700837 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000838 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000839 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000840 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000841 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000842 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
843 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
844 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
845 report->AddString(StatsReport::kStatsValueNameState,
846 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000847 }
848}
849
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000850StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000851 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000852 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700853 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700854 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
855 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000856 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000857}
858
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000859void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700860 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000861 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000862 for (const auto& it : local_audio_tracks_) {
863 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200864 uint32_t ssrc = it.second;
865 StatsReport* report =
866 GetReport(StatsReport::kStatsReportTypeSsrc,
867 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000868 if (report == NULL) {
869 // This can happen if a local audio track is added to a stream on the
870 // fly and the report has not been set up yet. Do nothing in this case.
871 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
872 continue;
873 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000874
875 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000876 const StatsReport::Value* v =
877 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000878 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000879 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000880
jbauchbe24c942015-06-22 15:06:43 -0700881 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000882 UpdateReportFromAudioTrack(track, report);
883 }
884}
885
886void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
887 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700888 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700889 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000890
891 int signal_level = 0;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000892 if (!track->GetSignalLevel(&signal_level))
893 signal_level = -1;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000895 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896 track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000897
898 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000899 if (audio_processor.get())
900 audio_processor->GetStats(&stats);
901
902 SetAudioProcessingStats(report, signal_level, stats.typing_noise_detected,
903 stats.echo_return_loss, stats.echo_return_loss_enhancement,
904 stats.echo_delay_median_ms, stats.aec_quality_min,
905 stats.echo_delay_std_ms);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000906}
907
Peter Boström0c4e06b2015-10-07 12:23:21 +0200908bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
909 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000910 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700911 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000912 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700913 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000914 LOG(LS_WARNING) << "The SSRC " << ssrc
915 << " is not associated with a sending track";
916 return false;
917 }
918 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700919 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700920 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000921 LOG(LS_WARNING) << "The SSRC " << ssrc
922 << " is not associated with a receiving track";
923 return false;
924 }
925 }
926
927 return true;
928}
929
jbauchbe24c942015-06-22 15:06:43 -0700930void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700931 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700932
933 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
934
935 for (const auto& entry : track_ids_) {
936 StatsReport* report = entry.second;
937 report->set_timestamp(stats_gathering_started_);
938 }
jbauchbe24c942015-06-22 15:06:43 -0700939}
940
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000941void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000942 stats_gathering_started_ = 0;
943}
944
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945} // namespace webrtc