blob: 893e5866b0312a8d064ca027541ae93228b8570c [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
andrew2fe1cb02015-11-27 17:27:35 -0800118void SetAudioProcessingStats(StatsReport* report,
119 bool typing_noise_detected,
120 int echo_return_loss,
121 int echo_return_loss_enhancement,
122 int echo_delay_median_ms,
123 float aec_quality_min,
124 int echo_delay_std_ms) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000125 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
126 typing_noise_detected);
127 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
128 aec_quality_min);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000129 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
andrew2fe1cb02015-11-27 17:27:35 -0800185 SetAudioProcessingStats(
186 report, info.typing_noise_detected, info.echo_return_loss,
187 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
188 info.aec_quality_min, info.echo_delay_std_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000189
andrew2fe1cb02015-11-27 17:27:35 -0800190 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000191 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800192 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000193 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
194 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
195 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
196 };
197
198 for (const auto& i : ints)
199 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200}
201
202void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700203 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000204 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000206 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000207 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000208 const IntForAdd ints[] = {
209 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
210 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
211 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
212 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
213 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
214 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
215 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
216 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
217 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
218 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
219 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
220 info.min_playout_delay_ms },
221 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
222 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
223 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
224 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
225 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
226 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
227 };
228
229 for (const auto& i : ints)
230 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231}
232
233void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000234 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000236 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
237 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000238 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
239 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000240 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
241 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000242
243 const IntForAdd ints[] = {
244 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
245 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000246 { StatsReport::kStatsValueNameEncodeUsagePercent,
247 info.encode_usage_percent },
248 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
249 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
250 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
251 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
252 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
253 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
254 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
255 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
256 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
257 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
258 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
259 };
260
261 for (const auto& i : ints)
262 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263}
264
265void ExtractStats(const cricket::BandwidthEstimationInfo& info,
266 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000267 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700269 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000271 report->set_timestamp(stats_gathering_started);
272 const IntForAdd ints[] = {
273 { StatsReport::kStatsValueNameAvailableSendBandwidth,
274 info.available_send_bandwidth },
275 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
276 info.available_recv_bandwidth },
277 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
278 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
279 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
280 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
281 };
282 for (const auto& i : ints)
283 report->AddInt(i.name, i.value);
284 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285}
286
wu@webrtc.org97077a32013-10-25 21:18:33 +0000287void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
288 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000289 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000290 // TODO(hta): Extract some stats here.
291}
292
293void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
294 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000295 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000296 // TODO(hta): Extract some stats here.
297}
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000300// In order to use the template, the functions that are called from it,
301// ExtractStats and ExtractRemoteStats, must be defined and overloaded
302// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303template<typename T>
304void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000305 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000306 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000307 StatsReport::Direction direction) {
308 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200309 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000310 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000311 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000312 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
313 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000314 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000315 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000316
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000317 if (!d.remote_stats.empty()) {
318 report = collector->PrepareReport(false, ssrc, transport_id, direction);
319 if (report)
320 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000321 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000323}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324
325} // namespace
326
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000327const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
328 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
329 return STATSREPORT_LOCAL_PORT_TYPE;
330 }
331 if (candidate_type == cricket::STUN_PORT_TYPE) {
332 return STATSREPORT_STUN_PORT_TYPE;
333 }
334 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
335 return STATSREPORT_PRFLX_PORT_TYPE;
336 }
337 if (candidate_type == cricket::RELAY_PORT_TYPE) {
338 return STATSREPORT_RELAY_PORT_TYPE;
339 }
henrikg91d6ede2015-09-17 00:24:34 -0700340 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000341 return "unknown";
342}
343
344const char* AdapterTypeToStatsType(rtc::AdapterType type) {
345 switch (type) {
346 case rtc::ADAPTER_TYPE_UNKNOWN:
347 return "unknown";
348 case rtc::ADAPTER_TYPE_ETHERNET:
349 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
350 case rtc::ADAPTER_TYPE_WIFI:
351 return STATSREPORT_ADAPTER_TYPE_WIFI;
352 case rtc::ADAPTER_TYPE_CELLULAR:
353 return STATSREPORT_ADAPTER_TYPE_WWAN;
354 case rtc::ADAPTER_TYPE_VPN:
355 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000356 case rtc::ADAPTER_TYPE_LOOPBACK:
357 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000358 default:
henrikg91d6ede2015-09-17 00:24:34 -0700359 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000360 return "";
361 }
362}
363
deadbeefab9b2d12015-10-14 11:33:11 -0700364StatsCollector::StatsCollector(PeerConnection* pc)
365 : pc_(pc), stats_gathering_started_(0) {
366 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000367}
368
369StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700370 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000373double StatsCollector::GetTimeNow() {
374 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
375}
376
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377// Adds a MediaStream with tracks that can be used as a |selector| in a call
378// to GetStats.
379void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700380 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700381 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382
383 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700384 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700386 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387}
388
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000389void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200390 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700391 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700392 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000393#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000394 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700395 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000396#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000397
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000398 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000399
400 // Create the kStatsReportTypeTrack report for the new track if there is no
401 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000402 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
403 audio_track->id()));
404 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000405 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000406 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000407 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000408 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000409}
410
411void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200412 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700413 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000414 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
415 local_audio_tracks_.end(),
416 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
417 return track.first == audio_track && track.second == ssrc;
418 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000419}
420
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000421void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700423 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700424 RTC_DCHECK(reports != NULL);
425 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000427 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
428
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000430 reports->reserve(reports_.size());
431 for (auto* r : reports_)
432 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000433 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 }
435
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000436 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700437 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000438 if (report)
439 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000441 report = reports_.Find(StatsReport::NewTypedId(
442 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000444 if (!report)
445 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000447 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448
449 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000450 for (const auto* r : reports_) {
451 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000453
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000454 const StatsReport::Value* v =
455 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000456 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000457 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000461void
462StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700463 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 double time_now = GetTimeNow();
465 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
466 // ms apart will be ignored.
467 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000468 if (stats_gathering_started_ != 0 &&
469 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 return;
471 }
472 stats_gathering_started_ = time_now;
473
deadbeefab9b2d12015-10-14 11:33:11 -0700474 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000475 // TODO(tommi): All of these hop over to the worker thread to fetch
476 // information. We could use an AsyncInvoker to run all of these and post
477 // the information back to the signaling thread where we can create and
478 // update stats reports. That would also clean up the threading story a bit
479 // since we'd be creating/updating the stats report objects consistently on
480 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 ExtractSessionInfo();
482 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000483 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000484 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700485 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 }
487}
488
deadbeefab9b2d12015-10-14 11:33:11 -0700489StatsReport* StatsCollector::PrepareReport(
490 bool local,
491 uint32_t ssrc,
492 const StatsReport::Id& transport_id,
493 StatsReport::Direction direction) {
494 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000495 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200496 local ? StatsReport::kStatsReportTypeSsrc
497 : StatsReport::kStatsReportTypeRemoteSsrc,
498 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000499 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500
xians@webrtc.org01bda202014-07-09 07:38:38 +0000501 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000503 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000504 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000505 // The ssrc is not used by any track or existing report, return NULL
506 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000508 }
509
510 // The ssrc is not used by any existing track. Keeps the old track id
511 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000512 const StatsReport::Value* v =
513 report->FindValue(StatsReport::kStatsValueNameTrackId);
514 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000515 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 }
517
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000518 if (!report)
519 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000521 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000522 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000524 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000525 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000527 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 return report;
529}
530
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000531StatsReport* StatsCollector::AddOneCertificateReport(
532 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
deadbeefab9b2d12015-10-14 11:33:11 -0700533 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000534
wu@webrtc.org4551b792013-10-09 15:37:36 +0000535 // TODO(bemasc): Move this computation to a helper class that caches these
536 // values to reduce CPU use in GetStats. This will require adding a fast
537 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000538
539 std::string digest_algorithm;
540 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000541 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000542
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000543 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
544 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000545
546 // SSLFingerprint::Create can fail if the algorithm returned by
547 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
548 // implementation of SSLCertificate::ComputeDigest. This currently happens
549 // with MD5- and SHA-224-signed certificates when linked to libNSS.
550 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000551 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000552
wu@webrtc.org4551b792013-10-09 15:37:36 +0000553 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
554
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000556 cert->ToDER(&der_buffer);
557 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000558 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
559 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000560
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000561 StatsReport::Id id(StatsReport::NewTypedId(
562 StatsReport::kStatsReportTypeCertificate, fingerprint));
563 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000564 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000565 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
566 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
567 digest_algorithm);
568 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000569 if (issuer)
570 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
571 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000572}
573
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000574StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700576 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000577 // Produces a chain of StatsReports representing this certificate and the rest
578 // of its chain, and adds those reports to |reports_|. The return value is
579 // the id of the leaf report. The provided cert must be non-null, so at least
580 // one report will always be provided and the returned string will never be
581 // empty.
henrikg91d6ede2015-09-17 00:24:34 -0700582 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000583
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000584 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000585 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000586 if (cert->GetChain(chain.accept())) {
587 // This loop runs in reverse, i.e. from root to leaf, so that each
588 // certificate's issuer's report ID is known before the child certificate's
589 // report is generated. The root certificate does not have an issuer ID
590 // value.
591 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000592 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000593 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000594 }
595 }
596 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000597 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000598}
599
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000600StatsReport* StatsCollector::AddConnectionInfoReport(
601 const std::string& content_name, int component, int connection_id,
602 const StatsReport::Id& channel_report_id,
603 const cricket::ConnectionInfo& info) {
604 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
605 connection_id));
606 StatsReport* report = reports_.ReplaceOrAddNew(id);
607 report->set_timestamp(stats_gathering_started_);
608
609 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700610 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
611 {StatsReport::kStatsValueNameReceiving, info.receiving},
612 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000613 };
614 for (const auto& b : bools)
615 report->AddBoolean(b.name, b.value);
616
617 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
618 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
619 AddCandidateReport(info.local_candidate, true)->id());
620 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
621 AddCandidateReport(info.remote_candidate, false)->id());
622
623 const Int64ForAdd int64s[] = {
624 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
625 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
626 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
627 { StatsReport::kStatsValueNameRtt, info.rtt },
628 { StatsReport::kStatsValueNameSendPacketsDiscarded,
629 info.sent_discarded_packets },
630 };
631 for (const auto& i : int64s)
632 report->AddInt64(i.name, i.value);
633
634 report->AddString(StatsReport::kStatsValueNameLocalAddress,
635 info.local_candidate.address().ToString());
636 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
637 info.local_candidate.type());
638 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
639 info.remote_candidate.address().ToString());
640 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
641 info.remote_candidate.type());
642 report->AddString(StatsReport::kStatsValueNameTransportType,
643 info.local_candidate.protocol());
644
645 return report;
646}
647
648StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000649 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000650 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000651 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
652 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000653 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000654 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000655 report->set_timestamp(stats_gathering_started_);
656 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000657 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
658 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000659 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000660 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
661 candidate.address().ipaddr().ToString());
662 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
663 candidate.address().PortAsString());
664 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
665 candidate.priority());
666 report->AddString(StatsReport::kStatsValueNameCandidateType,
667 IceCandidateTypeToStatsType(candidate.type()));
668 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
669 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000670 }
671
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000672 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000673}
674
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700676 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000677
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000679 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700680 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000681 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000682 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000683 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700684 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685
deadbeefd59daf82015-10-14 15:02:44 -0700686 SessionStats stats;
deadbeefab9b2d12015-10-14 11:33:11 -0700687 if (!pc_->session()->GetTransportStats(&stats)) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000688 return;
689 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000691 // Store the proxy map away for use in SSRC reporting.
692 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
693 // signaling thread after fetching them on the worker thread, then just use
694 // the proxy map directly from the session stats.
695 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
696 // data.
697 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000698
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000699 for (const auto& transport_iter : stats.transport_stats) {
700 // Attempt to get a copy of the certificates from the transport and
701 // expose them in stats reports. All channels in a transport share the
702 // same local and remote certificates.
703 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000704 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200705 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700706 if (pc_->session()->GetLocalCertificate(
707 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200708 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000709 if (r)
710 local_cert_report_id = r->id();
711 }
712
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000713 rtc::scoped_ptr<rtc::SSLCertificate> cert;
deadbeefab9b2d12015-10-14 11:33:11 -0700714 if (pc_->session()->GetRemoteSSLCertificate(
715 transport_iter.second.transport_name, cert.accept())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000716 StatsReport* r = AddCertificateReports(cert.get());
717 if (r)
718 remote_cert_report_id = r->id();
719 }
720
721 for (const auto& channel_iter : transport_iter.second.channel_stats) {
722 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700723 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000724 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
725 channel_report->set_timestamp(stats_gathering_started_);
726 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
727 channel_iter.component);
728 if (local_cert_report_id.get()) {
729 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
730 local_cert_report_id);
731 }
732 if (remote_cert_report_id.get()) {
733 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
734 remote_cert_report_id);
735 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800736 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
737 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
738 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
739 channel_report->AddString(
740 StatsReport::kStatsValueNameSrtpCipher,
741 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000742 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800743 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
744 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
745 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
746 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700747 channel_report->AddString(
748 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800749 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000750 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000751
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000752 int connection_id = 0;
753 for (const cricket::ConnectionInfo& info :
754 channel_iter.connection_infos) {
755 StatsReport* connection_report = AddConnectionInfoReport(
756 transport_iter.first, channel_iter.component, connection_id++,
757 channel_report->id(), info);
758 if (info.best_connection) {
759 channel_report->AddId(
760 StatsReport::kStatsValueNameSelectedCandidatePairId,
761 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 }
763 }
764 }
765 }
766}
767
768void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700769 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000770
deadbeefab9b2d12015-10-14 11:33:11 -0700771 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 return;
773 }
774 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700775 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 LOG(LS_ERROR) << "Failed to get voice channel stats.";
777 return;
778 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000779
780 // TODO(tommi): The above code should run on the worker thread and post the
781 // results back to the signaling thread, where we can add data to the reports.
782 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
783
deadbeefab9b2d12015-10-14 11:33:11 -0700784 StatsReport::Id transport_id(GetTransportIdFromProxy(
785 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000786 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700788 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 return;
790 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000791
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000792 ExtractStatsFromList(voice_info.receivers, transport_id, this,
793 StatsReport::kReceive);
794 ExtractStatsFromList(voice_info.senders, transport_id, this,
795 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000796
797 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798}
799
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000800void StatsCollector::ExtractVideoInfo(
801 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700802 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000803
deadbeefab9b2d12015-10-14 11:33:11 -0700804 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000806
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700808 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 LOG(LS_ERROR) << "Failed to get video channel stats.";
810 return;
811 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000812
813 // TODO(tommi): The above code should run on the worker thread and post the
814 // results back to the signaling thread, where we can add data to the reports.
815 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
816
deadbeefab9b2d12015-10-14 11:33:11 -0700817 StatsReport::Id transport_id(GetTransportIdFromProxy(
818 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000819 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700821 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 return;
823 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000824 ExtractStatsFromList(video_info.receivers, transport_id, this,
825 StatsReport::kReceive);
826 ExtractStatsFromList(video_info.senders, transport_id, this,
827 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 if (video_info.bw_estimations.size() != 1) {
829 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
830 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000831 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
832 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000834 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 }
836}
837
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000838void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700839 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000840
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000841 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
842
deadbeefab9b2d12015-10-14 11:33:11 -0700843 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000844 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000845 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000846 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000847 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000848 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
849 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
850 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
851 report->AddString(StatsReport::kStatsValueNameState,
852 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000853 }
854}
855
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000856StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000857 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000858 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700859 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700860 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
861 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000862 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000863}
864
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000865void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700866 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000867 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000868 for (const auto& it : local_audio_tracks_) {
869 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200870 uint32_t ssrc = it.second;
871 StatsReport* report =
872 GetReport(StatsReport::kStatsReportTypeSsrc,
873 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000874 if (report == NULL) {
875 // This can happen if a local audio track is added to a stream on the
876 // fly and the report has not been set up yet. Do nothing in this case.
877 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
878 continue;
879 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000880
881 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000882 const StatsReport::Value* v =
883 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000884 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000885 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000886
jbauchbe24c942015-06-22 15:06:43 -0700887 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000888 UpdateReportFromAudioTrack(track, report);
889 }
890}
891
892void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
893 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700894 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700895 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896
andrew2fe1cb02015-11-27 17:27:35 -0800897 // Don't overwrite report values if they're not available.
898 int signal_level;
899 if (track->GetSignalLevel(&signal_level)) {
900 RTC_DCHECK_GE(signal_level, 0);
901 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
902 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000903
andrew2fe1cb02015-11-27 17:27:35 -0800904 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000905
andrew2fe1cb02015-11-27 17:27:35 -0800906 if (audio_processor.get()) {
907 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000908 audio_processor->GetStats(&stats);
909
andrew2fe1cb02015-11-27 17:27:35 -0800910 SetAudioProcessingStats(
911 report, stats.typing_noise_detected, stats.echo_return_loss,
912 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
913 stats.aec_quality_min, stats.echo_delay_std_ms);
914 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000915}
916
Peter Boström0c4e06b2015-10-07 12:23:21 +0200917bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
918 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000919 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700920 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000921 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700922 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000923 LOG(LS_WARNING) << "The SSRC " << ssrc
924 << " is not associated with a sending track";
925 return false;
926 }
927 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700928 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700929 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000930 LOG(LS_WARNING) << "The SSRC " << ssrc
931 << " is not associated with a receiving track";
932 return false;
933 }
934 }
935
936 return true;
937}
938
jbauchbe24c942015-06-22 15:06:43 -0700939void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700940 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700941
942 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
943
944 for (const auto& entry : track_ids_) {
945 StatsReport* report = entry.second;
946 report->set_timestamp(stats_gathering_started_);
947 }
jbauchbe24c942015-06-22 15:06:43 -0700948}
949
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000950void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000951 stats_gathering_started_ = 0;
952}
953
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954} // namespace webrtc