blob: b514b42feeaf4369e538167f4d466f6006a52d66 [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);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100204 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
205 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000206 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000208 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000209 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000210 const IntForAdd ints[] = {
211 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
212 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
213 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
214 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
215 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
216 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
217 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
218 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
219 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
220 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
221 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
222 info.min_playout_delay_ms },
223 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
224 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
225 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
226 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
227 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
228 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
229 };
230
231 for (const auto& i : ints)
232 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233}
234
235void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000236 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237
Peter Boströmb7d9a972015-12-18 16:01:11 +0100238 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
239 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000240 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
241 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000242 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
243 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000244 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
245 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000246
247 const IntForAdd ints[] = {
248 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
249 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000250 { StatsReport::kStatsValueNameEncodeUsagePercent,
251 info.encode_usage_percent },
252 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
253 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
254 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
255 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
256 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
257 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
258 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
259 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
260 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
261 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
262 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
263 };
264
265 for (const auto& i : ints)
266 report->AddInt(i.name, i.value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267}
268
269void ExtractStats(const cricket::BandwidthEstimationInfo& info,
270 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000271 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700273 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000275 report->set_timestamp(stats_gathering_started);
276 const IntForAdd ints[] = {
277 { StatsReport::kStatsValueNameAvailableSendBandwidth,
278 info.available_send_bandwidth },
279 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
280 info.available_recv_bandwidth },
281 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
282 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
283 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
284 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
285 };
286 for (const auto& i : ints)
287 report->AddInt(i.name, i.value);
288 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289}
290
wu@webrtc.org97077a32013-10-25 21:18:33 +0000291void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
292 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000293 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000294 // TODO(hta): Extract some stats here.
295}
296
297void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
298 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000299 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000300 // TODO(hta): Extract some stats here.
301}
302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000304// In order to use the template, the functions that are called from it,
305// ExtractStats and ExtractRemoteStats, must be defined and overloaded
306// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307template<typename T>
308void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000309 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000310 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000311 StatsReport::Direction direction) {
312 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200313 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000314 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000315 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000316 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
317 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000318 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000319 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000320
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000321 if (!d.remote_stats.empty()) {
322 report = collector->PrepareReport(false, ssrc, transport_id, direction);
323 if (report)
324 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000325 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000327}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328
329} // namespace
330
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000331const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
332 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
333 return STATSREPORT_LOCAL_PORT_TYPE;
334 }
335 if (candidate_type == cricket::STUN_PORT_TYPE) {
336 return STATSREPORT_STUN_PORT_TYPE;
337 }
338 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
339 return STATSREPORT_PRFLX_PORT_TYPE;
340 }
341 if (candidate_type == cricket::RELAY_PORT_TYPE) {
342 return STATSREPORT_RELAY_PORT_TYPE;
343 }
henrikg91d6ede2015-09-17 00:24:34 -0700344 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000345 return "unknown";
346}
347
348const char* AdapterTypeToStatsType(rtc::AdapterType type) {
349 switch (type) {
350 case rtc::ADAPTER_TYPE_UNKNOWN:
351 return "unknown";
352 case rtc::ADAPTER_TYPE_ETHERNET:
353 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
354 case rtc::ADAPTER_TYPE_WIFI:
355 return STATSREPORT_ADAPTER_TYPE_WIFI;
356 case rtc::ADAPTER_TYPE_CELLULAR:
357 return STATSREPORT_ADAPTER_TYPE_WWAN;
358 case rtc::ADAPTER_TYPE_VPN:
359 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000360 case rtc::ADAPTER_TYPE_LOOPBACK:
361 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000362 default:
henrikg91d6ede2015-09-17 00:24:34 -0700363 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000364 return "";
365 }
366}
367
deadbeefab9b2d12015-10-14 11:33:11 -0700368StatsCollector::StatsCollector(PeerConnection* pc)
369 : pc_(pc), stats_gathering_started_(0) {
370 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000371}
372
373StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700374 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375}
376
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000377double StatsCollector::GetTimeNow() {
378 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
379}
380
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381// Adds a MediaStream with tracks that can be used as a |selector| in a call
382// to GetStats.
383void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700384 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700385 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
387 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700388 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700390 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391}
392
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000393void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200394 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700395 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700396 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000397#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000398 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700399 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000400#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000401
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000402 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000403
404 // Create the kStatsReportTypeTrack report for the new track if there is no
405 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000406 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
407 audio_track->id()));
408 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000409 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000410 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000411 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000412 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000413}
414
415void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200416 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700417 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000418 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
419 local_audio_tracks_.end(),
420 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
421 return track.first == audio_track && track.second == ssrc;
422 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000423}
424
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000425void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700427 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700428 RTC_DCHECK(reports != NULL);
429 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000431 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
432
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000434 reports->reserve(reports_.size());
435 for (auto* r : reports_)
436 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000437 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 }
439
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000440 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700441 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000442 if (report)
443 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000445 report = reports_.Find(StatsReport::NewTypedId(
446 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000448 if (!report)
449 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000451 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452
453 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000454 for (const auto* r : reports_) {
455 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000457
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000458 const StatsReport::Value* v =
459 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000460 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000461 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463}
464
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000465void
466StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700467 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 double time_now = GetTimeNow();
469 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
470 // ms apart will be ignored.
471 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000472 if (stats_gathering_started_ != 0 &&
473 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 return;
475 }
476 stats_gathering_started_ = time_now;
477
deadbeefab9b2d12015-10-14 11:33:11 -0700478 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000479 // TODO(tommi): All of these hop over to the worker thread to fetch
480 // information. We could use an AsyncInvoker to run all of these and post
481 // the information back to the signaling thread where we can create and
482 // update stats reports. That would also clean up the threading story a bit
483 // since we'd be creating/updating the stats report objects consistently on
484 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 ExtractSessionInfo();
486 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000487 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000488 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700489 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 }
491}
492
deadbeefab9b2d12015-10-14 11:33:11 -0700493StatsReport* StatsCollector::PrepareReport(
494 bool local,
495 uint32_t ssrc,
496 const StatsReport::Id& transport_id,
497 StatsReport::Direction direction) {
498 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000499 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200500 local ? StatsReport::kStatsReportTypeSsrc
501 : StatsReport::kStatsReportTypeRemoteSsrc,
502 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000503 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504
xians@webrtc.org01bda202014-07-09 07:38:38 +0000505 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000507 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000508 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000509 // The ssrc is not used by any track or existing report, return NULL
510 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000512 }
513
514 // The ssrc is not used by any existing track. Keeps the old track id
515 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000516 const StatsReport::Value* v =
517 report->FindValue(StatsReport::kStatsValueNameTrackId);
518 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000519 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 }
521
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000522 if (!report)
523 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000525 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000526 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000528 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000529 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000531 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 return report;
533}
534
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000535StatsReport* StatsCollector::AddOneCertificateReport(
536 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
deadbeefab9b2d12015-10-14 11:33:11 -0700537 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000538
wu@webrtc.org4551b792013-10-09 15:37:36 +0000539 // TODO(bemasc): Move this computation to a helper class that caches these
540 // values to reduce CPU use in GetStats. This will require adding a fast
541 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000542
543 std::string digest_algorithm;
544 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000545 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000546
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000547 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
548 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000549
550 // SSLFingerprint::Create can fail if the algorithm returned by
551 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
552 // implementation of SSLCertificate::ComputeDigest. This currently happens
553 // with MD5- and SHA-224-signed certificates when linked to libNSS.
554 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000555 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000556
wu@webrtc.org4551b792013-10-09 15:37:36 +0000557 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
558
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000559 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000560 cert->ToDER(&der_buffer);
561 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000562 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
563 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000564
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000565 StatsReport::Id id(StatsReport::NewTypedId(
566 StatsReport::kStatsReportTypeCertificate, fingerprint));
567 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000568 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000569 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
570 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
571 digest_algorithm);
572 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000573 if (issuer)
574 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
575 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000576}
577
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000578StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000579 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700580 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000581 // Produces a chain of StatsReports representing this certificate and the rest
582 // of its chain, and adds those reports to |reports_|. The return value is
583 // the id of the leaf report. The provided cert must be non-null, so at least
584 // one report will always be provided and the returned string will never be
585 // empty.
henrikg91d6ede2015-09-17 00:24:34 -0700586 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000587
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000588 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000590 if (cert->GetChain(chain.accept())) {
591 // This loop runs in reverse, i.e. from root to leaf, so that each
592 // certificate's issuer's report ID is known before the child certificate's
593 // report is generated. The root certificate does not have an issuer ID
594 // value.
595 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000596 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000597 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000598 }
599 }
600 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000601 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000602}
603
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000604StatsReport* StatsCollector::AddConnectionInfoReport(
605 const std::string& content_name, int component, int connection_id,
606 const StatsReport::Id& channel_report_id,
607 const cricket::ConnectionInfo& info) {
608 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
609 connection_id));
610 StatsReport* report = reports_.ReplaceOrAddNew(id);
611 report->set_timestamp(stats_gathering_started_);
612
613 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700614 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
615 {StatsReport::kStatsValueNameReceiving, info.receiving},
616 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000617 };
618 for (const auto& b : bools)
619 report->AddBoolean(b.name, b.value);
620
621 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
622 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
623 AddCandidateReport(info.local_candidate, true)->id());
624 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
625 AddCandidateReport(info.remote_candidate, false)->id());
626
627 const Int64ForAdd int64s[] = {
628 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
629 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
630 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
631 { StatsReport::kStatsValueNameRtt, info.rtt },
632 { StatsReport::kStatsValueNameSendPacketsDiscarded,
633 info.sent_discarded_packets },
634 };
635 for (const auto& i : int64s)
636 report->AddInt64(i.name, i.value);
637
638 report->AddString(StatsReport::kStatsValueNameLocalAddress,
639 info.local_candidate.address().ToString());
640 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
641 info.local_candidate.type());
642 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
643 info.remote_candidate.address().ToString());
644 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
645 info.remote_candidate.type());
646 report->AddString(StatsReport::kStatsValueNameTransportType,
647 info.local_candidate.protocol());
648
649 return report;
650}
651
652StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000653 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000654 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000655 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
656 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000657 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000658 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000659 report->set_timestamp(stats_gathering_started_);
660 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000661 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
662 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000663 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000664 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
665 candidate.address().ipaddr().ToString());
666 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
667 candidate.address().PortAsString());
668 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
669 candidate.priority());
670 report->AddString(StatsReport::kStatsValueNameCandidateType,
671 IceCandidateTypeToStatsType(candidate.type()));
672 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
673 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000674 }
675
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000676 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000677}
678
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700680 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000681
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000683 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700684 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000685 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000686 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000687 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700688 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689
deadbeefd59daf82015-10-14 15:02:44 -0700690 SessionStats stats;
deadbeefab9b2d12015-10-14 11:33:11 -0700691 if (!pc_->session()->GetTransportStats(&stats)) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000692 return;
693 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000695 // Store the proxy map away for use in SSRC reporting.
696 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
697 // signaling thread after fetching them on the worker thread, then just use
698 // the proxy map directly from the session stats.
699 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
700 // data.
701 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000702
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000703 for (const auto& transport_iter : stats.transport_stats) {
704 // Attempt to get a copy of the certificates from the transport and
705 // expose them in stats reports. All channels in a transport share the
706 // same local and remote certificates.
707 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000708 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200709 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700710 if (pc_->session()->GetLocalCertificate(
711 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200712 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000713 if (r)
714 local_cert_report_id = r->id();
715 }
716
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000717 rtc::scoped_ptr<rtc::SSLCertificate> cert;
deadbeefab9b2d12015-10-14 11:33:11 -0700718 if (pc_->session()->GetRemoteSSLCertificate(
719 transport_iter.second.transport_name, cert.accept())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000720 StatsReport* r = AddCertificateReports(cert.get());
721 if (r)
722 remote_cert_report_id = r->id();
723 }
724
725 for (const auto& channel_iter : transport_iter.second.channel_stats) {
726 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700727 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000728 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
729 channel_report->set_timestamp(stats_gathering_started_);
730 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
731 channel_iter.component);
732 if (local_cert_report_id.get()) {
733 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
734 local_cert_report_id);
735 }
736 if (remote_cert_report_id.get()) {
737 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
738 remote_cert_report_id);
739 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800740 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
741 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
742 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
743 channel_report->AddString(
744 StatsReport::kStatsValueNameSrtpCipher,
745 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000746 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800747 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
748 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
749 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
750 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700751 channel_report->AddString(
752 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800753 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000754 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000755
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000756 int connection_id = 0;
757 for (const cricket::ConnectionInfo& info :
758 channel_iter.connection_infos) {
759 StatsReport* connection_report = AddConnectionInfoReport(
760 transport_iter.first, channel_iter.component, connection_id++,
761 channel_report->id(), info);
762 if (info.best_connection) {
763 channel_report->AddId(
764 StatsReport::kStatsValueNameSelectedCandidatePairId,
765 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 }
767 }
768 }
769 }
770}
771
772void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700773 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000774
deadbeefab9b2d12015-10-14 11:33:11 -0700775 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 return;
777 }
778 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700779 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 LOG(LS_ERROR) << "Failed to get voice channel stats.";
781 return;
782 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000783
784 // TODO(tommi): The above code should run on the worker thread and post the
785 // results back to the signaling thread, where we can add data to the reports.
786 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
787
deadbeefab9b2d12015-10-14 11:33:11 -0700788 StatsReport::Id transport_id(GetTransportIdFromProxy(
789 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000790 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700792 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 return;
794 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000795
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000796 ExtractStatsFromList(voice_info.receivers, transport_id, this,
797 StatsReport::kReceive);
798 ExtractStatsFromList(voice_info.senders, transport_id, this,
799 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000800
801 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802}
803
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000804void StatsCollector::ExtractVideoInfo(
805 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700806 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000807
deadbeefab9b2d12015-10-14 11:33:11 -0700808 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700812 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 LOG(LS_ERROR) << "Failed to get video channel stats.";
814 return;
815 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000816
817 // TODO(tommi): The above code should run on the worker thread and post the
818 // results back to the signaling thread, where we can add data to the reports.
819 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
820
deadbeefab9b2d12015-10-14 11:33:11 -0700821 StatsReport::Id transport_id(GetTransportIdFromProxy(
822 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000823 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700825 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 return;
827 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000828 ExtractStatsFromList(video_info.receivers, transport_id, this,
829 StatsReport::kReceive);
830 ExtractStatsFromList(video_info.senders, transport_id, this,
831 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 if (video_info.bw_estimations.size() != 1) {
833 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
834 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000835 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
836 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000838 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840}
841
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000842void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700843 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000844
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000845 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
846
deadbeefab9b2d12015-10-14 11:33:11 -0700847 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000848 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000849 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000850 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000851 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000852 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
853 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
854 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
855 report->AddString(StatsReport::kStatsValueNameState,
856 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000857 }
858}
859
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000860StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000861 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000862 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700863 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700864 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
865 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000866 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000867}
868
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000869void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700870 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000871 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000872 for (const auto& it : local_audio_tracks_) {
873 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200874 uint32_t ssrc = it.second;
875 StatsReport* report =
876 GetReport(StatsReport::kStatsReportTypeSsrc,
877 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000878 if (report == NULL) {
879 // This can happen if a local audio track is added to a stream on the
880 // fly and the report has not been set up yet. Do nothing in this case.
881 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
882 continue;
883 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000884
885 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000886 const StatsReport::Value* v =
887 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000888 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000889 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000890
jbauchbe24c942015-06-22 15:06:43 -0700891 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000892 UpdateReportFromAudioTrack(track, report);
893 }
894}
895
896void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
897 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700898 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700899 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000900
andrew2fe1cb02015-11-27 17:27:35 -0800901 // Don't overwrite report values if they're not available.
902 int signal_level;
903 if (track->GetSignalLevel(&signal_level)) {
904 RTC_DCHECK_GE(signal_level, 0);
905 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
906 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000907
andrew2fe1cb02015-11-27 17:27:35 -0800908 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000909
andrew2fe1cb02015-11-27 17:27:35 -0800910 if (audio_processor.get()) {
911 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000912 audio_processor->GetStats(&stats);
913
andrew2fe1cb02015-11-27 17:27:35 -0800914 SetAudioProcessingStats(
915 report, stats.typing_noise_detected, stats.echo_return_loss,
916 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
917 stats.aec_quality_min, stats.echo_delay_std_ms);
918 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000919}
920
Peter Boström0c4e06b2015-10-07 12:23:21 +0200921bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
922 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000923 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700924 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000925 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700926 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000927 LOG(LS_WARNING) << "The SSRC " << ssrc
928 << " is not associated with a sending track";
929 return false;
930 }
931 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700932 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700933 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000934 LOG(LS_WARNING) << "The SSRC " << ssrc
935 << " is not associated with a receiving track";
936 return false;
937 }
938 }
939
940 return true;
941}
942
jbauchbe24c942015-06-22 15:06:43 -0700943void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700944 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700945
946 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
947
948 for (const auto& entry : track_ids_) {
949 StatsReport* report = entry.second;
950 report->set_timestamp(stats_gathering_started_);
951 }
jbauchbe24c942015-06-22 15:06:43 -0700952}
953
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000954void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000955 stats_gathering_started_ = 0;
956}
957
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958} // namespace webrtc